diff --git a/CHANGELOG.md b/CHANGELOG.md index 669ae45bb..e49f6b739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- New widgets: `kryon/chip` (selectable and dismissible pills), `kryon/list_item` (leading icon or avatar, title, subtitle, trailing text, badge, chevron or switch) and `kryon/skeleton` (block, line and circle placeholders that pulse unless motion is reduced). Each takes its size from the theme, draws where its container places it, and has a gallery example and a ui-map entry. + - When no app answers, the test driver names the fix: start and watch the app from the test (`StartOnDisplay`, `DriverEnvironment`, `DriverWatch`) instead of a hand-started Xvfb or app, or build it with `KRYON_TEST_DRIVER=1`. docs/FRAME_REPLAY.md lists all five frame capture bindings a replay host needs. - Project builds create stamp files by truncation instead of touch timestamps and set published page and executable modes through fchmod, so restricted builds that deny path chmod and timestamp syscalls still produce readable pages and runnable programs. diff --git a/Makefile b/Makefile index 16cedec11..f5b36a95f 100644 --- a/Makefile +++ b/Makefile @@ -432,6 +432,15 @@ driver-messages-test: @env -u DISPLAY -u WAYLAND_DISPLAY "$(ZIRAN_BIN)" run --target=c tests/driver_messages_test.zi test: driver-messages-test +# Chip, ListItem and Skeleton shot light and dark and driven on a private display. +.PHONY: new-widgets-test +new-widgets-test: + @rm -rf build/test/new-widgets + @env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \ + HARMONY_DESKTOP_RECOVERY=0 YUE_DESKTOP_RECOVERY=0 ZIRAN_BIN="$(ZIRAN_BIN)" \ + "$(ZIRAN_BIN)" run --target=c tests/new_widgets_test.zi +test: new-widgets-test + # Every supported target -- C, Go, and the portable runner -- # renders each template and example on the headless pixmap host, and every # image must equal C's byte for byte. It needs go and python3, and diff --git a/docs/CHEATSHEET.md b/docs/CHEATSHEET.md index b5006dd03..9b8d0c28a 100644 --- a/docs/CHEATSHEET.md +++ b/docs/CHEATSHEET.md @@ -153,12 +153,14 @@ NotesScreen :: (session: Session) { ```zi // file: src/widgets.zi -Avatar :: #import "kryon/avatar"; // .name .size; initials when no .image Button :: #import "kryon/button"; // .label .tone .emphasis .size .disabled Checkbox :: #import "kryon/checkbox"; // .label .checked -> .changed .checked +Chip :: #import "kryon/chip"; // .label .selectable .dismissible -> .changed .dismissed Layout :: #import "kryon/layout"; +ListItem :: #import "kryon/list_item"; // .title .subtitle .leading_name .trailing -> .clicked Progress :: #import "kryon/progress"; // .min .max .value .label Radio :: #import "kryon/radio"; // .label .checked -> 1 when picked +Skeleton :: #import "kryon/skeleton"; // .shape .now: a pulsing placeholder Slider :: #import "kryon/slider"; // DiscreteSlider: .value .min .max -> .changed .value Text :: #import "kryon/text"; // .text .size .wrap .align .strong Theme :: #import "kryon/theme"; @@ -191,11 +193,9 @@ Widgets :: (session: Session) { } bar := Slot(session, 8) if bar.opened { Progress.Draw(session, Progress.Props.{.max = 5, .value = level, .bounds = bar.bounds}); unused End(session) } - tag := Slot(session, 40) - if tag.opened { - unused Avatar.Draw(session, Avatar.Props.{.name = "Ada Lovelace", .bounds = .{tag.bounds.x, tag.bounds.y, 40, 40}}) - unused End(session) - } + unused Chip.Draw(session, Chip.Props.{.label = "Design", .selectable = true, .selected = on}) + unused ListItem.Draw(session, ListItem.Props.{.title = "Ada", .subtitle = "Profile", .leading_name = "Ada Lovelace", .trailing = .ListTrailingChevron}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonLine}) Layouts(session) } ``` @@ -204,8 +204,8 @@ Also: `kryon/text_field` (`Bind(session, *state, props)`), `kryon/card` (`.variant` -> `.content`), `kryon/title_bar` (`.trailing_width` -> `.trailing_bounds`), `kryon/box` (`.fill .border .radius`), `kryon/dropdown` (`Draw(session, props, options)`), `kryon/modal` (`opened` -> `End`, -`.dismissed`), `kryon/toast`, `kryon/image` (`.asset_path .fit`), -`kryon/badge` (`.text .tone`), `kryon/scroll`. `ziran api kryon/` prints an API. +`.dismissed`), `kryon/avatar` (`.name`), `kryon/image` (`.asset_path .fit`), +`kryon/badge`, `kryon/toast`, `kryon/scroll`; `ziran api kryon/` prints an API. ## Layout recipe diff --git a/docs/ui-map.json b/docs/ui-map.json index 9e3fd3753..ce2782770 100644 --- a/docs/ui-map.json +++ b/docs/ui-map.json @@ -31,6 +31,9 @@ {"html": ["details"], "import": "kryon/collapsible", "call": "Draw", "css": "Collapsible", "lists": [["headers", "CollapsibleHeader"]], "content": "result.content"}, {"html": ["dialog"], "import": "kryon/modal", "call": "Draw", "css": "Modal", "scope": "opened"}, {"html": ["hr"], "import": "kryon/separator", "call": "Draw", "css": "Separator"}, + {"html": ["li", "[role=listitem]"], "import": "kryon/list_item", "call": "Draw", "css": "Button", "event": "clicked"}, + {"html": [".chip", "[role=option]"], "import": "kryon/chip", "call": "Draw", "css": "Button", "event": "changed"}, + {"html": [".skeleton", "[aria-busy=true]"], "import": "kryon/skeleton", "call": "Draw", "css": "Box"}, {"html": ["img"], "import": "kryon/image", "call": "Draw", "css": "Image"}, {"html": ["svg"], "import": "kryon/svg_icon", "call": "Draw"} ], diff --git a/examples/chip/chip.zi b/examples/chip/chip.zi new file mode 100644 index 000000000..f726838ff --- /dev/null +++ b/examples/chip/chip.zi @@ -0,0 +1,45 @@ +Chip :: #import "kryon/chip"; +Flow :: #import "kryon/flow"; +Text :: #import "kryon/text"; +#import "kryon/control_props" +#import "kryon/geometry" +#import "kryon/session" +#import "kryon/tree" + +// Selectable filters and dismissible tags wrapped by a Flow. +filters: [4]bool = .[true, false, false, false]; +tags: [3]bool = .[true, true, true]; +filter_names: [4]string = .["All", "Design", "Code", "Docs"]; +tag_names: [3]string = .["Draft", "Shared", "Pinned"]; + +#program_export +Frame :: (session: Session, viewport: Rectangle) -> s32 { + Text.Draw(session, Text.Props.{.disabled = example_disabled, .bounds = .{viewport.x + 20.0, viewport.y + 16.0, viewport.width - 40.0, 40.0}, .text = "Chip", .size = 28}) + filter_row := Flow.Draw(session, Flow.Props.{.bounds = .{viewport.x + 20.0, viewport.y + 72.0, viewport.width - 40.0, 40.0}, .gap = 8}) + if filter_row.opened { + for i: 0..3 { + chip := Chip.Draw(session, Chip.Props.{.label = filter_names[i], .selectable = true, + .selected = filters[i], .tone = .ButtonToneAccent, .disabled = example_disabled}) + if chip.changed { filters[i] = chip.selected } + } + unused End(session) + } + tag_row := Flow.Draw(session, Flow.Props.{.bounds = .{viewport.x + 20.0, viewport.y + 124.0, viewport.width - 40.0, 40.0}, .gap = 8}) + if tag_row.opened { + for i: 0..2 { + if tags[i] && Chip.Draw(session, Chip.Props.{.label = tag_names[i], .dismissible = true, .disabled = example_disabled}).dismissed { tags[i] = false } + } + unused End(session) + } + 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 +} diff --git a/examples/chip/ziran.toml b/examples/chip/ziran.toml new file mode 100644 index 000000000..26cfe19a8 --- /dev/null +++ b/examples/chip/ziran.toml @@ -0,0 +1,26 @@ +[package] +name = "chip" +entry = "chip.zi" +module_roots = ["."] +tool = "kryon" + +[toolchain] +git = "https://git.waozi.xyz/ziranlang/ziran.git" +ref = "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0" + +[tool.kryon] +default_profile = "desktop" + +[tool.kryon.profiles.desktop] +backend = "desktop" + +[tool.kryon.profiles.web] +backend = "canvas" + +[tool.kryon.profiles.portable] +backend = "desktop" +codegen = "zx" + +[dependencies.kryon] +git = "https://git.waozi.xyz/kryonlabs/kryon.git" +ref = "c579fd1780eae1653ff8ad737be924d0df6b8d83" diff --git a/examples/listitem/listitem.zi b/examples/listitem/listitem.zi new file mode 100644 index 000000000..b1d0efa7f --- /dev/null +++ b/examples/listitem/listitem.zi @@ -0,0 +1,36 @@ +Layout :: #import "kryon/layout"; +ListItem :: #import "kryon/list_item"; +Text :: #import "kryon/text"; +#import "kryon/geometry" +#import "kryon/session" +#import "kryon/tree" + +// A settings list: an avatar row, a switch row, a badge row and a chevron row. +notifications: bool = true; + +#program_export +Frame :: (session: Session, viewport: Rectangle) -> s32 { + list := Layout.Draw(session, Layout.Props.{.bounds = .{viewport.x + 12.0, viewport.y + 12.0, viewport.width - 24.0, viewport.height - 24.0}}) + if !list.opened { return 0 } + unused ListItem.Draw(session, ListItem.Props.{.title = "Ada Lovelace", .subtitle = "Account and profile", + .leading_name = "Ada Lovelace", .trailing = .ListTrailingChevron, .disabled = example_disabled}) + row := ListItem.Draw(session, ListItem.Props.{.title = "Notifications", .trailing = .ListTrailingSwitch, + .switch_on = notifications, .disabled = example_disabled}) + if row.switch_changed { notifications = row.switch_on } + unused ListItem.Draw(session, ListItem.Props.{.title = "Inbox", .trailing = .ListTrailingBadge, + .trailing_text = "12", .disabled = example_disabled}) + unused ListItem.Draw(session, ListItem.Props.{.title = "Storage", .subtitle = "4.2 GB of 15 GB", + .trailing = .ListTrailingText, .trailing_text = "28%", .disabled = example_disabled}) + unused End(session) + 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 +} diff --git a/examples/listitem/ziran.toml b/examples/listitem/ziran.toml new file mode 100644 index 000000000..a392e2c20 --- /dev/null +++ b/examples/listitem/ziran.toml @@ -0,0 +1,26 @@ +[package] +name = "listitem" +entry = "listitem.zi" +module_roots = ["."] +tool = "kryon" + +[toolchain] +git = "https://git.waozi.xyz/ziranlang/ziran.git" +ref = "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0" + +[tool.kryon] +default_profile = "desktop" + +[tool.kryon.profiles.desktop] +backend = "desktop" + +[tool.kryon.profiles.web] +backend = "canvas" + +[tool.kryon.profiles.portable] +backend = "desktop" +codegen = "zx" + +[dependencies.kryon] +git = "https://git.waozi.xyz/kryonlabs/kryon.git" +ref = "c579fd1780eae1653ff8ad737be924d0df6b8d83" diff --git a/examples/skeleton/skeleton.zi b/examples/skeleton/skeleton.zi new file mode 100644 index 000000000..3cf0d4588 --- /dev/null +++ b/examples/skeleton/skeleton.zi @@ -0,0 +1,32 @@ +Layout :: #import "kryon/layout"; +Skeleton :: #import "kryon/skeleton"; +#import "kryon/geometry" +#import "kryon/session" +#import "kryon/tree" + +// A card's shape while its content loads: an avatar, two lines and a block. +seconds: float64; + +#program_export +Frame :: (session: Session, viewport: Rectangle) -> s32 { + if !example_reduced_motion { seconds += 1.0 / 60.0 } + column := Layout.Draw(session, Layout.Props.{.bounds = .{viewport.x + 20.0, viewport.y + 20.0, viewport.width - 40.0, viewport.height - 40.0}, + .gap = 10, .gap_override = true}) + if !column.opened { return 0 } + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonCircle, .now = seconds}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonLine, .now = seconds}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonLine, .now = seconds, .bounds = .{width = column.content.width * 0.6}}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonBlock, .now = seconds}) + unused End(session) + 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 +} diff --git a/examples/skeleton/ziran.toml b/examples/skeleton/ziran.toml new file mode 100644 index 000000000..8168e4391 --- /dev/null +++ b/examples/skeleton/ziran.toml @@ -0,0 +1,26 @@ +[package] +name = "skeleton" +entry = "skeleton.zi" +module_roots = ["."] +tool = "kryon" + +[toolchain] +git = "https://git.waozi.xyz/ziranlang/ziran.git" +ref = "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0" + +[tool.kryon] +default_profile = "desktop" + +[tool.kryon.profiles.desktop] +backend = "desktop" + +[tool.kryon.profiles.web] +backend = "canvas" + +[tool.kryon.profiles.portable] +backend = "desktop" +codegen = "zx" + +[dependencies.kryon] +git = "https://git.waozi.xyz/kryonlabs/kryon.git" +ref = "c579fd1780eae1653ff8ad737be924d0df6b8d83" diff --git a/scripts/gallery.py b/scripts/gallery.py index c24ba11ed..194a99ec9 100644 --- a/scripts/gallery.py +++ b/scripts/gallery.py @@ -246,6 +246,7 @@ def prepare(only=None, *, catalog=None, site=SITE, examples=None): # Standalone public components are maintained directly in examples/. additional = { 'alert': 'Alert', 'avatar': 'Avatar', 'badge': 'Badge', 'banner': 'Banner', + 'chip': 'Chip', 'listitem': 'List item', 'skeleton': 'Skeleton', 'buttongroup': 'Button group', 'confirmbutton': 'Confirm button', 'copybutton': 'Copy button', 'countdown': 'Countdown', 'emptystate': 'Empty state', 'keycap': 'Keycap', 'breadcrumb': 'Breadcrumb', 'svgicon': 'SVG icon', @@ -257,12 +258,12 @@ def prepare(only=None, *, catalog=None, site=SITE, examples=None): } component_categories = { 'Actions': ['buttongroup', 'confirmbutton', 'copybutton', 'holdbutton'], - 'Feedback': ['alert', 'badge', 'banner', 'countdown', 'emptystate', 'loading', 'loadingscreen', 'checklist', 'progressring', 'timeline'], + 'Feedback': ['alert', 'badge', 'banner', 'countdown', 'emptystate', 'loading', 'loadingscreen', 'skeleton', 'checklist', 'progressring', 'timeline'], 'Navigation': ['breadcrumb'], - 'Inputs': ['dpad', 'joystick', 'swipearea', 'swiperow'], + 'Inputs': ['chip', 'dpad', 'joystick', 'swipearea', 'swiperow'], 'Layout': ['gridmulti'], 'Media & graphics': ['avatar', 'svgicon', 'carousel', 'tilemap'], - 'Content': ['keycap'], + 'Content': ['keycap', 'listitem'], } core_count = sum(entry["moduleId"] == "kryon" for entry in entries) for extra, (slug, name) in enumerate(additional.items()): diff --git a/src/ui/chip/chip.zi b/src/ui/chip/chip.zi new file mode 100644 index 000000000..2eaa5f94c --- /dev/null +++ b/src/ui/chip/chip.zi @@ -0,0 +1,91 @@ +// A chip: a short pill label with an optional leading icon. A selectable +// chip turns its selection on and off when pressed; a dismissible chip ends +// in a "Remove" press target drawn as ×. Tone, states and look come from +// the theme through Button's faces, and Measure gives the natural size, so +// a Flow or Row places chips without arithmetic. +Button :: #import "button"; +Layout :: #import "layout"; +Text :: #import "text"; +#import "control_props" +#import "geometry" +#import "icon" +#import "session" +#import "text_align" +#import "theme" +#import "tree" + +Props :: struct { + key: u64 + // Zero width or height takes the measured size. + bounds: Rectangle + id: s32 + class_name: s32 + label: string + icon_type: s32 + tone: ButtonTone + selectable: bool + selected: bool + dismissible: bool + disabled: bool +} + +ChipResult :: struct { + // The selection after this frame, and whether a press changed it. + selected: bool + changed: bool + // The close control was pressed. + dismissed: bool +} + +// The pill itself: a small outlined button, soft when selected. +PillProps :: (props: Props) -> Button.Props { + emphasis := ButtonEmphasis.ButtonEmphasisOutline + if props.selected { emphasis = .ButtonEmphasisSoft } + return Button.Props.{.key = props.key, .id = props.id, .class_name = props.class_name, + .label = props.label, .icon_type = props.icon_type, .tone = props.tone, + .emphasis = emphasis, .size = .ControlSizeSmall, .pill = true, + .selected = props.selected, .disabled = props.disabled} +} + +// The chip's natural size, its close control included. +Measure :: (props: Props) -> Rectangle { + size := Button.ButtonMeasure(PillProps(props)) + width := size.width + if props.dismissible { width += size.height } + return Rectangle.{0.0, 0.0, width, size.height} +} + +Draw :: (session: Session, props: Props) -> ChipResult { + result := ChipResult.{selected = props.selected} + size := Measure(props) + bounds := props.bounds + if bounds.width <= 0.0 { bounds.width = size.width } + if bounds.height <= 0.0 { bounds.height = size.height } + pill := PillProps(props) + pressed := false + if !props.dismissible { + pill.bounds = bounds + pressed = Button.Draw(session, pill) == 1 + } else { + slot := Layout.Stack(session, Layout.Props.{.key = props.key, .bounds = bounds, + .padding_override = true}) + if !slot.opened { return result } + area := slot.bounds + pill.key = 0 + pill.bounds = area + pill.content_alignment = .ContentAlignmentStart + pressed = Button.Draw(session, pill) == 1 + close := Rectangle.{area.x + area.width - area.height, area.y, area.height, area.height} + result.dismissed = Button.Draw(session, Button.Props.{.label = "Remove", .invisible = true, + .bounds = close, .disabled = props.disabled}) == 1 + Text.Draw(session, Text.Props.{.bounds = close, .text = "×", .align = .TextAlignCenter, + .vertical_align = .TextAlignCenter, .size = cast(s32)CurrentThemeMetric(.ThemeMetricTextBody), + .disabled = props.disabled}) + unused End(session) + } + if pressed && props.selectable && !props.disabled { + result.selected = !props.selected + result.changed = true + } + return result +} diff --git a/src/ui/listitem/listitem.zi b/src/ui/listitem/listitem.zi new file mode 100644 index 000000000..6a4006023 --- /dev/null +++ b/src/ui/listitem/listitem.zi @@ -0,0 +1,114 @@ +// A list row: a leading icon or avatar, a title with an optional subtitle, +// and an optional trailing text, badge, chevron or switch. The row is one +// pressable surface with hover, pressed, selected and disabled states from +// Button's faces; Measure gives its height from the theme, so a column of +// rows needs no arithmetic. +Avatar :: #import "avatar"; +Badge :: #import "badge"; +Button :: #import "button"; +Layout :: #import "layout"; +Text :: #import "text"; +Toggle :: #import "toggle"; +#import "control_props" +#import "geometry" +#import "icon" +#import "session" +#import "text_align" +#import "theme" +#import "tree" + +ListTrailing :: enum { + ListTrailingNone :: 0 + ListTrailingText :: 1 + ListTrailingBadge :: 2 + ListTrailingChevron :: 3 + ListTrailingSwitch :: 4 +} + +Props :: struct { + key: u64 + // Zero width fills the column; zero height takes the measured height. + bounds: Rectangle + id: s32 + class_name: s32 + title: string + subtitle: string + // A leading icon, or an avatar of leading_name's initials. + leading_icon: s32 + leading_name: string + trailing: ListTrailing + // The trailing text or badge text. + trailing_text: string + // The trailing switch's value. + switch_on: bool + selected: bool + disabled: bool +} + +ListItemResult :: struct { + // The row was pressed. + clicked: bool + // The trailing switch changed, to switch_on. + switch_changed: bool + switch_on: bool +} + +// One line rows are a large control tall; a subtitle adds a small line. +Measure :: (props: Props) -> Rectangle { + height := CurrentThemeMetric(.ThemeMetricControlHeightLarge) + if props.subtitle != "" { height += CurrentThemeMetric(.ThemeMetricTextSM) * 1.4 } + return Rectangle.{0.0, 0.0, 0.0, height} +} + +// The width the trailing part needs at the row's right. +TrailingWidth :: (props: Props, height: float32) -> float32 { + if props.trailing == .ListTrailingNone { return 0.0 } + if props.trailing == .ListTrailingChevron { return height * 0.5 } + if props.trailing == .ListTrailingSwitch { return height * 1.25 } + return cast(float32)props.trailing_text.count * CurrentThemeMetric(.ThemeMetricTextSM) * 0.6 + + CurrentThemeMetric(.ThemeMetricSpace4) +} + +Draw :: (session: Session, props: Props) -> ListItemResult { + result := ListItemResult.{switch_on = props.switch_on} + bounds := props.bounds + if bounds.height <= 0.0 { bounds.height = Measure(props).height } + slot := Layout.Stack(session, Layout.Props.{.key = props.key, .bounds = bounds, + .padding_override = true}) + if !slot.opened { return result } + area := slot.bounds + space := CurrentThemeMetric(.ThemeMetricSpace3) + trailing := TrailingWidth(props, area.height) + leading: float32 = 0.0 + if props.leading_name != "" { leading = CurrentThemeMetric(.ThemeMetricControlHeightMedium) + space } + row := Button.Props.{.label = props.title, .detail = props.subtitle, .icon_type = props.leading_icon, + .id = props.id, .class_name = props.class_name, .emphasis = .ButtonEmphasisGhost, + .content_alignment = .ContentAlignmentStart, .selected = props.selected, + .disabled = props.disabled, + .bounds = Rectangle.{area.x + leading, area.y, area.width - leading, area.height}} + result.clicked = Button.Draw(session, row) == 1 + if props.leading_name != "" { + avatar := leading - space + unused Avatar.Draw(session, Avatar.Props.{.name = props.leading_name, .disabled = props.disabled, + .bounds = Rectangle.{area.x + space, area.y + (area.height - avatar) * 0.5, avatar, avatar}}) + } + end := Rectangle.{area.x + area.width - trailing - space, area.y, trailing, area.height} + if props.trailing == .ListTrailingText { + Text.Draw(session, Text.Props.{.bounds = end, .text = props.trailing_text, .align = .TextAlignEnd, + .vertical_align = .TextAlignCenter, .size = cast(s32)CurrentThemeMetric(.ThemeMetricTextSM)}) + } else if props.trailing == .ListTrailingBadge { + badge_height := CurrentThemeMetric(.ThemeMetricTextSM) * 1.6 + unused Badge.Draw(session, Badge.Props.{.text = props.trailing_text, .tone = .BadgeToneAccent, + .bounds = Rectangle.{end.x, end.y + (end.height - badge_height) * 0.5, 0.0, badge_height}}) + } else if props.trailing == .ListTrailingChevron { + Text.Draw(session, Text.Props.{.bounds = end, .text = "›", .align = .TextAlignEnd, + .vertical_align = .TextAlignCenter, .size = cast(s32)CurrentThemeMetric(.ThemeMetricTextLG)}) + } else if props.trailing == .ListTrailingSwitch { + switch := Toggle.Draw(session, Toggle.Props.{.bounds = end, .label = props.title, + .value = props.switch_on, .disabled = props.disabled}) + result.switch_changed = switch.changed + if switch.changed { result.switch_on = switch.value } + } + unused End(session) + return result +} diff --git a/src/ui/skeleton/skeleton.zi b/src/ui/skeleton/skeleton.zi new file mode 100644 index 000000000..0cb2c8490 --- /dev/null +++ b/src/ui/skeleton/skeleton.zi @@ -0,0 +1,65 @@ +// Skeleton placeholders hold the place of content that is still loading: +// a block, a line of text or a circle in a muted tone of the surface. They +// pulse gently with the caller's clock and hold still when the theme asks +// for reduced motion. Unlike Loading's dots, a skeleton keeps the layout's +// shape. Measure gives a line or circle its height from the theme. +Box :: #import "box"; +#import "drawing_props" +#import "geometry" +#import "session" +#import "theme" + +SkeletonShape :: enum { + SkeletonBlock :: 0 + SkeletonLine :: 1 + SkeletonCircle :: 2 +} + +Props :: struct { + key: u64 + // Zero width fills the column; zero height takes the measured height. + bounds: Rectangle + shape: SkeletonShape + // Seconds on any steady clock; the pulse follows it. + now: float64 + id: s32 + class_name: s32 +} + +// A block is as tall as a large control; a line as its text; a circle as +// an avatar. +Measure :: (props: Props) -> Rectangle { + height := CurrentThemeMetric(.ThemeMetricControlHeightLarge) * 2.0 + if props.shape == .SkeletonLine { height = CurrentThemeMetric(.ThemeMetricTextBody) * 1.4 } + if props.shape == .SkeletonCircle { height = CurrentThemeMetric(.ThemeMetricControlHeightLarge) } + width := ifx props.shape == .SkeletonCircle then height else 0.0 + return Rectangle.{0.0, 0.0, width, height} +} + +// How strongly the placeholder tints the surface, in percent: it breathes +// between two levels once a second and a half, or rests at the lower one. +SkeletonLevel :: (now: float64, dark: bool, reduced_motion: bool) -> u32 { + low: u32 = ifx dark then 18 else 12 + if reduced_motion { return low } + turns := now / 1.5 + phase := turns - cast(float64)cast(s64)turns + if phase < 0.0 { phase += 1.0 } + rise := ifx phase < 0.5 then phase * 2.0 else (1.0 - phase) * 2.0 + return low + cast(u32)(rise * 10.0) +} + +Draw :: (session: Session, props: Props) { + size := Measure(props) + bounds := props.bounds + if bounds.width <= 0.0 { bounds.width = size.width } + if bounds.height <= 0.0 { bounds.height = size.height } + theme := CurrentTheme() + palette := CurrentPalette() + level := SkeletonLevel(props.now, theme.dark, theme.motion.reduced_motion) + radius := CurrentThemeMetric(.ThemeMetricRadiusSmall) + if props.shape == .SkeletonBlock { radius = CurrentThemeMetric(.ThemeMetricRadiusPanel) } + if props.shape == .SkeletonCircle { radius = bounds.height * 0.5 } + unused Box.Draw(session, Box.Props.{.key = props.key, .id = props.id, .class_name = props.class_name, + .bounds = bounds, .radius = radius, + .fill = ColorFromPacked(StateLayer(palette.surface, palette.foreground, level))}) +} diff --git a/tests/new_widgets_test.zi b/tests/new_widgets_test.zi new file mode 100644 index 000000000..4b43f3a90 --- /dev/null +++ b/tests/new_widgets_test.zi @@ -0,0 +1,162 @@ +// Chip, ListItem and Skeleton in a real app: shot light and dark (each +// widget's labelled node is in the tree), then driven on a private display: +// a chip toggles, a dismissible chip closes, a row opens and its switch flips. +// Run from the Kryon root: make new-widgets-test (ZIRAN_BIN names ziran). +#import "private_display_linux" +#import "test_driver_client" +#import "std/file" +#import "std/file_tree_linux" +#import "std/environment_linux" +#import "std/process_run_linux" +#import "std/text_tools" +using Vectors :: #import "vec"; + +failures: s32; + +Expect :: (ok: bool, what: string) { + if ok { return } + failures += 1 + print("new widgets: FAIL %\n", what) +} + +Manifest :: #string TOML +[package] +name = "widgets" +entry = "src/app.zi" +module_roots = ["src"] +bridge_modules = ["app"] +links = ["X11"] +tool = "kryon" + +[toolchain] +git = "https://git.waozi.xyz/ziranlang/ziran.git" +ref = "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0" + +[dependencies.kryon] +git = "https://git.waozi.xyz/kryonlabs/kryon.git" +ref = "cdf23986a0434cfb01513ecca4763b0477d01a7e" + +[tool.kryon] +default_profile = "desktop" + +[tool.kryon.profiles.desktop] +backend = "desktop" +TOML; + +App :: #string ZI +Chip :: #import "kryon/chip"; +Layout :: #import "kryon/layout"; +ListItem :: #import "kryon/list_item"; +Skeleton :: #import "kryon/skeleton"; +Text :: #import "kryon/text"; +#import "kryon/geometry" +#import "kryon/session" +#import "kryon/tree" + +design: bool; +dismissed: bool; +opened: bool; +wifi: bool; + +#program_export +Frame :: (session: Session, viewport: Rectangle) -> s32 { + column := Layout.Draw(session, Layout.Props.{.bounds = viewport, .gap = 8, .gap_override = true, + .padding = 16, .padding_override = true}) + if !column.opened { return 0 } + chip := Chip.Draw(session, Chip.Props.{.label = "Design", .selectable = true, .selected = design}) + if chip.changed { design = chip.selected } + if !dismissed { + if Chip.Draw(session, Chip.Props.{.label = "Draft", .dismissible = true}).dismissed { dismissed = true } + } + row := ListItem.Draw(session, ListItem.Props.{.title = "Profile", .subtitle = "Edit your details", + .leading_name = "Ada Lovelace", .trailing = .ListTrailingChevron}) + if row.clicked { opened = true } + wifi_row := ListItem.Draw(session, ListItem.Props.{.title = "Wi-Fi", .trailing = .ListTrailingSwitch, .switch_on = wifi}) + if wifi_row.switch_changed { wifi = wifi_row.switch_on } + unused ListItem.Draw(session, ListItem.Props.{.title = "Inbox", .trailing = .ListTrailingBadge, .trailing_text = "3"}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonLine}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonCircle}) + Skeleton.Draw(session, Skeleton.Props.{.shape = .SkeletonBlock, .now = 0.75}) + state := ifx design then "Design on" else "Design off" + Text.Draw(session, Text.Props.{.bounds = .{height = 24}, .text = state}) + Text.Draw(session, Text.Props.{.bounds = .{height = 24}, .text = ifx dismissed then "Dismissed" else "Kept"}) + Text.Draw(session, Text.Props.{.bounds = .{height = 24}, .text = ifx opened then "Profile opened" else "Closed"}) + Text.Draw(session, Text.Props.{.bounds = .{height = 24}, .text = ifx wifi then "Wi-Fi on" else "Wi-Fi off"}) + unused End(session) + return 0 +} +ZI; + +Run :: (arguments: []string, directory: string, environment: []string, log: string) -> bool { + options: ProcessOptions = .{.arguments = arguments, .directory = directory, + .environment = environment, .output_path = log, .append_output = true, .merge_error = true} + run: ProcessRun + return RunProcess(options, *run) && run.code == 0 +} + +// The shot's tree has a node labelled LABEL. +Listed :: (tree: string, label: string) -> bool { + needle: Vec(u8) + BuilderPrint(*needle, "\"label\":\"%\"", label) + at := TextFind(tree, BuilderFinish(needle)) + return at >= 0 +} + +main :: () -> s32 { + ziran := EnvironmentText("ZIRAN_BIN") + if ziran == "" { ziran = "ziran" } + root := WorkingDirectory() + project := JoinPath(root, "build/test/new-widgets") + log := JoinPath(root, "build/test/new-widgets.log") + overrides: Vec(u8) + BuilderPrint(*overrides, "[overrides]\nkryon = \"%\"\n", root) + if !CreateDirectories(JoinPath(project, "src")) || + !WriteEntireFile(JoinPath(project, "ziran.toml"), Manifest) || + !WriteEntireFile(JoinPath(project, "ziran.local.toml"), BuilderFinish(overrides)) || + !WriteEntireFile(JoinPath(project, "src/app.zi"), App) { + print("new widgets: FAIL cannot write the project\n") + return 1 + } + plain: [0]string + lock: [2]string = .[ziran, "lock"] + Expect(Run(lock[:], project, plain[:], log), "ziran lock; see build/test/new-widgets.log") + themes: [2]string = .["light", "dark"] + for theme: themes { + png: Vec(u8) + BuilderPrint(*png, "%/build/test/new-widgets-%.png", root, theme) + path := BuilderFinish(png) + shot: [12]string = .[ziran, "tool", "kryon", "run", "--png", path, "--width", "390", "--height", "844", "--theme", theme] + Expect(Run(shot[:], project, plain[:], log), "shoot the widgets; see build/test/new-widgets.log") + tree_path := TextReplace(path, ".png", ".json") + tree, read := ReadEntireFile(tree_path) + Expect(read, "the shot writes its tree") + labels: [6]string = .["Design", "Draft", "Remove", "Profile", "Wi-Fi", "Inbox"] + for label: labels { Expect(Listed(tree, label), label) } + } + + driver_env: [1]string = .["KRYON_TEST_DRIVER=1"] + build: [5]string = .[ziran, "tool", "kryon", "build", "desktop"] + Expect(Run(build[:], project, driver_env[:], log), "build with the test driver; see build/test/new-widgets.log") + display := OpenPrivateDisplay(390, 844, "build/test/new-widgets-xvfb.log") + if !display.valid { print("new widgets: FAIL %\n", display.error); return 1 } + defer ClosePrivateDisplay(*display) + driver := DriverOpen(JoinPath(project, "driver")) + program: [1]string = .["./build/widgets-desktop"] + extra: [1]string = .[DriverEnvironment(*driver)] + app := StartOnDisplay(*display, program[:], extra[:], project, "build/test/new-widgets-app.log") + if !app.started { print("new widgets: FAIL the app did not start\n"); return 1 } + defer { unused StopOwnedProcess(*app, 5000) } + DriverWatch(*driver, app.pid, "build/test/new-widgets-app.log") + Expect(DriverWait(*driver, .{label = "Design off"}, 15000), "the app starts") + Expect(DriverPress(*driver, .{label = "Design", role = "button"}), "press the chip") + Expect(DriverWait(*driver, .{label = "Design on"}, 5000), "the chip toggles on") + Expect(DriverPress(*driver, .{label = "Remove", role = "button"}), "press the close control") + Expect(DriverWait(*driver, .{label = "Dismissed"}, 5000), "the chip is dismissed") + Expect(DriverPress(*driver, .{label = "Profile", role = "button"}), "press the row") + Expect(DriverWait(*driver, .{label = "Profile opened"}, 5000), "the row opens") + Expect(DriverPress(*driver, .{label = "Wi-Fi", index = 1}), "press the switch") + Expect(DriverWait(*driver, .{label = "Wi-Fi on"}, 5000), "the switch turns on") + if failures > 0 { return 1 } + print("new widgets: chip, list item and skeleton shot light and dark, and driven\n") + return 0 +} diff --git a/ziran.toml b/ziran.toml index 849e3c71b..11052749e 100644 --- a/ziran.toml +++ b/ziran.toml @@ -1,7 +1,7 @@ [package] name = "kryon" links = ["SDL2", "pangocairo-1.0", "pango-1.0", "cairo", "fontconfig", "freetype", "gio-2.0", "gobject-2.0", "glib-2.0", "X11", "m", "pthread"] -module_roots = ["src/ui", "src/backend", "src/ui/alert", "src/ui/avatar", "src/ui/badge", "src/ui/banner", "src/ui/box", "src/ui/breadcrumb", "src/ui/button", "src/ui/buttongroup", "src/ui/card", "src/ui/carousel", "src/ui/checkbox", "src/ui/checklist", "src/ui/collapsible", "src/ui/colorpicker", "src/ui/commandpalette", "src/ui/confirmbutton", "src/ui/confirmpopover", "src/ui/contextmenu", "src/ui/copybutton", "src/ui/countdown", "src/ui/dpad", "src/ui/dropdown", "src/ui/emptystate", "src/ui/fieldset", "src/ui/flow", "src/ui/gridmulti", "src/ui/guidedtour", "src/ui/holdbutton", "src/ui/hovercard", "src/ui/image", "src/ui/inlineedit", "src/ui/joystick", "src/ui/keycap", "src/ui/layout", "src/ui/link", "src/ui/listbox", "src/ui/loading", "src/ui/loadingscreen", "src/ui/menu", "src/ui/modal", "src/ui/moneyfield", "src/ui/navigation", "src/ui/onetimecode", "src/ui/overlay", "src/ui/page", "src/ui/panedview", "src/ui/press", "src/ui/progress", "src/ui/progressring", "src/ui/radio", "src/ui/reorder", "src/ui/scroll", "src/ui/scrubfield", "src/ui/segmented", "src/ui/selectable", "src/ui/separator", "src/ui/shape", "src/ui/sheet", "src/ui/shortcutrecorder", "src/ui/slider", "src/ui/spinbox", "src/ui/svgicon", "src/ui/swiperow", "src/ui/tabs", "src/ui/taginput", "src/ui/terminallist", "src/ui/text", "src/ui/textarea", "src/ui/markdown", "src/ui/textfield", "src/ui/tilemap", "src/ui/timeline", "src/ui/timepicker", "src/ui/titlebar", "src/ui/window_chrome", "src/ui/workspace_pager", "src/ui/toast", "src/ui/toggle", "src/ui/toolbar", "src/ui/tooltip", "tools"] +module_roots = ["src/ui", "src/backend", "src/ui/alert", "src/ui/avatar", "src/ui/badge", "src/ui/banner", "src/ui/box", "src/ui/breadcrumb", "src/ui/button", "src/ui/buttongroup", "src/ui/card", "src/ui/carousel", "src/ui/checkbox", "src/ui/checklist", "src/ui/chip", "src/ui/collapsible", "src/ui/colorpicker", "src/ui/commandpalette", "src/ui/confirmbutton", "src/ui/confirmpopover", "src/ui/contextmenu", "src/ui/copybutton", "src/ui/countdown", "src/ui/dpad", "src/ui/dropdown", "src/ui/emptystate", "src/ui/fieldset", "src/ui/flow", "src/ui/gridmulti", "src/ui/guidedtour", "src/ui/holdbutton", "src/ui/hovercard", "src/ui/image", "src/ui/inlineedit", "src/ui/joystick", "src/ui/keycap", "src/ui/layout", "src/ui/link", "src/ui/listitem", "src/ui/listbox", "src/ui/loading", "src/ui/loadingscreen", "src/ui/menu", "src/ui/modal", "src/ui/moneyfield", "src/ui/navigation", "src/ui/onetimecode", "src/ui/overlay", "src/ui/page", "src/ui/panedview", "src/ui/press", "src/ui/progress", "src/ui/progressring", "src/ui/radio", "src/ui/reorder", "src/ui/scroll", "src/ui/scrubfield", "src/ui/segmented", "src/ui/selectable", "src/ui/separator", "src/ui/shape", "src/ui/sheet", "src/ui/shortcutrecorder", "src/ui/skeleton", "src/ui/slider", "src/ui/spinbox", "src/ui/svgicon", "src/ui/swiperow", "src/ui/tabs", "src/ui/taginput", "src/ui/terminallist", "src/ui/text", "src/ui/textarea", "src/ui/markdown", "src/ui/textfield", "src/ui/tilemap", "src/ui/timeline", "src/ui/timepicker", "src/ui/titlebar", "src/ui/window_chrome", "src/ui/workspace_pager", "src/ui/toast", "src/ui/toggle", "src/ui/toolbar", "src/ui/tooltip", "tools"] [toolchain] git = "https://git.waozi.xyz/ziranlang/ziran.git" @@ -21,6 +21,9 @@ window_icon = "src/backend/window_icon.zi" alert = "src/ui/alert/alert.zi" avatar = "src/ui/avatar/avatar.zi" badge = "src/ui/badge/badge.zi" +chip = "src/ui/chip/chip.zi" +list_item = "src/ui/listitem/listitem.zi" +skeleton = "src/ui/skeleton/skeleton.zi" banner = "src/ui/banner/banner.zi" button_group = "src/ui/buttongroup/buttongroup.zi" copy_button = "src/ui/copybutton/copybutton.zi"