mirui 0.12.1

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
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
use crate::components::button::Button;
use crate::components::checkbox::Checkbox;
use crate::components::progress_bar::ProgressBar;
use crate::components::tabbar::TabBar;
use crate::components::text_input::TextInput;
use crate::ecs::{Entity, World};
use crate::types::Fixed;
use crate::widget::dirty::Dirty;

use super::gesture::GestureEvent;
use super::input::{InputEvent, KEY_BACKSPACE, KEY_DELETE, KEY_END, KEY_HOME, KEY_LEFT, KEY_RIGHT};

/// Resource toggled by `cursor_blink_system` every ~500 ms; renderers
/// query this to decide whether to draw the TextInput cursor.
#[derive(Default, Clone, Copy)]
pub struct CursorBlinkPhase(pub bool);

/// Reads the global MonoClock, flips `CursorBlinkPhase` every 500 ms,
/// and marks every focused TextInput as Dirty so it repaints. Idle
/// cost when no TextInput is present: one resource read + one
/// arithmetic check.
pub fn cursor_blink_system(world: &mut World) {
    let now_ms = match world.resource::<crate::ecs::MonoClock>() {
        Some(c) => (c.clock)() / 1_000_000,
        None => return,
    };
    let new_phase = (now_ms / 500) % 2 == 0;
    let prev_phase = world
        .resource::<CursorBlinkPhase>()
        .map(|p| p.0)
        .unwrap_or(!new_phase);
    if new_phase == prev_phase {
        return;
    }
    world.insert_resource(CursorBlinkPhase(new_phase));
    let entities: alloc::vec::Vec<_> = world.query::<TextInput>().collect();
    for e in entities {
        if world
            .get::<TextInput>(e)
            .map(|t| t.focused)
            .unwrap_or(false)
        {
            world.insert(e, Dirty);
        }
    }
}

/// Press feedback on Button: highlight while the gesture is in flight,
/// release on Tap / DragEnd. Without DragStart we'd never see a "held"
/// state because Tap is press+release in one event.
pub(crate) fn button_handler(world: &mut World, entity: Entity, event: &GestureEvent) -> bool {
    match event {
        GestureEvent::DragStart { .. } => {
            if let Some(btn) = world.get_mut::<Button>(entity) {
                btn.pressed = true;
            }
            world.insert(entity, Dirty);
            false
        }
        GestureEvent::Tap { .. } | GestureEvent::DragEnd { .. } => {
            if let Some(btn) = world.get_mut::<Button>(entity) {
                btn.pressed = false;
            }
            world.insert(entity, Dirty);
            true
        }
        _ => false,
    }
}

pub(crate) fn checkbox_handler(world: &mut World, entity: Entity, event: &GestureEvent) -> bool {
    if let GestureEvent::Tap { .. } = event {
        if let Some(cb) = world.get_mut::<Checkbox>(entity) {
            cb.toggle();
        }
        world.insert(entity, Dirty);
        return true;
    }
    false
}

/// TabBar tap → snap selected, jump indicator_offset. Apps that want
/// a smooth slide attach an animate! component on the same entity and
/// drive `indicator_offset` from a Tween (same pattern Switch uses for
/// its thumb position).
pub(crate) fn tabbar_handler(world: &mut World, entity: Entity, event: &GestureEvent) -> bool {
    let x = match event {
        GestureEvent::Tap { x, .. } => *x,
        _ => return false,
    };
    let Some(rect) = world
        .get::<crate::widget::ComputedRect>(entity)
        .map(|c| c.0)
    else {
        return false;
    };
    if rect.w <= Fixed::ZERO {
        return false;
    }
    let count = match world.get::<TabBar>(entity) {
        Some(tb) if tb.count > 0 => tb.count,
        _ => return false,
    };
    let local = (x - rect.x).max(Fixed::ZERO);
    let tab_w = rect.w / Fixed::from_int(count as i32);
    let idx = (local / tab_w).to_int().clamp(0, count as i32 - 1) as u8;
    if let Some(tb) = world.get_mut::<TabBar>(entity) {
        tb.selected = idx;
        tb.indicator_offset = Fixed::from_int(idx as i32);
    }
    world.insert(entity, Dirty);
    true
}

/// TextInput's gesture handler is a no-op pass-through; focus is set
/// by the focus_on_tap helper in App::run when the Tap target's
/// ancestors carry `Focusable`. We mirror the resulting focus state
/// onto TextInput.focused so the renderer can read it without going
/// through FocusState.
pub(crate) fn textinput_gesture_handler(
    world: &mut World,
    entity: Entity,
    event: &GestureEvent,
) -> bool {
    if let GestureEvent::Tap { .. } = event {
        sync_textinput_focus(world);
        world.insert(entity, Dirty);
        return true;
    }
    false
}

/// Walk every TextInput in the world and update `focused` from the
/// shared `FocusState`. Called from the gesture handler (focus changes
/// only on Tap) and after backspace/insert (Dirty already).
fn sync_textinput_focus(world: &mut World) {
    let focused = world
        .resource::<crate::event::focus::FocusState>()
        .and_then(|fs| fs.focused);
    let entities: alloc::vec::Vec<_> = world.query::<TextInput>().collect();
    for e in entities {
        let want = Some(e) == focused;
        let current = world
            .get::<TextInput>(e)
            .map(|t| t.focused)
            .unwrap_or(false);
        if want != current {
            if let Some(ti) = world.get_mut::<TextInput>(e) {
                ti.focused = want;
            }
            world.insert(e, Dirty);
        }
    }
}

pub(crate) fn textinput_key_handler(world: &mut World, entity: Entity, event: &InputEvent) -> bool {
    let Some(ti) = world.get_mut::<TextInput>(entity) else {
        return false;
    };
    let mut changed = false;
    match event {
        InputEvent::CharInput { ch } => {
            if (*ch as u32) < 128 {
                changed |= ti.insert(*ch as u8);
            }
        }
        InputEvent::Key { code, pressed } if *pressed => match *code {
            KEY_BACKSPACE => changed |= ti.backspace(),
            KEY_DELETE => changed |= ti.delete_forward(),
            KEY_LEFT => {
                ti.move_left();
                changed = true;
            }
            KEY_RIGHT => {
                ti.move_right();
                changed = true;
            }
            KEY_HOME => {
                ti.move_home();
                changed = true;
            }
            KEY_END => {
                ti.move_end();
                changed = true;
            }
            _ => return false,
        },
        _ => return false,
    }
    if changed {
        world.insert(entity, Dirty);
    }
    true
}

/// ProgressBar reads the last rendered rect (PrevRect) so it can map
/// the pointer x to a 0..1 ratio without a layout re-walk.
pub(crate) fn progress_bar_handler(
    world: &mut World,
    entity: Entity,
    event: &GestureEvent,
) -> bool {
    let x = match event {
        GestureEvent::Tap { x, .. } | GestureEvent::DragMove { x, .. } => *x,
        _ => return false,
    };
    let Some(rect) = world
        .get::<crate::widget::ComputedRect>(entity)
        .map(|c| c.0)
    else {
        return false;
    };
    if rect.w <= Fixed::ZERO {
        return false;
    }
    let ratio = ((x - rect.x).to_f32() / rect.w.to_f32()).clamp(0.0, 1.0);
    if let Some(pb) = world.get_mut::<ProgressBar>(entity) {
        pb.value = ratio;
    }
    world.insert(entity, Dirty);
    true
}

// Snapshot ViewAttach fn pointers so the borrow on ViewRegistry
// drops before each fn gets &mut World. The mut-borrow conflict
// is the reason for the two-step copy instead of streaming.
fn attach_handlers_for(world: &mut World, entity: Entity) {
    let mut pending: alloc::vec::Vec<crate::widget::view::ViewAttach> = alloc::vec::Vec::new();
    if let Some(reg) = world.resource::<crate::widget::view::ViewRegistry>() {
        for v in reg.iter() {
            if let Some(f) = v.auto_attach {
                pending.push(f);
            }
        }
    }
    for f in pending {
        f(world, entity);
    }
}

/// Walk the widget tree from `root` and auto-install gesture/key
/// handlers on built-in widgets that don't already have one. Call once
/// after building the tree.
pub fn attach_widget_input_handlers(world: &mut World, root: Entity) {
    let mut stack = alloc::vec::Vec::with_capacity(16);
    stack.push(root);
    while let Some(entity) = stack.pop() {
        if let Some(children) = world.get::<crate::widget::Children>(entity) {
            for &child in &children.0 {
                stack.push(child);
            }
        }
        attach_handlers_for(world, entity);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::GestureHandler;
    use crate::types::Rect;
    use crate::widget::ComputedRect;

    #[test]
    fn tabbar_tap_picks_correct_tab() {
        let mut world = World::default();
        let e = world.spawn();
        world.insert(e, ComputedRect(Rect::new(0, 0, 300, 40)));
        world.insert(e, TabBar::new(3));
        // Tab width = 100. Tap at x=50 → tab 0; x=150 → tab 1; x=270 → tab 2.
        for (x, expected) in [(50, 0u8), (150, 1), (270, 2), (0, 0), (299, 2)] {
            tabbar_handler(
                &mut world,
                e,
                &GestureEvent::Tap {
                    x: Fixed::from_int(x),
                    y: Fixed::from_int(20),
                    target: e,
                },
            );
            let tb = world.get::<TabBar>(e).unwrap();
            assert_eq!(tb.selected, expected, "x={x} → expected {expected}");
            assert_eq!(tb.indicator_offset, Fixed::from_int(expected as i32));
        }
    }

    #[test]
    fn registry_attach_installs_button_gesture_handler() {
        use crate::types::Color;
        use crate::widget::view::ViewRegistry;

        let mut world = World::default();
        let mut reg = ViewRegistry::default();
        reg.register(crate::components::button::view());
        reg.sort_by_priority();
        world.insert_resource(reg);

        let e = world.spawn();
        world.insert(e, Button::new(Color::rgb(0, 0, 0), Color::rgb(0, 0, 0)));

        attach_handlers_for(&mut world, e);

        assert!(
            world.get::<GestureHandler>(e).is_some(),
            "registry-driven attach must install a GestureHandler on Button entities"
        );
    }

    #[test]
    fn registry_attach_skips_when_handler_already_present() {
        use crate::types::Color;
        use crate::widget::view::ViewRegistry;

        let mut world = World::default();
        let mut reg = ViewRegistry::default();
        reg.register(crate::components::button::view());
        reg.sort_by_priority();
        world.insert_resource(reg);

        let e = world.spawn();
        world.insert(e, Button::new(Color::rgb(0, 0, 0), Color::rgb(0, 0, 0)));
        fn user_handler(_: &mut World, _: Entity, _: &GestureEvent) -> bool {
            false
        }
        world.insert(
            e,
            GestureHandler {
                on_gesture: user_handler,
            },
        );

        attach_handlers_for(&mut world, e);

        let h = world.get::<GestureHandler>(e).expect("handler present");
        let installed: *const () = h.on_gesture as *const ();
        let expected: *const () = user_handler as *const ();
        assert!(
            core::ptr::eq(installed, expected),
            "user-supplied handler must not be overwritten"
        );
    }

    #[test]
    fn registry_attach_installs_text_input_handlers_and_focusable() {
        use crate::event::focus::{Focusable, KeyHandler};
        use crate::widget::view::ViewRegistry;

        let mut world = World::default();
        let mut reg = ViewRegistry::default();
        reg.register(crate::components::text_input::view());
        reg.sort_by_priority();
        world.insert_resource(reg);

        let e = world.spawn();
        world.insert(e, TextInput::new());

        attach_handlers_for(&mut world, e);

        assert!(world.get::<GestureHandler>(e).is_some());
        assert!(world.get::<Focusable>(e).is_some());
        assert!(world.get::<KeyHandler>(e).is_some());
    }

    #[test]
    fn registry_attach_installs_progress_bar_gesture_handler() {
        use crate::types::Color;
        use crate::widget::view::ViewRegistry;

        let mut world = World::default();
        let mut reg = ViewRegistry::default();
        reg.register(crate::components::progress_bar::view());
        reg.sort_by_priority();
        world.insert_resource(reg);

        let e = world.spawn();
        world.insert(
            e,
            ProgressBar::new(Color::rgb(0, 0, 0), Color::rgb(0, 0, 0)),
        );

        attach_handlers_for(&mut world, e);

        assert!(
            world.get::<GestureHandler>(e).is_some(),
            "registry-driven attach must install a GestureHandler on ProgressBar entities"
        );
    }

    #[test]
    fn registry_attach_installs_checkbox_gesture_handler() {
        use crate::types::Color;
        use crate::widget::view::ViewRegistry;

        let mut world = World::default();
        let mut reg = ViewRegistry::default();
        reg.register(crate::components::checkbox::view());
        reg.sort_by_priority();
        world.insert_resource(reg);

        let e = world.spawn();
        world.insert(e, Checkbox::new(Color::rgb(0, 0, 0), Color::rgb(0, 0, 0)));

        attach_handlers_for(&mut world, e);

        assert!(
            world.get::<GestureHandler>(e).is_some(),
            "registry-driven attach must install a GestureHandler on Checkbox entities"
        );
    }

    #[test]
    fn checkbox_tap_toggles_checked() {
        use crate::types::Color;
        use crate::widget::view::ViewRegistry;

        let mut world = World::default();
        let mut reg = ViewRegistry::default();
        reg.register(crate::components::checkbox::view());
        reg.sort_by_priority();
        world.insert_resource(reg);

        let e = world.spawn();
        world.insert(e, Checkbox::new(Color::rgb(0, 0, 0), Color::rgb(0, 0, 0)));
        attach_handlers_for(&mut world, e);

        let consumed = checkbox_handler(
            &mut world,
            e,
            &GestureEvent::Tap {
                x: Fixed::ZERO,
                y: Fixed::ZERO,
                target: e,
            },
        );
        assert!(consumed);
        assert!(world.get::<Checkbox>(e).unwrap().checked);
    }

    #[test]
    fn tabbar_ignores_non_tap() {
        let mut world = World::default();
        let e = world.spawn();
        world.insert(e, ComputedRect(Rect::new(0, 0, 300, 40)));
        world.insert(e, TabBar::new(3));
        let consumed = tabbar_handler(
            &mut world,
            e,
            &GestureEvent::DragMove {
                x: Fixed::from_int(50),
                y: Fixed::from_int(20),
                dx: Fixed::ZERO,
                dy: Fixed::ZERO,
                target: e,
            },
        );
        assert!(!consumed);
        assert_eq!(world.get::<TabBar>(e).unwrap().selected, 0);
    }
}