mod generated;
mod typography;
pub use generated::*;
pub use typography::*;
use std::fmt;
use std::rc::Rc;
use waft_embedder::Color;
use waft_foundation::App;
use waft_widgets::{
BuildContext, InheritedWidget, IntoWidget, MediaQuery, StatelessWidget, WidgetRef,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AcrylicBrushResources {
pub tint_color: Color,
pub tint_opacity: f64,
pub tint_luminosity_opacity: Option<f64>,
pub fallback_color: Color,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Theme {
#[default]
Light,
Dark,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AccentPalette {
pub base: Color,
pub light1: Color,
pub light2: Color,
pub light3: Color,
pub dark1: Color,
pub dark2: Color,
pub dark3: Color,
}
impl AccentPalette {
pub const WINDOWS_BLUE: AccentPalette = AccentPalette {
base: Color::from_argb(255, 0, 120, 212),
light1: Color::from_argb(255, 0, 145, 248),
light2: Color::from_argb(255, 76, 194, 255),
light3: Color::from_argb(255, 153, 235, 255),
dark1: Color::from_argb(255, 0, 103, 192),
dark2: Color::from_argb(255, 0, 62, 146),
dark3: Color::from_argb(255, 0, 26, 104),
};
}
impl Default for AccentPalette {
fn default() -> Self {
Self::WINDOWS_BLUE
}
}
impl ThemeResources {
pub fn of(app: &mut App, context: BuildContext) -> Rc<ThemeResources> {
if let Some(scope) =
context.depend_on_inherited_widget_of_exact_type::<InheritedResources>(app)
{
return scope.resources.clone();
}
let theme = match MediaQuery::maybe_platform_brightness_of(app, context) {
Some(waft_embedder::Brightness::Dark) => Theme::Dark,
_ => Theme::Light,
};
ThemeResources::default_for(theme)
}
fn default_for(theme: Theme) -> Rc<ThemeResources> {
thread_local! {
static LIGHT: Rc<ThemeResources> = Rc::new(ThemeResources::new(Theme::Light, AccentPalette::default()));
static DARK: Rc<ThemeResources> = Rc::new(ThemeResources::new(Theme::Dark, AccentPalette::default()));
}
match theme {
Theme::Light => LIGHT.with(Rc::clone),
Theme::Dark => DARK.with(Rc::clone),
}
}
}
pub struct ThemeScope {
base: Base,
any: Option<Override>,
light: Option<Override>,
dark: Option<Override>,
child: WidgetRef,
}
#[allow(clippy::large_enum_variant, reason = "a scope is made once per build")]
enum Base {
Theme(Theme, AccentPalette),
Enclosing,
Resources(Rc<ThemeResources>),
}
type Override = Rc<dyn Fn(&mut ThemeResources)>;
impl ThemeScope {
pub fn new<K>(theme: Theme, child: impl IntoWidget<K>) -> ThemeScope {
ThemeScope::from_base(Base::Theme(theme, AccentPalette::default()), child)
}
pub fn overriding<K>(child: impl IntoWidget<K>) -> ThemeScope {
ThemeScope::from_base(Base::Enclosing, child)
}
pub fn with_resources<K>(
resources: Rc<ThemeResources>,
child: impl IntoWidget<K>,
) -> ThemeScope {
ThemeScope::from_base(Base::Resources(resources), child)
}
fn from_base<K>(base: Base, child: impl IntoWidget<K>) -> ThemeScope {
ThemeScope {
base,
any: None,
light: None,
dark: None,
child: child.into_widget(),
}
}
pub fn accent(mut self, accent: AccentPalette) -> ThemeScope {
match &mut self.base {
Base::Theme(_, palette) => *palette = accent,
_ => panic!("`accent` sets the palette of a scope made with `ThemeScope::new`"),
}
self
}
pub fn any(mut self, change: impl Fn(&mut ThemeResources) + 'static) -> ThemeScope {
self.any = Some(Rc::new(change));
self
}
pub fn light(mut self, change: impl Fn(&mut ThemeResources) + 'static) -> ThemeScope {
self.light = Some(Rc::new(change));
self
}
pub fn dark(mut self, change: impl Fn(&mut ThemeResources) + 'static) -> ThemeScope {
self.dark = Some(Rc::new(change));
self
}
fn base(&self, app: &mut App, context: BuildContext) -> Rc<ThemeResources> {
match &self.base {
Base::Theme(theme, accent) if *accent == AccentPalette::default() => {
ThemeResources::default_for(*theme)
}
Base::Theme(theme, accent) => Rc::new(ThemeResources::new(*theme, *accent)),
Base::Enclosing => ThemeResources::of(app, context),
Base::Resources(resources) => resources.clone(),
}
}
fn changed(&self, resources: Rc<ThemeResources>) -> Rc<ThemeResources> {
let for_theme = match resources.theme {
Theme::Light => &self.light,
Theme::Dark => &self.dark,
};
let changes: Vec<&Override> = self.any.iter().chain(for_theme).collect();
if changes.is_empty() {
return resources;
}
let mut resources = Rc::unwrap_or_clone(resources);
for change in changes {
change(&mut resources);
}
Rc::new(resources)
}
}
impl fmt::Debug for ThemeScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ThemeScope").finish_non_exhaustive()
}
}
impl StatelessWidget for ThemeScope {
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let resources = self.changed(self.base(app, context));
InheritedResources {
resources,
child: self.child.clone(),
}
.into_widget()
}
}
#[derive(Debug)]
struct InheritedResources {
resources: Rc<ThemeResources>,
child: WidgetRef,
}
impl InheritedWidget for InheritedResources {
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old: &Self) -> bool {
!Rc::ptr_eq(&self.resources, &old.resources) && self.resources != old.resources
}
}