Files
kryon/tests/cursor_model_test.zi
T
waozi 8f2291577c Show an application pointing cursor only over clickable targets
CursorSetPointing sets the cursor shown where a click does something:
clickable widgets and painted targets an app hit-tests itself and marks
with CursorHover while it builds the frame. The mark commits with the
tree, so retained or cancelled frames keep it and the cursor changes only
when the hovered target does. PressProps.cursor lets a press the app
refines with its own hit test name the arrow off its painted shape, on
desktop, Canvas and DOM, without changing presses, touch or keys.
2026-11-03 22:11:47 -03:00

150 lines
8.9 KiB
Ziran

#import "session"
#import "cursor"
#import "tree"
#import "tree_input"
#import "geometry"
#import "std/byte_text_linux"
using CursorShape;
// Requests persist until the next deliberate change, sessions inherit the
// application cursor, and only text, crosshair and resize shapes show through.
#program_export
Answer :: () -> s32 {
a := SessionOpen()
b := SessionOpen()
if CursorSet(Session.{}, .{}) || CursorSet(Session.{}, .{.hidden = true}) { return 1 }
bytes: [16]u8 = .[97, 115, 115, 101, 116, 115, 47, 104, 97, 110, 100, 46, 112, 110, 103, 0]
{
// No frame is needed; the path is copied.
borrowed := CursorProps.{.asset_path = TextFromBytes(bytes[:15]), .width = 32, .height = 48, .hotspot_x = 3, .hotspot_y = 4}
if !CursorSet(a, borrowed) || CursorRequested(b) { return 2 }
}
props := CursorProps.{.asset_path = "assets/hand.png", .width = 32, .height = 48, .hotspot_x = 3, .hotspot_y = 4}
bytes[7] = cast(u8)120
if CursorGet(a).asset_path != "assets/hand.png" { return 3 }
// Invalid requests are refused and keep the last valid one.
props.hotspot_x = 32
if CursorSet(a, props) || CursorGet(a).hotspot_x != 3 { return 4 }
props.hotspot_x = -1
if CursorSet(a, props) { return 5 }
props.hotspot_x = 0; props.width = 129
if CursorSet(a, props) { return 6 }
props.width = 0
if CursorSet(a, props) { return 7 }
props.width = 32; props.hidden = true
if CursorSet(a, props) || CursorGet(a).hidden || CursorGet(a).width != 32 { return 8 }
// Frames, cancelled frames and rejected frames never change a request.
TreeStart(a, cast(u64)1, .{0.0, 0.0, 100.0, 100.0})
TreeCancel(a)
if !CursorRequested(a) || CursorGet(a).asset_path != "assets/hand.png" { return 9 }
TreeStart(a, cast(u64)1, .{0.0, 0.0, 100.0, 100.0})
unused TreeSubmit(a, cast(u64)2, 77, 1, .{0.0, 0.0, 1.0, 1.0})
if TreeFinish(a) || !CursorRequested(a) { return 10 }
TreeStart(a, cast(u64)1, .{0.0, 0.0, 100.0, 100.0})
if !TreeFinish(a) || !CursorRequested(a) { return 11 }
// Repeating the request changes nothing; the empty request withdraws it.
before := CursorGet(a)
if !CursorSet(a, before) || !CursorPropsSame(CursorGet(a), before) { return 12 }
if !CursorSet(a, .{}) || CursorRequested(a) || CursorGet(a).asset_path != "" { return 13 }
// Without an application cursor every session shows its widget shape.
if CursorResolve(a, cast(s32)CursorClickable).shape != cast(s32)CursorClickable ||
CursorResolve(a, cast(s32)CursorClickable).asset_path != "" || CursorResolve(a, 99).shape != 0 { return 14 }
app := CursorProps.{.asset_path = "assets/app.png", .width = 32, .height = 32, .hotspot_x = 7, .hotspot_y = 1}
if !CursorSetDefault(app) || !CursorPropsSame(CursorGetDefault(), app) { return 15 }
// Default, arrow, clickable and disabled; then text, crosshair and resizes.
pointers: [4]s32 = .[0, 1, 4, 10]
for shape: pointers {
if !CursorPropsSame(CursorResolve(a, shape), app) || !CursorPropsSame(CursorResolve(b, shape), app) { return 16 }
}
functional: [7]s32 = .[2, 3, 5, 6, 7, 8, 9]
for shape: functional {
shown := CursorResolve(a, shape)
if shown.asset_path != "" || shown.shape != shape { return 17 }
}
// A session's own request replaces the application cursor everywhere.
tool := CursorProps.{.asset_path = "assets/tool.png", .width = 32, .height = 32, .hotspot_x = 8, .hotspot_y = 28}
if !CursorSet(a, tool) || !CursorPropsSame(CursorResolve(a, cast(s32)CursorText), tool) { return 18 }
if !CursorPropsSame(CursorResolve(b, cast(s32)CursorDefault), app) { return 19 }
if !CursorSet(a, .{.hidden = true}) || !CursorResolve(a, 0).hidden || CursorResolve(a, 0).asset_path != "" { return 20 }
if !CursorSet(a, .{.shape = cast(s32)CursorCrosshair}) || CursorResolve(a, 0).shape != cast(s32)CursorCrosshair { return 21 }
// Disposed and closed sessions keep no request; a reused slot inherits.
TreeStart(a, cast(u64)1, .{0.0, 0.0, 100.0, 100.0})
TreeDispose(a)
if CursorRequested(a) { return 22 }
if !CursorSet(a, tool) || !SessionClose(a) || CursorRequested(a) { return 23 }
fresh := SessionOpen()
if fresh.slot != a.slot || CursorRequested(fresh) || !CursorPropsSame(CursorResolve(fresh, 0), app) { return 24 }
if CursorSet(a, tool) || CursorRequested(fresh) { return 25 }
// The application cursor validates like requests and can be withdrawn.
if CursorSetDefault(.{.asset_path = "assets/a", .width = 129, .height = 1}) || !CursorPropsSame(CursorGetDefault(), app) { return 26 }
if !CursorSetDefault(.{}) || CursorResolve(fresh, cast(s32)CursorClickable).shape != cast(s32)CursorClickable { return 27 }
if !CursorPackedPath("assets/a.png") || CursorPackedPath("assets//a") ||
CursorPackedPath("assets/../a") || CursorPackedPath("/assets/a") ||
CursorPackedPath("assets/a/") || CursorPackedPath("assets/a\\b") ||
CursorPackedPath("assets/a:b") || CursorPackedPath("assets/./a") { return 28 }
if !CursorPropsValid(.{.asset_path = "assets/a", .width = 128, .height = 1, .hotspot_x = 127}) ||
CursorPropsValid(.{.asset_path = "assets/a", .width = 1, .height = 1, .hotspot_y = 1}) ||
CursorPropsValid(.{.shape = 11}) || CursorPropsValid(.{.shape = -1}) ||
CursorPropsValid(.{.hidden = true, .width = 1}) { return 29 }
if CursorPropsSame(app, tool) || !CursorPropsEmpty(.{}) || CursorPropsEmpty(.{.shape = 1}) { return 30 }
// The pointing cursor shows only over clickable shapes; the calm default
// shows over everything else a pointer shape covers.
calm := CursorProps.{.asset_path = "assets/calm.png", .width = 32, .height = 32, .hotspot_x = 6, .hotspot_y = 2}
point := CursorProps.{.asset_path = "assets/point.png", .width = 32, .height = 32, .hotspot_x = 8, .hotspot_y = 1}
if !CursorSetDefault(calm) || !CursorSetPointing(point) || !CursorPropsSame(CursorGetPointing(), point) { return 31 }
if !CursorPropsSame(CursorResolve(fresh, cast(s32)CursorClickable), point) { return 32 }
calm_shapes: [3]s32 = .[0, 1, 10]
for shape: calm_shapes { if !CursorPropsSame(CursorResolve(fresh, shape), calm) { return 33 } }
if CursorResolve(fresh, cast(s32)CursorText).shape != cast(s32)CursorText { return 34 }
// A session's own tool request still wins over both.
if !CursorSet(fresh, tool) || !CursorPropsSame(CursorResolve(fresh, cast(s32)CursorClickable), tool) ||
!CursorSet(fresh, .{}) { return 35 }
if CursorSetPointing(.{.asset_path = "assets/a", .width = 0, .height = 1}) ||
!CursorPropsSame(CursorGetPointing(), point) { return 36 }
// An app-drawn target's mark shows once its frame commits, survives a
// cancelled (retained) or failed frame, and an unmarked frame clears it.
view := Rectangle.{0.0, 0.0, 100.0, 100.0}
if CursorHover(Session.{}, cast(s32)CursorClickable) || CursorHover(fresh, 11) || CursorHover(fresh, -1) { return 37 }
if CursorHovered(fresh) != cast(s32)CursorDefault { return 38 }
TreeStart(fresh, cast(u64)1, view)
if !CursorHover(fresh, cast(s32)CursorClickable) || CursorHovered(fresh) != cast(s32)CursorDefault { return 39 }
if !TreeFinish(fresh) || CursorHovered(fresh) != cast(s32)CursorClickable { return 40 }
TreeStart(fresh, cast(u64)1, view)
TreeCancel(fresh)
if CursorHovered(fresh) != cast(s32)CursorClickable { return 41 }
TreeStart(fresh, cast(u64)1, view)
unused TreeSubmit(fresh, cast(u64)2, 77, 1, .{0.0, 0.0, 1.0, 1.0})
if TreeFinish(fresh) || CursorHovered(fresh) != cast(s32)CursorClickable { return 43 }
// Repeating the same mark every frame keeps the same shape.
for frame: 0..3 {
TreeStart(fresh, cast(u64)1, view)
unused CursorHover(fresh, cast(s32)CursorClickable)
if !TreeFinish(fresh) || CursorHovered(fresh) != cast(s32)CursorClickable { return 44 }
}
// The highest-priority mark of a frame wins.
TreeStart(fresh, cast(u64)1, view)
unused CursorHover(fresh, cast(s32)CursorDisabled)
unused CursorHover(fresh, cast(s32)CursorClickable)
unused CursorHover(fresh, cast(s32)CursorText)
if !TreeFinish(fresh) || CursorHovered(fresh) != cast(s32)CursorClickable { return 45 }
TreeStart(fresh, cast(u64)1, view)
if !TreeFinish(fresh) || CursorHovered(fresh) != cast(s32)CursorDefault { return 46 }
// Marks belong to their session, and disposal drops them.
other := SessionOpen()
TreeStart(other, cast(u64)1, view)
unused CursorHover(other, cast(s32)CursorClickable)
if !TreeFinish(other) || CursorHovered(other) != cast(s32)CursorClickable ||
CursorHovered(fresh) != cast(s32)CursorDefault { return 47 }
TreeDispose(other)
if CursorHovered(other) != cast(s32)CursorDefault { return 48 }
// Withdrawn, clickable shows the calm default again.
if !CursorSetPointing(.{}) || !CursorPropsSame(CursorResolve(fresh, cast(s32)CursorClickable), calm) { return 49 }
return 42
}
#program_export
main :: () -> s32 {
answer := Answer()
return ifx answer == 42 then 0 else answer
}