oriel-iced 0.1.0

iced integrator for the oriel headless data grid: satisfies the data and render seams for iced 0.14.
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
//! Ready-made grid themes — faithful ports of the looks users already know.
//!
//! Every value here was extracted from the theme's *published* CSS (or, for
//! the spreadsheet look, the dominant web spreadsheet implementations) and
//! independently re-verified against those sources — provenance, the exact
//! CSS custom properties, and the alpha-compositing math are documented in
//! `docs/table-themes.md`. Deliberate infidelities (fixed 13 px font, fixed
//! semibold headers, parity-adjusted row heights) are documented there too.
//!
//! A [`GridTheme`] is plain data over the grid's existing chrome knobs — no
//! new machinery. [`GridTheme::apply`] bundles the setters; every color and
//! length is a public field, so tweaking a preset is struct-update syntax,
//! and an app that wants to *wrap* a themed style (row tints over Alpine,
//! say) calls the public [`GridTheme::row_style`] inside its own hook — the
//! same no-cliffs contract as the `default_*` functions.
//!
//! Pair [`apply`](GridTheme::apply) (the grid's chrome) with
//! [`iced_theme`](GridTheme::iced_theme) (the ambient `iced::Theme`) so
//! text, scrollbars, rules, and the app around the grid agree with the
//! preset.
//!
//! There is deliberately no "Oriel" preset here: the built-in look derives
//! every color from the ambient palette, so "oriel light"/"oriel dark" are
//! just the grid's defaults paired with `iced::Theme::Light`/`Dark` (or any
//! custom palette) — see the examples' theme dropdowns.

use iced::theme::Palette;
use iced::{Color, Task, Theme};
use oriel_core::data::DataSource;

use crate::grid::{Message, RowContext, VirtualGrid};

/// A complete visual preset for [`VirtualGrid`] — colors and geometry as
/// plain public data. See the [module docs](self) for provenance and the
/// wrap/replace contract.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GridTheme {
    /// Display name (doubles as the custom `iced::Theme` name).
    pub name: &'static str,
    /// Row pitch. Themes with a [`row_divider`](Self::row_divider) use an
    /// EVEN height: the 1 px line is carved out of it, leaving an odd
    /// content box that centers ink on integer pixels (the parity chain).
    pub row_height: f32,
    /// Header-band height. Parity note: header labels are raw text centered
    /// in the band, so EVEN heights center the fractional line box most
    /// evenly — every shipped preset uses one.
    pub header_height: f32,
    /// Horizontal padding inside each cell.
    pub cell_padding: f32,
    /// Body background. Drawn by the app / ambient theme — pair with
    /// [`iced_theme`](Self::iced_theme).
    pub background: Color,
    /// Body text color (carried on the row style, so it applies even
    /// without the ambient theme).
    pub text: Color,
    /// Header/footer band fill.
    pub header_background: Color,
    /// Header/footer band text (also recolors the sort arrow — it follows
    /// the header's foreground, ag-grid's own behavior).
    pub header_text: Color,
    /// The theme's structural border color — drives the grid's frame rules
    /// (the separators under the header and above the footer) via
    /// [`VirtualGrid::set_rule_color`] on apply.
    pub border: Color,
    /// Horizontal 1 px line under every row, if the theme has one.
    pub row_divider: Option<Color>,
    /// Vertical 1 px line between columns, if the theme has one.
    pub column_divider: Option<Color>,
    /// Hovered-row background.
    pub row_hover: Color,
    /// Selected-row background (the focused-row color is synthesized from
    /// it — see [`row_style`](Self::row_style)).
    pub row_selected: Color,
    /// Zebra stripe for odd rows (0-based, ag-grid's `.ag-row-odd`).
    pub row_stripe: Option<Color>,
    /// Brand/active color — selection accents, `iced` palette `primary`.
    pub accent: Color,
}

fn rgb(hex: u32) -> Color {
    Color::from_rgb8((hex >> 16) as u8, (hex >> 8) as u8, hex as u8)
}

/// Linear per-channel mix — used to synthesize the focused-row color
/// (CSS grids mark keyboard focus with a cell border instead; oriel marks
/// the row, so the color is derived: selected diluted toward background).
fn mix(a: Color, b: Color, t: f32) -> Color {
    Color {
        r: a.r + (b.r - a.r) * t,
        g: a.g + (b.g - a.g) * t,
        b: a.b + (b.b - a.b) * t,
        a: 1.0,
    }
}

impl GridTheme {
    /// Applies the preset to a grid: geometry, dividers, frame-rule color,
    /// and the row/header/footer style hooks (the cell hook resets to the
    /// default — themed dividers come from the divider knobs, not border
    /// hacks) — then queues an
    /// [`auto_size_all`](VirtualGrid::auto_size_all): the theme changes the
    /// metrics columns were fitted under (padding, density), so every
    /// column re-fits to its content on the next frame. Chain your own
    /// `set_*_style` calls AFTER this to wrap the theme — e.g. data-driven
    /// tints over [`row_style`](Self::row_style).
    ///
    /// Returns the follow-up [`Task`] (the density change re-anchors the
    /// viewport **by row**, which commands the scrollable) — feed it to
    /// iced.
    ///
    /// # Example — apply a preset, then wrap it with app chrome
    ///
    /// ```
    /// # use oriel_core::{columns::ColumnId, data::VecStore};
    /// # use oriel_iced::grid::{BoxedColumn, VirtualGrid};
    /// # use oriel_iced::column::TextColumn;
    /// # #[derive(Clone, Debug)] struct Row { id: u64, alarming: bool }
    /// # fn key(r: &Row) -> u64 { r.id }
    /// # let store = VecStore::new(
    /// #     (0..100u64).map(|id| Row { id, alarming: id % 7 == 0 }).collect(),
    /// #     key as fn(&Row) -> u64,
    /// # );
    /// # let columns: Vec<BoxedColumn<Row>> =
    /// #     vec![Box::new(TextColumn::new(ColumnId(1), "#", 80.0, |r: &Row| r.id.to_string()))];
    /// # let (mut grid, _init) = VirtualGrid::new(store, columns);
    /// use oriel_iced::theme::GridTheme;
    ///
    /// let theme = GridTheme::balham();
    /// let task = theme.apply(&mut grid); // …feed to iced
    ///
    /// // Wrap AFTER applying — the preset's row chrome stays underneath:
    /// grid.set_row_style(move |iced_theme, ctx| {
    ///     let mut style = theme.row_style(ctx);
    ///     if !ctx.is_selected && ctx.row.is_some_and(|r| r.alarming) {
    ///         let danger = iced_theme.extended_palette().danger.weak.color;
    ///         style.background = Some(danger.into());
    ///     }
    ///     style
    /// });
    /// // Pair with the matching ambient theme from your application's
    /// // `theme` fn: `Some(GridTheme::balham().iced_theme())`.
    /// # let _ = task;
    /// ```
    pub fn apply<S: DataSource>(&self, grid: &mut VirtualGrid<S>) -> Task<Message<S::Row>> {
        let task = grid.set_row_height(self.row_height);
        grid.set_header_height(self.header_height);
        grid.set_footer_height(self.header_height);
        grid.set_cell_padding(self.cell_padding);
        grid.set_row_divider(self.row_divider);
        grid.set_column_divider(self.column_divider);
        grid.set_rule_color(Some(self.border));
        grid.set_cell_style(crate::grid::default_cell_style);
        let theme = *self;
        grid.set_row_style(move |_, ctx| theme.row_style(ctx));
        let theme = *self;
        grid.set_header_style(move |_| theme.header_style());
        let theme = *self;
        grid.set_footer_style(move |_| theme.header_style());
        grid.auto_size_all();
        task
    }

    /// The preset's row chrome — same precedence as
    /// [`default_row_style`](crate::grid::default_row_style):
    /// selected > focused > hovered > stripe. Public so apps can wrap it.
    #[must_use]
    pub fn row_style<Row>(&self, ctx: &RowContext<'_, Row>) -> iced::widget::container::Style {
        let background = if ctx.is_selected {
            Some(self.row_selected)
        } else if ctx.is_focused {
            Some(mix(self.background, self.row_selected, 0.55))
        } else if ctx.is_hovered {
            Some(self.row_hover)
        } else if ctx.index % 2 == 1 {
            self.row_stripe
        } else {
            None
        };
        iced::widget::container::Style {
            text_color: Some(self.text),
            background: background.map(Into::into),
            ..iced::widget::container::Style::default()
        }
    }

    /// The preset's header/footer band chrome. Public so apps can wrap it.
    #[must_use]
    pub fn header_style(&self) -> iced::widget::container::Style {
        iced::widget::container::Style {
            text_color: Some(self.header_text),
            background: Some(self.header_background.into()),
            ..iced::widget::container::Style::default()
        }
    }

    /// A matching ambient [`iced::Theme`] (custom palette: the preset's
    /// background/text/accent over the stock light or dark ramp, picked by
    /// background luminance). Set it as the application theme so text,
    /// scrollbars, rules, and surrounding chrome agree with the grid.
    #[must_use]
    pub fn iced_theme(&self) -> Theme {
        let luma =
            0.299 * self.background.r + 0.587 * self.background.g + 0.114 * self.background.b;
        let base = if luma < 0.5 { Palette::DARK } else { Palette::LIGHT };
        Theme::custom(
            self.name,
            Palette {
                background: self.background,
                text: self.text,
                primary: self.accent,
                ..base
            },
        )
    }

    /// All built-in presets, dropdown-ready.
    #[must_use]
    pub fn presets() -> [GridTheme; 10] {
        [
            Self::quartz(),
            Self::quartz_dark(),
            Self::alpine(),
            Self::alpine_dark(),
            Self::balham(),
            Self::balham_dark(),
            Self::material(),
            Self::spreadsheet(),
            Self::bootstrap(),
            Self::bootstrap_dark(),
        ]
    }

    /// ag-grid **Quartz** (v31+ default): roomy, cool, blue-tinted
    /// hover/selection, row lines only.
    #[must_use]
    pub fn quartz() -> Self {
        Self {
            name: "Quartz",
            row_height: 42.0,
            header_height: 48.0,
            cell_padding: 16.0,
            background: rgb(0xFFFFFF),
            text: rgb(0x181D1F),
            header_background: rgb(0xFAFAFB),
            header_text: rgb(0x181D1F),
            border: rgb(0xDCDDDD),
            row_divider: Some(rgb(0xDCDDDD)),
            column_divider: None,
            row_hover: rgb(0xE4F2FE),
            row_selected: rgb(0xEDF7FE),
            row_stripe: None,
            accent: rgb(0x2196F3),
        }
    }

    /// ag-grid **Quartz Dark** (selected == hover there, per its CSS).
    #[must_use]
    pub fn quartz_dark() -> Self {
        Self {
            name: "Quartz Dark",
            background: rgb(0x1F2936),
            text: rgb(0xFFFFFF),
            header_background: rgb(0x28313E),
            header_text: rgb(0xFFFFFF),
            border: rgb(0x434B56),
            row_divider: Some(rgb(0x434B56)),
            row_hover: rgb(0x1F3F5C),
            row_selected: rgb(0x1F3F5C),
            ..Self::quartz()
        }
    }

    /// ag-grid **Alpine** (the v23–v30 default):
    /// subtle zebra, strong blue selection, bold headers.
    #[must_use]
    pub fn alpine() -> Self {
        Self {
            name: "Alpine",
            row_height: 42.0,
            header_height: 48.0,
            cell_padding: 18.0,
            background: rgb(0xFFFFFF),
            text: rgb(0x181D1F),
            header_background: rgb(0xF8F8F8),
            header_text: rgb(0x181D1F),
            border: rgb(0xBABFC7),
            row_divider: Some(rgb(0xDDE2EB)),
            column_divider: None,
            row_hover: rgb(0xE9F4FE),
            row_selected: rgb(0xBCE0FB),
            row_stripe: Some(rgb(0xFCFCFC)),
            accent: rgb(0x2196F3),
        }
    }

    /// ag-grid **Alpine Dark**.
    #[must_use]
    pub fn alpine_dark() -> Self {
        Self {
            name: "Alpine Dark",
            background: rgb(0x181D1F),
            text: rgb(0xFFFFFF),
            header_background: rgb(0x222628),
            header_text: rgb(0xFFFFFF),
            border: rgb(0x68686E),
            row_divider: Some(rgb(0x383A39)),
            row_hover: rgb(0x192934),
            row_selected: rgb(0x1B415F),
            row_stripe: Some(rgb(0x222628)),
            ..Self::alpine()
        }
    }

    /// ag-grid **Balham**: the dense enterprise/finance theme — small rows,
    /// muted gray header text.
    #[must_use]
    pub fn balham() -> Self {
        Self {
            name: "Balham",
            row_height: 28.0,
            header_height: 32.0,
            cell_padding: 12.0,
            background: rgb(0xFFFFFF),
            text: rgb(0x000000),
            header_background: rgb(0xF5F7F7),
            header_text: rgb(0x717272),
            border: rgb(0xBDC3C7),
            row_divider: Some(rgb(0xD9DCDE)),
            column_divider: None,
            row_hover: rgb(0xECF0F1),
            row_selected: rgb(0xB8E0F9),
            row_stripe: Some(rgb(0xFCFDFE)),
            accent: rgb(0x0091EA),
        }
    }

    /// ag-grid **Balham Dark**.
    #[must_use]
    pub fn balham_dark() -> Self {
        Self {
            name: "Balham Dark",
            background: rgb(0x2D3436),
            text: rgb(0xF5F5F5),
            header_background: rgb(0x1C1C1C),
            header_text: rgb(0xA7A7A7),
            border: rgb(0x424242),
            row_divider: Some(rgb(0x5C5C5C)),
            row_hover: rgb(0x3D4749),
            row_selected: rgb(0x20576E),
            row_stripe: Some(rgb(0x262C2E)),
            accent: rgb(0x00B0FF),
            ..Self::balham()
        }
    }

    /// ag-grid **Material** (MD1 data-table spec: 48 px rows, 56 px header,
    /// 24 px edge padding, headerless band — dividers only).
    #[must_use]
    pub fn material() -> Self {
        Self {
            name: "Material",
            row_height: 48.0,
            header_height: 56.0,
            cell_padding: 24.0,
            background: rgb(0xFFFFFF),
            text: rgb(0x212121),
            header_background: rgb(0xFFFFFF),
            header_text: rgb(0x757575),
            border: rgb(0xE2E2E2),
            row_divider: Some(rgb(0xE2E2E2)),
            column_divider: None,
            row_hover: rgb(0xFAFAFA),
            row_selected: rgb(0xBCE0FB),
            row_stripe: None,
            accent: rgb(0xFF4081),
        }
    }

    /// The **Spreadsheet** look (Excel / Google Sheets): full gridlines,
    /// tight rows, quiet header band, no hover.
    #[must_use]
    pub fn spreadsheet() -> Self {
        Self {
            name: "Spreadsheet",
            row_height: 22.0,
            header_height: 24.0,
            cell_padding: 4.0,
            background: rgb(0xFFFFFF),
            text: rgb(0x000000),
            header_background: rgb(0xF3F3F3),
            header_text: rgb(0x444444),
            border: rgb(0xCCCCCC),
            row_divider: Some(rgb(0xCCCCCC)),
            column_divider: Some(rgb(0xCCCCCC)),
            // Faithful no-op: real spreadsheets don't highlight hovered rows.
            row_hover: rgb(0xFFFFFF),
            row_selected: rgb(0xE5EFFF),
            row_stripe: None,
            accent: rgb(0x1A73E8),
        }
    }

    /// **Bootstrap** `.table-striped.table-hover` (v5.3).
    #[must_use]
    pub fn bootstrap() -> Self {
        Self {
            name: "Bootstrap",
            row_height: 42.0,
            header_height: 42.0,
            cell_padding: 8.0,
            background: rgb(0xFFFFFF),
            text: rgb(0x000000),
            header_background: rgb(0xFFFFFF),
            header_text: rgb(0x000000),
            border: rgb(0xDEE2E6),
            row_divider: Some(rgb(0xDEE2E6)),
            column_divider: None,
            row_hover: rgb(0xECECEC),
            row_selected: rgb(0xE5E5E5),
            row_stripe: Some(rgb(0xF2F2F2)),
            accent: rgb(0x0D6EFD),
        }
    }

    /// **Bootstrap Dark** (`data-bs-theme="dark"`).
    #[must_use]
    pub fn bootstrap_dark() -> Self {
        Self {
            name: "Bootstrap Dark",
            background: rgb(0x212529),
            text: rgb(0xFFFFFF),
            header_background: rgb(0x212529),
            header_text: rgb(0xFFFFFF),
            border: rgb(0x495057),
            row_divider: Some(rgb(0x495057)),
            row_hover: rgb(0x323539),
            row_selected: rgb(0x373B3E),
            row_stripe: Some(rgb(0x2C3034)),
            ..Self::bootstrap()
        }
    }
}