orbtk-api 0.3.1-alpha3

API 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
use std::{any::type_name, cell::RefCell, rc::Rc};

use dces::prelude::*;

use crate::{
    event::ChangedEvent, event::*, properties::Constraint, 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, StringComponentStore>,
) {
    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);
            }
        }
    }
}

/// 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, StringComponentStore>,
    current_node: Entity,
    theme: &'a Theme,
    event_queue: Option<&'a Rc<RefCell<EventQueue>>>,
}

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

    fn mark_as_dirty(&mut self, key: &str) {
        mark_as_dirty(key, self.current_node, 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);
        }
    }

    /// 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);

        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
    }

    /// 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;
        }
        self.mark_as_dirty(key);

        let mut on_changed = false;

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

        if on_changed {
            if let Some(event_queue) = self.event_queue {
                event_queue.borrow_mut().register_event_with_strategy(
                    ChangedEvent(self.current_node, String::from(key)),
                    EventStrategy::Direct,
                    self.current_node,
                );
            }
        }

        self.set_non_dirty(key, value);
    }

    /// 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) = self.theme.properties(&selector) {
            for (key, value) in props {
                match key.as_str() {
                    "foreground" | "background" | "icon_brush" | "border_brush" => {
                        self.update_value::<Brush, Value>(key, Value(value.clone()));
                    }
                    "font_size" | "icon_size" | "spacing" | "border_radius" => {
                        self.update_value::<f64, Value>(key, Value(value.clone()));
                    }
                    "padding" | "border_width" => {
                        self.update_value::<Thickness, Value>(key, Value(value.clone()));
                    }
                    "padding_left" | "padding_top" | "padding_right" | "padding_bottom" => {
                        self.update_padding(key, Value(value.clone()));
                    }
                    "font_family" | "icon_family" => {
                        self.update_value::<String, Value>(key, Value(value.clone()));
                    }
                    "opacity" => {
                        self.update_value::<f32, Value>(key, Value(value.clone()));
                    }
                    "width" | "height" | "min_width" | "min_height" | "max_width"
                    | "max_height" => self.update_constraint(key, Value(value.clone())),
                    _ => {}
                }
            }
        }

        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")
        }
    }
}