sqlly-datatable 1.4.1

Configurable virtualized data grid component for the GPUI toolkit.
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
//! Canvas paint functions and the lightweight snapshot that GPUI's
//! `canvas(...)` closure hands to the painter.
//!
//! `PaintData` is constructed once per layout pass; it clones the small
//! state needed for paint (selection, scroll offsets, resolved formats) but
//! keeps the bulk [`crate::data::GridData`] behind a count of visible rows
//! rather than copying the entire dataset.

use crate::config::ResolvedColumnFormat;
use crate::data::Column;
use crate::grid::menu::{self};
use crate::grid::selection::{
    is_cell_selected, is_column_selected, is_row_selected, HitResult, Selection, SortDirection,
};
use crate::grid::state::{state_inner, GridState, SCROLLBAR_SIZE};
use crate::grid::theme::GridTheme;

use gpui::{point, px, size, App, Bounds, CursorStyle, Hsla, PaintQuad, Pixels, Point, Window};
use std::sync::Arc;

const SCROLLBAR_THUMB_COLOR: Hsla = hsla_const(0.0, 0.0, 0.55, 1.0);

const fn hsla_const(h: f32, s: f32, l: f32, a: f32) -> Hsla {
    Hsla { h, s, l, a }
}

#[derive(Clone)]
pub(crate) struct PaintData {
    pub(crate) display_indices: Arc<Vec<usize>>,
    pub(crate) selection: Selection,
    pub(crate) sort: Option<(usize, SortDirection)>,
    pub(crate) theme: GridTheme,
    pub(crate) columns: Vec<Column>,
    pub(crate) resolved_formats: Vec<ResolvedColumnFormat>,
    pub(crate) rows: Arc<Vec<Vec<crate::data::CellValue>>>,
    pub(crate) filters_active: Vec<bool>,
    pub(crate) scroll_offset: Point<Pixels>,
    pub(crate) row_height: f32,
    pub(crate) header_height: f32,
    pub(crate) row_header_width: f32,
    pub(crate) font_size: f32,
    pub(crate) char_width: f32,
    pub(crate) drag_rect: Option<(Point<Pixels>, Point<Pixels>)>,
    pub(crate) hover_hit: Option<HitResult>,
}

impl PaintData {
    pub(crate) fn from_state(s: &GridState) -> Self {
        Self {
            display_indices: Arc::clone(&s.display_indices),
            selection: s.selection.clone(),
            sort: s.sort,
            theme: s.theme.clone(),
            columns: s.data.columns.clone(),
            resolved_formats: s.resolved_formats.clone(),
            rows: Arc::clone(&s.data_rows),
            filters_active: s.filters.iter().map(|f| f.is_active()).collect(),
            scroll_offset: s.scroll_handle.offset(),
            row_height: s.row_height,
            header_height: s.header_height,
            row_header_width: s.row_header_width,
            font_size: s.font_size,
            char_width: s.char_width,
            drag_rect: s.drag_screen_rect(),
            hover_hit: s.hover_hit,
        }
    }
}

pub(crate) struct StatusBarData {
    pub(crate) text: String,
    pub(crate) theme: GridTheme,
    pub(crate) font_size: f32,
}

impl StatusBarData {
    pub(crate) fn from_state(s: &GridState) -> Self {
        Self {
            text: state_inner::format_current_status(s),
            theme: s.theme.clone(),
            font_size: s.font_size,
        }
    }
}

fn fill_quad(window: &mut Window, x: f32, y: f32, w: f32, h: f32, color: Hsla) {
    window.paint_quad(PaintQuad {
        bounds: Bounds {
            origin: point(px(x), px(y)),
            size: size(px(w), px(h)),
        },
        background: color.into(),
        border_color: Hsla {
            h: 0.0,
            s: 0.0,
            l: 0.0,
            a: 0.0,
        },
        border_widths: Default::default(),
        corner_radii: Default::default(),
        border_style: Default::default(),
    });
}

fn paint_filter_icon(window: &mut Window, x: f32, y: f32, color: Hsla) {
    let rows: [(f32, f32); 5] = [(10.0, 2.0), (8.0, 2.0), (6.0, 2.0), (4.0, 2.0), (2.0, 4.0)];
    let mut cy = y;
    for (w, h) in rows {
        let offset = (10.0 - w) * 0.5;
        fill_quad(window, x + offset, cy, w, h, color);
        cy += h;
    }
}

pub(crate) fn paint_scrollbars(
    data: &PaintData,
    window: &mut Window,
    ox: f32,
    oy: f32,
    sw: f32,
    sh: f32,
    theme: &GridTheme,
) {
    let scroll = data.scroll_offset;
    let (content_w, content_h) = (
        data.columns.iter().map(|c| c.width).sum::<f32>(),
        data.display_indices.len() as f32 * data.row_height,
    );
    let vw_full = sw - data.row_header_width;
    let vh_full = sh - data.header_height;
    let has_v = content_h > vh_full;
    let has_h = content_w > vw_full;
    let reserved_w = if has_v { SCROLLBAR_SIZE } else { 0.0 };
    let reserved_h = if has_h { SCROLLBAR_SIZE } else { 0.0 };
    let vw = vw_full - reserved_w;
    let vh = vh_full - reserved_h;
    let max_x = (content_w - vw).max(0.0);
    let max_y = (content_h - vh).max(0.0);
    let (sx, sy) = (f32::from(scroll.x), f32::from(scroll.y));
    let track_bg = theme.row_header_bg;

    if has_v {
        let track_x = ox + sw - SCROLLBAR_SIZE;
        let track_y = oy + data.header_height;
        let track_h = sh - data.header_height - reserved_h;
        if track_h > 0.0 {
            fill_quad(window, track_x, track_y, SCROLLBAR_SIZE, track_h, track_bg);
            let thumb_h = ((track_h * (vh / content_h)).max(20.0)).min(track_h);
            let frac = if max_y > 0.0 { sy / max_y } else { 0.0 };
            let thumb_y = track_y + frac * (track_h - thumb_h);
            fill_quad(
                window,
                track_x + 3.0,
                thumb_y,
                SCROLLBAR_SIZE - 6.0,
                thumb_h,
                SCROLLBAR_THUMB_COLOR,
            );
        }
    }
    if has_h {
        let track_x = ox + data.row_header_width;
        let track_y = oy + sh - SCROLLBAR_SIZE;
        let track_w = sw - data.row_header_width - reserved_w;
        if track_w > 0.0 {
            fill_quad(window, track_x, track_y, track_w, SCROLLBAR_SIZE, track_bg);
            let thumb_w = ((track_w * (vw / content_w)).max(20.0)).min(track_w);
            let frac = if max_x > 0.0 { sx / max_x } else { 0.0 };
            let thumb_x = track_x + frac * (track_w - thumb_w);
            fill_quad(
                window,
                thumb_x,
                track_y + 3.0,
                thumb_w,
                SCROLLBAR_SIZE - 6.0,
                SCROLLBAR_THUMB_COLOR,
            );
        }
    }
    if has_v && has_h {
        fill_quad(
            window,
            ox + sw - SCROLLBAR_SIZE,
            oy + sh - SCROLLBAR_SIZE,
            SCROLLBAR_SIZE,
            SCROLLBAR_SIZE,
            track_bg,
        );
    }
}

pub(crate) fn paint_grid(
    data: &PaintData,
    window: &mut Window,
    cx: &mut App,
    bounds: Bounds<Pixels>,
) {
    if matches!(data.hover_hit, Some(HitResult::ColumnBorder(_))) {
        window.set_window_cursor_style(CursorStyle::ResizeLeftRight);
    }
    let ox = f32::from(bounds.origin.x);
    let oy = f32::from(bounds.origin.y);
    let sw = f32::from(bounds.size.width);
    let sh = f32::from(bounds.size.height);
    let (sx, sy) = (
        f32::from(data.scroll_offset.x),
        f32::from(data.scroll_offset.y),
    );
    let row_h = data.row_height;
    let hdr_h = data.header_height;
    let rhw = data.row_header_width;
    let fs = data.font_size;
    let cw = data.char_width;
    let theme = &data.theme;

    let text_system = window.text_system().clone();
    let font_size = px(fs);
    let line_height = px(fs * 1.2);
    let font = gpui::font("monospace");
    let paint_txt = |win: &mut Window,
                     cx: &mut App,
                     text: &str,
                     x: f32,
                     y: f32,
                     color: Hsla,
                     max_w: Option<f32>| {
        let mk_run = |t: &str| gpui::TextRun {
            len: t.len(),
            color,
            font: font.clone(),
            background_color: None,
            underline: None,
            strikethrough: None,
        };
        let shaped =
            text_system.shape_line(text.to_owned().into(), font_size, &[mk_run(text)], None);
        let shaped = match max_w {
            Some(mw) if mw <= 0.0 => return,
            Some(mw) if f32::from(shaped.width) > mw => {
                let byte_idx = shaped.index_for_x(px(mw)).unwrap_or(0);
                let truncated = &text[..byte_idx.min(text.len())];
                text_system.shape_line(
                    truncated.to_owned().into(),
                    font_size,
                    &[mk_run(truncated)],
                    None,
                )
            }
            _ => shaped,
        };
        let _ = shaped.paint(Point { x: px(x), y: px(y) }, line_height, win, cx);
    };

    fill_quad(window, ox, oy, sw, sh, theme.bg);
    fill_quad(window, ox, oy, rhw, sh, theme.row_header_bg);

    let data_y = hdr_h;
    let visible_h = sh - data_y;
    let first_row = ((sy / row_h) as usize).min(data.display_indices.len());
    let vis_rows = ((visible_h / row_h) as usize) + 1;
    let last_row = (first_row + vis_rows).min(data.display_indices.len());

    for dr in first_row..last_row {
        let y = oy + data_y + (dr as f32 * row_h) - sy;
        if y + row_h < oy + data_y || y > oy + sh {
            continue;
        }
        let row_idx = data.display_indices[dr];
        let row_sel = is_row_selected(&data.selection, dr);
        let alt = dr % 2 == 1;
        if row_sel {
            fill_quad(window, ox + rhw, y, sw - rhw, row_h, theme.selection_bg);
        } else if alt {
            fill_quad(window, ox + rhw, y, sw - rhw, row_h, theme.alt_row_bg);
        }

        let mut col_x = rhw - sx;
        for (ci, col) in data.columns.iter().enumerate() {
            let x = ox + col_x;
            let w = col.width;
            if x + w < ox + rhw || x > ox + sw {
                col_x += w;
                continue;
            }
            let cell_sel = is_cell_selected(&data.selection, dr, ci);
            if cell_sel {
                fill_quad(window, x, y, w, row_h, theme.selection_bg);
            }
            let cell = &data.rows[row_idx][ci];
            let fmt = &data.resolved_formats[ci];
            let (text, is_neg) = crate::format::format_cell(cell, fmt);
            let color = if is_neg && fmt.number.show_negative_red {
                theme.negative_fg
            } else {
                theme.text_fg
            };
            let text_w = text_w_approx(&text, cw);
            let tx = match fmt.alignment() {
                crate::config::TextAlignment::Left => x + 8.0,
                crate::config::TextAlignment::Center => x + (w - text_w) * 0.5,
                crate::config::TextAlignment::Right => x + w - text_w - 8.0,
            };
            let ty = y + (row_h - fs) * 0.5;
            paint_txt(window, cx, &text, tx, ty, color, Some(w - 16.0));
            fill_quad(window, x + w, y, 1.0, row_h, theme.grid_line);
            col_x += w;
        }
        fill_quad(window, ox, y + row_h, sw, 1.0, theme.grid_line);
    }

    for dr in first_row..last_row {
        let y = oy + data_y + (dr as f32 * row_h) - sy;
        if y + row_h < oy + data_y || y > oy + sh {
            continue;
        }
        let row_sel = is_row_selected(&data.selection, dr);
        let alt = dr % 2 == 1;
        let rh_bg = if row_sel {
            theme.selection_bg
        } else if alt {
            theme.alt_row_bg
        } else {
            theme.row_header_bg
        };
        fill_quad(window, ox, y, rhw, row_h, rh_bg);
        paint_txt(
            window,
            cx,
            &(dr + 1).to_string(),
            ox + 6.0,
            y + (row_h - fs) * 0.5,
            theme.header_fg,
            None,
        );
        fill_quad(window, ox, y + row_h, rhw, 1.0, theme.grid_line);
    }

    fill_quad(window, ox, oy, sw, hdr_h, theme.header_bg);
    let mut col_x = rhw - sx;
    for (ci, col) in data.columns.iter().enumerate() {
        let x = ox + col_x;
        let w = col.width;
        if x + w < ox + rhw || x > ox + sw {
            col_x += w;
            continue;
        }
        if is_column_selected(&data.selection, ci) {
            fill_quad(window, x, oy, w, hdr_h, theme.selection_bg);
        }
        paint_txt(
            window,
            cx,
            &col.name,
            x + 8.0,
            oy + (hdr_h - fs) * 0.5,
            theme.header_fg,
            Some(w - 28.0),
        );
        let btn_w = 20.0;
        let btn_x = x + w - btn_w;
        // The sort button shares the column header's background color, set off
        // from the header only by a 1px outline drawn around it.
        let btn_bg = theme.header_bg;
        let btn_y = oy + 4.0;
        let btn_h = hdr_h - 8.0;
        fill_quad(window, btn_x, btn_y, btn_w, btn_h, btn_bg);
        // 1px outline around the button (top, bottom, left, right edges).
        fill_quad(window, btn_x, btn_y, btn_w, 1.0, theme.grid_line);
        fill_quad(
            window,
            btn_x,
            btn_y + btn_h - 1.0,
            btn_w,
            1.0,
            theme.grid_line,
        );
        fill_quad(window, btn_x, btn_y, 1.0, btn_h, theme.grid_line);
        fill_quad(
            window,
            btn_x + btn_w - 1.0,
            btn_y,
            1.0,
            btn_h,
            theme.grid_line,
        );
        let (ind, ind_color) = match data.sort {
            Some((sc, SortDirection::Ascending)) if sc == ci => ("^", theme.sort_indicator),
            Some((sc, SortDirection::Descending)) if sc == ci => ("v", theme.sort_indicator),
            _ => ("-", theme.header_fg),
        };
        paint_txt(
            window,
            cx,
            ind,
            btn_x + (btn_w - cw) * 0.5,
            oy + (hdr_h - fs) * 0.5,
            ind_color,
            None,
        );
        if data.filters_active[ci] {
            paint_filter_icon(
                window,
                btn_x - 14.0,
                oy + (hdr_h - 12.0) * 0.5,
                theme.sort_indicator,
            );
        }
        fill_quad(window, x + w, oy, 1.0, hdr_h, theme.grid_line);
        col_x += w;
    }
    fill_quad(window, ox, oy, rhw, hdr_h, theme.row_header_bg);

    fill_quad(window, ox, oy + hdr_h, sw, 1.0, theme.grid_line);
    fill_quad(window, ox + rhw, oy, 1.0, sh, theme.grid_line);

    if let Some((start, current)) = data.drag_rect {
        // `drag_rect` corners are grid-relative; shift by the grid origin to
        // paint them in the window's absolute coordinate space.
        let (sx0, sy0) = (ox + f32::from(start.x), oy + f32::from(start.y));
        let (sx1, sy1) = (ox + f32::from(current.x), oy + f32::from(current.y));
        let (rx, ry) = (sx0.min(sx1), sy0.min(sy1));
        let (rw, rh) = ((sx1 - sx0).abs(), (sy1 - sy0).abs());
        window.paint_quad(PaintQuad {
            bounds: Bounds {
                origin: Point {
                    x: px(rx),
                    y: px(ry),
                },
                size: size(px(rw), px(rh)),
            },
            background: hsla_const(0.0, 0.0, 0.0, 0.0).into(),
            border_color: hsla_const(0.0, 0.0, 0.0, 0.0),
            border_widths: Default::default(),
            corner_radii: Default::default(),
            border_style: Default::default(),
        });
    }

    paint_scrollbars(data, window, ox, oy, sw, sh, theme);

    // The context menu is no longer painted here. It is rendered as a
    // `deferred` + `anchored` overlay in `widget.rs` so that it paints — and
    // receives mouse events — on top of everything, including regions outside
    // the grid widget's layout bounds (e.g. a host header above the grid). The
    // filter panel uses the same overlay mechanism, so it is not painted here
    // either.
}

fn text_w_approx(text: &str, char_width: f32) -> f32 {
    text.chars().count() as f32 * char_width
}

pub(crate) fn paint_status_bar(
    data: &StatusBarData,
    window: &mut Window,
    cx: &mut App,
    bounds: Bounds<Pixels>,
) {
    let ox = f32::from(bounds.origin.x);
    let oy = f32::from(bounds.origin.y);
    let sw = f32::from(bounds.size.width);
    let sh = f32::from(bounds.size.height);
    let theme = &data.theme;
    let fs = data.font_size;

    fill_quad(window, ox, oy, sw, sh, theme.header_bg);
    fill_quad(window, ox, oy, sw, 1.0, theme.grid_line);

    let text_system = window.text_system().clone();
    let font_size = px(fs);
    let line_height = px(fs * 1.2);
    let font = gpui::font("monospace");
    let run = gpui::TextRun {
        len: data.text.len(),
        color: theme.text_fg,
        font,
        background_color: None,
        underline: None,
        strikethrough: None,
    };
    let shaped = text_system.shape_line(data.text.clone().into(), font_size, &[run], None);
    let _ = shaped.paint(
        Point {
            x: px(ox + 8.0),
            y: px(oy + (sh - fs) * 0.5),
        },
        line_height,
        window,
        cx,
    );
}

// Re-export MenuAction so widget code can mention it without a long path.
#[allow(unused_imports)]
pub(crate) use menu::MenuAction as _MenuAction;