Skip to content

Commit

Permalink
refactor: remove once_cell from styling example
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Sep 8, 2022
1 parent 22cc0a3 commit c0ee1d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 0 additions & 1 deletion examples/styling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ publish = false

[dependencies]
iced = { path = "../.." }
once_cell = "1.14.0"
28 changes: 15 additions & 13 deletions examples/styling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ use iced::widget::{
vertical_space,
};
use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color};
use once_cell::sync::OnceCell;

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())
}

static CUSTOM_THEME: OnceCell<Theme> = OnceCell::new();

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ThemeType {
Expand All @@ -32,6 +22,7 @@ enum ThemeType {

#[derive(Default)]
struct Styling {
custom_theme: Theme,
theme: Theme,
input_value: String,
slider_value: f32,
Expand All @@ -53,7 +44,18 @@ impl Sandbox for Styling {
type Message = Message;

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 {
Expand All @@ -65,7 +67,7 @@ impl Sandbox for Styling {
Message::ThemeChanged(theme) => self.theme = match theme {
ThemeType::Light => Theme::Light,
ThemeType::Dark => Theme::Dark,
ThemeType::Custom => *CUSTOM_THEME.get().unwrap(),
ThemeType::Custom => self.custom_theme,
},
Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {}
Expand Down

0 comments on commit c0ee1d1

Please sign in to comment.