rlvgl-widgets 0.2.5

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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! LVGL-parity table widget with shaped-text cells (LPAR-14c).
//!
//! [`Table`] displays a grid of text cells with configurable column widths,
//! per-cell alignment, optional cell spanning, and a keyboard-navigable
//! selected-cell cursor.
//!
//! Text measurement and row-height computation use the LPAR-08 shared
//! measurement primitives (`wrap_greedy_ltr`, `shape_text_ltr`) — no parallel
//! text measurement is introduced.
//!
//! # Navigation
//!
//! Wire the navigate helpers to `ObjectEvent::Key` in a node handler:
//!
//! ```ignore
//! table.navigate_next();      // Key::ArrowRight / Key::Tab
//! table.navigate_prev();      // Key::ArrowLeft  / Key::BackTab
//! table.navigate_up();        // Key::ArrowUp
//! table.navigate_down();      // Key::ArrowDown
//! table.activate_selected();  // Key::Enter
//! ```

use alloc::string::String;
use alloc::vec::Vec;

use rlvgl_core::draw::draw_widget_bg;
use rlvgl_core::event::Event;
use rlvgl_core::font::{FontMetrics, WidgetFont, shape_text_ltr, wrap_greedy_ltr};
use rlvgl_core::renderer::{ClipRenderer, Renderer};
use rlvgl_core::style::Style;
use rlvgl_core::widget::{Color, Rect, Widget};

// ---------------------------------------------------------------------------
// CellCtrl
// ---------------------------------------------------------------------------

/// Bit-flags controlling individual cell appearance and layout.
///
/// Registration policy: **Specification Required**.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CellCtrl(pub u8);

impl CellCtrl {
    /// No special behavior.
    pub const NONE: Self = Self(0);
    /// The cell visually spans into the next column (both widths are consumed).
    pub const MERGE_RIGHT: Self = Self(1 << 0);
    /// Suppress wrapping; crop text to the column width instead.
    pub const TEXT_CROP: Self = Self(1 << 1);

    /// Return `true` when `flag` is set.
    pub fn contains(self, flag: Self) -> bool {
        self.0 & flag.0 != 0
    }
}

impl core::ops::BitOr for CellCtrl {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}

// ---------------------------------------------------------------------------
// CellAlign
// ---------------------------------------------------------------------------

/// Horizontal text alignment within a table cell.
///
/// Registration policy: **Specification Required**.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CellAlign {
    /// Align text to the left edge of the cell.
    #[default]
    Left,
    /// Center text horizontally in the cell.
    Center,
    /// Align text to the right edge of the cell.
    Right,
}

// ---------------------------------------------------------------------------
// Internal cell storage
// ---------------------------------------------------------------------------

#[derive(Default)]
struct Cell {
    text: Option<String>,
    ctrl: CellCtrl,
    align: CellAlign,
}

// ---------------------------------------------------------------------------
// Table
// ---------------------------------------------------------------------------

/// LVGL-parity table widget.
///
/// Stores cells as a flat row-major `Vec`; column widths as a `Vec<i32>`.
/// Row heights are computed dynamically from the cell content using the
/// LPAR-08 shared `wrap_greedy_ltr` measurement.
pub struct Table {
    bounds: Rect,
    rows: usize,
    cols: usize,
    cells: Vec<Cell>,
    col_widths: Vec<i32>,
    /// Row heights computed from content (cached; recomputed on set_bounds).
    row_heights: Vec<i32>,
    selected_row: Option<usize>,
    selected_col: Option<usize>,
    /// Last activated cell poll slot.
    last_activated: Option<(usize, usize)>,
    /// Background and outer border style.
    pub style: Style,
    /// Color for cell background and grid lines.
    pub cell_bg_color: Color,
    /// Color for the selected cell background.
    pub selected_color: Color,
    /// Text color.
    pub text_color: Color,
    /// Grid line color.
    pub grid_color: Color,
    /// Font assignment for this widget (FONT-00 §5); resolves to `FONT_6X10`
    /// when unset.
    font: WidgetFont,
}

impl Table {
    /// Create an empty table at `bounds` with `0` rows and `0` columns.
    pub fn new(bounds: Rect) -> Self {
        Self {
            bounds,
            rows: 0,
            cols: 0,
            cells: Vec::new(),
            col_widths: Vec::new(),
            row_heights: Vec::new(),
            selected_row: None,
            selected_col: None,
            last_activated: None,
            style: Style::default(),
            cell_bg_color: Color(240, 240, 240, 255),
            selected_color: Color(100, 160, 230, 255),
            text_color: Color(30, 30, 30, 255),
            grid_color: Color(180, 180, 180, 255),
            font: WidgetFont::new(),
        }
    }

    /// Assign the font used to render this widget (FONT-00 §5); resolves to
    /// `FONT_6X10` when unset.
    pub fn set_font(&mut self, font: &'static dyn FontMetrics) {
        self.font.set(font);
    }

    // ── Dimensions ────────────────────────────────────────────────────────

    /// Return the number of rows.
    pub fn row_count(&self) -> usize {
        self.rows
    }

    /// Return the number of columns.
    pub fn column_count(&self) -> usize {
        self.cols
    }

    /// Set the number of rows, extending or truncating the cell vector.
    pub fn set_row_count(&mut self, rows: usize) {
        self.cells.resize_with(rows * self.cols, Cell::default);
        self.rows = rows;
        self.recompute_row_heights();
    }

    /// Set the number of columns, extending or truncating; preserves existing
    /// cell content where indices overlap.
    pub fn set_column_count(&mut self, cols: usize) {
        if cols == self.cols {
            return;
        }
        let mut new_cells: Vec<Cell> = Vec::with_capacity(self.rows * cols);
        for r in 0..self.rows {
            for c in 0..cols {
                let cell = if c < self.cols {
                    let old_idx = r * self.cols + c;
                    let old = &self.cells[old_idx];
                    Cell {
                        text: old.text.clone(),
                        ctrl: old.ctrl,
                        align: old.align,
                    }
                } else {
                    Cell::default()
                };
                new_cells.push(cell);
            }
        }
        self.cells = new_cells;
        self.col_widths.resize(cols, 0);
        self.cols = cols;
        self.recompute_row_heights();
    }

    // ── Cell accessors ────────────────────────────────────────────────────

    fn cell_index(&self, row: usize, col: usize) -> Option<usize> {
        if row < self.rows && col < self.cols {
            Some(row * self.cols + col)
        } else {
            None
        }
    }

    /// Set the text value of a cell.
    ///
    /// Out-of-bounds indices are silently ignored (matching LVGL behavior).
    pub fn set_cell_value(&mut self, row: usize, col: usize, text: &str) {
        if let Some(idx) = self.cell_index(row, col) {
            self.cells[idx].text = Some(String::from(text));
            self.recompute_row_heights();
        }
    }

    /// Return the text value of a cell, or `None` if empty or out of bounds.
    pub fn cell_value(&self, row: usize, col: usize) -> Option<&str> {
        self.cell_index(row, col)
            .and_then(|idx| self.cells[idx].text.as_deref())
    }

    /// Set control flags for a cell.
    pub fn set_cell_ctrl(&mut self, row: usize, col: usize, ctrl: CellCtrl) {
        if let Some(idx) = self.cell_index(row, col) {
            self.cells[idx].ctrl = ctrl;
        }
    }

    /// Return control flags for a cell.
    pub fn cell_ctrl(&self, row: usize, col: usize) -> CellCtrl {
        self.cell_index(row, col)
            .map(|idx| self.cells[idx].ctrl)
            .unwrap_or(CellCtrl::NONE)
    }

    /// Set horizontal alignment for a cell.
    pub fn set_cell_align(&mut self, row: usize, col: usize, align: CellAlign) {
        if let Some(idx) = self.cell_index(row, col) {
            self.cells[idx].align = align;
        }
    }

    // ── Column widths ─────────────────────────────────────────────────────

    /// Set the pixel width of column `col`.
    ///
    /// A width of `0` means "auto" — the column participates in the remaining
    /// width distribution.
    pub fn set_column_width(&mut self, col: usize, width: i32) {
        if col < self.cols {
            self.col_widths[col] = width;
        }
    }

    /// Return the stored column width (0 = auto).
    pub fn column_width(&self, col: usize) -> i32 {
        self.col_widths.get(col).copied().unwrap_or(0)
    }

    /// Resolve effective column widths, distributing remaining space evenly
    /// among auto columns.
    fn resolved_col_widths(&self) -> Vec<i32> {
        if self.cols == 0 {
            return Vec::new();
        }
        let mut widths = self.col_widths.clone();
        widths.resize(self.cols, 0);

        let explicit_total: i32 = widths.iter().map(|&w| w.max(0)).sum();
        let auto_count = widths.iter().filter(|&&w| w == 0).count();
        let remaining = (self.bounds.width - explicit_total).max(0);
        let auto_w = if auto_count > 0 {
            remaining / auto_count as i32
        } else {
            0
        };

        for w in &mut widths {
            if *w == 0 {
                *w = auto_w;
            }
        }
        widths
    }

    // ── Row height computation ─────────────────────────────────────────────

    /// Recompute row heights using the LPAR-08 shared `wrap_greedy_ltr`.
    ///
    /// Each row's height is the maximum wrapped height across all cells in that
    /// row. This is the load-bearing use of the shared font measurement.
    fn recompute_row_heights(&mut self) {
        let font: &dyn FontMetrics = self.font.resolve();
        let metrics = font.line_metrics();
        let col_widths = self.resolved_col_widths();

        self.row_heights.resize(self.rows, 0);
        for r in 0..self.rows {
            let mut max_h = metrics.line_height as i32;
            for c in 0..self.cols {
                let idx = r * self.cols + c;
                let cell = &self.cells[idx];
                let Some(ref text) = cell.text else {
                    continue;
                };
                let col_w = col_widths.get(c).copied().unwrap_or(0).max(1);
                // Use the LPAR-08 shared greedy-wrap — not per-cell advance arithmetic.
                let wrap_w = if cell.ctrl.contains(CellCtrl::TEXT_CROP) {
                    i32::MAX // no wrapping; measure single line
                } else {
                    col_w
                };
                let wrapped = wrap_greedy_ltr(font, text, wrap_w, 0, 0);
                let h = wrapped.used_height.max(metrics.line_height as i32);
                if h > max_h {
                    max_h = h;
                }
            }
            self.row_heights[r] = max_h;
        }
    }

    // ── Selection ─────────────────────────────────────────────────────────

    /// Set the selected cell. Out-of-bounds is silently ignored.
    pub fn set_selected_cell(&mut self, row: usize, col: usize) {
        if row < self.rows && col < self.cols {
            self.selected_row = Some(row);
            self.selected_col = Some(col);
        }
    }

    /// Return the selected cell `(row, col)`, or `None` if nothing is selected.
    pub fn selected_cell(&self) -> Option<(usize, usize)> {
        match (self.selected_row, self.selected_col) {
            (Some(r), Some(c)) => Some((r, c)),
            _ => None,
        }
    }

    /// Move selection to the next cell (right then down; wraps at the last cell
    /// to the first).
    ///
    /// Wire to `ObjectEvent::Key(Key::ArrowRight)` or `Key::Tab` in a node handler.
    pub fn navigate_next(&mut self) {
        if self.rows == 0 || self.cols == 0 {
            return;
        }
        let (r, c) = self.selected_cell().unwrap_or((0, self.cols - 1));
        let (nr, nc) = if c + 1 < self.cols {
            (r, c + 1)
        } else if r + 1 < self.rows {
            (r + 1, 0)
        } else {
            (0, 0)
        };
        self.selected_row = Some(nr);
        self.selected_col = Some(nc);
    }

    /// Move selection to the previous cell (left then up; wraps at the first
    /// cell to the last).
    ///
    /// Wire to `ObjectEvent::Key(Key::ArrowLeft)` or `Key::BackTab`.
    pub fn navigate_prev(&mut self) {
        if self.rows == 0 || self.cols == 0 {
            return;
        }
        let (r, c) = self.selected_cell().unwrap_or((0, 0));
        let (nr, nc) = if c > 0 {
            (r, c - 1)
        } else if r > 0 {
            (r - 1, self.cols - 1)
        } else {
            (self.rows - 1, self.cols - 1)
        };
        self.selected_row = Some(nr);
        self.selected_col = Some(nc);
    }

    /// Move selection one row up.
    ///
    /// Wire to `ObjectEvent::Key(Key::ArrowUp)` in a node handler.
    pub fn navigate_up(&mut self) {
        if self.rows == 0 {
            return;
        }
        if let Some(r) = self.selected_row {
            if r > 0 {
                self.selected_row = Some(r - 1);
            }
        } else {
            self.selected_row = Some(self.rows - 1);
            self.selected_col = Some(0);
        }
    }

    /// Move selection one row down.
    ///
    /// Wire to `ObjectEvent::Key(Key::ArrowDown)` in a node handler.
    pub fn navigate_down(&mut self) {
        if self.rows == 0 {
            return;
        }
        if let Some(r) = self.selected_row {
            if r + 1 < self.rows {
                self.selected_row = Some(r + 1);
            }
        } else {
            self.selected_row = Some(0);
            self.selected_col = Some(0);
        }
    }

    /// Activate the selected cell and record it in the poll slot.
    ///
    /// Wire to `ObjectEvent::Key(Key::Enter)` in a node handler.
    pub fn activate_selected(&mut self) {
        if let Some(cell) = self.selected_cell() {
            self.last_activated = Some(cell);
        }
    }

    /// Drain the activation poll slot.
    pub fn last_activated(&mut self) -> Option<(usize, usize)> {
        self.last_activated.take()
    }

    // ── Internal draw helpers ─────────────────────────────────────────────

    fn draw_cells(&self, renderer: &mut dyn Renderer) {
        let font: &dyn FontMetrics = self.font.resolve();
        let metrics = font.line_metrics();
        let col_widths = self.resolved_col_widths();

        let mut y = self.bounds.y;
        for r in 0..self.rows {
            let row_h = self
                .row_heights
                .get(r)
                .copied()
                .unwrap_or(metrics.line_height as i32);
            let mut x = self.bounds.x;

            for c in 0..self.cols {
                let idx = r * self.cols + c;
                let cell = &self.cells[idx];
                let mut cell_w = col_widths.get(c).copied().unwrap_or(0);

                // Span: consume the next column's width too.
                if cell.ctrl.contains(CellCtrl::MERGE_RIGHT) && c + 1 < self.cols {
                    cell_w += col_widths.get(c + 1).copied().unwrap_or(0);
                }

                let cell_rect = Rect {
                    x,
                    y,
                    width: cell_w.max(0),
                    height: row_h,
                };

                // Background
                let is_selected = self.selected_row == Some(r) && self.selected_col == Some(c);
                let bg = if is_selected {
                    self.selected_color
                } else {
                    self.cell_bg_color
                };
                renderer.fill_rect(cell_rect, bg.with_alpha(self.style.alpha));

                // Grid border (right + bottom edges)
                renderer.fill_rect(
                    Rect {
                        x: cell_rect.x + cell_rect.width,
                        y: cell_rect.y,
                        width: 1,
                        height: cell_rect.height,
                    },
                    self.grid_color.with_alpha(self.style.alpha),
                );
                renderer.fill_rect(
                    Rect {
                        x: cell_rect.x,
                        y: cell_rect.y + cell_rect.height,
                        width: cell_rect.width + 1,
                        height: 1,
                    },
                    self.grid_color.with_alpha(self.style.alpha),
                );

                // Text
                if let Some(ref text) = cell.text
                    && cell_rect.width > 0
                    && cell_rect.height > 0
                {
                    let text_color = self.text_color.with_alpha(self.style.alpha);
                    let baseline_y = cell_rect.y + metrics.ascent as i32 + 2;

                    if cell.ctrl.contains(CellCtrl::TEXT_CROP) {
                        // Single-line crop: shape once and draw with clip.
                        let shaped = shape_text_ltr(font, text, (cell_rect.x + 2, baseline_y), 0);
                        let mut clipped = ClipRenderer::new(renderer, cell_rect);
                        clipped.draw_text_shaped(&shaped, (0, 0), text_color);
                    } else {
                        // Wrapped text via the SHARED `wrap_greedy_ltr` measurement.
                        let wrap_w = cell_rect.width.max(1) - 4;
                        let wrapped = wrap_greedy_ltr(font, text, wrap_w, 0, 0);
                        let mut clipped = ClipRenderer::new(renderer, cell_rect);
                        let mut line_y = baseline_y;
                        for line in &wrapped.lines {
                            let line_text = &text[line.start..line.end];
                            let line_advance_px = (line.advance_fp16 + 8) >> 4;

                            let x_off = match cell.align {
                                CellAlign::Left => cell_rect.x + 2,
                                CellAlign::Center => {
                                    cell_rect.x + (cell_rect.width - line_advance_px) / 2
                                }
                                CellAlign::Right => {
                                    cell_rect.x + cell_rect.width - line_advance_px - 2
                                }
                            };
                            let shaped = shape_text_ltr(font, line_text, (x_off, line_y), 0);
                            clipped.draw_text_shaped(&shaped, (0, 0), text_color);
                            line_y += metrics.line_height as i32;
                        }
                    }
                }

                // Advance x; if MERGE_RIGHT skip the next column.
                x += cell_w;
                if cell.ctrl.contains(CellCtrl::MERGE_RIGHT) {
                    // We already consumed c+1's width above; skip that column's
                    // own draw by bumping x past it (it will just draw the
                    // already-covered area — the simpler approach is to handle
                    // it in the outer loop via a skip flag, but for v1 we draw
                    // it invisibly: its rect is zero-width because x consumed
                    // its width).
                }
            }
            y += row_h + 1; // +1 for grid line
        }
    }
}

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

    fn widget_font_mut(&mut self) -> Option<&mut WidgetFont> {
        Some(&mut self.font)
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.bounds = bounds;
        self.recompute_row_heights();
    }

    fn draw(&self, renderer: &mut dyn Renderer) {
        // Part::MAIN — background
        draw_widget_bg(renderer, self.bounds, &self.style);
        // Part::ITEMS + Part::SELECTED — cells
        self.draw_cells(renderer);
    }

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

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn r(x: i32, y: i32, w: i32, h: i32) -> Rect {
        Rect {
            x,
            y,
            width: w,
            height: h,
        }
    }

    #[test]
    fn set_size_and_cell_value() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(3);
        t.set_column_count(4);
        assert_eq!(t.row_count(), 3);
        assert_eq!(t.column_count(), 4);

        t.set_cell_value(1, 2, "hello");
        assert_eq!(t.cell_value(1, 2), Some("hello"));
        assert_eq!(t.cell_value(0, 0), None);
    }

    #[test]
    fn out_of_bounds_cell_is_noop() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(2);
        t.set_column_count(2);
        t.set_cell_value(5, 5, "oob"); // no-op
        assert_eq!(t.cell_value(5, 5), None);
    }

    #[test]
    fn column_widths_stored() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_column_count(3);
        t.set_column_width(1, 60);
        assert_eq!(t.column_width(1), 60);
        assert_eq!(t.column_width(0), 0); // auto
    }

    #[test]
    fn cell_align_stored() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(1);
        t.set_column_count(2);
        t.set_cell_align(0, 1, CellAlign::Center);
        let idx = 1; // row 0, col 1 in a 2-col table
        assert_eq!(t.cells[idx].align, CellAlign::Center);
    }

    #[test]
    fn merge_right_ctrl_flag() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(1);
        t.set_column_count(3);
        t.set_cell_ctrl(0, 0, CellCtrl::MERGE_RIGHT);
        assert!(t.cell_ctrl(0, 0).contains(CellCtrl::MERGE_RIGHT));
        assert!(!t.cell_ctrl(0, 1).contains(CellCtrl::MERGE_RIGHT));
    }

    #[test]
    fn navigate_next_wraps() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(2);
        t.set_column_count(2);
        t.set_selected_cell(0, 0);

        t.navigate_next();
        assert_eq!(t.selected_cell(), Some((0, 1)));
        t.navigate_next(); // end of row 0 → row 1, col 0
        assert_eq!(t.selected_cell(), Some((1, 0)));
        t.navigate_next();
        assert_eq!(t.selected_cell(), Some((1, 1)));
        t.navigate_next(); // wrap to start
        assert_eq!(t.selected_cell(), Some((0, 0)));
    }

    #[test]
    fn navigate_prev_wraps() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(2);
        t.set_column_count(2);
        t.set_selected_cell(0, 0);
        t.navigate_prev(); // wrap to last cell
        assert_eq!(t.selected_cell(), Some((1, 1)));
    }

    #[test]
    fn navigate_up_down() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(3);
        t.set_column_count(2);
        t.set_selected_cell(1, 0);
        t.navigate_up();
        assert_eq!(t.selected_cell(), Some((0, 0)));
        t.navigate_down();
        assert_eq!(t.selected_cell(), Some((1, 0)));
    }

    #[test]
    fn activate_selected_sets_poll_slot() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(2);
        t.set_column_count(2);
        t.set_selected_cell(1, 1);
        t.activate_selected();
        assert_eq!(t.last_activated(), Some((1, 1)));
        assert_eq!(t.last_activated(), None); // drained
    }

    #[test]
    fn resize_recomputes_row_heights() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(2);
        t.set_column_count(2);
        t.set_cell_value(0, 0, "Hello world this is a long line");
        t.set_bounds(r(0, 0, 300, 150));
        assert_eq!(t.bounds(), r(0, 0, 300, 150));
        // Row heights are recomputed — just ensure they are non-zero.
        assert!(!t.row_heights.is_empty());
        assert!(t.row_heights[0] > 0);
    }

    #[test]
    fn column_count_change_preserves_existing_cells() {
        let mut t = Table::new(r(0, 0, 200, 100));
        t.set_row_count(1);
        t.set_column_count(3);
        t.set_cell_value(0, 1, "keep");
        t.set_column_count(5);
        assert_eq!(t.cell_value(0, 1), Some("keep"));
        assert_eq!(t.column_count(), 5);
    }
}