-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.rs
408 lines (360 loc) · 11.5 KB
/
theme.rs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
pub mod dynamic_style;
pub mod dynamic_style_attribute;
pub mod icons;
pub mod pseudo_state;
pub mod style_animation;
pub mod theme_colors;
pub mod theme_data;
pub mod theme_spacing;
pub mod typography;
use std::{
any::{type_name, TypeId},
collections::HashSet,
marker::PhantomData,
};
use bevy::{prelude::*, ui::UiSystem};
use dynamic_style::{DynamicStyle, DynamicStylePlugin};
use pseudo_state::{AutoPseudoStatePlugin, PseudoState, PseudoStates};
use theme_colors::{loader::ThemeColorsLoader, ThemeColors};
use theme_data::ThemeData;
use crate::{prelude::UiBuilder, ui_commands::RefreshThemeExt, ui_style::builder::StyleBuilder};
pub mod prelude {
pub use super::{
dynamic_style::{
ContextStyleAttribute, DynamicStyle, DynamicStyleEnterState, DynamicStylePostUpdate,
},
dynamic_style_attribute::{DynamicStyleAttribute, DynamicStyleController},
icons::IconData,
pseudo_state::{
FlexDirectionToPseudoState, HierarchyToPseudoState, PseudoState, PseudoStates,
VisibilityToPseudoState,
},
style_animation::{
AnimationLoop, AnimationSettings, AnimationState, InteractionStyle,
LoopedAnimationConfig,
},
theme_colors::{Accent, Container, OnColor, Surface},
theme_data::{Contrast, Scheme, ThemeData},
typography::{FontScale, FontStyle, FontType, SizedFont},
ComponentThemePlugin, CustomThemeUpdate, DefaultTheme, DynamicStyleBuilder, PseudoTheme,
Theme, ThemeUpdate, UiContext,
};
}
pub struct ThemePlugin;
impl Plugin for ThemePlugin {
fn build(&self, app: &mut App) {
app.configure_sets(
PostUpdate,
(ThemeUpdate, CustomThemeUpdate.after(ThemeUpdate)).before(UiSystem::Layout),
)
.init_resource::<ThemeData>()
.init_resource::<ThemeRegistry>()
.init_asset::<ThemeColors>()
.init_asset_loader::<ThemeColorsLoader>()
.add_plugins((AutoPseudoStatePlugin, DynamicStylePlugin));
}
}
#[derive(SystemSet, Clone, Eq, Debug, Hash, PartialEq)]
pub struct ThemeUpdate;
#[derive(SystemSet, Clone, Eq, Debug, Hash, PartialEq)]
pub struct CustomThemeUpdate;
#[derive(Clone, Debug)]
pub enum DynamicStyleBuilder<C> {
Static(DynamicStyle),
StyleBuilder(fn(&mut StyleBuilder, &ThemeData)),
ContextStyleBuilder(fn(&mut StyleBuilder, &C, &ThemeData)),
WorldStyleBuilder(fn(&mut StyleBuilder, Entity, &C, &World)),
InfoWorldStyleBuilder(
fn(&mut StyleBuilder, Option<Entity>, &Option<Vec<PseudoState>>, Entity, &C, &World),
),
}
impl<C> From<StyleBuilder> for DynamicStyleBuilder<C> {
fn from(value: StyleBuilder) -> Self {
Self::Static(value.into())
}
}
impl<C> From<DynamicStyle> for DynamicStyleBuilder<C> {
fn from(value: DynamicStyle) -> Self {
Self::Static(value)
}
}
#[derive(Clone, Debug)]
pub struct PseudoTheme<C> {
state: Option<Vec<PseudoState>>,
builder: DynamicStyleBuilder<C>,
}
impl<C> PseudoTheme<C> {
pub fn new(
state: impl Into<Option<Vec<PseudoState>>>,
theme: impl Into<DynamicStyleBuilder<C>>,
) -> Self {
Self {
state: state.into(),
builder: theme.into(),
}
}
pub fn state(&self) -> &Option<Vec<PseudoState>> {
&self.state
}
pub fn builder(&self) -> &DynamicStyleBuilder<C> {
&self.builder
}
pub fn build(
state: impl Into<Option<Vec<PseudoState>>>,
builder: fn(&mut StyleBuilder),
) -> Self {
let mut style_builder = StyleBuilder::new();
builder(&mut style_builder);
Self {
state: state.into(),
builder: style_builder.into(),
}
}
pub fn deferred(
state: impl Into<Option<Vec<PseudoState>>>,
builder: fn(&mut StyleBuilder, &ThemeData),
) -> Self {
Self {
state: state.into(),
builder: DynamicStyleBuilder::StyleBuilder(builder),
}
}
pub fn deferred_context(
state: impl Into<Option<Vec<PseudoState>>>,
builder: fn(&mut StyleBuilder, &C, &ThemeData),
) -> Self {
Self {
state: state.into(),
builder: DynamicStyleBuilder::ContextStyleBuilder(builder),
}
}
pub fn deferred_world(
state: impl Into<Option<Vec<PseudoState>>>,
builder: fn(&mut StyleBuilder, Entity, &C, &World),
) -> Self {
Self {
state: state.into(),
builder: DynamicStyleBuilder::WorldStyleBuilder(builder),
}
}
pub fn deferred_info_world(
state: impl Into<Option<Vec<PseudoState>>>,
builder: fn(
&mut StyleBuilder,
Option<Entity>,
&Option<Vec<PseudoState>>,
Entity,
&C,
&World,
),
) -> Self {
Self {
state: state.into(),
builder: DynamicStyleBuilder::InfoWorldStyleBuilder(builder),
}
}
pub fn is_base_theme(&self) -> bool {
match &self.state {
Some(list) => list.is_empty(),
None => true,
}
}
pub fn count_match(&self, node_states: &Vec<PseudoState>) -> usize {
match &self.state {
// Only consider pseudo themes that are specific to an inclusive substet of the themed
// element's pseudo states. A theme for [Checked, Disabled] will apply to
// elements with [Checked, Disabled, FirstChild], but will not apply to
// elements with [Checked] (because the theme targets more specific elements)
// or [Checked, FirstChild] (because they are disjoint)
Some(targeted_states) => match targeted_states
.iter()
.all(|state| node_states.contains(state))
{
true => targeted_states.len(),
false => 0,
},
None => 0,
}
}
}
pub trait UiContext {
fn get(&self, _target: &str) -> Result<Entity, String> {
Err(format!(
"{} has no UI contexts",
std::any::type_name::<Self>()
))
}
/// These are the contexts cleared by the parent theme when no DynamicStyle
/// is placed to them.
///
/// By default this is the full list of contexts.
///
/// Warning: If a context is a sub-widget with its own theme, it should not
/// be included in the cleared contexts, nor should it be used for placement
/// from the main entity. The behavior is undefined.
fn cleared_contexts(&self) -> impl Iterator<Item = &str> + '_ {
self.contexts()
}
fn contexts(&self) -> impl Iterator<Item = &str> + '_ {
[].into_iter()
}
}
pub trait DefaultTheme: Sized + Component + UiContext {
fn default_theme() -> Option<Theme<Self>> {
None
}
}
#[derive(Component, Debug)]
pub struct Theme<C>
where
C: DefaultTheme,
{
context: PhantomData<C>,
pseudo_themes: Vec<PseudoTheme<C>>,
}
impl<C> Theme<C>
where
C: DefaultTheme,
{
pub fn new(pseudo_themes: impl Into<Vec<PseudoTheme<C>>>) -> Self {
Self {
context: PhantomData,
pseudo_themes: pseudo_themes.into(),
}
}
pub fn pseudo_themes(&self) -> &Vec<PseudoTheme<C>> {
&self.pseudo_themes
}
pub fn post_update() -> impl IntoSystemConfigs<()> {
Theme::<C>::post_update_in(ThemeUpdate)
}
pub fn custom_post_update() -> impl IntoSystemConfigs<()> {
Theme::<C>::post_update_in(CustomThemeUpdate)
}
pub fn post_update_in(set: impl SystemSet) -> impl IntoSystemConfigs<()> {
(
Theme::<C>::process_theme_update,
Theme::<C>::process_updated_pseudo_states,
)
.in_set(set)
}
fn process_theme_update(
q_targets: Query<Entity, With<C>>,
q_added_targets: Query<Entity, Added<C>>,
q_removed_themes: RemovedComponents<Theme<C>>,
q_changed_themes: Query<Entity, Changed<Theme<C>>>,
theme_data: Res<ThemeData>,
mut commands: Commands,
) {
if theme_data.is_changed()
|| q_removed_themes.len() > 0
|| q_changed_themes.iter().count() > 0
{
for entity in &q_targets {
commands.entity(entity).refresh_theme::<C>();
}
} else {
for entity in &q_added_targets {
commands.entity(entity).refresh_theme::<C>();
}
}
}
fn process_updated_pseudo_states(
q_targets: Query<Entity, With<C>>,
q_changed_targets: Query<Entity, (With<C>, Changed<PseudoStates>)>,
mut q_removed_targets: RemovedComponents<PseudoStates>,
mut commands: Commands,
) {
for entity in &q_changed_targets {
commands.entity(entity).refresh_theme::<C>();
}
for entity in q_removed_targets.read() {
if q_targets.contains(entity) {
commands.entity(entity).refresh_theme::<C>();
}
}
}
}
pub trait InsertThemedComponentExt {
/// Inserts `C` as a component to the entity and checks if
/// [`ComponentThemePlugin<C>`](ComponentThemePlugin) was added to the app.
fn insert_themed_component<C: DefaultTheme + Component>(&mut self, component: C) -> &mut Self;
}
impl InsertThemedComponentExt for UiBuilder<'_, Entity> {
fn insert_themed_component<C: DefaultTheme + Component>(&mut self, component: C) -> &mut Self {
self.insert(component);
self.commands().add(|world: &mut World| {
if !world.resource::<ThemeRegistry>().contains_by_id(TypeId::of::<C>()) {
warn!("themed component {} was not registered; add its ComponentThemePlugin to your app", type_name::<C>());
}
});
self
}
}
/// Tracks all the themes that have been registered with [`ComponentThemePlugin`].
///
/// This can be used to check if a theme's plugin is missing.
#[derive(Resource, Default, Debug)]
pub struct ThemeRegistry {
themes: HashSet<TypeId>,
}
impl ThemeRegistry {
fn new_with<C: 'static>() -> Self {
let mut registry = Self::default();
registry.add::<C>();
registry
}
fn add<C: 'static>(&mut self) {
self.themes.insert(TypeId::of::<C>());
}
/// Returns `true` if the theme `C` has been registered.
pub fn contains<C: 'static>(&self) -> bool {
self.contains_by_id(TypeId::of::<C>())
}
/// Returns `true` if the theme `id` has been registered.
pub fn contains_by_id(&self, id: TypeId) -> bool {
self.themes.contains(&id)
}
}
#[derive(Default)]
pub struct ComponentThemePlugin<C>
where
C: DefaultTheme,
{
context: PhantomData<C>,
is_custom: bool,
}
impl<C> ComponentThemePlugin<C>
where
C: DefaultTheme,
{
pub fn new() -> Self {
Self {
context: PhantomData,
is_custom: false,
}
}
/// Adds the theme update systems to the `CustomThemeUpdate` system set
pub fn custom() -> Self {
Self {
context: PhantomData,
is_custom: true,
}
}
}
impl<C> Plugin for ComponentThemePlugin<C>
where
C: DefaultTheme,
{
fn build(&self, app: &mut App) {
if let Some(mut registry) = app.world_mut().get_resource_mut::<ThemeRegistry>() {
registry.add::<C>();
} else {
app.insert_resource(ThemeRegistry::new_with::<C>());
}
match self.is_custom {
true => app.add_systems(PostUpdate, Theme::<C>::custom_post_update()),
false => app.add_systems(PostUpdate, Theme::<C>::post_update()),
};
}
}