mirui 0.38.0

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
pub mod focus;
pub mod gesture;
pub mod hit_test;
pub mod input;
pub mod multi_tap;
pub mod scroll;
pub mod sim;
pub mod widget_input;

use crate::ecs::{Entity, World};
use crate::types::Fixed;
use crate::ui::{Parent, UserState};

use focus::key_dispatch;
use gesture::{GestureEvent, GestureSystem};
use hit_test::hit_test;
use input::InputEvent;
use scroll::{ScrollDragState, scroll_system};

#[derive(Clone, Copy, Default)]
pub struct PointerCursor {
    pub x: Fixed,
    pub y: Fixed,
    pub down: bool,
    /// Bumps on every PointerDown / PointerUp; PointerMove leaves it.
    pub event_seq: u32,
}

/// Single source of truth for the per-event side of the input
/// pipeline. Both `App::run`'s real input loop and
/// `sim_timeline_system` (which fakes pointer events) call this so
/// that simulated inputs traverse the exact same scroll / hit-test /
/// gesture-recognizer / key-dispatch path as real ones.
///
/// Does *not* drain `GestureSystem.events` — the caller decides when
/// to dispatch (real input loop drains after the whole `poll_event`
/// burst; sim drains every system tick).
pub fn dispatch_input(
    world: &mut World,
    root: Entity,
    event: &InputEvent,
    now_ms: u32,
    lw: u16,
    lh: u16,
) {
    match event {
        InputEvent::PointerDown { x, y, .. } => {
            let mut next = world
                .resource::<PointerCursor>()
                .copied()
                .unwrap_or_default();
            next.x = *x;
            next.y = *y;
            next.down = true;
            next.event_seq = next.event_seq.wrapping_add(1);
            world.insert_resource(next);
        }
        InputEvent::PointerMove { x, y, .. } => {
            let mut next = world
                .resource::<PointerCursor>()
                .copied()
                .unwrap_or_default();
            next.x = *x;
            next.y = *y;
            world.insert_resource(next);
        }
        InputEvent::PointerUp { x, y, .. } => {
            let mut next = world
                .resource::<PointerCursor>()
                .copied()
                .unwrap_or_default();
            next.x = *x;
            next.y = *y;
            next.down = false;
            next.event_seq = next.event_seq.wrapping_add(1);
            world.insert_resource(next);
        }
        _ => {}
    }

    if let InputEvent::PointerDown { x, y, .. } = event {
        if let Some(target) = hit_test(world, root, *x, *y, lw, lh) {
            if entity_or_ancestor_disabled(world, target) {
                key_dispatch(world, event);
                return;
            }
        }
    }

    scroll_system(world, root, event, lw, lh);

    let hit = match event {
        InputEvent::PointerDown { x, y, .. } => hit_test(world, root, *x, *y, lw, lh),
        _ => None,
    };
    let scroll_claimed = world
        .resource::<ScrollDragState>()
        .is_some_and(|s| s.active && s.resolved);
    if let Some(gs) = world.resource_mut::<GestureSystem>() {
        gs.recognizer.scroll_claimed = scroll_claimed;
        gs.recognizer.update(event, now_ms, hit, &mut gs.events);
    }

    key_dispatch(world, event);
}

pub fn entity_or_ancestor_disabled(world: &World, entity: Entity) -> bool {
    let mut cur = Some(entity);
    while let Some(e) = cur {
        if matches!(world.get::<UserState>(e), Some(UserState::Disabled)) {
            return true;
        }
        cur = world.get::<Parent>(e).map(|p| p.0);
    }
    false
}

/// Gesture handler callback. `Fn` is a zero-alloc pointer for internal and
/// hand-written handlers; `Closure` carries an `Rc` so `ui!` handlers can
/// capture state (e.g. a `Signal`). Returns `true` to consume the event.
type GestureFn = fn(&mut World, Entity, &GestureEvent) -> bool;
type GestureClosure = alloc::rc::Rc<dyn Fn(&mut World, Entity, &GestureEvent) -> bool>;

pub enum GestureCallback {
    Fn(GestureFn),
    Closure(GestureClosure),
}

impl GestureCallback {
    fn call(&self, world: &mut World, entity: Entity, event: &GestureEvent) -> bool {
        match self {
            GestureCallback::Fn(f) => f(world, entity, event),
            GestureCallback::Closure(rc) => rc(world, entity, event),
        }
    }
}

pub struct GestureHandler {
    pub on_gesture: GestureCallback,
}

impl GestureHandler {
    pub fn from_fn(f: GestureFn) -> Self {
        GestureHandler {
            on_gesture: GestureCallback::Fn(f),
        }
    }

    /// Look up `entity`'s gesture handler and run it. Clones the callback out
    /// of the borrow before invoking so the closure may touch the World.
    pub fn trigger(world: &mut World, entity: Entity, event: &GestureEvent) -> Option<bool> {
        let cb = world
            .get::<GestureHandler>(entity)
            .map(|h| match &h.on_gesture {
                GestureCallback::Fn(f) => GestureCallback::Fn(*f),
                GestureCallback::Closure(rc) => GestureCallback::Closure(alloc::rc::Rc::clone(rc)),
            })?;
        Some(cb.call(world, entity, event))
    }
}

/// Business-event callback, generic over the widget's event type. `Closure`
/// carries an `Rc` so a `ui!` handler can capture state such as a `Signal`.
pub type BusinessFn<E> = fn(&mut World, Entity, &E) -> bool;
pub type BusinessClosure<E> = alloc::rc::Rc<dyn Fn(&mut World, Entity, &E) -> bool>;

pub enum BusinessCallback<E> {
    Fn(BusinessFn<E>),
    Closure(BusinessClosure<E>),
}

impl<E> BusinessCallback<E> {
    pub fn call(&self, world: &mut World, entity: Entity, event: &E) -> bool {
        match self {
            BusinessCallback::Fn(f) => f(world, entity, event),
            BusinessCallback::Closure(rc) => rc(world, entity, event),
        }
    }

    /// Clone the callback out of a component borrow so it can run while the
    /// World is mutably borrowed (same pattern as `GestureHandler::trigger`).
    pub fn clone_out(&self) -> Self {
        match self {
            BusinessCallback::Fn(f) => BusinessCallback::Fn(*f),
            BusinessCallback::Closure(rc) => BusinessCallback::Closure(alloc::rc::Rc::clone(rc)),
        }
    }
}

impl<E> From<BusinessFn<E>> for BusinessCallback<E> {
    fn from(f: BusinessFn<E>) -> Self {
        BusinessCallback::Fn(f)
    }
}

/// Aggregated context handed to user `on EventKind` bodies and callback-form fns.
pub struct HandlerCtx<'a, E> {
    pub world: &'a mut World,
    pub entity: Entity,
    pub event: &'a E,
}

/// Spelled-out bubble control for `on EventKind` body return values.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BubbleControl {
    Prevent,
    Allow,
}

/// Bridges user `on EventKind` body return types into the dispatch fn's bubble-control bit.
///
/// - `()` → `Prevent` (default; mirrors web's "stopPropagation by default" idiom)
/// - `bool` → `true = Prevent, false = Allow`
/// - `BubbleControl` → pass through
pub trait HandlerReturn {
    fn into_consumed(self) -> bool;
}

impl HandlerReturn for () {
    fn into_consumed(self) -> bool {
        true
    }
}

impl HandlerReturn for bool {
    fn into_consumed(self) -> bool {
        self
    }
}

impl HandlerReturn for BubbleControl {
    fn into_consumed(self) -> bool {
        matches!(self, BubbleControl::Prevent)
    }
}

/// Walk from `target` up via `Parent` links, invoking the first
/// `GestureHandler` found. Stops when a handler returns `true`
/// (consumed) or the root is reached.
pub fn bubble_dispatch(world: &mut World, event: &GestureEvent) {
    bubble_dispatch_at(world, event, 0);
}

/// `now_ms`-aware variant; pass `0` when no clock is available.
pub fn bubble_dispatch_at(world: &mut World, event: &GestureEvent, now_ms: u32) {
    multi_tap::observe_gesture(world, event, now_ms);
    let mut current = event.target();
    loop {
        let internals = collect_internal_handlers(world, current);
        for f in internals {
            if f(world, current, event) {
                return;
            }
        }
        if GestureHandler::trigger(world, current, event) == Some(true) {
            return;
        }
        match world.get::<Parent>(current) {
            Some(p) => current = p.0,
            None => return,
        }
    }
}

fn collect_internal_handlers(
    world: &World,
    entity: Entity,
) -> alloc::vec::Vec<crate::ui::view::ViewInternalGesture> {
    let Some(registry) = world.resource::<crate::ui::view::ViewRegistry>() else {
        return alloc::vec::Vec::new();
    };
    registry
        .iter()
        .filter_map(|v| {
            let f = v.internal_gesture()?;
            match v.component_filter() {
                Some(type_id) if world.has_type(entity, type_id) => Some(f),
                Some(_) => None,
                None => Some(f),
            }
        })
        .collect()
}

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

    #[test]
    fn ancestor_disabled_propagates() {
        let mut world = World::new();
        let parent = world.spawn_empty();
        let child = world.spawn_empty();
        world.insert(child, Parent(parent));
        world.insert(parent, UserState::Disabled);
        assert!(entity_or_ancestor_disabled(&world, child));
    }

    #[test]
    fn entity_self_disabled() {
        let mut world = World::new();
        let e = world.spawn_empty();
        world.insert(e, UserState::Disabled);
        assert!(entity_or_ancestor_disabled(&world, e));
    }

    #[test]
    fn unrelated_entity_not_disabled() {
        let mut world = World::new();
        let a = world.spawn_empty();
        let b = world.spawn_empty();
        world.insert(a, UserState::Disabled);
        assert!(!entity_or_ancestor_disabled(&world, b));
    }

    #[test]
    fn errored_does_not_propagate_via_disabled_walk() {
        let mut world = World::new();
        let e = world.spawn_empty();
        world.insert(e, UserState::Errored);
        assert!(!entity_or_ancestor_disabled(&world, e));
    }

    mod dual_channel {
        use super::*;
        use crate::types::Fixed;
        use crate::ui::view::{View, ViewRegistry};
        use core::sync::atomic::{AtomicU8, Ordering};
        use std::sync::Mutex;

        struct ChannelMarker;

        static INTERNAL_FIRES: AtomicU8 = AtomicU8::new(0);
        static USER_FIRES: AtomicU8 = AtomicU8::new(0);
        static SERIAL: Mutex<()> = Mutex::new(());

        fn reset() {
            INTERNAL_FIRES.store(0, Ordering::SeqCst);
            USER_FIRES.store(0, Ordering::SeqCst);
        }

        fn internal_consume(_w: &mut World, _e: Entity, _ev: &GestureEvent) -> bool {
            INTERNAL_FIRES.fetch_add(1, Ordering::SeqCst);
            true
        }

        fn internal_passthrough(_w: &mut World, _e: Entity, _ev: &GestureEvent) -> bool {
            INTERNAL_FIRES.fetch_add(1, Ordering::SeqCst);
            false
        }

        fn user_handler(_w: &mut World, _e: Entity, _ev: &GestureEvent) -> bool {
            USER_FIRES.fetch_add(1, Ordering::SeqCst);
            true
        }

        fn registry_with(internal: fn(&mut World, Entity, &GestureEvent) -> bool) -> ViewRegistry {
            fn dummy_render(
                _r: &mut dyn crate::render::renderer::Renderer,
                _w: &World,
                _e: Entity,
                _rect: &crate::types::Rect,
                _ctx: &mut crate::ui::view::ViewCtx,
            ) {
            }
            let mut reg = ViewRegistry::default();
            reg.insert(
                View::new("ChannelMarker", 60, dummy_render)
                    .with_filter::<ChannelMarker>()
                    .with_internal_gesture(internal),
            );
            reg
        }

        fn tap_event(target: Entity) -> GestureEvent {
            GestureEvent::Tap {
                x: Fixed::ZERO,
                y: Fixed::ZERO,
                target,
            }
        }

        #[test]
        fn user_only_runs_when_no_internal() {
            let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
            reset();
            let mut world = World::new();
            world.insert_resource(ViewRegistry::default());
            let e = world.spawn_empty();
            world.insert(e, GestureHandler::from_fn(user_handler));
            bubble_dispatch_at(&mut world, &tap_event(e), 0);
            assert_eq!(INTERNAL_FIRES.load(Ordering::SeqCst), 0);
            assert_eq!(USER_FIRES.load(Ordering::SeqCst), 1);
        }

        #[test]
        fn internal_only_runs_when_no_user() {
            let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
            reset();
            let mut world = World::new();
            world.insert_resource(registry_with(internal_consume));
            let e = world.spawn_empty();
            world.insert(e, ChannelMarker);
            bubble_dispatch_at(&mut world, &tap_event(e), 0);
            assert_eq!(INTERNAL_FIRES.load(Ordering::SeqCst), 1);
            assert_eq!(USER_FIRES.load(Ordering::SeqCst), 0);
        }

        #[test]
        fn internal_consumes_blocks_user() {
            let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
            reset();
            let mut world = World::new();
            world.insert_resource(registry_with(internal_consume));
            let e = world.spawn_empty();
            world.insert(e, ChannelMarker);
            world.insert(e, GestureHandler::from_fn(user_handler));
            bubble_dispatch_at(&mut world, &tap_event(e), 0);
            assert_eq!(INTERNAL_FIRES.load(Ordering::SeqCst), 1);
            assert_eq!(
                USER_FIRES.load(Ordering::SeqCst),
                0,
                "internal returned true, user must not fire",
            );
        }

        #[test]
        fn internal_passthrough_lets_user_run() {
            let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
            reset();
            let mut world = World::new();
            world.insert_resource(registry_with(internal_passthrough));
            let e = world.spawn_empty();
            world.insert(e, ChannelMarker);
            world.insert(e, GestureHandler::from_fn(user_handler));
            bubble_dispatch_at(&mut world, &tap_event(e), 0);
            assert_eq!(INTERNAL_FIRES.load(Ordering::SeqCst), 1);
            assert_eq!(
                USER_FIRES.load(Ordering::SeqCst),
                1,
                "internal returned false, user must fire on the same entity",
            );
        }
    }
}