Files
kryon/examples/tableview/tableview.zi
T

172 lines
6.8 KiB
Ziran

#import "kryon/geometry"
#import "kryon/session"
#import "kryon/frame_snapshot"
#import "kryon/style_sheet"
Box :: #import "kryon/box";
Text :: #import "kryon/text";
#import "kryon/control_props"
#import "kryon/drawing_props"
Tables :: #import "data_views/TableView";
Trees :: #import "data_views/TreeView";
Board :: #import "data_views/Kanban";
Theme :: #import "kryon/theme";
WidgetContext :: struct { theme: s32; now: float32; disabled: bool; reduced_motion: bool }
WidgetInput :: (session: Session) -> WidgetContext {
snapshot := CaptureFrameInput(session)
result: WidgetContext
result.now = cast(float32)snapshot.elapsed_ms / 1000.0
result.theme = ifx StyleTokenColor("canvas").r < 128 then 2 else 1
result.disabled = example_disabled
result.reduced_motion = example_reduced_motion
return result
}
gallery_view: Rectangle = Rectangle.{.x = 0.0, .y = 0.0, .width = 480.0, .height = 320.0};
gallery_live: bool;
gallery_now: float32;
gallery_reduced_motion: bool;
SetGalleryViewport :: (view: Rectangle, live: bool, now: float32, reduced_motion: bool) {
gallery_view = view; gallery_live = live; gallery_now = now
gallery_reduced_motion = reduced_motion
}
GalleryBounds :: (bounds: Rectangle) -> Rectangle {
if !gallery_live { return bounds }
bounds.x = gallery_view.x + bounds.x * gallery_view.width / 480.0
bounds.y += gallery_view.y
bounds.width *= gallery_view.width / 480.0
return bounds
}
installed_style: s32 = -1;
InstallStyles :: (dark: bool) {
wanted: s32 = ifx dark then 1 else 0
if wanted == installed_style { return }
installed_style = wanted
palette := Theme.DefaultPalette(dark)
rules := ActiveStyleRules()
rule: StyleRule
rule.selector = StyleDefaultSelector(); rule.selector.kind = StyleKindTreeView()
rule.style.fields = cast(u32)StyleField.StyleBackground | cast(u32)StyleField.StyleForeground | cast(u32)StyleField.StyleBorder
rule.style.background = palette.surface; rule.style.foreground = palette.foreground; rule.style.border = palette.border
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
rule.selector.kind = StyleKindTreeViewItem()
rule.style.fields = cast(u32)StyleField.StyleForeground
rule.style.foreground = palette.foreground
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
rule.style.fields |= cast(u32)StyleField.StyleBackground
rule.style.background = palette.selection
rule.style.foreground = palette.selection_text
rule.state = cast(s32)ButtonState.ButtonStateHover
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
rule.state = cast(s32)ButtonState.ButtonStateSelected
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
rule.state = cast(s32)ButtonState.ButtonStateNormal
rule.selector.kind = StyleKindTableView()
rule.selector.role = StyleAny()
rule.style.fields = cast(u32)StyleField.StyleBackground | cast(u32)StyleField.StyleForeground | cast(u32)StyleField.StyleBorder
rule.style.background = palette.surface; rule.style.foreground = palette.foreground; rule.style.border = palette.border
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
rule.state = cast(s32)ButtonState.ButtonStateSelected
rule.style.background = palette.selection
rule.style.foreground = palette.selection_text
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
unused InstallStyleRules(rules)
}
table_headers: [3]string = .{"Example", "Package", "Status"};
table_cells: [18]string = .{"Button", "Kryon", "Ready", "TreeView", "DataViews", "Ready", "Plot", "Plot", "Interactive", "Image", "Kryon", "Embedded", "TableView", "DataViews", "Ready", "Kanban", "DataViews", "Draggable"};
table_rows: [6]Tables.TableRow;
table_widths: [3]s32 = .{154, 144, 134};
table_selection: s32 = -1;
table_column: s32 = -1;
table_sort: s32 = -1;
table_direction: s32;
table_scroll: s32;
table_ready: bool;
TableTextOrder :: (left: string, right: string) -> s32 {
index: s64
while index < left.count && index < right.count {
if left[index] < right[index] { return -1 }
if left[index] > right[index] { return 1 }
index += 1
}
if left.count < right.count { return -1 }
if left.count > right.count { return 1 }
return 0
}
bounds: Rectangle;
Prepare :: (session: Session, scene: s32) {
bounds = GalleryBounds(Rectangle.{24.0, 64.0, 432.0, 208.0})
}
#program_export
Frame :: (session: Session, viewport: Rectangle) -> s32 {
example_input := WidgetInput(session)
SetGalleryViewport(viewport, true, example_input.now, example_input.reduced_motion)
Box.Draw(session, Box.Props.{.key = 9000, .bounds = viewport, .fill = StyleTokenColor("canvas")})
Text.Draw(session, Text.Props.{.key = 9001, .bounds = .{viewport.x + 24.0, viewport.y + 12.0, viewport.width - 48.0, 36.0}, .text = "TableView", .size = 23})
disabled := example_input.disabled
scene: s32 = 2
InstallStyles(example_input.theme == 2)
Prepare(session, scene)
if !table_ready {
for index: 0..5 { table_rows[index].cells = table_cells[index * 3:index * 3 + 3] }
table_ready = true
}
result := Tables.TableView(session, Tables.TableViewProps.{.key = 600,
.bounds = bounds, .columns = table_headers[:], .rows = table_rows[:],
.column_widths = table_widths[:], .selected_row = table_selection,
.selected_column = table_column, .sort_column = table_sort,
.sort_direction = table_direction, .scroll_offset = table_scroll,
.row_height = 36, .header_height = 36, .resizable = true,
.disabled = disabled})
table_selection = result.selected_row; table_column = result.selected_column
table_scroll = result.scroll_offset
if result.sort_column != table_sort || result.sort_direction != table_direction {
table_sort = result.sort_column; table_direction = result.sort_direction
if table_sort >= 0 && table_direction != 0 {
for pass: 0..4 {
for index: 0..4 - pass {
if TableTextOrder(table_rows[index].cells[table_sort], table_rows[index + 1].cells[table_sort]) * table_direction > 0 {
swap := table_rows[index]; table_rows[index] = table_rows[index + 1]; table_rows[index + 1] = swap
}
}
}
} else {
for index: 0..5 { table_rows[index].cells = table_cells[index * 3:index * 3 + 3] }
}
table_selection = -1; table_column = -1
}
return 0
}
example_disabled: bool;
example_reduced_motion: bool;
// Hosts publish these options before the unchanged application entry point.
#program_export
SetExampleOptions :: (disabled: bool, reduced_motion: bool) {
example_disabled = disabled
example_reduced_motion = reduced_motion
}