Files
data-views/tests/table_view_render.zi

239 lines
11 KiB
Ziran

#import "kryon/geometry"
#import "kryon/session"
#import "kryon/tree"
#import "kryon/tree_input"
#import "kryon/paint_queue"
#import "kryon/widget_palette"
#import "kryon/style_sheet"
#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"};
data: [4]string = .{"One", "Ready", "Two", "Working"};
rows: [6]Table.TableRow;
widths: [2]s32 = .{120, 120};
selected_row: s32 = -1;
selected_column: s32 = -1;
sort_column: s32 = -1;
sort_direction: s32;
scroll: s32;
multi: bool;
flags: [6]bool;
selection: []bool;
focus: s32 = -1;
anchor: s32 = -1;
row_count: s32 = 6;
last: Table.TableViewResult;
Draw :: (disabled: bool = false) -> bool {
Host.BeginPaint()
TreeStart(session, 1, Rectangle.{0.0, 0.0, 300.0, 120.0})
result := Table.TableView(session, Table.TableViewProps.{
.id = 42, .bounds = .{10.0, 10.0, 240.0, 80.0},
.columns = headers[:], .rows = rows[0:row_count],
.multi_select = multi, .selection = selection, .focused_row = focus, .anchor = anchor,
.column_widths = widths[:], .selected_row = selected_row,
.selected_column = selected_column, .sort_column = sort_column,
.sort_direction = sort_direction, .scroll_offset = scroll,
.row_height = 20, .header_height = 20, .freeze_rows = 1,
.resizable = true, .disabled = disabled})
if !TreeFinish(session) || TreeCount(session) > 20 { return false }
PaintFlush(session)
Host.EndPaint()
last = result
focus = result.focused_row
anchor = result.anchor
selected_row = result.selected_row
selected_column = result.selected_column
sort_column = result.sort_column
sort_direction = result.sort_direction
scroll = result.scroll_offset
return result.changed
}
Click :: (x: float32, y: float32) {
TreePointerUpdate(session, PointerFrame.{x, y, true, true, false})
TreePointerUpdate(session, PointerFrame.{x, y, false, false, true})
}
// 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(43, text) && Host.HasInk(63, selected_text)
if !matches {
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(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
// 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}
dark := WidgetPalette.{.background = cast(u32)0x202020ff, .base = cast(u32)0x101010ff,
.text = cast(u32)0xe0e0e0ff, .border = cast(u32)0x606060ff,
.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.text, light.border) { return 52 }
if !WidgetPaletteUse(session, dark) { return 53 }
if !PaintColors(dark.base, dark.background, cast(u32)0x313d43ff, dark.text, dark.text, dark.border) { return 54 }
rules := ActiveStyleRules()
custom: StyleRule
custom.selector = StyleDefaultSelector()
custom.selector.kind = StyleKindTableView()
custom.style.fields = cast(u32)StyleField.StyleBackground | cast(u32)StyleField.StyleForeground |
cast(u32)StyleField.StyleBorder
custom.style.background = cast(u32)0x523456ff
custom.style.foreground = cast(u32)0xfedcbaff
custom.style.border = cast(u32)0xabcdefff
rules.items[rules.count] = custom
rules.count += 1
if !InstallStyleRules(rules) { return 55 }
if !PaintColors(custom.style.background, custom.style.background, custom.style.background,
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.text, light.border) { return 58 }
if !WidgetPaletteRestore(session) { return 59 }
if !PaintThemeColors(dark_colors) { return 60 }
selected_row = -1
selected_column = -1
unused Draw()
return 0
}
#program_export
main :: () -> s32 {
session = SessionOpen()
for index: 0..5 { rows[index] = .{data[(index % 2) * 2:(index % 2) * 2 + 2]} }
if Draw() || Host.failed || Host.labels < 6 { return 1 }
palette_result := PalettePaintTests()
if palette_result != 0 { return palette_result }
Click(180.0, 60.0)
if !Draw() || selected_row != 1 || selected_column != 1 ||
last.clicked_row != 1 || last.clicked_column != 1 { return 2 }
Click(60.0, 20.0)
if !Draw() || sort_column != 0 || sort_direction != 1 { return 3 }
Click(60.0, 20.0)
if !Draw() || sort_direction != -1 { return 4 }
PointerWheelSend(session, 70.0, 70.0, -1.0)
unused Draw()
if scroll <= 0 { return 5 }
// The frozen first row stays interactive while the body scrolls.
Click(30.0, 40.0)
if !Draw() || selected_row != 0 || selected_column != 0 { return 6 }
KeyboardSend(session, 269)
if !Draw() || selected_row != 5 { return 10 }
KeyboardSend(session, 265)
if !Draw() || selected_row != 4 { return 11 }
KeyboardSend(session, 268)
if !Draw() || selected_row != 0 { return 12 }
unused KeyboardSend(session, 257)
if Draw() || last.activated_row != 0 || last.activated_column != 0 { return 16 }
unused Draw(true)
Click(180.0, 60.0)
if Draw(true) || selected_row != 0 { return 7 }
unused Draw()
TreePointerUpdate(session, PointerFrame.{130.0, 20.0, true, true, false})
TreePointerUpdate(session, PointerFrame.{170.0, 20.0, true, false, false})
if !Draw() || widths[0] != 160 { return 8 }
TreePointerUpdate(session, PointerFrame.{170.0, 20.0, false, false, true})
unused Draw()
if Host.failed { return 9 }
multi = true
selection = flags[:]
scroll = 0
unused Draw()
if !last.selection_valid || last.selection_changed || last.selected_count != 0 { return 20 }
Click(180.0, 40.0)
if !Draw() || !flags[0] || focus != 0 || anchor != 0 ||
!last.selection_changed || !last.focus_changed ||
last.clicked_row != 0 || last.clicked_column != 1 { return 21 }
Click(180.0, 40.0)
if Draw() || last.clicked_row != 0 || last.clicked_column != 1 ||
last.selection_changed || last.focus_changed || last.activated_row != -1 { return 40 }
unused Draw()
if last.clicked_row != -1 || last.clicked_column != -1 { return 41 }
unused KeyboardModifiersSend(session, KeyModifierControl)
Click(30.0, 60.0)
if !Draw() || !flags[0] || !flags[1] || anchor != 1 || focus != 1 { return 22 }
Click(30.0, 60.0)
if !Draw() || flags[1] || !flags[0] || focus != 1 { return 23 }
unused KeyboardModifiersSend(session, KeyModifierShift)
Click(30.0, 80.0)
if !Draw() || flags[0] || !flags[1] || !flags[2] || anchor != 1 || focus != 2 { return 24 }
unused KeyboardModifiersSend(session, KeyModifierControl | KeyModifierShift)
Click(30.0, 40.0)
if !Draw() || last.selected_count != 3 || anchor != 1 || focus != 0 { return 25 }
unused KeyboardModifiersSend(session, KeyModifierControl)
unused KeyboardSend(session, 269)
if !Draw() || focus != 5 || flags[5] || scroll <= 0 || last.selection_changed { return 26 }
unused KeyboardSend(session, 32)
if !Draw() || !flags[5] || last.selected_count != 4 || anchor != 5 { return 27 }
unused KeyboardModifiersSend(session, 0)
unused KeyboardSend(session, 257)
if !Draw() || last.activated_row != 5 || last.activated_column != -1 ||
last.selected_count != 1 || !flags[5] { return 28 }
unused Draw()
if last.activated_row != -1 || last.changed { return 29 }
PointerWheelSend(session, 70.0, 70.0, 1.0)
unused Draw()
if scroll >= 60 || focus != 5 || last.selection_changed { return 39 }
unused KeyboardSend(session, 265)
if !Draw() || focus != 4 || anchor != 4 || !flags[4] || flags[5] { return 30 }
unused KeyboardModifiersSend(session, KeyModifierShift)
unused KeyboardSend(session, 268)
if !Draw() || focus != 0 || anchor != 4 || last.selected_count != 5 { return 31 }
unused KeyboardModifiersSend(session, KeyModifierControl | KeyModifierShift)
unused KeyboardSend(session, 269)
if !Draw() || focus != 5 || anchor != 4 || last.selected_count != 6 { return 32 }
unused KeyboardModifiersSend(session, 0)
unused KeyboardSend(session, 264)
if !Draw() || focus != 5 || last.selected_count != 1 { return 33 }
unused Draw(true)
Click(30.0, 40.0)
if Draw(true) || !flags[5] || focus != 5 { return 34 }
// Sorting leaves row identities and their remapping with the application.
unused Draw()
Click(30.0, 20.0)
if !Draw() || last.selection_changed || !flags[5] || last.selected_count != 1 { return 35 }
selection = flags[0:1]
if Draw() || last.node != -1 || last.selection_valid || !flags[5] { return 36 }
selection = flags[0:0]
row_count = 0
focus = 99
anchor = 99
unused Draw()
if !last.selection_valid || last.selected_count != 0 || focus != -1 || anchor != -1 { return 37 }
if Host.failed { return 38 }
unused SessionClose(session)
return 0
}