From 0974e9e86523174e7a07d992b48d681aa07d4a7e Mon Sep 17 00:00:00 2001 From: Casper Rogild Storm <2248455+casperstorm@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:36:00 +0100 Subject: Added ability to customize accessory content. --- native/src/widget/pick_list.rs | 98 ++++++++++++++++++++++++++++++++++++------ src/widget.rs | 4 +- style/src/pick_list.rs | 2 - style/src/theme.rs | 2 - 4 files changed, 87 insertions(+), 19 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index a6459cd6..9fc92496 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -20,6 +20,60 @@ use std::borrow::Cow; pub use iced_style::pick_list::{Appearance, StyleSheet}; +/// The content to the right side of the [`PickList`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum AccessoryContent +where + Renderer: text::Renderer, +{ + /// Default accessory content. + Default { + /// Font size of the content. + size: Option, + }, + /// Custom accessory content. + Custom { + /// Font which will be used in the accessory content. + font: Renderer::Font, + /// Content which will be shown. + content: String, + /// Font size of the content. + size: Option, + }, + /// No accessory content will be shown. + None, +} + +impl Default for AccessoryContent +where + Renderer: text::Renderer, +{ + fn default() -> Self { + Self::Default { size: None } + } +} + +impl AccessoryContent +where + Renderer: text::Renderer, +{ + fn content(&self) -> Option<(Renderer::Font, String, Option)> { + match self { + AccessoryContent::Default { size } => Some(( + Renderer::ICON_FONT, + Renderer::ARROW_DOWN_ICON.to_string(), + *size, + )), + AccessoryContent::Custom { + font, + content, + size, + } => Some((font.clone(), content.clone(), *size)), + AccessoryContent::None => None, + } + } +} + /// A widget for selecting a single value from a list of options. #[allow(missing_debug_implementations)] pub struct PickList<'a, T, Message, Renderer> @@ -36,6 +90,7 @@ where padding: Padding, text_size: Option, font: Renderer::Font, + accessory_content: AccessoryContent, style: ::Style, } @@ -67,9 +122,10 @@ where placeholder: None, selected, width: Length::Shrink, - text_size: None, padding: Self::DEFAULT_PADDING, + text_size: None, font: Default::default(), + accessory_content: Default::default(), style: Default::default(), } } @@ -104,6 +160,15 @@ where self } + /// Sets the [`AccessoryContent`] of the [`PickList`]. + pub fn accessory_content( + mut self, + accessory_content: AccessoryContent, + ) -> Self { + self.accessory_content = accessory_content; + self + } + /// Sets the style of the [`PickList`]. pub fn style( mut self, @@ -541,19 +606,24 @@ pub fn draw( style.background, ); - renderer.fill_text(Text { - content: &Renderer::ARROW_DOWN_ICON.to_string(), - font: Renderer::ICON_FONT, - size: bounds.height * style.icon_size, - bounds: Rectangle { - x: bounds.x + bounds.width - f32::from(padding.horizontal()), - y: bounds.center_y(), - ..bounds - }, - color: style.text_color, - horizontal_alignment: alignment::Horizontal::Right, - vertical_alignment: alignment::Vertical::Center, - }); + if let Some((font, content, size)) = accessory_content.content() { + let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); + + renderer.fill_text(Text { + content: &content, + size, + font, + color: style.text_color, + bounds: Rectangle { + x: bounds.x + bounds.width - f32::from(padding.horizontal()), + y: bounds.center_y() - size / 2.0, + height: size, + ..bounds + }, + horizontal_alignment: alignment::Horizontal::Right, + vertical_alignment: alignment::Vertical::Top, + }); + } let label = selected.map(ToString::to_string); diff --git a/src/widget.rs b/src/widget.rs index 7c67a599..505ae799 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -80,7 +80,9 @@ pub mod pane_grid { pub mod pick_list { //! Display a dropdown list of selectable values. - pub use iced_native::widget::pick_list::{Appearance, StyleSheet}; + pub use iced_native::widget::pick_list::{ + AccessoryContent, Appearance, StyleSheet, + }; /// A widget allowing the selection of a single value from a list of options. pub type PickList<'a, T, Message, Renderer = crate::Renderer> = diff --git a/style/src/pick_list.rs b/style/src/pick_list.rs index 8d93dff2..103cc026 100644 --- a/style/src/pick_list.rs +++ b/style/src/pick_list.rs @@ -16,8 +16,6 @@ pub struct Appearance { pub border_width: f32, /// The border color of the pick list. pub border_color: Color, - /// The size of the arrow icon of the pick list. - pub icon_size: f32, } /// A set of rules that dictate the style of a container. diff --git a/style/src/theme.rs b/style/src/theme.rs index d7ebb827..3b38c2c1 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -537,7 +537,6 @@ impl pick_list::StyleSheet for Theme { border_radius: 2.0, border_width: 1.0, border_color: palette.background.strong.color, - icon_size: 0.7, } } PickList::Custom(custom, _) => custom.active(self), @@ -556,7 +555,6 @@ impl pick_list::StyleSheet for Theme { border_radius: 2.0, border_width: 1.0, border_color: palette.primary.strong.color, - icon_size: 0.7, } } PickList::Custom(custom, _) => custom.active(self), -- cgit From f727fa09cd18bb5597094131f106c88ddcdd2ab3 Mon Sep 17 00:00:00 2001 From: Casper Rogild Storm <2248455+casperstorm@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:52:52 +0100 Subject: Correctly pass accessory content. --- native/src/widget/pick_list.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 9fc92496..31f7bf5b 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -279,6 +279,7 @@ where &self.font, self.placeholder.as_deref(), self.selected.as_ref(), + &self.accessory_content, &self.style, ) } @@ -580,6 +581,7 @@ pub fn draw( font: &Renderer::Font, placeholder: Option<&str>, selected: Option<&T>, + accessory_content: &AccessoryContent, style: &::Style, ) where Renderer: text::Renderer, -- cgit From fe5ab1ee87ede2d80212aa58724fd5ed463f58e4 Mon Sep 17 00:00:00 2001 From: Casper Rogild Storm <2248455+casperstorm@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:06:45 +0100 Subject: Added accessory_content_color to appreance. --- native/src/widget/pick_list.rs | 2 +- style/src/pick_list.rs | 2 ++ style/src/theme.rs | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 31f7bf5b..6a2061b9 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -615,7 +615,7 @@ pub fn draw( content: &content, size, font, - color: style.text_color, + color: style.accessory_content_color, bounds: Rectangle { x: bounds.x + bounds.width - f32::from(padding.horizontal()), y: bounds.center_y() - size / 2.0, diff --git a/style/src/pick_list.rs b/style/src/pick_list.rs index 103cc026..877a58da 100644 --- a/style/src/pick_list.rs +++ b/style/src/pick_list.rs @@ -8,6 +8,8 @@ pub struct Appearance { pub text_color: Color, /// The placeholder [`Color`] of the pick list. pub placeholder_color: Color, + /// The accessory content [`Color`] of the pick list. + pub accessory_content_color: Color, /// The [`Background`] of the pick list. pub background: Background, /// The border radius of the pick list. diff --git a/style/src/theme.rs b/style/src/theme.rs index 3b38c2c1..81bcfa67 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -534,6 +534,7 @@ impl pick_list::StyleSheet for Theme { text_color: palette.background.weak.text, background: palette.background.weak.color.into(), placeholder_color: palette.background.strong.color, + accessory_content_color: palette.background.weak.text, border_radius: 2.0, border_width: 1.0, border_color: palette.background.strong.color, @@ -552,6 +553,7 @@ impl pick_list::StyleSheet for Theme { text_color: palette.background.weak.text, background: palette.background.weak.color.into(), placeholder_color: palette.background.strong.color, + accessory_content_color: palette.background.weak.text, border_radius: 2.0, border_width: 1.0, border_color: palette.primary.strong.color, -- cgit From 39f49186cefa688a1cc80ed754ec4028c642636a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 14 Dec 2022 03:29:20 +0100 Subject: Rename `pick_list::AccessoryContent` to `Handle` ... and rename `Default` variant to `Arrow`. --- native/src/widget/pick_list.rs | 61 ++++++++++++++++++++---------------------- src/widget.rs | 4 +-- style/src/pick_list.rs | 4 +-- style/src/theme.rs | 4 +-- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 6a2061b9..43ae7ebb 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -20,56 +20,56 @@ use std::borrow::Cow; pub use iced_style::pick_list::{Appearance, StyleSheet}; -/// The content to the right side of the [`PickList`]. +/// The handle to the right side of the [`PickList`]. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum AccessoryContent +pub enum Handle where Renderer: text::Renderer, { - /// Default accessory content. - Default { + /// Displays an arrow icon (▼). + /// + /// This is the default. + Arrow { /// Font size of the content. size: Option, }, - /// Custom accessory content. + /// A custom handle. Custom { - /// Font which will be used in the accessory content. + /// Font that will be used to display the `text`, font: Renderer::Font, - /// Content which will be shown. - content: String, + /// Text that will be shown. + text: String, /// Font size of the content. size: Option, }, - /// No accessory content will be shown. + /// No handle will be shown. None, } -impl Default for AccessoryContent +impl Default for Handle where Renderer: text::Renderer, { fn default() -> Self { - Self::Default { size: None } + Self::Arrow { size: None } } } -impl AccessoryContent +impl Handle where Renderer: text::Renderer, { fn content(&self) -> Option<(Renderer::Font, String, Option)> { match self { - AccessoryContent::Default { size } => Some(( + Self::Arrow { size } => Some(( Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON.to_string(), *size, )), - AccessoryContent::Custom { - font, - content, - size, - } => Some((font.clone(), content.clone(), *size)), - AccessoryContent::None => None, + Self::Custom { font, text, size } => { + Some((font.clone(), text.clone(), *size)) + } + Self::None => None, } } } @@ -90,7 +90,7 @@ where padding: Padding, text_size: Option, font: Renderer::Font, - accessory_content: AccessoryContent, + handle: Handle, style: ::Style, } @@ -125,7 +125,7 @@ where padding: Self::DEFAULT_PADDING, text_size: None, font: Default::default(), - accessory_content: Default::default(), + handle: Default::default(), style: Default::default(), } } @@ -160,12 +160,9 @@ where self } - /// Sets the [`AccessoryContent`] of the [`PickList`]. - pub fn accessory_content( - mut self, - accessory_content: AccessoryContent, - ) -> Self { - self.accessory_content = accessory_content; + /// Sets the [`Handle`] of the [`PickList`]. + pub fn handle(mut self, handle: Handle) -> Self { + self.handle = handle; self } @@ -279,7 +276,7 @@ where &self.font, self.placeholder.as_deref(), self.selected.as_ref(), - &self.accessory_content, + &self.handle, &self.style, ) } @@ -581,7 +578,7 @@ pub fn draw( font: &Renderer::Font, placeholder: Option<&str>, selected: Option<&T>, - accessory_content: &AccessoryContent, + handle: &Handle, style: &::Style, ) where Renderer: text::Renderer, @@ -608,14 +605,14 @@ pub fn draw( style.background, ); - if let Some((font, content, size)) = accessory_content.content() { + if let Some((font, text, size)) = handle.content() { let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); renderer.fill_text(Text { - content: &content, + content: &text, size, font, - color: style.accessory_content_color, + color: style.handle_color, bounds: Rectangle { x: bounds.x + bounds.width - f32::from(padding.horizontal()), y: bounds.center_y() - size / 2.0, diff --git a/src/widget.rs b/src/widget.rs index 505ae799..acc9da9a 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -80,9 +80,7 @@ pub mod pane_grid { pub mod pick_list { //! Display a dropdown list of selectable values. - pub use iced_native::widget::pick_list::{ - AccessoryContent, Appearance, StyleSheet, - }; + pub use iced_native::widget::pick_list::{Appearance, Handle, StyleSheet}; /// A widget allowing the selection of a single value from a list of options. pub type PickList<'a, T, Message, Renderer = crate::Renderer> = diff --git a/style/src/pick_list.rs b/style/src/pick_list.rs index 877a58da..11e13b01 100644 --- a/style/src/pick_list.rs +++ b/style/src/pick_list.rs @@ -8,8 +8,8 @@ pub struct Appearance { pub text_color: Color, /// The placeholder [`Color`] of the pick list. pub placeholder_color: Color, - /// The accessory content [`Color`] of the pick list. - pub accessory_content_color: Color, + /// The handle [`Color`] of the pick list. + pub handle_color: Color, /// The [`Background`] of the pick list. pub background: Background, /// The border radius of the pick list. diff --git a/style/src/theme.rs b/style/src/theme.rs index 81bcfa67..bd0ae28e 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -534,7 +534,7 @@ impl pick_list::StyleSheet for Theme { text_color: palette.background.weak.text, background: palette.background.weak.color.into(), placeholder_color: palette.background.strong.color, - accessory_content_color: palette.background.weak.text, + handle_color: palette.background.weak.text, border_radius: 2.0, border_width: 1.0, border_color: palette.background.strong.color, @@ -553,7 +553,7 @@ impl pick_list::StyleSheet for Theme { text_color: palette.background.weak.text, background: palette.background.weak.color.into(), placeholder_color: palette.background.strong.color, - accessory_content_color: palette.background.weak.text, + handle_color: palette.background.weak.text, border_radius: 2.0, border_width: 1.0, border_color: palette.primary.strong.color, -- cgit