Merge pull request #2326 from iced-rs/closure-styles

Use closures for widget styling
This commit is contained in:
Héctor Ramón 2024-03-12 18:17:19 +01:00 committed by GitHub
commit 3d915d3cb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 419 additions and 446 deletions

View file

@ -6,7 +6,8 @@ use crate::renderer;
use crate::text::{self, Paragraph}; use crate::text::{self, Paragraph};
use crate::widget::tree::{self, Tree}; use crate::widget::tree::{self, Tree};
use crate::{ use crate::{
Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget, Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Theme,
Widget,
}; };
use std::borrow::Cow; use std::borrow::Cow;
@ -28,7 +29,7 @@ where
vertical_alignment: alignment::Vertical, vertical_alignment: alignment::Vertical,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
shaping: Shaping, shaping: Shaping,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Theme, Renderer> Text<'a, Theme, Renderer> impl<'a, Theme, Renderer> Text<'a, Theme, Renderer>
@ -36,7 +37,10 @@ where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
/// Create a new fragment of [`Text`] with the given contents. /// Create a new fragment of [`Text`] with the given contents.
pub fn new(content: impl Into<Cow<'a, str>>) -> Self { pub fn new(content: impl Into<Cow<'a, str>>) -> Self
where
Theme: DefaultStyle + 'a,
{
Text { Text {
content: content.into(), content: content.into(),
size: None, size: None,
@ -47,7 +51,7 @@ where
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
shaping: Shaping::Basic, shaping: Shaping::Basic,
style: Style::default(), style: Box::new(Theme::default_style),
} }
} }
@ -72,20 +76,21 @@ where
} }
/// Sets the style of the [`Text`]. /// Sets the style of the [`Text`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = Style::Themed(style); self.style = Box::new(style);
self self
} }
/// Sets the [`Color`] of the [`Text`]. /// Sets the [`Color`] of the [`Text`].
pub fn color(mut self, color: impl Into<Color>) -> Self { pub fn color(self, color: impl Into<Color>) -> Self {
self.style = Style::Colored(Some(color.into())); self.color_maybe(Some(color))
self
} }
/// Sets the [`Color`] of the [`Text`], if `Some`. /// Sets the [`Color`] of the [`Text`], if `Some`.
pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self { pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self {
self.style = Style::Colored(color.map(Into::into)); let color = color.map(Into::into);
self.style = Box::new(move |_theme| Appearance { color });
self self
} }
@ -183,11 +188,7 @@ where
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>(); let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>();
let appearance = (self.style)(theme);
let appearance = match self.style {
Style::Themed(f) => f(theme),
Style::Colored(color) => Appearance { color },
};
draw(renderer, style, layout, state, appearance, viewport); draw(renderer, style, layout, state, appearance, viewport);
} }
@ -290,28 +291,9 @@ where
} }
} }
impl<'a, Theme, Renderer> Clone for Text<'a, Theme, Renderer>
where
Renderer: text::Renderer,
{
fn clone(&self) -> Self {
Self {
content: self.content.clone(),
size: self.size,
line_height: self.line_height,
width: self.width,
height: self.height,
horizontal_alignment: self.horizontal_alignment,
vertical_alignment: self.vertical_alignment,
font: self.font,
style: self.style,
shaping: self.shaping,
}
}
}
impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer> impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer>
where where
Theme: DefaultStyle + 'a,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
fn from(content: &'a str) -> Self { fn from(content: &'a str) -> Self {
@ -322,7 +304,7 @@ where
impl<'a, Message, Theme, Renderer> From<&'a str> impl<'a, Message, Theme, Renderer> From<&'a str>
for Element<'a, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer>
where where
Theme: 'a, Theme: DefaultStyle + 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
fn from(content: &'a str) -> Self { fn from(content: &'a str) -> Self {
@ -339,28 +321,23 @@ pub struct Appearance {
pub color: Option<Color>, pub color: Option<Color>,
} }
#[derive(Debug)] /// The style of some [`Text`].
enum Style<Theme> { pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
Themed(fn(&Theme) -> Appearance),
Colored(Option<Color>), /// The default style of some [`Text`].
pub trait DefaultStyle {
/// Returns the default style of some [`Text`].
fn default_style(&self) -> Appearance;
} }
impl<Theme> Clone for Style<Theme> { impl DefaultStyle for Theme {
fn clone(&self) -> Self { fn default_style(&self) -> Appearance {
*self Appearance::default()
} }
} }
impl<Theme> Copy for Style<Theme> {} impl DefaultStyle for Color {
fn default_style(&self) -> Appearance {
impl<Theme> Default for Style<Theme> { Appearance { color: Some(*self) }
fn default() -> Self {
Style::Colored(None)
}
}
impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> {
fn from(f: fn(&Theme) -> Appearance) -> Self {
Style::Themed(f)
} }
} }

View file

@ -63,17 +63,16 @@ impl Application for Example {
let default_checkbox = checkbox("Default", self.default) let default_checkbox = checkbox("Default", self.default)
.on_toggle(Message::DefaultToggled); .on_toggle(Message::DefaultToggled);
let styled_checkbox = |label, style| { let styled_checkbox = |label| {
checkbox(label, self.styled) checkbox(label, self.styled)
.on_toggle_maybe(self.default.then_some(Message::StyledToggled)) .on_toggle_maybe(self.default.then_some(Message::StyledToggled))
.style(style)
}; };
let checkboxes = row![ let checkboxes = row![
styled_checkbox("Primary", checkbox::primary), styled_checkbox("Primary").style(checkbox::primary),
styled_checkbox("Secondary", checkbox::secondary), styled_checkbox("Secondary").style(checkbox::secondary),
styled_checkbox("Success", checkbox::success), styled_checkbox("Success").style(checkbox::success),
styled_checkbox("Danger", checkbox::danger), styled_checkbox("Danger").style(checkbox::danger),
] ]
.spacing(20); .spacing(20);

View file

@ -83,7 +83,10 @@ mod numeric_input {
impl<Message, Theme> Component<Message, Theme> for NumericInput<Message> impl<Message, Theme> Component<Message, Theme> for NumericInput<Message>
where where
Theme: button::DefaultStyle + text_input::DefaultStyle + 'static, Theme: text::DefaultStyle
+ button::DefaultStyle
+ text_input::DefaultStyle
+ 'static,
{ {
type State = (); type State = ();
type Event = Event; type Event = Event;
@ -158,7 +161,10 @@ mod numeric_input {
impl<'a, Message, Theme> From<NumericInput<Message>> impl<'a, Message, Theme> From<NumericInput<Message>>
for Element<'a, Message, Theme> for Element<'a, Message, Theme>
where where
Theme: button::DefaultStyle + text_input::DefaultStyle + 'static, Theme: text::DefaultStyle
+ button::DefaultStyle
+ text_input::DefaultStyle
+ 'static,
Message: 'a, Message: 'a,
{ {
fn from(numeric_input: NumericInput<Message>) -> Self { fn from(numeric_input: NumericInput<Message>) -> Self {

View file

@ -1,6 +1,6 @@
use iced::application; use iced::application;
use iced::widget::{ use iced::widget::{
checkbox, column, container, horizontal_space, row, slider, text, themer, checkbox, column, container, horizontal_space, row, slider, text,
}; };
use iced::{gradient, window}; use iced::{gradient, window};
use iced::{ use iced::{
@ -70,16 +70,16 @@ impl Sandbox for Gradient {
transparent, transparent,
} = *self; } = *self;
let gradient = gradient::Linear::new(angle) let gradient_box = container(horizontal_space())
.add_stop(0.0, start) .style(move |_theme, _status| {
.add_stop(1.0, end); let gradient = gradient::Linear::new(angle)
.add_stop(0.0, start)
.add_stop(1.0, end);
let gradient_box = themer( gradient.into()
gradient, })
container(horizontal_space()) .width(Length::Fill)
.width(Length::Fill) .height(Length::Fill);
.height(Length::Fill),
);
let angle_picker = row![ let angle_picker = row![
text("Angle").width(64), text("Angle").width(64),

View file

@ -41,12 +41,12 @@ impl Sandbox for Tiger {
)); ));
let svg = svg(handle).width(Length::Fill).height(Length::Fill).style( let svg = svg(handle).width(Length::Fill).height(Length::Fill).style(
if self.apply_color_filter { |_theme, _status| svg::Appearance {
|_theme, _status| svg::Appearance { color: if self.apply_color_filter {
color: Some(color!(0x0000ff)), Some(color!(0x0000ff))
} } else {
} else { None
|_theme, _status| svg::Appearance::default() },
}, },
); );

View file

@ -56,7 +56,7 @@ where
height: Length, height: Length,
padding: Padding, padding: Padding,
clip: bool, clip: bool,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer>
@ -68,7 +68,7 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
let content = content.into(); let content = content.into();
let size = content.as_widget().size_hint(); let size = content.as_widget().size_hint();
@ -80,7 +80,7 @@ where
height: size.height.fluid(), height: size.height.fluid(),
padding: DEFAULT_PADDING, padding: DEFAULT_PADDING,
clip: false, clip: false,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -120,8 +120,11 @@ where
} }
/// Sets the style variant of this [`Button`]. /// Sets the style variant of this [`Button`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
@ -439,29 +442,29 @@ impl std::default::Default for Appearance {
} }
/// The style of a [`Button`]. /// 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`]. /// The default style of a [`Button`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Button`]. /// Returns the default style of a [`Button`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
primary primary(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }
impl DefaultStyle for Color { impl DefaultStyle for Color {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|color, _status| Appearance::default().with_background(*color) Appearance::default().with_background(*self)
} }
} }

View file

@ -51,7 +51,7 @@ pub struct Checkbox<
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>, icon: Icon<Renderer::Font>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer> 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 /// * a boolean describing whether the [`Checkbox`] is checked or not
pub fn new(label: impl Into<String>, is_checked: bool) -> Self pub fn new(label: impl Into<String>, is_checked: bool) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Checkbox { Checkbox {
is_checked, is_checked,
@ -91,7 +91,7 @@ where
line_height: text::LineHeight::default(), line_height: text::LineHeight::default(),
shaping: text::Shaping::Basic, 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`]. /// Sets the style of the [`Checkbox`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -424,23 +427,23 @@ pub struct Appearance {
} }
/// The style of a [`Checkbox`]. /// 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`]. /// The default style of a [`Checkbox`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Checkbox`]. /// Returns the default style of a [`Checkbox`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
primary primary(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -42,7 +42,7 @@ pub struct ComboBox<
on_option_hovered: Option<Box<dyn Fn(T) -> Message>>, on_option_hovered: Option<Box<dyn Fn(T) -> Message>>,
on_close: Option<Message>, on_close: Option<Message>,
on_input: Option<Box<dyn Fn(String) -> Message>>, on_input: Option<Box<dyn Fn(String) -> Message>>,
menu_style: menu::Style<Theme>, menu_style: menu::Style<'a, Theme>,
padding: Padding, padding: Padding,
size: Option<f32>, size: Option<f32>,
} }
@ -62,7 +62,7 @@ where
on_selected: impl Fn(T) -> Message + 'static, on_selected: impl Fn(T) -> Message + 'static,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
let style = Theme::default_style(); let style = Theme::default_style();
@ -125,7 +125,10 @@ where
} }
/// Sets the style of the [`ComboBox`]. /// 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(); let style = style.into();
self.text_input = self.text_input.style(style.text_input); self.text_input = self.text_input.style(style.text_input);
@ -669,7 +672,7 @@ where
self.state.sync_filtered_options(filtered_options); self.state.sync_filtered_options(filtered_options);
let mut menu = menu::Menu::with_style( let mut menu = menu::Menu::new(
menu, menu,
&filtered_options.options, &filtered_options.options,
hovered_option, hovered_option,
@ -683,7 +686,7 @@ where
(self.on_selected)(x) (self.on_selected)(x)
}, },
self.on_option_hovered.as_deref(), self.on_option_hovered.as_deref(),
self.menu_style, &self.menu_style,
) )
.width(bounds.width) .width(bounds.width)
.padding(self.padding); .padding(self.padding);
@ -761,41 +764,28 @@ where
} }
/// The style of a [`ComboBox`]. /// The style of a [`ComboBox`].
#[derive(Debug, PartialEq, Eq)] #[allow(missing_debug_implementations)]
pub struct Style<Theme> { pub struct Style<'a, Theme> {
/// The style of the [`TextInput`] of the [`ComboBox`]. /// 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`]. /// The style of the [`Menu`] of the [`ComboBox`].
/// ///
/// [`Menu`]: menu::Menu /// [`Menu`]: menu::Menu
pub menu: menu::Style<Theme>, pub menu: menu::Style<'a, 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
}
}
impl<Theme> Copy for Style<Theme> {}
/// The default style of a [`ComboBox`]. /// The default style of a [`ComboBox`].
pub trait DefaultStyle: Sized { pub trait DefaultStyle: Sized {
/// Returns the default style of a [`ComboBox`]. /// Returns the default style of a [`ComboBox`].
fn default_style() -> Style<Self>; fn default_style() -> Style<'static, Self>;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style() -> Style<'static, Self> {
Style::<Self>::DEFAULT Style {
text_input: Box::new(text_input::default),
menu: menu::DefaultStyle::default_style(),
}
} }
} }

View file

@ -36,7 +36,7 @@ pub struct Container<
vertical_alignment: alignment::Vertical, vertical_alignment: alignment::Vertical,
clip: bool, clip: bool,
content: Element<'a, Message, Theme, Renderer>, content: Element<'a, Message, Theme, Renderer>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer>
@ -48,15 +48,15 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self ) -> Self
where 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. /// Creates a [`Container`] with the given content and style.
pub fn with_style( pub fn with_style(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
style: fn(&Theme, Status) -> Appearance, style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self { ) -> Self {
let content = content.into(); let content = content.into();
let size = content.as_widget().size_hint(); let size = content.as_widget().size_hint();
@ -71,8 +71,8 @@ where
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
clip: false, clip: false,
style: Box::new(style),
content, content,
style,
} }
} }
@ -137,8 +137,11 @@ where
} }
/// Sets the style of the [`Container`]. /// Sets the style of the [`Container`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self 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`]. /// The possible status of a [`Container`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status { pub enum Status {
@ -546,41 +567,41 @@ pub enum Status {
} }
/// The style of a [`Container`]. /// 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`]. /// The default style of a [`Container`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Container`]. /// Returns the default style of a [`Container`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
transparent transparent(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }
impl DefaultStyle for Color { impl DefaultStyle for Color {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|color, _status| Appearance::default().with_background(*color) Appearance::from(*self)
} }
} }
impl DefaultStyle for Gradient { impl DefaultStyle for Gradient {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|gradient, _status| Appearance::default().with_background(*gradient) Appearance::from(*self)
} }
} }
impl DefaultStyle for gradient::Linear { impl DefaultStyle for gradient::Linear {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|gradient, _status| Appearance::default().with_background(*gradient) Appearance::from(*self)
} }
} }

View file

@ -14,7 +14,7 @@ use crate::rule::{self, Rule};
use crate::runtime::Command; use crate::runtime::Command;
use crate::scrollable::{self, Scrollable}; use crate::scrollable::{self, Scrollable};
use crate::slider::{self, Slider}; use crate::slider::{self, Slider};
use crate::text::Text; use crate::text::{self, Text};
use crate::text_editor::{self, TextEditor}; use crate::text_editor::{self, TextEditor};
use crate::text_input::{self, TextInput}; use crate::text_input::{self, TextInput};
use crate::toggler::{self, Toggler}; use crate::toggler::{self, Toggler};
@ -58,7 +58,7 @@ pub fn container<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Container<'a, Message, Theme, Renderer> ) -> Container<'a, Message, Theme, Renderer>
where where
Theme: container::DefaultStyle, Theme: container::DefaultStyle + 'a,
Renderer: core::Renderer, Renderer: core::Renderer,
{ {
Container::new(content) Container::new(content)
@ -104,7 +104,7 @@ pub fn scrollable<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Scrollable<'a, Message, Theme, Renderer> ) -> Scrollable<'a, Message, Theme, Renderer>
where where
Theme: scrollable::DefaultStyle, Theme: scrollable::DefaultStyle + 'a,
Renderer: core::Renderer, Renderer: core::Renderer,
{ {
Scrollable::new(content) Scrollable::new(content)
@ -117,7 +117,7 @@ pub fn button<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Button<'a, Message, Theme, Renderer> ) -> Button<'a, Message, Theme, Renderer>
where where
Theme: button::DefaultStyle, Theme: button::DefaultStyle + 'a,
Renderer: core::Renderer, Renderer: core::Renderer,
{ {
Button::new(content) Button::new(content)
@ -134,7 +134,7 @@ pub fn tooltip<'a, Message, Theme, Renderer>(
position: tooltip::Position, position: tooltip::Position,
) -> crate::Tooltip<'a, Message, Theme, Renderer> ) -> crate::Tooltip<'a, Message, Theme, Renderer>
where where
Theme: container::DefaultStyle, Theme: container::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Tooltip::new(content, tooltip, position) Tooltip::new(content, tooltip, position)
@ -147,6 +147,7 @@ pub fn text<'a, Theme, Renderer>(
text: impl ToString, text: impl ToString,
) -> Text<'a, Theme, Renderer> ) -> Text<'a, Theme, Renderer>
where where
Theme: text::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Text::new(text.to_string()) Text::new(text.to_string())
@ -160,7 +161,7 @@ pub fn checkbox<'a, Message, Theme, Renderer>(
is_checked: bool, is_checked: bool,
) -> Checkbox<'a, Message, Theme, Renderer> ) -> Checkbox<'a, Message, Theme, Renderer>
where where
Theme: checkbox::DefaultStyle, Theme: checkbox::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Checkbox::new(label, is_checked) Checkbox::new(label, is_checked)
@ -169,15 +170,15 @@ where
/// Creates a new [`Radio`]. /// Creates a new [`Radio`].
/// ///
/// [`Radio`]: crate::Radio /// [`Radio`]: crate::Radio
pub fn radio<Message, Theme, Renderer, V>( pub fn radio<'a, Message, Theme, Renderer, V>(
label: impl Into<String>, label: impl Into<String>,
value: V, value: V,
selected: Option<V>, selected: Option<V>,
on_click: impl FnOnce(V) -> Message, on_click: impl FnOnce(V) -> Message,
) -> Radio<Message, Theme, Renderer> ) -> Radio<'a, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Theme: radio::DefaultStyle, Theme: radio::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
V: Copy + Eq, V: Copy + Eq,
{ {
@ -193,7 +194,7 @@ pub fn toggler<'a, Message, Theme, Renderer>(
f: impl Fn(bool) -> Message + 'a, f: impl Fn(bool) -> Message + 'a,
) -> Toggler<'a, Message, Theme, Renderer> ) -> Toggler<'a, Message, Theme, Renderer>
where where
Theme: toggler::DefaultStyle, Theme: toggler::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Toggler::new(label, is_checked, f) Toggler::new(label, is_checked, f)
@ -208,7 +209,7 @@ pub fn text_input<'a, Message, Theme, Renderer>(
) -> TextInput<'a, Message, Theme, Renderer> ) -> TextInput<'a, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Theme: text_input::DefaultStyle, Theme: text_input::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
TextInput::new(placeholder, value) TextInput::new(placeholder, value)
@ -217,12 +218,12 @@ where
/// Creates a new [`TextEditor`]. /// Creates a new [`TextEditor`].
/// ///
/// [`TextEditor`]: crate::TextEditor /// [`TextEditor`]: crate::TextEditor
pub fn text_editor<Message, Theme, Renderer>( pub fn text_editor<'a, Message, Theme, Renderer>(
content: &text_editor::Content<Renderer>, content: &'a text_editor::Content<Renderer>,
) -> TextEditor<'_, core::text::highlighter::PlainText, Message, Theme, Renderer> ) -> TextEditor<'a, core::text::highlighter::PlainText, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Theme: text_editor::DefaultStyle, Theme: text_editor::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
TextEditor::new(content) TextEditor::new(content)
@ -239,7 +240,7 @@ pub fn slider<'a, T, Message, Theme>(
where where
T: Copy + From<u8> + std::cmp::PartialOrd, T: Copy + From<u8> + std::cmp::PartialOrd,
Message: Clone, Message: Clone,
Theme: slider::DefaultStyle, Theme: slider::DefaultStyle + 'a,
{ {
Slider::new(range, value, on_change) Slider::new(range, value, on_change)
} }
@ -255,7 +256,7 @@ pub fn vertical_slider<'a, T, Message, Theme>(
where where
T: Copy + From<u8> + std::cmp::PartialOrd, T: Copy + From<u8> + std::cmp::PartialOrd,
Message: Clone, Message: Clone,
Theme: vertical_slider::DefaultStyle, Theme: vertical_slider::DefaultStyle + 'a,
{ {
VerticalSlider::new(range, value, on_change) 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> ) -> ComboBox<'a, T, Message, Theme, Renderer>
where where
T: std::fmt::Display + Clone, T: std::fmt::Display + Clone,
Theme: combo_box::DefaultStyle, Theme: combo_box::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
ComboBox::new(state, placeholder, selection, on_selected) ComboBox::new(state, placeholder, selection, on_selected)
@ -315,9 +316,9 @@ pub fn vertical_space() -> Space {
/// Creates a horizontal [`Rule`] with the given height. /// Creates a horizontal [`Rule`] with the given height.
/// ///
/// [`Rule`]: crate::Rule /// [`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 where
Theme: rule::DefaultStyle, Theme: rule::DefaultStyle + 'a,
{ {
Rule::horizontal(height) Rule::horizontal(height)
} }
@ -325,9 +326,9 @@ where
/// Creates a vertical [`Rule`] with the given width. /// Creates a vertical [`Rule`] with the given width.
/// ///
/// [`Rule`]: crate::Rule /// [`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 where
Theme: rule::DefaultStyle, Theme: rule::DefaultStyle + 'a,
{ {
Rule::vertical(width) Rule::vertical(width)
} }
@ -339,12 +340,12 @@ where
/// * the current value of the [`ProgressBar`]. /// * the current value of the [`ProgressBar`].
/// ///
/// [`ProgressBar`]: crate::ProgressBar /// [`ProgressBar`]: crate::ProgressBar
pub fn progress_bar<Theme>( pub fn progress_bar<'a, Theme>(
range: RangeInclusive<f32>, range: RangeInclusive<f32>,
value: f32, value: f32,
) -> ProgressBar<Theme> ) -> ProgressBar<'a, Theme>
where where
Theme: progress_bar::DefaultStyle, Theme: progress_bar::DefaultStyle + 'a,
{ {
ProgressBar::new(range, value) ProgressBar::new(range, value)
} }
@ -362,9 +363,11 @@ pub fn image<Handle>(handle: impl Into<Handle>) -> crate::Image<Handle> {
/// [`Svg`]: crate::Svg /// [`Svg`]: crate::Svg
/// [`Handle`]: crate::svg::Handle /// [`Handle`]: crate::svg::Handle
#[cfg(feature = "svg")] #[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 where
Theme: crate::svg::DefaultStyle, Theme: crate::svg::DefaultStyle + 'a,
{ {
crate::Svg::new(handle) crate::Svg::new(handle)
} }
@ -388,9 +391,11 @@ where
/// [`QRCode`]: crate::QRCode /// [`QRCode`]: crate::QRCode
/// [`Data`]: crate::qr_code::Data /// [`Data`]: crate::qr_code::Data
#[cfg(feature = "qr_code")] #[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 where
Theme: crate::qr_code::DefaultStyle, Theme: crate::qr_code::DefaultStyle + 'a,
{ {
crate::QRCode::new(data) crate::QRCode::new(data)
} }

View file

@ -38,7 +38,7 @@ pub struct Menu<
text_line_height: text::LineHeight, text_line_height: text::LineHeight,
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
style: Style<Theme>, style: &'a Style<'a, Theme>,
} }
impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer> impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer>
@ -48,37 +48,15 @@ where
Theme: 'a, Theme: 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
/// Creates a new [`Menu`] with the given [`State`], a list of options, and /// Creates a new [`Menu`] with the given [`State`], a list of options,
/// the message to produced when an option is selected. /// the message to produced when an option is selected, and its [`Style`].
pub fn new( pub fn new(
state: &'a mut State, state: &'a mut State,
options: &'a [T], options: &'a [T],
hovered_option: &'a mut Option<usize>, hovered_option: &'a mut Option<usize>,
on_selected: impl FnMut(T) -> Message + 'a, on_selected: impl FnMut(T) -> Message + 'a,
on_option_hovered: Option<&'a dyn Fn(T) -> Message>, on_option_hovered: Option<&'a dyn Fn(T) -> Message>,
) -> Self style: &'a Style<'a, Theme>,
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(
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>,
) -> Self { ) -> Self {
Menu { Menu {
state, state,
@ -135,12 +113,6 @@ where
self 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 /// Turns the [`Menu`] into an overlay [`Element`] at the given target
/// position. /// position.
/// ///
@ -190,7 +162,7 @@ where
container: Container<'a, Message, Theme, Renderer>, container: Container<'a, Message, Theme, Renderer>,
width: f32, width: f32,
target_height: f32, target_height: f32,
style: Style<Theme>, style: &'a Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer>
@ -234,10 +206,10 @@ where
text_line_height, text_line_height,
text_shaping, text_shaping,
padding, padding,
style: style.list, style: &style.list,
}, },
scrollable::Direction::default(), scrollable::Direction::default(),
style.scrollable, &style.scrollable,
), ),
container::transparent, container::transparent,
); );
@ -356,7 +328,7 @@ where
text_line_height: text::LineHeight, text_line_height: text::LineHeight,
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
style: fn(&Theme) -> Appearance, style: &'a dyn Fn(&Theme) -> Appearance,
} }
impl<'a, T, Message, Theme, Renderer> Widget<Message, Theme, Renderer> 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`]. /// The style of the different parts of a [`Menu`].
#[derive(Debug, PartialEq, Eq)] #[allow(missing_debug_implementations)]
pub struct Style<Theme> { pub struct Style<'a, Theme> {
/// The style of the list of the [`Menu`]. /// 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`]. /// 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`]. /// The default style of a [`Menu`].
pub trait DefaultStyle: Sized { pub trait DefaultStyle: Sized {
/// Returns the default style of a [`Menu`]. /// Returns the default style of a [`Menu`].
fn default_style() -> Style<Self>; fn default_style() -> Style<'static, Self>;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style() -> Style<'static, Self> {
Style::<Theme>::DEFAULT Style {
list: Box::new(default),
scrollable: Box::new(scrollable::default),
}
} }
} }

View file

@ -110,7 +110,7 @@ pub struct PaneGrid<
on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>,
on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
on_resize: Option<(f32, Box<dyn Fn(ResizeEvent) -> 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> 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>, view: impl Fn(Pane, &'a T, bool) -> Content<'a, Message, Theme, Renderer>,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
let contents = if let Some((pane, pane_state)) = let contents = if let Some((pane, pane_state)) =
state.maximized.and_then(|pane| { state.maximized.and_then(|pane| {
@ -158,7 +158,7 @@ where
on_click: None, on_click: None,
on_drag: None, on_drag: None,
on_resize: 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`]. /// Sets the style of the [`PaneGrid`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = style; self.style = Box::new(style);
self self
} }
@ -1146,23 +1146,23 @@ pub struct Line {
} }
/// The style of a [`PaneGrid`]. /// 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`]. /// The default style of a [`PaneGrid`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`PaneGrid`]. /// Returns the default style of a [`PaneGrid`].
fn default_style() -> Style<Self>; fn default_style(&self) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
default default(self)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
|appearance| *appearance *self
} }
} }

View file

@ -24,7 +24,7 @@ pub struct Content<
{ {
title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>, title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>,
body: Element<'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> impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer>
@ -34,12 +34,12 @@ where
/// Creates a new [`Content`] with the provided body. /// Creates a new [`Content`] with the provided body.
pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self
where where
Theme: container::DefaultStyle, Theme: container::DefaultStyle + 'a,
{ {
Self { Self {
title_bar: None, title_bar: None,
body: body.into(), body: body.into(),
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -55,9 +55,9 @@ where
/// Sets the style of the [`Content`]. /// Sets the style of the [`Content`].
pub fn style( pub fn style(
mut self, mut self,
style: fn(&Theme, container::Status) -> container::Appearance, style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
) -> Self { ) -> Self {
self.style = style.into(); self.style = Box::new(style);
self self
} }
} }
@ -403,7 +403,7 @@ impl<'a, T, Message, Theme, Renderer> From<T>
for Content<'a, Message, Theme, Renderer> for Content<'a, Message, Theme, Renderer>
where where
T: Into<Element<'a, Message, Theme, Renderer>>, T: Into<Element<'a, Message, Theme, Renderer>>,
Theme: container::DefaultStyle, Theme: container::DefaultStyle + 'a,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
fn from(element: T) -> Self { fn from(element: T) -> Self {

View file

@ -25,7 +25,7 @@ pub struct TitleBar<
controls: Option<Element<'a, Message, Theme, Renderer>>, controls: Option<Element<'a, Message, Theme, Renderer>>,
padding: Padding, padding: Padding,
always_show_controls: bool, always_show_controls: bool,
style: container::Style<Theme>, style: container::Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer>
@ -37,14 +37,14 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self ) -> Self
where where
Theme: container::DefaultStyle, Theme: container::DefaultStyle + 'a,
{ {
Self { Self {
content: content.into(), content: content.into(),
controls: None, controls: None,
padding: Padding::ZERO, padding: Padding::ZERO,
always_show_controls: false, 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`]. /// Sets the style of the [`TitleBar`].
pub fn style( pub fn style(
mut self, mut self,
style: fn(&Theme, container::Status) -> container::Appearance, style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
) -> Self { ) -> Self {
self.style = style.into(); self.style = Box::new(style);
self self
} }

View file

@ -47,7 +47,7 @@ pub struct PickList<
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
handle: Handle<Renderer::Font>, handle: Handle<Renderer::Font>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, T, L, V, Message, Theme, Renderer> impl<'a, T, L, V, Message, Theme, Renderer>
@ -151,7 +151,7 @@ where
} }
/// Sets the style of the [`PickList`]. /// 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.style = style.into();
self self
} }
@ -529,7 +529,7 @@ where
let on_select = &self.on_select; let on_select = &self.on_select;
let mut menu = Menu::with_style( let mut menu = Menu::new(
&mut state.menu, &mut state.menu,
self.options.borrow(), self.options.borrow(),
&mut state.hovered_option, &mut state.hovered_option,
@ -539,7 +539,7 @@ where
(on_select)(option) (on_select)(option)
}, },
None, None,
self.style.menu, &self.style.menu,
) )
.width(bounds.width) .width(bounds.width)
.padding(self.padding) .padding(self.padding)
@ -676,40 +676,27 @@ pub struct Appearance {
} }
/// The styles of the different parts of a [`PickList`]. /// The styles of the different parts of a [`PickList`].
#[derive(Debug, PartialEq, Eq)] #[allow(missing_debug_implementations)]
pub struct Style<Theme> { pub struct Style<'a, Theme> {
/// The style of the [`PickList`] itself. /// 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. /// 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`]. /// The default style of a [`PickList`].
pub trait DefaultStyle: Sized { pub trait DefaultStyle: Sized {
/// Returns the default style of a [`PickList`]. /// Returns the default style of a [`PickList`].
fn default_style() -> Style<Self>; fn default_style() -> Style<'static, Self>;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style() -> Style<'static, Self> {
Style::<Self>::DEFAULT Style {
field: Box::new(default),
menu: menu::DefaultStyle::default_style(),
}
} }
} }

View file

@ -13,7 +13,7 @@ use std::ops::RangeInclusive;
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
/// # type ProgressBar = iced_widget::ProgressBar; /// # type ProgressBar<'a> = iced_widget::ProgressBar<'a>;
/// # /// #
/// let value = 50.0; /// let value = 50.0;
/// ///
@ -22,15 +22,15 @@ use std::ops::RangeInclusive;
/// ///
/// ![Progress bar drawn with `iced_wgpu`](https://user-images.githubusercontent.com/18618951/71662391-a316c200-2d51-11ea-9cef-52758cab85e3.png) /// ![Progress bar drawn with `iced_wgpu`](https://user-images.githubusercontent.com/18618951/71662391-a316c200-2d51-11ea-9cef-52758cab85e3.png)
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct ProgressBar<Theme = crate::Theme> { pub struct ProgressBar<'a, Theme = crate::Theme> {
range: RangeInclusive<f32>, range: RangeInclusive<f32>,
value: f32, value: f32,
width: Length, width: Length,
height: Option<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`]. /// The default height of a [`ProgressBar`].
pub const DEFAULT_HEIGHT: f32 = 30.0; pub const DEFAULT_HEIGHT: f32 = 30.0;
@ -41,14 +41,14 @@ impl<Theme> ProgressBar<Theme> {
/// * the current value of the [`ProgressBar`] /// * the current value of the [`ProgressBar`]
pub fn new(range: RangeInclusive<f32>, value: f32) -> Self pub fn new(range: RangeInclusive<f32>, value: f32) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
ProgressBar { ProgressBar {
value: value.clamp(*range.start(), *range.end()), value: value.clamp(*range.start(), *range.end()),
range, range,
width: Length::Fill, width: Length::Fill,
height: None, 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`]. /// Sets the style of the [`ProgressBar`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = style.into(); self.style = Box::new(style);
self self
} }
} }
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for ProgressBar<Theme> for ProgressBar<'a, Theme>
where where
Renderer: crate::core::Renderer, 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> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
@ -151,7 +151,7 @@ where
Renderer: 'a + crate::core::Renderer, Renderer: 'a + crate::core::Renderer,
{ {
fn from( fn from(
progress_bar: ProgressBar<Theme>, progress_bar: ProgressBar<'a, Theme>,
) -> Element<'a, Message, Theme, Renderer> { ) -> Element<'a, Message, Theme, Renderer> {
Element::new(progress_bar) Element::new(progress_bar)
} }
@ -169,23 +169,23 @@ pub struct Appearance {
} }
/// The style of a [`ProgressBar`]. /// 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`]. /// The default style of a [`ProgressBar`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`ProgressBar`]. /// Returns the default style of a [`ProgressBar`].
fn default_style() -> Style<Self>; fn default_style(&self) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
primary primary(self)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
|appearance| *appearance *self
} }
} }

View file

@ -19,23 +19,23 @@ const QUIET_ZONE: usize = 2;
/// A type of matrix barcode consisting of squares arranged in a grid which /// A type of matrix barcode consisting of squares arranged in a grid which
/// can be read by an imaging device, such as a camera. /// can be read by an imaging device, such as a camera.
#[derive(Debug)] #[allow(missing_debug_implementations)]
pub struct QRCode<'a, Theme = crate::Theme> { pub struct QRCode<'a, Theme = crate::Theme> {
data: &'a Data, data: &'a Data,
cell_size: u16, cell_size: u16,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Theme> QRCode<'a, Theme> { impl<'a, Theme> QRCode<'a, Theme> {
/// Creates a new [`QRCode`] with the provided [`Data`]. /// Creates a new [`QRCode`] with the provided [`Data`].
pub fn new(data: &'a Data) -> Self pub fn new(data: &'a Data) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self { Self {
data, data,
cell_size: DEFAULT_CELL_SIZE, 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`]. /// Sets the style of the [`QRCode`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = style.into(); self.style = Box::new(style);
self self
} }
} }
@ -336,23 +336,23 @@ pub struct Appearance {
} }
/// The style of a [`QRCode`]. /// 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`]. /// The default style of a [`QRCode`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`QRCode`]. /// Returns the default style of a [`QRCode`].
fn default_style() -> Style<Self>; fn default_style(&self) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
default default(self)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
|appearance| *appearance *self
} }
} }

View file

@ -17,8 +17,8 @@ use crate::core::{
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
/// # type Radio<Message> = /// # type Radio<'a, Message> =
/// # iced_widget::Radio<Message, iced_widget::Theme, iced_widget::renderer::Renderer>; /// # iced_widget::Radio<'a, Message, iced_widget::Theme, iced_widget::renderer::Renderer>;
/// # /// #
/// # use iced_widget::column; /// # use iced_widget::column;
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -67,7 +67,7 @@ use crate::core::{
/// let content = column![a, b, c, all]; /// let content = column![a, b, c, all];
/// ``` /// ```
#[allow(missing_debug_implementations)] #[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 where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
@ -81,10 +81,10 @@ where
text_line_height: text::LineHeight, text_line_height: text::LineHeight,
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, 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 where
Message: Clone, Message: Clone,
Renderer: text::Renderer, Renderer: text::Renderer,
@ -110,7 +110,7 @@ where
f: F, f: F,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
V: Eq + Copy, V: Eq + Copy,
F: FnOnce(V) -> Message, F: FnOnce(V) -> Message,
{ {
@ -125,7 +125,7 @@ where
text_line_height: text::LineHeight::default(), text_line_height: text::LineHeight::default(),
text_shaping: text::Shaping::Basic, text_shaping: text::Shaping::Basic,
font: None, font: None,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -175,14 +175,17 @@ where
} }
/// Sets the style of the [`Radio`] button. /// Sets the style of the [`Radio`] button.
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Radio<Message, Theme, Renderer> for Radio<'a, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Renderer: text::Renderer, 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> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a + Clone, Message: 'a + Clone,
@ -361,7 +364,7 @@ where
Renderer: 'a + text::Renderer, Renderer: 'a + text::Renderer,
{ {
fn from( fn from(
radio: Radio<Message, Theme, Renderer>, radio: Radio<'a, Message, Theme, Renderer>,
) -> Element<'a, Message, Theme, Renderer> { ) -> Element<'a, Message, Theme, Renderer> {
Element::new(radio) Element::new(radio)
} }
@ -398,23 +401,23 @@ pub struct Appearance {
} }
/// The style of a [`Radio`] button. /// 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. /// The default style of a [`Radio`] button.
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Radio`] button. /// Returns the default style of a [`Radio`] button.
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -10,48 +10,49 @@ use crate::core::{
/// Display a horizontal or vertical rule for dividing content. /// Display a horizontal or vertical rule for dividing content.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Rule<Theme = crate::Theme> { pub struct Rule<'a, Theme = crate::Theme> {
width: Length, width: Length,
height: Length, height: Length,
is_horizontal: bool, 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. /// Creates a horizontal [`Rule`] with the given height.
pub fn horizontal(height: impl Into<Pixels>) -> Self pub fn horizontal(height: impl Into<Pixels>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Rule { Rule {
width: Length::Fill, width: Length::Fill,
height: Length::Fixed(height.into().0), height: Length::Fixed(height.into().0),
is_horizontal: true, is_horizontal: true,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
/// Creates a vertical [`Rule`] with the given width. /// Creates a vertical [`Rule`] with the given width.
pub fn vertical(width: impl Into<Pixels>) -> Self pub fn vertical(width: impl Into<Pixels>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Rule { Rule {
width: Length::Fixed(width.into().0), width: Length::Fixed(width.into().0),
height: Length::Fill, height: Length::Fill,
is_horizontal: false, is_horizontal: false,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
/// Sets the style of the [`Rule`]. /// Sets the style of the [`Rule`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = style; self.style = Box::new(style);
self 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 where
Renderer: crate::core::Renderer, 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> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: 'a, Theme: 'a,
Renderer: 'a + crate::core::Renderer, 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) Element::new(rule)
} }
} }
@ -216,23 +217,23 @@ impl FillMode {
} }
/// The style of a [`Rule`]. /// 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`]. /// The default style of a [`Rule`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Rule`]. /// Returns the default style of a [`Rule`].
fn default_style() -> Style<Self>; fn default_style(&self) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
default default(self)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
|appearance| *appearance *self
} }
} }

View file

@ -36,7 +36,7 @@ pub struct Scrollable<
direction: Direction, direction: Direction,
content: Element<'a, Message, Theme, Renderer>, content: Element<'a, Message, Theme, Renderer>,
on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>, 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> impl<'a, Message, Theme, Renderer> Scrollable<'a, Message, Theme, Renderer>
@ -48,7 +48,7 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self::with_direction(content, Direction::default()) Self::with_direction(content, Direction::default())
} }
@ -59,20 +59,16 @@ where
direction: Direction, direction: Direction,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self::with_direction_and_style( Self::with_direction_and_style(content, direction, Theme::default_style)
content,
direction,
Theme::default_style(),
)
} }
/// Creates a new [`Scrollable`] with the given [`Direction`] and style. /// Creates a new [`Scrollable`] with the given [`Direction`] and style.
pub fn with_direction_and_style( pub fn with_direction_and_style(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
direction: Direction, direction: Direction,
style: fn(&Theme, Status) -> Appearance, style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self { ) -> Self {
let content = content.into(); let content = content.into();
@ -95,7 +91,7 @@ where
direction, direction,
content, content,
on_scroll: None, on_scroll: None,
style: style.into(), style: Box::new(style),
} }
} }
@ -126,8 +122,11 @@ where
} }
/// Sets the style of the [`Scrollable`] . /// Sets the style of the [`Scrollable`] .
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -1603,23 +1602,23 @@ pub struct Scroller {
} }
/// The style of a [`Scrollable`]. /// 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`]. /// The default style of a [`Scrollable`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Scrollable`]. /// Returns the default style of a [`Scrollable`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -49,7 +49,7 @@ pub struct Slider<'a, T, Message, Theme = crate::Theme> {
on_release: Option<Message>, on_release: Option<Message>,
width: Length, width: Length,
height: f32, height: f32,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme> impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme>
@ -70,7 +70,7 @@ where
/// `Message`. /// `Message`.
pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
F: 'a + Fn(T) -> Message, F: 'a + Fn(T) -> Message,
{ {
let value = if value >= *range.start() { let value = if value >= *range.start() {
@ -95,7 +95,7 @@ where
on_release: None, on_release: None,
width: Length::Fill, width: Length::Fill,
height: Self::DEFAULT_HEIGHT, 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`]. /// Sets the style of the [`Slider`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
@ -547,23 +550,23 @@ pub enum HandleShape {
} }
/// The style of a [`Slider`]. /// 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`]. /// The default style of a [`Slider`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Slider`]. /// Returns the default style of a [`Slider`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -20,26 +20,26 @@ pub use crate::core::svg::Handle;
/// [`Svg`] images can have a considerable rendering cost when resized, /// [`Svg`] images can have a considerable rendering cost when resized,
/// specially when they are complex. /// specially when they are complex.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Svg<Theme = crate::Theme> { pub struct Svg<'a, Theme = crate::Theme> {
handle: Handle, handle: Handle,
width: Length, width: Length,
height: Length, height: Length,
content_fit: ContentFit, 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`]. /// Creates a new [`Svg`] from the given [`Handle`].
pub fn new(handle: impl Into<Handle>) -> Self pub fn new(handle: impl Into<Handle>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Svg { Svg {
handle: handle.into(), handle: handle.into(),
width: Length::Fill, width: Length::Fill,
height: Length::Shrink, height: Length::Shrink,
content_fit: ContentFit::Contain, content_fit: ContentFit::Contain,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -48,7 +48,7 @@ impl<Theme> Svg<Theme> {
#[must_use] #[must_use]
pub fn from_path(path: impl Into<PathBuf>) -> Self pub fn from_path(path: impl Into<PathBuf>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self::new(Handle::from_path(path)) Self::new(Handle::from_path(path))
} }
@ -80,13 +80,17 @@ impl<Theme> Svg<Theme> {
/// Sets the style variant of this [`Svg`]. /// Sets the style variant of this [`Svg`].
#[must_use] #[must_use]
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self 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 where
Renderer: svg::Renderer, 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> for Element<'a, Message, Theme, Renderer>
where where
Theme: 'a, Theme: 'a,
Renderer: svg::Renderer + '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) Element::new(icon)
} }
} }
@ -214,22 +218,22 @@ pub struct Appearance {
} }
/// The style of an [`Svg`]. /// 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`]. /// The default style of an [`Svg`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of an [`Svg`]. /// Returns the default style of an [`Svg`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|_theme, _status| Appearance::default() Appearance::default()
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -41,7 +41,7 @@ pub struct TextEditor<
width: Length, width: Length,
height: Length, height: Length,
padding: Padding, padding: Padding,
style: Style<Theme>, style: Style<'a, Theme>,
on_edit: Option<Box<dyn Fn(Action) -> Message + 'a>>, on_edit: Option<Box<dyn Fn(Action) -> Message + 'a>>,
highlighter_settings: Highlighter::Settings, highlighter_settings: Highlighter::Settings,
highlighter_format: fn( highlighter_format: fn(
@ -58,7 +58,7 @@ where
/// Creates new [`TextEditor`] with the given [`Content`]. /// Creates new [`TextEditor`] with the given [`Content`].
pub fn new(content: &'a Content<Renderer>) -> Self pub fn new(content: &'a Content<Renderer>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self { Self {
content, content,
@ -68,7 +68,7 @@ where
width: Length::Fill, width: Length::Fill,
height: Length::Shrink, height: Length::Shrink,
padding: Padding::new(5.0), padding: Padding::new(5.0),
style: Theme::default_style(), style: Box::new(Theme::default_style),
on_edit: None, on_edit: None,
highlighter_settings: (), highlighter_settings: (),
highlighter_format: |_highlight, _theme| { highlighter_format: |_highlight, _theme| {
@ -142,8 +142,11 @@ where
} }
/// Sets the style of the [`TextEditor`]. /// Sets the style of the [`TextEditor`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -809,23 +812,23 @@ pub struct Appearance {
} }
/// The style of a [`TextEditor`]. /// 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`]. /// The default style of a [`TextEditor`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`TextEditor`]. /// Returns the default style of a [`TextEditor`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -75,7 +75,7 @@ pub struct TextInput<
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>, on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_submit: Option<Message>, on_submit: Option<Message>,
icon: Option<Icon<Renderer::Font>>, icon: Option<Icon<Renderer::Font>>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
/// The default [`Padding`] of a [`TextInput`]. /// The default [`Padding`] of a [`TextInput`].
@ -90,9 +90,9 @@ where
/// its current value. /// its current value.
pub fn new(placeholder: &str, value: &str) -> Self pub fn new(placeholder: &str, value: &str) -> Self
where 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, /// Creates a new [`TextInput`] with the given placeholder,
@ -100,7 +100,7 @@ where
pub fn with_style( pub fn with_style(
placeholder: &str, placeholder: &str,
value: &str, value: &str,
style: fn(&Theme, Status) -> Appearance, style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self { ) -> Self {
TextInput { TextInput {
id: None, id: None,
@ -116,7 +116,7 @@ where
on_paste: None, on_paste: None,
on_submit: None, on_submit: None,
icon: None, icon: None,
style: style.into(), style: Box::new(style),
} }
} }
@ -203,8 +203,11 @@ where
} }
/// Sets the style of the [`TextInput`]. /// Sets the style of the [`TextInput`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
@ -1413,23 +1416,23 @@ pub struct Appearance {
} }
/// The style of a [`TextInput`]. /// 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`]. /// The default style of a [`TextInput`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`TextInput`]. /// Returns the default style of a [`TextInput`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -48,7 +48,7 @@ pub struct Toggler<
text_shaping: text::Shaping, text_shaping: text::Shaping,
spacing: f32, spacing: f32,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Toggler<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Toggler<'a, Message, Theme, Renderer>
@ -72,7 +72,7 @@ where
f: F, f: F,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: 'a + DefaultStyle,
F: 'a + Fn(bool) -> Message, F: 'a + Fn(bool) -> Message,
{ {
Toggler { Toggler {
@ -87,7 +87,7 @@ where
text_shaping: text::Shaping::Basic, text_shaping: text::Shaping::Basic,
spacing: Self::DEFAULT_SIZE / 2.0, spacing: Self::DEFAULT_SIZE / 2.0,
font: None, font: None,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -145,8 +145,11 @@ where
} }
/// Sets the style of the [`Toggler`]. /// Sets the style of the [`Toggler`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -398,23 +401,23 @@ pub struct Appearance {
} }
/// The style of a [`Toggler`]. /// 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`]. /// The default style of a [`Toggler`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Toggler`]. /// Returns the default style of a [`Toggler`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -28,7 +28,7 @@ pub struct Tooltip<
gap: f32, gap: f32,
padding: f32, padding: f32,
snap_within_viewport: bool, snap_within_viewport: bool,
style: container::Style<Theme>, style: container::Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer>
@ -47,7 +47,7 @@ where
position: Position, position: Position,
) -> Self ) -> Self
where where
Theme: container::DefaultStyle, Theme: container::DefaultStyle + 'a,
{ {
Tooltip { Tooltip {
content: content.into(), content: content.into(),
@ -56,7 +56,7 @@ where
gap: 0.0, gap: 0.0,
padding: Self::DEFAULT_PADDING, padding: Self::DEFAULT_PADDING,
snap_within_viewport: true, 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`]. /// Sets the style of the [`Tooltip`].
pub fn style( pub fn style(
mut self, mut self,
style: fn(&Theme, container::Status) -> container::Appearance, style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
) -> Self { ) -> Self {
self.style = style.into(); self.style = Box::new(style);
self self
} }
} }
@ -239,7 +239,7 @@ where
positioning: self.position, positioning: self.position,
gap: self.gap, gap: self.gap,
padding: self.padding, padding: self.padding,
style: self.style, style: &self.style,
}))) })))
} else { } else {
None None
@ -309,7 +309,8 @@ where
positioning: Position, positioning: Position,
gap: f32, gap: f32,
padding: f32, padding: f32,
style: container::Style<Theme>, style:
&'b (dyn Fn(&Theme, container::Status) -> container::Appearance + 'a),
} }
impl<'a, 'b, Message, Theme, Renderer> impl<'a, 'b, Message, Theme, Renderer>

View file

@ -51,7 +51,7 @@ pub struct VerticalSlider<'a, T, Message, Theme = crate::Theme> {
on_release: Option<Message>, on_release: Option<Message>,
width: f32, width: f32,
height: Length, height: Length,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme> impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme>
@ -72,7 +72,7 @@ where
/// `Message`. /// `Message`.
pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
F: 'a + Fn(T) -> Message, F: 'a + Fn(T) -> Message,
{ {
let value = if value >= *range.start() { let value = if value >= *range.start() {
@ -97,7 +97,7 @@ where
on_release: None, on_release: None,
width: Self::DEFAULT_WIDTH, width: Self::DEFAULT_WIDTH,
height: Length::Fill, height: Length::Fill,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -133,8 +133,11 @@ where
} }
/// Sets the style of the [`VerticalSlider`]. /// Sets the style of the [`VerticalSlider`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }