Repository navigation
Expand file tree
/
Copy pathcontext.rs
More file actions
204 lines (187 loc) · 6.54 KB
/
Copy pathcontext.rs
File metadata and controls
204 lines (187 loc) · 6.54 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Reading the theme last applied to an egui context.
use egui::{Context, Ui};
use super::Theme;
/// Read the theme last passed to [`apply_to_egui`](super::apply_to_egui).
///
/// Widgets call it once per frame and read the colours egui does not model from it;
/// the rest come from `ui.visuals()`, which `apply_to_egui` sets from the same theme.
///
/// ```
/// use egui_charts::theme::{Theme, ThemeExt, apply_to_egui};
///
/// # egui::__run_test_ui(|ui| {
/// apply_to_egui(ui.ctx(), &Theme::light());
/// let theme = ui.active_theme();
/// assert_eq!(theme, Theme::light());
/// let _on_accent = theme.ui.accent_text;
/// # });
/// ```
pub trait ThemeExt {
/// The theme last applied, or [`Theme::dark`] if none has been.
fn active_theme(&self) -> Theme;
}
impl ThemeExt for Context {
fn active_theme(&self) -> Theme {
applied_theme(self).unwrap_or_default()
}
}
impl ThemeExt for Ui {
fn active_theme(&self) -> Theme {
self.ctx().active_theme()
}
}
fn active_theme_id() -> egui::Id {
egui::Id::NULL.with("active_theme")
}
/// The theme last applied to `ctx`, or `None` if none has been.
pub(crate) fn applied_theme(ctx: &Context) -> Option<Theme> {
ctx.data(|d| d.get_temp::<Theme>(active_theme_id()))
}
/// Store the theme [`ThemeExt::active_theme`] returns.
pub(crate) fn set_active_theme(ctx: &Context, theme: Theme) {
ctx.data_mut(|d| d.insert_temp(active_theme_id(), theme));
}
/// Restores the active palette after painting one chart, including during unwinding.
pub(crate) struct ThemeScope {
ctx: Context,
previous: Option<Theme>,
}
impl ThemeScope {
pub(crate) fn new(ctx: &Context, theme: &Theme) -> Self {
let previous = applied_theme(ctx);
set_active_theme(ctx, theme.clone());
Self {
ctx: ctx.clone(),
previous,
}
}
}
impl Drop for ThemeScope {
fn drop(&mut self) {
if let Some(theme) = self.previous.take() {
set_active_theme(&self.ctx, theme);
} else {
self.ctx
.data_mut(|data| data.remove::<Theme>(active_theme_id()));
}
}
}
/// A component config derived from a theme, whose fields can follow a new theme one by one.
#[cfg(feature = "ui")]
pub(crate) trait ThemeDerived {
/// Move every field still equal to its value in `old` to its value in `new`,
/// keeping the fields the caller has changed.
fn follow(&mut self, old: &Self, new: &Self);
}
/// Implement [`ThemeDerived`] for a config struct, listing all of its fields.
#[cfg(feature = "ui")]
macro_rules! theme_derived {
($config:ty { $($field:ident),+ $(,)? }) => {
impl $crate::theme::ThemeDerived for $config {
fn follow(&mut self, old: &Self, new: &Self) {
// Exhaustive, so a field added to the struct must be listed here.
let Self { $($field),+ } = self;
$(if *$field == old.$field {
*$field = new.$field.clone();
})+
}
}
};
}
#[cfg(feature = "ui")]
pub(crate) use theme_derived;
/// Rederive a theme-derived component config when the active theme changes.
///
/// `derived` holds the theme and config that `config` was last derived from, or
/// `None` for a config the caller supplied, which is never replaced. Fields the
/// caller has changed since are kept; the others follow the new theme.
#[cfg(feature = "ui")]
pub(crate) fn follow_active_theme<C: ThemeDerived>(
ctx: &Context,
derived: &mut Option<(Theme, C)>,
config: &mut C,
from_theme: impl FnOnce(&Theme) -> C,
) {
if let Some(theme) = applied_theme(ctx)
&& let Some((derived_from, old)) = derived
&& *derived_from != theme
{
let new = from_theme(&theme);
config.follow(old, &new);
*derived_from = theme;
*old = new;
}
}
#[cfg(all(test, feature = "ui"))]
mod tests {
use super::*;
use crate::theme::apply_to_egui;
use crate::ui::timeframe_toolbar::{TimeframeToolbar, TimeframeToolbarConfig};
use egui::Color32;
#[test]
fn theme_derived_config_follows_the_active_theme_and_keeps_caller_edits() {
let ctx = Context::default();
let mut themed = TimeframeToolbar::new();
themed.config.panel_height = 40.0;
themed.config.text_color = Color32::RED;
let mut custom =
TimeframeToolbar::with_config(TimeframeToolbarConfig::from_theme(&Theme::dark()));
// Light differs from the Dark config the toolbar is built with; Dark is a later change,
// and an edited Dark theme must propagate too.
let mut edited_dark = Theme::dark();
edited_dark.ui.border = Color32::GREEN;
edited_dark.text.body = 17.0;
edited_dark.text.bottom_bar = 19.0;
edited_dark.spacing.md = 9.0;
edited_dark.radius.sm = 7.0;
for (name, theme) in [
("light", Theme::light()),
("dark", Theme::dark()),
("edited dark", edited_dark),
] {
ctx.run_ui(Default::default(), |ui| {
apply_to_egui(ui.ctx(), &theme);
egui::CentralPanel::default().show(ui, |ui| {
themed.show(ui);
custom.show(ui);
});
})
.drop_without_applying_deltas();
assert_eq!(
themed.config.panel_height, 40.0,
"{name}: edited height replaced"
);
assert_eq!(
themed.config.text_color,
Color32::RED,
"{name}: edited colour replaced"
);
assert_eq!(
themed.config.border_color, theme.ui.border,
"{name}: untouched colour stale"
);
assert_eq!(
themed.config.font_size, theme.text.bottom_bar,
"{name}: font stale"
);
assert_eq!(
themed.config.btn_padding_h, theme.spacing.md,
"{name}: spacing stale"
);
assert_eq!(
ctx.global_style().text_styles[&egui::TextStyle::Body].size,
theme.text.body
);
assert_eq!(
ctx.global_style().visuals.widgets.inactive.corner_radius,
egui::CornerRadius::from(theme.radius.sm)
);
assert_eq!(custom.config.font_size, Theme::dark().text.bottom_bar);
assert_eq!(
custom.config.border_color,
Theme::dark().ui.border,
"{name}: with_config replaced"
);
}
}
}