Use Theme::ALL for theme selector in styling example

This commit is contained in:
Héctor Ramón Jiménez 2024-02-03 18:51:30 +01:00
parent 5770efe06d
commit 17cb548e20
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 28 additions and 74 deletions

View file

@ -1,10 +1,9 @@
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,
button, checkbox, column, container, horizontal_rule, pick_list,
progress_bar, row, scrollable, slider, text, text_input, toggler,
vertical_rule, vertical_space,
};
use iced::{Alignment, Color, Element, Length, Sandbox, Settings};
use iced::{Alignment, Element, Length, Sandbox, Settings, Theme};
pub fn main() -> iced::Result {
Styling::run(Settings::default())
@ -19,22 +18,9 @@ struct Styling {
toggler_value: bool,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ThemeType {
Light,
Dark,
Nord,
GruvboxLight,
GruvboxDark,
Dracula,
SolarizedLight,
SolarizedDark,
Custom,
}
#[derive(Debug, Clone)]
enum Message {
ThemeChanged(ThemeType),
ThemeChanged(Theme),
InputChanged(String),
ButtonPressed,
SliderChanged(f32),
@ -56,26 +42,7 @@ 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::Nord => Theme::Nord,
ThemeType::GruvboxLight => Theme::GruvboxLight,
ThemeType::GruvboxDark => Theme::GruvboxDark,
ThemeType::Dracula => Theme::Dracula,
ThemeType::SolarizedLight => Theme::SolarizedLight,
ThemeType::SolarizedDark => Theme::SolarizedDark,
ThemeType::Custom => Theme::custom(
String::from("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),
},
),
}
self.theme = theme;
}
Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {}
@ -86,39 +53,16 @@ impl Sandbox for Styling {
}
fn view(&self) -> Element<Message> {
let choose_theme = [
ThemeType::Light,
ThemeType::Dark,
ThemeType::Nord,
ThemeType::Dracula,
ThemeType::SolarizedLight,
ThemeType::SolarizedDark,
ThemeType::GruvboxLight,
ThemeType::GruvboxDark,
ThemeType::Custom,
let choose_theme = column![
text("Theme:"),
pick_list(
Theme::ALL,
Some(self.theme.clone()),
Message::ThemeChanged
)
.width(Length::Fill),
]
.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::Dracula => ThemeType::Dracula,
Theme::Nord => ThemeType::Nord,
Theme::SolarizedLight => ThemeType::SolarizedLight,
Theme::SolarizedDark => ThemeType::SolarizedDark,
Theme::GruvboxLight => ThemeType::GruvboxLight,
Theme::GruvboxDark => ThemeType::GruvboxDark,
Theme::Custom { .. } => ThemeType::Custom,
}),
Message::ThemeChanged,
))
},
);
.spacing(10);
let text_input = text_input("Type something...", &self.input_value)
.on_input(Message::InputChanged)

View file

@ -25,6 +25,7 @@ use crate::core::{Background, Border, Color, Shadow, Vector};
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;
/// A built-in theme.
#[derive(Debug, Clone, PartialEq, Default)]
@ -47,12 +48,21 @@ pub enum Theme {
/// The built-in gruvbox dark variant.
GruvboxDark,
/// A [`Theme`] that uses a [`Custom`] palette.
Custom(Box<Custom>),
Custom(Arc<Custom>),
}
impl Theme {
/// A list with all the defined themes.
pub const ALL: &'static [Self] = &[Self::Light, Self::Dark];
pub const ALL: &'static [Self] = &[
Self::Light,
Self::Dark,
Self::Dracula,
Self::Nord,
Self::SolarizedLight,
Self::SolarizedDark,
Self::GruvboxLight,
Self::GruvboxDark,
];
/// Creates a new custom [`Theme`] from the given [`Palette`].
pub fn custom(name: String, palette: Palette) -> Self {
@ -66,7 +76,7 @@ impl Theme {
palette: Palette,
generate: impl FnOnce(Palette) -> palette::Extended,
) -> Self {
Self::Custom(Box::new(Custom::with_fn(name, palette, generate)))
Self::Custom(Arc::new(Custom::with_fn(name, palette, generate)))
}
/// Returns the [`Palette`] of the [`Theme`].