From 29326215ccf13e1d1e25bf3bf5ada007856bff69 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 03:48:08 +0100 Subject: Simplify theming for `Container` widget --- widget/src/combo_box.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index e3862174..665b1da9 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -299,7 +299,7 @@ impl<'a, T, Message, Theme, Renderer> Widget where T: Display + Clone + 'static, Message: Clone, - Theme: container::StyleSheet + Theme: container::Style + text_input::StyleSheet + scrollable::StyleSheet + menu::StyleSheet, @@ -719,7 +719,7 @@ impl<'a, T, Message, Theme, Renderer> where T: Display + Clone + 'static, Message: Clone + 'a, - Theme: container::StyleSheet + Theme: container::Style + text_input::StyleSheet + scrollable::StyleSheet + menu::StyleSheet -- cgit From d681aaa57e3106cf0ce90b74ade040ca7bb97832 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 04:42:25 +0100 Subject: Simplify theming for `Scrollable` widget --- widget/src/combo_box.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 665b1da9..0cca8d56 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -301,7 +301,7 @@ where Message: Clone, Theme: container::Style + text_input::StyleSheet - + scrollable::StyleSheet + + scrollable::Tradition + menu::StyleSheet, Renderer: text::Renderer, { @@ -721,7 +721,7 @@ where Message: Clone + 'a, Theme: container::Style + text_input::StyleSheet - + scrollable::StyleSheet + + scrollable::Tradition + menu::StyleSheet + 'a, Renderer: text::Renderer + 'a, -- cgit From 704ec9cb5cdc1d44f2df2f15de700b0af330b1d7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 15:53:59 +0100 Subject: Simplify theming for `TextInput` widget --- widget/src/combo_box.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 0cca8d56..2ecf799d 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -32,7 +32,7 @@ pub struct ComboBox< Theme = crate::Theme, Renderer = crate::Renderer, > where - Theme: text_input::StyleSheet + menu::StyleSheet, + Theme: text_input::Style + menu::StyleSheet, Renderer: text::Renderer, { state: &'a State, @@ -51,7 +51,7 @@ pub struct ComboBox< impl<'a, T, Message, Theme, Renderer> ComboBox<'a, T, Message, Theme, Renderer> where T: std::fmt::Display + Clone, - Theme: text_input::StyleSheet + menu::StyleSheet, + Theme: text_input::Style + menu::StyleSheet, Renderer: text::Renderer, { /// Creates a new [`ComboBox`] with the given list of options, a placeholder, @@ -121,20 +121,17 @@ where // TODO: Define its own `StyleSheet` trait pub fn style(mut self, style: S) -> Self where - S: Into<::Style> - + Into<::Style> - + Clone, + S: Into<::Style>, { - self.menu_style = style.clone().into(); - self.text_input = self.text_input.style(style); + self.menu_style = style.into(); self } /// Sets the style of the [`TextInput`] of the [`ComboBox`]. - pub fn text_input_style(mut self, style: S) -> Self - where - S: Into<::Style> + Clone, - { + pub fn text_input_style( + mut self, + style: fn(&Theme, text_input::Status) -> text_input::Appearance, + ) -> Self { self.text_input = self.text_input.style(style); self } @@ -300,8 +297,8 @@ where T: Display + Clone + 'static, Message: Clone, Theme: container::Style - + text_input::StyleSheet - + scrollable::Tradition + + text_input::Style + + scrollable::Style + menu::StyleSheet, Renderer: text::Renderer, { @@ -720,8 +717,8 @@ where T: Display + Clone + 'static, Message: Clone + 'a, Theme: container::Style - + text_input::StyleSheet - + scrollable::Tradition + + text_input::Style + + scrollable::Style + menu::StyleSheet + 'a, Renderer: text::Renderer + 'a, -- cgit From 597a41cea73f078eda04eb3ff40cfda5d37d6135 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 17:08:28 +0100 Subject: Simplify theming for `PickList`, `ComboBox`, and `Menu` widgets --- widget/src/combo_box.rs | 89 +++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 33 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 2ecf799d..533daab2 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -1,4 +1,5 @@ //! Display a dropdown list of searchable and selectable options. +use crate::container; use crate::core::event::{self, Event}; use crate::core::keyboard; use crate::core::keyboard::key; @@ -13,8 +14,10 @@ use crate::core::{ Clipboard, Element, Length, Padding, Rectangle, Shell, Size, Vector, }; use crate::overlay::menu; +use crate::scrollable; +use crate::style::Theme; use crate::text::LineHeight; -use crate::{container, scrollable, text_input, TextInput}; +use crate::text_input::{self, TextInput}; use std::cell::RefCell; use std::fmt::Display; @@ -32,7 +35,6 @@ pub struct ComboBox< Theme = crate::Theme, Renderer = crate::Renderer, > where - Theme: text_input::Style + menu::StyleSheet, Renderer: text::Renderer, { state: &'a State, @@ -43,7 +45,7 @@ pub struct ComboBox< on_option_hovered: Option Message>>, on_close: Option, on_input: Option Message>>, - menu_style: ::Style, + menu_style: menu::Style, padding: Padding, size: Option, } @@ -51,7 +53,6 @@ pub struct ComboBox< impl<'a, T, Message, Theme, Renderer> ComboBox<'a, T, Message, Theme, Renderer> where T: std::fmt::Display + Clone, - Theme: text_input::Style + menu::StyleSheet, Renderer: text::Renderer, { /// Creates a new [`ComboBox`] with the given list of options, a placeholder, @@ -62,9 +63,16 @@ where placeholder: &str, selection: Option<&T>, on_selected: impl Fn(T) -> Message + 'static, - ) -> Self { + ) -> Self + where + Theme: text_input::Style, + Style: Default, + { + let style = Style::::default(); + let text_input = TextInput::new(placeholder, &state.value()) - .on_input(TextInputEvent::TextChanged); + .on_input(TextInputEvent::TextChanged) + .style(style.text_input); let selection = selection.map(T::to_string).unwrap_or_default(); @@ -77,7 +85,7 @@ where on_option_hovered: None, on_input: None, on_close: None, - menu_style: Default::default(), + menu_style: style.menu, padding: text_input::DEFAULT_PADDING, size: None, } @@ -118,21 +126,11 @@ where } /// Sets the style of the [`ComboBox`]. - // TODO: Define its own `StyleSheet` trait - pub fn style(mut self, style: S) -> Self - where - S: Into<::Style>, - { - self.menu_style = style.into(); - self - } + pub fn style(mut self, style: impl Into>) -> Self { + let style = style.into(); - /// Sets the style of the [`TextInput`] of the [`ComboBox`]. - pub fn text_input_style( - mut self, - style: fn(&Theme, text_input::Status) -> text_input::Appearance, - ) -> Self { - self.text_input = self.text_input.style(style); + self.text_input = self.text_input.style(style.text_input); + self.menu_style = style.menu; self } @@ -296,10 +294,7 @@ impl<'a, T, Message, Theme, Renderer> Widget where T: Display + Clone + 'static, Message: Clone, - Theme: container::Style - + text_input::Style - + scrollable::Style - + menu::StyleSheet, + Theme: scrollable::Style + container::Style, Renderer: text::Renderer, { fn size(&self) -> Size { @@ -676,7 +671,7 @@ where self.state.sync_filtered_options(filtered_options); - let mut menu = menu::Menu::new( + let mut menu = menu::Menu::with_style( menu, &filtered_options.options, hovered_option, @@ -690,10 +685,10 @@ where (self.on_selected)(x) }, self.on_option_hovered.as_deref(), + self.menu_style, ) .width(bounds.width) - .padding(self.padding) - .style(self.menu_style.clone()); + .padding(self.padding); if let Some(font) = self.font { menu = menu.font(font); @@ -716,11 +711,7 @@ impl<'a, T, Message, Theme, Renderer> where T: Display + Clone + 'static, Message: Clone + 'a, - Theme: container::Style - + text_input::Style - + scrollable::Style - + menu::StyleSheet - + 'a, + Theme: scrollable::Style + container::Style + 'a, Renderer: text::Renderer + 'a, { fn from(combo_box: ComboBox<'a, T, Message, Theme, Renderer>) -> Self { @@ -772,3 +763,35 @@ where }) .collect() } + +/// The appearance of a [`ComboBox`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style { + /// The style of the [`TextInput`] of the [`ComboBox`]. + text_input: fn(&Theme, text_input::Status) -> text_input::Appearance, + + /// The style of the [`Menu`] of the [`ComboBox`]. + menu: menu::Style, +} + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + default() + } +} + +/// The default style of a [`ComboBox`]. +pub fn default() -> Style { + Style { + text_input: text_input::default, + menu: menu::Style::default(), + } +} -- cgit From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/combo_box.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 533daab2..1140f1c6 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -1,5 +1,4 @@ //! Display a dropdown list of searchable and selectable options. -use crate::container; use crate::core::event::{self, Event}; use crate::core::keyboard; use crate::core::keyboard::key; @@ -14,7 +13,6 @@ use crate::core::{ Clipboard, Element, Length, Padding, Rectangle, Shell, Size, Vector, }; use crate::overlay::menu; -use crate::scrollable; use crate::style::Theme; use crate::text::LineHeight; use crate::text_input::{self, TextInput}; @@ -65,14 +63,16 @@ where on_selected: impl Fn(T) -> Message + 'static, ) -> Self where - Theme: text_input::Style, Style: Default, { let style = Style::::default(); - let text_input = TextInput::new(placeholder, &state.value()) - .on_input(TextInputEvent::TextChanged) - .style(style.text_input); + let text_input = TextInput::with_style( + placeholder, + &state.value(), + style.text_input, + ) + .on_input(TextInputEvent::TextChanged); let selection = selection.map(T::to_string).unwrap_or_default(); @@ -294,7 +294,6 @@ impl<'a, T, Message, Theme, Renderer> Widget where T: Display + Clone + 'static, Message: Clone, - Theme: scrollable::Style + container::Style, Renderer: text::Renderer, { fn size(&self) -> Size { @@ -711,7 +710,7 @@ impl<'a, T, Message, Theme, Renderer> where T: Display + Clone + 'static, Message: Clone + 'a, - Theme: scrollable::Style + container::Style + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from(combo_box: ComboBox<'a, T, Message, Theme, Renderer>) -> Self { -- cgit From 905f2160e6eb7504f52d9bd62c7bfa42c8ec2902 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:14:41 +0100 Subject: Move `Theme` type to `iced_core` --- widget/src/combo_box.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 1140f1c6..62c19137 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -10,10 +10,9 @@ use crate::core::text; use crate::core::time::Instant; use crate::core::widget::{self, Widget}; use crate::core::{ - Clipboard, Element, Length, Padding, Rectangle, Shell, Size, Vector, + Clipboard, Element, Length, Padding, Rectangle, Shell, Size, Theme, Vector, }; use crate::overlay::menu; -use crate::style::Theme; use crate::text::LineHeight; use crate::text_input::{self, TextInput}; -- cgit From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/combo_box.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 62c19137..44830d8a 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -62,9 +62,9 @@ where on_selected: impl Fn(T) -> Message + 'static, ) -> Self where - Style: Default, + Theme: DefaultStyle, { - let style = Style::::default(); + let style = Theme::default_style(); let text_input = TextInput::with_style( placeholder, @@ -762,7 +762,7 @@ where .collect() } -/// The appearance of a [`ComboBox`]. +/// The style of a [`ComboBox`]. #[derive(Debug, PartialEq, Eq)] pub struct Style { /// The style of the [`TextInput`] of the [`ComboBox`]. @@ -772,6 +772,14 @@ pub struct Style { menu: menu::Style, } +impl Style { + /// The default style of a [`ComboBox`]. + pub const DEFAULT: Self = Self { + text_input: text_input::default, + menu: menu::Style::::DEFAULT, + }; +} + impl Clone for Style { fn clone(&self) -> Self { *self @@ -780,16 +788,14 @@ impl Clone for Style { impl Copy for Style {} -impl Default for Style { - fn default() -> Self { - default() - } +/// The default style of a [`ComboBox`]. +pub trait DefaultStyle: Sized { + /// Returns the default style of a [`ComboBox`]. + fn default_style() -> Style; } -/// The default style of a [`ComboBox`]. -pub fn default() -> Style { - Style { - text_input: text_input::default, - menu: menu::Style::default(), +impl DefaultStyle for Theme { + fn default_style() -> Style { + Style::::DEFAULT } } -- cgit From e11776055dd6c79298c17e00a86ba43aba917bba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:13:29 +0100 Subject: Make fields of `Style` structs public --- widget/src/combo_box.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 44830d8a..933a0fac 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -766,10 +766,10 @@ where #[derive(Debug, PartialEq, Eq)] pub struct Style { /// The style of the [`TextInput`] of the [`ComboBox`]. - text_input: fn(&Theme, text_input::Status) -> text_input::Appearance, + pub text_input: fn(&Theme, text_input::Status) -> text_input::Appearance, /// The style of the [`Menu`] of the [`ComboBox`]. - menu: menu::Style, + pub menu: menu::Style, } impl Style { -- cgit From 288025f5143f4e3f8bc5af36e86f7afa7f07a4c7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Mar 2024 13:34:36 +0100 Subject: Inline helper functions in `widget` modules --- widget/src/combo_box.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'widget/src/combo_box.rs') diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 933a0fac..bddf2789 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -717,8 +717,7 @@ where } } -/// Search list of options for a given query. -pub fn search<'a, T, A>( +fn search<'a, T, A>( options: impl IntoIterator + 'a, option_matchers: impl IntoIterator + 'a, query: &'a str, @@ -745,8 +744,7 @@ where }) } -/// Build matchers from given list of options. -pub fn build_matchers<'a, T>( +fn build_matchers<'a, T>( options: impl IntoIterator + 'a, ) -> Vec where @@ -769,6 +767,8 @@ pub struct Style { pub text_input: fn(&Theme, text_input::Status) -> text_input::Appearance, /// The style of the [`Menu`] of the [`ComboBox`]. + /// + /// [`Menu`]: menu::Menu pub menu: menu::Style, } -- cgit