Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use closures for widget styling #2326

Merged
merged 17 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 30 additions & 53 deletions core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,15 +29,18 @@ 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>
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,
Expand All @@ -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),
}
}

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

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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>;

impl<Theme> Clone for Style<Theme> {
fn clone(&self) -> Self {
*self
}
/// The default style of some [`Text`].
pub trait DefaultStyle {
/// Returns the default style of some [`Text`].
fn default_style(&self) -> Appearance;
}

impl<Theme> Copy for Style<Theme> {}

impl<Theme> Default for Style<Theme> {
fn default() -> Self {
Style::Colored(None)
impl DefaultStyle for Theme {
fn default_style(&self) -> Appearance {
Appearance::default()
}
}

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) }
}
}
11 changes: 5 additions & 6 deletions examples/checkbox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 8 additions & 2 deletions examples/component/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions examples/gradient/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions examples/svg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
);

Expand Down
29 changes: 16 additions & 13 deletions widget/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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();
Expand All @@ -80,7 +80,7 @@ where
height: size.height.fluid(),
padding: DEFAULT_PADDING,
clip: false,
style: Theme::default_style(),
style: Box::new(Theme::default_style),
}
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
Loading
Loading