From 1dd1a2f97fc747e15e12b5188dad6c41b0d052ea Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Jun 2022 10:51:01 +0200 Subject: Introduce `StyleSheet` for `Text` widget --- style/src/lib.rs | 1 + style/src/text.rs | 18 ++++++++++++++++++ style/src/theme.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 style/src/text.rs (limited to 'style/src') diff --git a/style/src/lib.rs b/style/src/lib.rs index 4a0a6a14..ee426e98 100644 --- a/style/src/lib.rs +++ b/style/src/lib.rs @@ -21,6 +21,7 @@ pub mod radio; pub mod rule; pub mod scrollable; pub mod slider; +pub mod text; pub mod text_input; pub mod theme; pub mod toggler; diff --git a/style/src/text.rs b/style/src/text.rs new file mode 100644 index 00000000..69a4ed85 --- /dev/null +++ b/style/src/text.rs @@ -0,0 +1,18 @@ +use iced_core::Color; + +pub trait StyleSheet { + type Style: Default + Copy; + + fn appearance(&self, style: Self::Style) -> Appearance; +} + +#[derive(Debug, Clone, Copy)] +pub struct Appearance { + pub color: Option, +} + +impl Default for Appearance { + fn default() -> Self { + Self { color: None } + } +} diff --git a/style/src/theme.rs b/style/src/theme.rs index b1e18c55..0bae671e 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -14,6 +14,7 @@ use crate::radio; use crate::rule; use crate::scrollable; use crate::slider; +use crate::text; use crate::text_input; use crate::toggler; @@ -601,6 +602,40 @@ impl scrollable::StyleSheet for Theme { } } +/* + * Text + */ +#[derive(Clone, Copy)] +pub enum Text { + Default, + Color(Color), + Custom(fn(&Theme) -> text::Appearance), +} + +impl Default for Text { + fn default() -> Self { + Self::Default + } +} + +impl From for Text { + fn from(color: Color) -> Self { + Text::Color(color) + } +} + +impl text::StyleSheet for Theme { + type Style = Text; + + fn appearance(&self, style: Self::Style) -> text::Appearance { + match style { + Text::Default => Default::default(), + Text::Color(c) => text::Appearance { color: Some(c) }, + Text::Custom(f) => f(self), + } + } +} + /* * Text Input */ -- cgit