Simplify theming for Button widget

This commit is contained in:
Héctor Ramón Jiménez 2024-03-04 20:42:37 +01:00
parent db92e1c942
commit f4a4845ddb
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
16 changed files with 306 additions and 412 deletions

View file

@ -1,79 +1 @@
//! Change the apperance of a button.
use iced_core::{Background, Border, Color, Shadow, Vector};
/// 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<Background>,
/// 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,
}
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(),
}
}
}
/// A set of rules that dictate the style of a button.
pub trait StyleSheet {
/// The supported style of the [`StyleSheet`].
type Style: Default;
/// Produces the active [`Appearance`] of a button.
fn active(&self, style: &Self::Style) -> Appearance;
/// Produces the hovered [`Appearance`] of a button.
fn hovered(&self, style: &Self::Style) -> Appearance {
let active = self.active(style);
Appearance {
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
..active
}
}
/// Produces the pressed [`Appearance`] of a button.
fn pressed(&self, style: &Self::Style) -> Appearance {
Appearance {
shadow_offset: Vector::default(),
..self.active(style)
}
}
/// Produces the disabled [`Appearance`] of a button.
fn disabled(&self, style: &Self::Style) -> Appearance {
let active = self.active(style);
Appearance {
shadow_offset: Vector::default(),
background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
Background::Gradient(gradient) => {
Background::Gradient(gradient.mul_alpha(0.5))
}
}),
text_color: Color {
a: active.text_color.a * 0.5,
..active.text_color
},
..active
}
}
}

View file

@ -30,15 +30,7 @@ pub trait StyleSheet {
let active = self.active(style, is_checked);
Appearance {
background: match active.background {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
Background::Gradient(gradient) => {
Background::Gradient(gradient.mul_alpha(0.5))
}
},
background: active.background.transparentize(0.5),
..active
}
}

View file

@ -4,7 +4,6 @@ pub mod palette;
pub use palette::Palette;
use crate::application;
use crate::button;
use crate::checkbox;
use crate::container;
use crate::core::widget::text;
@ -22,7 +21,7 @@ use crate::text_editor;
use crate::text_input;
use crate::toggler;
use crate::core::{Background, Border, Color, Shadow, Vector};
use crate::core::{Background, Border, Color, Shadow};
use std::fmt;
use std::rc::Rc;
@ -285,126 +284,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
}
}
/// The style of a button.
#[derive(Default)]
pub enum Button {
/// The primary style.
#[default]
Primary,
/// The secondary style.
Secondary,
/// The positive style.
Positive,
/// The destructive style.
Destructive,
/// The text style.
///
/// Useful for links!
Text,
/// A custom style.
Custom(Box<dyn button::StyleSheet<Style = Theme>>),
}
impl Button {
/// Creates a custom [`Button`] style variant.
pub fn custom(
style_sheet: impl button::StyleSheet<Style = Theme> + 'static,
) -> Self {
Self::Custom(Box::new(style_sheet))
}
}
impl button::StyleSheet for Theme {
type Style = Button;
fn active(&self, style: &Self::Style) -> button::Appearance {
let palette = self.extended_palette();
let appearance = button::Appearance {
border: Border::with_radius(2),
..button::Appearance::default()
};
let from_pair = |pair: palette::Pair| button::Appearance {
background: Some(pair.color.into()),
text_color: pair.text,
..appearance
};
match style {
Button::Primary => from_pair(palette.primary.strong),
Button::Secondary => from_pair(palette.secondary.base),
Button::Positive => from_pair(palette.success.base),
Button::Destructive => from_pair(palette.danger.base),
Button::Text => button::Appearance {
text_color: palette.background.base.text,
..appearance
},
Button::Custom(custom) => custom.active(self),
}
}
fn hovered(&self, style: &Self::Style) -> button::Appearance {
let palette = self.extended_palette();
if let Button::Custom(custom) = style {
return custom.hovered(self);
}
let active = self.active(style);
let background = match style {
Button::Primary => Some(palette.primary.base.color),
Button::Secondary => Some(palette.background.strong.color),
Button::Positive => Some(palette.success.strong.color),
Button::Destructive => Some(palette.danger.strong.color),
Button::Text | Button::Custom(_) => None,
};
button::Appearance {
background: background.map(Background::from),
..active
}
}
fn pressed(&self, style: &Self::Style) -> button::Appearance {
if let Button::Custom(custom) = style {
return custom.pressed(self);
}
button::Appearance {
shadow_offset: Vector::default(),
..self.active(style)
}
}
fn disabled(&self, style: &Self::Style) -> button::Appearance {
if let Button::Custom(custom) = style {
return custom.disabled(self);
}
let active = self.active(style);
button::Appearance {
shadow_offset: Vector::default(),
background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
Background::Gradient(gradient) => {
Background::Gradient(gradient.mul_alpha(0.5))
}
}),
text_color: Color {
a: active.text_color.a * 0.5,
..active.text_color
},
..active
}
}
}
/// The style of a checkbox.
#[derive(Default)]
pub enum Checkbox {