summaryrefslogtreecommitdiffstats
path: root/style
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-10 08:15:05 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-10 10:01:51 +0100
commit3850a46db6e13f2948f5731f4ceec42764391f5d (patch)
tree032c2cf31412cbc87beef56b7654bbc1a1909c81 /style
parentd76705df29f1960124bd06277683448e18f788b0 (diff)
downloadiced-3850a46db6e13f2948f5731f4ceec42764391f5d.tar.gz
iced-3850a46db6e13f2948f5731f4ceec42764391f5d.tar.bz2
iced-3850a46db6e13f2948f5731f4ceec42764391f5d.zip
Add `Theme` selector to `layout` example
Diffstat (limited to 'style')
-rw-r--r--style/src/theme.rs36
1 files changed, 30 insertions, 6 deletions
diff --git a/style/src/theme.rs b/style/src/theme.rs
index eafb0b47..deccf455 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -23,6 +23,7 @@ use crate::toggler;
use iced_core::{Background, Color, Vector};
+use std::fmt;
use std::rc::Rc;
/// A built-in theme.
@@ -38,18 +39,22 @@ pub enum Theme {
}
impl Theme {
+ /// A list with all the defined themes.
+ pub const ALL: &'static [Self] = &[Self::Light, Self::Dark];
+
/// Creates a new custom [`Theme`] from the given [`Palette`].
- pub fn custom(palette: Palette) -> Self {
- Self::custom_with_fn(palette, palette::Extended::generate)
+ pub fn custom(name: String, palette: Palette) -> Self {
+ Self::custom_with_fn(name, palette, palette::Extended::generate)
}
/// Creates a new custom [`Theme`] from the given [`Palette`], with
/// a custom generator of a [`palette::Extended`].
pub fn custom_with_fn(
+ name: String,
palette: Palette,
generate: impl FnOnce(Palette) -> palette::Extended,
) -> Self {
- Self::Custom(Box::new(Custom::with_fn(palette, generate)))
+ Self::Custom(Box::new(Custom::with_fn(name, palette, generate)))
}
/// Returns the [`Palette`] of the [`Theme`].
@@ -71,32 +76,51 @@ impl Theme {
}
}
+impl fmt::Display for Theme {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::Light => write!(f, "Light"),
+ Self::Dark => write!(f, "Dark"),
+ Self::Custom(custom) => custom.fmt(f),
+ }
+ }
+}
+
/// A [`Theme`] with a customized [`Palette`].
-#[derive(Debug, Clone, Copy, PartialEq)]
+#[derive(Debug, Clone, PartialEq)]
pub struct Custom {
+ name: String,
palette: Palette,
extended: palette::Extended,
}
impl Custom {
/// Creates a [`Custom`] theme from the given [`Palette`].
- pub fn new(palette: Palette) -> Self {
- Self::with_fn(palette, palette::Extended::generate)
+ pub fn new(name: String, palette: Palette) -> Self {
+ Self::with_fn(name, palette, palette::Extended::generate)
}
/// Creates a [`Custom`] theme from the given [`Palette`] with
/// a custom generator of a [`palette::Extended`].
pub fn with_fn(
+ name: String,
palette: Palette,
generate: impl FnOnce(Palette) -> palette::Extended,
) -> Self {
Self {
+ name,
palette,
extended: generate(palette),
}
}
}
+impl fmt::Display for Custom {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.name)
+ }
+}
+
/// The style of an application.
#[derive(Default)]
pub enum Application {