rlvgl-widgets 0.2.4

Built-in widgets for rlvgl.
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
// SPDX-License-Identifier: MIT
//! REND-00 §12 acceptance: spike (unclipped tree path documented),
//! edge-crop goldens, the driving-case scroll-reveal, and the dirty-rect
//! contract.

use std::cell::RefCell;
use std::rc::Rc;

use rlvgl_core::event::Event;
use rlvgl_core::font::{FontLineMetrics, FontMetrics, GlyphInfo, shape_text_ltr};
use rlvgl_core::renderer::Renderer;
use rlvgl_core::style::StyleBuilder;
use rlvgl_core::widget::{Color, Rect, Widget};
use rlvgl_widgets::scroll_view::ScrollView;

const W: usize = 480;
const H: usize = 800;
const BG: u32 = 0xFF10_1018;

/// Headless ARGB8888 buffer renderer (the "D-dump" surface).
struct Buffer {
    pixels: Vec<u32>,
}

impl Buffer {
    fn new() -> Self {
        Self {
            pixels: vec![BG; W * H],
        }
    }
    fn at(&self, x: i32, y: i32) -> u32 {
        self.pixels[y as usize * W + x as usize]
    }
}

impl Renderer for Buffer {
    fn fill_rect(&mut self, rect: Rect, color: Color) {
        let argb = color.to_argb8888();
        for y in rect.y.max(0)..(rect.y + rect.height).min(H as i32) {
            for x in rect.x.max(0)..(rect.x + rect.width).min(W as i32) {
                self.pixels[y as usize * W + x as usize] = argb;
            }
        }
    }
    fn draw_text(&mut self, _position: (i32, i32), _text: &str, _color: Color) {}
}

/// A plain colored cell widget positioned in whatever space its parent uses.
struct Cell {
    bounds: Rect,
    color: Color,
}

impl Cell {
    fn new(bounds: Rect, color: Color) -> Rc<RefCell<Self>> {
        Rc::new(RefCell::new(Self { bounds, color }))
    }
}

impl Widget for Cell {
    fn bounds(&self) -> Rect {
        self.bounds
    }
    fn draw(&self, renderer: &mut dyn Renderer) {
        renderer.fill_rect(self.bounds, self.color);
    }
    fn handle_event(&mut self, _event: &Event) -> bool {
        false
    }
}

/// Distinct opaque color per row index so bleed/missing rows are visible.
fn row_color(row: i32) -> Color {
    Color(40 + row as u8 * 30, 80, 200 - row as u8 * 25, 255)
}

struct ProbeFont;

impl FontMetrics for ProbeFont {
    fn glyph_metrics(&self, ch: char) -> Option<GlyphInfo> {
        (ch == 'G').then_some(GlyphInfo {
            advance_fp16: 16,
            bearing_x: 0,
            bearing_y: 4,
            width: 4,
            height: 4,
        })
    }

    fn line_metrics(&self) -> FontLineMetrics {
        FontLineMetrics {
            line_height: 4,
            ascent: 4,
            descent: 0,
        }
    }

    fn glyph_coverage_row(&self, _ch: char, row: u16, x_offset: u16, coverage: &mut [u8]) -> bool {
        if row >= 4 {
            return false;
        }

        for (idx, alpha) in coverage.iter_mut().enumerate() {
            let x = x_offset + u16::try_from(idx).unwrap_or(u16::MAX);
            *alpha = match x {
                1 | 2 => 255,
                _ => 0,
            };
        }
        true
    }
}

struct ShapedTextProbe {
    bounds: Rect,
    color: Color,
}

impl ShapedTextProbe {
    fn new(bounds: Rect, color: Color) -> Rc<RefCell<Self>> {
        Rc::new(RefCell::new(Self { bounds, color }))
    }
}

impl Widget for ShapedTextProbe {
    fn bounds(&self) -> Rect {
        self.bounds
    }

    fn draw(&self, renderer: &mut dyn Renderer) {
        let font = ProbeFont;
        let baseline = self.bounds.y + font.line_metrics().ascent as i32;
        let shaped = shape_text_ltr(&font, "G", (self.bounds.x, baseline), 0);
        renderer.draw_text_shaped(&shaped, (0, 0), self.color);
    }

    fn handle_event(&mut self, _event: &Event) -> bool {
        false
    }
}

// ───────────────────────────────────────────────────────────────────────────
// Spike (REND-00 §12, deliverable 1)
// ───────────────────────────────────────────────────────────────────────────

/// Documents the gap REND closes: on the plain widget-tree draw path a
/// child straddling its parent's edge renders fully — nothing clips it.
/// (If this test ever fails, clipping appeared somewhere else; re-read
/// REND-00 §2 before touching it.)
#[test]
fn spike_unclipped_tree_path_bleeds_past_parent_bounds() {
    let parent = Rect {
        x: 100,
        y: 100,
        width: 200,
        height: 200,
    };
    // Child overhangs the parent's bottom edge by 100 px.
    let child = Cell::new(
        Rect {
            x: 120,
            y: 250,
            width: 50,
            height: 100,
        },
        Color(255, 0, 0, 255),
    );

    let mut frame = Buffer::new();
    // Parent background, then child — the bare tree path, no ScrollView.
    frame.fill_rect(parent, Color(0, 0, 0, 255));
    child.borrow().draw(&mut frame);

    // The child painted below the parent's bottom edge (y >= 300): bleed.
    assert_eq!(
        frame.at(130, 320),
        Color(255, 0, 0, 255).to_argb8888(),
        "spike: child bleeds past the parent edge on the unclipped path"
    );
}

#[test]
fn text_coverage_is_clipped_by_scroll_viewport_at_top_and_bottom() {
    let viewport = Rect {
        x: 10,
        y: 10,
        width: 20,
        height: 20,
    };
    let mut view = ScrollView::new(viewport, 100);
    view.style = StyleBuilder::new().bg_color(Color(0, 0, 0, 255)).build();

    // Text runs begin at y=-1 and y=19 in content space, each glyph is 4px tall.
    // With the viewport origin at y=10 and no scroll offset, both glyphs
    // cross viewport edges by one pixel.
    view.add_child(ShapedTextProbe::new(
        Rect {
            x: 2,
            y: -1,
            width: 4,
            height: 4,
        },
        Color(255, 0, 0, 255),
    ));
    view.add_child(ShapedTextProbe::new(
        Rect {
            x: 2,
            y: 19,
            width: 4,
            height: 4,
        },
        Color(255, 0, 0, 255),
    ));

    let mut frame = Buffer::new();
    view.draw(&mut frame);

    let visible_red_x = viewport.x + 4;
    // Above and below the viewport: no bleed from straddling glyph rows.
    assert_eq!(
        frame.at(viewport.x + 3, viewport.y - 1),
        BG,
        "top overhang is clipped to viewport"
    );
    assert_eq!(
        frame.at(viewport.x + 3, viewport.y + viewport.height),
        BG,
        "bottom overhang is clipped to viewport"
    );

    // Interior clipping boundaries are honored and glyph coverage reaches both
    // top and bottom edges.
    let red = Color(255, 0, 0, 255);
    let red_argb = red.to_argb8888();
    let view_bg = Color(0, 0, 0, 255).to_argb8888();
    assert_eq!(
        frame.at(visible_red_x, viewport.y),
        red_argb,
        "top-straddling glyph contributes at first visible row"
    );
    assert_eq!(
        frame.at(visible_red_x, viewport.y + viewport.height - 1),
        red_argb,
        "bottom-straddling glyph contributes at last visible row"
    );
    assert_eq!(
        frame.at(visible_red_x - 2, viewport.y),
        view_bg,
        "coverage mask leaves first column untouched"
    );
    assert_eq!(
        frame.at(visible_red_x + 2, viewport.y),
        view_bg,
        "coverage mask leaves last column untouched"
    );
}

// ───────────────────────────────────────────────────────────────────────────
// Edge crops (acceptance a)
// ───────────────────────────────────────────────────────────────────────────

#[test]
fn children_crop_exactly_at_all_four_viewport_edges() {
    let viewport = Rect {
        x: 100,
        y: 100,
        width: 200,
        height: 200,
    };
    let mut view = ScrollView::new(viewport, 200);
    view.style = StyleBuilder::new().bg_color(Color(0, 0, 0, 255)).build();
    let red = Color(255, 0, 0, 255);
    // Content space: viewport interior spans (0..200, 0..200).
    for bounds in [
        Rect {
            x: -50,
            y: 80,
            width: 100,
            height: 40,
        }, // left overhang
        Rect {
            x: 150,
            y: 80,
            width: 100,
            height: 40,
        }, // right overhang
        Rect {
            x: 20,
            y: -30,
            width: 40,
            height: 60,
        }, // top overhang
        Rect {
            x: 20,
            y: 170,
            width: 40,
            height: 60,
        }, // bottom overhang
    ] {
        view.add_child(Cell::new(bounds, red));
    }

    let mut frame = Buffer::new();
    view.draw(&mut frame);

    let inside = |x: i32, y: i32| {
        x >= viewport.x
            && x < viewport.x + viewport.width
            && y >= viewport.y
            && y < viewport.y + viewport.height
    };
    let red_argb = red.to_argb8888();
    // No bleed: every red pixel in the frame is inside the viewport.
    for y in 0..H as i32 {
        for x in 0..W as i32 {
            if frame.at(x, y) == red_argb {
                assert!(inside(x, y), "bleed at ({x}, {y})");
            }
        }
    }
    // No missing interior: the visible part of each overhang is painted
    // right up to the edge.
    assert_eq!(frame.at(100, 190), red_argb, "left edge interior");
    assert_eq!(frame.at(299, 190), red_argb, "right edge interior");
    assert_eq!(frame.at(130, 100), red_argb, "top edge interior");
    assert_eq!(frame.at(130, 299), red_argb, "bottom edge interior");
    // And the first pixel past each edge is untouched background-of-frame.
    assert_eq!(frame.at(99, 190), BG, "no bleed past left");
    assert_eq!(frame.at(300, 190), BG, "no bleed past right");
    assert_eq!(frame.at(130, 99), BG, "no bleed past top");
    assert_eq!(frame.at(130, 300), BG, "no bleed past bottom");
}

// ───────────────────────────────────────────────────────────────────────────
// Driving case: 2-column grid of 200 px cells, scroll reveals a row
// (acceptance b)
// ───────────────────────────────────────────────────────────────────────────

/// Build the ticket's driving-case view: 5 rows × 2 columns of 200×200
/// cells (content 1000 px) behind a 400×500 viewport.
fn driving_case_view() -> (ScrollView, Rect) {
    let viewport = Rect {
        x: 40,
        y: 100,
        width: 400,
        height: 500,
    };
    let mut view = ScrollView::new(viewport, 1000);
    view.style = StyleBuilder::new().bg_color(Color(0, 0, 0, 255)).build();
    for row in 0..5 {
        for col in 0..2 {
            view.add_child(Cell::new(
                Rect {
                    x: col * 200,
                    y: row * 200,
                    width: 200,
                    height: 200,
                },
                row_color(row),
            ));
        }
    }
    (view, viewport)
}

/// Reference render: directly paint each cell's viewport intersection.
fn reference_frame(viewport: Rect, scroll_y: i32) -> Buffer {
    let mut frame = Buffer::new();
    frame.fill_rect(viewport, Color(0, 0, 0, 255));
    for row in 0..5 {
        for col in 0..2 {
            let screen = Rect {
                x: viewport.x + col * 200,
                y: viewport.y + row * 200 - scroll_y,
                width: 200,
                height: 200,
            };
            if let Some(visible) = screen.intersect(viewport) {
                frame.fill_rect(visible, row_color(row));
            }
        }
    }
    frame
}

#[test]
fn scroll_reveals_hidden_row_with_clean_partial_rows() {
    let (mut view, viewport) = driving_case_view();

    // Unscrolled: rows 0-1 fully visible, row 2 half visible, rows 3-4
    // hidden. Byte-identical to the reference.
    let mut frame = Buffer::new();
    view.draw(&mut frame);
    assert_eq!(frame.pixels, reference_frame(viewport, 0).pixels);
    assert_eq!(
        frame.at(viewport.x + 10, viewport.y + 499),
        row_color(2).to_argb8888(),
        "row 2 partially visible at the bottom edge"
    );

    // Scroll 350 px: row 1 now straddles the top edge, row 3 (previously
    // fully hidden) is revealed straddling the bottom edge.
    view.scroll_by(350);
    let mut frame = Buffer::new();
    view.draw(&mut frame);
    assert_eq!(frame.pixels, reference_frame(viewport, 350).pixels);

    let top_row_color = frame.at(viewport.x + 10, viewport.y);
    assert_eq!(
        top_row_color,
        row_color(1).to_argb8888(),
        "partial row 1 at top"
    );
    let bottom_row_color = frame.at(viewport.x + 10, viewport.y + 499);
    assert_eq!(
        bottom_row_color,
        row_color(4).to_argb8888(),
        "row 4 revealed at the bottom edge (350 + 500 = 850 ∈ row 4)"
    );
    // Clean edges: nothing painted outside the viewport.
    assert_eq!(frame.at(viewport.x + 10, viewport.y - 1), BG);
    assert_eq!(frame.at(viewport.x + 10, viewport.y + 500), BG);

    // scroll_to(absolute) renders byte-identically to the scroll_by chain.
    let (mut absolute, _) = driving_case_view();
    absolute.scroll_to(350);
    let mut frame_abs = Buffer::new();
    absolute.draw(&mut frame_abs);
    assert_eq!(frame.pixels, frame_abs.pixels);
}

#[test]
fn scroll_clamps_to_content_extent() {
    let (mut view, viewport) = driving_case_view();
    assert_eq!(view.max_scroll(), 500);
    view.scroll_by(10_000);
    assert_eq!(view.scroll_y(), 500);
    let mut frame = Buffer::new();
    view.draw(&mut frame);
    assert_eq!(frame.pixels, reference_frame(viewport, 500).pixels);
    view.scroll_by(-10_000);
    assert_eq!(view.scroll_y(), 0);
}

// ───────────────────────────────────────────────────────────────────────────
// Dirty-rect contract (acceptance c)
// ───────────────────────────────────────────────────────────────────────────

#[test]
fn take_dirty_reports_viewport_only_on_offset_change() {
    let (mut view, viewport) = driving_case_view();
    assert_eq!(view.take_dirty(), None, "clean at construction");

    view.scroll_by(100);
    assert_eq!(
        view.take_dirty(),
        Some(viewport),
        "scroll dirties the viewport"
    );
    assert_eq!(view.take_dirty(), None, "drained");

    view.scroll_to(100);
    assert_eq!(view.take_dirty(), None, "same offset is a no-op");
    view.scroll_by(0);
    assert_eq!(view.take_dirty(), None, "zero delta is a no-op");

    view.scroll_to(500);
    view.scroll_by(10_000);
    assert_eq!(
        view.take_dirty(),
        Some(viewport),
        "one viewport rect per scroll burst"
    );
    view.scroll_by(1); // clamped at max: offset unchanged
    assert_eq!(view.take_dirty(), None, "clamped overshoot is a no-op");
}

// ───────────────────────────────────────────────────────────────────────────
// Event translation (REND-00 §6.7)
// ───────────────────────────────────────────────────────────────────────────

struct EventLog {
    bounds: Rect,
    seen: Vec<Event>,
}

impl Widget for EventLog {
    fn bounds(&self) -> Rect {
        self.bounds
    }
    fn draw(&self, _renderer: &mut dyn Renderer) {}
    fn handle_event(&mut self, event: &Event) -> bool {
        self.seen.push(event.clone());
        false
    }
}

#[test]
fn pointer_events_translate_to_content_space_and_gate_on_viewport() {
    let viewport = Rect {
        x: 100,
        y: 100,
        width: 200,
        height: 200,
    };
    let child = Rc::new(RefCell::new(EventLog {
        bounds: Rect {
            x: 0,
            y: 0,
            width: 200,
            height: 1000,
        },
        seen: Vec::new(),
    }));
    let mut view = ScrollView::new(viewport, 1000);
    view.add_child(child.clone());
    view.scroll_by(300);

    // Inside the viewport: translated into content space (+scroll).
    view.handle_event(&Event::PressRelease { x: 150, y: 180 });
    // Outside the viewport: never delivered.
    view.handle_event(&Event::PressRelease { x: 10, y: 10 });
    // Non-pointer events pass through unchanged.
    view.handle_event(&Event::Tick);

    let seen = &child.borrow().seen;
    assert_eq!(
        seen.as_slice(),
        &[Event::PressRelease { x: 50, y: 380 }, Event::Tick,]
    );
}