summaryrefslogtreecommitdiffstats
path: root/style
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-04 20:42:37 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-04 20:42:37 +0100
commitf4a4845ddbdced81ae4ff60bfa19f0e602d84709 (patch)
treeb532017384eb9e43e57bf73be372aea0d55af652 /style
parentdb92e1c942154bee474fee5e2c187f8a52a1bb96 (diff)
downloadiced-f4a4845ddbdced81ae4ff60bfa19f0e602d84709.tar.gz
iced-f4a4845ddbdced81ae4ff60bfa19f0e602d84709.tar.bz2
iced-f4a4845ddbdced81ae4ff60bfa19f0e602d84709.zip
Simplify theming for `Button` widget
Diffstat (limited to 'style')
-rw-r--r--style/src/button.rs78
-rw-r--r--style/src/checkbox.rs10
-rw-r--r--style/src/theme.rs123
3 files changed, 2 insertions, 209 deletions
diff --git a/style/src/button.rs b/style/src/button.rs
index 0d7a668a..8b137891 100644
--- a/style/src/button.rs
+++ b/style/src/button.rs
@@ -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
- }
- }
-}
diff --git a/style/src/checkbox.rs b/style/src/checkbox.rs
index 77093f69..5e1c8374 100644
--- a/style/src/checkbox.rs
+++ b/style/src/checkbox.rs
@@ -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
}
}
diff --git a/style/src/theme.rs b/style/src/theme.rs
index 43e7cafd..f967aebc 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -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 {