diff options
author | 2024-03-07 20:11:32 +0100 | |
---|---|---|
committer | 2024-03-07 20:11:32 +0100 | |
commit | 833538ee7f3a60a839304762dfc29b0881d19094 (patch) | |
tree | 7afbc69659c95f9cbec58c938f1939cca3290b04 /widget/src | |
parent | 44f002f64a9d53040f09affe69bd92675e302e16 (diff) | |
download | iced-833538ee7f3a60a839304762dfc29b0881d19094.tar.gz iced-833538ee7f3a60a839304762dfc29b0881d19094.tar.bz2 iced-833538ee7f3a60a839304762dfc29b0881d19094.zip |
Leverage `DefaultStyle` traits instead of `Default`
Diffstat (limited to 'widget/src')
-rw-r--r-- | widget/src/button.rs | 33 | ||||
-rw-r--r-- | widget/src/checkbox.rs | 33 | ||||
-rw-r--r-- | widget/src/combo_box.rs | 30 | ||||
-rw-r--r-- | widget/src/container.rs | 49 | ||||
-rw-r--r-- | widget/src/helpers.rs | 36 | ||||
-rw-r--r-- | widget/src/overlay/menu.rs | 39 | ||||
-rw-r--r-- | widget/src/pane_grid.rs | 33 | ||||
-rw-r--r-- | widget/src/pane_grid/content.rs | 8 | ||||
-rw-r--r-- | widget/src/pane_grid/title_bar.rs | 6 | ||||
-rw-r--r-- | widget/src/pick_list.rs | 35 | ||||
-rw-r--r-- | widget/src/progress_bar.rs | 31 | ||||
-rw-r--r-- | widget/src/qr_code.rs | 31 | ||||
-rw-r--r-- | widget/src/radio.rs | 33 | ||||
-rw-r--r-- | widget/src/rule.rs | 37 | ||||
-rw-r--r-- | widget/src/scrollable.rs | 37 | ||||
-rw-r--r-- | widget/src/slider.rs | 31 | ||||
-rw-r--r-- | widget/src/svg.rs | 35 | ||||
-rw-r--r-- | widget/src/text_editor.rs | 31 | ||||
-rw-r--r-- | widget/src/text_input.rs | 31 | ||||
-rw-r--r-- | widget/src/toggler.rs | 31 | ||||
-rw-r--r-- | widget/src/tooltip.rs | 6 | ||||
-rw-r--r-- | widget/src/vertical_slider.rs | 8 |
22 files changed, 310 insertions, 334 deletions
diff --git a/widget/src/button.rs b/widget/src/button.rs index 5fa62280..f9859353 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -68,7 +68,7 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { let content = content.into(); let size = content.as_widget().size_hint(); @@ -80,7 +80,7 @@ where height: size.height.fluid(), padding: Padding::new(5.0), clip: false, - style: Style::default(), + style: Theme::default_style(), } } @@ -121,7 +121,7 @@ where /// Sets the style variant of this [`Button`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } @@ -301,7 +301,7 @@ where Status::Active }; - let styling = (self.style.0)(theme, status); + let styling = (self.style)(theme, status); if styling.background.is_some() || styling.border.width > 0.0 @@ -424,26 +424,23 @@ impl std::default::Default for Appearance { } /// The style of a [`Button`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Button`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Button`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(primary) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + primary } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index a297627b..c837ab3f 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -71,7 +71,7 @@ where /// * a boolean describing whether the [`Checkbox`] is checked or not pub fn new(label: impl Into<String>, is_checked: bool) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Checkbox { is_checked, @@ -91,7 +91,7 @@ where line_height: text::LineHeight::default(), shaping: text::Shaping::Basic, }, - style: Style::default(), + style: Theme::default_style(), } } @@ -175,7 +175,7 @@ where /// Sets the style of the [`Checkbox`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -301,7 +301,7 @@ where Status::Active { is_checked } }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); { let layout = children.next().unwrap(); @@ -424,26 +424,23 @@ pub struct Appearance { } /// The style of a [`Checkbox`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Checkbox`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Checkbox`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(primary) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + primary } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } 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<Theme>: Default, + Theme: DefaultStyle, { - let style = Style::<Theme>::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<Theme> { /// The style of the [`TextInput`] of the [`ComboBox`]. @@ -772,6 +772,14 @@ pub struct Style<Theme> { menu: menu::Style<Theme>, } +impl Style<Theme> { + /// The default style of a [`ComboBox`]. + pub const DEFAULT: Self = Self { + text_input: text_input::default, + menu: menu::Style::<Theme>::DEFAULT, + }; +} + impl<Theme> Clone for Style<Theme> { fn clone(&self) -> Self { *self @@ -780,16 +788,14 @@ impl<Theme> Clone for Style<Theme> { impl<Theme> Copy for Style<Theme> {} -impl Default for Style<Theme> { - 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<Self>; } -/// The default style of a [`ComboBox`]. -pub fn default() -> Style<Theme> { - Style { - text_input: text_input::default, - menu: menu::Style::default(), +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + Style::<Self>::DEFAULT } } diff --git a/widget/src/container.rs b/widget/src/container.rs index 99d877fe..5e16312c 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -47,9 +47,9 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { - Self::with_style(content, Style::default().0) + Self::with_style(content, Theme::default_style()) } /// Creates a [`Container`] with the given content and style. @@ -71,7 +71,7 @@ where vertical_alignment: alignment::Vertical::Top, clip: false, content, - style: Style(style), + style, } } @@ -137,7 +137,7 @@ where /// Sets the style of the [`Container`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } @@ -275,7 +275,7 @@ where Status::Idle }; - let style = (self.style.0)(theme, status); + let style = (self.style)(theme, status); if let Some(clipped_viewport) = bounds.intersection(viewport) { draw_background(renderer, &style, bounds); @@ -546,40 +546,23 @@ pub enum Status { } /// The style of a [`Container`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Style<Theme> { - /// Resolves the [`Style`] with the given `Theme` and [`Status`] to - /// produce an [`Appearance`]. - pub fn resolve(self, theme: &Theme, status: Status) -> Appearance { - (self.0)(theme, status) - } -} - -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } -} - -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(transparent) - } +/// The default style of a [`Container`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Container`]. + fn default_style() -> Style<Self>; } -impl Default for Style<Appearance> { - fn default() -> Self { - Style(|appearance, _status| *appearance) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + transparent } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index fdc9462d..75072d2e 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -58,8 +58,8 @@ pub fn container<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Container<'a, Message, Theme, Renderer> where + Theme: container::DefaultStyle, Renderer: core::Renderer, - container::Style<Theme>: Default, { Container::new(content) } @@ -104,8 +104,8 @@ pub fn scrollable<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Scrollable<'a, Message, Theme, Renderer> where + Theme: scrollable::DefaultStyle, Renderer: core::Renderer, - scrollable::Style<Theme>: Default, { Scrollable::new(content) } @@ -117,8 +117,8 @@ pub fn button<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Button<'a, Message, Theme, Renderer> where + Theme: button::DefaultStyle, Renderer: core::Renderer, - button::Style<Theme>: Default, { Button::new(content) } @@ -134,8 +134,8 @@ pub fn tooltip<'a, Message, Theme, Renderer>( position: tooltip::Position, ) -> crate::Tooltip<'a, Message, Theme, Renderer> where + Theme: container::DefaultStyle, Renderer: core::text::Renderer, - container::Style<Theme>: Default, { Tooltip::new(content, tooltip, position) } @@ -160,8 +160,8 @@ pub fn checkbox<'a, Message, Theme, Renderer>( is_checked: bool, ) -> Checkbox<'a, Message, Theme, Renderer> where + Theme: checkbox::DefaultStyle, Renderer: core::text::Renderer, - checkbox::Style<Theme>: Default, { Checkbox::new(label, is_checked) } @@ -177,9 +177,9 @@ pub fn radio<Message, Theme, Renderer, V>( ) -> Radio<Message, Theme, Renderer> where Message: Clone, + Theme: radio::DefaultStyle, Renderer: core::text::Renderer, V: Copy + Eq, - radio::Style<Theme>: Default, { Radio::new(label, value, selected, on_click) } @@ -193,8 +193,8 @@ pub fn toggler<'a, Message, Theme, Renderer>( f: impl Fn(bool) -> Message + 'a, ) -> Toggler<'a, Message, Theme, Renderer> where + Theme: toggler::DefaultStyle, Renderer: core::text::Renderer, - toggler::Style<Theme>: Default, { Toggler::new(label, is_checked, f) } @@ -208,8 +208,8 @@ pub fn text_input<'a, Message, Theme, Renderer>( ) -> TextInput<'a, Message, Theme, Renderer> where Message: Clone, + Theme: text_input::DefaultStyle, Renderer: core::text::Renderer, - text_input::Style<Theme>: Default, { TextInput::new(placeholder, value) } @@ -222,8 +222,8 @@ pub fn text_editor<Message, Theme, Renderer>( ) -> TextEditor<'_, core::text::highlighter::PlainText, Message, Theme, Renderer> where Message: Clone, + Theme: text_editor::DefaultStyle, Renderer: core::text::Renderer, - text_editor::Style<Theme>: Default, { TextEditor::new(content) } @@ -239,7 +239,7 @@ pub fn slider<'a, T, Message, Theme>( where T: Copy + From<u8> + std::cmp::PartialOrd, Message: Clone, - slider::Style<Theme>: Default, + Theme: slider::DefaultStyle, { Slider::new(range, value, on_change) } @@ -255,7 +255,7 @@ pub fn vertical_slider<'a, T, Message, Theme>( where T: Copy + From<u8> + std::cmp::PartialOrd, Message: Clone, - vertical_slider::Style<Theme>: Default, + Theme: vertical_slider::DefaultStyle, { VerticalSlider::new(range, value, on_change) } @@ -273,8 +273,8 @@ where L: Borrow<[T]> + 'a, V: Borrow<T> + 'a, Message: Clone, + Theme: pick_list::DefaultStyle, Renderer: core::text::Renderer, - pick_list::Style<Theme>: Default, { PickList::new(options, selected, on_selected) } @@ -290,8 +290,8 @@ pub fn combo_box<'a, T, Message, Theme, Renderer>( ) -> ComboBox<'a, T, Message, Theme, Renderer> where T: std::fmt::Display + Clone, + Theme: combo_box::DefaultStyle, Renderer: core::text::Renderer, - combo_box::Style<Theme>: Default, { ComboBox::new(state, placeholder, selection, on_selected) } @@ -317,7 +317,7 @@ pub fn vertical_space() -> Space { /// [`Rule`]: crate::Rule pub fn horizontal_rule<Theme>(height: impl Into<Pixels>) -> Rule<Theme> where - rule::Style<Theme>: Default, + Theme: rule::DefaultStyle, { Rule::horizontal(height) } @@ -327,7 +327,7 @@ where /// [`Rule`]: crate::Rule pub fn vertical_rule<Theme>(width: impl Into<Pixels>) -> Rule<Theme> where - rule::Style<Theme>: Default, + Theme: rule::DefaultStyle, { Rule::vertical(width) } @@ -344,7 +344,7 @@ pub fn progress_bar<Theme>( value: f32, ) -> ProgressBar<Theme> where - progress_bar::Style<Theme>: Default, + Theme: progress_bar::DefaultStyle, { ProgressBar::new(range, value) } @@ -364,7 +364,7 @@ pub fn image<Handle>(handle: impl Into<Handle>) -> crate::Image<Handle> { #[cfg(feature = "svg")] pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme> where - crate::svg::Style<Theme>: Default, + Theme: crate::svg::DefaultStyle, { crate::Svg::new(handle) } @@ -390,7 +390,7 @@ where #[cfg(feature = "qr_code")] pub fn qr_code<Theme>(data: &crate::qr_code::Data) -> crate::QRCode<'_, Theme> where - crate::qr_code::Style<Theme>: Default, + Theme: crate::qr_code::DefaultStyle, { crate::QRCode::new(data) } diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 2b9e0d03..3ed26b7d 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -58,7 +58,7 @@ where on_option_hovered: Option<&'a dyn Fn(T) -> Message>, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Self::with_style( state, @@ -66,7 +66,7 @@ where hovered_option, on_selected, on_option_hovered, - Style::default(), + Theme::default_style(), ) } @@ -234,7 +234,7 @@ where text_line_height, text_shaping, padding, - style: style.menu, + style: style.list, }, scrollable::Direction::default(), style.scrollable, @@ -327,7 +327,7 @@ where ) { let bounds = layout.bounds(); - let appearance = (self.style.menu)(theme); + let appearance = (self.style.list)(theme); renderer.fill_quad( renderer::Quad { @@ -598,15 +598,23 @@ pub struct Appearance { pub selected_background: Background, } -/// The definiton of the default style of a [`Menu`]. +/// The style of the different parts of a [`Menu`]. #[derive(Debug, PartialEq, Eq)] pub struct Style<Theme> { - /// The style of the [`Menu`]. - menu: fn(&Theme) -> Appearance, + /// The style of the list of the [`Menu`]. + list: fn(&Theme) -> Appearance, /// The style of the [`Scrollable`] of the [`Menu`]. scrollable: fn(&Theme, scrollable::Status) -> scrollable::Appearance, } +impl Style<Theme> { + /// The default style of a [`Menu`] with the built-in [`Theme`]. + pub const DEFAULT: Self = Self { + list: default, + scrollable: scrollable::default, + }; +} + impl<Theme> Clone for Style<Theme> { fn clone(&self) -> Self { *self @@ -615,16 +623,19 @@ impl<Theme> Clone for Style<Theme> { impl<Theme> Copy for Style<Theme> {} -impl Default for Style<Theme> { - fn default() -> Self { - Self { - menu: default, - scrollable: scrollable::default, - } +/// The default style of a [`Menu`]. +pub trait DefaultStyle: Sized { + /// Returns the default style of a [`Menu`]. + fn default_style() -> Style<Self>; +} + +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + Style::<Theme>::DEFAULT } } -/// The default style of a [`Menu`]. +/// The default style of the list of a [`Menu`]. pub fn default(theme: &Theme) -> Appearance { let palette = theme.extended_palette(); diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index 5403b2f5..d60d5e3b 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -126,7 +126,7 @@ where view: impl Fn(Pane, &'a T, bool) -> Content<'a, Message, Theme, Renderer>, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { let contents = if let Some((pane, pane_state)) = state.maximized.and_then(|pane| { @@ -158,7 +158,7 @@ where on_click: None, on_drag: None, on_resize: None, - style: Style::default(), + style: Theme::default_style(), } } @@ -219,7 +219,7 @@ where /// Sets the style of the [`PaneGrid`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } @@ -677,7 +677,7 @@ where None }; - let appearance = (self.style.0)(theme); + let appearance = (self.style)(theme); for ((id, (content, tree)), pane_layout) in contents.zip(layout.children()) @@ -1146,26 +1146,23 @@ pub struct Line { } /// The style of a [`PaneGrid`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme) -> Appearance); +pub type Style<Theme> = fn(&Theme) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`PaneGrid`]. +pub trait DefaultStyle { + /// Returns the default style of a [`PaneGrid`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance| *appearance } } diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index ce29e8d0..aecec777 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -34,12 +34,12 @@ where /// Creates a new [`Content`] with the provided body. pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self where - container::Style<Theme>: Default, + Theme: container::DefaultStyle, { Self { title_bar: None, body: body.into(), - style: container::Style::default(), + style: Theme::default_style(), } } @@ -114,7 +114,7 @@ where container::Status::Idle }; - self.style.resolve(theme, status) + (self.style)(theme, status) }; container::draw_background(renderer, &style, bounds); @@ -403,8 +403,8 @@ impl<'a, T, Message, Theme, Renderer> From<T> for Content<'a, Message, Theme, Renderer> where T: Into<Element<'a, Message, Theme, Renderer>>, + Theme: container::DefaultStyle, Renderer: crate::core::Renderer, - container::Style<Theme>: Default, { fn from(element: T) -> Self { Self::new(element) diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index b1cdcde3..37f0f160 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -37,14 +37,14 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - container::Style<Theme>: Default, + Theme: container::DefaultStyle, { Self { content: content.into(), controls: None, padding: Padding::ZERO, always_show_controls: false, - style: container::Style::default(), + style: Theme::default_style(), } } @@ -138,7 +138,7 @@ where container::Status::Idle }; - self.style.resolve(theme, status) + (self.style)(theme, status) }; let inherited_style = renderer::Style { diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 649daafe..cfeabbb7 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -69,7 +69,7 @@ where on_select: impl Fn(T) -> Message + 'a, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Self { on_select: Box::new(on_select), @@ -85,7 +85,7 @@ where text_shaping: text::Shaping::Basic, font: None, handle: Handle::default(), - style: Style::default(), + style: Theme::default_style(), } } @@ -266,7 +266,7 @@ where Status::Active }; - let appearance = (self.style.pick_list)(theme, status); + let appearance = (self.style.field)(theme, status); renderer.fill_quad( renderer::Quad { @@ -737,16 +737,24 @@ pub struct Appearance { pub border: Border, } -/// The different styles of a [`PickList`]. +/// The styles of the different parts of a [`PickList`]. #[derive(Debug, PartialEq, Eq)] pub struct Style<Theme> { /// The style of the [`PickList`] itself. - pub pick_list: fn(&Theme, Status) -> Appearance, + pub field: fn(&Theme, Status) -> Appearance, /// The style of the [`Menu`] of the pick list. pub menu: menu::Style<Theme>, } +impl Style<Theme> { + /// The default style of a [`PickList`] with the built-in [`Theme`]. + pub const DEFAULT: Self = Self { + field: default, + menu: menu::Style::<Theme>::DEFAULT, + }; +} + impl<Theme> Clone for Style<Theme> { fn clone(&self) -> Self { *self @@ -755,16 +763,19 @@ impl<Theme> Clone for Style<Theme> { impl<Theme> Copy for Style<Theme> {} -impl Default for Style<Theme> { - fn default() -> Self { - Self { - pick_list: default, - menu: menu::Style::default(), - } +/// The default style of a [`PickList`]. +pub trait DefaultStyle: Sized { + /// Returns the default style of a [`PickList`]. + fn default_style() -> Style<Self>; +} + +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + Style::<Self>::DEFAULT } } -/// The default style of a [`PickList`]. +/// The default style of the field of a [`PickList`]. pub fn default(theme: &Theme, status: Status) -> Appearance { let palette = theme.extended_palette(); diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index b667b506..f945a7b5 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -41,14 +41,14 @@ impl<Theme> ProgressBar<Theme> { /// * the current value of the [`ProgressBar`] pub fn new(range: RangeInclusive<f32>, value: f32) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { ProgressBar { value: value.clamp(*range.start(), *range.end()), range, width: Length::Fill, height: None, - style: Style::default(), + style: Theme::default_style(), } } @@ -116,7 +116,7 @@ where / (range_end - range_start) }; - let appearance = (self.style.0)(theme); + let appearance = (self.style)(theme); renderer.fill_quad( renderer::Quad { @@ -169,26 +169,23 @@ pub struct Appearance { } /// The style of a [`ProgressBar`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme) -> Appearance); +pub type Style<Theme> = fn(&Theme) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`ProgressBar`]. +pub trait DefaultStyle { + /// Returns the default style of a [`ProgressBar`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(primary) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + primary } } -impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance| *appearance } } diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index 66513775..41bcb83e 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -30,12 +30,12 @@ impl<'a, Theme> QRCode<'a, Theme> { /// Creates a new [`QRCode`] with the provided [`Data`]. pub fn new(data: &'a Data) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Self { data, cell_size: DEFAULT_CELL_SIZE, - style: Style::default(), + style: Theme::default_style(), } } @@ -97,7 +97,7 @@ impl<'a, Message, Theme> Widget<Message, Theme, Renderer> let bounds = layout.bounds(); let side_length = self.data.width + 2 * QUIET_ZONE; - let appearance = (self.style.0)(theme); + let appearance = (self.style)(theme); let mut last_appearance = state.last_appearance.borrow_mut(); if Some(appearance) != *last_appearance { @@ -336,26 +336,23 @@ pub struct Appearance { } /// The style of a [`QRCode`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme) -> Appearance); +pub type Style<Theme> = fn(&Theme) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`QRCode`]. +pub trait DefaultStyle { + /// Returns the default style of a [`QRCode`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance| *appearance } } diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 6bb72650..e8f1eb1f 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -110,7 +110,7 @@ where f: F, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, V: Eq + Copy, F: FnOnce(V) -> Message, { @@ -125,7 +125,7 @@ where text_line_height: text::LineHeight::default(), text_shaping: text::Shaping::Basic, font: None, - style: Style::default(), + style: Theme::default_style(), } } @@ -176,7 +176,7 @@ where /// Sets the style of the [`Radio`] button. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -297,7 +297,7 @@ where Status::Active { is_selected } }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); { let layout = children.next().unwrap(); @@ -398,26 +398,23 @@ pub struct Appearance { } /// The style of a [`Radio`] button. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Radio`] button. +pub trait DefaultStyle { + /// Returns the default style of a [`Radio`] button. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 19ad43f6..384baed4 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -21,32 +21,32 @@ impl<Theme> Rule<Theme> { /// Creates a horizontal [`Rule`] with the given height. pub fn horizontal(height: impl Into<Pixels>) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Rule { width: Length::Fill, height: Length::Fixed(height.into().0), is_horizontal: true, - style: Style::default(), + style: Theme::default_style(), } } /// Creates a vertical [`Rule`] with the given width. pub fn vertical(width: impl Into<Pixels>) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Rule { width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, - style: Style::default(), + style: Theme::default_style(), } } /// Sets the style of the [`Rule`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -82,7 +82,7 @@ where _viewport: &Rectangle, ) { let bounds = layout.bounds(); - let appearance = (self.style.0)(theme); + let appearance = (self.style)(theme); let bounds = if self.is_horizontal { let line_y = (bounds.y + (bounds.height / 2.0) @@ -216,26 +216,23 @@ impl FillMode { } /// The style of a [`Rule`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme) -> Appearance); +pub type Style<Theme> = fn(&Theme) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Rule`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Rule`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance| *appearance } } diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 861f1bfb..8d2b2057 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -48,7 +48,7 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Self::with_direction(content, Direction::default()) } @@ -59,9 +59,13 @@ where direction: Direction, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { - Self::with_direction_and_style(content, direction, Style::default().0) + Self::with_direction_and_style( + content, + direction, + Theme::default_style(), + ) } /// Creates a new [`Scrollable`] with the given [`Direction`] and style. @@ -407,7 +411,7 @@ where Status::Active }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); container::draw_background( renderer, @@ -1662,26 +1666,23 @@ pub struct Scroller { } /// The style of a [`Scrollable`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Scrollable`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Scrollable`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/slider.rs b/widget/src/slider.rs index 79850f63..6449b18e 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -70,7 +70,7 @@ where /// `Message`. pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, F: 'a + Fn(T) -> Message, { let value = if value >= *range.start() { @@ -95,7 +95,7 @@ where on_release: None, width: Length::Fill, height: Self::DEFAULT_HEIGHT, - style: Style::default(), + style: Theme::default_style(), } } @@ -346,7 +346,7 @@ where let bounds = layout.bounds(); let is_mouse_over = cursor.is_over(bounds); - let style = (self.style.0)( + let style = (self.style)( theme, if state.is_dragging { Status::Dragged @@ -547,26 +547,23 @@ pub enum HandleShape { } /// The style of a [`Slider`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(pub(crate) fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Slider`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Slider`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 34fd9a7b..6e61d27a 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -32,14 +32,14 @@ impl<Theme> Svg<Theme> { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into<Handle>) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Style::default(), + style: Theme::default_style(), } } @@ -48,7 +48,7 @@ impl<Theme> Svg<Theme> { #[must_use] pub fn from_path(path: impl Into<PathBuf>) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Self::new(Handle::from_path(path)) } @@ -81,7 +81,7 @@ impl<Theme> Svg<Theme> { /// Sets the style variant of this [`Svg`]. #[must_use] pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -163,7 +163,7 @@ where Status::Idle }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); renderer.draw( self.handle.clone(), @@ -214,25 +214,22 @@ pub struct Appearance { } /// The style of an [`Svg`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of an [`Svg`]. +pub trait DefaultStyle { + /// Returns the default style of an [`Svg`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(|_, _| Appearance::default()) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + |_theme, _status| Appearance::default() } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 0212a7a0..018ffd9c 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -58,7 +58,7 @@ where /// Creates new [`TextEditor`] with the given [`Content`]. pub fn new(content: &'a Content<Renderer>) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { Self { content, @@ -68,7 +68,7 @@ where width: Length::Fill, height: Length::Shrink, padding: Padding::new(5.0), - style: Style::default(), + style: Theme::default_style(), on_edit: None, highlighter_settings: (), highlighter_format: |_highlight, _theme| { @@ -505,7 +505,7 @@ where Status::Active }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); renderer.fill_quad( renderer::Quad { @@ -809,26 +809,23 @@ pub struct Appearance { } /// The style of a [`TextEditor`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`TextEditor`]. +pub trait DefaultStyle { + /// Returns the default style of a [`TextEditor`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 6bad0afe..449524fc 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -90,9 +90,9 @@ where /// its current value. pub fn new(placeholder: &str, value: &str) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { - Self::with_style(placeholder, value, Style::default().0) + Self::with_style(placeholder, value, Theme::default_style()) } /// Creates a new [`TextInput`] with the given placeholder, @@ -342,7 +342,7 @@ where Status::Active }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); renderer.fill_quad( renderer::Quad { @@ -1412,26 +1412,23 @@ pub struct Appearance { } /// The style of a [`TextInput`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`TextInput`]. +pub trait DefaultStyle { + /// Returns the default style of a [`TextInput`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index adc82f73..6b540f1c 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -72,7 +72,7 @@ where f: F, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, F: 'a + Fn(bool) -> Message, { Toggler { @@ -87,7 +87,7 @@ where text_shaping: text::Shaping::Basic, spacing: Self::DEFAULT_SIZE / 2.0, font: None, - style: Style::default(), + style: Theme::default_style(), } } @@ -299,7 +299,7 @@ where } }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); let border_radius = bounds.height / BORDER_RADIUS_RATIO; let space = SPACE_RATIO * bounds.height; @@ -398,26 +398,23 @@ pub struct Appearance { } /// The style of a [`Toggler`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Toggler`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Toggler`]. + fn default_style() -> Style<Self>; } -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + default } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } diff --git a/widget/src/tooltip.rs b/widget/src/tooltip.rs index 4e026e8f..8c8ee983 100644 --- a/widget/src/tooltip.rs +++ b/widget/src/tooltip.rs @@ -47,7 +47,7 @@ where position: Position, ) -> Self where - container::Style<Theme>: Default, + Theme: container::DefaultStyle, { Tooltip { content: content.into(), @@ -56,7 +56,7 @@ where gap: 0.0, padding: Self::DEFAULT_PADDING, snap_within_viewport: true, - style: container::Style::default(), + style: Theme::default_style(), } } @@ -424,7 +424,7 @@ where layout: Layout<'_>, cursor_position: mouse::Cursor, ) { - let style = self.style.resolve(theme, container::Status::Idle); + let style = (self.style)(theme, container::Status::Idle); container::draw_background(renderer, &style, layout.bounds()); diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index 93abe02a..103c3b7d 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -2,7 +2,7 @@ use std::ops::RangeInclusive; pub use crate::slider::{ - default, Appearance, Handle, HandleShape, Status, Style, + default, Appearance, DefaultStyle, Handle, HandleShape, Status, Style, }; use crate::core; @@ -72,7 +72,7 @@ where /// `Message`. pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, F: 'a + Fn(T) -> Message, { let value = if value >= *range.start() { @@ -97,7 +97,7 @@ where on_release: None, width: Self::DEFAULT_WIDTH, height: Length::Fill, - style: Style::default(), + style: Theme::default_style(), } } @@ -351,7 +351,7 @@ where let bounds = layout.bounds(); let is_mouse_over = cursor.is_over(bounds); - let style = (self.style.0)( + let style = (self.style)( theme, if state.is_dragging { Status::Dragged |