ori_core/view/
state.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
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
use std::{
    any::Any,
    fmt::{Debug, Display},
    num::NonZero,
    sync::atomic::{AtomicU64, Ordering},
};

use crate::{
    event::Ime,
    layout::{Affine, Point, Rect, Size, Vector},
    window::Cursor,
};

bitflags::bitflags! {
    /// Flags that indicate what needs to be updated.
    #[must_use]
    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
    pub struct Update: u8 {
        /// The view needs to be laid out.
        const LAYOUT = 1 << 1;

        /// The view needs to be drawn.
        const DRAW = 1 << 2;

        /// The view needs an animation frame.
        const ANIMATE = 1 << 3;
    }
}

bitflags::bitflags! {
    /// Flags that indicate state of a view.
    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
    pub struct ViewFlags: u8 {
        /// The view is hovered.
        const HOVERED = 1 << 0;

        /// The view is focused.
        const FOCUSED = 1 << 1;

        /// The view is active.
        const ACTIVE = 1 << 2;

        /// The view has a hovered child.
        const HAS_HOVERED = 1 << 3;

        /// The view has a focused child.
        const HAS_FOCUSED = 1 << 4;

        /// The view has an active child.
        const HAS_ACTIVE = 1 << 5;

        /// The view is focusable.
        const FOCUSABLE = 1 << 6;

        /// Equivalent to `Self::HOVERED | Self::FOCUSED | Self::ACTIVE`.
        const IS = Self::HOVERED.bits() | Self::FOCUSED.bits() | Self::ACTIVE.bits();

        /// Equivalent to `Self::HAS_HOVERED | Self::HAS_FOCUSED | Self::HAS_ACTIVE`.
        const HAS = Self::HAS_HOVERED.bits() | Self::HAS_FOCUSED.bits() | Self::HAS_ACTIVE.bits();
    }
}

impl ViewFlags {
    fn has(self) -> Self {
        (self & Self::HAS) | Self::from_bits_retain((self & Self::IS).bits() << 3)
    }
}

/// An opaque unique identifier for a view.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ViewId {
    id: NonZero<u64>,
}

impl Default for ViewId {
    fn default() -> Self {
        Self::new()
    }
}

impl ViewId {
    /// Create a new [`ViewId`].
    pub fn new() -> Self {
        static NEXT_ID: AtomicU64 = AtomicU64::new(1);

        loop {
            let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);

            if let Some(id) = NonZero::new(id) {
                break Self { id };
            };
        }
    }

    /// Get the underlying id as a [`u64`].
    pub fn as_u64(&self) -> u64 {
        self.id.get()
    }
}

impl Debug for ViewId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ViewId(0x{:x})", self.id)
    }
}

impl Display for ViewId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "0x{:x}", self.id)
    }
}

/// State associated with a [`View`](super::View).
#[derive(Debug)]
pub struct ViewState {
    pub(crate) id: ViewId,

    /* flags */
    pub(crate) prev_flags: ViewFlags,
    pub(crate) flags: ViewFlags,
    pub(crate) update: Update,

    /* properties */
    pub(crate) properties: Properties,

    /* layout */
    pub(crate) size: Size,
    pub(crate) transform: Affine,

    /* cursor */
    pub(crate) cursor: Option<Cursor>,
    pub(crate) inherited_cursor: Option<Cursor>,

    /* ime */
    pub(crate) ime: Option<Ime>,
    pub(crate) inherited_ime: Option<Ime>,
}

impl Default for ViewState {
    fn default() -> Self {
        Self::new(ViewId::new())
    }
}

impl ViewState {
    /// Create a new [`ViewState`] with the given [`ViewId`].
    pub fn new(id: ViewId) -> Self {
        Self {
            id,

            /* flags */
            prev_flags: ViewFlags::default(),
            flags: ViewFlags::default(),
            update: Update::LAYOUT | Update::DRAW,

            /* properties */
            properties: Properties::new(),

            /* layout */
            size: Size::ZERO,
            transform: Affine::IDENTITY,

            /* cursor */
            cursor: None,
            inherited_cursor: None,

            /* ime */
            ime: None,
            inherited_ime: None,
        }
    }

    /// Prepare the view.
    pub fn prepare(&mut self) {
        self.flags.remove(ViewFlags::HAS);
        self.flags |= self.flags.has();

        self.inherited_cursor = self.cursor;
        self.inherited_ime = self.ime.clone();
    }

    /// Propagate the state of a child view.
    pub fn propagate(&mut self, child: &mut Self) {
        self.update |= child.update;
        self.flags |= child.flags.has();
        self.inherited_cursor = self.cursor().or(child.cursor());
        self.inherited_ime = self.ime().or(child.ime()).cloned();
    }

    /// Get the id of the view.
    pub fn id(&self) -> ViewId {
        self.id
    }

    /// Get whether the view is hovered.
    pub fn is_hovered(&self) -> bool {
        self.flags.contains(ViewFlags::HOVERED)
    }

    /// Set whether the view is hovered.
    pub fn set_hovered(&mut self, hovered: bool) {
        self.flags.set(ViewFlags::HOVERED, hovered);
    }

    /// Get whether the view is focused.
    pub fn is_focused(&self) -> bool {
        self.flags.contains(ViewFlags::FOCUSED)
    }

    /// Set whether the view is focused.
    pub fn set_focused(&mut self, focused: bool) {
        self.flags.set(ViewFlags::FOCUSED, focused);
    }

    /// Get whether the view is active.
    pub fn is_active(&self) -> bool {
        self.flags.contains(ViewFlags::ACTIVE)
    }

    /// Set whether the view is active.
    pub fn set_active(&mut self, active: bool) {
        self.flags.set(ViewFlags::ACTIVE, active);
    }

    /// Get whether the view has a hovered child.
    pub fn has_hovered(&self) -> bool {
        let flags = self.flags & (ViewFlags::HOVERED | ViewFlags::HAS_HOVERED);
        flags != ViewFlags::empty()
    }

    /// Get whether the view has a focused child.
    pub fn has_focused(&self) -> bool {
        let flags = self.flags & (ViewFlags::FOCUSED | ViewFlags::HAS_FOCUSED);
        flags != ViewFlags::empty()
    }

    /// Get whether the view has an active child.
    pub fn has_active(&self) -> bool {
        let flags = self.flags & (ViewFlags::ACTIVE | ViewFlags::HAS_ACTIVE);
        flags != ViewFlags::empty()
    }

    /// Get whether the view is focusable.
    pub fn is_focusable(&self) -> bool {
        self.flags.contains(ViewFlags::FOCUSABLE)
    }

    /// Set whether the view is focusable.
    pub fn set_focusable(&mut self, focusable: bool) {
        self.flags.set(ViewFlags::FOCUSABLE, focusable);
    }

    /// Check if the view has the property `T`.
    pub fn contains_property<T: 'static>(&self) -> bool {
        self.properties.contains::<T>()
    }

    /// Insert a property into the view.
    pub fn insert_property<T: 'static>(&mut self, item: T) {
        self.properties.insert(item);
    }

    /// Remove a property from the view.
    pub fn remove_property<T: 'static>(&mut self) -> Option<T> {
        self.properties.remove::<T>()
    }

    /// Get the property `T` of the view.
    pub fn get_property<T: 'static>(&self) -> Option<&T> {
        self.properties.get()
    }

    /// Get the property `T` of the view mutably.
    pub fn get_property_mut<T: 'static>(&mut self) -> Option<&mut T> {
        self.properties.get_mut()
    }

    /// Get the property `T` of the view or insert it with a value.
    pub fn property_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
        self.properties.get_or_insert_with(f)
    }

    /// Get the property `T` of the view or insert it with a value.
    pub fn property_or<T: 'static>(&mut self, item: T) -> &mut T {
        self.properties.get_or_insert(item)
    }

    /// Get the property `T` of the view or insert it with a default value.
    pub fn property_or_default<T: 'static + Default>(&mut self) -> &mut T {
        self.properties.get_or_default()
    }

    /// Set the size of the view.
    pub fn set_size(&mut self, size: Size) {
        self.size = size;
    }

    /// Get the size of the view.
    pub fn size(&self) -> Size {
        self.size
    }

    /// Get the rect of the view in local coordinates.
    pub fn rect(&self) -> Rect {
        Rect::min_size(Point::ZERO, self.size)
    }

    /// Get the transform of the view.
    pub fn transform(&self) -> Affine {
        self.transform
    }

    /// Set the transform of the view.
    pub fn set_transform(&mut self, transform: Affine) {
        self.transform = transform;
    }

    /// Translate the transform of the view.
    pub fn translate(&mut self, translation: Vector) {
        self.transform = Affine::translate(translation);
    }

    /// Request a layout of the view tree.
    pub fn request_layout(&mut self) {
        self.update |= Update::LAYOUT | Update::DRAW;
    }

    /// Request a draw of the view tree.
    pub fn request_draw(&mut self) {
        self.update |= Update::DRAW;
    }

    /// Request an animation frame of the view tree.
    pub fn request_animate(&mut self) {
        self.update |= Update::ANIMATE;
    }

    /// Get whether the view needs to be laid out.
    pub fn needs_layout(&self) -> bool {
        self.update.contains(Update::LAYOUT)
    }

    /// Get whether the view needs to be drawn.
    pub fn needs_draw(&self) -> bool {
        self.update.contains(Update::DRAW)
    }

    /// Get whether the view needs an animation frame.
    pub fn needs_animate(&self) -> bool {
        self.update.contains(Update::ANIMATE)
    }

    /// Mark the view as laid out.
    ///
    /// This will remove the [`Update::LAYOUT`] flag.
    pub fn mark_layed_out(&mut self) {
        self.update.remove(Update::LAYOUT);
    }

    /// Mark the view as drawn.
    ///
    /// This will remove the [`Update::DRAW`] flag.
    pub fn mark_drawn(&mut self) {
        self.update.remove(Update::DRAW);
    }

    /// Mark the view as animated.
    ///
    /// This will remove the [`Update::ANIMATE`] flag.
    pub fn mark_animated(&mut self) {
        self.update.remove(Update::ANIMATE);
    }

    /// Get the flags of the view.
    pub fn flags(&self) -> ViewFlags {
        self.flags
    }

    /// Get the [`Update`] of the view.
    pub fn update(&self) -> Update {
        self.update
    }

    /// Get the cursor of the view.
    pub fn cursor(&self) -> Option<Cursor> {
        self.cursor.or(self.inherited_cursor)
    }

    /// Set the cursor of the view.
    pub fn set_cursor(&mut self, cursor: Option<Cursor>) {
        self.cursor = cursor;
    }

    /// Get the IME of the view.
    pub fn ime(&self) -> Option<&Ime> {
        self.ime.as_ref().or(self.inherited_ime.as_ref())
    }

    /// Set the IME of the view.
    pub fn set_ime(&mut self, ime: Option<Ime>) {
        self.ime = ime;
    }
}

pub(crate) struct Properties {
    items: Vec<Box<dyn Any>>,
}

impl Properties {
    fn new() -> Self {
        Self { items: Vec::new() }
    }

    fn insert<T: 'static>(&mut self, item: T) {
        if let Some(index) = self.get_index::<T>() {
            self.items[index] = Box::new(item);
        } else {
            self.items.push(Box::new(item));
        }
    }

    fn remove<T: 'static>(&mut self) -> Option<T> {
        if let Some(index) = self.get_index::<T>() {
            Some(*self.items.remove(index).downcast().unwrap())
        } else {
            None
        }
    }

    fn contains<T: 'static>(&self) -> bool {
        self.items.iter().any(|item| item.is::<T>())
    }

    fn get<T: 'static>(&self) -> Option<&T> {
        self.items.iter().find_map(|item| item.downcast_ref())
    }

    fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
        self.items.iter_mut().find_map(|item| item.downcast_mut())
    }

    fn get_index<T: 'static>(&self) -> Option<usize> {
        self.items.iter().position(|item| item.is::<T>())
    }

    fn get_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
        if let Some(index) = self.get_index::<T>() {
            self.items[index].downcast_mut().unwrap()
        } else {
            let item = f();
            self.insert(item);
            self.items.last_mut().unwrap().downcast_mut().unwrap()
        }
    }

    fn get_or_insert<T: 'static>(&mut self, item: T) -> &mut T {
        self.get_or_insert_with(|| item)
    }

    fn get_or_default<T: 'static + Default>(&mut self) -> &mut T {
        self.get_or_insert_with(Default::default)
    }
}

impl Debug for Properties {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Properties").finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_propagate() {
        assert_eq!(ViewFlags::HOVERED.has(), ViewFlags::HAS_HOVERED);
        assert_eq!(ViewFlags::FOCUSED.has(), ViewFlags::HAS_FOCUSED);
        assert_eq!(ViewFlags::ACTIVE.has(), ViewFlags::HAS_ACTIVE);
    }
}