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::widget::tree::{self, Tree};
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;
@ -28,7 +29,7 @@ where
vertical_alignment: alignment::Vertical,
font: Option<Renderer::Font>,
shaping: Shaping,
style: Style<Theme>,
style: Style<'a, Theme>,
}
impl<'a, Theme, Renderer> Text<'a, Theme, Renderer>
@ -36,7 +37,10 @@ where
Renderer: text::Renderer,
{
/// 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 {
content: content.into(),
size: None,
@ -47,7 +51,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: Shaping::Basic,
style: Style::default(),
style: Box::new(Theme::default_style),
}
}
@ -72,20 +76,21 @@ where
}
/// Sets the style of the [`Text`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self {
self.style = Style::Themed(style);
pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = Box::new(style);
self
}
/// Sets the [`Color`] of the [`Text`].
pub fn color(mut self, color: impl Into<Color>) -> Self {
self.style = Style::Colored(Some(color.into()));
self
pub fn color(self, color: impl Into<Color>) -> Self {
self.color_maybe(Some(color))
}
/// Sets the [`Color`] of the [`Text`], if `Some`.
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
}
@ -183,11 +188,7 @@ where
viewport: &Rectangle,
) {
let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>();
let appearance = match self.style {
Style::Themed(f) => f(theme),
Style::Colored(color) => Appearance { color },
};
let appearance = (self.style)(theme);
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>
where
Theme: DefaultStyle + 'a,
Renderer: text::Renderer,
{
fn from(content: &'a str) -> Self {
@ -322,7 +304,7 @@ where
impl<'a, Message, Theme, Renderer> From<&'a str>
for Element<'a, Message, Theme, Renderer>
where
Theme: 'a,
Theme: DefaultStyle + 'a,
Renderer: text::Renderer + 'a,
{
fn from(content: &'a str) -> Self {
@ -339,28 +321,23 @@ pub struct Appearance {
pub color: Option<Color>,
}
#[derive(Debug)]
enum Style<Theme> {
Themed(fn(&Theme) -> Appearance),
Colored(Option<Color>),
/// The style of some [`Text`].
pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
/// 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> {
fn clone(&self) -> Self {
*self
impl DefaultStyle for Theme {
fn default_style(&self) -> Appearance {
Appearance::default()
}
}
impl<Theme> Copy for Style<Theme> {}
impl<Theme> Default for Style<Theme> {
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)
impl DefaultStyle for Color {
fn default_style(&self) -> Appearance {
Appearance { color: Some(*self) }
}
}

View file

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

View file

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

View file

@ -1,6 +1,6 @@
use iced::application;
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::{
@ -70,16 +70,16 @@ impl Sandbox for Gradient {
transparent,
} = *self;
let gradient = gradient::Linear::new(angle)
.add_stop(0.0, start)
.add_stop(1.0, end);
let gradient_box = container(horizontal_space())
.style(move |_theme, _status| {
let gradient = gradient::Linear::new(angle)
.add_stop(0.0, start)
.add_stop(1.0, end);
let gradient_box = themer(
gradient,
container(horizontal_space())
.width(Length::Fill)
.height(Length::Fill),
);
gradient.into()
})
.width(Length::Fill)
.height(Length::Fill);
let angle_picker = row![
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(
if self.apply_color_filter {
|_theme, _status| svg::Appearance {
color: Some(color!(0x0000ff)),
}
} else {
|_theme, _status| svg::Appearance::default()
|_theme, _status| svg::Appearance {
color: if self.apply_color_filter {
Some(color!(0x0000ff))
} else {
None
},
},
);

View file

@ -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)
}
}

View file

@ -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
}
}

View file

@ -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>,
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`].
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(),
}
}
}

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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.
/// 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 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(
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),
}
}
}

View file

@ -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
}
}

View file

@ -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 {

View file

@ -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
}

View file

@ -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(),
}
}
}

View file

@ -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;
///
/// ![Progress bar drawn with `iced_wgpu`](https://user-images.githubusercontent.com/18618951/71662391-a316c200-2d51-11ea-9cef-52758cab85e3.png)
#[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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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>

View file

@ -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
}