uzor 1.0.32

Core UI engine — geometry, interaction, input state
Documentation
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
//! Sense flags for widget interaction detection
//!
//! The [`Sense`] type determines what kinds of user interactions a widget
//! will detect and respond to.

/// What interactions a widget is sensitive to
///
/// Using `CLICK_AND_DRAG` introduces latency to distinguish click vs drag intent.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub struct Sense {
    /// Widget responds to clicks
    pub click: bool,
    /// Widget responds to drags
    pub drag: bool,
    /// Widget tracks hover state
    pub hover: bool,
    /// Widget can receive keyboard focus
    pub focus: bool,
    /// Widget responds to scroll wheel / touchpad scroll
    pub scroll: bool,
}

// Predefined constants
impl Sense {
    /// No interactions at all
    pub const NONE: Sense = Sense {
        click: false,
        drag: false,
        hover: false,
        focus: false,
        scroll: false,
    };

    /// Only hover detection
    pub const HOVER: Sense = Sense {
        click: false,
        drag: false,
        hover: true,
        focus: false,
        scroll: false,
    };

    /// Click and hover (for buttons, checkboxes)
    pub const CLICK: Sense = Sense {
        click: true,
        drag: false,
        hover: true,
        focus: false,
        scroll: false,
    };

    /// Drag and hover (for sliders, scrollbars)
    pub const DRAG: Sense = Sense {
        click: false,
        drag: true,
        hover: true,
        focus: false,
        scroll: false,
    };

    /// Both click and drag (introduces latency)
    pub const CLICK_AND_DRAG: Sense = Sense {
        click: true,
        drag: true,
        hover: true,
        focus: false,
        scroll: false,
    };

    /// Can receive keyboard focus but no mouse interaction
    pub const FOCUSABLE: Sense = Sense {
        click: false,
        drag: false,
        hover: true,
        focus: true,
        scroll: false,
    };

    /// Scroll-sensitive (for scrollable container viewports)
    pub const SCROLL: Sense = Sense {
        click: false,
        drag: false,
        hover: true,
        focus: false,
        scroll: true,
    };

    /// Full interaction - click, drag, hover, focus, scroll
    pub const ALL: Sense = Sense {
        click: true,
        drag: true,
        hover: true,
        focus: true,
        scroll: true,
    };
}

// Constructor methods
impl Sense {
    /// Create empty sense (no interactions)
    #[inline]
    pub fn none() -> Self {
        Self::NONE
    }

    /// Create hover-only sense
    #[inline]
    pub fn hover() -> Self {
        Self::HOVER
    }

    /// Create click sense (includes hover)
    #[inline]
    pub fn click() -> Self {
        Self::CLICK
    }

    /// Create drag sense (includes hover)
    #[inline]
    pub fn drag() -> Self {
        Self::DRAG
    }

    /// Create click and drag sense (includes hover, introduces latency)
    #[inline]
    pub fn click_and_drag() -> Self {
        Self::CLICK_AND_DRAG
    }

    /// Create focusable sense (for keyboard navigation)
    #[inline]
    pub fn focusable() -> Self {
        Self::FOCUSABLE
    }

    /// Create scroll-sensitive sense (includes hover)
    #[inline]
    pub fn scroll() -> Self {
        Self::SCROLL
    }

    /// Create full interaction sense
    #[inline]
    pub fn all() -> Self {
        Self::ALL
    }
}

// Combination methods
impl Sense {
    /// Union of two senses (OR)
    #[inline]
    pub fn union(self, other: Sense) -> Sense {
        Sense {
            click: self.click || other.click,
            drag: self.drag || other.drag,
            hover: self.hover || other.hover,
            focus: self.focus || other.focus,
            scroll: self.scroll || other.scroll,
        }
    }

    /// Intersection of two senses (AND)
    #[inline]
    pub fn intersection(self, other: Sense) -> Sense {
        Sense {
            click: self.click && other.click,
            drag: self.drag && other.drag,
            hover: self.hover && other.hover,
            focus: self.focus && other.focus,
            scroll: self.scroll && other.scroll,
        }
    }

    /// Add click sensing (also adds hover)
    #[inline]
    pub fn with_click(mut self) -> Self {
        self.click = true;
        self.hover = true;
        self
    }

    /// Add drag sensing (also adds hover)
    #[inline]
    pub fn with_drag(mut self) -> Self {
        self.drag = true;
        self.hover = true;
        self
    }

    /// Add focus capability
    #[inline]
    pub fn with_focus(mut self) -> Self {
        self.focus = true;
        self
    }

    /// Add scroll sensing (also adds hover)
    #[inline]
    pub fn with_scroll(mut self) -> Self {
        self.scroll = true;
        self.hover = true;
        self
    }
}

// Query methods
impl Sense {
    /// Check if any interaction is sensed (click, drag, focus, or scroll)
    #[inline]
    pub fn interactive(&self) -> bool {
        self.click || self.drag || self.focus || self.scroll
    }

    /// Check if both click and drag are sensed (has latency)
    #[inline]
    pub fn has_click_and_drag(&self) -> bool {
        self.click && self.drag
    }

    /// Check if widget is purely visual (no interactions)
    #[inline]
    pub fn is_passive(&self) -> bool {
        !self.click && !self.drag && !self.focus && !self.scroll
    }
}

impl std::ops::BitOr for Sense {
    type Output = Sense;

    /// Combine two senses using the `|` operator (equivalent to union)
    #[inline]
    fn bitor(self, rhs: Self) -> Self::Output {
        self.union(rhs)
    }
}

impl std::ops::BitOrAssign for Sense {
    /// Combine this sense with another using `|=`
    #[inline]
    fn bitor_assign(&mut self, rhs: Self) {
        *self = self.union(rhs);
    }
}

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

    #[test]
    fn test_predefined_constants() {
        assert!(!Sense::NONE.click);
        assert!(!Sense::NONE.drag);
        assert!(!Sense::NONE.hover);
        assert!(!Sense::NONE.focus);
        assert!(!Sense::NONE.scroll);

        assert!(!Sense::HOVER.click);
        assert!(Sense::HOVER.hover);
        assert!(!Sense::HOVER.scroll);

        assert!(Sense::CLICK.click);
        assert!(!Sense::CLICK.drag);
        assert!(Sense::CLICK.hover);
        assert!(!Sense::CLICK.scroll);

        assert!(!Sense::DRAG.click);
        assert!(Sense::DRAG.drag);
        assert!(Sense::DRAG.hover);
        assert!(!Sense::DRAG.scroll);

        assert!(Sense::CLICK_AND_DRAG.click);
        assert!(Sense::CLICK_AND_DRAG.drag);
        assert!(Sense::CLICK_AND_DRAG.hover);
        assert!(!Sense::CLICK_AND_DRAG.scroll);

        assert!(!Sense::FOCUSABLE.click);
        assert!(Sense::FOCUSABLE.hover);
        assert!(Sense::FOCUSABLE.focus);
        assert!(!Sense::FOCUSABLE.scroll);

        assert!(!Sense::SCROLL.click);
        assert!(!Sense::SCROLL.drag);
        assert!(Sense::SCROLL.hover);
        assert!(!Sense::SCROLL.focus);
        assert!(Sense::SCROLL.scroll);

        assert!(Sense::ALL.click);
        assert!(Sense::ALL.drag);
        assert!(Sense::ALL.hover);
        assert!(Sense::ALL.focus);
        assert!(Sense::ALL.scroll);
    }

    #[test]
    fn test_constructor_methods() {
        assert_eq!(Sense::none(), Sense::NONE);
        assert_eq!(Sense::hover(), Sense::HOVER);
        assert_eq!(Sense::click(), Sense::CLICK);
        assert_eq!(Sense::drag(), Sense::DRAG);
        assert_eq!(Sense::click_and_drag(), Sense::CLICK_AND_DRAG);
        assert_eq!(Sense::focusable(), Sense::FOCUSABLE);
        assert_eq!(Sense::scroll(), Sense::SCROLL);
        assert_eq!(Sense::all(), Sense::ALL);
    }

    #[test]
    fn test_union() {
        let click = Sense::click();
        let drag = Sense::drag();
        let combined = click.union(drag);

        assert!(combined.click);
        assert!(combined.drag);
        assert!(combined.hover);
        assert!(!combined.focus);
        assert!(!combined.scroll);
        assert_eq!(combined, Sense::CLICK_AND_DRAG);

        let with_scroll = Sense::click().union(Sense::scroll());
        assert!(with_scroll.click);
        assert!(with_scroll.scroll);
        assert!(with_scroll.hover);
    }

    #[test]
    fn test_intersection() {
        let click_and_drag = Sense::CLICK_AND_DRAG;
        let click = Sense::click();
        let common = click_and_drag.intersection(click);

        assert!(common.click);
        assert!(!common.drag);
        assert!(common.hover);
        assert!(!common.focus);
        assert!(!common.scroll);

        let all_and_scroll = Sense::ALL.intersection(Sense::SCROLL);
        assert!(!all_and_scroll.click);
        assert!(!all_and_scroll.drag);
        assert!(all_and_scroll.hover);
        assert!(!all_and_scroll.focus);
        assert!(all_and_scroll.scroll);
    }

    #[test]
    fn test_with_methods() {
        let sense = Sense::none().with_click();
        assert!(sense.click);
        assert!(sense.hover);
        assert!(!sense.drag);

        let sense = Sense::none().with_drag();
        assert!(sense.drag);
        assert!(sense.hover);
        assert!(!sense.click);

        let sense = Sense::click().with_focus();
        assert!(sense.click);
        assert!(sense.focus);
        assert!(sense.hover);

        let sense = Sense::none().with_scroll();
        assert!(sense.scroll);
        assert!(sense.hover);
        assert!(!sense.click);
        assert!(!sense.drag);
        assert!(!sense.focus);

        let sense = Sense::none().with_click().with_drag().with_focus().with_scroll();
        assert_eq!(sense, Sense::ALL);
    }

    #[test]
    fn test_query_methods() {
        assert!(Sense::click().interactive());
        assert!(Sense::drag().interactive());
        assert!(Sense::focusable().interactive());
        assert!(Sense::scroll().interactive());
        assert!(!Sense::hover().interactive());
        assert!(!Sense::none().interactive());

        assert!(Sense::CLICK_AND_DRAG.has_click_and_drag());
        assert!(Sense::ALL.has_click_and_drag());
        assert!(!Sense::click().has_click_and_drag());
        assert!(!Sense::drag().has_click_and_drag());

        assert!(Sense::none().is_passive());
        assert!(Sense::hover().is_passive());
        assert!(!Sense::click().is_passive());
        assert!(!Sense::drag().is_passive());
        assert!(!Sense::focusable().is_passive());
        assert!(!Sense::scroll().is_passive());
    }

    #[test]
    fn test_bitor_operator() {
        let combined = Sense::click() | Sense::drag();
        assert_eq!(combined, Sense::CLICK_AND_DRAG);

        let mut sense = Sense::click();
        sense |= Sense::drag();
        assert_eq!(sense, Sense::CLICK_AND_DRAG);

        let complex = Sense::click() | Sense::drag() | Sense::focusable();
        assert!(complex.click);
        assert!(complex.drag);
        assert!(complex.focus);
        assert!(complex.hover);
        assert!(!complex.scroll);

        let with_scroll = Sense::click() | Sense::scroll();
        assert!(with_scroll.click);
        assert!(with_scroll.scroll);
        assert!(with_scroll.hover);
    }

    #[test]
    fn test_default() {
        let default_sense = Sense::default();
        assert_eq!(default_sense, Sense::NONE);
    }

    #[test]
    fn test_eq_and_hash() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(Sense::click());
        set.insert(Sense::click());
        set.insert(Sense::drag());
        set.insert(Sense::scroll());

        assert_eq!(set.len(), 3);
        assert!(set.contains(&Sense::click()));
        assert!(set.contains(&Sense::drag()));
        assert!(set.contains(&Sense::scroll()));
    }

    #[test]
    fn test_clone_and_copy() {
        let original = Sense::click();
        let cloned = original.clone();
        let copied = original;

        assert_eq!(original, cloned);
        assert_eq!(original, copied);
    }
}