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

View file

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

View file

@ -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: <Renderer::Theme as StyleSheet>::Variant,
style: <Renderer::Theme as StyleSheet>::Style,
}
impl<'a, Message, Renderer> Button<'a, Message, Renderer>
@ -74,7 +74,6 @@ where
Message: Clone,
Renderer: crate::Renderer,
Renderer::Theme: StyleSheet,
<Renderer::Theme as 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: <Renderer::Theme as StyleSheet>::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: <Renderer::Theme as StyleSheet>::Variant,
style: <Renderer::Theme as StyleSheet>::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<Variant = Variant>,
variation: Variant,
style_sheet: &dyn StyleSheet<
Style = <Renderer::Theme as StyleSheet>::Style,
>,
style: <Renderer::Theme as StyleSheet>::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,
<Renderer::Theme as 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,
<Renderer::Theme as StyleSheet>::Variant: Copy,
{
fn from(
button: Button<'a, Message, Renderer>,

View file

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

View file

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

View file

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

View file

@ -11,7 +11,7 @@ use iced_native::{Clipboard, Layout, Length, Point, Rectangle, Shell, Size};
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
/// values.
@ -49,7 +49,7 @@ where
on_release: Option<Message>,
width: Length,
height: u16,
variant: <Renderer::Theme as StyleSheet>::Variant,
style: <Renderer::Theme as StyleSheet>::Style,
}
impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer>
@ -94,7 +94,7 @@ where
on_release: None,
width: Length::Fill,
height: Self::DEFAULT_HEIGHT,
variant: Default::default(),
style: Default::default(),
}
}
@ -124,9 +124,9 @@ where
/// Sets the style of the [`Slider`].
pub fn style(
mut self,
variant: impl Into<<Renderer::Theme as StyleSheet>::Variant>,
style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
self.variant = variant.into();
self.style = style.into();
self
}
@ -216,7 +216,7 @@ where
self.value,
&self.range,
theme,
self.variant,
self.style,
)
}

View file

@ -14,7 +14,7 @@ pub type Text<Theme = crate::Theme> =
pub mod 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.
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.
//!
//! 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.
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.
#[derive(Debug, Clone, Copy)]
pub struct Style {
pub struct Appearance {
pub shadow_offset: Vector,
pub background: Option<Background>,
pub border_radius: f32,
@ -12,7 +12,7 @@ pub struct Style {
pub text_color: Color,
}
impl std::default::Default for Style {
impl std::default::Default for Appearance {
fn default() -> Self {
Self {
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.
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 {
let active = self.active(variant);
fn hovered(&self, style: Self::Style) -> Appearance {
let active = self.active(style);
Style {
Appearance {
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
..active
}
}
fn pressed(&self, variant: Self::Variant) -> Style {
Style {
fn pressed(&self, style: Self::Style) -> Appearance {
Appearance {
shadow_offset: Vector::default(),
..self.active(variant)
..self.active(style)
}
}
fn disabled(&self, variant: Self::Variant) -> Style {
let active = self.active(variant);
fn disabled(&self, style: Self::Style) -> Appearance {
let active = self.active(style);
Style {
Appearance {
shadow_offset: Vector::default(),
background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color {

View file

@ -3,7 +3,7 @@ use iced_core::Color;
/// The appearance of a slider.
#[derive(Debug, Clone, Copy)]
pub struct Style {
pub struct Appearance {
pub rail_colors: (Color, Color),
pub handle: Handle,
}
@ -26,14 +26,14 @@ pub enum HandleShape {
/// A set of rules that dictate the style of a slider.
pub trait StyleSheet {
type Variant: Default + Copy;
type Style: Default + Copy;
/// 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.
fn hovered(&self, variant: Self::Variant) -> Style;
fn hovered(&self, style: Self::Style) -> Appearance;
/// 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 {
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 style = button::Style {
let appearance = button::Appearance {
border_radius: 2.0,
..button::Style::default()
..button::Appearance::default()
};
match variant {
Button::Primary => button::Style {
match style {
Button::Primary => button::Appearance {
background: Some(palette.primary.strong.into()),
text_color: palette.primary.text,
..style
..appearance
},
Button::Secondary => button::Style {
Button::Secondary => button::Appearance {
background: Some(palette.background.weak.into()),
text_color: palette.background.text,
..style
..appearance
},
Button::Positive => button::Style {
Button::Positive => button::Appearance {
background: Some(palette.success.base.into()),
text_color: palette.success.text,
..style
..appearance
},
Button::Destructive => button::Style {
Button::Destructive => button::Appearance {
background: Some(palette.danger.base.into()),
text_color: palette.danger.text,
..style
..appearance
},
Button::Text => button::Style {
Button::Text => button::Appearance {
text_color: palette.background.text,
..style
..appearance
},
}
}
fn hovered(&self, variant: Self::Variant) -> button::Style {
let active = self.active(variant);
fn hovered(&self, style: Self::Style) -> button::Appearance {
let active = self.active(style);
let palette = self.extended_palette();
let background = match variant {
let background = match style {
Button::Primary => Some(palette.primary.base),
Button::Secondary => Some(palette.background.strong),
Button::Positive => Some(palette.success.strong),
@ -116,7 +116,7 @@ impl button::StyleSheet for Theme {
Button::Text => None,
};
button::Style {
button::Appearance {
background: background.map(Background::from),
..active
}
@ -124,9 +124,9 @@ impl button::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 handle = slider::Handle {
@ -139,7 +139,7 @@ impl slider::StyleSheet for Theme {
border_width: 1.0,
};
slider::Style {
slider::Appearance {
rail_colors: (palette.primary.base, palette.background.base),
handle: slider::Handle {
color: palette.background.base,
@ -149,11 +149,11 @@ impl slider::StyleSheet for Theme {
}
}
fn hovered(&self, variant: Self::Variant) -> slider::Style {
let active = self.active(variant);
fn hovered(&self, style: Self::Style) -> slider::Appearance {
let active = self.active(style);
let palette = self.extended_palette();
slider::Style {
slider::Appearance {
handle: slider::Handle {
color: palette.primary.weak,
..active.handle
@ -162,11 +162,11 @@ impl slider::StyleSheet for Theme {
}
}
fn dragging(&self, variant: Self::Variant) -> slider::Style {
let active = self.active(variant);
fn dragging(&self, style: Self::Style) -> slider::Appearance {
let active = self.active(style);
let palette = self.extended_palette();
slider::Style {
slider::Appearance {
handle: slider::Handle {
color: palette.primary.base,
..active.handle