summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-03 03:21:26 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-03 03:23:36 +0100
commit9966c6f8834220e62fd66766d2e11d8a36e334e2 (patch)
treea1fc8c1ace0457016ae8baa74cbceebb28c873ab
parent09a531cd4427ff5dc972621094f33de0f94a0919 (diff)
downloadiced-9966c6f8834220e62fd66766d2e11d8a36e334e2.tar.gz
iced-9966c6f8834220e62fd66766d2e11d8a36e334e2.tar.bz2
iced-9966c6f8834220e62fd66766d2e11d8a36e334e2.zip
Make `Theme::Custom` fields opaque
-rw-r--r--examples/styling/src/main.rs89
-rw-r--r--style/src/theme.rs28
2 files changed, 63 insertions, 54 deletions
diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs
index 784ea581..e16860ad 100644
--- a/examples/styling/src/main.rs
+++ b/examples/styling/src/main.rs
@@ -1,28 +1,17 @@
-use iced::theme::Palette;
-use iced::theme::palette::Extended;
+use iced::theme::{self, Theme};
use iced::widget::{
button, checkbox, column, container, horizontal_rule, progress_bar, radio,
row, scrollable, slider, text, text_input, toggler, vertical_rule,
vertical_space,
};
-use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color};
+use iced::{Alignment, Color, Element, Length, Sandbox, Settings};
pub fn main() -> iced::Result {
-
Styling::run(Settings::default())
}
-
-#[derive(Debug, PartialEq, Eq, Clone, Copy)]
-enum ThemeType {
- Light,
- Dark,
- Custom,
-}
-
#[derive(Default)]
struct Styling {
- custom_theme: Theme,
theme: Theme,
input_value: String,
slider_value: f32,
@@ -30,6 +19,13 @@ struct Styling {
toggler_value: bool,
}
+#[derive(Debug, PartialEq, Eq, Clone, Copy)]
+enum ThemeType {
+ Light,
+ Dark,
+ Custom,
+}
+
#[derive(Debug, Clone)]
enum Message {
ThemeChanged(ThemeType),
@@ -44,21 +40,7 @@ impl Sandbox for Styling {
type Message = Message;
fn new() -> Self {
- let palette = Palette {
- background: Color::from_rgb(1.0, 0.9, 1.0),
- text: Color::BLACK,
- primary: Color::from_rgb(0.5, 0.5, 0.0),
- success: Color::from_rgb(0.0, 1.0, 0.0),
- danger: Color::from_rgb(1.0, 0.0, 0.0),
- };
- let extended = Extended::generate(palette);
- Styling {
- custom_theme: Theme::Custom {
- palette: Box::new(palette),
- extended: Box::new(extended)
- },
- ..Default::default()
- }
+ Styling::default()
}
fn title(&self) -> String {
@@ -67,11 +49,19 @@ impl Sandbox for Styling {
fn update(&mut self, message: Message) {
match message {
- Message::ThemeChanged(theme) => self.theme = match theme {
- ThemeType::Light => Theme::Light,
- ThemeType::Dark => Theme::Dark,
- ThemeType::Custom => self.custom_theme.clone(),
- },
+ Message::ThemeChanged(theme) => {
+ self.theme = match theme {
+ ThemeType::Light => Theme::Light,
+ ThemeType::Dark => Theme::Dark,
+ ThemeType::Custom => Theme::custom(theme::Palette {
+ background: Color::from_rgb(1.0, 0.9, 1.0),
+ text: Color::BLACK,
+ primary: Color::from_rgb(0.5, 0.5, 0.0),
+ success: Color::from_rgb(0.0, 1.0, 0.0),
+ danger: Color::from_rgb(1.0, 0.0, 0.0),
+ }),
+ }
+ }
Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {}
Message::SliderChanged(value) => self.slider_value = value,
@@ -81,21 +71,24 @@ impl Sandbox for Styling {
}
fn view(&self) -> Element<Message> {
- let choose_theme = [ThemeType::Light, ThemeType::Dark, ThemeType::Custom].iter().fold(
- column![text("Choose a theme:")].spacing(10),
- |column, theme| {
- column.push(radio(
- format!("{:?}", theme),
- *theme,
- Some(match self.theme {
- Theme::Light => ThemeType::Light,
- Theme::Dark => ThemeType::Dark,
- Theme::Custom { .. } => ThemeType::Custom,
- }),
- Message::ThemeChanged,
- ))
- },
- );
+ let choose_theme =
+ [ThemeType::Light, ThemeType::Dark, ThemeType::Custom]
+ .iter()
+ .fold(
+ column![text("Choose a theme:")].spacing(10),
+ |column, theme| {
+ column.push(radio(
+ format!("{:?}", theme),
+ *theme,
+ Some(match self.theme {
+ Theme::Light => ThemeType::Light,
+ Theme::Dark => ThemeType::Dark,
+ Theme::Custom { .. } => ThemeType::Custom,
+ }),
+ Message::ThemeChanged,
+ ))
+ },
+ );
let text_input = text_input(
"Type something...",
diff --git a/style/src/theme.rs b/style/src/theme.rs
index 4e83758b..35945aca 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -25,18 +25,19 @@ use iced_core::{Background, Color};
pub enum Theme {
Light,
Dark,
- Custom {
- palette: Box<Palette>,
- extended: Box<Extended>,
- }
+ Custom(Custom),
}
impl Theme {
+ pub fn custom(palette: Palette) -> Self {
+ Self::Custom(Custom::new(palette))
+ }
+
pub fn palette(self) -> Palette {
match self {
Self::Light => Palette::LIGHT,
Self::Dark => Palette::DARK,
- Self::Custom { palette, .. } => *palette
+ Self::Custom(custom) => custom.palette,
}
}
@@ -44,7 +45,7 @@ impl Theme {
match self {
Self::Light => &palette::EXTENDED_LIGHT,
Self::Dark => &palette::EXTENDED_DARK,
- Self::Custom { extended, .. } => extended,
+ Self::Custom(custom) => &custom.extended,
}
}
}
@@ -55,6 +56,21 @@ impl Default for Theme {
}
}
+#[derive(Debug, Clone, PartialEq)]
+pub struct Custom {
+ palette: Palette,
+ extended: Extended,
+}
+
+impl Custom {
+ pub fn new(palette: Palette) -> Self {
+ Self {
+ palette,
+ extended: Extended::generate(palette),
+ }
+ }
+}
+
#[derive(Debug, Clone, Copy)]
pub enum Application {
Default,