1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashMap;
use std::marker::Unsize;
use crate::{StackDst, Theme, ThemeDst, WindowDst};
use kas::draw::{Colour, DrawHandle};
use kas::geom::Rect;
use kas::{ThemeAction, ThemeApi};
pub struct MultiTheme<Draw> {
names: HashMap<String, usize>,
themes: Vec<StackDst<dyn ThemeDst<Draw>>>,
active: usize,
}
pub struct MultiThemeBuilder<Draw> {
names: HashMap<String, usize>,
themes: Vec<StackDst<dyn ThemeDst<Draw>>>,
}
impl<Draw> MultiTheme<Draw> {
pub fn builder() -> MultiThemeBuilder<Draw> {
MultiThemeBuilder {
names: HashMap::new(),
themes: vec![],
}
}
}
impl<Draw> MultiThemeBuilder<Draw> {
pub fn add<S: ToString, U>(mut self, name: S, theme: U) -> Self
where
U: Unsize<dyn ThemeDst<Draw>>,
Box<U>: Unsize<dyn ThemeDst<Draw>>,
{
let index = self.themes.len();
self.names.insert(name.to_string(), index);
self.themes.push(StackDst::new_or_boxed(theme));
self
}
pub fn try_build(self) -> Result<MultiTheme<Draw>, ()> {
if self.themes.len() == 0 {
return Err(());
}
Ok(MultiTheme {
names: self.names,
themes: self.themes,
active: 0,
})
}
pub fn build(self) -> MultiTheme<Draw> {
self.try_build()
.unwrap_or_else(|_| panic!("MultiThemeBuilder: no themes added"))
}
}
impl<Draw: 'static> Theme<Draw> for MultiTheme<Draw> {
type Window = StackDst<dyn WindowDst<Draw>>;
#[cfg(not(feature = "gat"))]
type DrawHandle = StackDst<dyn DrawHandle>;
#[cfg(feature = "gat")]
type DrawHandle<'a> = StackDst<dyn DrawHandle + 'a>;
fn init(&mut self, draw: &mut Draw) {
for theme in &mut self.themes {
theme.init(draw);
}
}
fn new_window(&self, draw: &mut Draw, dpi_factor: f32) -> Self::Window {
self.themes[self.active].new_window(draw, dpi_factor)
}
fn update_window(&self, window: &mut Self::Window, dpi_factor: f32) {
self.themes[self.active].update_window(window, dpi_factor);
}
#[cfg(not(feature = "gat"))]
unsafe fn draw_handle<'a>(
&'a self,
draw: &'a mut Draw,
window: &'a mut Self::Window,
rect: Rect,
) -> StackDst<dyn DrawHandle> {
self.themes[self.active].draw_handle(draw, window, rect)
}
#[cfg(feature = "gat")]
fn draw_handle<'a>(
&'a self,
draw: &'a mut Draw,
window: &'a mut Self::Window,
rect: Rect,
) -> StackDst<dyn DrawHandle + 'a> {
self.themes[self.active].draw_handle(draw, window, rect)
}
fn clear_colour(&self) -> Colour {
self.themes[self.active].clear_colour()
}
}
impl<Draw> ThemeApi for MultiTheme<Draw> {
fn set_font_size(&mut self, size: f32) -> ThemeAction {
let mut action = ThemeAction::None;
for theme in &mut self.themes {
action = action.max(theme.set_font_size(size));
}
action
}
fn set_colours(&mut self, scheme: &str) -> ThemeAction {
let mut action = ThemeAction::None;
for theme in &mut self.themes {
action = action.max(theme.set_colours(scheme));
}
action
}
fn set_theme(&mut self, theme: &str) -> ThemeAction {
if let Some(index) = self.names.get(theme).cloned() {
if index != self.active {
self.active = index;
return ThemeAction::ThemeResize;
}
}
ThemeAction::None
}
}