orbtk-core 0.3.1-alpha4

Core crate that provides base api and elements for OrbTk like widgets basis.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use std::any::type_name;

use dces::prelude::*;

use crate::{event::ChangedEvent, event::*, theming::*, tree::*, utils::prelude::*};

/// Mark the widget and shared widgets as dirty.
pub fn mark_as_dirty(key: &str, entity: Entity, ecm: &mut EntityComponentManager<Tree>) {
    let root = ecm.entity_store().root();

    for entity in ecm.component_store().entities_of_component(key, entity) {
        *ecm.component_store_mut()
            .get_mut::<bool>("dirty", entity)
            .unwrap() = true;

        if let Ok(dirty_widgets) = ecm
            .component_store_mut()
            .get_mut::<Vec<Entity>>("dirty_widgets", root)
        {
            // don't add the same widget twice in a row
            if dirty_widgets.is_empty() || *dirty_widgets.last().unwrap() != entity {
                dirty_widgets.push(entity);
            }
        }
    }
}

/// Mark the widget dirty.
pub fn mark_as_dirty_self(entity: Entity, ecm: &mut EntityComponentManager<Tree>) {
    let root = ecm.entity_store().root();

    *ecm.component_store_mut()
        .get_mut::<bool>("dirty", entity)
        .unwrap() = true;

    if let Ok(dirty_widgets) = ecm
        .component_store_mut()
        .get_mut::<Vec<Entity>>("dirty_widgets", root)
    {
        // don't add the same widget twice in a row
        if dirty_widgets.is_empty() || *dirty_widgets.last().unwrap() != entity {
            dirty_widgets.push(entity);
        }
    }
}

/// The `WidgetContainer` wraps the entity of a widget and provides access to its properties, its children properties and its parent properties.
pub struct WidgetContainer<'a> {
    ecm: &'a mut EntityComponentManager<Tree>,
    current_node: Entity,
    theme: &'a Theme,
    event_adapter: Option<&'a EventAdapter>,
}

impl<'a> WidgetContainer<'a> {
    /// Creates a new widget container for the given `entity`.
    pub fn new(
        root: Entity,
        ecm: &'a mut EntityComponentManager<Tree>,
        theme: &'a Theme,
        event_adapter: Option<&'a EventAdapter>,
    ) -> Self {
        WidgetContainer {
            ecm,
            current_node: root,
            theme,
            event_adapter,
        }
    }

    fn mark_as_dirty(&mut self, key: &str, entity: Entity) {
        mark_as_dirty(key, entity, self.ecm);
    }

    fn mark_as_dirty_self(&mut self, entity: Entity) {
        mark_as_dirty_self(entity, self.ecm);
    }

    /// Gets the entity of the widget.
    pub fn entity(&self) -> Entity {
        self.current_node
    }

    /// Remove the dirty flag from the current widget.
    pub fn clear_dirty(&mut self) {
        let root = self.ecm.entity_store().root();
        *self
            .ecm
            .component_store_mut()
            .get_mut::<bool>("dirty", self.current_node)
            .unwrap() = false;

        let index = self
            .ecm
            .component_store()
            .get::<Vec<Entity>>("dirty_widgets", root)
            .unwrap()
            .iter()
            .position(|&r| r == self.current_node)
            .unwrap();

        if let Ok(dirty_widgets) = self
            .ecm
            .component_store_mut()
            .get_mut::<Vec<Entity>>("dirty_widgets", root)
        {
            dirty_widgets.remove(index);
        }
    }

    /// Check if the given type is the type of the property. If the property is not part of the widget `None` will be returned.
    pub fn is<P: 'static>(&self, key: &str) -> bool {
        if let Ok(is_type) = self.ecm.component_store().is::<P>(key, self.current_node) {
            return is_type;
        }

        false
    }

    /// Gets the property.
    ///
    /// # Panics
    ///
    /// Panics if the widget does not contains the property.
    pub fn get<P>(&self, key: &str) -> &P
    where
        P: Clone + Component,
    {
        if let Ok(property) = self.ecm.component_store().get::<P>(key, self.current_node) {
            return property;
        }

        let name = self.get_name();

        panic!(
            "Widget: {} with entity: {} does not contain property with type {:?} for key: {}",
            name,
            self.current_node.0,
            type_name::<P>(),
            key
        );
    }

    /// Gets a mutable reference of the property of type `P`.
    ///
    /// # Panics
    ///
    /// Panics if the widget does not contain the property.
    pub fn get_mut<P>(&mut self, key: &str) -> &mut P
    where
        P: Clone + Component,
    {
        self.mark_as_dirty(key, self.current_node);

        if let Ok(property) = self
            .ecm
            .component_store_mut()
            .get_mut::<P>(key, self.current_node)
        {
            return property;
        }

        panic!(
            "Entity {} does not contain property type {:?}, with key: {}",
            self.current_node.0,
            type_name::<P>(),
            key
        );
    }

    /// Clones the property. If the property does not exists for the widget the
    /// default of the property value will be returned,
    pub fn clone_or_default<P>(&self, key: &str) -> P
    where
        P: Clone + Component + Default,
    {
        if let Ok(property) = self.ecm.component_store().get::<P>(key, self.current_node) {
            return property.clone();
        }

        P::default()
    }

    /// Clones the property.
    ///
    /// # Panics
    ///
    /// Panics if the widget does not contains the property.
    pub fn clone<P>(&self, key: &str) -> P
    where
        P: Clone + Component,
    {
        if let Ok(property) = self.ecm.component_store().get::<P>(key, self.current_node) {
            return property.clone();
        }

        let name = self.get_name();

        panic!(
            "Widget: {} with entity: {} does not contain property with type {:?} for key: {}",
            name,
            self.current_node.0,
            type_name::<P>(),
            key
        );
    }

    /// Clones the property of type `P` from the given widget entity. If the entity does
    /// not exists or it doesn't have a component of type `P` `None` will be returned.
    pub fn try_clone<P>(&self, key: &str) -> Option<P>
    where
        P: Clone + Component,
    {
        if let Ok(property) = self.ecm.component_store().get::<P>(key, self.current_node) {
            return Some(property.clone());
        }

        None
    }

    fn toggle_enabled_state(&mut self) {
        if *self.get::<bool>("enabled") && self.get::<Selector>("selector").has_state("disabled") {
            self.get_mut::<Selector>("selector")
                .remove_state("disabled");
            self.update(false);
        } else if !*self.get::<bool>("enabled")
            && !self.get::<Selector>("selector").has_state("disabled")
        {
            self.get_mut::<Selector>("selector").push_state("disabled");
            self.update(false);
        }
    }

    /// Sets the property of type `P`. Sets the `dirty` flag of the widget to `true`.
    ///
    /// # Panics
    ///
    /// Panics if the widget does not contains the property.
    pub fn set<P>(&mut self, key: &str, value: P)
    where
        P: Component + Clone + PartialEq,
    {
        if self
            .ecm
            .component_store()
            .get::<P>(key, self.current_node)
            .unwrap()
            == &value
        {
            return;
        }

        let mut on_changed = false;

        let mut source = self.current_node;
        let mut source_key = key.to_string();

        if let Ok((src, key)) = self.ecm.component_store().source(self.current_node, key) {
            source = src;
            source_key = key;
        }

        for entity in self
            .ecm
            .component_store()
            .entities_of_component(key, self.current_node)
        {
            let mut target_key = source_key.clone();

            if entity != source {
                let result = self
                    .ecm
                    .component_store()
                    .target_key(source, entity, source_key.as_str())
                    .expect("WidgetContainer::set: dces error could not find shared entity.");

                target_key = result;
            }

            self.mark_as_dirty_self(entity);

            // each widget has this filter therefore unwrap.
            match self
                .ecm
                .component_store()
                .get::<Filter>("on_changed_filter", entity)
                .unwrap()
            {
                // nothing to do, every key is inactive.
                Filter::Complete => {}
                Filter::Nothing => on_changed = true,
                Filter::List(list) => {
                    if list.contains(&target_key) {
                        on_changed = true;
                    }
                }
            }

            if on_changed {
                if let Some(event_adapter) = &self.event_adapter {
                    event_adapter
                        .push_event_direct(entity, ChangedEvent(self.current_node, target_key));
                }
            }
        }

        self.set_non_dirty(key, value);

        if key.eq("enabled") {
            self.toggle_enabled_state();
        }
    }

    /// Sets the property of type `P` without setting the widget dirty.
    ///
    /// # Panics
    ///
    /// Panics if the widget does not contains the property.
    pub fn set_non_dirty<P>(&mut self, key: &str, value: P)
    where
        P: Component + Clone,
    {
        if let Ok(property) = self
            .ecm
            .component_store_mut()
            .get_mut::<P>(key, self.current_node)
        {
            *property = value;
            return;
        }

        let name = self.get_name();

        panic!(
            "Widget: {} with entity: {} does not contain property with type {:?} for key: {}",
            name,
            self.current_node.0,
            type_name::<P>(),
            key
        );
    }

    /// Returns `true` if the widget has a property of type `P` otherwise `false`.
    pub fn has<P>(&self, key: &str) -> bool
    where
        P: Clone + Component,
    {
        self.ecm
            .component_store()
            .get::<P>(key, self.current_node)
            .is_ok()
    }

    /// Returns a reference of a property of type `P` from the given widget entity. If the entity does
    /// not exists or it doesn't have a component of type `P` `None` will be returned.
    pub fn try_get<P: Component>(&self, key: &str) -> Option<&P> {
        self.ecm
            .component_store()
            .get::<P>(key, self.current_node)
            .ok()
    }

    /// Returns a mutable reference of a property of type `P` from the given widget entity. If the entity does
    /// not exists or it doesn't have a component of type `P` `None` will be returned.
    pub fn try_get_mut<P: Component>(&mut self, key: &str) -> Option<&mut P> {
        mark_as_dirty(key, self.current_node, self.ecm);
        self.ecm
            .component_store_mut()
            .get_mut::<P>(key, self.current_node)
            .ok()
    }

    /// Checks if the given value is equal to the given property.
    pub fn eq<P: Component + PartialEq>(&self, key: &str, other: &P) -> bool {
        if let Some(value) = self.try_get::<P>(key) {
            return value.eq(other);
        }

        false
    }

    fn update_constraint(&mut self, key: &str, value: Value) {
        let value = if let Ok(value) = value.0.into_rust::<f64>() {
            value
        } else {
            0.0
        };

        if let Ok(constraint) = self
            .ecm
            .component_store_mut()
            .get_mut::<Constraint>("constraint", self.current_node)
        {
            match key {
                "width" => constraint.set_width(value),
                "height" => constraint.set_height(value),
                "min_width" => constraint.set_min_width(value),
                "min_height" => constraint.set_min_height(value),
                "max_width" => constraint.set_max_width(value),
                "max_height" => constraint.set_max_height(value),
                _ => {}
            }
        }
    }

    fn update_padding(&mut self, key: &str, value: Value) {
        let value = if let Ok(value) = value.0.into_rust::<f64>() {
            value
        } else {
            0.0
        };

        if let Some(padding) = self.try_get_mut::<Thickness>("padding") {
            match key {
                "padding_left" => padding.set_left(value),
                "padding_top" => padding.set_top(value),
                "padding_right" => padding.set_right(value),
                "padding_bottom" => padding.set_bottom(value),
                _ => {}
            }
        }
    }

    fn update_value<T, V>(&mut self, key: &str, value: V)
    where
        T: Component + Clone,
        V: Into<T>,
    {
        if self.has::<T>(key) {
            *self
                .ecm
                .component_store_mut()
                .get_mut::<T>(key, self.current_node)
                .unwrap() = value.into();
        }
    }

    /// Update all properties from theme for the current widget.
    pub fn update(&mut self, force: bool) {
        self.update_widget(self.current_node, force, false);
    }

    /// Update all properties from theme for the current widget and mark the widget as dirty.
    pub fn update_dirty(&mut self, force: bool) {
        self.update_widget(self.current_node, force, true);
    }

    /// Update all properties from theme for the given widget.
    pub fn update_widget(&mut self, entity: Entity, force: bool, should_mark_as_dirty: bool) {
        self.current_node = entity;
        if !self.has::<Selector>("selector") {
            return;
        }

        if force {
            // direct access to prevent initial setting of dirty flag on widget
            self.ecm
                .component_store_mut()
                .get_mut::<Selector>("selector", self.current_node)
                .unwrap()
                .set_dirty(true);
        }

        let selector = self.clone::<Selector>("selector");

        if !selector.dirty() {
            return;
        }

        if let Some(props) = &mut self.theme.properties(&selector) {
            for (key, value) in props.drain() {
                match key.as_str() {
                    // special mapping
                    "padding_left" | "padding_top" | "padding_right" | "padding_bottom" => {
                        self.update_padding(&key, Value(value));
                    }
                    "width" | "height" | "min_width" | "min_height" | "max_width"
                    | "max_height" => self.update_constraint(&key, Value(value)),
                    _ => {
                        // common mapping
                        if self.is::<Brush>(&key) {
                            self.update_value::<Brush, Value>(&key, Value(value));
                        } else if self.is::<f32>(&key) {
                            self.update_value::<f32, Value>(&key, Value(value));
                        } else if self.is::<f64>(&key) {
                            self.update_value::<f64, Value>(&key, Value(value));
                        } else if self.is::<Thickness>(&key) {
                            self.update_value::<Thickness, Value>(&key, Value(value));
                        } else if self.is::<String>(&key) {
                            self.update_value::<String, Value>(&key, Value(value));
                        } else if self.is::<Alignment>(&key) {
                            self.update_value::<Alignment, Value>(&key, Value(value));
                        }
                    }
                }
            }
        }

        let force = selector.dirty() || force;

        for child in &(self.ecm.entity_store().children.clone())[&entity] {
            self.update_widget(*child, force, should_mark_as_dirty);
        }

        self.current_node = entity;

        // direct access to prevent initial setting of dirty flag on widget
        self.ecm
            .component_store_mut()
            .get_mut::<Selector>("selector", self.current_node)
            .unwrap()
            .set_dirty(false);

        if should_mark_as_dirty {
            mark_as_dirty("selector", self.current_node, self.ecm);
        }
    }

    fn get_name(&self) -> String {
        if self.has::<String>("name") {
            self.ecm
                .component_store()
                .get::<String>("name", self.current_node)
                .unwrap()
                .clone()
        } else {
            String::from("unknown")
        }
    }

    /// Returns the count of the children.
    pub fn children_count(&mut self) -> Option<usize> {
        if let Some(children) = self.ecm.entity_store().children.get(&self.current_node) {
            return Some(children.len());
        }

        None
    }

    /// Mutable access to the children entities of the widget.
    pub fn children_mut(&mut self) -> Option<&mut Vec<Entity>> {
        self.ecm.entity_store().children.get_mut(&self.current_node)
    }

    /// Check if the given widget (entity) is child of the current widget.
    pub fn is_child(&mut self, entity: Entity) -> bool {
        if let Some(children) = self.ecm.entity_store().children.get(&self.current_node) {
            return children.contains(&entity);
        }

        false
    }
}

impl<'a> From<&'a mut super::Context<'_>> for WidgetContainer<'a> {
    fn from(ctx: &'a mut super::Context<'_>) -> Self {
        WidgetContainer::new(
            ctx.entity(),
            ctx.ecm,
            &ctx.theme,
            Some(&ctx.provider.event_adapter),
        )
    }
}