summaryrefslogtreecommitdiffstats
path: root/style
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-04 03:57:03 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-04 03:57:03 +0100
commitce309db37b8eb9860ae1f1be1710fb39e7a9edea (patch)
treea28ef6659f6235ec7bc2a7403cfef467a71fb2bf /style
parent1bb5a1b9a23e1c4739430ac87ca33b06c2f4d9df (diff)
downloadiced-ce309db37b8eb9860ae1f1be1710fb39e7a9edea.tar.gz
iced-ce309db37b8eb9860ae1f1be1710fb39e7a9edea.tar.bz2
iced-ce309db37b8eb9860ae1f1be1710fb39e7a9edea.zip
Try new approach to theming for `Slider`
Diffstat (limited to 'style')
-rw-r--r--style/src/lib.rs2
-rw-r--r--style/src/slider.rs29
-rw-r--r--style/src/theme.rs108
3 files changed, 49 insertions, 90 deletions
diff --git a/style/src/lib.rs b/style/src/lib.rs
index 3c2865eb..17ba09c4 100644
--- a/style/src/lib.rs
+++ b/style/src/lib.rs
@@ -10,7 +10,7 @@
#![forbid(unsafe_code, rust_2018_idioms)]
#![deny(
unused_results,
- missing_docs,
+ // missing_docs,
unused_results,
rustdoc::broken_intra_doc_links
)]
diff --git a/style/src/slider.rs b/style/src/slider.rs
index bf1c7329..0c19e47d 100644
--- a/style/src/slider.rs
+++ b/style/src/slider.rs
@@ -1,6 +1,6 @@
//! Change the apperance of a slider.
use crate::core::border;
-use crate::core::Color;
+use crate::core::{Color, Pixels};
/// The appearance of a slider.
#[derive(Debug, Clone, Copy)]
@@ -11,6 +11,17 @@ pub struct Appearance {
pub handle: Handle,
}
+impl Appearance {
+ /// Changes the [`HandleShape`] of the [`Appearance`] to a circle
+ /// with the given radius.
+ pub fn with_circular_handle(mut self, radius: impl Into<Pixels>) -> Self {
+ self.handle.shape = HandleShape::Circle {
+ radius: radius.into().0,
+ };
+ self
+ }
+}
+
/// The appearance of a slider rail
#[derive(Debug, Clone, Copy)]
pub struct Rail {
@@ -54,15 +65,11 @@ pub enum HandleShape {
/// A set of rules that dictate the style of a slider.
pub trait StyleSheet {
- /// The supported style of the [`StyleSheet`].
- type Style: Default;
-
- /// Produces the style of an active slider.
- fn active(&self, style: &Self::Style) -> Appearance;
-
- /// Produces the style of an hovered slider.
- fn hovered(&self, style: &Self::Style) -> Appearance;
+ fn default() -> fn(&Self, Status) -> Appearance;
+}
- /// Produces the style of a slider that is being dragged.
- fn dragging(&self, style: &Self::Style) -> Appearance;
+pub enum Status {
+ Active,
+ Hovered,
+ Dragging,
}
diff --git a/style/src/theme.rs b/style/src/theme.rs
index 0b56e101..656d6bf9 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -608,88 +608,40 @@ impl<T: Fn(&Theme) -> container::Appearance> container::StyleSheet for T {
}
}
-/// The style of a slider.
-#[derive(Default)]
-pub enum Slider {
- /// The default style.
- #[default]
- Default,
- /// A custom style.
- Custom(Box<dyn slider::StyleSheet<Style = Theme>>),
-}
-
impl slider::StyleSheet for Theme {
- type Style = Slider;
-
- fn active(&self, style: &Self::Style) -> slider::Appearance {
- match style {
- Slider::Default => {
- let palette = self.extended_palette();
-
- let handle = slider::Handle {
- shape: slider::HandleShape::Rectangle {
- width: 8,
- border_radius: 4.0.into(),
- },
- color: Color::WHITE,
- border_color: Color::WHITE,
- border_width: 1.0,
- };
-
- slider::Appearance {
- rail: slider::Rail {
- colors: (
- palette.primary.base.color,
- palette.secondary.base.color,
- ),
- width: 4.0,
- border_radius: 2.0.into(),
- },
- handle: slider::Handle {
- color: palette.background.base.color,
- border_color: palette.primary.base.color,
- ..handle
- },
- }
- }
- Slider::Custom(custom) => custom.active(self),
- }
- }
-
- fn hovered(&self, style: &Self::Style) -> slider::Appearance {
- match style {
- Slider::Default => {
- let active = self.active(style);
- let palette = self.extended_palette();
-
- slider::Appearance {
- handle: slider::Handle {
- color: palette.primary.weak.color,
- ..active.handle
- },
- ..active
- }
- }
- Slider::Custom(custom) => custom.hovered(self),
- }
+ fn default() -> fn(&Self, slider::Status) -> slider::Appearance {
+ slider
}
+}
- fn dragging(&self, style: &Self::Style) -> slider::Appearance {
- match style {
- Slider::Default => {
- let active = self.active(style);
- let palette = self.extended_palette();
+pub fn slider(theme: &Theme, status: slider::Status) -> slider::Appearance {
+ let palette = theme.extended_palette();
- slider::Appearance {
- handle: slider::Handle {
- color: palette.primary.base.color,
- ..active.handle
- },
- ..active
- }
- }
- Slider::Custom(custom) => custom.dragging(self),
- }
+ let handle = slider::Handle {
+ shape: slider::HandleShape::Rectangle {
+ width: 8,
+ border_radius: 4.0.into(),
+ },
+ color: Color::WHITE,
+ border_color: Color::WHITE,
+ border_width: 1.0,
+ };
+
+ slider::Appearance {
+ rail: slider::Rail {
+ colors: (palette.primary.base.color, palette.secondary.base.color),
+ width: 4.0,
+ border_radius: 2.0.into(),
+ },
+ handle: slider::Handle {
+ color: match status {
+ slider::Status::Active => palette.background.base.color,
+ slider::Status::Hovered => palette.primary.weak.color,
+ slider::Status::Dragging => palette.primary.base.color,
+ },
+ border_color: palette.primary.base.color,
+ ..handle
+ },
}
}