From e8bdd33596d49a946271dd8c33eb4ace0a531a53 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Wed, 8 Nov 2023 09:25:16 -0500 Subject: [PATCH] Fixed Selection list --- Cargo.toml | 4 +-- examples/selection_list/src/main.rs | 6 ++--- src/native/selection_list.rs | 36 +++++++++++++++++---------- src/native/selection_list/list.rs | 38 ++++++++++++++++------------- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc379ac9..754ea434 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,7 +51,7 @@ default = [ "tabs", "time_picker", "wrap", - #"selection_list", + "selection_list", #"split", #"menu", #"quad", @@ -98,7 +98,7 @@ members = [ "examples/time_picker", "examples/wrap", "examples/number_input", - #"examples/selection_list", + "examples/selection_list", #"examples/split", #"examples/split_scroller", #"examples/menu", diff --git a/examples/selection_list/src/main.rs b/examples/selection_list/src/main.rs index 11fc0bbf..cb4e11af 100644 --- a/examples/selection_list/src/main.rs +++ b/examples/selection_list/src/main.rs @@ -61,10 +61,10 @@ impl Sandbox for Example { self.manual_select = None; } Message::ManualSelection => { - if let Some(option) = self.vec.get(0) { + if let Some(option) = self.vec.get(2) { self.selected_language = option.to_owned(); - self.selected_index = 0; - self.manual_select = Some(0); + self.selected_index = 2; + self.manual_select = Some(2); } } } diff --git a/src/native/selection_list.rs b/src/native/selection_list.rs index cdbfcc38..7b53a0ac 100644 --- a/src/native/selection_list.rs +++ b/src/native/selection_list.rs @@ -5,13 +5,17 @@ use crate::style::selection_list::StyleSheet; use iced_widget::{ container, core::{ - self, event, + self, + alignment::{Horizontal, Vertical}, + event, layout::{Limits, Node}, mouse::{self, Cursor}, renderer, + text::{Paragraph, Text}, widget::Tree, - Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Size, Widget, + Clipboard, Element, Event, Layout, Length, Pixels, Rectangle, Shell, Size, Widget, }, + graphics, runtime::Font, scrollable, text, text::LineHeight, @@ -168,7 +172,7 @@ where Length::Shrink } - fn layout(&self, renderer: &Renderer, limits: &Limits) -> Node { + fn layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node { use std::f32; let limits = limits.width(self.width).height(self.height); @@ -179,16 +183,20 @@ where labels .map(|label| { - let size = renderer.measure( - &label, - self.text_size, - LineHeight::default(), - self.font, - Size::new(f32::INFINITY, f32::INFINITY), - text::Shaping::Advanced, - ); + let text = Text { + content: &label, + size: Pixels(self.text_size), + line_height: LineHeight::default(), + bounds: Size::INFINITY, + font: self.font, + horizontal_alignment: Horizontal::Left, + vertical_alignment: Vertical::Top, + shaping: text::Shaping::Advanced, + }; - size.width.round() as u32 + self.padding as u32 * 2 + let mut paragraph = graphics::text::Paragraph::new(); + paragraph.update(text); + paragraph.min_bounds().width.round() as u32 + self.padding as u32 * 2 }) .max() .unwrap_or(100) @@ -198,7 +206,9 @@ where let limits = limits.max_width(max_width as f32 + self.padding * 2.0); - let content = self.container.layout(renderer, &limits); + let content = self + .container + .layout(&mut tree.children[0], renderer, &limits); let size = limits.resolve(content.size()); Node::with_children(size, vec![content]) } diff --git a/src/native/selection_list/list.rs b/src/native/selection_list/list.rs index 2de9a5bc..4c36dad1 100644 --- a/src/native/selection_list/list.rs +++ b/src/native/selection_list/list.rs @@ -13,7 +13,8 @@ use iced_widget::{ tree::{State, Tag}, Tree, }, - Clipboard, Color, Element, Event, Layout, Length, Rectangle, Shell, Size, Widget, + Clipboard, Color, Element, Event, Layout, Length, Pixels, Point, Rectangle, Shell, Size, + Widget, }, text::LineHeight, }; @@ -109,7 +110,12 @@ where Length::Shrink } - fn layout(&self, _renderer: &Renderer, limits: &layout::Limits) -> layout::Node { + fn layout( + &self, + _tree: &mut Tree, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { use std::f32; let limits = limits.height(Length::Fill).width(Length::Fill); @@ -255,22 +261,20 @@ where theme.style(&self.style).text_color }; - renderer.fill_text(core::text::Text { - content: &option.to_string(), - bounds: Rectangle { - x: bounds.x, - y: bounds.center_y(), - width: f32::INFINITY, - ..bounds + renderer.fill_text( + core::text::Text { + content: &option.to_string(), + bounds: Size::new(f32::INFINITY, bounds.height), + size: Pixels(self.text_size), + font: self.font, + horizontal_alignment: Horizontal::Left, + vertical_alignment: Vertical::Center, + line_height: LineHeight::default(), + shaping: iced_widget::text::Shaping::Advanced, }, - size: self.text_size, - color: text_color, - font: self.font, - horizontal_alignment: Horizontal::Left, - vertical_alignment: Vertical::Center, - line_height: LineHeight::default(), - shaping: iced_widget::text::Shaping::Advanced, - }); + Point::new(bounds.x, bounds.center_y()), + text_color, + ); } } }