@native-sdk/cli 0.8.1 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/build.zig +52 -23
- package/package.json +10 -10
- package/packages/core/compile-surface/core.ts +16 -0
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/sdk/core.d.ts +11 -0
- package/packages/core/sdk/core.ts +29 -2
- package/packages/core/sdk/events.d.ts +10 -0
- package/packages/core/sdk/events.ts +24 -4
- package/packages/core/src/checker.ts +1 -1
- package/packages/core/src/contract.ts +4 -1
- package/packages/core/src/devhost.ts +10 -1
- package/packages/core/src/diagnostics.ts +1 -1
- package/skill-data/core/SKILL.md +48 -20
- package/skill-data/core/references/app-model-runtime.md +2 -2
- package/skill-data/core/references/project-anatomy.md +22 -6
- package/skill-data/native-ui/SKILL.md +68 -27
- package/skill-data/ts-core/SKILL.md +13 -8
- package/skill-data/zig/SKILL.md +1 -1
- package/skills/native-sdk/SKILL.md +6 -5
- package/src/app_runner/app_assets.zig +138 -0
- package/src/app_runner/root.zig +2 -0
- package/src/app_runner/ts_core_main.zig +4 -2
- package/src/embed/tests.zig +3 -1
- package/src/primitives/canvas/events.zig +8 -0
- package/src/primitives/canvas/root.zig +2 -0
- package/src/primitives/canvas/test_support.zig +1 -0
- package/src/primitives/canvas/ui.zig +53 -8
- package/src/primitives/canvas/ui_markup.zig +1 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +32 -3
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +27 -0
- package/src/primitives/canvas/ui_markup_contract.zig +20 -3
- package/src/primitives/canvas/ui_markup_reflect.zig +41 -0
- package/src/primitives/canvas/ui_markup_view.zig +29 -3
- package/src/primitives/canvas/ui_markup_view_tests.zig +48 -1
- package/src/primitives/canvas/ui_schema.zig +5 -1
- package/src/primitives/canvas/ui_schema_tests.zig +3 -3
- package/src/primitives/canvas/ui_tests.zig +14 -0
- package/src/primitives/canvas/widget_access.zig +4 -3
- package/src/primitives/canvas/widget_invalidation.zig +11 -0
- package/src/primitives/canvas/widget_render.zig +106 -1
- package/src/primitives/canvas/widget_routing.zig +12 -7
- package/src/primitives/canvas/widget_runtime_tests.zig +172 -0
- package/src/primitives/canvas/widgets.zig +52 -0
- package/src/root.zig +1 -0
- package/src/runtime/automation_widget_dispatch.zig +19 -0
- package/src/runtime/canvas_limits.zig +5 -0
- package/src/runtime/canvas_widget_event_tests.zig +425 -8
- package/src/runtime/canvas_widget_events.zig +254 -7
- package/src/runtime/canvas_widget_state.zig +127 -1
- package/src/runtime/core.zig +6 -0
- package/src/runtime/effects.zig +27 -1
- package/src/runtime/effects_notification_tests.zig +65 -0
- package/src/runtime/gpu_surface_events.zig +60 -14
- package/src/runtime/tests.zig +1 -0
- package/src/runtime/ts_core_host.zig +16 -0
- package/src/runtime/ts_ui_app.zig +55 -2
- package/src/runtime/ui_app.zig +112 -0
- package/src/runtime/ui_app_tests.zig +356 -0
- package/src/runtime/view.zig +35 -0
- package/src/runtime/view_widget_tree.zig +12 -0
- package/src/tooling/package.zig +4 -4
- package/tools/corewire/core_abi.zig +1 -0
- package/tools/corewire/emit.zig +55 -8
- package/tools/corewire/emit_facade.zig +63 -2
- package/tools/corewire/emit_profile.zig +4 -1
- package/tools/corewire/sidecar.zig +36 -6
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: native-sdk
|
|
3
|
-
description: Discovery skill for the Native SDK, the complete toolkit for building native desktop applications
|
|
3
|
+
description: Discovery skill for the Native SDK, the complete toolkit for building native desktop applications. Apps are authored in TypeScript + declarative Native markup (.native) by default and compiled to native code with no JS runtime in the binary; Zig cores are an explicit alternative, and WebViews are the optional web-content path. Use when the user asks what the Native SDK is, how to build a Native SDK app, author native UI, scaffold an app, configure app.zon, add bridge commands, embed web content, package an app, test a running app, or automate a Native SDK app.
|
|
4
4
|
allowed-tools: Bash(native:*), Bash(npx @native-sdk/cli:*)
|
|
5
5
|
hidden: true
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Native SDK
|
|
9
9
|
|
|
10
|
-
The Native SDK is the complete toolkit for building native desktop applications.
|
|
10
|
+
The Native SDK is the complete toolkit for building native desktop applications. **The primary authoring path is TypeScript app logic in `src/core.ts` plus declarative Native markup in `.native` files.** The TypeScript core is checked and compiled ahead of time to native code, so the shipped binary contains no browser, WebView, JS runtime, or interpreter. Zig is how the toolkit itself works and is a first-class, explicitly chosen app-core alternative (`--template zig-core`); it is not the default inferred from the SDK's implementation. Every app embeds a deterministic automation server, so agents can snapshot, drive, and screenshot the running window. Desktop is the mature surface (macOS deepest, Linux and Windows exercised in CI); mobile embedding is experimental. WebView surfaces coexist as the optional path for embedding web content or hosting an existing web frontend.
|
|
11
11
|
|
|
12
12
|
## Start here
|
|
13
13
|
|
|
@@ -16,10 +16,11 @@ This file is a discovery stub for agents that installed the Native SDK once with
|
|
|
16
16
|
```bash
|
|
17
17
|
native skills list
|
|
18
18
|
native skills get core
|
|
19
|
-
native skills get
|
|
19
|
+
native skills get native-ui
|
|
20
|
+
native skills get ts-core
|
|
20
21
|
```
|
|
21
22
|
|
|
22
|
-
Use `native skills get core` for initial orientation.
|
|
23
|
+
Use `native skills get core` for initial orientation. For the default app-authoring path, load **both** `native-ui` (views, bindings, the app loop) and `ts-core` (the TypeScript subset, effects, subscriptions, and modules) before implementing. Use `native skills get core --full` when work reaches lower-level runtime wiring, WebViews, bridge/security, native capabilities, packaging, or debugging. Use `native skills get automation` when testing a running app, taking snapshots, requesting reloads, or using the built-in automation server. Use `native skills get zig` only for an existing Zig core, a toolkit extension, SDK implementation work, or a Zig 0.16 compile error.
|
|
23
24
|
|
|
24
25
|
## Quick orientation
|
|
25
26
|
|
|
@@ -30,4 +31,4 @@ cd my_app
|
|
|
30
31
|
native dev
|
|
31
32
|
```
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
`native init my_app` generates the primary three-file app: `app.zon`, `src/app.native` (the markup view), and `src/core.ts` (`Model`, `Msg`, `update`). Inspect the tree before editing an existing app and preserve the core language it already uses. `src/main.zig` means the app explicitly uses the Zig-core template; web-frontend shells additionally carry `frontend/` and usually owned build/runtime wiring.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
//! Startup asset reads shared by generated app runners.
|
|
2
|
+
//!
|
|
3
|
+
//! Relative paths keep their app-directory meaning during development. In a
|
|
4
|
+
//! packaged macOS app, the same tree lives below `Contents/Resources`; Finder
|
|
5
|
+
//! and `open` do not preserve a useful working directory, so resolve that tree
|
|
6
|
+
//! from the executable's bundle layout before reading it.
|
|
7
|
+
|
|
8
|
+
const std = @import("std");
|
|
9
|
+
const builtin = @import("builtin");
|
|
10
|
+
|
|
11
|
+
pub fn readFileAlloc(
|
|
12
|
+
io: std.Io,
|
|
13
|
+
path: []const u8,
|
|
14
|
+
allocator: std.mem.Allocator,
|
|
15
|
+
limit: std.Io.Limit,
|
|
16
|
+
) std.Io.Dir.ReadFileAllocError![]u8 {
|
|
17
|
+
if (comptime builtin.os.tag == .macos) {
|
|
18
|
+
var executable_buffer: [std.Io.Dir.max_path_bytes]u8 = undefined;
|
|
19
|
+
const executable_len = std.process.executablePath(io, &executable_buffer) catch
|
|
20
|
+
return std.Io.Dir.cwd().readFileAlloc(io, path, allocator, limit);
|
|
21
|
+
return readFileAllocFromMacosExecutable(
|
|
22
|
+
io,
|
|
23
|
+
executable_buffer[0..executable_len],
|
|
24
|
+
path,
|
|
25
|
+
allocator,
|
|
26
|
+
limit,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return std.Io.Dir.cwd().readFileAlloc(io, path, allocator, limit);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pub fn readFileAllocFromMacosExecutable(
|
|
34
|
+
io: std.Io,
|
|
35
|
+
executable_path: []const u8,
|
|
36
|
+
path: []const u8,
|
|
37
|
+
allocator: std.mem.Allocator,
|
|
38
|
+
limit: std.Io.Limit,
|
|
39
|
+
) std.Io.Dir.ReadFileAllocError![]u8 {
|
|
40
|
+
var bundled_path_buffer: [std.Io.Dir.max_path_bytes]u8 = undefined;
|
|
41
|
+
if (macosBundleResourcePath(executable_path, path, &bundled_path_buffer) catch null) |bundled_path| {
|
|
42
|
+
return std.Io.Dir.cwd().readFileAlloc(io, bundled_path, allocator, limit) catch |err| switch (err) {
|
|
43
|
+
// Match the platform host's asset resolver: a package miss keeps
|
|
44
|
+
// the old cwd-relative behavior, while a real read failure is not
|
|
45
|
+
// hidden by a second lookup.
|
|
46
|
+
error.FileNotFound => std.Io.Dir.cwd().readFileAlloc(io, path, allocator, limit),
|
|
47
|
+
else => |read_err| read_err,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return std.Io.Dir.cwd().readFileAlloc(io, path, allocator, limit);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
pub fn macosBundleResourcePath(
|
|
54
|
+
executable_path: []const u8,
|
|
55
|
+
asset_path: []const u8,
|
|
56
|
+
output: []u8,
|
|
57
|
+
) error{NoSpaceLeft}!?[]const u8 {
|
|
58
|
+
if (asset_path.len == 0 or std.fs.path.isAbsolutePosix(asset_path)) return null;
|
|
59
|
+
|
|
60
|
+
const executable_dir = std.fs.path.dirnamePosix(executable_path) orelse return null;
|
|
61
|
+
if (!std.mem.eql(u8, std.fs.path.basenamePosix(executable_dir), "MacOS")) return null;
|
|
62
|
+
|
|
63
|
+
const contents_dir = std.fs.path.dirnamePosix(executable_dir) orelse return null;
|
|
64
|
+
if (!std.mem.eql(u8, std.fs.path.basenamePosix(contents_dir), "Contents")) return null;
|
|
65
|
+
|
|
66
|
+
const bundle_dir = std.fs.path.dirnamePosix(contents_dir) orelse return null;
|
|
67
|
+
if (!std.ascii.eqlIgnoreCase(std.fs.path.extension(bundle_dir), ".app")) return null;
|
|
68
|
+
|
|
69
|
+
return std.fmt.bufPrint(output, "{s}/Resources/{s}", .{ contents_dir, asset_path }) catch
|
|
70
|
+
error.NoSpaceLeft;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
test "macOS bundle resource path mirrors the app-relative asset tree" {
|
|
74
|
+
var buffer: [256]u8 = undefined;
|
|
75
|
+
const resolved = (try macosBundleResourcePath(
|
|
76
|
+
"/Applications/Kanban.app/Contents/MacOS/kanban",
|
|
77
|
+
"assets/logos/openai-svgl.png",
|
|
78
|
+
&buffer,
|
|
79
|
+
)).?;
|
|
80
|
+
try std.testing.expectEqualStrings(
|
|
81
|
+
"/Applications/Kanban.app/Contents/Resources/assets/logos/openai-svgl.png",
|
|
82
|
+
resolved,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const uppercase = (try macosBundleResourcePath(
|
|
86
|
+
"/Applications/Kanban.APP/Contents/MacOS/kanban",
|
|
87
|
+
"assets/logo.png",
|
|
88
|
+
&buffer,
|
|
89
|
+
)).?;
|
|
90
|
+
try std.testing.expectEqualStrings(
|
|
91
|
+
"/Applications/Kanban.APP/Contents/Resources/assets/logo.png",
|
|
92
|
+
uppercase,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
try std.testing.expectEqual(null, try macosBundleResourcePath(
|
|
96
|
+
"/tmp/kanban",
|
|
97
|
+
"assets/logo.png",
|
|
98
|
+
&buffer,
|
|
99
|
+
));
|
|
100
|
+
try std.testing.expectEqual(null, try macosBundleResourcePath(
|
|
101
|
+
"/Applications/Kanban.app/Contents/Helpers/kanban",
|
|
102
|
+
"assets/logo.png",
|
|
103
|
+
&buffer,
|
|
104
|
+
));
|
|
105
|
+
try std.testing.expectEqual(null, try macosBundleResourcePath(
|
|
106
|
+
"/Applications/Kanban.app/Contents/MacOS/kanban",
|
|
107
|
+
"/tmp/logo.png",
|
|
108
|
+
&buffer,
|
|
109
|
+
));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
test "packaged macOS startup assets read from Contents Resources" {
|
|
113
|
+
var tmp = std.testing.tmpDir(.{});
|
|
114
|
+
defer tmp.cleanup();
|
|
115
|
+
|
|
116
|
+
const resource_dir = "Kanban.app/Contents/Resources/assets/logos";
|
|
117
|
+
try tmp.dir.createDirPath(std.testing.io, resource_dir);
|
|
118
|
+
try tmp.dir.writeFile(std.testing.io, .{
|
|
119
|
+
.sub_path = resource_dir ++ "/openai.png",
|
|
120
|
+
.data = "packaged-avatar",
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
var executable_buffer: [std.Io.Dir.max_path_bytes]u8 = undefined;
|
|
124
|
+
const executable_path = try std.fmt.bufPrint(
|
|
125
|
+
&executable_buffer,
|
|
126
|
+
".zig-cache/tmp/{s}/Kanban.app/Contents/MacOS/kanban",
|
|
127
|
+
.{tmp.sub_path[0..]},
|
|
128
|
+
);
|
|
129
|
+
const bytes = try readFileAllocFromMacosExecutable(
|
|
130
|
+
std.testing.io,
|
|
131
|
+
executable_path,
|
|
132
|
+
"assets/logos/openai.png",
|
|
133
|
+
std.testing.allocator,
|
|
134
|
+
.limited(1024),
|
|
135
|
+
);
|
|
136
|
+
defer std.testing.allocator.free(bytes);
|
|
137
|
+
try std.testing.expectEqualStrings("packaged-avatar", bytes);
|
|
138
|
+
}
|
package/src/app_runner/root.zig
CHANGED
|
@@ -5,6 +5,8 @@ const app_manifest = @import("app_manifest_zon");
|
|
|
5
5
|
const manifest_shortcuts = if (@hasField(@TypeOf(app_manifest), "shortcuts")) app_manifest.shortcuts else .{};
|
|
6
6
|
const manifest_windows = if (@hasField(@TypeOf(app_manifest), "windows")) app_manifest.windows else .{};
|
|
7
7
|
|
|
8
|
+
pub const app_assets = @import("app_assets.zig");
|
|
9
|
+
|
|
8
10
|
pub const StdoutTraceSink = struct {
|
|
9
11
|
pub fn sink(self: *StdoutTraceSink) native_sdk.trace.Sink {
|
|
10
12
|
return .{ .context = self, .write_fn = write };
|
|
@@ -31,9 +31,11 @@
|
|
|
31
31
|
//! its exports (export exists -> wired): `frameMsg`
|
|
32
32
|
//! (presented frames), `keyMsg` (the app-level key
|
|
33
33
|
//! fallback), `appearanceMsg`/`chromeMsg` (Msg arms
|
|
34
|
-
//! the host fills structurally)
|
|
34
|
+
//! the host fills structurally), and `dropMsg`
|
|
35
|
+
//! (native file drops with source, point, and paths).
|
|
35
36
|
//! images app.zon's `.assets.images` entries (`.{ .id = 1,
|
|
36
37
|
//! .path = "assets/..." }`) are read once at launch
|
|
38
|
+
//! (from `Contents/Resources` in a packaged macOS app)
|
|
37
39
|
//! and registered on the installing frame — the ids
|
|
38
40
|
//! are the `ImageId`s markup avatar bindings use. A
|
|
39
41
|
//! missing file or failed decode skips the entry
|
|
@@ -111,7 +113,7 @@ pub fn main(init: std.process.Init) !void {
|
|
|
111
113
|
var boot_images_buffer: [manifest_images.len]Adapter.BootImage = undefined;
|
|
112
114
|
var boot_image_count: usize = 0;
|
|
113
115
|
inline for (manifest_images) |asset| {
|
|
114
|
-
if (
|
|
116
|
+
if (runner.app_assets.readFileAlloc(init.io, asset.path, std.heap.page_allocator, .limited(max_boot_image_bytes))) |bytes| {
|
|
115
117
|
boot_images_buffer[boot_image_count] = .{ .id = asset.id, .bytes = bytes };
|
|
116
118
|
boot_image_count += 1;
|
|
117
119
|
} else |_| {}
|
package/src/embed/tests.zig
CHANGED
|
@@ -933,7 +933,9 @@ test "mobile C ABI dispatches GPU widget accessibility actions" {
|
|
|
933
933
|
.text_len = drag_delta.len,
|
|
934
934
|
};
|
|
935
935
|
try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
|
|
936
|
-
|
|
936
|
+
// Drag automation is a complete gesture: the movement is followed by
|
|
937
|
+
// the release that lets markup `on-drag` handlers commit the drop.
|
|
938
|
+
try std.testing.expectEqual(platform.GpuSurfaceInputKind.pointer_up, self.last_input_kind);
|
|
937
939
|
try std.testing.expectApproxEqAbs(@as(f32, 276), self.last_touch_x, 0.001);
|
|
938
940
|
try std.testing.expectApproxEqAbs(@as(f32, 74), self.last_touch_y, 0.001);
|
|
939
941
|
|
|
@@ -388,9 +388,17 @@ pub const WidgetFileDropEvent = struct {
|
|
|
388
388
|
paths: []const []const u8 = &.{},
|
|
389
389
|
};
|
|
390
390
|
|
|
391
|
+
pub const WidgetDragPhase = enum {
|
|
392
|
+
change,
|
|
393
|
+
end,
|
|
394
|
+
cancel,
|
|
395
|
+
};
|
|
396
|
+
|
|
391
397
|
pub const WidgetDragEvent = struct {
|
|
392
398
|
source_id: ObjectId = 0,
|
|
399
|
+
phase: WidgetDragPhase = .change,
|
|
393
400
|
point: geometry.PointF,
|
|
401
|
+
/// Total displacement from the pointer-down that began the gesture.
|
|
394
402
|
delta: geometry.OffsetF = .{},
|
|
395
403
|
};
|
|
396
404
|
|
|
@@ -425,6 +425,7 @@ pub const DesignTokens = token_model.DesignTokens;
|
|
|
425
425
|
pub const WidgetKind = widget_model.WidgetKind;
|
|
426
426
|
pub const WidgetCursor = widget_model.WidgetCursor;
|
|
427
427
|
pub const WidgetState = widget_model.WidgetState;
|
|
428
|
+
pub const WidgetLayoutMotion = widget_model.WidgetLayoutMotion;
|
|
428
429
|
pub const WidgetRenderState = widget_model.WidgetRenderState;
|
|
429
430
|
pub const WidgetMainAlignment = widget_model.WidgetMainAlignment;
|
|
430
431
|
pub const WidgetCrossAlignment = widget_model.WidgetCrossAlignment;
|
|
@@ -625,6 +626,7 @@ pub const WidgetControlIntentKind = event_model.WidgetControlIntentKind;
|
|
|
625
626
|
pub const WidgetControlIntent = event_model.WidgetControlIntent;
|
|
626
627
|
pub const WidgetSemanticAction = event_model.WidgetSemanticAction;
|
|
627
628
|
pub const WidgetFileDropEvent = event_model.WidgetFileDropEvent;
|
|
629
|
+
pub const WidgetDragPhase = event_model.WidgetDragPhase;
|
|
628
630
|
pub const WidgetDragEvent = event_model.WidgetDragEvent;
|
|
629
631
|
pub const WidgetEventPhase = event_model.WidgetEventPhase;
|
|
630
632
|
pub const WidgetEventRouteEntry = event_model.WidgetEventRouteEntry;
|
|
@@ -227,6 +227,7 @@ pub const DesignTokens = canvas.DesignTokens;
|
|
|
227
227
|
pub const WidgetKind = canvas.WidgetKind;
|
|
228
228
|
pub const WidgetCursor = canvas.WidgetCursor;
|
|
229
229
|
pub const WidgetState = canvas.WidgetState;
|
|
230
|
+
pub const WidgetLayoutMotion = canvas.WidgetLayoutMotion;
|
|
230
231
|
pub const WidgetRenderState = canvas.WidgetRenderState;
|
|
231
232
|
pub const WidgetMainAlignment = canvas.WidgetMainAlignment;
|
|
232
233
|
pub const WidgetCrossAlignment = canvas.WidgetCrossAlignment;
|
|
@@ -312,6 +312,10 @@ pub const UiHandlerEvent = enum {
|
|
|
312
312
|
/// (select, then open/play), the way list selection conventions
|
|
313
313
|
/// expect.
|
|
314
314
|
double_press,
|
|
315
|
+
/// A draggable element moved, ended, or was cancelled. The handler's
|
|
316
|
+
/// sourceId comes from markup; live phase, coordinates, and view size
|
|
317
|
+
/// are injected by `Tree.msgForDrag`.
|
|
318
|
+
drag,
|
|
315
319
|
toggle,
|
|
316
320
|
change,
|
|
317
321
|
submit,
|
|
@@ -618,7 +622,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
618
622
|
min_width: f32 = 0,
|
|
619
623
|
grow: f32 = 0,
|
|
620
624
|
gap: f32 = 0,
|
|
621
|
-
padding: f32 =
|
|
625
|
+
padding: ?f32 = null,
|
|
622
626
|
main: canvas.WidgetMainAlignment = .start,
|
|
623
627
|
cross: canvas.WidgetCrossAlignment = .stretch,
|
|
624
628
|
/// Line policy for `text` leaves. `true`: word-wrap through
|
|
@@ -766,6 +770,12 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
766
770
|
/// the live bottom. Meaningless on every other element.
|
|
767
771
|
scrollback: u32 = 0,
|
|
768
772
|
on_press: ?Msg = null,
|
|
773
|
+
/// Live widget drag message (markup: `on-drag`). Its payload
|
|
774
|
+
/// is the closed `{ sourceId, phase, x, y, viewWidth,
|
|
775
|
+
/// viewHeight }` record: markup supplies `sourceId`, while the
|
|
776
|
+
/// runtime supplies phase (0 change, 1 end, 2 cancel) and the
|
|
777
|
+
/// four geometry fields.
|
|
778
|
+
on_drag: ?Msg = null,
|
|
769
779
|
/// Double-click Msg (markup: `on-double-press`): dispatched
|
|
770
780
|
/// on a release whose click count reached 2, in place of
|
|
771
781
|
/// `on_press` for that release. The FIRST click of the double
|
|
@@ -923,6 +933,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
923
933
|
wrap: ?bool = null,
|
|
924
934
|
style_tokens: StyleTokenRefs = .{},
|
|
925
935
|
on_press: ?Msg = null,
|
|
936
|
+
on_drag: ?Msg = null,
|
|
926
937
|
on_double_press: ?Msg = null,
|
|
927
938
|
on_toggle: ?Msg = null,
|
|
928
939
|
on_change: ?Msg = null,
|
|
@@ -1229,6 +1240,41 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
1229
1240
|
return null;
|
|
1230
1241
|
}
|
|
1231
1242
|
|
|
1243
|
+
/// Build a live drag Msg by copying the handler's authored
|
|
1244
|
+
/// source payload and injecting runtime phase + geometry.
|
|
1245
|
+
pub fn msgForDrag(self: Tree, id: ObjectId, drag: canvas.WidgetDragEvent, view_size: geometry.SizeF) ?Msg {
|
|
1246
|
+
for (self.handlers) |handler| {
|
|
1247
|
+
if (handler.id != id or handler.event != .drag or handler.action != .message) continue;
|
|
1248
|
+
return msgForDragTemplate(handler.action.message, drag, view_size);
|
|
1249
|
+
}
|
|
1250
|
+
return null;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
/// Rehydrate a previously captured drag handler template with the
|
|
1254
|
+
/// terminal phase and geometry. UiApp uses this when the source's
|
|
1255
|
+
/// phase-0 update unmounted the live handler before cancellation.
|
|
1256
|
+
pub fn msgForDragTemplate(template: Msg, drag: canvas.WidgetDragEvent, view_size: geometry.SizeF) ?Msg {
|
|
1257
|
+
return switch (template) {
|
|
1258
|
+
inline else => |payload, tag| if (comptime reflect.declaredWidgetDragDropRecord(@TypeOf(payload))) blk: {
|
|
1259
|
+
var out = payload;
|
|
1260
|
+
out.phase = dragNumber(@FieldType(@TypeOf(payload), "phase"), @floatFromInt(@intFromEnum(drag.phase)));
|
|
1261
|
+
out.x = dragNumber(@FieldType(@TypeOf(payload), "x"), drag.point.x);
|
|
1262
|
+
out.y = dragNumber(@FieldType(@TypeOf(payload), "y"), drag.point.y);
|
|
1263
|
+
out.viewWidth = dragNumber(@FieldType(@TypeOf(payload), "viewWidth"), view_size.width);
|
|
1264
|
+
out.viewHeight = dragNumber(@FieldType(@TypeOf(payload), "viewHeight"), view_size.height);
|
|
1265
|
+
break :blk @unionInit(Msg, @tagName(tag), out);
|
|
1266
|
+
} else null,
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
fn dragNumber(comptime Number: type, value: f32) Number {
|
|
1271
|
+
return switch (@typeInfo(Number)) {
|
|
1272
|
+
.float => @floatCast(value),
|
|
1273
|
+
.int => @intFromFloat(@round(value)),
|
|
1274
|
+
else => unreachable,
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1232
1278
|
/// Typed dispatch for text edits: builds the message through the
|
|
1233
1279
|
/// widget's `on_input` constructor.
|
|
1234
1280
|
pub fn msgForTextEdit(self: Tree, id: ObjectId, edit: canvas.TextInputEvent) ?Msg {
|
|
@@ -1469,6 +1515,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
1469
1515
|
.wrap = options.wrap,
|
|
1470
1516
|
.style_tokens = options.style_tokens,
|
|
1471
1517
|
.on_press = options.on_press,
|
|
1518
|
+
.on_drag = options.on_drag,
|
|
1472
1519
|
.on_double_press = options.on_double_press,
|
|
1473
1520
|
.on_toggle = options.on_toggle,
|
|
1474
1521
|
.on_change = options.on_change,
|
|
@@ -3335,6 +3382,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
3335
3382
|
// Typed handlers imply the matching accessibility actions, the
|
|
3336
3383
|
// same way a stringly `command` does for engine-owned dispatch.
|
|
3337
3384
|
if (node.on_press != null) widget.semantics.actions.press = true;
|
|
3385
|
+
if (node.on_drag != null) widget.semantics.actions.drag = true;
|
|
3338
3386
|
// A double-press handler makes the element pressable too:
|
|
3339
3387
|
// the double-click's first release must land somewhere, and
|
|
3340
3388
|
// an element that acts on double click without claiming
|
|
@@ -3383,6 +3431,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
3383
3431
|
widget.children = child_widgets;
|
|
3384
3432
|
}
|
|
3385
3433
|
appendHandler(handlers, handler_len, widget.id, .press, node.on_press);
|
|
3434
|
+
appendHandler(handlers, handler_len, widget.id, .drag, node.on_drag);
|
|
3386
3435
|
appendHandler(handlers, handler_len, widget.id, .double_press, node.on_double_press);
|
|
3387
3436
|
appendHandler(handlers, handler_len, widget.id, .toggle, node.on_toggle);
|
|
3388
3437
|
appendHandler(handlers, handler_len, widget.id, .change, node.on_change);
|
|
@@ -3534,6 +3583,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
3534
3583
|
fn countHandlers(node: Node) usize {
|
|
3535
3584
|
var total: usize = 0;
|
|
3536
3585
|
if (node.on_press != null) total += 1;
|
|
3586
|
+
if (node.on_drag != null) total += 1;
|
|
3537
3587
|
if (node.on_double_press != null) total += 1;
|
|
3538
3588
|
if (node.on_toggle != null) total += 1;
|
|
3539
3589
|
if (node.on_change != null) total += 1;
|
|
@@ -3614,7 +3664,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
3614
3664
|
/// default gap. Explicit spacing always wins for its own field.
|
|
3615
3665
|
fn applyKindDefaultLayout(kind: WidgetKind, options: ElementOptions, layout: *canvas.WidgetLayoutStyle) void {
|
|
3616
3666
|
const defaults = canvas.widgetKindDefaultLayout(kind, options.size) orelse return;
|
|
3617
|
-
if (options.padding ==
|
|
3667
|
+
if (options.padding == null) {
|
|
3618
3668
|
layout.padding = defaults.padding;
|
|
3619
3669
|
layout.padding_is_kind_default = true;
|
|
3620
3670
|
}
|
|
@@ -3654,12 +3704,7 @@ pub fn Ui(comptime Msg: type) type {
|
|
|
3654
3704
|
.disabled = options.disabled,
|
|
3655
3705
|
},
|
|
3656
3706
|
.layout = .{
|
|
3657
|
-
.padding = .
|
|
3658
|
-
.top = options.padding,
|
|
3659
|
-
.right = options.padding,
|
|
3660
|
-
.bottom = options.padding,
|
|
3661
|
-
.left = options.padding,
|
|
3662
|
-
},
|
|
3707
|
+
.padding = geometry.InsetsF.all(options.padding orelse 0),
|
|
3663
3708
|
.gap = options.gap,
|
|
3664
3709
|
.grow = options.grow,
|
|
3665
3710
|
.main_alignment = options.main,
|
|
@@ -1122,6 +1122,7 @@ pub const on_scroll_legacy_payload_message = "on-scroll payloads are two-axis no
|
|
|
1122
1122
|
|
|
1123
1123
|
pub const on_resize_element_message = "on-resize is only supported on split - the runtime emits fraction changes for split dividers, so the handler belongs on the split element itself";
|
|
1124
1124
|
pub const on_resize_payload_message = "on-resize takes a bare Msg tag whose payload is the new first-pane fraction (an f32 variant, like sidebar_resized: f32; transpiled cores declare a one-number float arm)";
|
|
1125
|
+
pub const on_drag_payload_message = "on-drag takes a Msg tag plus one integer source binding (for example card_dragged:{card.id}); that Msg arm must be exactly { sourceId, phase, x, y, viewWidth, viewHeight } - sourceId is numeric, phase is numeric and must represent 0..2, and the four live geometry fields are f32/f64 (or wider floats) so captured out-of-view coordinates cannot overflow; markup fills sourceId and the runtime fills the rest";
|
|
1125
1126
|
pub const split_children_message = "split takes exactly two element children (the panes) - put conditional or repeated content inside a pane container, and nest splits for more panes";
|
|
1126
1127
|
|
|
1127
1128
|
/// Elements the runtime's dismissal machinery closes (Escape, click
|
|
@@ -2032,9 +2032,18 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
|
|
|
2032
2032
|
};
|
|
2033
2033
|
},
|
|
2034
2034
|
.bool => @field(options, zig_field) = evalExpr(node, entries, raw, ui, model, scope).truthy(),
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2035
|
+
.optional => |optional| switch (@typeInfo(optional.child)) {
|
|
2036
|
+
.bool => @field(options, zig_field) = evalExpr(node, entries, raw, ui, model, scope).truthy(),
|
|
2037
|
+
.float => {
|
|
2038
|
+
comptime requireVariant(variant, &.{ .float, .integer }, node, "expected a number");
|
|
2039
|
+
@field(options, zig_field) = switch (evalExpr(node, entries, raw, ui, model, scope)) {
|
|
2040
|
+
.float => |float| float,
|
|
2041
|
+
.integer => |int| @floatFromInt(int),
|
|
2042
|
+
else => runtimeFail(optional.child, ui),
|
|
2043
|
+
};
|
|
2044
|
+
},
|
|
2045
|
+
else => runtimeFail(optional.child, ui),
|
|
2046
|
+
},
|
|
2038
2047
|
.int => {
|
|
2039
2048
|
comptime requireVariant(variant, &.{.integer}, node, "expected a whole number");
|
|
2040
2049
|
// Range-checked against the field's own integer type
|
|
@@ -2145,6 +2154,10 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
|
|
|
2145
2154
|
options.on_resize = comptime (resizeConstructor(expression.tag) orelse fail(node, markup.on_resize_payload_message));
|
|
2146
2155
|
return;
|
|
2147
2156
|
}
|
|
2157
|
+
if (comptime std.mem.eql(u8, event, "drag")) {
|
|
2158
|
+
options.on_drag = constructDragMessage(node, expression, entries, ui, model, scope);
|
|
2159
|
+
return;
|
|
2160
|
+
}
|
|
2148
2161
|
// The value-payload change event: a slider's `on-change` with a
|
|
2149
2162
|
// bare tag naming a value-carrying arm dispatches the APPLIED
|
|
2150
2163
|
// value through the `on_value` constructor (the `on-resize`
|
|
@@ -2333,6 +2346,22 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
|
|
|
2333
2346
|
return @unionInit(MsgT, field.name, coerce(field.type, node, variant, ui, value));
|
|
2334
2347
|
}
|
|
2335
2348
|
|
|
2349
|
+
fn constructDragMessage(comptime node: markup.MarkupNode, comptime expression: markup.MessageExpression, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) MsgT {
|
|
2350
|
+
comptime {
|
|
2351
|
+
if (expression.payload.len == 0) fail(node, markup.on_drag_payload_message);
|
|
2352
|
+
}
|
|
2353
|
+
const tag_index = comptime (msgTagIndex(expression.tag) orelse fail(node, markup.on_drag_payload_message));
|
|
2354
|
+
const field = comptime @typeInfo(MsgT).@"union".fields[tag_index];
|
|
2355
|
+
comptime {
|
|
2356
|
+
if (!interpreter.declaredWidgetDragDropRecord(field.type)) fail(node, markup.on_drag_payload_message);
|
|
2357
|
+
}
|
|
2358
|
+
const variant = comptime pathVariant(node, entries, expression.payload, true);
|
|
2359
|
+
const value = bindingValue(node, entries, expression.payload, ui, model, scope, true);
|
|
2360
|
+
var payload: field.type = std.mem.zeroes(field.type);
|
|
2361
|
+
payload.sourceId = coerce(@FieldType(field.type, "sourceId"), node, variant, ui, value);
|
|
2362
|
+
return @unionInit(MsgT, field.name, payload);
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2336
2365
|
/// Runtime mirror of the interpreter's `coerce`, with the
|
|
2337
2366
|
/// type-determined mismatches promoted to compile errors. Only
|
|
2338
2367
|
/// value-dependent conversions (optional bindings, enum tags from
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
const std = @import("std");
|
|
14
14
|
const canvas = @import("root.zig");
|
|
15
|
+
const geometry = @import("geometry");
|
|
15
16
|
const markup_view = @import("ui_markup_view.zig");
|
|
16
17
|
const fixture = @import("ui_markup_view_tests.zig");
|
|
17
18
|
|
|
@@ -91,6 +92,32 @@ const InboxUi = canvas.Ui(fixture.Msg);
|
|
|
91
92
|
const InboxInterpreter = markup_view.MarkupView(fixture.Model, fixture.Msg);
|
|
92
93
|
const InboxCompiled = canvas.CompiledMarkupView(fixture.Model, fixture.Msg, fixture.inbox_markup_source);
|
|
93
94
|
|
|
95
|
+
const zero_card_padding_markup =
|
|
96
|
+
\\<card padding="0">
|
|
97
|
+
\\ <text>Flush</text>
|
|
98
|
+
\\</card>
|
|
99
|
+
;
|
|
100
|
+
const ZeroCardPaddingCompiled = canvas.CompiledMarkupView(fixture.Model, fixture.Msg, zero_card_padding_markup);
|
|
101
|
+
|
|
102
|
+
test "explicit zero card padding survives interpreted and compiled markup" {
|
|
103
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
104
|
+
defer arena_state.deinit();
|
|
105
|
+
const arena = arena_state.allocator();
|
|
106
|
+
const model = fixture.testModel();
|
|
107
|
+
|
|
108
|
+
var interpreter = try InboxInterpreter.init(arena, zero_card_padding_markup);
|
|
109
|
+
var interpreter_ui = InboxUi.init(arena);
|
|
110
|
+
const interpreted = try interpreter_ui.finalize(try interpreter.build(&interpreter_ui, &model));
|
|
111
|
+
var compiled_ui = InboxUi.init(arena);
|
|
112
|
+
const compiled = try compiled_ui.finalize(ZeroCardPaddingCompiled.build(&compiled_ui, &model));
|
|
113
|
+
|
|
114
|
+
try testing.expectEqual(canvas.WidgetKind.card, interpreted.root.kind);
|
|
115
|
+
try testing.expectEqual(geometry.InsetsF{}, interpreted.root.layout.padding);
|
|
116
|
+
try testing.expect(!interpreted.root.layout.padding_is_kind_default);
|
|
117
|
+
try testing.expectEqualDeep(interpreted.root.layout.padding, compiled.root.layout.padding);
|
|
118
|
+
try testing.expect(!compiled.root.layout.padding_is_kind_default);
|
|
119
|
+
}
|
|
120
|
+
|
|
94
121
|
fn interpretInbox(arena: std.mem.Allocator, model: *const fixture.Model) !InboxUi.Tree {
|
|
95
122
|
var view = try InboxInterpreter.init(arena, fixture.inbox_markup_source);
|
|
96
123
|
var ui = InboxUi.init(arena);
|
|
@@ -54,7 +54,14 @@ pub const ValueKind = expr.ValueKind;
|
|
|
54
54
|
/// gained a bare-tag rule — a format-2 artifact classified a four-field
|
|
55
55
|
/// terminal message as `.unsupported` and would reject valid `<terminal>`
|
|
56
56
|
/// markup, so a stale artifact must force regeneration rather than pass.
|
|
57
|
-
|
|
57
|
+
/// Version 4: `drag_drop` classifies the closed release-geometry record
|
|
58
|
+
/// used by `on-drag`; older artifacts call it unsupported and would reject
|
|
59
|
+
/// a valid binding.
|
|
60
|
+
/// Version 5: that record gained the numeric live `phase` field so apps can
|
|
61
|
+
/// preview insertion/reordering on change and restore on cancellation.
|
|
62
|
+
/// Version 6: live drag geometry requires floating-point fields so captured
|
|
63
|
+
/// out-of-view coordinates cannot trap an integer conversion at dispatch.
|
|
64
|
+
pub const format_version: u32 = 6;
|
|
58
65
|
|
|
59
66
|
/// Where the app's build step writes the artifact, relative to the app
|
|
60
67
|
/// directory (a build product lives under zig-out, not in durable state).
|
|
@@ -139,7 +146,7 @@ pub const Iterable = struct {
|
|
|
139
146
|
/// cannot be built from markup at all. `legacy_scroll_state` is the
|
|
140
147
|
/// RETIRED one-axis scroll record, recognized only so `on-scroll` can
|
|
141
148
|
/// teach the two-axis migration by field name.
|
|
142
|
-
pub const PayloadClass = enum { none, string, integer, float, boolean, enum_tag, text_input, scroll_state, legacy_scroll_state, terminal_state, unsupported };
|
|
149
|
+
pub const PayloadClass = enum { none, string, integer, float, boolean, enum_tag, text_input, scroll_state, legacy_scroll_state, terminal_state, drag_drop, unsupported };
|
|
143
150
|
|
|
144
151
|
pub const MsgTag = struct {
|
|
145
152
|
name: []const u8,
|
|
@@ -348,6 +355,7 @@ fn payloadClassOf(comptime T: type, comptime specials: Specials) PayloadClass {
|
|
|
348
355
|
// binds through on-terminal exactly like the canvas type — same
|
|
349
356
|
// resolution as both engines' terminalConstructor.
|
|
350
357
|
if (reflect.declaredTerminalStateRecord(T)) return .terminal_state;
|
|
358
|
+
if (reflect.declaredWidgetDragDropRecord(T)) return .drag_drop;
|
|
351
359
|
return switch (@typeInfo(T)) {
|
|
352
360
|
.int => .integer,
|
|
353
361
|
.float => .float,
|
|
@@ -904,6 +912,15 @@ const Checker = struct {
|
|
|
904
912
|
}
|
|
905
913
|
return;
|
|
906
914
|
}
|
|
915
|
+
if (std.mem.eql(u8, event, "drag")) {
|
|
916
|
+
const found = tag orelse return self.failAttr(node, attribute, markup.on_drag_payload_message);
|
|
917
|
+
if (found.payload != .drag_drop or expression.payload.len == 0) {
|
|
918
|
+
return self.failAttr(node, attribute, markup.on_drag_payload_message);
|
|
919
|
+
}
|
|
920
|
+
const resolved = try self.resolveBinding(node, expression.payload, true);
|
|
921
|
+
try self.requirePayloadKind(node, attribute, resolved, &.{.integer}, found);
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
907
924
|
// The value-payload change event (engine parity): a slider's
|
|
908
925
|
// on-change with a bare tag naming a value-carrying arm — f32 (the
|
|
909
926
|
// canvas-native shape) or the transpiled one-number float arm
|
|
@@ -931,7 +948,7 @@ const Checker = struct {
|
|
|
931
948
|
.boolean => {},
|
|
932
949
|
// These payloads cannot be constructed from a markup binding
|
|
933
950
|
// (input/scroll payloads bind through their own events).
|
|
934
|
-
.text_input, .scroll_state, .legacy_scroll_state, .terminal_state, .unsupported => return self.failPayloadType(node, attribute, resolved, found),
|
|
951
|
+
.text_input, .scroll_state, .legacy_scroll_state, .terminal_state, .drag_drop, .unsupported => return self.failPayloadType(node, attribute, resolved, found),
|
|
935
952
|
.none => unreachable,
|
|
936
953
|
}
|
|
937
954
|
}
|
|
@@ -256,6 +256,47 @@ pub fn declaredTerminalStateRecord(comptime T: type) bool {
|
|
|
256
256
|
return declaredRecordMatchesVocabulary(T, &terminal_state_field_names, &terminal_state_field_names);
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
+
/// A markup `on-drag` Msg payload. `sourceId` is filled from the authored
|
|
260
|
+
/// binding (`on-drag="card_dropped:{card.id}"`); the runtime fills the
|
|
261
|
+
/// drag phase, point, and receiving view size. Keeping this as one closed
|
|
262
|
+
/// record makes the event usable from transpiled cores without type
|
|
263
|
+
/// identity crossing the generated-module boundary.
|
|
264
|
+
pub fn declaredWidgetDragDropRecord(comptime T: type) bool {
|
|
265
|
+
const info = switch (@typeInfo(T)) {
|
|
266
|
+
.@"struct" => |s| s,
|
|
267
|
+
else => return false,
|
|
268
|
+
};
|
|
269
|
+
if (info.fields.len != 6) return false;
|
|
270
|
+
if (!@hasField(T, "sourceId") or !@hasField(T, "phase") or !@hasField(T, "x") or !@hasField(T, "y") or
|
|
271
|
+
!@hasField(T, "viewWidth") or !@hasField(T, "viewHeight")) return false;
|
|
272
|
+
if (!isNumeric(@FieldType(T, "sourceId"))) return false;
|
|
273
|
+
if (!isDragPhaseNumber(@FieldType(T, "phase"))) return false;
|
|
274
|
+
inline for (.{ "x", "y", "viewWidth", "viewHeight" }) |name| {
|
|
275
|
+
if (!isDragGeometryNumber(@FieldType(T, name))) return false;
|
|
276
|
+
}
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/// Live geometry starts as f32 and can be negative while pointer capture
|
|
281
|
+
/// carries a drag outside the view. Require a float at least as wide as the
|
|
282
|
+
/// source so every accepted record is injectable without a range trap.
|
|
283
|
+
fn isDragGeometryNumber(comptime T: type) bool {
|
|
284
|
+
return switch (@typeInfo(T)) {
|
|
285
|
+
.float => |info| info.bits >= 32,
|
|
286
|
+
else => false,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/// Phase injection needs to represent all three wire values (0, 1, 2).
|
|
291
|
+
/// Floats do so exactly; integer records need an honest upper bound.
|
|
292
|
+
fn isDragPhaseNumber(comptime T: type) bool {
|
|
293
|
+
return switch (@typeInfo(T)) {
|
|
294
|
+
.float => true,
|
|
295
|
+
.int => std.math.maxInt(T) >= 2,
|
|
296
|
+
else => false,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
259
300
|
/// A mirror of the RETIRED one-axis scroll state — `{offset, velocity,
|
|
260
301
|
/// viewport_extent, content_extent}` in either spelling. Recognized only
|
|
261
302
|
/// to fail with a teaching that names the new per-axis fields, so an app
|