use super::{ColorsLinear, ColorsSrgb, RasterConfig, ThemeDraw, ThemeSize};
use crate::draw::{color, DrawIface, DrawSharedImpl, SharedState};
use crate::event::EventState;
use crate::{autoimpl, Action};
use std::any::Any;
#[allow(unused)] use crate::event::EventCx;
#[crate::autoimpl(for<T: trait + ?Sized> &mut T, Box<T>)]
pub trait ThemeControl {
fn set_font_size(&mut self, pt_size: f32) -> Action;
fn active_scheme(&self) -> &str;
fn list_schemes(&self) -> Vec<&str>;
fn get_scheme(&self, name: &str) -> Option<&ColorsSrgb>;
fn get_colors(&self) -> &ColorsLinear;
fn set_colors(&mut self, name: String, scheme: ColorsLinear) -> Action;
fn set_scheme(&mut self, name: &str) -> Action {
if name != self.active_scheme() {
if let Some(scheme) = self.get_scheme(name) {
return self.set_colors(name.to_string(), scheme.into());
}
}
Action::empty()
}
fn set_theme(&mut self, _theme: &str) -> Action {
Action::empty()
}
}
#[cfg(not(feature = "serde"))]
pub trait ThemeConfig: Clone + std::fmt::Debug + 'static {
fn apply_startup(&self);
fn raster(&self) -> &RasterConfig;
}
#[cfg(feature = "serde")]
pub trait ThemeConfig:
Clone + std::fmt::Debug + 'static + for<'a> serde::Deserialize<'a> + serde::Serialize
{
fn is_dirty(&self) -> bool;
fn apply_startup(&self);
fn raster(&self) -> &RasterConfig;
}
#[autoimpl(for<T: trait + ?Sized> Box<T>)]
pub trait Theme<DS: DrawSharedImpl>: ThemeControl {
type Config: ThemeConfig;
type Window: Window;
type Draw<'a>: ThemeDraw
where
DS: 'a,
Self: 'a;
fn config(&self) -> std::borrow::Cow<Self::Config>;
fn apply_config(&mut self, config: &Self::Config) -> Action;
fn init(&mut self, shared: &mut SharedState<DS>);
fn new_window(&self, dpi_factor: f32) -> Self::Window;
fn update_window(&self, window: &mut Self::Window, dpi_factor: f32);
fn draw<'a>(
&'a self,
draw: DrawIface<'a, DS>,
ev: &'a mut EventState,
window: &'a mut Self::Window,
) -> Self::Draw<'a>;
fn draw_upcast<'a>(
draw: DrawIface<'a, DS>,
ev: &'a mut EventState,
w: &'a mut Self::Window,
cols: &'a ColorsLinear,
) -> Self::Draw<'a>;
fn clear_color(&self) -> color::Rgba;
}
#[autoimpl(for<T: trait + ?Sized> Box<T>)]
pub trait Window: 'static {
fn size(&self) -> &dyn ThemeSize;
fn as_any_mut(&mut self) -> &mut dyn Any;
}