From 664251f3f5c7b76f69a97683af1468094bba887f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 May 2022 01:47:55 +0200 Subject: Draft first-class `Theme` support RFC: https://github.com/iced-rs/rfcs/pull/6 --- native/src/widget/button.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index b03d9f27..09c59cbe 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -55,20 +55,26 @@ pub use iced_style::button::{Style, StyleSheet}; /// } /// ``` #[allow(missing_debug_implementations)] -pub struct Button<'a, Message, Renderer> { +pub struct Button<'a, Message, Renderer> +where + Renderer: crate::Renderer, + Renderer::Theme: StyleSheet, +{ state: &'a mut State, content: Element<'a, Message, Renderer>, on_press: Option, width: Length, height: Length, padding: Padding, - style_sheet: Box, + variant: ::Variant, } impl<'a, Message, Renderer> Button<'a, Message, Renderer> where Message: Clone, Renderer: crate::Renderer, + Renderer::Theme: StyleSheet, + ::Variant: Default, { /// Creates a new [`Button`] with some local [`State`] and the given /// content. @@ -83,7 +89,7 @@ where width: Length::Shrink, height: Length::Shrink, padding: Padding::new(5), - style_sheet: Default::default(), + variant: ::Variant::default(), } } @@ -112,12 +118,12 @@ where self } - /// Sets the style of the [`Button`]. + /// Sets the style variant of this [`Button`]. pub fn style( mut self, - style_sheet: impl Into>, + variant: ::Variant, ) -> Self { - self.style_sheet = style_sheet.into(); + self.variant = variant; self } } @@ -190,28 +196,29 @@ pub fn update<'a, Message: Clone>( } /// Draws a [`Button`]. -pub fn draw<'a, Renderer: crate::Renderer>( +pub fn draw<'a, Renderer: crate::Renderer, Variant>( renderer: &mut Renderer, bounds: Rectangle, cursor_position: Point, is_enabled: bool, - style_sheet: &dyn StyleSheet, + style_sheet: &dyn StyleSheet, + variation: Variant, state: impl FnOnce() -> &'a State, ) -> Style { let is_mouse_over = bounds.contains(cursor_position); let styling = if !is_enabled { - style_sheet.disabled() + style_sheet.disabled(variation) } else if is_mouse_over { let state = state(); if state.is_pressed { - style_sheet.pressed() + style_sheet.pressed(variation) } else { - style_sheet.hovered() + style_sheet.hovered(variation) } } else { - style_sheet.active() + style_sheet.active(variation) }; if styling.background.is_some() || styling.border_width > 0.0 { @@ -287,6 +294,8 @@ impl<'a, Message, Renderer> Widget where Message: Clone, Renderer: crate::Renderer, + Renderer::Theme: StyleSheet, + ::Variant: Copy, { fn width(&self) -> Length { self.width @@ -354,6 +363,7 @@ where fn draw( &self, renderer: &mut Renderer, + theme: &Renderer::Theme, _style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, @@ -367,12 +377,14 @@ where bounds, cursor_position, self.on_press.is_some(), - self.style_sheet.as_ref(), + theme, + self.variant, || &self.state, ); self.content.draw( renderer, + theme, &renderer::Style { text_color: styling.text_color, }, @@ -397,6 +409,8 @@ impl<'a, Message, Renderer> From> where Message: 'a + Clone, Renderer: 'a + crate::Renderer, + Renderer::Theme: StyleSheet, + ::Variant: Copy, { fn from( button: Button<'a, Message, Renderer>, -- cgit From cf0230072c01ea9523f4d98a3656f5c975b3f347 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 26 May 2022 23:07:34 +0200 Subject: Rename `Variant` to `Style` and `Style` to `Appearance` --- native/src/widget/button.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 09c59cbe..d4e88424 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -12,7 +12,7 @@ use crate::{ Rectangle, Shell, Vector, Widget, }; -pub use iced_style::button::{Style, StyleSheet}; +pub use iced_style::button::{Appearance, StyleSheet}; /// A generic widget that produces a message when pressed. /// @@ -66,7 +66,7 @@ where width: Length, height: Length, padding: Padding, - variant: ::Variant, + style: ::Style, } impl<'a, Message, Renderer> Button<'a, Message, Renderer> @@ -74,7 +74,6 @@ where Message: Clone, Renderer: crate::Renderer, Renderer::Theme: StyleSheet, - ::Variant: Default, { /// Creates a new [`Button`] with some local [`State`] and the given /// content. @@ -89,7 +88,7 @@ where width: Length::Shrink, height: Length::Shrink, padding: Padding::new(5), - variant: ::Variant::default(), + style: Default::default(), } } @@ -118,12 +117,12 @@ where self } - /// Sets the style variant of this [`Button`]. + /// Sets the style of this [`Button`]. pub fn style( mut self, - variant: ::Variant, + style: ::Style, ) -> Self { - self.variant = variant; + self.style = style; self } } @@ -196,29 +195,34 @@ pub fn update<'a, Message: Clone>( } /// Draws a [`Button`]. -pub fn draw<'a, Renderer: crate::Renderer, Variant>( +pub fn draw<'a, Renderer: crate::Renderer>( renderer: &mut Renderer, bounds: Rectangle, cursor_position: Point, is_enabled: bool, - style_sheet: &dyn StyleSheet, - variation: Variant, + style_sheet: &dyn StyleSheet< + Style = ::Style, + >, + style: ::Style, state: impl FnOnce() -> &'a State, -) -> Style { +) -> Appearance +where + Renderer::Theme: StyleSheet, +{ let is_mouse_over = bounds.contains(cursor_position); let styling = if !is_enabled { - style_sheet.disabled(variation) + style_sheet.disabled(style) } else if is_mouse_over { let state = state(); if state.is_pressed { - style_sheet.pressed(variation) + style_sheet.pressed(style) } else { - style_sheet.hovered(variation) + style_sheet.hovered(style) } } else { - style_sheet.active(variation) + style_sheet.active(style) }; if styling.background.is_some() || styling.border_width > 0.0 { @@ -295,7 +299,6 @@ where Message: Clone, Renderer: crate::Renderer, Renderer::Theme: StyleSheet, - ::Variant: Copy, { fn width(&self) -> Length { self.width @@ -378,7 +381,7 @@ where cursor_position, self.on_press.is_some(), theme, - self.variant, + self.style, || &self.state, ); @@ -410,7 +413,6 @@ where Message: 'a + Clone, Renderer: 'a + crate::Renderer, Renderer::Theme: StyleSheet, - ::Variant: Copy, { fn from( button: Button<'a, Message, Renderer>, -- cgit