Skip to main content

kas_core/theme/
theme_dst.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Stack-DST versions of theme traits
7
8use std::cell::RefCell;
9
10use super::{Theme, Window};
11use crate::config::{Config, WindowConfig};
12use crate::draw::{DrawIface, DrawSharedImpl, color};
13use crate::event::EventState;
14use crate::theme::ThemeDraw;
15
16/// As [`Theme`], but without associated types
17///
18/// This trait is implemented automatically for all implementations of
19/// [`Theme`]. It is intended only for use where a less parameterised
20/// trait is required.
21pub trait ThemeDst<DS: DrawSharedImpl> {
22    /// Theme initialisation
23    ///
24    /// See also [`Theme::init`].
25    fn init(&mut self, config: &RefCell<Config>);
26
27    /// Construct per-window storage
28    ///
29    /// See also [`Theme::new_window`].
30    fn new_window(&mut self, config: &WindowConfig) -> Box<dyn Window>;
31
32    /// Update a window created by [`Theme::new_window`]
33    ///
34    /// See also [`Theme::update_window`].
35    ///
36    /// Returns `true` when a resize is required based on changes to the scale factor or font size.
37    fn update_window(&mut self, window: &mut dyn Window, config: &WindowConfig) -> bool;
38
39    fn draw<'a>(
40        &'a self,
41        draw: DrawIface<'a, DS>,
42        ev: &'a mut EventState,
43        window: &'a mut dyn Window,
44    ) -> Box<dyn ThemeDraw + 'a>;
45
46    /// Background colour
47    ///
48    /// See also [`Theme::clear_color`].
49    fn clear_color(&self) -> color::Rgba;
50}
51
52impl<DS: DrawSharedImpl, T: Theme<DS>> ThemeDst<DS> for T {
53    fn init(&mut self, config: &RefCell<Config>) {
54        self.init(config);
55    }
56
57    fn new_window(&mut self, config: &WindowConfig) -> Box<dyn Window> {
58        let window = <T as Theme<DS>>::new_window(self, config);
59        Box::new(window)
60    }
61
62    fn update_window(&mut self, window: &mut dyn Window, config: &WindowConfig) -> bool {
63        let window = window.as_any_mut().downcast_mut().unwrap();
64        self.update_window(window, config)
65    }
66
67    fn draw<'b>(
68        &'b self,
69        draw: DrawIface<'b, DS>,
70        ev: &'b mut EventState,
71        window: &'b mut dyn Window,
72    ) -> Box<dyn ThemeDraw + 'b> {
73        let window = window.as_any_mut().downcast_mut().unwrap();
74        Box::new(<T as Theme<DS>>::draw(self, draw, ev, window))
75    }
76
77    fn clear_color(&self) -> color::Rgba {
78        self.clear_color()
79    }
80}