Make Theme::Custom fields opaque
This commit is contained in:
parent
09a531cd44
commit
9966c6f883
2 changed files with 64 additions and 55 deletions
|
|
@ -1,17 +1,23 @@
|
|||
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(Default)]
|
||||
struct Styling {
|
||||
theme: Theme,
|
||||
input_value: String,
|
||||
slider_value: f32,
|
||||
checkbox_value: bool,
|
||||
toggler_value: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
enum ThemeType {
|
||||
|
|
@ -20,16 +26,6 @@ enum ThemeType {
|
|||
Custom,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Styling {
|
||||
custom_theme: Theme,
|
||||
theme: Theme,
|
||||
input_value: String,
|
||||
slider_value: f32,
|
||||
checkbox_value: bool,
|
||||
toggler_value: bool,
|
||||
}
|
||||
|
||||
#[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...",
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue