diff options
author | 2024-03-12 18:17:19 +0100 | |
---|---|---|
committer | 2024-03-12 18:17:19 +0100 | |
commit | 3d915d3cb30e5d08829aa2928676a53c505a601e (patch) | |
tree | 4acb46e00ef3037aad6a8273ab67d4278d560784 /widget | |
parent | 34317bba5db0a0f9e3ffdbbac0d7136a32bd0f95 (diff) | |
parent | 98621aa344a7a0e1b23f320d21a4687af559998e (diff) | |
download | iced-3d915d3cb30e5d08829aa2928676a53c505a601e.tar.gz iced-3d915d3cb30e5d08829aa2928676a53c505a601e.tar.bz2 iced-3d915d3cb30e5d08829aa2928676a53c505a601e.zip |
Merge pull request #2326 from iced-rs/closure-styles
Use closures for widget styling
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/button.rs | 29 | ||||
-rw-r--r-- | widget/src/checkbox.rs | 25 | ||||
-rw-r--r-- | widget/src/combo_box.rs | 46 | ||||
-rw-r--r-- | widget/src/container.rs | 59 | ||||
-rw-r--r-- | widget/src/helpers.rs | 63 | ||||
-rw-r--r-- | widget/src/overlay/menu.rs | 75 | ||||
-rw-r--r-- | widget/src/pane_grid.rs | 22 | ||||
-rw-r--r-- | widget/src/pane_grid/content.rs | 12 | ||||
-rw-r--r-- | widget/src/pane_grid/title_bar.rs | 10 | ||||
-rw-r--r-- | widget/src/pick_list.rs | 41 | ||||
-rw-r--r-- | widget/src/progress_bar.rs | 36 | ||||
-rw-r--r-- | widget/src/qr_code.rs | 24 | ||||
-rw-r--r-- | widget/src/radio.rs | 41 | ||||
-rw-r--r-- | widget/src/rule.rs | 37 | ||||
-rw-r--r-- | widget/src/scrollable.rs | 35 | ||||
-rw-r--r-- | widget/src/slider.rs | 25 | ||||
-rw-r--r-- | widget/src/svg.rs | 38 | ||||
-rw-r--r-- | widget/src/text_editor.rs | 25 | ||||
-rw-r--r-- | widget/src/text_input.rs | 29 | ||||
-rw-r--r-- | widget/src/toggler.rs | 25 | ||||
-rw-r--r-- | widget/src/tooltip.rs | 15 | ||||
-rw-r--r-- | widget/src/vertical_slider.rs | 13 |
22 files changed, 358 insertions, 367 deletions
diff --git a/widget/src/button.rs b/widget/src/button.rs index e265aa1f..5790f811 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -56,7 +56,7 @@ where height: Length, padding: Padding, clip: bool, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> @@ -68,7 +68,7 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { let content = content.into(); let size = content.as_widget().size_hint(); @@ -80,7 +80,7 @@ where height: size.height.fluid(), padding: DEFAULT_PADDING, clip: false, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -120,8 +120,11 @@ where } /// Sets the style variant of this [`Button`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -439,29 +442,29 @@ impl std::default::Default for Appearance { } /// The style of a [`Button`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Button`]. pub trait DefaultStyle { /// Returns the default style of a [`Button`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - primary + fn default_style(&self, status: Status) -> Appearance { + primary(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } impl DefaultStyle for Color { - fn default_style() -> Style<Self> { - |color, _status| Appearance::default().with_background(*color) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default().with_background(*self) } } diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index f0c7357b..15fb8f58 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -51,7 +51,7 @@ pub struct Checkbox< text_shaping: text::Shaping, font: Option<Renderer::Font>, icon: Icon<Renderer::Font>, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer> @@ -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 - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Checkbox { is_checked, @@ -91,7 +91,7 @@ where line_height: text::LineHeight::default(), shaping: text::Shaping::Basic, }, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -174,8 +174,11 @@ where } /// Sets the style of the [`Checkbox`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } @@ -424,23 +427,23 @@ pub struct Appearance { } /// The style of a [`Checkbox`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Checkbox`]. pub trait DefaultStyle { /// Returns the default style of a [`Checkbox`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - primary + fn default_style(&self, status: Status) -> Appearance { + primary(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index bddf2789..ee24d742 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -42,7 +42,7 @@ pub struct ComboBox< on_option_hovered: Option<Box<dyn Fn(T) -> Message>>, on_close: Option<Message>, on_input: Option<Box<dyn Fn(String) -> Message>>, - menu_style: menu::Style<Theme>, + menu_style: menu::Style<'a, Theme>, padding: Padding, size: Option<f32>, } @@ -62,7 +62,7 @@ where on_selected: impl Fn(T) -> Message + 'static, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { let style = Theme::default_style(); @@ -125,7 +125,10 @@ where } /// Sets the style of the [`ComboBox`]. - pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self { + pub fn style(mut self, style: impl Into<Style<'a, Theme>>) -> Self + where + Theme: 'a, + { let style = style.into(); self.text_input = self.text_input.style(style.text_input); @@ -669,7 +672,7 @@ where self.state.sync_filtered_options(filtered_options); - let mut menu = menu::Menu::with_style( + let mut menu = menu::Menu::new( menu, &filtered_options.options, hovered_option, @@ -683,7 +686,7 @@ where (self.on_selected)(x) }, self.on_option_hovered.as_deref(), - self.menu_style, + &self.menu_style, ) .width(bounds.width) .padding(self.padding); @@ -761,41 +764,28 @@ where } /// The style of a [`ComboBox`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme> { +#[allow(missing_debug_implementations)] +pub struct Style<'a, Theme> { /// The style of the [`TextInput`] of the [`ComboBox`]. - pub text_input: fn(&Theme, text_input::Status) -> text_input::Appearance, + pub text_input: text_input::Style<'a, Theme>, /// The style of the [`Menu`] of the [`ComboBox`]. /// /// [`Menu`]: menu::Menu - pub 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, - }; + pub menu: menu::Style<'a, Theme>, } -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } -} - -impl<Theme> Copy for Style<Theme> {} - /// The default style of a [`ComboBox`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`ComboBox`]. - fn default_style() -> Style<Self>; + fn default_style() -> Style<'static, Self>; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - Style::<Self>::DEFAULT + fn default_style() -> Style<'static, Self> { + Style { + text_input: Box::new(text_input::default), + menu: menu::DefaultStyle::default_style(), + } } } diff --git a/widget/src/container.rs b/widget/src/container.rs index d606b0aa..7c133588 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -36,7 +36,7 @@ pub struct Container< vertical_alignment: alignment::Vertical, clip: bool, content: Element<'a, Message, Theme, Renderer>, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer> @@ -48,15 +48,15 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { - Self::with_style(content, Theme::default_style()) + Self::with_style(content, Theme::default_style) } /// Creates a [`Container`] with the given content and style. pub fn with_style( content: impl Into<Element<'a, Message, Theme, Renderer>>, - style: fn(&Theme, Status) -> Appearance, + style: impl Fn(&Theme, Status) -> Appearance + 'a, ) -> Self { let content = content.into(); let size = content.as_widget().size_hint(); @@ -71,8 +71,8 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, clip: false, + style: Box::new(style), content, - style, } } @@ -137,8 +137,11 @@ where } /// Sets the style of the [`Container`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -536,6 +539,24 @@ impl Appearance { } } +impl From<Color> for Appearance { + fn from(color: Color) -> Self { + Self::default().with_background(color) + } +} + +impl From<Gradient> for Appearance { + fn from(gradient: Gradient) -> Self { + Self::default().with_background(gradient) + } +} + +impl From<gradient::Linear> for Appearance { + fn from(gradient: gradient::Linear) -> Self { + Self::default().with_background(gradient) + } +} + /// The possible status of a [`Container`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Status { @@ -546,41 +567,41 @@ pub enum Status { } /// The style of a [`Container`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Container`]. pub trait DefaultStyle { /// Returns the default style of a [`Container`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - transparent + fn default_style(&self, status: Status) -> Appearance { + transparent(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } impl DefaultStyle for Color { - fn default_style() -> Style<Self> { - |color, _status| Appearance::default().with_background(*color) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::from(*self) } } impl DefaultStyle for Gradient { - fn default_style() -> Style<Self> { - |gradient, _status| Appearance::default().with_background(*gradient) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::from(*self) } } impl DefaultStyle for gradient::Linear { - fn default_style() -> Style<Self> { - |gradient, _status| Appearance::default().with_background(*gradient) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::from(*self) } } diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 75072d2e..4863e550 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -14,7 +14,7 @@ use crate::rule::{self, Rule}; use crate::runtime::Command; use crate::scrollable::{self, Scrollable}; use crate::slider::{self, Slider}; -use crate::text::Text; +use crate::text::{self, Text}; use crate::text_editor::{self, TextEditor}; use crate::text_input::{self, TextInput}; use crate::toggler::{self, Toggler}; @@ -58,7 +58,7 @@ pub fn container<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Container<'a, Message, Theme, Renderer> where - Theme: container::DefaultStyle, + Theme: container::DefaultStyle + 'a, Renderer: core::Renderer, { Container::new(content) @@ -104,7 +104,7 @@ pub fn scrollable<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Scrollable<'a, Message, Theme, Renderer> where - Theme: scrollable::DefaultStyle, + Theme: scrollable::DefaultStyle + 'a, Renderer: core::Renderer, { Scrollable::new(content) @@ -117,7 +117,7 @@ pub fn button<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Button<'a, Message, Theme, Renderer> where - Theme: button::DefaultStyle, + Theme: button::DefaultStyle + 'a, Renderer: core::Renderer, { Button::new(content) @@ -134,7 +134,7 @@ pub fn tooltip<'a, Message, Theme, Renderer>( position: tooltip::Position, ) -> crate::Tooltip<'a, Message, Theme, Renderer> where - Theme: container::DefaultStyle, + Theme: container::DefaultStyle + 'a, Renderer: core::text::Renderer, { Tooltip::new(content, tooltip, position) @@ -147,6 +147,7 @@ pub fn text<'a, Theme, Renderer>( text: impl ToString, ) -> Text<'a, Theme, Renderer> where + Theme: text::DefaultStyle + 'a, Renderer: core::text::Renderer, { Text::new(text.to_string()) @@ -160,7 +161,7 @@ pub fn checkbox<'a, Message, Theme, Renderer>( is_checked: bool, ) -> Checkbox<'a, Message, Theme, Renderer> where - Theme: checkbox::DefaultStyle, + Theme: checkbox::DefaultStyle + 'a, Renderer: core::text::Renderer, { Checkbox::new(label, is_checked) @@ -169,15 +170,15 @@ where /// Creates a new [`Radio`]. /// /// [`Radio`]: crate::Radio -pub fn radio<Message, Theme, Renderer, V>( +pub fn radio<'a, Message, Theme, Renderer, V>( label: impl Into<String>, value: V, selected: Option<V>, on_click: impl FnOnce(V) -> Message, -) -> Radio<Message, Theme, Renderer> +) -> Radio<'a, Message, Theme, Renderer> where Message: Clone, - Theme: radio::DefaultStyle, + Theme: radio::DefaultStyle + 'a, Renderer: core::text::Renderer, V: Copy + Eq, { @@ -193,7 +194,7 @@ pub fn toggler<'a, Message, Theme, Renderer>( f: impl Fn(bool) -> Message + 'a, ) -> Toggler<'a, Message, Theme, Renderer> where - Theme: toggler::DefaultStyle, + Theme: toggler::DefaultStyle + 'a, Renderer: core::text::Renderer, { Toggler::new(label, is_checked, f) @@ -208,7 +209,7 @@ pub fn text_input<'a, Message, Theme, Renderer>( ) -> TextInput<'a, Message, Theme, Renderer> where Message: Clone, - Theme: text_input::DefaultStyle, + Theme: text_input::DefaultStyle + 'a, Renderer: core::text::Renderer, { TextInput::new(placeholder, value) @@ -217,12 +218,12 @@ where /// Creates a new [`TextEditor`]. /// /// [`TextEditor`]: crate::TextEditor -pub fn text_editor<Message, Theme, Renderer>( - content: &text_editor::Content<Renderer>, -) -> TextEditor<'_, core::text::highlighter::PlainText, Message, Theme, Renderer> +pub fn text_editor<'a, Message, Theme, Renderer>( + content: &'a text_editor::Content<Renderer>, +) -> TextEditor<'a, core::text::highlighter::PlainText, Message, Theme, Renderer> where Message: Clone, - Theme: text_editor::DefaultStyle, + Theme: text_editor::DefaultStyle + 'a, Renderer: core::text::Renderer, { TextEditor::new(content) @@ -239,7 +240,7 @@ pub fn slider<'a, T, Message, Theme>( where T: Copy + From<u8> + std::cmp::PartialOrd, Message: Clone, - Theme: slider::DefaultStyle, + Theme: slider::DefaultStyle + 'a, { Slider::new(range, value, on_change) } @@ -255,7 +256,7 @@ pub fn vertical_slider<'a, T, Message, Theme>( where T: Copy + From<u8> + std::cmp::PartialOrd, Message: Clone, - Theme: vertical_slider::DefaultStyle, + Theme: vertical_slider::DefaultStyle + 'a, { VerticalSlider::new(range, value, on_change) } @@ -290,7 +291,7 @@ 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, + Theme: combo_box::DefaultStyle + 'a, Renderer: core::text::Renderer, { ComboBox::new(state, placeholder, selection, on_selected) @@ -315,9 +316,9 @@ pub fn vertical_space() -> Space { /// Creates a horizontal [`Rule`] with the given height. /// /// [`Rule`]: crate::Rule -pub fn horizontal_rule<Theme>(height: impl Into<Pixels>) -> Rule<Theme> +pub fn horizontal_rule<'a, Theme>(height: impl Into<Pixels>) -> Rule<'a, Theme> where - Theme: rule::DefaultStyle, + Theme: rule::DefaultStyle + 'a, { Rule::horizontal(height) } @@ -325,9 +326,9 @@ where /// Creates a vertical [`Rule`] with the given width. /// /// [`Rule`]: crate::Rule -pub fn vertical_rule<Theme>(width: impl Into<Pixels>) -> Rule<Theme> +pub fn vertical_rule<'a, Theme>(width: impl Into<Pixels>) -> Rule<'a, Theme> where - Theme: rule::DefaultStyle, + Theme: rule::DefaultStyle + 'a, { Rule::vertical(width) } @@ -339,12 +340,12 @@ where /// * the current value of the [`ProgressBar`]. /// /// [`ProgressBar`]: crate::ProgressBar -pub fn progress_bar<Theme>( +pub fn progress_bar<'a, Theme>( range: RangeInclusive<f32>, value: f32, -) -> ProgressBar<Theme> +) -> ProgressBar<'a, Theme> where - Theme: progress_bar::DefaultStyle, + Theme: progress_bar::DefaultStyle + 'a, { ProgressBar::new(range, value) } @@ -362,9 +363,11 @@ pub fn image<Handle>(handle: impl Into<Handle>) -> crate::Image<Handle> { /// [`Svg`]: crate::Svg /// [`Handle`]: crate::svg::Handle #[cfg(feature = "svg")] -pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme> +pub fn svg<'a, Theme>( + handle: impl Into<core::svg::Handle>, +) -> crate::Svg<'a, Theme> where - Theme: crate::svg::DefaultStyle, + Theme: crate::svg::DefaultStyle + 'a, { crate::Svg::new(handle) } @@ -388,9 +391,11 @@ where /// [`QRCode`]: crate::QRCode /// [`Data`]: crate::qr_code::Data #[cfg(feature = "qr_code")] -pub fn qr_code<Theme>(data: &crate::qr_code::Data) -> crate::QRCode<'_, Theme> +pub fn qr_code<'a, Theme>( + data: &'a crate::qr_code::Data, +) -> crate::QRCode<'a, Theme> where - Theme: crate::qr_code::DefaultStyle, + Theme: crate::qr_code::DefaultStyle + 'a, { crate::QRCode::new(data) } diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 746407c6..0364f980 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -38,7 +38,7 @@ pub struct Menu< text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option<Renderer::Font>, - style: Style<Theme>, + style: &'a Style<'a, Theme>, } impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer> @@ -48,37 +48,15 @@ where Theme: 'a, Renderer: text::Renderer + 'a, { - /// Creates a new [`Menu`] with the given [`State`], a list of options, and - /// the message to produced when an option is selected. - pub fn new( - state: &'a mut State, - options: &'a [T], - hovered_option: &'a mut Option<usize>, - on_selected: impl FnMut(T) -> Message + 'a, - on_option_hovered: Option<&'a dyn Fn(T) -> Message>, - ) -> Self - where - Theme: DefaultStyle, - { - Self::with_style( - state, - options, - hovered_option, - on_selected, - on_option_hovered, - Theme::default_style(), - ) - } - /// Creates a new [`Menu`] with the given [`State`], a list of options, /// the message to produced when an option is selected, and its [`Style`]. - pub fn with_style( + pub fn new( state: &'a mut State, options: &'a [T], hovered_option: &'a mut Option<usize>, on_selected: impl FnMut(T) -> Message + 'a, on_option_hovered: Option<&'a dyn Fn(T) -> Message>, - style: Style<Theme>, + style: &'a Style<'a, Theme>, ) -> Self { Menu { state, @@ -135,12 +113,6 @@ where self } - /// Sets the style of the [`Menu`]. - pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self { - self.style = style.into(); - self - } - /// Turns the [`Menu`] into an overlay [`Element`] at the given target /// position. /// @@ -190,7 +162,7 @@ where container: Container<'a, Message, Theme, Renderer>, width: f32, target_height: f32, - style: Style<Theme>, + style: &'a Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer> @@ -234,10 +206,10 @@ where text_line_height, text_shaping, padding, - style: style.list, + style: &style.list, }, scrollable::Direction::default(), - style.scrollable, + &style.scrollable, ), container::transparent, ); @@ -356,7 +328,7 @@ where text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option<Renderer::Font>, - style: fn(&Theme) -> Appearance, + style: &'a dyn Fn(&Theme) -> Appearance, } impl<'a, T, Message, Theme, Renderer> Widget<Message, Theme, Renderer> @@ -599,39 +571,26 @@ pub struct Appearance { } /// The style of the different parts of a [`Menu`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme> { +#[allow(missing_debug_implementations)] +pub struct Style<'a, Theme> { /// The style of the list of the [`Menu`]. - pub list: fn(&Theme) -> Appearance, + pub list: Box<dyn Fn(&Theme) -> Appearance + 'a>, /// The style of the [`Scrollable`] of the [`Menu`]. - pub scrollable: fn(&Theme, scrollable::Status) -> scrollable::Appearance, + pub scrollable: scrollable::Style<'a, Theme>, } -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 - } -} - -impl<Theme> Copy for Style<Theme> {} - /// The default style of a [`Menu`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`Menu`]. - fn default_style() -> Style<Self>; + fn default_style() -> Style<'static, Self>; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - Style::<Theme>::DEFAULT + fn default_style() -> Style<'static, Self> { + Style { + list: Box::new(default), + scrollable: Box::new(scrollable::default), + } } } diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index bdeb4250..beac0bd8 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -110,7 +110,7 @@ pub struct PaneGrid< on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, on_resize: Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> PaneGrid<'a, Message, Theme, Renderer> @@ -126,7 +126,7 @@ where view: impl Fn(Pane, &'a T, bool) -> Content<'a, Message, Theme, Renderer>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { 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: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -218,8 +218,8 @@ where } /// Sets the style of the [`PaneGrid`]. - pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self { + self.style = Box::new(style); self } @@ -1146,23 +1146,23 @@ pub struct Line { } /// The style of a [`PaneGrid`]. -pub type Style<Theme> = fn(&Theme) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>; /// The default style of a [`PaneGrid`]. pub trait DefaultStyle { /// Returns the default style of a [`PaneGrid`]. - fn default_style() -> Style<Self>; + fn default_style(&self) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self) -> Appearance { + default(self) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance| *appearance + fn default_style(&self) -> Appearance { + *self } } diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index aecec777..98f4f99a 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -24,7 +24,7 @@ pub struct Content< { title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>, body: Element<'a, Message, Theme, Renderer>, - style: container::Style<Theme>, + style: container::Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> @@ -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 - Theme: container::DefaultStyle, + Theme: container::DefaultStyle + 'a, { Self { title_bar: None, body: body.into(), - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -55,9 +55,9 @@ where /// Sets the style of the [`Content`]. pub fn style( mut self, - style: fn(&Theme, container::Status) -> container::Appearance, + style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a, ) -> Self { - self.style = style.into(); + self.style = Box::new(style); self } } @@ -403,7 +403,7 @@ 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, + Theme: container::DefaultStyle + 'a, Renderer: crate::core::Renderer, { fn from(element: T) -> Self { diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index 37f0f160..8dfea6e3 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -25,7 +25,7 @@ pub struct TitleBar< controls: Option<Element<'a, Message, Theme, Renderer>>, padding: Padding, always_show_controls: bool, - style: container::Style<Theme>, + style: container::Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> @@ -37,14 +37,14 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Theme: container::DefaultStyle, + Theme: container::DefaultStyle + 'a, { Self { content: content.into(), controls: None, padding: Padding::ZERO, always_show_controls: false, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -66,9 +66,9 @@ where /// Sets the style of the [`TitleBar`]. pub fn style( mut self, - style: fn(&Theme, container::Status) -> container::Appearance, + style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a, ) -> Self { - self.style = style.into(); + self.style = Box::new(style); self } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index beb4e0c1..52d54397 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -47,7 +47,7 @@ pub struct PickList< text_shaping: text::Shaping, font: Option<Renderer::Font>, handle: Handle<Renderer::Font>, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, T, L, V, Message, Theme, Renderer> @@ -151,7 +151,7 @@ where } /// Sets the style of the [`PickList`]. - pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self { + pub fn style(mut self, style: impl Into<Style<'a, Theme>>) -> Self { self.style = style.into(); self } @@ -529,7 +529,7 @@ where let on_select = &self.on_select; - let mut menu = Menu::with_style( + let mut menu = Menu::new( &mut state.menu, self.options.borrow(), &mut state.hovered_option, @@ -539,7 +539,7 @@ where (on_select)(option) }, None, - self.style.menu, + &self.style.menu, ) .width(bounds.width) .padding(self.padding) @@ -676,40 +676,27 @@ pub struct Appearance { } /// The styles of the different parts of a [`PickList`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme> { +#[allow(missing_debug_implementations)] +pub struct Style<'a, Theme> { /// The style of the [`PickList`] itself. - pub field: fn(&Theme, Status) -> Appearance, + pub field: Box<dyn Fn(&Theme, Status) -> Appearance + 'a>, /// The style of the [`Menu`] of the pick list. - pub menu: menu::Style<Theme>, + pub menu: menu::Style<'a, 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 - } -} - -impl<Theme> Copy for Style<Theme> {} - /// The default style of a [`PickList`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`PickList`]. - fn default_style() -> Style<Self>; + fn default_style() -> Style<'static, Self>; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - Style::<Self>::DEFAULT + fn default_style() -> Style<'static, Self> { + Style { + field: Box::new(default), + menu: menu::DefaultStyle::default_style(), + } } } diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index 7b0ea63f..38d8da85 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -13,7 +13,7 @@ use std::ops::RangeInclusive; /// /// # Example /// ```no_run -/// # type ProgressBar = iced_widget::ProgressBar; +/// # type ProgressBar<'a> = iced_widget::ProgressBar<'a>; /// # /// let value = 50.0; /// @@ -22,15 +22,15 @@ use std::ops::RangeInclusive; /// ///  #[allow(missing_debug_implementations)] -pub struct ProgressBar<Theme = crate::Theme> { +pub struct ProgressBar<'a, Theme = crate::Theme> { range: RangeInclusive<f32>, value: f32, width: Length, height: Option<Length>, - style: Style<Theme>, + style: Style<'a, Theme>, } -impl<Theme> ProgressBar<Theme> { +impl<'a, Theme> ProgressBar<'a, Theme> { /// The default height of a [`ProgressBar`]. pub const DEFAULT_HEIGHT: f32 = 30.0; @@ -41,14 +41,14 @@ impl<Theme> ProgressBar<Theme> { /// * the current value of the [`ProgressBar`] pub fn new(range: RangeInclusive<f32>, value: f32) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { ProgressBar { value: value.clamp(*range.start(), *range.end()), range, width: Length::Fill, height: None, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -65,14 +65,14 @@ impl<Theme> ProgressBar<Theme> { } /// Sets the style of the [`ProgressBar`]. - pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style.into(); + pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self { + self.style = Box::new(style); self } } -impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> - for ProgressBar<Theme> +impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> + for ProgressBar<'a, Theme> where Renderer: crate::core::Renderer, { @@ -143,7 +143,7 @@ where } } -impl<'a, Message, Theme, Renderer> From<ProgressBar<Theme>> +impl<'a, Message, Theme, Renderer> From<ProgressBar<'a, Theme>> for Element<'a, Message, Theme, Renderer> where Message: 'a, @@ -151,7 +151,7 @@ where Renderer: 'a + crate::core::Renderer, { fn from( - progress_bar: ProgressBar<Theme>, + progress_bar: ProgressBar<'a, Theme>, ) -> Element<'a, Message, Theme, Renderer> { Element::new(progress_bar) } @@ -169,23 +169,23 @@ pub struct Appearance { } /// The style of a [`ProgressBar`]. -pub type Style<Theme> = fn(&Theme) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>; /// The default style of a [`ProgressBar`]. pub trait DefaultStyle { /// Returns the default style of a [`ProgressBar`]. - fn default_style() -> Style<Self>; + fn default_style(&self) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - primary + fn default_style(&self) -> Appearance { + primary(self) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance| *appearance + fn default_style(&self) -> Appearance { + *self } } diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index 41bcb83e..90c0c970 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -19,23 +19,23 @@ const QUIET_ZONE: usize = 2; /// A type of matrix barcode consisting of squares arranged in a grid which /// can be read by an imaging device, such as a camera. -#[derive(Debug)] +#[allow(missing_debug_implementations)] pub struct QRCode<'a, Theme = crate::Theme> { data: &'a Data, cell_size: u16, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Theme> QRCode<'a, Theme> { /// Creates a new [`QRCode`] with the provided [`Data`]. pub fn new(data: &'a Data) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self { data, cell_size: DEFAULT_CELL_SIZE, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -46,8 +46,8 @@ impl<'a, Theme> QRCode<'a, Theme> { } /// Sets the style of the [`QRCode`]. - pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style.into(); + pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self { + self.style = Box::new(style); self } } @@ -336,23 +336,23 @@ pub struct Appearance { } /// The style of a [`QRCode`]. -pub type Style<Theme> = fn(&Theme) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>; /// The default style of a [`QRCode`]. pub trait DefaultStyle { /// Returns the default style of a [`QRCode`]. - fn default_style() -> Style<Self>; + fn default_style(&self) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self) -> Appearance { + default(self) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance| *appearance + fn default_style(&self) -> Appearance { + *self } } diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 5e4a3c1f..a7b7dd03 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -17,8 +17,8 @@ use crate::core::{ /// /// # Example /// ```no_run -/// # type Radio<Message> = -/// # iced_widget::Radio<Message, iced_widget::Theme, iced_widget::renderer::Renderer>; +/// # type Radio<'a, Message> = +/// # iced_widget::Radio<'a, Message, iced_widget::Theme, iced_widget::renderer::Renderer>; /// # /// # use iced_widget::column; /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -67,7 +67,7 @@ use crate::core::{ /// let content = column![a, b, c, all]; /// ``` #[allow(missing_debug_implementations)] -pub struct Radio<Message, Theme = crate::Theme, Renderer = crate::Renderer> +pub struct Radio<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> where Renderer: text::Renderer, { @@ -81,10 +81,10 @@ where text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option<Renderer::Font>, - style: Style<Theme>, + style: Style<'a, Theme>, } -impl<Message, Theme, Renderer> Radio<Message, Theme, Renderer> +impl<'a, Message, Theme, Renderer> Radio<'a, Message, Theme, Renderer> where Message: Clone, Renderer: text::Renderer, @@ -110,7 +110,7 @@ where f: F, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, 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: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -175,14 +175,17 @@ where } /// Sets the style of the [`Radio`] button. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } -impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> - for Radio<Message, Theme, Renderer> +impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> + for Radio<'a, Message, Theme, Renderer> where Message: Clone, Renderer: text::Renderer, @@ -353,7 +356,7 @@ where } } -impl<'a, Message, Theme, Renderer> From<Radio<Message, Theme, Renderer>> +impl<'a, Message, Theme, Renderer> From<Radio<'a, Message, Theme, Renderer>> for Element<'a, Message, Theme, Renderer> where Message: 'a + Clone, @@ -361,7 +364,7 @@ where Renderer: 'a + text::Renderer, { fn from( - radio: Radio<Message, Theme, Renderer>, + radio: Radio<'a, Message, Theme, Renderer>, ) -> Element<'a, Message, Theme, Renderer> { Element::new(radio) } @@ -398,23 +401,23 @@ pub struct Appearance { } /// The style of a [`Radio`] button. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Radio`] button. pub trait DefaultStyle { /// Returns the default style of a [`Radio`] button. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 8580d4c7..9fa5f74f 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -10,48 +10,49 @@ use crate::core::{ /// Display a horizontal or vertical rule for dividing content. #[allow(missing_debug_implementations)] -pub struct Rule<Theme = crate::Theme> { +pub struct Rule<'a, Theme = crate::Theme> { width: Length, height: Length, is_horizontal: bool, - style: Style<Theme>, + style: Style<'a, Theme>, } -impl<Theme> Rule<Theme> { +impl<'a, Theme> Rule<'a, Theme> { /// Creates a horizontal [`Rule`] with the given height. pub fn horizontal(height: impl Into<Pixels>) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Rule { width: Length::Fill, height: Length::Fixed(height.into().0), is_horizontal: true, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } /// Creates a vertical [`Rule`] with the given width. pub fn vertical(width: impl Into<Pixels>) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Rule { width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } /// Sets the style of the [`Rule`]. - pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self { + self.style = Box::new(style); self } } -impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Rule<Theme> +impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> + for Rule<'a, Theme> where Renderer: crate::core::Renderer, { @@ -126,14 +127,14 @@ where } } -impl<'a, Message, Theme, Renderer> From<Rule<Theme>> +impl<'a, Message, Theme, Renderer> From<Rule<'a, Theme>> for Element<'a, Message, Theme, Renderer> where Message: 'a, Theme: 'a, Renderer: 'a + crate::core::Renderer, { - fn from(rule: Rule<Theme>) -> Element<'a, Message, Theme, Renderer> { + fn from(rule: Rule<'a, Theme>) -> Element<'a, Message, Theme, Renderer> { Element::new(rule) } } @@ -216,23 +217,23 @@ impl FillMode { } /// The style of a [`Rule`]. -pub type Style<Theme> = fn(&Theme) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>; /// The default style of a [`Rule`]. pub trait DefaultStyle { /// Returns the default style of a [`Rule`]. - fn default_style() -> Style<Self>; + fn default_style(&self) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self) -> Appearance { + default(self) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance| *appearance + fn default_style(&self) -> Appearance { + *self } } diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 34312752..c03bbb7d 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -36,7 +36,7 @@ pub struct Scrollable< direction: Direction, content: Element<'a, Message, Theme, Renderer>, on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Scrollable<'a, Message, Theme, Renderer> @@ -48,7 +48,7 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self::with_direction(content, Direction::default()) } @@ -59,20 +59,16 @@ where direction: Direction, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { - Self::with_direction_and_style( - content, - direction, - Theme::default_style(), - ) + Self::with_direction_and_style(content, direction, Theme::default_style) } /// Creates a new [`Scrollable`] with the given [`Direction`] and style. pub fn with_direction_and_style( content: impl Into<Element<'a, Message, Theme, Renderer>>, direction: Direction, - style: fn(&Theme, Status) -> Appearance, + style: impl Fn(&Theme, Status) -> Appearance + 'a, ) -> Self { let content = content.into(); @@ -95,7 +91,7 @@ where direction, content, on_scroll: None, - style: style.into(), + style: Box::new(style), } } @@ -126,8 +122,11 @@ where } /// Sets the style of the [`Scrollable`] . - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } @@ -1603,23 +1602,23 @@ pub struct Scroller { } /// The style of a [`Scrollable`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Scrollable`]. pub trait DefaultStyle { /// Returns the default style of a [`Scrollable`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/slider.rs b/widget/src/slider.rs index f3ea9bfd..d3b46a98 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -49,7 +49,7 @@ pub struct Slider<'a, T, Message, Theme = crate::Theme> { on_release: Option<Message>, width: Length, height: f32, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme> @@ -70,7 +70,7 @@ where /// `Message`. pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, 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: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -131,8 +131,11 @@ where } /// Sets the style of the [`Slider`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -547,23 +550,23 @@ pub enum HandleShape { } /// The style of a [`Slider`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Slider`]. pub trait DefaultStyle { /// Returns the default style of a [`Slider`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 6e61d27a..1ac07ade 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -20,26 +20,26 @@ pub use crate::core::svg::Handle; /// [`Svg`] images can have a considerable rendering cost when resized, /// specially when they are complex. #[allow(missing_debug_implementations)] -pub struct Svg<Theme = crate::Theme> { +pub struct Svg<'a, Theme = crate::Theme> { handle: Handle, width: Length, height: Length, content_fit: ContentFit, - style: Style<Theme>, + style: Style<'a, Theme>, } -impl<Theme> Svg<Theme> { +impl<'a, Theme> Svg<'a, Theme> { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into<Handle>) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -48,7 +48,7 @@ impl<Theme> Svg<Theme> { #[must_use] pub fn from_path(path: impl Into<PathBuf>) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self::new(Handle::from_path(path)) } @@ -80,13 +80,17 @@ 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; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } -impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Svg<Theme> +impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> + for Svg<'a, Theme> where Renderer: svg::Renderer, { @@ -182,13 +186,13 @@ where } } -impl<'a, Message, Theme, Renderer> From<Svg<Theme>> +impl<'a, Message, Theme, Renderer> From<Svg<'a, Theme>> for Element<'a, Message, Theme, Renderer> where Theme: 'a, Renderer: svg::Renderer + 'a, { - fn from(icon: Svg<Theme>) -> Element<'a, Message, Theme, Renderer> { + fn from(icon: Svg<'a, Theme>) -> Element<'a, Message, Theme, Renderer> { Element::new(icon) } } @@ -214,22 +218,22 @@ pub struct Appearance { } /// The style of an [`Svg`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of an [`Svg`]. pub trait DefaultStyle { /// Returns the default style of an [`Svg`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - |_theme, _status| Appearance::default() + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default() } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index c7d62d92..5b8f6a1b 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -41,7 +41,7 @@ pub struct TextEditor< width: Length, height: Length, padding: Padding, - style: Style<Theme>, + style: Style<'a, Theme>, on_edit: Option<Box<dyn Fn(Action) -> Message + 'a>>, highlighter_settings: Highlighter::Settings, highlighter_format: fn( @@ -58,7 +58,7 @@ where /// Creates new [`TextEditor`] with the given [`Content`]. pub fn new(content: &'a Content<Renderer>) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self { content, @@ -68,7 +68,7 @@ where width: Length::Fill, height: Length::Shrink, padding: Padding::new(5.0), - style: Theme::default_style(), + style: Box::new(Theme::default_style), on_edit: None, highlighter_settings: (), highlighter_format: |_highlight, _theme| { @@ -142,8 +142,11 @@ where } /// Sets the style of the [`TextEditor`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } @@ -809,23 +812,23 @@ pub struct Appearance { } /// The style of a [`TextEditor`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`TextEditor`]. pub trait DefaultStyle { /// Returns the default style of a [`TextEditor`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 9ed8480a..b161ec74 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -75,7 +75,7 @@ pub struct TextInput< on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>, on_submit: Option<Message>, icon: Option<Icon<Renderer::Font>>, - style: Style<Theme>, + style: Style<'a, Theme>, } /// The default [`Padding`] of a [`TextInput`]. @@ -90,9 +90,9 @@ where /// its current value. pub fn new(placeholder: &str, value: &str) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { - Self::with_style(placeholder, value, Theme::default_style()) + Self::with_style(placeholder, value, Theme::default_style) } /// Creates a new [`TextInput`] with the given placeholder, @@ -100,7 +100,7 @@ where pub fn with_style( placeholder: &str, value: &str, - style: fn(&Theme, Status) -> Appearance, + style: impl Fn(&Theme, Status) -> Appearance + 'a, ) -> Self { TextInput { id: None, @@ -116,7 +116,7 @@ where on_paste: None, on_submit: None, icon: None, - style: style.into(), + style: Box::new(style), } } @@ -203,8 +203,11 @@ where } /// Sets the style of the [`TextInput`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -1413,23 +1416,23 @@ pub struct Appearance { } /// The style of a [`TextInput`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`TextInput`]. pub trait DefaultStyle { /// Returns the default style of a [`TextInput`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 9e81ba33..fc9e06e1 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -48,7 +48,7 @@ pub struct Toggler< text_shaping: text::Shaping, spacing: f32, font: Option<Renderer::Font>, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Toggler<'a, Message, Theme, Renderer> @@ -72,7 +72,7 @@ where f: F, ) -> Self where - Theme: DefaultStyle, + Theme: 'a + 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: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -145,8 +145,11 @@ where } /// Sets the style of the [`Toggler`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } @@ -398,23 +401,23 @@ pub struct Appearance { } /// The style of a [`Toggler`]. -pub type Style<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Toggler`]. pub trait DefaultStyle { /// Returns the default style of a [`Toggler`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } diff --git a/widget/src/tooltip.rs b/widget/src/tooltip.rs index 8c8ee983..8e11ca98 100644 --- a/widget/src/tooltip.rs +++ b/widget/src/tooltip.rs @@ -28,7 +28,7 @@ pub struct Tooltip< gap: f32, padding: f32, snap_within_viewport: bool, - style: container::Style<Theme>, + style: container::Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer> @@ -47,7 +47,7 @@ where position: Position, ) -> Self where - Theme: container::DefaultStyle, + Theme: container::DefaultStyle + 'a, { Tooltip { content: content.into(), @@ -56,7 +56,7 @@ where gap: 0.0, padding: Self::DEFAULT_PADDING, snap_within_viewport: true, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -81,9 +81,9 @@ where /// Sets the style of the [`Tooltip`]. pub fn style( mut self, - style: fn(&Theme, container::Status) -> container::Appearance, + style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a, ) -> Self { - self.style = style.into(); + self.style = Box::new(style); self } } @@ -239,7 +239,7 @@ where positioning: self.position, gap: self.gap, padding: self.padding, - style: self.style, + style: &self.style, }))) } else { None @@ -309,7 +309,8 @@ where positioning: Position, gap: f32, padding: f32, - style: container::Style<Theme>, + style: + &'b (dyn Fn(&Theme, container::Status) -> container::Appearance + 'a), } impl<'a, 'b, Message, Theme, Renderer> diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index f7030584..2aa8f4d1 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -51,7 +51,7 @@ pub struct VerticalSlider<'a, T, Message, Theme = crate::Theme> { on_release: Option<Message>, width: f32, height: Length, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme> @@ -72,7 +72,7 @@ where /// `Message`. pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, 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: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -133,8 +133,11 @@ where } /// Sets the style of the [`VerticalSlider`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } |