Rename Variant to Style and Style to Appearance

This commit is contained in:
Héctor Ramón Jiménez 2022-05-26 23:07:34 +02:00
parent 7f3b7075db
commit cf0230072c
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
12 changed files with 91 additions and 99 deletions

View file

@ -96,7 +96,6 @@ mod numeric_input {
where where
Renderer: 'a + text::Renderer, Renderer: 'a + text::Renderer,
Renderer::Theme: button::StyleSheet, Renderer::Theme: button::StyleSheet,
<Renderer::Theme as button::StyleSheet>::Variant: Default + Copy,
{ {
type Event = Event; type Event = Event;
@ -175,7 +174,6 @@ mod numeric_input {
Message: 'a, Message: 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
Renderer::Theme: button::StyleSheet, Renderer::Theme: button::StyleSheet,
<Renderer::Theme as button::StyleSheet>::Variant: Default + Copy,
{ {
fn from(numeric_input: NumericInput<'a, Message>) -> Self { fn from(numeric_input: NumericInput<'a, Message>) -> Self {
component::view(numeric_input) component::view(numeric_input)

View file

@ -90,8 +90,6 @@ mod numeric_input {
where where
Renderer: text::Renderer + 'static, Renderer: text::Renderer + 'static,
Renderer::Theme: widget::button::StyleSheet, Renderer::Theme: widget::button::StyleSheet,
<Renderer::Theme as widget::button::StyleSheet>::Variant:
Default + Copy,
{ {
type State = (); type State = ();
type Event = Event; type Event = Event;
@ -163,8 +161,6 @@ mod numeric_input {
Message: 'a, Message: 'a,
Renderer: 'static + text::Renderer, Renderer: 'static + text::Renderer,
Renderer::Theme: widget::button::StyleSheet, Renderer::Theme: widget::button::StyleSheet,
<Renderer::Theme as widget::button::StyleSheet>::Variant:
Default + Copy,
{ {
fn from(numeric_input: NumericInput<Message>) -> Self { fn from(numeric_input: NumericInput<Message>) -> Self {
pure::component(numeric_input) pure::component(numeric_input)

View file

@ -12,7 +12,7 @@ use crate::{
Rectangle, Shell, Vector, Widget, 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. /// A generic widget that produces a message when pressed.
/// ///
@ -66,7 +66,7 @@ where
width: Length, width: Length,
height: Length, height: Length,
padding: Padding, padding: Padding,
variant: <Renderer::Theme as StyleSheet>::Variant, style: <Renderer::Theme as StyleSheet>::Style,
} }
impl<'a, Message, Renderer> Button<'a, Message, Renderer> impl<'a, Message, Renderer> Button<'a, Message, Renderer>
@ -74,7 +74,6 @@ where
Message: Clone, Message: Clone,
Renderer: crate::Renderer, Renderer: crate::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
<Renderer::Theme as StyleSheet>::Variant: Default,
{ {
/// Creates a new [`Button`] with some local [`State`] and the given /// Creates a new [`Button`] with some local [`State`] and the given
/// content. /// content.
@ -89,7 +88,7 @@ where
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
padding: Padding::new(5), padding: Padding::new(5),
variant: <Renderer::Theme as StyleSheet>::Variant::default(), style: Default::default(),
} }
} }
@ -118,12 +117,12 @@ where
self self
} }
/// Sets the style variant of this [`Button`]. /// Sets the style of this [`Button`].
pub fn style( pub fn style(
mut self, mut self,
variant: <Renderer::Theme as StyleSheet>::Variant, style: <Renderer::Theme as StyleSheet>::Style,
) -> Self { ) -> Self {
self.variant = variant; self.style = style;
self self
} }
} }
@ -196,29 +195,34 @@ pub fn update<'a, Message: Clone>(
} }
/// Draws a [`Button`]. /// Draws a [`Button`].
pub fn draw<'a, Renderer: crate::Renderer, Variant>( pub fn draw<'a, Renderer: crate::Renderer>(
renderer: &mut Renderer, renderer: &mut Renderer,
bounds: Rectangle, bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
is_enabled: bool, is_enabled: bool,
style_sheet: &dyn StyleSheet<Variant = Variant>, style_sheet: &dyn StyleSheet<
variation: Variant, Style = <Renderer::Theme as StyleSheet>::Style,
>,
style: <Renderer::Theme as StyleSheet>::Style,
state: impl FnOnce() -> &'a State, state: impl FnOnce() -> &'a State,
) -> Style { ) -> Appearance
where
Renderer::Theme: StyleSheet,
{
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = bounds.contains(cursor_position);
let styling = if !is_enabled { let styling = if !is_enabled {
style_sheet.disabled(variation) style_sheet.disabled(style)
} else if is_mouse_over { } else if is_mouse_over {
let state = state(); let state = state();
if state.is_pressed { if state.is_pressed {
style_sheet.pressed(variation) style_sheet.pressed(style)
} else { } else {
style_sheet.hovered(variation) style_sheet.hovered(style)
} }
} else { } else {
style_sheet.active(variation) style_sheet.active(style)
}; };
if styling.background.is_some() || styling.border_width > 0.0 { if styling.background.is_some() || styling.border_width > 0.0 {
@ -295,7 +299,6 @@ where
Message: Clone, Message: Clone,
Renderer: crate::Renderer, Renderer: crate::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
<Renderer::Theme as StyleSheet>::Variant: Copy,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
self.width self.width
@ -378,7 +381,7 @@ where
cursor_position, cursor_position,
self.on_press.is_some(), self.on_press.is_some(),
theme, theme,
self.variant, self.style,
|| &self.state, || &self.state,
); );
@ -410,7 +413,6 @@ where
Message: 'a + Clone, Message: 'a + Clone,
Renderer: 'a + crate::Renderer, Renderer: 'a + crate::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
<Renderer::Theme as StyleSheet>::Variant: Copy,
{ {
fn from( fn from(
button: Button<'a, Message, Renderer>, button: Button<'a, Message, Renderer>,

View file

@ -13,7 +13,7 @@ use crate::{
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet}; pub use iced_style::slider::{Appearance, Handle, HandleShape, StyleSheet};
/// An horizontal bar and a handle that selects a single value from a range of /// An horizontal bar and a handle that selects a single value from a range of
/// values. /// values.
@ -53,7 +53,7 @@ where
on_release: Option<Message>, on_release: Option<Message>,
width: Length, width: Length,
height: u16, height: u16,
variant: <Renderer::Theme as StyleSheet>::Variant, style: <Renderer::Theme as StyleSheet>::Style,
} }
impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer> impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer>
@ -105,7 +105,7 @@ where
on_release: None, on_release: None,
width: Length::Fill, width: Length::Fill,
height: Self::DEFAULT_HEIGHT, height: Self::DEFAULT_HEIGHT,
variant: Default::default(), style: Default::default(),
} }
} }
@ -135,9 +135,9 @@ where
/// Sets the style of the [`Slider`]. /// Sets the style of the [`Slider`].
pub fn style( pub fn style(
mut self, mut self,
variant: impl Into<<Renderer::Theme as StyleSheet>::Variant>, style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self { ) -> Self {
self.variant = variant.into(); self.style = style.into();
self self
} }
@ -243,8 +243,8 @@ pub fn draw<T, R>(
state: &State, state: &State,
value: T, value: T,
range: &RangeInclusive<T>, range: &RangeInclusive<T>,
style_sheet: &dyn StyleSheet<Variant = <R::Theme as StyleSheet>::Variant>, style_sheet: &dyn StyleSheet<Style = <R::Theme as StyleSheet>::Style>,
variant: <R::Theme as StyleSheet>::Variant, style: <R::Theme as StyleSheet>::Style,
) where ) where
T: Into<f64> + Copy, T: Into<f64> + Copy,
R: crate::Renderer, R: crate::Renderer,
@ -254,11 +254,11 @@ pub fn draw<T, R>(
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = bounds.contains(cursor_position);
let style = if state.is_dragging { let style = if state.is_dragging {
style_sheet.dragging(variant) style_sheet.dragging(style)
} else if is_mouse_over { } else if is_mouse_over {
style_sheet.hovered(variant) style_sheet.hovered(style)
} else { } else {
style_sheet.active(variant) style_sheet.active(style)
}; };
let rail_y = bounds.y + (bounds.height / 2.0).round(); let rail_y = bounds.y + (bounds.height / 2.0).round();
@ -434,7 +434,7 @@ where
self.value, self.value,
&self.range, &self.range,
theme, theme,
self.variant, self.style,
) )
} }

View file

@ -54,7 +54,6 @@ pub fn button<'a, Message, Renderer>(
where where
Renderer: iced_native::Renderer, Renderer: iced_native::Renderer,
Renderer::Theme: widget::button::StyleSheet, Renderer::Theme: widget::button::StyleSheet,
<Renderer::Theme as widget::button::StyleSheet>::Variant: Default,
{ {
widget::Button::new(content) widget::Button::new(content)
} }

View file

@ -12,7 +12,7 @@ use iced_native::{
Clipboard, Layout, Length, Padding, Point, Rectangle, Shell, Clipboard, Layout, Length, Padding, Point, Rectangle, Shell,
}; };
pub use iced_style::button::{Style, StyleSheet}; pub use iced_style::button::{Appearance, StyleSheet};
use button::State; use button::State;
@ -60,14 +60,13 @@ where
width: Length, width: Length,
height: Length, height: Length,
padding: Padding, padding: Padding,
variant: <Renderer::Theme as StyleSheet>::Variant, style: <Renderer::Theme as StyleSheet>::Style,
} }
impl<'a, Message, Renderer> Button<'a, Message, Renderer> impl<'a, Message, Renderer> Button<'a, Message, Renderer>
where where
Renderer: iced_native::Renderer, Renderer: iced_native::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
<Renderer::Theme as StyleSheet>::Variant: Default,
{ {
/// Creates a new [`Button`] with the given content. /// Creates a new [`Button`] with the given content.
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self { pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
@ -77,7 +76,7 @@ where
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
padding: Padding::new(5), padding: Padding::new(5),
variant: <Renderer::Theme as StyleSheet>::Variant::default(), style: <Renderer::Theme as StyleSheet>::Style::default(),
} }
} }
@ -110,9 +109,9 @@ where
/// Sets the style variant of this [`Button`]. /// Sets the style variant of this [`Button`].
pub fn style( pub fn style(
mut self, mut self,
variant: <Renderer::Theme as StyleSheet>::Variant, style: <Renderer::Theme as StyleSheet>::Style,
) -> Self { ) -> Self {
self.variant = variant; self.style = style;
self self
} }
} }
@ -123,7 +122,6 @@ where
Message: 'a + Clone, Message: 'a + Clone,
Renderer: 'a + iced_native::Renderer, Renderer: 'a + iced_native::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
<Renderer::Theme as StyleSheet>::Variant: Copy,
{ {
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>() tree::Tag::of::<State>()
@ -217,7 +215,7 @@ where
cursor_position, cursor_position,
self.on_press.is_some(), self.on_press.is_some(),
theme, theme,
self.variant, self.style,
|| tree.state.downcast_ref::<State>(), || tree.state.downcast_ref::<State>(),
); );
@ -269,7 +267,6 @@ where
Message: Clone + 'a, Message: Clone + 'a,
Renderer: iced_native::Renderer + 'a, Renderer: iced_native::Renderer + 'a,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
<Renderer::Theme as StyleSheet>::Variant: Copy,
{ {
fn into(self) -> Element<'a, Message, Renderer> { fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self) Element::new(self)

View file

@ -11,7 +11,7 @@ use iced_native::{Clipboard, Layout, Length, Point, Rectangle, Shell, Size};
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet}; pub use iced_style::slider::{Appearance, Handle, HandleShape, StyleSheet};
/// An horizontal bar and a handle that selects a single value from a range of /// An horizontal bar and a handle that selects a single value from a range of
/// values. /// values.
@ -49,7 +49,7 @@ where
on_release: Option<Message>, on_release: Option<Message>,
width: Length, width: Length,
height: u16, height: u16,
variant: <Renderer::Theme as StyleSheet>::Variant, style: <Renderer::Theme as StyleSheet>::Style,
} }
impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer> impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer>
@ -94,7 +94,7 @@ where
on_release: None, on_release: None,
width: Length::Fill, width: Length::Fill,
height: Self::DEFAULT_HEIGHT, height: Self::DEFAULT_HEIGHT,
variant: Default::default(), style: Default::default(),
} }
} }
@ -124,9 +124,9 @@ where
/// Sets the style of the [`Slider`]. /// Sets the style of the [`Slider`].
pub fn style( pub fn style(
mut self, mut self,
variant: impl Into<<Renderer::Theme as StyleSheet>::Variant>, style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self { ) -> Self {
self.variant = variant.into(); self.style = style.into();
self self
} }
@ -216,7 +216,7 @@ where
self.value, self.value,
&self.range, &self.range,
theme, theme,
self.variant, self.style,
) )
} }

View file

@ -14,7 +14,7 @@ pub type Text<Theme = crate::Theme> =
pub mod button { pub mod button {
//! Allow your users to perform actions by pressing a button. //! Allow your users to perform actions by pressing a button.
pub use iced_pure::widget::button::{Style, StyleSheet}; pub use iced_pure::widget::button::{Appearance, StyleSheet};
/// A widget that produces a message when clicked. /// A widget that produces a message when clicked.
pub type Button<'a, Message, Theme = crate::Theme> = pub type Button<'a, Message, Theme = crate::Theme> =

View file

@ -30,7 +30,7 @@ pub mod button {
//! Allow your users to perform actions by pressing a button. //! Allow your users to perform actions by pressing a button.
//! //!
//! A [`Button`] has some local [`State`]. //! A [`Button`] has some local [`State`].
pub use iced_native::widget::button::{State, Style, StyleSheet}; pub use iced_native::widget::button::{Appearance, State, StyleSheet};
/// A widget that produces a message when clicked. /// A widget that produces a message when clicked.
pub type Button<'a, Message, Theme = crate::Theme> = pub type Button<'a, Message, Theme = crate::Theme> =

View file

@ -3,7 +3,7 @@ use iced_core::{Background, Color, Vector};
/// The appearance of a button. /// The appearance of a button.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Style { pub struct Appearance {
pub shadow_offset: Vector, pub shadow_offset: Vector,
pub background: Option<Background>, pub background: Option<Background>,
pub border_radius: f32, pub border_radius: f32,
@ -12,7 +12,7 @@ pub struct Style {
pub text_color: Color, pub text_color: Color,
} }
impl std::default::Default for Style { impl std::default::Default for Appearance {
fn default() -> Self { fn default() -> Self {
Self { Self {
shadow_offset: Vector::default(), shadow_offset: Vector::default(),
@ -27,30 +27,30 @@ impl std::default::Default for Style {
/// A set of rules that dictate the style of a button. /// A set of rules that dictate the style of a button.
pub trait StyleSheet { pub trait StyleSheet {
type Variant; type Style: Default + Copy;
fn active(&self, variant: Self::Variant) -> Style; fn active(&self, style: Self::Style) -> Appearance;
fn hovered(&self, variant: Self::Variant) -> Style { fn hovered(&self, style: Self::Style) -> Appearance {
let active = self.active(variant); let active = self.active(style);
Style { Appearance {
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0), shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
..active ..active
} }
} }
fn pressed(&self, variant: Self::Variant) -> Style { fn pressed(&self, style: Self::Style) -> Appearance {
Style { Appearance {
shadow_offset: Vector::default(), shadow_offset: Vector::default(),
..self.active(variant) ..self.active(style)
} }
} }
fn disabled(&self, variant: Self::Variant) -> Style { fn disabled(&self, style: Self::Style) -> Appearance {
let active = self.active(variant); let active = self.active(style);
Style { Appearance {
shadow_offset: Vector::default(), shadow_offset: Vector::default(),
background: active.background.map(|background| match background { background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color { Background::Color(color) => Background::Color(Color {

View file

@ -3,7 +3,7 @@ use iced_core::Color;
/// The appearance of a slider. /// The appearance of a slider.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Style { pub struct Appearance {
pub rail_colors: (Color, Color), pub rail_colors: (Color, Color),
pub handle: Handle, pub handle: Handle,
} }
@ -26,14 +26,14 @@ pub enum HandleShape {
/// A set of rules that dictate the style of a slider. /// A set of rules that dictate the style of a slider.
pub trait StyleSheet { pub trait StyleSheet {
type Variant: Default + Copy; type Style: Default + Copy;
/// Produces the style of an active slider. /// Produces the style of an active slider.
fn active(&self, variant: Self::Variant) -> Style; fn active(&self, style: Self::Style) -> Appearance;
/// Produces the style of an hovered slider. /// Produces the style of an hovered slider.
fn hovered(&self, variant: Self::Variant) -> Style; fn hovered(&self, style: Self::Style) -> Appearance;
/// Produces the style of a slider that is being dragged. /// Produces the style of a slider that is being dragged.
fn dragging(&self, variant: Self::Variant) -> Style; fn dragging(&self, style: Self::Style) -> Appearance;
} }

View file

@ -66,49 +66,49 @@ impl Default for Button {
} }
impl button::StyleSheet for Theme { impl button::StyleSheet for Theme {
type Variant = Button; type Style = Button;
fn active(&self, variant: Self::Variant) -> button::Style { fn active(&self, style: Self::Style) -> button::Appearance {
let palette = self.extended_palette(); let palette = self.extended_palette();
let style = button::Style { let appearance = button::Appearance {
border_radius: 2.0, border_radius: 2.0,
..button::Style::default() ..button::Appearance::default()
}; };
match variant { match style {
Button::Primary => button::Style { Button::Primary => button::Appearance {
background: Some(palette.primary.strong.into()), background: Some(palette.primary.strong.into()),
text_color: palette.primary.text, text_color: palette.primary.text,
..style ..appearance
}, },
Button::Secondary => button::Style { Button::Secondary => button::Appearance {
background: Some(palette.background.weak.into()), background: Some(palette.background.weak.into()),
text_color: palette.background.text, text_color: palette.background.text,
..style ..appearance
}, },
Button::Positive => button::Style { Button::Positive => button::Appearance {
background: Some(palette.success.base.into()), background: Some(palette.success.base.into()),
text_color: palette.success.text, text_color: palette.success.text,
..style ..appearance
}, },
Button::Destructive => button::Style { Button::Destructive => button::Appearance {
background: Some(palette.danger.base.into()), background: Some(palette.danger.base.into()),
text_color: palette.danger.text, text_color: palette.danger.text,
..style ..appearance
}, },
Button::Text => button::Style { Button::Text => button::Appearance {
text_color: palette.background.text, text_color: palette.background.text,
..style ..appearance
}, },
} }
} }
fn hovered(&self, variant: Self::Variant) -> button::Style { fn hovered(&self, style: Self::Style) -> button::Appearance {
let active = self.active(variant); let active = self.active(style);
let palette = self.extended_palette(); let palette = self.extended_palette();
let background = match variant { let background = match style {
Button::Primary => Some(palette.primary.base), Button::Primary => Some(palette.primary.base),
Button::Secondary => Some(palette.background.strong), Button::Secondary => Some(palette.background.strong),
Button::Positive => Some(palette.success.strong), Button::Positive => Some(palette.success.strong),
@ -116,7 +116,7 @@ impl button::StyleSheet for Theme {
Button::Text => None, Button::Text => None,
}; };
button::Style { button::Appearance {
background: background.map(Background::from), background: background.map(Background::from),
..active ..active
} }
@ -124,9 +124,9 @@ impl button::StyleSheet for Theme {
} }
impl slider::StyleSheet for Theme { impl slider::StyleSheet for Theme {
type Variant = (); type Style = ();
fn active(&self, _variant: Self::Variant) -> slider::Style { fn active(&self, _style: Self::Style) -> slider::Appearance {
let palette = self.extended_palette(); let palette = self.extended_palette();
let handle = slider::Handle { let handle = slider::Handle {
@ -139,7 +139,7 @@ impl slider::StyleSheet for Theme {
border_width: 1.0, border_width: 1.0,
}; };
slider::Style { slider::Appearance {
rail_colors: (palette.primary.base, palette.background.base), rail_colors: (palette.primary.base, palette.background.base),
handle: slider::Handle { handle: slider::Handle {
color: palette.background.base, color: palette.background.base,
@ -149,11 +149,11 @@ impl slider::StyleSheet for Theme {
} }
} }
fn hovered(&self, variant: Self::Variant) -> slider::Style { fn hovered(&self, style: Self::Style) -> slider::Appearance {
let active = self.active(variant); let active = self.active(style);
let palette = self.extended_palette(); let palette = self.extended_palette();
slider::Style { slider::Appearance {
handle: slider::Handle { handle: slider::Handle {
color: palette.primary.weak, color: palette.primary.weak,
..active.handle ..active.handle
@ -162,11 +162,11 @@ impl slider::StyleSheet for Theme {
} }
} }
fn dragging(&self, variant: Self::Variant) -> slider::Style { fn dragging(&self, style: Self::Style) -> slider::Appearance {
let active = self.active(variant); let active = self.active(style);
let palette = self.extended_palette(); let palette = self.extended_palette();
slider::Style { slider::Appearance {
handle: slider::Handle { handle: slider::Handle {
color: palette.primary.base, color: palette.primary.base,
..active.handle ..active.handle