Add Theme selector to layout example
This commit is contained in:
parent
d76705df29
commit
3850a46db6
5 changed files with 60 additions and 21 deletions
|
|
@ -1,8 +1,8 @@
|
|||
use iced::executor;
|
||||
use iced::keyboard;
|
||||
use iced::widget::{
|
||||
button, checkbox, column, container, horizontal_space, row, text,
|
||||
vertical_rule,
|
||||
button, checkbox, column, container, horizontal_space, pick_list, row,
|
||||
text, vertical_rule,
|
||||
};
|
||||
use iced::{
|
||||
color, Alignment, Application, Color, Command, Element, Font, Length,
|
||||
|
|
@ -17,13 +17,15 @@ pub fn main() -> iced::Result {
|
|||
struct Layout {
|
||||
example: Example,
|
||||
explain: bool,
|
||||
theme: Theme,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
Next,
|
||||
Previous,
|
||||
ExplainToggled(bool),
|
||||
ThemeSelected(Theme),
|
||||
}
|
||||
|
||||
impl Application for Layout {
|
||||
|
|
@ -37,6 +39,7 @@ impl Application for Layout {
|
|||
Self {
|
||||
example: Example::default(),
|
||||
explain: false,
|
||||
theme: Theme::Light,
|
||||
},
|
||||
Command::none(),
|
||||
)
|
||||
|
|
@ -57,6 +60,9 @@ impl Application for Layout {
|
|||
Message::ExplainToggled(explain) => {
|
||||
self.explain = explain;
|
||||
}
|
||||
Message::ThemeSelected(theme) => {
|
||||
self.theme = theme;
|
||||
}
|
||||
}
|
||||
|
||||
Command::none()
|
||||
|
|
@ -75,7 +81,13 @@ impl Application for Layout {
|
|||
text(self.example.title).size(20).font(Font::MONOSPACE),
|
||||
horizontal_space(Length::Fill),
|
||||
checkbox("Explain", self.explain, Message::ExplainToggled),
|
||||
pick_list(
|
||||
Theme::ALL,
|
||||
Some(self.theme.clone()),
|
||||
Message::ThemeSelected
|
||||
),
|
||||
]
|
||||
.spacing(20)
|
||||
.align_items(Alignment::Center);
|
||||
|
||||
let example = container(if self.explain {
|
||||
|
|
@ -115,7 +127,7 @@ impl Application for Layout {
|
|||
}
|
||||
|
||||
fn theme(&self) -> Theme {
|
||||
Theme::Dark
|
||||
self.theme.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -53,13 +53,16 @@ impl Sandbox for Styling {
|
|||
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),
|
||||
}),
|
||||
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),
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
Message::InputChanged(value) => self.input_value = value,
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ use crate::toggler;
|
|||
|
||||
use iced_core::{Background, Color, Vector};
|
||||
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A built-in theme.
|
||||
|
|
@ -38,18 +39,22 @@ pub enum Theme {
|
|||
}
|
||||
|
||||
impl Theme {
|
||||
/// A list with all the defined themes.
|
||||
pub const ALL: &'static [Self] = &[Self::Light, Self::Dark];
|
||||
|
||||
/// Creates a new custom [`Theme`] from the given [`Palette`].
|
||||
pub fn custom(palette: Palette) -> Self {
|
||||
Self::custom_with_fn(palette, palette::Extended::generate)
|
||||
pub fn custom(name: String, palette: Palette) -> Self {
|
||||
Self::custom_with_fn(name, palette, palette::Extended::generate)
|
||||
}
|
||||
|
||||
/// Creates a new custom [`Theme`] from the given [`Palette`], with
|
||||
/// a custom generator of a [`palette::Extended`].
|
||||
pub fn custom_with_fn(
|
||||
name: String,
|
||||
palette: Palette,
|
||||
generate: impl FnOnce(Palette) -> palette::Extended,
|
||||
) -> Self {
|
||||
Self::Custom(Box::new(Custom::with_fn(palette, generate)))
|
||||
Self::Custom(Box::new(Custom::with_fn(name, palette, generate)))
|
||||
}
|
||||
|
||||
/// Returns the [`Palette`] of the [`Theme`].
|
||||
|
|
@ -71,32 +76,51 @@ impl Theme {
|
|||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Theme {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Light => write!(f, "Light"),
|
||||
Self::Dark => write!(f, "Dark"),
|
||||
Self::Custom(custom) => custom.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A [`Theme`] with a customized [`Palette`].
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Custom {
|
||||
name: String,
|
||||
palette: Palette,
|
||||
extended: palette::Extended,
|
||||
}
|
||||
|
||||
impl Custom {
|
||||
/// Creates a [`Custom`] theme from the given [`Palette`].
|
||||
pub fn new(palette: Palette) -> Self {
|
||||
Self::with_fn(palette, palette::Extended::generate)
|
||||
pub fn new(name: String, palette: Palette) -> Self {
|
||||
Self::with_fn(name, palette, palette::Extended::generate)
|
||||
}
|
||||
|
||||
/// Creates a [`Custom`] theme from the given [`Palette`] with
|
||||
/// a custom generator of a [`palette::Extended`].
|
||||
pub fn with_fn(
|
||||
name: String,
|
||||
palette: Palette,
|
||||
generate: impl FnOnce(Palette) -> palette::Extended,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
palette,
|
||||
extended: generate(palette),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Custom {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.name)
|
||||
}
|
||||
}
|
||||
|
||||
/// The style of an application.
|
||||
#[derive(Default)]
|
||||
pub enum Application {
|
||||
|
|
|
|||
|
|
@ -271,7 +271,7 @@ pub fn pick_list<'a, Message, Renderer, T>(
|
|||
on_selected: impl Fn(T) -> Message + 'a,
|
||||
) -> PickList<'a, T, Message, Renderer>
|
||||
where
|
||||
T: ToString + Eq + 'static,
|
||||
T: ToString + PartialEq + 'static,
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
Renderer: core::text::Renderer,
|
||||
Renderer::Theme: pick_list::StyleSheet
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ where
|
|||
|
||||
impl<'a, T: 'a, Message, Renderer> PickList<'a, T, Message, Renderer>
|
||||
where
|
||||
T: ToString + Eq,
|
||||
T: ToString + PartialEq,
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
Renderer: text::Renderer,
|
||||
Renderer::Theme: StyleSheet
|
||||
|
|
@ -145,7 +145,7 @@ where
|
|||
impl<'a, T: 'a, Message, Renderer> Widget<Message, Renderer>
|
||||
for PickList<'a, T, Message, Renderer>
|
||||
where
|
||||
T: Clone + ToString + Eq + 'static,
|
||||
T: Clone + ToString + PartialEq + 'static,
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
Message: 'a,
|
||||
Renderer: text::Renderer + 'a,
|
||||
|
|
@ -281,7 +281,7 @@ where
|
|||
impl<'a, T: 'a, Message, Renderer> From<PickList<'a, T, Message, Renderer>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
T: Clone + ToString + Eq + 'static,
|
||||
T: Clone + ToString + PartialEq + 'static,
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
Message: 'a,
|
||||
Renderer: text::Renderer + 'a,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue