refactor: remove once_cell from styling example

This commit is contained in:
Ashley Wulber 2022-09-08 17:39:59 -04:00 committed by Héctor Ramón Jiménez
parent 269d6f9a3f
commit d5a933b047
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 15 additions and 14 deletions

View file

@ -7,4 +7,3 @@ publish = false
[dependencies] [dependencies]
iced = { path = "../.." } iced = { path = "../.." }
once_cell = "1.14.0"

View file

@ -6,22 +6,12 @@ use iced::widget::{
vertical_space, vertical_space,
}; };
use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color}; use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color};
use once_cell::sync::OnceCell;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
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);
CUSTOM_THEME.set(Theme::Custom { palette, extended }).unwrap();
Styling::run(Settings::default()) Styling::run(Settings::default())
} }
static CUSTOM_THEME: OnceCell<Theme> = OnceCell::new();
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ThemeType { enum ThemeType {
@ -32,6 +22,7 @@ enum ThemeType {
#[derive(Default)] #[derive(Default)]
struct Styling { struct Styling {
custom_theme: Theme,
theme: Theme, theme: Theme,
input_value: String, input_value: String,
slider_value: f32, slider_value: f32,
@ -53,7 +44,18 @@ impl Sandbox for Styling {
type Message = Message; type Message = Message;
fn new() -> Self { fn new() -> Self {
Styling::default() 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, extended },
..Default::default()
}
} }
fn title(&self) -> String { fn title(&self) -> String {
@ -65,7 +67,7 @@ impl Sandbox for Styling {
Message::ThemeChanged(theme) => self.theme = match theme { Message::ThemeChanged(theme) => self.theme = match theme {
ThemeType::Light => Theme::Light, ThemeType::Light => Theme::Light,
ThemeType::Dark => Theme::Dark, ThemeType::Dark => Theme::Dark,
ThemeType::Custom => *CUSTOM_THEME.get().unwrap(), ThemeType::Custom => self.custom_theme,
}, },
Message::InputChanged(value) => self.input_value = value, Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {} Message::ButtonPressed => {}