Draft first-class Theme support
RFC: https://github.com/iced-rs/rfcs/pull/6
This commit is contained in:
parent
5de337f214
commit
664251f3f5
113 changed files with 767 additions and 878 deletions
|
|
@ -27,10 +27,12 @@ impl std::default::Default for Style {
|
|||
|
||||
/// A set of rules that dictate the style of a button.
|
||||
pub trait StyleSheet {
|
||||
fn active(&self) -> Style;
|
||||
type Variant;
|
||||
|
||||
fn hovered(&self) -> Style {
|
||||
let active = self.active();
|
||||
fn active(&self, variant: Self::Variant) -> Style;
|
||||
|
||||
fn hovered(&self, variant: Self::Variant) -> Style {
|
||||
let active = self.active(variant);
|
||||
|
||||
Style {
|
||||
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
|
||||
|
|
@ -38,15 +40,15 @@ pub trait StyleSheet {
|
|||
}
|
||||
}
|
||||
|
||||
fn pressed(&self) -> Style {
|
||||
fn pressed(&self, variant: Self::Variant) -> Style {
|
||||
Style {
|
||||
shadow_offset: Vector::default(),
|
||||
..self.active()
|
||||
..self.active(variant)
|
||||
}
|
||||
}
|
||||
|
||||
fn disabled(&self) -> Style {
|
||||
let active = self.active();
|
||||
fn disabled(&self, variant: Self::Variant) -> Style {
|
||||
let active = self.active(variant);
|
||||
|
||||
Style {
|
||||
shadow_offset: Vector::default(),
|
||||
|
|
@ -64,33 +66,3 @@ pub trait StyleSheet {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Default;
|
||||
|
||||
impl StyleSheet for Default {
|
||||
fn active(&self) -> Style {
|
||||
Style {
|
||||
shadow_offset: Vector::new(0.0, 0.0),
|
||||
background: Some(Background::Color([0.87, 0.87, 0.87].into())),
|
||||
border_radius: 2.0,
|
||||
border_width: 1.0,
|
||||
border_color: [0.7, 0.7, 0.7].into(),
|
||||
text_color: Color::BLACK,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> std::default::Default for Box<dyn StyleSheet + 'a> {
|
||||
fn default() -> Self {
|
||||
Box::new(Default)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> From<T> for Box<dyn StyleSheet + 'a>
|
||||
where
|
||||
T: StyleSheet + 'a,
|
||||
{
|
||||
fn from(style_sheet: T) -> Self {
|
||||
Box::new(style_sheet)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,4 +21,7 @@ pub mod rule;
|
|||
pub mod scrollable;
|
||||
pub mod slider;
|
||||
pub mod text_input;
|
||||
pub mod theme;
|
||||
pub mod toggler;
|
||||
|
||||
pub use theme::Theme;
|
||||
|
|
|
|||
45
style/src/theme.rs
Normal file
45
style/src/theme.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::button;
|
||||
|
||||
use iced_core::{Color, Vector};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Theme {
|
||||
Light,
|
||||
Dark,
|
||||
}
|
||||
|
||||
impl Default for Theme {
|
||||
fn default() -> Self {
|
||||
Self::Light
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Button {
|
||||
Primary,
|
||||
Secondary,
|
||||
Positive,
|
||||
Destructive,
|
||||
Text,
|
||||
}
|
||||
|
||||
impl Default for Button {
|
||||
fn default() -> Self {
|
||||
Self::Primary
|
||||
}
|
||||
}
|
||||
|
||||
impl button::StyleSheet for Theme {
|
||||
type Variant = Button;
|
||||
|
||||
fn active(&self, _variant: Self::Variant) -> button::Style {
|
||||
button::Style {
|
||||
shadow_offset: Vector::default(),
|
||||
background: None,
|
||||
border_radius: 0.0,
|
||||
border_width: 0.0,
|
||||
border_color: Color::TRANSPARENT,
|
||||
text_color: Color::BLACK,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue