Follow the active Kryon theme in TableView

This commit is contained in:
waozi committed 2026-11-20 00:43:07 -03:00
1 parent 42f2466a96
commit 45dc6669ba
6 files changed
+69 -40

No files matched your search

+3 -3
View File
@@ -5,9 +5,9 @@ DataViews adds `TableView`, `TreeView` and `Kanban` to
Kryon widgets written in Ziran: the table draws a header, rows, cells, and a
keyboard selection over caller-owned data, and the tree shows nested items with keyboard navigation. They use Kryon's styles, so
the `TableView` and `TreeView` rules in any Kryon style pack apply.
Table colors follow the current semantic palette, including scoped
`WidgetPaletteUse` light/dark colors and restoration. Explicit TableView style
rules take precedence; without palette tokens the original default colors apply.
Table colors follow Kryon’s active theme as it changes. Scoped `WidgetPaletteUse`
colors and other explicit tokens override those defaults. TableView style rules
take precedence over both.
## Use it
+19 -9
View File
@@ -15,11 +15,12 @@
#import "kryon/surface"
#import "kryon/scroll"
#import "kryon/widget_kind"
Theme :: #import "kryon/theme";
using WidgetKind;
using ButtonState;
using StyleField;
// Tokens supply defaults only; installed caller rules still resolve last.
// Explicit tokens override the active theme; caller style rules resolve last.
TableTokenColor :: (name: string, fallback: u32) -> u32 {
color := StyleTokenColorOr(name, ColorFromPacked(fallback))
return (cast(u32)color.r << 24) | (cast(u32)color.g << 16) |
@@ -27,17 +28,23 @@ TableTokenColor :: (name: string, fallback: u32) -> u32 {
}
TableFace :: (class_name: s32, role: s32, state: s32) -> StyleFrame {
palette := Theme.CurrentPalette()
defaults: StyleData
defaults.fields = cast(u32)StyleBackground | cast(u32)StyleForeground |
cast(u32)StyleBorder | cast(u32)StyleBorderWidth | cast(u32)StyleOpacity
defaults.background = TableTokenColor("surface", cast(u32)0xf8fafcff)
defaults.foreground = TableTokenColor("text", cast(u32)0x1e293bff)
defaults.border = TableTokenColor("border", cast(u32)0xcbd5e1ff)
defaults.background = TableTokenColor("surface", palette.surface)
defaults.foreground = TableTokenColor("text", palette.foreground)
defaults.border = TableTokenColor("border", palette.border)
defaults.border_width = 1.0
defaults.opacity = 1.0
if role == TableViewHeaderRole() { defaults.background = TableTokenColor("canvas", cast(u32)0xe2e8f0ff) }
if state == ButtonStateSelected { defaults.background = TableTokenColor("accent-soft", cast(u32)0xcffafeff) }
if state == ButtonStateHover { defaults.background = TableTokenColor("button", cast(u32)0xe0f2feff) }
if role == TableViewHeaderRole() { defaults.background = TableTokenColor("canvas", palette.surface_muted) }
if state == ButtonStateSelected {
defaults.background = TableTokenColor("accent-soft", palette.selection)
defaults.foreground = TableTokenColor("text", palette.selection_text)
}
if state == ButtonStateHover {
defaults.background = TableTokenColor("button", Theme.StateLayer(palette.surface, palette.foreground, 10))
}
if state == ButtonStateDisabled { defaults.opacity = 0.45 }
frame: StyleFrame
frame.value = ResolveInstalledStyleRules(defaults,
@@ -124,6 +131,10 @@ TableView :: (session: Session, props: TableViewProps) -> TableViewResult {
panel := TableFace(props.class_name, TableViewPanelRole(), state)
header := TableFace(props.class_name, TableViewHeaderRole(), state)
cell := TableFace(props.class_name, TableViewCellRole(), state)
selected_cell := cell
if !props.disabled {
selected_cell = TableFace(props.class_name, TableViewCellRole(), cast(s32)ButtonStateSelected)
}
metrics := TableViewMetricsFor(1.0, panel, header, cell, cell)
layout := TableViewLayoutFor(bounds, cast(s32)props.rows.count, props.row_height,
props.header_height, props.freeze_rows, 1.0, metrics)
@@ -337,8 +348,7 @@ TableView :: (session: Session, props: TableViewProps) -> TableViewResult {
selected := result.selected_row == row &&
(result.selected_column < 0 || result.selected_column == column)
if props.multi_select { selected = props.selection[row] }
face := cell
if selected { face = TableFace(props.class_name, TableViewCellRole(), ifx props.disabled then cast(s32)ButtonStateDisabled else cast(s32)ButtonStateSelected) }
face := ifx selected then selected_cell else cell
cell_node := TreeSubmitCurrent(session, cell_key, WidgetKindSelectable, clip)
TreeSetInteractive(session, cell_node, props.disabled, false, props.id)
TreeSetSelected(session, cell_node, selected)
+2 -2
View File
@@ -10,8 +10,8 @@ PixelIs :: (x: s32, y: s32, rgba: u32) -> bool {
argb := (rgba >> 8) | ((rgba & cast(u32)255) << 24)
return pixels[y * 300 + x] == argb
}
HasInk :: (rgba: u32) -> bool {
for y: 43..58 {
HasInk :: (top: s32, rgba: u32) -> bool {
for y: top..top + 15 {
for x: 18..42 { if PixelIs(cast(s32)x, cast(s32)y, rgba) { return true } }
}
return false
+32 -13
View File
@@ -8,6 +8,8 @@
#import "kryon/control_props"
using Table :: #import "TableView";
Host :: #import "table_view_host";
SystemTheme :: #import "kryon/system_theme";
Theme :: #import "kryon/theme";
session: Session;
headers: [2]string = .{"Name", "Status"};
@@ -57,23 +59,41 @@ Click :: (x: float32, y: float32) {
TreePointerUpdate(session, PointerFrame.{x, y, true, true, false})
TreePointerUpdate(session, PointerFrame.{x, y, false, false, true})
}
// Sample real raster output away from glyphs, plus an opaque glyph pixel.
PaintColors :: (body: u32, header: u32, selected: u32, text: u32, border: u32) -> bool {
// Sample the real raster fills, border, and ink in normal and selected cells.
PaintColors :: (body: u32, header: u32, selected: u32, text: u32, selected_text: u32, border: u32) -> bool {
unused Draw()
matches := Host.PixelIs(100, 50, body) && Host.PixelIs(100, 20, header) &&
Host.PixelIs(100, 70, selected) && Host.PixelIs(10, 50, border) && Host.HasInk(text)
Host.PixelIs(100, 70, selected) && Host.PixelIs(10, 50, border) &&
Host.HasInk(43, text) && Host.HasInk(63, selected_text)
if !matches {
print("Table pixels: body=% header=% selected=% border=% ink=%\n",
print("Table pixels: body=% header=% selected=% border=% ink=% selected ink=%\n",
Host.pixels[50 * 300 + 100], Host.pixels[20 * 300 + 100],
Host.pixels[70 * 300 + 100], Host.pixels[50 * 300 + 10], Host.HasInk(text))
Host.pixels[70 * 300 + 100], Host.pixels[50 * 300 + 10], Host.HasInk(43, text), Host.HasInk(63, selected_text))
}
return matches
}
PaintThemeColors :: (colors: Theme.Palette) -> bool {
return PaintColors(colors.surface, colors.surface_muted, colors.selection,
colors.foreground, colors.selection_text, colors.border)
}
PalettePaintTests :: () -> s32 {
selected_row = 1
selected_column = -1
if !PaintColors(cast(u32)0xf8fafcff, cast(u32)0xe2e8f0ff,
cast(u32)0xcffafeff, cast(u32)0x1e293bff, cast(u32)0xcbd5e1ff) { return 50 }
// The default system path updates the typed palette without installing style tokens.
light_system := SystemTheme.SystemPalette.{.found = true,
.background = cast(u32)0xe8e8e8ff, .base = cast(u32)0xf6f6f6ff,
.text = cast(u32)0x101010ff, .border = cast(u32)0x9a9a9aff,
.selected = cast(u32)0x3170bfff}
dark_system := SystemTheme.SystemPalette.{.found = true, .dark = true,
.background = cast(u32)0x232323ff, .base = cast(u32)0x151515ff,
.text = cast(u32)0xefefefff, .border = cast(u32)0x585858ff,
.selected = cast(u32)0x88bdf4ff}
light_colors := SystemTheme.SystemPaletteColors(light_system)
dark_colors := SystemTheme.SystemPaletteColors(dark_system)
if !SystemTheme.SystemPaletteUpdateDefault(light_system) || !PaintThemeColors(light_colors) { return 50 }
if !SystemTheme.SystemPaletteUpdateDefault(dark_system) || !PaintThemeColors(dark_colors) { return 61 }
if !SystemTheme.SystemPaletteUpdateDefault(light_system) || !PaintThemeColors(light_colors) { return 62 }
if !SystemTheme.SystemPaletteUpdateDefault(dark_system) { return 63 }
light := WidgetPalette.{.background = cast(u32)0xf0f0f0ff, .base = cast(u32)0xffffffff,
.text = cast(u32)0x202020ff, .border = cast(u32)0x909090ff,
.selected = cast(u32)0x4080c0ff, .selected_text = cast(u32)0xffffffff}
@@ -82,9 +102,9 @@ PalettePaintTests :: () -> s32 {
.selected = cast(u32)0x80c0e0ff, .selected_text = cast(u32)0x101010ff}
if !WidgetPaletteUse(session, light) { return 51 }
// accent-soft is the canonical 18% accent tint, retaining normal text.
if !PaintColors(light.base, light.background, cast(u32)0xd0dce7ff, light.text, light.border) { return 52 }
if !PaintColors(light.base, light.background, cast(u32)0xd0dce7ff, light.text, light.text, light.border) { return 52 }
if !WidgetPaletteUse(session, dark) { return 53 }
if !PaintColors(dark.base, dark.background, cast(u32)0x313d43ff, dark.text, dark.border) { return 54 }
if !PaintColors(dark.base, dark.background, cast(u32)0x313d43ff, dark.text, dark.text, dark.border) { return 54 }
rules := ActiveStyleRules()
custom: StyleRule
custom.selector = StyleDefaultSelector()
@@ -98,13 +118,12 @@ PalettePaintTests :: () -> s32 {
rules.count += 1
if !InstallStyleRules(rules) { return 55 }
if !PaintColors(custom.style.background, custom.style.background, custom.style.background,
custom.style.foreground, custom.style.border) { return 56 }
custom.style.foreground, custom.style.foreground, custom.style.border) { return 56 }
// Restoring the nested scope restores light tokens and its original rules.
if !WidgetPaletteRestore(session) { return 57 }
if !PaintColors(light.base, light.background, cast(u32)0xd0dce7ff, light.text, light.border) { return 58 }
if !PaintColors(light.base, light.background, cast(u32)0xd0dce7ff, light.text, light.text, light.border) { return 58 }
if !WidgetPaletteRestore(session) { return 59 }
if !PaintColors(cast(u32)0xf8fafcff, cast(u32)0xe2e8f0ff,
cast(u32)0xcffafeff, cast(u32)0x1e293bff, cast(u32)0xcbd5e1ff) { return 60 }
if !PaintThemeColors(dark_colors) { return 60 }
selected_row = -1
selected_column = -1
unused Draw()
+11 -11
View File
@@ -46,6 +46,15 @@
"ref": "f5f186bb13569ef13338f81366ff63fb9b9282f6",
"url": "https://github.com/ziranlang/avif.git"
},
{
"commit": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91",
"dependencies": {"audio": "p59deb49c7f103127", "avif": "p3ed49c5d0912e411", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"},
"dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"},
"id": "p4f02cca35d4a652d",
"name": "kryon",
"ref": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91",
"url": "https://github.com/kryonlabs/kryon.git"
},
{
"commit": "1aadfad932c98c069a1204261b1856f81f3bc199",
"dependencies": {},
@@ -90,15 +99,6 @@
"source": true,
"url": "https://aomedia.googlesource.com/aom"
},
{
"commit": "35fc05cbb776ed17fecf760fe8ae85127e01f5a3",
"dependencies": {"audio": "p59deb49c7f103127", "avif": "p3ed49c5d0912e411", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"},
"dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"},
"id": "p9543cfaa0f8911a8",
"name": "kryon",
"ref": "35fc05cbb776ed17fecf760fe8ae85127e01f5a3",
"url": "https://git.waozi.xyz/kryonlabs/kryon.git"
},
{
"commit": "54706fc6bc0cdecab7e9593974a4039cc038fca7",
"dependencies": {},
@@ -119,8 +119,8 @@
}
],
"root": {
"dependencies": {"kryon": "p9543cfaa0f8911a8"},
"dependency_refs": {"kryon": "35fc05cbb776ed17fecf760fe8ae85127e01f5a3"},
"dependencies": {"kryon": "p4f02cca35d4a652d"},
"dependency_refs": {"kryon": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91"},
"name": "DataViews"
},
"toolchain": {
+2 -2
View File
@@ -7,8 +7,8 @@ git = "https://git.waozi.xyz/ziranlang/ziran.git"
ref = "9432a110709c4a4f72c4f732fcfc368e15213a54"
[dependencies.kryon]
git = "https://git.waozi.xyz/kryonlabs/kryon.git"
ref = "35fc05cbb776ed17fecf760fe8ae85127e01f5a3"
git = "https://github.com/kryonlabs/kryon.git"
ref = "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91"
[exports]
TableView = "src/TableView/module.zi"