Simplify theming for Container widget

This commit is contained in:
Héctor Ramón Jiménez 2024-03-05 03:48:08 +01:00
parent 1f0a0c235a
commit 29326215cc
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
20 changed files with 275 additions and 186 deletions

View file

@ -20,7 +20,7 @@ use crate::text_editor;
use crate::text_input;
use crate::toggler;
use crate::core::{Background, Border, Color, Shadow};
use crate::core::{Background, Border, Color};
use std::fmt;
use std::rc::Rc;
@ -283,59 +283,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
}
}
/// The style of a container.
#[derive(Default)]
pub enum Container {
/// No style.
#[default]
Transparent,
/// A simple box.
Box,
/// A custom style.
Custom(Box<dyn container::StyleSheet<Style = Theme>>),
}
impl From<container::Appearance> for Container {
fn from(appearance: container::Appearance) -> Self {
Self::Custom(Box::new(move |_: &_| appearance))
}
}
impl<T: Fn(&Theme) -> container::Appearance + 'static> From<T> for Container {
fn from(f: T) -> Self {
Self::Custom(Box::new(f))
}
}
impl container::StyleSheet for Theme {
type Style = Container;
fn appearance(&self, style: &Self::Style) -> container::Appearance {
match style {
Container::Transparent => container::Appearance::default(),
Container::Box => {
let palette = self.extended_palette();
container::Appearance {
text_color: None,
background: Some(palette.background.weak.color.into()),
border: Border::with_radius(2),
shadow: Shadow::default(),
}
}
Container::Custom(custom) => custom.appearance(self),
}
}
}
impl<T: Fn(&Theme) -> container::Appearance> container::StyleSheet for T {
type Style = Theme;
fn appearance(&self, style: &Self::Style) -> container::Appearance {
(self)(style)
}
}
impl slider::StyleSheet for Theme {
fn default() -> fn(&Self, slider::Status) -> slider::Appearance {
slider