uzor 1.0.13

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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Integration tests for uzor - widget + input interaction
//!
//! These tests verify that widgets work correctly with the input system,
//! covering InputState query methods, scroll state, keyboard modifiers,
//! and boundary conditions.

use uzor::input::{InputState, MouseButton, ModifierKeys, PointerState};
use uzor::input::state::DragState as InputDragState;
use uzor::types::{
    ScrollState, WidgetId, WidgetInputState, WidgetInteraction, WidgetRect, WidgetState,
};

// =============================================================================
// Helper Functions
// =============================================================================

/// Create InputState with pointer at position
fn input_at(x: f64, y: f64) -> InputState {
    InputState {
        pointer: PointerState {
            pos: Some((x, y)),
            ..Default::default()
        },
        ..Default::default()
    }
}

/// Create InputState with pointer at position and button clicked
fn input_clicked_at(x: f64, y: f64) -> InputState {
    InputState {
        pointer: PointerState {
            pos: Some((x, y)),
            clicked: Some(MouseButton::Left),
            ..Default::default()
        },
        ..Default::default()
    }
}

/// Create InputState with pointer at position and button pressed
fn input_pressed_at(x: f64, y: f64) -> InputState {
    InputState {
        pointer: PointerState {
            pos: Some((x, y)),
            button_down: Some(MouseButton::Left),
            ..Default::default()
        },
        ..Default::default()
    }
}

/// Create InputState with pointer at position and right button clicked
fn input_right_clicked_at(x: f64, y: f64) -> InputState {
    InputState {
        pointer: PointerState {
            pos: Some((x, y)),
            clicked: Some(MouseButton::Right),
            ..Default::default()
        },
        ..Default::default()
    }
}

/// Create InputState with scroll wheel delta
fn input_scroll_at(x: f64, y: f64, delta_y: f64) -> InputState {
    InputState {
        pointer: PointerState {
            pos: Some((x, y)),
            ..Default::default()
        },
        scroll_delta: (0.0, delta_y),
        ..Default::default()
    }
}

/// Create InputState with drag
fn input_dragging_at(start_x: f64, start_y: f64, current_x: f64, current_y: f64) -> InputState {
    InputState {
        pointer: PointerState {
            pos: Some((current_x, current_y)),
            button_down: Some(MouseButton::Left),
            prev_pos: Some((start_x, start_y)),
            ..Default::default()
        },
        drag: Some(InputDragState::new(
            (start_x, start_y),
            (current_x, current_y),
            MouseButton::Left,
        )),
        ..Default::default()
    }
}

/// Determine WidgetState from InputState and rect
fn widget_state_from_input(input: &InputState, rect: &WidgetRect, disabled: bool) -> WidgetState {
    if disabled {
        return WidgetState::Disabled;
    }

    let hovered = input.is_hovered(rect);

    if hovered && input.is_mouse_down() {
        WidgetState::Pressed
    } else if hovered {
        WidgetState::Hovered
    } else {
        WidgetState::Normal
    }
}

// =============================================================================
// 1. Button Interaction Flow
// =============================================================================

#[test]
fn test_button_full_interaction_flow() {
    let rect = WidgetRect::new(100.0, 100.0, 100.0, 40.0);

    // 1. Pointer outside - Normal state
    let input = input_at(50.0, 50.0);
    let state = widget_state_from_input(&input, &rect, false);
    assert_eq!(state, WidgetState::Normal);

    // 2. Move pointer over button - Hovered state
    let input = input_at(150.0, 120.0);
    let state = widget_state_from_input(&input, &rect, false);
    assert_eq!(state, WidgetState::Hovered);

    // 3. Press mouse button - Pressed state
    let input = input_pressed_at(150.0, 120.0);
    let state = widget_state_from_input(&input, &rect, false);
    assert_eq!(state, WidgetState::Pressed);

    // 4. Release (click)
    let input = input_clicked_at(150.0, 120.0);
    assert!(input.is_clicked());
    assert!(input.is_hovered(&rect));

    // 5. Disabled button
    let input = input_clicked_at(150.0, 120.0);
    let state = widget_state_from_input(&input, &rect, true);
    assert_eq!(state, WidgetState::Disabled);
}

#[test]
fn test_button_click_outside_not_registered() {
    let rect = WidgetRect::new(100.0, 100.0, 100.0, 40.0);

    let input_press = input_pressed_at(150.0, 120.0);
    assert!(input_press.is_hovered(&rect));

    let input_release = input_at(50.0, 50.0);
    assert!(!input_release.is_hovered(&rect));
}

// =============================================================================
// 2. Checkbox Toggle Flow
// =============================================================================

#[test]
fn test_checkbox_hover_states() {
    let rect = WidgetRect::new(100.0, 100.0, 150.0, 24.0);

    // Outside - Normal
    let input = input_at(50.0, 50.0);
    let state = widget_state_from_input(&input, &rect, false);
    assert_eq!(state, WidgetState::Normal);

    // Inside - Hovered
    let input = input_at(150.0, 112.0);
    let state = widget_state_from_input(&input, &rect, false);
    assert_eq!(state, WidgetState::Hovered);

    // Inside + Pressed
    let input = input_pressed_at(150.0, 112.0);
    let state = widget_state_from_input(&input, &rect, false);
    assert_eq!(state, WidgetState::Pressed);
}

// =============================================================================
// 3. Slider Drag Flow
// =============================================================================

#[test]
fn test_slider_drag_flow() {
    let rect = WidgetRect::new(100.0, 100.0, 200.0, 30.0);

    // Click on track at 50% position
    let track_center_x = rect.x + rect.width / 2.0;
    let input = input_clicked_at(track_center_x, rect.center_y());
    assert!(input.is_hovered(&rect));
    assert!(input.is_clicked());

    // Press on handle
    let input = input_pressed_at(track_center_x, rect.center_y());
    assert!(input.is_mouse_down());
    assert!(input.is_hovered(&rect));

    // Drag to 75% position
    let drag_x = rect.x + rect.width * 0.75;
    let input = input_dragging_at(track_center_x, rect.center_y(), drag_x, rect.center_y());
    assert!(input.is_dragging());

    // Release
    let input = input_at(drag_x, rect.center_y());
    assert!(!input.is_dragging());
}

#[test]
fn test_slider_disabled_state() {
    let rect = WidgetRect::new(100.0, 100.0, 200.0, 30.0);

    let input = input_dragging_at(150.0, 115.0, 200.0, 115.0);
    let state = widget_state_from_input(&input, &rect, true);
    assert_eq!(state, WidgetState::Disabled);
}

// =============================================================================
// 4. Scrollbar Interaction
// =============================================================================

#[test]
fn test_scrollbar_wheel_and_drag() {
    let mut scroll_state = ScrollState::new();
    let content_height = 500.0;
    let viewport_height = 200.0;
    let track_height = 200.0;

    let handled = scroll_state.handle_wheel(1.0, content_height, viewport_height);
    assert!(handled);
    assert!(scroll_state.offset > 0.0);
    let offset_after_one_scroll = scroll_state.offset;

    let handled = scroll_state.handle_wheel(-1.0, content_height, viewport_height);
    assert!(handled);
    assert!(scroll_state.offset < offset_after_one_scroll);

    // Drag handle
    scroll_state.offset = 0.0;
    scroll_state.start_drag(100.0);
    assert!(scroll_state.is_dragging);

    scroll_state.handle_drag(150.0, track_height, content_height, viewport_height);
    assert!(scroll_state.offset > 0.0);

    scroll_state.end_drag();
    assert!(!scroll_state.is_dragging);
}

#[test]
fn test_scrollbar_clamping() {
    let mut scroll_state = ScrollState::new();
    let content_height = 500.0;
    let viewport_height = 200.0;
    let max_scroll = content_height - viewport_height;

    scroll_state.offset = 1000.0;
    scroll_state.clamp(content_height, viewport_height);
    assert_eq!(scroll_state.offset, max_scroll);

    scroll_state.offset = -100.0;
    scroll_state.clamp(content_height, viewport_height);
    assert_eq!(scroll_state.offset, 0.0);
}

#[test]
fn test_scrollbar_no_scroll_when_content_fits() {
    let mut scroll_state = ScrollState::new();
    let content_height = 100.0;
    let viewport_height = 200.0;

    let handled = scroll_state.handle_wheel(1.0, content_height, viewport_height);
    assert!(!handled);
    assert_eq!(scroll_state.offset, 0.0);
}

// =============================================================================
// 5. WidgetInputState Flow
// =============================================================================

#[test]
fn test_widget_input_state_management() {
    let mut state = WidgetInputState::new();
    let widget1 = WidgetId::new("widget1");
    let widget2 = WidgetId::new("widget2");

    // Focus changes
    state.focus.set_focus(widget1.clone());
    assert!(state.focus.is_focused(&widget1));
    assert!(!state.focus.is_focused(&widget2));

    state.focus.set_focus(widget2.clone());
    assert!(!state.focus.is_focused(&widget1));
    assert!(state.focus.is_focused(&widget2));

    // Hover tracking
    state.hover.set_hovered(Some(widget1.clone()));
    assert!(state.hover.is_hovered(&widget1));
    assert!(!state.hover.is_hovered(&widget2));

    // Drag start/update/end
    state.start_drag(widget1.clone(), 100.0, 50.0);
    assert!(state.drag.is_dragging(&widget1));
    assert_eq!(state.drag.start_pos, (100.0, 50.0));

    state.drag.update(150.0, 60.0);
    assert_eq!(state.drag.current_pos, (150.0, 60.0));
    assert_eq!(state.drag.delta(), (50.0, 10.0));

    state.drag.end();
    assert!(!state.drag.is_dragging(&widget1));

    // Double-click detection
    let widget_id = WidgetId::new("button1");

    state.mouse_press(100.0, 50.0, Some(widget_id.clone()));
    let interaction = state.mouse_release(100.0, 50.0, 1000.0);
    assert_eq!(interaction, WidgetInteraction::Click);

    state.mouse_press(101.0, 51.0, Some(widget_id.clone()));
    let interaction = state.mouse_release(101.0, 51.0, 1200.0);
    assert_eq!(interaction, WidgetInteraction::DoubleClick);
}

#[test]
fn test_widget_input_state_pending_focus() {
    let mut state = WidgetInputState::new();
    let widget_id = WidgetId::new("input1");

    state.focus.request_focus(widget_id.clone());
    assert!(!state.focus.is_focused(&widget_id));

    state.end_frame();
    assert!(state.focus.is_focused(&widget_id));
}

// =============================================================================
// 6. Keyboard and Modifier Keys
// =============================================================================

#[test]
fn test_keyboard_modifiers() {
    let mut input = input_clicked_at(150.0, 120.0);
    input.modifiers.ctrl = true;
    assert!(input.ctrl());
    assert!(input.is_clicked());

    let mut input = input_clicked_at(150.0, 120.0);
    input.modifiers.shift = true;
    assert!(input.shift());
    assert!(input.is_clicked());

    let mut input = input_clicked_at(150.0, 120.0);
    input.modifiers.ctrl = true;
    input.modifiers.shift = true;
    assert!(input.ctrl());
    assert!(input.shift());
    assert!(input.modifiers.ctrl_shift());
    assert!(!input.modifiers.ctrl_alt());

    let modifiers = ModifierKeys {
        ctrl: true,
        alt: true,
        ..Default::default()
    };
    assert!(modifiers.ctrl_alt());
    assert!(!modifiers.ctrl_shift());
}

#[test]
fn test_modifier_any() {
    let none = ModifierKeys::none();
    assert!(!none.any());

    let shift = ModifierKeys::shift();
    assert!(shift.any());
    assert!(shift.shift);

    let ctrl = ModifierKeys::ctrl();
    assert!(ctrl.any());
    assert!(ctrl.ctrl);
}

// =============================================================================
// 7. Right-click / Context Menu
// =============================================================================

#[test]
fn test_right_click_context_menu() {
    let rect = WidgetRect::new(100.0, 100.0, 200.0, 150.0);

    let input = input_right_clicked_at(150.0, 120.0);
    assert!(input.is_hovered(&rect));
    assert!(input.is_right_clicked());

    let input = input_right_clicked_at(150.0, 120.0);
    assert!(input.is_right_clicked());
}

#[test]
fn test_middle_click() {
    let rect = WidgetRect::new(100.0, 100.0, 200.0, 150.0);

    let input = InputState {
        pointer: PointerState {
            pos: Some((150.0, 120.0)),
            clicked: Some(MouseButton::Middle),
            ..Default::default()
        },
        ..Default::default()
    };

    assert!(input.is_hovered(&rect));
    assert!(input.is_middle_clicked());
}

// =============================================================================
// 8. Scroll Events
// =============================================================================

#[test]
fn test_scroll_in_scrollable_area() {
    let mut scroll_state = ScrollState::new();
    let content_height = 1000.0;
    let viewport_height = 300.0;
    let max_scroll = content_height - viewport_height;

    let handled = scroll_state.handle_wheel(1.0, content_height, viewport_height);
    assert!(handled);
    assert!(scroll_state.offset > 0.0);
    let first_offset = scroll_state.offset;

    let handled = scroll_state.handle_wheel(1.0, content_height, viewport_height);
    assert!(handled);
    assert!(scroll_state.offset > first_offset);

    scroll_state.offset = 10000.0;
    scroll_state.clamp(content_height, viewport_height);
    assert_eq!(scroll_state.offset, max_scroll);

    scroll_state.offset = -500.0;
    scroll_state.clamp(content_height, viewport_height);
    assert_eq!(scroll_state.offset, 0.0);
}

#[test]
fn test_scroll_track_click() {
    let mut scroll_state = ScrollState::new();
    let content_height = 1000.0;
    let viewport_height = 300.0;
    let track_y = 100.0;
    let track_height = 300.0;

    let click_y = track_y + track_height * 0.5;
    scroll_state.handle_track_click(
        click_y,
        track_y,
        track_height,
        content_height,
        viewport_height,
    );

    let max_scroll = content_height - viewport_height;
    let expected_offset = 0.5 * max_scroll;
    assert!((scroll_state.offset - expected_offset).abs() < 1.0);
}

// =============================================================================
// 9. Boundary Testing
// =============================================================================

#[test]
fn test_boundary_clicks() {
    let rect = WidgetRect::new(100.0, 100.0, 100.0, 50.0);

    let input = input_clicked_at(100.0, 125.0);
    assert!(input.is_hovered(&rect));

    let input = input_clicked_at(200.0, 125.0);
    assert!(input.is_hovered(&rect));

    let input = input_clicked_at(150.0, 100.0);
    assert!(input.is_hovered(&rect));

    let input = input_clicked_at(150.0, 150.0);
    assert!(input.is_hovered(&rect));

    let input = input_clicked_at(99.9, 125.0);
    assert!(!input.is_hovered(&rect));

    let input = input_clicked_at(200.1, 125.0);
    assert!(!input.is_hovered(&rect));
}

#[test]
fn test_zero_size_rect() {
    let rect = WidgetRect::new(100.0, 100.0, 0.0, 0.0);

    let input = input_clicked_at(100.0, 100.0);
    assert!(input.is_hovered(&rect));

    let input = input_clicked_at(100.1, 100.0);
    assert!(!input.is_hovered(&rect));
}

#[test]
fn test_consume_events() {
    let mut input = input_clicked_at(150.0, 120.0);

    assert!(input.is_clicked());
    assert!(input.consume_click());
    assert!(!input.is_clicked());

    let mut input = input_scroll_at(150.0, 120.0, 1.0);
    assert_eq!(input.scroll_delta, (0.0, 1.0));
    let delta = input.consume_scroll();
    assert_eq!(delta, (0.0, 1.0));
    assert_eq!(input.scroll_delta, (0.0, 0.0));

    let input = InputState {
        pointer: PointerState {
            pos: Some((150.0, 120.0)),
            double_clicked: Some(MouseButton::Left),
            ..Default::default()
        },
        ..Default::default()
    };
    assert!(input.is_double_clicked());
}

#[test]
fn test_drag_delta_calculation() {
    let input = input_dragging_at(100.0, 100.0, 150.0, 130.0);

    assert!(input.is_dragging());
    let drag = input.drag.as_ref().unwrap();
    assert_eq!(drag.start, (100.0, 100.0));
    assert_eq!(drag.current, (150.0, 130.0));
    assert_eq!(drag.total_delta, (50.0, 30.0));
}

// =============================================================================
// 10. Complex Interaction Sequences
// =============================================================================

#[test]
fn test_complex_drag_sequence() {
    let mut widget_state = WidgetInputState::new();
    let slider_id = WidgetId::new("slider1");

    widget_state.start_drag_with_value(slider_id.clone(), 100.0, 50.0, 0.0);
    assert!(widget_state.drag.is_dragging(&slider_id));
    assert_eq!(widget_state.drag.initial_value, 0.0);

    widget_state.update_mouse(120.0, 50.0);
    assert_eq!(widget_state.drag.current_pos, (120.0, 50.0));
    assert_eq!(widget_state.drag.delta(), (20.0, 0.0));

    widget_state.update_mouse(150.0, 50.0);
    assert_eq!(widget_state.drag.current_pos, (150.0, 50.0));
    assert_eq!(widget_state.drag.delta(), (50.0, 0.0));

    widget_state.drag.end();
    assert!(!widget_state.drag.is_dragging(&slider_id));
}

#[test]
fn test_modal_with_scrollable_content() {
    let mut scroll_state = ScrollState::new();
    let modal_rect = WidgetRect::new(200.0, 100.0, 600.0, 500.0);
    let content_rect = modal_rect.inset(16.0);

    let content_height = 1000.0;
    let viewport_height = content_rect.height;

    let input = input_scroll_at(400.0, 300.0, 1.0);
    assert!(input.is_hovered(&modal_rect));

    let handled = scroll_state.handle_wheel(1.0, content_height, viewport_height);
    assert!(handled);
    assert!(scroll_state.offset > 0.0);
}

#[test]
fn test_slider_with_modifiers() {
    let _rect = WidgetRect::new(100.0, 100.0, 200.0, 30.0);

    let input = input_dragging_at(150.0, 115.0, 180.0, 115.0);
    assert!(input.is_dragging());
    assert!(!input.ctrl());

    let mut input = input_dragging_at(150.0, 115.0, 180.0, 115.0);
    input.modifiers.ctrl = true;
    assert!(input.is_dragging());
    assert!(input.ctrl());

    let mut input = input_dragging_at(150.0, 115.0, 180.0, 115.0);
    input.modifiers.shift = true;
    assert!(input.is_dragging());
    assert!(input.shift());
}