wip: Custom palette for built in theme
This commit is contained in:
parent
231d2fd845
commit
e2166ecad0
6 changed files with 39 additions and 7 deletions
|
|
@ -7,3 +7,4 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced = { path = "../.." }
|
iced = { path = "../.." }
|
||||||
|
once_cell = "1.14.0"
|
||||||
|
|
@ -1,14 +1,28 @@
|
||||||
|
use iced::theme::Palette;
|
||||||
|
use iced::theme::palette::Extended;
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, checkbox, column, container, horizontal_rule, progress_bar, radio,
|
button, checkbox, column, container, horizontal_rule, progress_bar, radio,
|
||||||
row, scrollable, slider, text, text_input, toggler, vertical_rule,
|
row, scrollable, slider, text, text_input, toggler, vertical_rule,
|
||||||
vertical_space,
|
vertical_space,
|
||||||
};
|
};
|
||||||
use iced::{Alignment, Element, Length, Sandbox, Settings, Theme};
|
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(Default)]
|
#[derive(Default)]
|
||||||
struct Styling {
|
struct Styling {
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
|
|
@ -51,11 +65,15 @@ impl Sandbox for Styling {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
let choose_theme = [Theme::Light, Theme::Dark].iter().fold(
|
let choose_theme = [Theme::Light, Theme::Dark, *CUSTOM_THEME.get().unwrap()].iter().fold(
|
||||||
column![text("Choose a theme:")].spacing(10),
|
column![text("Choose a theme:")].spacing(10),
|
||||||
|column, theme| {
|
|column, theme| {
|
||||||
column.push(radio(
|
column.push(radio(
|
||||||
format!("{:?}", theme),
|
match theme {
|
||||||
|
Theme::Light => "Light",
|
||||||
|
Theme::Dark => "Dark",
|
||||||
|
Theme::Custom { .. } => "Custom",
|
||||||
|
},
|
||||||
*theme,
|
*theme,
|
||||||
Some(self.theme),
|
Some(self.theme),
|
||||||
Message::ThemeChanged,
|
Message::ThemeChanged,
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,7 @@ where
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Renderer: crate::text::Renderer,
|
Renderer: crate::text::Renderer,
|
||||||
Renderer::Theme: widget::radio::StyleSheet,
|
Renderer::Theme: widget::radio::StyleSheet,
|
||||||
V: Copy + Eq,
|
V: Copy + PartialEq,
|
||||||
{
|
{
|
||||||
widget::Radio::new(value, label, selected, on_click)
|
widget::Radio::new(value, label, selected, on_click)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ where
|
||||||
f: F,
|
f: F,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
V: Eq + Copy,
|
V: PartialEq + Copy,
|
||||||
F: FnOnce(V) -> Message,
|
F: FnOnce(V) -> Message,
|
||||||
{
|
{
|
||||||
Radio {
|
Radio {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod palette;
|
pub mod palette;
|
||||||
|
|
||||||
|
use self::palette::Extended;
|
||||||
pub use self::palette::Palette;
|
pub use self::palette::Palette;
|
||||||
|
|
||||||
use crate::application;
|
use crate::application;
|
||||||
|
|
@ -20,10 +21,14 @@ use crate::toggler;
|
||||||
|
|
||||||
use iced_core::{Background, Color};
|
use iced_core::{Background, Color};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Theme {
|
pub enum Theme {
|
||||||
Light,
|
Light,
|
||||||
Dark,
|
Dark,
|
||||||
|
Custom {
|
||||||
|
palette: Palette,
|
||||||
|
extended: Extended
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Theme {
|
impl Theme {
|
||||||
|
|
@ -31,6 +36,7 @@ impl Theme {
|
||||||
match self {
|
match self {
|
||||||
Self::Light => Palette::LIGHT,
|
Self::Light => Palette::LIGHT,
|
||||||
Self::Dark => Palette::DARK,
|
Self::Dark => Palette::DARK,
|
||||||
|
Self::Custom { palette, .. } => palette
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,6 +44,7 @@ impl Theme {
|
||||||
match self {
|
match self {
|
||||||
Self::Light => &palette::EXTENDED_LIGHT,
|
Self::Light => &palette::EXTENDED_LIGHT,
|
||||||
Self::Dark => &palette::EXTENDED_DARK,
|
Self::Dark => &palette::EXTENDED_DARK,
|
||||||
|
Self::Custom { extended, .. } => extended,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ impl Palette {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Extended {
|
pub struct Extended {
|
||||||
pub background: Background,
|
pub background: Background,
|
||||||
pub primary: Primary,
|
pub primary: Primary,
|
||||||
|
|
@ -95,7 +96,7 @@ impl Extended {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Pair {
|
pub struct Pair {
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub text: Color,
|
pub text: Color,
|
||||||
|
|
@ -110,6 +111,7 @@ impl Pair {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Background {
|
pub struct Background {
|
||||||
pub base: Pair,
|
pub base: Pair,
|
||||||
pub weak: Pair,
|
pub weak: Pair,
|
||||||
|
|
@ -129,6 +131,7 @@ impl Background {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Primary {
|
pub struct Primary {
|
||||||
pub base: Pair,
|
pub base: Pair,
|
||||||
pub weak: Pair,
|
pub weak: Pair,
|
||||||
|
|
@ -148,6 +151,7 @@ impl Primary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Secondary {
|
pub struct Secondary {
|
||||||
pub base: Pair,
|
pub base: Pair,
|
||||||
pub weak: Pair,
|
pub weak: Pair,
|
||||||
|
|
@ -168,6 +172,7 @@ impl Secondary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Success {
|
pub struct Success {
|
||||||
pub base: Pair,
|
pub base: Pair,
|
||||||
pub weak: Pair,
|
pub weak: Pair,
|
||||||
|
|
@ -187,6 +192,7 @@ impl Success {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Danger {
|
pub struct Danger {
|
||||||
pub base: Pair,
|
pub base: Pair,
|
||||||
pub weak: Pair,
|
pub weak: Pair,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue