From f4a4845ddbdced81ae4ff60bfa19f0e602d84709 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Mar 2024 20:42:37 +0100 Subject: Simplify theming for `Button` widget --- widget/src/button.rs | 382 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 234 insertions(+), 148 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 867fbfaf..798a8206 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -10,11 +10,11 @@ use crate::core::touch; use crate::core::widget::tree::{self, Tree}; use crate::core::widget::Operation; use crate::core::{ - Background, Clipboard, Color, Element, Layout, Length, Padding, Rectangle, - Shell, Size, Vector, Widget, + Background, Border, Clipboard, Color, Element, Layout, Length, Padding, + Rectangle, Shadow, Shell, Size, Vector, Widget, }; - -pub use crate::style::button::{Appearance, StyleSheet}; +use crate::style::theme::palette; +use crate::style::Theme; /// A generic widget that produces a message when pressed. /// @@ -53,7 +53,7 @@ pub use crate::style::button::{Appearance, StyleSheet}; #[allow(missing_debug_implementations)] pub struct Button<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> where - Theme: StyleSheet, + Theme: Style, Renderer: crate::core::Renderer, { content: Element<'a, Message, Theme, Renderer>, @@ -62,12 +62,12 @@ where height: Length, padding: Padding, clip: bool, - style: Theme::Style, + style: fn(&Theme, Status) -> Appearance, } impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> where - Theme: StyleSheet, + Theme: Style, Renderer: crate::core::Renderer, { /// Creates a new [`Button`] with the given content. @@ -84,7 +84,7 @@ where height: size.height.fluid(), padding: Padding::new(5.0), clip: false, - style: Theme::Style::default(), + style: Theme::DEFAULT, } } @@ -124,7 +124,7 @@ where } /// Sets the style variant of this [`Button`]. - pub fn style(mut self, style: impl Into) -> Self { + pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { self.style = style.into(); self } @@ -137,11 +137,16 @@ where } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +struct State { + is_pressed: bool, +} + impl<'a, Message, Theme, Renderer> Widget for Button<'a, Message, Theme, Renderer> where Message: 'a + Clone, - Theme: StyleSheet, + Theme: Style, Renderer: 'a + crate::core::Renderer, { fn tag(&self) -> tree::Tag { @@ -149,7 +154,7 @@ where } fn state(&self) -> tree::State { - tree::State::new(State::new()) + tree::State::new(State::default()) } fn children(&self) -> Vec { @@ -173,13 +178,19 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - layout(limits, self.width, self.height, self.padding, |limits| { - self.content.as_widget().layout( - &mut tree.children[0], - renderer, - limits, - ) - }) + layout::padded( + limits, + self.width, + self.height, + self.padding, + |limits| { + self.content.as_widget().layout( + &mut tree.children[0], + renderer, + limits, + ) + }, + ) } fn operate( @@ -223,9 +234,48 @@ where return event::Status::Captured; } - update(event, layout, cursor, shell, &self.on_press, || { - tree.state.downcast_mut::() - }) + match event { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) + | Event::Touch(touch::Event::FingerPressed { .. }) => { + if self.on_press.is_some() { + let bounds = layout.bounds(); + + if cursor.is_over(bounds) { + let state = tree.state.downcast_mut::(); + + state.is_pressed = true; + + return event::Status::Captured; + } + } + } + Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) + | Event::Touch(touch::Event::FingerLifted { .. }) => { + if let Some(on_press) = self.on_press.clone() { + let state = tree.state.downcast_mut::(); + + if state.is_pressed { + state.is_pressed = false; + + let bounds = layout.bounds(); + + if cursor.is_over(bounds) { + shell.publish(on_press); + } + + return event::Status::Captured; + } + } + } + Event::Touch(touch::Event::FingerLost { .. }) => { + let state = tree.state.downcast_mut::(); + + state.is_pressed = false; + } + _ => {} + } + + event::Status::Ignored } fn draw( @@ -240,16 +290,39 @@ where ) { let bounds = layout.bounds(); let content_layout = layout.children().next().unwrap(); + let is_mouse_over = cursor.is_over(bounds); - let styling = draw( - renderer, - bounds, - cursor, - self.on_press.is_some(), - theme, - &self.style, - || tree.state.downcast_ref::(), - ); + let status = if self.on_press.is_none() { + Status::Disabled + } else if is_mouse_over { + let state = tree.state.downcast_ref::(); + + if state.is_pressed { + Status::Pressed + } else { + Status::Hovered + } + } else { + Status::Active + }; + + let styling = (self.style)(theme, status); + + if styling.background.is_some() + || styling.border.width > 0.0 + || styling.shadow.color.a > 0.0 + { + renderer.fill_quad( + renderer::Quad { + bounds, + border: styling.border, + shadow: styling.shadow, + }, + styling + .background + .unwrap_or(Background::Color(Color::TRANSPARENT)), + ); + } let viewport = if self.clip { bounds.intersection(viewport).unwrap_or(*viewport) @@ -278,7 +351,13 @@ where _viewport: &Rectangle, _renderer: &Renderer, ) -> mouse::Interaction { - mouse_interaction(layout, cursor, self.on_press.is_some()) + let is_mouse_over = cursor.is_over(layout.bounds()); + + if is_mouse_over && self.on_press.is_some() { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + } } fn overlay<'b>( @@ -301,7 +380,7 @@ impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: Clone + 'a, - Theme: StyleSheet + 'a, + Theme: Style + 'a, Renderer: crate::core::Renderer + 'a, { fn from(button: Button<'a, Message, Theme, Renderer>) -> Self { @@ -309,143 +388,150 @@ where } } -/// The local state of a [`Button`]. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_pressed: bool, +/// The possible status of a [`Button`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Status { + /// The [`Button`] can be pressed. + Active, + /// The [`Button`] can be pressed and it is being hovered. + Hovered, + /// The [`Button`] is being pressed. + Pressed, + /// The [`Button`] cannot be pressed. + Disabled, } -impl State { - /// Creates a new [`State`]. - pub fn new() -> State { - State::default() - } +/// The appearance of a button. +#[derive(Debug, Clone, Copy)] +pub struct Appearance { + /// The amount of offset to apply to the shadow of the button. + pub shadow_offset: Vector, + /// The [`Background`] of the button. + pub background: Option, + /// The text [`Color`] of the button. + pub text_color: Color, + /// The [`Border`] of the buton. + pub border: Border, + /// The [`Shadow`] of the butoon. + pub shadow: Shadow, } -/// Processes the given [`Event`] and updates the [`State`] of a [`Button`] -/// accordingly. -pub fn update<'a, Message: Clone>( - event: Event, - layout: Layout<'_>, - cursor: mouse::Cursor, - shell: &mut Shell<'_, Message>, - on_press: &Option, - state: impl FnOnce() -> &'a mut State, -) -> event::Status { - match event { - Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(touch::Event::FingerPressed { .. }) => { - if on_press.is_some() { - let bounds = layout.bounds(); - - if cursor.is_over(bounds) { - let state = state(); - - state.is_pressed = true; - - return event::Status::Captured; - } - } +impl std::default::Default for Appearance { + fn default() -> Self { + Self { + shadow_offset: Vector::default(), + background: None, + text_color: Color::BLACK, + border: Border::default(), + shadow: Shadow::default(), } - Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) - | Event::Touch(touch::Event::FingerLifted { .. }) => { - if let Some(on_press) = on_press.clone() { - let state = state(); - - if state.is_pressed { - state.is_pressed = false; + } +} - let bounds = layout.bounds(); +/// The default style of a [`Button`] for a given theme. +pub trait Style { + /// The default style. + const DEFAULT: fn(&Self, Status) -> Appearance; +} - if cursor.is_over(bounds) { - shell.publish(on_press); - } +impl Style for Theme { + const DEFAULT: fn(&Self, Status) -> Appearance = primary; +} - return event::Status::Captured; - } - } - } - Event::Touch(touch::Event::FingerLost { .. }) => { - let state = state(); +/// A primary button; denoting a main action. +pub fn primary(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); + let base = styled(palette.primary.strong); + + match status { + Status::Active | Status::Pressed => base, + Status::Hovered => Appearance { + background: Some(Background::Color(palette.primary.base.color)), + ..base + }, + Status::Disabled => disabled(base), + } +} - state.is_pressed = false; - } - _ => {} +/// A secondary button; denoting a complementary action. +pub fn secondary(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); + let base = styled(palette.secondary.base); + + match status { + Status::Active | Status::Pressed => base, + Status::Hovered => Appearance { + background: Some(Background::Color(palette.secondary.strong.color)), + ..base + }, + Status::Disabled => disabled(base), } +} - event::Status::Ignored +/// A positive button; denoting a good outcome. +pub fn positive(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); + let base = styled(palette.success.base); + + match status { + Status::Active | Status::Pressed => base, + Status::Hovered => Appearance { + background: Some(Background::Color(palette.success.strong.color)), + ..base + }, + Status::Disabled => disabled(base), + } } -/// Draws a [`Button`]. -pub fn draw<'a, Theme, Renderer: crate::core::Renderer>( - renderer: &mut Renderer, - bounds: Rectangle, - cursor: mouse::Cursor, - is_enabled: bool, - theme: &Theme, - style: &Theme::Style, - state: impl FnOnce() -> &'a State, -) -> Appearance -where - Theme: StyleSheet, -{ - let is_mouse_over = cursor.is_over(bounds); +/// A destructive button; denoting a dangerous action. +pub fn destructive(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); + let base = styled(palette.danger.base); + + match status { + Status::Active | Status::Pressed => base, + Status::Hovered => Appearance { + background: Some(Background::Color(palette.danger.strong.color)), + ..base + }, + Status::Disabled => disabled(base), + } +} - let styling = if !is_enabled { - theme.disabled(style) - } else if is_mouse_over { - let state = state(); +/// A text button; useful for links. +pub fn text(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); - if state.is_pressed { - theme.pressed(style) - } else { - theme.hovered(style) - } - } else { - theme.active(style) + let base = Appearance { + text_color: palette.background.base.text, + ..Appearance::default() }; - if styling.background.is_some() - || styling.border.width > 0.0 - || styling.shadow.color.a > 0.0 - { - renderer.fill_quad( - renderer::Quad { - bounds, - border: styling.border, - shadow: styling.shadow, - }, - styling - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - ); + match status { + Status::Active | Status::Pressed => base, + Status::Hovered => Appearance { + text_color: palette.background.base.text.transparentize(0.8), + ..base + }, + Status::Disabled => disabled(base), } - - styling } -/// Computes the layout of a [`Button`]. -pub fn layout( - limits: &layout::Limits, - width: Length, - height: Length, - padding: Padding, - layout_content: impl FnOnce(&layout::Limits) -> layout::Node, -) -> layout::Node { - layout::padded(limits, width, height, padding, layout_content) +fn styled(pair: palette::Pair) -> Appearance { + Appearance { + background: Some(Background::Color(pair.color)), + text_color: pair.text, + border: Border::with_radius(2), + ..Appearance::default() + } } -/// Returns the [`mouse::Interaction`] of a [`Button`]. -pub fn mouse_interaction( - layout: Layout<'_>, - cursor: mouse::Cursor, - is_enabled: bool, -) -> mouse::Interaction { - let is_mouse_over = cursor.is_over(layout.bounds()); - - if is_mouse_over && is_enabled { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() +fn disabled(appearance: Appearance) -> Appearance { + Appearance { + background: appearance + .background + .map(|background| background.transparentize(0.5)), + text_color: appearance.text_color.transparentize(0.5), + ..appearance } } -- cgit From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/button.rs | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 798a8206..b5786baa 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -53,7 +53,6 @@ use crate::style::Theme; #[allow(missing_debug_implementations)] pub struct Button<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> where - Theme: Style, Renderer: crate::core::Renderer, { content: Element<'a, Message, Theme, Renderer>, @@ -62,18 +61,20 @@ where height: Length, padding: Padding, clip: bool, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> where - Theme: Style, Renderer: crate::core::Renderer, { /// Creates a new [`Button`] with the given content. pub fn new( content: impl Into>, - ) -> Self { + ) -> Self + where + Style: Default, + { let content = content.into(); let size = content.as_widget().size_hint(); @@ -84,7 +85,7 @@ where height: size.height.fluid(), padding: Padding::new(5.0), clip: false, - style: Theme::DEFAULT, + style: Style::default(), } } @@ -125,7 +126,7 @@ where /// Sets the style variant of this [`Button`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + self.style = Style(style); self } @@ -146,7 +147,6 @@ impl<'a, Message, Theme, Renderer> Widget for Button<'a, Message, Theme, Renderer> where Message: 'a + Clone, - Theme: Style, Renderer: 'a + crate::core::Renderer, { fn tag(&self) -> tree::Tag { @@ -306,7 +306,7 @@ where Status::Active }; - let styling = (self.style)(theme, status); + let styling = (self.style.0)(theme, status); if styling.background.is_some() || styling.border.width > 0.0 @@ -380,7 +380,7 @@ impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: Clone + 'a, - Theme: Style + 'a, + Theme: 'a, Renderer: crate::core::Renderer + 'a, { fn from(button: Button<'a, Message, Theme, Renderer>) -> Self { @@ -428,14 +428,28 @@ impl std::default::Default for Appearance { } } -/// The default style of a [`Button`] for a given theme. -pub trait Style { - /// The default style. - const DEFAULT: fn(&Self, Status) -> Appearance; +/// The style of a [`Button`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme, Status) -> Appearance); + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style(primary) + } } -impl Style for Theme { - const DEFAULT: fn(&Self, Status) -> Appearance = primary; +impl From Appearance> for Style { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) + } } /// A primary button; denoting a main action. -- cgit From 905f2160e6eb7504f52d9bd62c7bfa42c8ec2902 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:14:41 +0100 Subject: Move `Theme` type to `iced_core` --- widget/src/button.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index b5786baa..d0d3cb4a 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -6,21 +6,19 @@ use crate::core::layout; use crate::core::mouse; use crate::core::overlay; use crate::core::renderer; +use crate::core::theme::palette; use crate::core::touch; use crate::core::widget::tree::{self, Tree}; use crate::core::widget::Operation; use crate::core::{ Background, Border, Clipboard, Color, Element, Layout, Length, Padding, - Rectangle, Shadow, Shell, Size, Vector, Widget, + Rectangle, Shadow, Shell, Size, Theme, Vector, Widget, }; -use crate::style::theme::palette; -use crate::style::Theme; /// A generic widget that produces a message when pressed. /// /// ```no_run -/// # type Button<'a, Message> = -/// # iced_widget::Button<'a, Message, iced_widget::style::Theme, iced_widget::renderer::Renderer>; +/// # type Button<'a, Message> = iced_widget::Button<'a, Message>; /// # /// #[derive(Clone)] /// enum Message { @@ -34,8 +32,7 @@ use crate::style::Theme; /// be disabled: /// /// ``` -/// # type Button<'a, Message> = -/// # iced_widget::Button<'a, Message, iced_widget::style::Theme, iced_widget::renderer::Renderer>; +/// # type Button<'a, Message> = iced_widget::Button<'a, Message>; /// # /// #[derive(Clone)] /// enum Message { -- cgit From 6785a452eea5f6b6f69bac123789245dacbc936e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:19:24 +0100 Subject: Fix broken links in documentation --- widget/src/button.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index d0d3cb4a..22dbaec5 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -1,6 +1,4 @@ //! Allow your users to perform actions by pressing a button. -//! -//! A [`Button`] has some local [`State`]. use crate::core::event::{self, Event}; use crate::core::layout; use crate::core::mouse; -- cgit From 44f002f64a9d53040f09affe69bd92675e302e16 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 15:21:42 +0100 Subject: Rename `positive` and `destructive` to `success` and `danger` in `button` --- widget/src/button.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 22dbaec5..5fa62280 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -477,8 +477,8 @@ pub fn secondary(theme: &Theme, status: Status) -> Appearance { } } -/// A positive button; denoting a good outcome. -pub fn positive(theme: &Theme, status: Status) -> Appearance { +/// A success button; denoting a good outcome. +pub fn success(theme: &Theme, status: Status) -> Appearance { let palette = theme.extended_palette(); let base = styled(palette.success.base); @@ -492,8 +492,8 @@ pub fn positive(theme: &Theme, status: Status) -> Appearance { } } -/// A destructive button; denoting a dangerous action. -pub fn destructive(theme: &Theme, status: Status) -> Appearance { +/// A danger button; denoting a destructive action. +pub fn danger(theme: &Theme, status: Status) -> Appearance { let palette = theme.extended_palette(); let base = styled(palette.danger.base); -- cgit From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/button.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 5fa62280..f9859353 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -68,7 +68,7 @@ where content: impl Into>, ) -> Self where - Style: Default, + Theme: DefaultStyle, { let content = content.into(); let size = content.as_widget().size_hint(); @@ -80,7 +80,7 @@ where height: size.height.fluid(), padding: Padding::new(5.0), clip: false, - style: Style::default(), + style: Theme::default_style(), } } @@ -121,7 +121,7 @@ where /// Sets the style variant of this [`Button`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } @@ -301,7 +301,7 @@ where Status::Active }; - let styling = (self.style.0)(theme, status); + let styling = (self.style)(theme, status); if styling.background.is_some() || styling.border.width > 0.0 @@ -424,26 +424,23 @@ impl std::default::Default for Appearance { } /// The style of a [`Button`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(fn(&Theme, Status) -> Appearance); +pub type Style = fn(&Theme, Status) -> Appearance; -impl Clone for Style { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Button`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Button`]. + fn default_style() -> Style; } -impl Copy for Style {} - -impl Default for Style { - fn default() -> Self { - Style(primary) +impl DefaultStyle for Theme { + fn default_style() -> Style { + primary } } -impl From Appearance> for Style { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style { + |appearance, _status| *appearance } } -- cgit From 34ca5386b52ae56d7373ce1af7933cf9aa104b08 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:15:49 +0100 Subject: Implement `with_background` for `button::Appearance` --- widget/src/button.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index f9859353..bfda8fe3 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -411,6 +411,16 @@ pub struct Appearance { pub shadow: Shadow, } +impl Appearance { + /// Creates an [`Appearance`] with the given [`Background`]. + pub fn with_background(background: impl Into) -> Self { + Self { + background: Some(background.into()), + ..Self::default() + } + } +} + impl std::default::Default for Appearance { fn default() -> Self { Self { -- cgit From b8f05eb8dd0394e308385796c229cfc5bc4f3a73 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:16:07 +0100 Subject: Implement `button::DefaultStyle` for `Color` --- widget/src/button.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index bfda8fe3..12716acd 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -454,6 +454,12 @@ impl DefaultStyle for Appearance { } } +impl DefaultStyle for Color { + fn default_style() -> Style { + |color, _status| Appearance::with_background(*color) + } +} + /// A primary button; denoting a main action. pub fn primary(theme: &Theme, status: Status) -> Appearance { let palette = theme.extended_palette(); -- cgit From 7ece5eea509f3595432babfc7729701f2e063b21 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 21:02:17 +0100 Subject: Implement additional helpers for `Border` and `container::Appearance` --- widget/src/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 12716acd..9ce856bb 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -543,7 +543,7 @@ fn styled(pair: palette::Pair) -> Appearance { Appearance { background: Some(Background::Color(pair.color)), text_color: pair.text, - border: Border::with_radius(2), + border: Border::rounded(2), ..Appearance::default() } } -- cgit From 1f46fd871b04ba738e3817d03131bf5ce46f5e46 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 21:13:23 +0100 Subject: Fix consistency of `with_background` for `button::Appearance` --- widget/src/button.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 9ce856bb..4563f3f4 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -397,7 +397,7 @@ pub enum Status { } /// The appearance of a button. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Appearance { /// The amount of offset to apply to the shadow of the button. pub shadow_offset: Vector, @@ -412,11 +412,11 @@ pub struct Appearance { } impl Appearance { - /// Creates an [`Appearance`] with the given [`Background`]. - pub fn with_background(background: impl Into) -> Self { + /// Updates the [`Appearance`] with the given [`Background`]. + pub fn with_background(self, background: impl Into) -> Self { Self { background: Some(background.into()), - ..Self::default() + ..self } } } @@ -456,7 +456,7 @@ impl DefaultStyle for Appearance { impl DefaultStyle for Color { fn default_style() -> Style { - |color, _status| Appearance::with_background(*color) + |color, _status| Appearance::default().with_background(*color) } } -- cgit From 8fe7f9e4354fb46144b6dc459f1205fc87616259 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 23:39:19 +0100 Subject: Remove obsolete `shadow_offset` field from `button::Appearance` --- widget/src/button.rs | 3 --- 1 file changed, 3 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 4563f3f4..579ad434 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -399,8 +399,6 @@ pub enum Status { /// The appearance of a button. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Appearance { - /// The amount of offset to apply to the shadow of the button. - pub shadow_offset: Vector, /// The [`Background`] of the button. pub background: Option, /// The text [`Color`] of the button. @@ -424,7 +422,6 @@ impl Appearance { impl std::default::Default for Appearance { fn default() -> Self { Self { - shadow_offset: Vector::default(), background: None, text_color: Color::BLACK, border: Border::default(), -- cgit From 1b96868e4836b0532ad5c2c0bdb85d1d6fd0698f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Mar 2024 00:24:49 +0100 Subject: Improve default padding of `Button` widget --- widget/src/button.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 579ad434..a23a7156 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -63,6 +63,14 @@ impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> where Renderer: crate::core::Renderer, { + /// The default [`Padding`] of a [`Button`]. + pub const DEFAULT_PADDING: Padding = Padding { + top: 5.0, + bottom: 5.0, + right: 10.0, + left: 10.0, + }; + /// Creates a new [`Button`] with the given content. pub fn new( content: impl Into>, @@ -78,7 +86,7 @@ where on_press: None, width: size.width.fluid(), height: size.height.fluid(), - padding: Padding::new(5.0), + padding: Self::DEFAULT_PADDING, clip: false, style: Theme::default_style(), } -- cgit From 1db823b4c528441627dd075d989fca2fa0a44346 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Mar 2024 00:36:37 +0100 Subject: Make `PickList` padding consistent with `Button` --- widget/src/button.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index a23a7156..243eaf04 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -63,14 +63,6 @@ impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> where Renderer: crate::core::Renderer, { - /// The default [`Padding`] of a [`Button`]. - pub const DEFAULT_PADDING: Padding = Padding { - top: 5.0, - bottom: 5.0, - right: 10.0, - left: 10.0, - }; - /// Creates a new [`Button`] with the given content. pub fn new( content: impl Into>, @@ -86,7 +78,7 @@ where on_press: None, width: size.width.fluid(), height: size.height.fluid(), - padding: Self::DEFAULT_PADDING, + padding: DEFAULT_PADDING, clip: false, style: Theme::default_style(), } @@ -391,6 +383,14 @@ where } } +/// The default [`Padding`] of a [`Button`]. +pub(crate) const DEFAULT_PADDING: Padding = Padding { + top: 5.0, + bottom: 5.0, + right: 10.0, + left: 10.0, +}; + /// The possible status of a [`Button`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Status { -- cgit From 3e99f39a8680bb48d5c15b043c399a3337765ae5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Mar 2024 13:40:10 +0100 Subject: Rename `transparentize` to `scale_alpha` --- widget/src/button.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index 243eaf04..e265aa1f 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -537,7 +537,7 @@ pub fn text(theme: &Theme, status: Status) -> Appearance { match status { Status::Active | Status::Pressed => base, Status::Hovered => Appearance { - text_color: palette.background.base.text.transparentize(0.8), + text_color: palette.background.base.text.scale_alpha(0.8), ..base }, Status::Disabled => disabled(base), @@ -557,8 +557,8 @@ fn disabled(appearance: Appearance) -> Appearance { Appearance { background: appearance .background - .map(|background| background.transparentize(0.5)), - text_color: appearance.text_color.transparentize(0.5), + .map(|background| background.scale_alpha(0.5)), + text_color: appearance.text_color.scale_alpha(0.5), ..appearance } } -- cgit