oxiui-table 0.1.1

Virtualized table widget with egui and iced backends for OxiUI
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
//! Tests for `CumulativeHeightCache`, `RowCache`, and virtual column rendering.

use oxiui_table::{Cell, ColumnDef, CumulativeHeightCache, RowCache, RowSource, Table};

// ── CumulativeHeightCache tests ───────────────────────────────────────────────

#[test]
fn test_cumulative_heights_basic() {
    let mut cache = CumulativeHeightCache::new();
    cache.set_heights(vec![20.0, 40.0, 30.0]);

    assert_eq!(cache.row_at_offset(0.0), 0, "y=0 → row 0");
    assert_eq!(cache.row_at_offset(19.9), 0, "y=19.9 → row 0");
    assert_eq!(cache.row_at_offset(20.0), 1, "y=20.0 → row 1 starts");
    assert_eq!(cache.row_at_offset(50.0), 1, "y=50.0 → still row 1");
    assert_eq!(cache.row_at_offset(60.0), 2, "y=60.0 → row 2 starts");
    assert!(
        (cache.total_height() - 90.0).abs() < f32::EPSILON,
        "total height = 20+40+30 = 90"
    );
}

#[test]
fn test_visible_range() {
    // Heights [20, 40, 30] → prefix sums [0, 20, 60, 90].
    // viewport_y=30 (inside row 1), viewport_height=50 → end_y=80 (inside row 2).
    // Should cover rows 1 and 2 → 1..3.
    let mut cache = CumulativeHeightCache::new();
    cache.set_heights(vec![20.0, 40.0, 30.0]);

    let range = cache.visible_range(30.0, 50.0);
    assert_eq!(range, 1..3, "visible_range(30, 50) should be 1..3");
}

#[test]
fn test_binary_search_10k_rows() {
    // 10,000 rows each 25 px tall → total height = 250,000 px.
    // Last row starts at 249,975.0.
    let mut cache = CumulativeHeightCache::new();
    cache.set_uniform_height(10_000, 25.0);

    // One pixel before the very end should land in the last row (9999).
    let last_row = cache.row_at_offset(10_000_f32 * 25.0 - 1.0);
    assert_eq!(last_row, 9999, "near-end offset → last row 9999");

    assert_eq!(cache.row_at_offset(0.0), 0, "offset 0 → row 0");
}

#[test]
fn test_variable_row_height_visible_range() {
    // Heights [20, 40, 30] → prefix sums [0, 20, 60, 90].
    // viewport_y=25 (inside row 1, which spans 20..60),
    // viewport_height=30 → end_y=55 (still inside row 1 since row 1 ends at 60).
    // visible_range should at least contain row 1.
    let mut cache = CumulativeHeightCache::new();
    cache.set_heights(vec![20.0, 40.0, 30.0]);

    let range = cache.visible_range(25.0, 30.0);
    assert!(range.contains(&1), "range {range:?} must contain row 1");
}

#[test]
fn test_row_y_range() {
    let mut cache = CumulativeHeightCache::new();
    cache.set_heights(vec![20.0, 40.0, 30.0]);

    let (start, end) = cache.row_y_range(1);
    assert!((start - 20.0).abs() < f32::EPSILON, "row 1 starts at y=20");
    assert!((end - 60.0).abs() < f32::EPSILON, "row 1 ends at y=60");
}

#[test]
fn test_total_height_empty_cache() {
    let mut cache = CumulativeHeightCache::new();
    assert!(
        (cache.total_height() - 0.0).abs() < f32::EPSILON,
        "empty cache → total_height=0"
    );
}

// ── RowCache tests ────────────────────────────────────────────────────────────

#[test]
fn test_row_cache_hit_and_evict() {
    // Capacity of 2: insert rows 0 and 1, then 2 → evicts 0.
    let mut cache = RowCache::new(2);
    cache.insert(0, vec![Cell::Int(0)]);
    cache.insert(1, vec![Cell::Int(1)]);
    // Both present.
    assert!(cache.get(0).is_some(), "row 0 should be cached");
    assert!(cache.get(1).is_some(), "row 1 should be cached");

    // Inserting row 2 should evict row 0 (oldest).
    cache.insert(2, vec![Cell::Int(2)]);
    assert!(cache.get(0).is_none(), "row 0 should have been evicted");
    assert!(cache.get(1).is_some(), "row 1 should still be cached");
    assert!(cache.get(2).is_some(), "row 2 should be cached");
    assert_eq!(cache.len(), 2);
}

#[test]
fn test_row_cache_invalidate() {
    let mut cache = RowCache::new(10);
    cache.insert(0, vec![Cell::Empty]);
    cache.insert(1, vec![Cell::Empty]);
    cache.insert(2, vec![Cell::Empty]);
    assert_eq!(cache.len(), 3);

    cache.invalidate();
    assert!(cache.is_empty(), "cache should be empty after invalidate");
}

#[test]
fn test_row_cache_update_moves_to_back() {
    // Re-inserting an existing key should refresh it (bump to back).
    let mut cache = RowCache::new(2);
    cache.insert(0, vec![Cell::Int(0)]);
    cache.insert(1, vec![Cell::Int(1)]);
    // Re-insert row 0 → it goes to the back; row 1 is now the oldest.
    cache.insert(0, vec![Cell::Int(99)]);
    // Insert row 2 → should evict row 1 (oldest), not row 0.
    cache.insert(2, vec![Cell::Int(2)]);
    assert!(
        cache.get(0).is_some(),
        "row 0 (refreshed) should survive eviction"
    );
    assert!(cache.get(1).is_none(), "row 1 (oldest) should be evicted");
    assert!(cache.get(2).is_some(), "row 2 should be cached");
}

#[test]
fn test_row_cache_zero_capacity_never_caches() {
    let mut cache = RowCache::new(0);
    cache.insert(0, vec![Cell::Int(42)]);
    assert!(
        cache.is_empty(),
        "zero-capacity cache should never store entries"
    );
    assert!(cache.get(0).is_none());
}

// ── Table integration tests ───────────────────────────────────────────────────

struct SimpleSource {
    rows: usize,
    cols: Vec<ColumnDef>,
}

impl SimpleSource {
    fn new(rows: usize) -> Self {
        Self {
            rows,
            cols: vec![ColumnDef {
                name: "V".into(),
                width: 60.0,
                ..ColumnDef::default()
            }],
        }
    }
}

impl RowSource for SimpleSource {
    fn row_count(&self) -> usize {
        self.rows
    }
    fn row(&self, i: usize) -> Vec<Cell> {
        vec![Cell::Int(i as i64)]
    }
    fn column_defs(&self) -> &[ColumnDef] {
        &self.cols
    }
}

#[test]
fn test_table_cache_row_at_offset_uniform() {
    // 5 rows × 20 px each → row starts at 0, 20, 40, 60, 80.
    let mut table = Table::new(SimpleSource::new(5)).with_row_height(20.0);
    assert_eq!(table.cache_row_at_offset(0.0), 0);
    assert_eq!(table.cache_row_at_offset(19.9), 0);
    assert_eq!(table.cache_row_at_offset(20.0), 1);
    assert_eq!(table.cache_row_at_offset(40.0), 2);
    assert_eq!(table.cache_row_at_offset(80.0), 4);
}

#[test]
fn test_table_cache_visible_range_uniform() {
    // 10 rows × 24 px → total 240 px.
    // viewport_y=24, viewport_h=72 → end_y=96 → rows 1,2,3,4 visible.
    let mut table = Table::new(SimpleSource::new(10)).with_row_height(24.0);
    let range = table.cache_visible_range(24.0, 72.0);
    assert!(range.start <= 1, "range should start at or before row 1");
    assert!(range.end >= 4, "range should include at least row 4");
    assert!(range.end <= 10);
}

#[test]
fn test_table_with_row_heights() {
    // Variable heights: [20, 40, 30].
    let mut table = Table::new(SimpleSource::new(3)).with_row_heights(vec![20.0, 40.0, 30.0]);
    assert_eq!(table.cache_row_at_offset(0.0), 0);
    assert_eq!(table.cache_row_at_offset(20.0), 1);
    assert_eq!(table.cache_row_at_offset(60.0), 2);
}

#[test]
fn test_table_get_or_fetch_row_caches() {
    let mut table = Table::new(SimpleSource::new(5)).with_row_height(24.0);
    // First fetch — cache miss, materialises from source.
    let r0_first = table.get_or_fetch_row(0);
    assert!(matches!(r0_first[0], Cell::Int(0)));
    // Second fetch — should hit the cache.
    let r0_second = table.get_or_fetch_row(0);
    assert!(matches!(r0_second[0], Cell::Int(0)));
}

#[test]
fn test_table_invalidate_row_cache() {
    let mut table = Table::new(SimpleSource::new(3)).with_row_height(24.0);
    table.get_or_fetch_row(0);
    table.get_or_fetch_row(1);
    // Invalidate should clear the internal cache without panicking.
    table.invalidate_row_cache();
    // Fetching again should still work.
    let row = table.get_or_fetch_row(0);
    assert!(matches!(row[0], Cell::Int(0)));
}

// ── Cell-edit integration test ─────────────────────────────────────────────

struct MutableEditSource {
    data: Vec<Vec<Cell>>,
    cols: Vec<ColumnDef>,
}

impl MutableEditSource {
    fn new() -> Self {
        Self {
            data: vec![
                vec![Cell::Int(1), Cell::Text("alpha".into())],
                vec![Cell::Int(2), Cell::Text("beta".into())],
            ],
            cols: vec![
                ColumnDef {
                    name: "ID".into(),
                    width: 60.0,
                    ..ColumnDef::default()
                },
                ColumnDef {
                    name: "Name".into(),
                    width: 120.0,
                    ..ColumnDef::default()
                },
            ],
        }
    }
}

use oxiui_table::TableError;

impl RowSource for MutableEditSource {
    fn row_count(&self) -> usize {
        self.data.len()
    }
    fn row(&self, i: usize) -> Vec<Cell> {
        self.data[i].clone()
    }
    fn column_defs(&self) -> &[ColumnDef] {
        &self.cols
    }
    fn set_cell(&mut self, row: usize, col: usize, value: Cell) -> Result<(), TableError> {
        if row >= self.data.len() || col >= self.cols.len() {
            return Err(TableError::OutOfBounds { row, col });
        }
        self.data[row][col] = value;
        Ok(())
    }
}

#[test]
fn test_cell_edit_via_source() {
    let mut source = MutableEditSource::new();
    // Simulate an edit: set row 0, col 1 to "gamma".
    source
        .set_cell(0, 1, Cell::Text("gamma".into()))
        .expect("edit should succeed");
    let row = source.row(0);
    assert!(
        matches!(&row[1], Cell::Text(s) if s == "gamma"),
        "cell should be updated to 'gamma'"
    );

    // Commit at invalid position returns OutOfBounds.
    let err = source
        .set_cell(99, 0, Cell::Int(0))
        .expect_err("should be out of bounds");
    assert_eq!(err, TableError::OutOfBounds { row: 99, col: 0 });
}

// ── Virtual column rendering tests ────────────────────────────────────────────

/// Helper: build a source with N columns each of the given `width`.
struct UniformColSource {
    col_defs: Vec<ColumnDef>,
}

impl UniformColSource {
    fn new(n_cols: usize, width: f32) -> Self {
        let col_defs = (0..n_cols)
            .map(|i| ColumnDef {
                name: format!("C{i}"),
                width,
                ..ColumnDef::default()
            })
            .collect();
        Self { col_defs }
    }
}

impl RowSource for UniformColSource {
    fn row_count(&self) -> usize {
        1
    }
    fn row(&self, _i: usize) -> Vec<Cell> {
        self.col_defs.iter().map(|_| Cell::Empty).collect()
    }
    fn column_defs(&self) -> &[ColumnDef] {
        &self.col_defs
    }
}

#[test]
fn test_visible_columns_basic() {
    // 5 columns × 100 px each → prefix = [0, 100, 200, 300, 400, 500].
    // h_scroll=150, viewport_width=200 → visible window [150, 350).
    // start: partition_point(p ≤ 150) = 2, saturating_sub(1) = 1.
    // end_raw: partition_point(p < 350) = 4 (0,100,200,300 < 350).
    // → range 1..4, i.e. columns at render positions {1, 2, 3}.
    let table = Table::new(UniformColSource::new(5, 100.0));
    let range = table.visible_column_range(150.0, 200.0);
    assert_eq!(
        range,
        1..4,
        "viewport [150,350) should expose columns 1,2,3"
    );
}

#[test]
fn test_visible_columns_full_viewport() {
    // Viewport wider than all 5 columns (500 px total, viewport 600 px).
    // h_scroll=0, so all columns must be visible → 0..5.
    let table = Table::new(UniformColSource::new(5, 100.0));
    let range = table.visible_column_range(0.0, 600.0);
    assert_eq!(range, 0..5, "full viewport should expose all 5 columns");
}

#[test]
fn test_visible_columns_scrolled_past_end() {
    // h_scroll beyond the total width (500 px) → no columns visible.
    // start = partition_point(p ≤ 600) - 1 = 6 - 1 = 5 (clamped to 5 inside n=5).
    // end_raw = partition_point(p < 600+200=800) = 6, clamped to n=5.
    // start..end = 5..5 (empty).
    let table = Table::new(UniformColSource::new(5, 100.0));
    let range = table.visible_column_range(600.0, 200.0);
    assert!(
        range.is_empty(),
        "scrolled past end → empty range, got {range:?}"
    );
}

#[test]
fn test_visible_columns_empty_source() {
    // No columns → should return 0..0 without panicking.
    let table = Table::new(UniformColSource::new(0, 100.0));
    let range = table.visible_column_range(0.0, 400.0);
    assert_eq!(range, 0..0, "zero columns → 0..0");
}

// ── Widget trait bridge test ──────────────────────────────────────────────────

/// Minimal no-op [`oxiui_core::UiCtx`] for testing.
struct NullUiCtx {
    label_count: usize,
}

impl NullUiCtx {
    fn new() -> Self {
        Self { label_count: 0 }
    }
}

use oxiui_core::{ButtonResponse, UiCtx};

impl UiCtx for NullUiCtx {
    fn heading(&mut self, _text: &str) {}
    fn label(&mut self, _text: &str) {
        self.label_count += 1;
    }
    fn button(&mut self, _label: &str) -> ButtonResponse {
        ButtonResponse::default()
    }
}

/// A simple RowSource with 3 rows and 2 columns for Widget render testing.
struct SmallSource {
    defs: Vec<ColumnDef>,
}

impl SmallSource {
    fn new() -> Self {
        Self {
            defs: vec![ColumnDef::new("A"), ColumnDef::new("B")],
        }
    }
}

impl RowSource for SmallSource {
    fn row_count(&self) -> usize {
        3
    }
    fn row(&self, i: usize) -> Vec<Cell> {
        vec![Cell::Int(i as i64), Cell::Text(format!("row-{i}"))]
    }
    fn column_defs(&self) -> &[ColumnDef] {
        &self.defs
    }
}

#[test]
fn test_table_widget_render_no_panic() {
    use oxiui_core::Widget;
    let mut table = Table::new(SmallSource::new());
    let mut ctx = NullUiCtx::new();
    // Should not panic; each row emits one label call.
    table.render(&mut ctx);
    assert_eq!(ctx.label_count, 3, "render should emit one label per row");
}