mirui 0.28.2

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
use crate::draw::command::DrawCommand;
use crate::draw::renderer::Renderer;
use crate::ecs::{Entity, World};
use crate::event::gesture::GestureEvent;
use crate::types::{Fixed, Rect};
use crate::widget::ComputedRect;
use crate::widget::dirty::Dirty;
use crate::widget::theme::{ColorToken, ThemedColor};
use crate::widget::view::{View, ViewCtx};

#[derive(Clone, Debug)]
pub enum SliderEvent {
    ValueChanged { new: Fixed, old: Fixed },
    DragStarted,
    DragEnded,
}

pub struct SliderHandler {
    pub on_event: fn(&mut World, Entity, &SliderEvent) -> bool,
}

#[derive(crate::Component)]
pub struct Slider {
    pub value: Fixed,
    pub min: Fixed,
    pub max: Fixed,
    pub track_color: ThemedColor,
    pub fill_color: ThemedColor,
    pub thumb_color: ThemedColor,
}

impl Default for Slider {
    fn default() -> Self {
        Self::new(Fixed::ZERO, Fixed::ONE)
    }
}

impl Slider {
    pub fn new(min: Fixed, max: Fixed) -> Self {
        Self {
            value: min,
            min,
            max,
            track_color: ThemedColor::Token(ColorToken::SurfaceVariant),
            fill_color: ThemedColor::Token(ColorToken::Primary),
            thumb_color: ThemedColor::Token(ColorToken::OnPrimary),
        }
    }

    pub fn with_track_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.track_color = color.into();
        self
    }

    pub fn with_fill_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.fill_color = color.into();
        self
    }

    pub fn with_thumb_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.thumb_color = color.into();
        self
    }

    pub fn ratio(&self) -> Fixed {
        let range = self.max - self.min;
        if range <= Fixed::ZERO {
            return Fixed::ZERO;
        }
        (self.value - self.min) / range
    }

    pub fn set_ratio(&mut self, ratio: Fixed) {
        let clamped = ratio.clamp(Fixed::ZERO, Fixed::ONE);
        self.value = self.min + clamped * (self.max - self.min);
    }

    pub fn build(min: impl Into<Fixed>, max: impl Into<Fixed>) -> SliderBuilder {
        SliderBuilder {
            slider: Slider::new(min.into(), max.into()),
            style: None,
            handler: None,
        }
    }
}

pub struct SliderBuilder {
    slider: Slider,
    style: Option<crate::widget::Style>,
    handler: Option<SliderHandler>,
}

impl SliderBuilder {
    pub fn style(mut self, style: crate::widget::Style) -> Self {
        self.style = Some(style);
        self
    }

    pub fn on_change(mut self, on_event: fn(&mut World, Entity, &SliderEvent) -> bool) -> Self {
        self.handler = Some(SliderHandler { on_event });
        self
    }

    pub fn track_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.slider.track_color = color.into();
        self
    }

    pub fn fill_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.slider.fill_color = color.into();
        self
    }

    pub fn thumb_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.slider.thumb_color = color.into();
        self
    }

    pub fn spawn(self, world: &mut World) -> Entity {
        world.spawn(self)
    }
}

impl crate::ecs::IntoBundle for SliderBuilder {
    fn spawn_into(self, world: &mut World, entity: Entity) {
        world.insert(entity, self.slider);
        if let Some(style) = self.style {
            world.insert(entity, style);
        }
        if let Some(handler) = self.handler {
            world.insert(entity, handler);
        }
    }
}

fn slider_render(
    renderer: &mut dyn Renderer,
    world: &World,
    entity: Entity,
    rect: &Rect,
    ctx: &mut ViewCtx,
) {
    let Some(s) = world.get::<Slider>(entity) else {
        return;
    };
    let theme = ctx.theme(world);
    let track_color = s.track_color.resolve_in(theme, ctx.state);
    let fill_color = s.fill_color.resolve_in(theme, ctx.state);
    let thumb_color = s.thumb_color.resolve_in(theme, ctx.state);
    let ratio = s.ratio();
    let cap_radius = rect.h / Fixed::from_int(2);

    renderer.draw(
        &DrawCommand::Fill {
            area: *rect,
            transform: ctx.transform,
            quad: ctx.quad,
            color: track_color,
            radius: cap_radius,
            opa: 255,
        },
        ctx.clip,
    );

    // Fill bar: full-width capsule with the right side cut off via a
    // narrowed clip rect — preserves the rounded right end at any ratio.
    let ratio_w = rect.w * ratio;
    if ratio_w > Fixed::ZERO {
        let ratio_box = Rect {
            x: rect.x,
            y: rect.y,
            w: ratio_w,
            h: rect.h,
        };
        if let Some(fill_clip) = ctx.clip.intersect(&ratio_box) {
            renderer.draw(
                &DrawCommand::Fill {
                    area: *rect,
                    transform: ctx.transform,
                    quad: ctx.quad,
                    color: fill_color,
                    radius: cap_radius,
                    opa: 255,
                },
                &fill_clip,
            );
        }
    }

    let thumb_size = rect.h;
    let thumb_x = rect.x + ratio * (rect.w - thumb_size);
    renderer.draw(
        &DrawCommand::Fill {
            area: Rect {
                x: thumb_x,
                y: rect.y,
                w: thumb_size,
                h: thumb_size,
            },
            transform: ctx.transform,
            quad: ctx.quad,
            color: thumb_color,
            radius: thumb_size / Fixed::from_int(2),
            opa: 255,
        },
        ctx.clip,
    );
}

pub(crate) fn slider_handler(world: &mut World, entity: Entity, event: &GestureEvent) -> bool {
    match event {
        GestureEvent::DragStart { .. } => {
            emit_slider_event(world, entity, &SliderEvent::DragStarted);
            return true;
        }
        GestureEvent::DragEnd { .. } => {
            emit_slider_event(world, entity, &SliderEvent::DragEnded);
            return true;
        }
        GestureEvent::Tap { .. } | GestureEvent::DragMove { .. } => {}
        _ => return false,
    }

    let x = match event {
        GestureEvent::Tap { x, .. } | GestureEvent::DragMove { x, .. } => *x,
        _ => return false,
    };
    let Some(rect) = world.get::<ComputedRect>(entity).map(|r| r.0) else {
        return false;
    };
    if rect.w <= Fixed::ZERO {
        return false;
    }
    let local = (x - rect.x).max(Fixed::ZERO);
    let ratio = local / rect.w;

    let (old_value, new_value) = {
        let Some(s) = world.get_mut::<Slider>(entity) else {
            return false;
        };
        let old = s.value;
        s.set_ratio(ratio);
        (old, s.value)
    };
    if old_value != new_value {
        emit_slider_event(
            world,
            entity,
            &SliderEvent::ValueChanged {
                new: new_value,
                old: old_value,
            },
        );
    }
    world.insert(entity, Dirty);
    true
}

fn emit_slider_event(world: &mut World, entity: Entity, event: &SliderEvent) {
    let cb = world.get::<SliderHandler>(entity).map(|h| h.on_event);
    if let Some(f) = cb {
        f(world, entity, event);
    }
}

fn slider_attach(world: &mut World, entity: Entity) {
    let _ = world;
    let _ = entity;
}

pub fn view() -> View {
    View::new("Slider", 60, slider_render)
        .with_filter::<Slider>()
        .with_attach(slider_attach)
        .with_internal_gesture(slider_handler)
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::sync::atomic::{AtomicI64, Ordering};
    use std::sync::Mutex;

    static EVENTS: Mutex<Vec<&'static str>> = Mutex::new(Vec::new());
    static LAST_NEW: AtomicI64 = AtomicI64::new(0);
    static LAST_OLD: AtomicI64 = AtomicI64::new(0);
    static SERIAL: Mutex<()> = Mutex::new(());

    fn record_handler(_w: &mut World, _e: Entity, ev: &SliderEvent) -> bool {
        let mut v = EVENTS.lock().unwrap_or_else(|e| e.into_inner());
        match ev {
            SliderEvent::ValueChanged { new, old } => {
                LAST_NEW.store(new.to_int() as i64, Ordering::SeqCst);
                LAST_OLD.store(old.to_int() as i64, Ordering::SeqCst);
                v.push("ValueChanged");
            }
            SliderEvent::DragStarted => v.push("DragStarted"),
            SliderEvent::DragEnded => v.push("DragEnded"),
        }
        true
    }

    fn fresh() -> (World, Entity) {
        let mut world = World::new();
        let e = world.spawn_empty();
        world.insert(e, Slider::new(Fixed::ZERO, Fixed::from_int(100)));
        world.insert(
            e,
            ComputedRect(Rect {
                x: Fixed::ZERO,
                y: Fixed::ZERO,
                w: Fixed::from_int(100),
                h: Fixed::from_int(20),
            }),
        );
        world.insert(
            e,
            SliderHandler {
                on_event: record_handler,
            },
        );
        EVENTS.lock().unwrap_or_else(|x| x.into_inner()).clear();
        LAST_NEW.store(0, Ordering::SeqCst);
        LAST_OLD.store(0, Ordering::SeqCst);
        (world, e)
    }

    fn drain_events() -> Vec<&'static str> {
        EVENTS.lock().unwrap_or_else(|x| x.into_inner()).clone()
    }

    #[test]
    fn tap_emits_value_changed_with_new_old() {
        let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
        let (mut world, e) = fresh();
        let event = GestureEvent::Tap {
            x: Fixed::from_int(50),
            y: Fixed::ZERO,
            target: e,
        };
        slider_handler(&mut world, e, &event);
        assert_eq!(drain_events(), &["ValueChanged"]);
        assert_eq!(LAST_OLD.load(Ordering::SeqCst), 0);
        assert_eq!(LAST_NEW.load(Ordering::SeqCst), 50);
    }

    #[test]
    fn drag_move_emits_value_changed() {
        let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
        let (mut world, e) = fresh();
        let event = GestureEvent::DragMove {
            x: Fixed::from_int(75),
            y: Fixed::ZERO,
            dx: Fixed::ZERO,
            dy: Fixed::ZERO,
            target: e,
        };
        slider_handler(&mut world, e, &event);
        assert_eq!(drain_events(), &["ValueChanged"]);
        assert_eq!(LAST_NEW.load(Ordering::SeqCst), 75);
    }

    #[test]
    fn no_value_change_no_emit() {
        let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
        let (mut world, e) = fresh();
        if let Some(s) = world.get_mut::<Slider>(e) {
            s.value = Fixed::from_int(50);
        }
        let event = GestureEvent::Tap {
            x: Fixed::from_int(50),
            y: Fixed::ZERO,
            target: e,
        };
        slider_handler(&mut world, e, &event);
        assert!(
            drain_events().is_empty(),
            "tapping at the current value must not emit",
        );
    }

    #[test]
    fn drag_start_emits_drag_started() {
        let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
        let (mut world, e) = fresh();
        let event = GestureEvent::DragStart {
            x: Fixed::from_int(40),
            y: Fixed::ZERO,
            target: e,
        };
        slider_handler(&mut world, e, &event);
        assert_eq!(drain_events(), &["DragStarted"]);
    }

    #[test]
    fn drag_end_emits_drag_ended() {
        let _g = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
        let (mut world, e) = fresh();
        let event = GestureEvent::DragEnd {
            x: Fixed::from_int(40),
            y: Fixed::ZERO,
            vx: Fixed::ZERO,
            vy: Fixed::ZERO,
            target: e,
        };
        slider_handler(&mut world, e, &event);
        assert_eq!(drain_events(), &["DragEnded"]);
    }

    #[test]
    fn build_spawns_slider_with_style_and_handler() {
        let mut world = World::new();
        let e = Slider::build(Fixed::ZERO, Fixed::from_int(100))
            .style(crate::widget::Style::default())
            .on_change(record_handler)
            .spawn(&mut world);
        assert_eq!(world.get::<Slider>(e).unwrap().max, Fixed::from_int(100));
        assert!(world.has::<crate::widget::Style>(e));
        assert!(world.has::<SliderHandler>(e));
        assert!(world.has::<crate::widget::Widget>(e));
    }

    #[test]
    fn build_without_handler_omits_it() {
        let mut world = World::new();
        let e = Slider::build(Fixed::ZERO, Fixed::ONE).spawn(&mut world);
        assert!(world.has::<Slider>(e));
        assert!(!world.has::<SliderHandler>(e));
        assert!(!world.has::<crate::widget::Style>(e));
    }
}