@native-sdk/cli 0.5.0 → 0.5.1

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.
@@ -1274,13 +1274,77 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1274
1274
  self.reloadMarkup(self.options.markup.?.source) catch {};
1275
1275
  }
1276
1276
  }
1277
- const tokens = runtime.tokensWithTextMeasure(self.effectiveTokens());
1277
+ var tokens = runtime.tokensWithTextMeasure(self.effectiveTokens());
1278
1278
  const next_index = self.arena_index ^ 1;
1279
1279
  // Widget layout is inset by the runtime's viewport chrome
1280
1280
  // (safe areas + keyboard on mobile, zero on desktop); the
1281
1281
  // canvas itself stays surface-sized so chrome and the clear
1282
1282
  // color still paint edge to edge under notches and bars.
1283
1283
  const bounds = geometry.RectF.fromSize(self.canvas_size).deflate(self.layoutViewportInsets(runtime, window_id));
1284
+ var built = try self.buildLayoutPass(runtime, window_id, bounds, tokens, next_index);
1285
+ // Window-control clearance is a one-retry pass like the
1286
+ // virtual-window coverage retry inside the build: only when
1287
+ // the finished layout left a drag header's CONTENT under the
1288
+ // OS window-control cluster (Windows caption buttons
1289
+ // trailing, macOS traffic lights leading) is the cluster
1290
+ // stamped into the tokens for one more build — drag rows
1291
+ // then lay their content out clear of it
1292
+ // (`canvas.windowControlsClearedContent` inside layout).
1293
+ // Apps whose headers already pad through the chrome
1294
+ // channel's insets never collide, never retry, and keep a
1295
+ // byte-identical layout.
1296
+ if (windowControlsReservation(runtime, window_id, self.options.canvas_label, built.layout, tokens)) |controls| {
1297
+ tokens.window_controls = controls;
1298
+ built = try self.buildLayoutPass(runtime, window_id, bounds, tokens, next_index);
1299
+ }
1300
+ const tree = built.tree;
1301
+ const layout = built.layout;
1302
+ launch_timing.lapOnce("first_view_built");
1303
+
1304
+ if (self.options.chrome) |chrome| {
1305
+ try self.installChromeDisplayList(runtime, window_id, chrome, layout, tokens);
1306
+ } else {
1307
+ _ = try runtime.setCanvasWidgetLayout(window_id, self.options.canvas_label, layout);
1308
+ if (self.installed and self.rebuildEmitsTokens(runtime, window_id, self.options.canvas_label, tokens)) {
1309
+ _ = try runtime.emitCanvasWidgetDisplayList(window_id, self.options.canvas_label, tokens);
1310
+ }
1311
+ }
1312
+
1313
+ self.tree = tree;
1314
+ self.arena_index = next_index;
1315
+ // The fallback menu's target vanished from this build (the
1316
+ // model dropped the row, or its menu emptied): the open state
1317
+ // has nothing to present, so it closes.
1318
+ if (self.contextMenuFallbackTargetForLabel(self.options.canvas_label) != 0 and tree.context_menu_fallback == null) {
1319
+ self.clearContextMenuFallback();
1320
+ }
1321
+ try self.scheduleAnimations(runtime, window_id);
1322
+ try self.scheduleLayoutTweens(runtime, window_id);
1323
+ self.applyWebPanes(runtime, window_id, layout);
1324
+ self.applyStatusItem(runtime);
1325
+ self.applyWindows(runtime);
1326
+ self.applyChromeSelection();
1327
+ self.applyChromeNavigation();
1328
+ }
1329
+
1330
+ const BuiltLayout = struct {
1331
+ tree: Ui.Tree,
1332
+ layout: canvas.WidgetLayoutTree,
1333
+ };
1334
+
1335
+ /// One view-build + flex-layout pass of `rebuild`, including the
1336
+ /// windowed-virtual-list coverage retry: builds the tree into the
1337
+ /// next arena and lays it out against `bounds` with `tokens`.
1338
+ /// Extracted so rebuild can run it again with the window-control
1339
+ /// reservation stamped when the first pass proved a collision.
1340
+ fn buildLayoutPass(
1341
+ self: *Self,
1342
+ runtime: *Runtime,
1343
+ window_id: platform.WindowId,
1344
+ bounds: geometry.RectF,
1345
+ tokens: canvas.DesignTokens,
1346
+ next_index: usize,
1347
+ ) anyerror!BuiltLayout {
1284
1348
  var window_source = VirtualWindowResolver{
1285
1349
  .runtime = runtime,
1286
1350
  .window_id = window_id,
@@ -1342,32 +1406,32 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1342
1406
  if (pass >= 2 or (!corrected and !self.virtualWindowsUndercovered(layout))) break;
1343
1407
  window_source.fresh = layout;
1344
1408
  }
1345
- launch_timing.lapOnce("first_view_built");
1346
-
1347
- if (self.options.chrome) |chrome| {
1348
- try self.installChromeDisplayList(runtime, window_id, chrome, layout, tokens);
1349
- } else {
1350
- _ = try runtime.setCanvasWidgetLayout(window_id, self.options.canvas_label, layout);
1351
- if (self.installed and self.rebuildEmitsTokens(runtime, window_id, self.options.canvas_label, tokens)) {
1352
- _ = try runtime.emitCanvasWidgetDisplayList(window_id, self.options.canvas_label, tokens);
1409
+ return .{ .tree = tree, .layout = layout };
1410
+ }
1411
+
1412
+ /// The window-control cluster's canvas-local frame — but only
1413
+ /// when the just-built layout actually left drag-header content
1414
+ /// under it, so a rebuild (the main canvas's `rebuild` or a
1415
+ /// secondary window's `rebuildWindowSlot`) knows a clearance
1416
+ /// retry will change something. Cheap in the common case: a tree
1417
+ /// with no visible drag region never even polls the platform's
1418
+ /// chrome report, and standard-chrome windows report a zero
1419
+ /// cluster. `tokens` must be the tokens the layout was built
1420
+ /// with: the scan re-measures text through the same seam to
1421
+ /// judge painted bounds, not frames.
1422
+ fn windowControlsReservation(runtime: *Runtime, window_id: platform.WindowId, canvas_label: []const u8, layout: canvas.WidgetLayoutTree, tokens: canvas.DesignTokens) ?geometry.RectF {
1423
+ var has_drag_region = false;
1424
+ for (layout.nodes) |node| {
1425
+ if (canvas.widgetIsWindowDragRegion(node.widget)) {
1426
+ has_drag_region = true;
1427
+ break;
1353
1428
  }
1354
1429
  }
1355
-
1356
- self.tree = tree;
1357
- self.arena_index = next_index;
1358
- // The fallback menu's target vanished from this build (the
1359
- // model dropped the row, or its menu emptied): the open state
1360
- // has nothing to present, so it closes.
1361
- if (self.contextMenuFallbackTargetForLabel(self.options.canvas_label) != 0 and tree.context_menu_fallback == null) {
1362
- self.clearContextMenuFallback();
1363
- }
1364
- try self.scheduleAnimations(runtime, window_id);
1365
- try self.scheduleLayoutTweens(runtime, window_id);
1366
- self.applyWebPanes(runtime, window_id, layout);
1367
- self.applyStatusItem(runtime);
1368
- self.applyWindows(runtime);
1369
- self.applyChromeSelection();
1370
- self.applyChromeNavigation();
1430
+ if (!has_drag_region) return null;
1431
+ const controls = runtime.windowControlsForView(window_id, canvas_label);
1432
+ if (controls.width <= 0 or controls.height <= 0) return null;
1433
+ if (!canvas.windowDragContentUnderWindowControls(layout.nodes, controls, tokens)) return null;
1434
+ return controls;
1371
1435
  }
1372
1436
 
1373
1437
  /// Re-derive the model's selected chrome tab after a rebuild
@@ -1880,16 +1944,54 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1880
1944
  }
1881
1945
 
1882
1946
  fn rebuildWindowSlot(self: *Self, runtime: *Runtime, slot: *WindowSlot) anyerror!void {
1883
- const window_view = self.options.window_view orelse return;
1884
- const tokens = runtime.tokensWithTextMeasure(self.slotEffectiveTokens(slot));
1947
+ if (self.options.window_view == null) return;
1948
+ var tokens = runtime.tokensWithTextMeasure(self.slotEffectiveTokens(slot));
1885
1949
  const next_index = slot.arena_index ^ 1;
1950
+ const bounds = geometry.RectF.fromSize(slot.canvas_size).deflate(runtime.viewportInsetsForWindow(slot.window_id));
1951
+ var built = try self.buildWindowSlotPass(slot, bounds, tokens, next_index);
1952
+ // The same one-retry window-control clearance the main
1953
+ // rebuild runs (see `rebuild`): a secondary hidden-inset
1954
+ // window's drag header collides with the OS caption cluster
1955
+ // exactly like the main window's, so a proven collision
1956
+ // stamps the cluster into THIS slot's tokens (a local copy —
1957
+ // other windows keep their own layout) for one more pass.
1958
+ if (windowControlsReservation(runtime, slot.window_id, slot.canvasLabel(), built.layout, tokens)) |controls| {
1959
+ tokens.window_controls = controls;
1960
+ built = try self.buildWindowSlotPass(slot, bounds, tokens, next_index);
1961
+ }
1962
+ const tree = built.tree;
1963
+ const layout = built.layout;
1964
+ _ = try runtime.setCanvasWidgetLayout(slot.window_id, slot.canvasLabel(), layout);
1965
+ if (slot.installed and self.rebuildEmitsTokens(runtime, slot.window_id, slot.canvasLabel(), tokens)) {
1966
+ _ = try runtime.emitCanvasWidgetDisplayList(slot.window_id, slot.canvasLabel(), tokens);
1967
+ }
1968
+ slot.tree = tree;
1969
+ slot.arena_index = next_index;
1970
+ // Same close-on-vanish rule as the main canvas rebuild.
1971
+ if (self.contextMenuFallbackTargetForLabel(slot.canvasLabel()) != 0 and tree.context_menu_fallback == null) {
1972
+ self.clearContextMenuFallback();
1973
+ }
1974
+ }
1975
+
1976
+ /// One view-build + flex-layout pass of `rebuildWindowSlot`,
1977
+ /// the slot-path sibling of `buildLayoutPass`: resets the slot's
1978
+ /// next arena (retaining capacity, so the clearance retry reuses
1979
+ /// the same pass's memory) and builds the window view against
1980
+ /// `bounds` with `tokens`.
1981
+ fn buildWindowSlotPass(
1982
+ self: *Self,
1983
+ slot: *WindowSlot,
1984
+ bounds: geometry.RectF,
1985
+ tokens: canvas.DesignTokens,
1986
+ next_index: usize,
1987
+ ) anyerror!BuiltLayout {
1988
+ const window_view = self.options.window_view.?;
1886
1989
  _ = slot.arenas[next_index].reset(.retain_capacity);
1887
1990
  var ui = Ui.init(slot.arenas[next_index].allocator());
1888
1991
  ui.context_menu_fallback_target = self.contextMenuFallbackTargetForLabel(slot.canvasLabel());
1889
1992
  self.armUiFragmentHost(&ui);
1890
1993
  const node = window_view(&ui, &self.model, slot.label());
1891
1994
  const tree = try ui.finalizeWithTokens(node, tokens);
1892
- const bounds = geometry.RectF.fromSize(slot.canvas_size).deflate(runtime.viewportInsetsForWindow(slot.window_id));
1893
1995
  const layout = canvas.layoutWidgetTreeWithTokens(tree.root, bounds, tokens, &self.layout_nodes) catch |err| {
1894
1996
  if (err == error.WidgetLayoutListFull) {
1895
1997
  ui_app_log.warn(
@@ -1899,16 +2001,7 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1899
2001
  }
1900
2002
  return err;
1901
2003
  };
1902
- _ = try runtime.setCanvasWidgetLayout(slot.window_id, slot.canvasLabel(), layout);
1903
- if (slot.installed and self.rebuildEmitsTokens(runtime, slot.window_id, slot.canvasLabel(), tokens)) {
1904
- _ = try runtime.emitCanvasWidgetDisplayList(slot.window_id, slot.canvasLabel(), tokens);
1905
- }
1906
- slot.tree = tree;
1907
- slot.arena_index = next_index;
1908
- // Same close-on-vanish rule as the main canvas rebuild.
1909
- if (self.contextMenuFallbackTargetForLabel(slot.canvasLabel()) != 0 and tree.context_menu_fallback == null) {
1910
- self.clearContextMenuFallback();
1911
- }
2004
+ return .{ .tree = tree, .layout = layout };
1912
2005
  }
1913
2006
 
1914
2007
  /// Re-apply the model-derived webview panes against the freshly
@@ -3185,6 +3185,227 @@ test "ui app delivers chrome overlay geometry before install and on change" {
3185
3185
  try std.testing.expect(try retainedTextExists(&harness.runtime, "leading 0"));
3186
3186
  }
3187
3187
 
3188
+ // ---------------------------------- window-control clearance fixtures
3189
+
3190
+ const CaptionModel = struct {
3191
+ /// The trailing chrome reservation the CONTRACT app consumes (the
3192
+ /// soundboard pattern: a spacer sized to `insets.right`); the naive
3193
+ /// app leaves it unwired at 0, exactly like an app that never
3194
+ /// subscribed to the chrome channel.
3195
+ trailing: f32 = 0,
3196
+ };
3197
+
3198
+ const CaptionMsg = union(enum) {
3199
+ chrome: zero_platform.WindowChrome,
3200
+ };
3201
+
3202
+ const CaptionApp = ui_app_model.UiApp(CaptionModel, CaptionMsg);
3203
+
3204
+ fn captionUpdate(model: *CaptionModel, msg: CaptionMsg) void {
3205
+ switch (msg) {
3206
+ .chrome => |chrome| model.trailing = chrome.insets.right,
3207
+ }
3208
+ }
3209
+
3210
+ /// A hidden-titlebar header that never consumed the chrome channel:
3211
+ /// title leading, status text trailing — the system-monitor shape whose
3212
+ /// status line rendered UNDER the Windows caption buttons.
3213
+ fn captionNaiveView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
3214
+ _ = model;
3215
+ return ui.column(.{}, .{
3216
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
3217
+ ui.text(.{}, "Monitor"),
3218
+ ui.el(.stack, .{ .grow = 1 }, .{}),
3219
+ ui.text(.{}, "sampling"),
3220
+ }),
3221
+ ui.text(.{}, "body"),
3222
+ });
3223
+ }
3224
+
3225
+ /// The documented contract shape (the soundboard pattern): the header
3226
+ /// ends with a spacer sized to `insets.right`, so its own content
3227
+ /// already clears the caption cluster and the runtime reservation must
3228
+ /// stay out of the way.
3229
+ fn captionContractView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
3230
+ return ui.column(.{}, .{
3231
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
3232
+ ui.text(.{}, "Monitor"),
3233
+ ui.el(.stack, .{ .grow = 1 }, .{}),
3234
+ ui.text(.{}, "sampling"),
3235
+ ui.el(.stack, .{ .width = model.trailing }, .{}),
3236
+ }),
3237
+ ui.text(.{}, "body"),
3238
+ });
3239
+ }
3240
+
3241
+ /// The centered-title pattern: ONE grow text spanning the header row.
3242
+ /// Its FRAME runs under the caption cluster, but the centered glyphs
3243
+ /// sit well clear of it — nothing here needs (or may pay for) the
3244
+ /// clearance retry.
3245
+ fn captionCenteredView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
3246
+ _ = model;
3247
+ return ui.column(.{}, .{
3248
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
3249
+ ui.text(.{ .grow = 1, .text_alignment = .center }, "Monitor"),
3250
+ }),
3251
+ ui.text(.{}, "body"),
3252
+ });
3253
+ }
3254
+
3255
+ fn captionChromeMap(chrome: zero_platform.WindowChrome) ?CaptionMsg {
3256
+ return .{ .chrome = chrome };
3257
+ }
3258
+
3259
+ fn captionTextFrame(runtime: *core.Runtime, text: []const u8) !geometry.RectF {
3260
+ const layout = try runtime.canvasWidgetLayout(1, canvas_label);
3261
+ for (layout.nodes) |node| {
3262
+ if (node.widget.kind == .text and std.mem.eql(u8, node.widget.text, text)) return node.frame;
3263
+ }
3264
+ return error.TestUnexpectedResult;
3265
+ }
3266
+
3267
+ test "drag header trailing content stays clear of the Windows caption cluster" {
3268
+ // Windows-shaped hidden-titlebar chrome on a 400pt window: the DWM
3269
+ // caption cluster overlays the trailing 138pt of the top band, and
3270
+ // the platform reports it through the same chrome channel macOS
3271
+ // reports the traffic lights on. The app never consumed the
3272
+ // channel, so its right-aligned header status would lay out flush
3273
+ // to the window edge — UNDER the min/max/close buttons. The runtime
3274
+ // detects the collision after layout and re-lays the drag header
3275
+ // with the cluster reserved, so the status text ends at the
3276
+ // cluster's leading edge instead.
3277
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3278
+ defer harness.destroy(std.testing.allocator);
3279
+ harness.null_platform.gpu_surfaces = true;
3280
+ harness.null_platform.window_chrome = .{
3281
+ .insets = .{ .top = 32, .right = 138 },
3282
+ .buttons = geometry.RectF.init(262, 0, 138, 32),
3283
+ };
3284
+
3285
+ const app_state = try std.testing.allocator.create(CaptionApp);
3286
+ defer std.testing.allocator.destroy(app_state);
3287
+ app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
3288
+ .name = "ui-app-caption-naive",
3289
+ .scene = counter_scene,
3290
+ .canvas_label = canvas_label,
3291
+ .update = captionUpdate,
3292
+ .view = captionNaiveView,
3293
+ });
3294
+ defer app_state.deinit();
3295
+ const app = app_state.app();
3296
+ try harness.start(app);
3297
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3298
+ .label = canvas_label,
3299
+ .size = geometry.SizeF.init(400, 300),
3300
+ .scale_factor = 1,
3301
+ .frame_index = 1,
3302
+ .timestamp_ns = 1_000_000,
3303
+ .nonblank = true,
3304
+ } });
3305
+ try std.testing.expect(app_state.installed);
3306
+
3307
+ // The trailing status text ends at (or before) the cluster's
3308
+ // leading edge; without the reservation it ended at the window's
3309
+ // right edge, inside the cluster.
3310
+ const status = try captionTextFrame(&harness.runtime, "sampling");
3311
+ try std.testing.expect(status.maxX() <= 262 + 0.01);
3312
+ // The leading title and the body below the band are untouched.
3313
+ const title = try captionTextFrame(&harness.runtime, "Monitor");
3314
+ try std.testing.expectEqual(@as(f32, 0), title.x);
3315
+ }
3316
+
3317
+ test "drag header that already pads the caption cluster keeps its layout" {
3318
+ // The contract app (the soundboard shape): its header ends with a
3319
+ // spacer consuming `insets.right`, so nothing collides and the
3320
+ // runtime reservation must NOT fire — a double reservation would
3321
+ // shove the status text a full cluster-width further left.
3322
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3323
+ defer harness.destroy(std.testing.allocator);
3324
+ harness.null_platform.gpu_surfaces = true;
3325
+ harness.null_platform.window_chrome = .{
3326
+ .insets = .{ .top = 32, .right = 138 },
3327
+ .buttons = geometry.RectF.init(262, 0, 138, 32),
3328
+ };
3329
+
3330
+ const app_state = try std.testing.allocator.create(CaptionApp);
3331
+ defer std.testing.allocator.destroy(app_state);
3332
+ app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
3333
+ .name = "ui-app-caption-contract",
3334
+ .scene = counter_scene,
3335
+ .canvas_label = canvas_label,
3336
+ .update = captionUpdate,
3337
+ .view = captionContractView,
3338
+ .on_chrome = captionChromeMap,
3339
+ });
3340
+ defer app_state.deinit();
3341
+ const app = app_state.app();
3342
+ try harness.start(app);
3343
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3344
+ .label = canvas_label,
3345
+ .size = geometry.SizeF.init(400, 300),
3346
+ .scale_factor = 1,
3347
+ .frame_index = 1,
3348
+ .timestamp_ns = 1_000_000,
3349
+ .nonblank = true,
3350
+ } });
3351
+ try std.testing.expect(app_state.installed);
3352
+ try std.testing.expectEqual(@as(f32, 138), app_state.model.trailing);
3353
+
3354
+ // The app's own spacer puts the status text at the cluster's edge;
3355
+ // the runtime must not reserve on top of it (a double reservation
3356
+ // would land it near 262 - 138 = 124).
3357
+ const status = try captionTextFrame(&harness.runtime, "sampling");
3358
+ try std.testing.expect(@abs(status.maxX() - 262) < 0.5);
3359
+ }
3360
+
3361
+ test "drag header centered title never pays the clearance retry" {
3362
+ // The false positive the painted-bounds scan removes: a grow text
3363
+ // spanning the header row with centered glyphs. Its FRAME runs
3364
+ // under the caption cluster, but nothing inked does — a frame-based
3365
+ // scan paid the one retry here, and the remedy's trimmed content
3366
+ // box then visibly shifted the centered title left. The scan judges
3367
+ // aligned painted bounds, so no retry fires and the layout is
3368
+ // byte-identical to an unstamped build: the title's grow frame
3369
+ // still spans the full row, cluster and all.
3370
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3371
+ defer harness.destroy(std.testing.allocator);
3372
+ harness.null_platform.gpu_surfaces = true;
3373
+ harness.null_platform.window_chrome = .{
3374
+ .insets = .{ .top = 32, .right = 138 },
3375
+ .buttons = geometry.RectF.init(262, 0, 138, 32),
3376
+ };
3377
+
3378
+ const app_state = try std.testing.allocator.create(CaptionApp);
3379
+ defer std.testing.allocator.destroy(app_state);
3380
+ app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
3381
+ .name = "ui-app-caption-centered",
3382
+ .scene = counter_scene,
3383
+ .canvas_label = canvas_label,
3384
+ .update = captionUpdate,
3385
+ .view = captionCenteredView,
3386
+ });
3387
+ defer app_state.deinit();
3388
+ const app = app_state.app();
3389
+ try harness.start(app);
3390
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3391
+ .label = canvas_label,
3392
+ .size = geometry.SizeF.init(400, 300),
3393
+ .scale_factor = 1,
3394
+ .frame_index = 1,
3395
+ .timestamp_ns = 1_000_000,
3396
+ .nonblank = true,
3397
+ } });
3398
+ try std.testing.expect(app_state.installed);
3399
+
3400
+ // No reservation fired: the title's frame still spans the whole
3401
+ // row. A paid retry would have trimmed the drag row's content box
3402
+ // at the cluster's leading edge (262) and re-centered the glyphs
3403
+ // inside the trimmed frame — the visible title shift.
3404
+ const title = try captionTextFrame(&harness.runtime, "Monitor");
3405
+ try std.testing.expectEqual(@as(f32, 0), title.x);
3406
+ try std.testing.expectEqual(@as(f32, 400), title.maxX());
3407
+ }
3408
+
3188
3409
  // -------------------------------------- windowed virtual list fixture
3189
3410
 
3190
3411
  const VirtualFeedModel = struct {
@@ -402,6 +402,188 @@ test "each window's tokens carry its own surface density, not the main canvas's"
402
402
  try std.testing.expectEqual(@as(f32, 1), main_after.pixel_snap.scale);
403
403
  }
404
404
 
405
+ // ------------------- window-control clearance in secondary windows
406
+
407
+ const CaptionWinModel = struct {
408
+ settings_open: bool = true,
409
+ /// The padded variant appends the contract spacer (the soundboard
410
+ /// pattern) so its header already clears the caption cluster.
411
+ padded: bool = false,
412
+ };
413
+
414
+ const CaptionWinMsg = union(enum) {
415
+ settings_closed,
416
+ };
417
+
418
+ const CaptionWinApp = ui_app_model.UiApp(CaptionWinModel, CaptionWinMsg);
419
+
420
+ fn captionWinUpdate(model: *CaptionWinModel, msg: CaptionWinMsg) void {
421
+ switch (msg) {
422
+ .settings_closed => model.settings_open = false,
423
+ }
424
+ }
425
+
426
+ fn captionWinView(ui: *CaptionWinApp.Ui, model: *const CaptionWinModel) CaptionWinApp.Ui.Node {
427
+ _ = model;
428
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
429
+ ui.text(.{}, "main body"),
430
+ });
431
+ }
432
+
433
+ fn captionWinWindows(model: *const CaptionWinModel, scratch: *CaptionWinApp.WindowsScratch) []const CaptionWinApp.WindowDescriptor {
434
+ var count: usize = 0;
435
+ if (model.settings_open) {
436
+ scratch.windows[count] = .{
437
+ .label = settings_window_label,
438
+ .canvas_label = settings_canvas_label,
439
+ .title = "Tools",
440
+ .width = 320,
441
+ .height = 240,
442
+ .on_close = .settings_closed,
443
+ };
444
+ count += 1;
445
+ }
446
+ return scratch.windows[0..count];
447
+ }
448
+
449
+ /// A hidden-inset SECONDARY window's titlebar: a drag row with a
450
+ /// leading title and a right-aligned status text — the same
451
+ /// system-monitor shape the main-canvas caption tests use, but built
452
+ /// through `window_view` and `rebuildWindowSlot`.
453
+ fn captionWinWindowView(ui: *CaptionWinApp.Ui, model: *const CaptionWinModel, window_label: []const u8) CaptionWinApp.Ui.Node {
454
+ std.debug.assert(std.mem.eql(u8, window_label, settings_window_label));
455
+ if (model.padded) {
456
+ return ui.column(.{}, .{
457
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
458
+ ui.text(.{}, "Tools"),
459
+ ui.el(.stack, .{ .grow = 1 }, .{}),
460
+ ui.text(.{}, "recording"),
461
+ ui.el(.stack, .{ .width = 138 }, .{}),
462
+ }),
463
+ ui.text(.{}, "body"),
464
+ });
465
+ }
466
+ return ui.column(.{}, .{
467
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
468
+ ui.text(.{}, "Tools"),
469
+ ui.el(.stack, .{ .grow = 1 }, .{}),
470
+ ui.text(.{}, "recording"),
471
+ }),
472
+ ui.text(.{}, "body"),
473
+ });
474
+ }
475
+
476
+ const CaptionWinFixture = struct {
477
+ harness: *core.TestHarness(),
478
+ app_state: *CaptionWinApp,
479
+ app: core.App,
480
+ settings_id: support.platform.WindowId,
481
+
482
+ /// Start the app with Windows-shaped hidden-titlebar chrome (the
483
+ /// DWM caption cluster overlays the trailing 138pt of the 320pt
484
+ /// secondary window's top band) and install both canvases.
485
+ fn create(padded: bool) !CaptionWinFixture {
486
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
487
+ errdefer harness.destroy(std.testing.allocator);
488
+ harness.null_platform.gpu_surfaces = true;
489
+ harness.null_platform.window_chrome = .{
490
+ .insets = .{ .top = 32, .right = 138 },
491
+ .buttons = geometry.RectF.init(182, 0, 138, 32),
492
+ };
493
+ const app_state = try CaptionWinApp.create(std.heap.page_allocator, .{
494
+ .name = "ui-app-caption-window",
495
+ .scene = panel_scene,
496
+ .canvas_label = canvas_label,
497
+ .update = captionWinUpdate,
498
+ .view = captionWinView,
499
+ .windows_fn = captionWinWindows,
500
+ .window_view = captionWinWindowView,
501
+ });
502
+ errdefer app_state.destroy();
503
+ app_state.model.padded = padded;
504
+ const app = app_state.app();
505
+ try harness.start(app);
506
+ // The model declares the settings window from the start, so the
507
+ // main canvas's installing rebuild creates it.
508
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
509
+ .label = canvas_label,
510
+ .size = geometry.SizeF.init(400, 300),
511
+ .scale_factor = 1,
512
+ .frame_index = 1,
513
+ .timestamp_ns = 1_000_000,
514
+ .nonblank = true,
515
+ } });
516
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
517
+ var settings_id: support.platform.WindowId = 0;
518
+ for (harness.runtime.listWindows(&buffer)) |info| {
519
+ if (std.mem.eql(u8, info.label, settings_window_label)) settings_id = info.id;
520
+ }
521
+ if (settings_id == 0) return error.TestUnexpectedResult;
522
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
523
+ .window_id = settings_id,
524
+ .label = settings_canvas_label,
525
+ .size = geometry.SizeF.init(320, 240),
526
+ .scale_factor = 1,
527
+ .frame_index = 1,
528
+ .timestamp_ns = 2_000_000,
529
+ .nonblank = true,
530
+ } });
531
+ return .{ .harness = harness, .app_state = app_state, .app = app, .settings_id = settings_id };
532
+ }
533
+
534
+ fn destroy(self: CaptionWinFixture) void {
535
+ self.app_state.destroy();
536
+ self.harness.destroy(std.testing.allocator);
537
+ }
538
+
539
+ fn textFrame(self: CaptionWinFixture, window_id: support.platform.WindowId, label: []const u8, text: []const u8) !geometry.RectF {
540
+ const layout = try self.harness.runtime.canvasWidgetLayout(window_id, label);
541
+ for (layout.nodes) |node| {
542
+ if (node.widget.kind == .text and std.mem.eql(u8, node.widget.text, text)) return node.frame;
543
+ }
544
+ return error.TestUnexpectedResult;
545
+ }
546
+ };
547
+
548
+ test "a secondary window's drag header content stays clear of the Windows caption cluster" {
549
+ // The naive shape: the window view never consumed the chrome
550
+ // channel, so its right-aligned status would lay out flush to the
551
+ // window edge — UNDER the min/max/close buttons. `rebuildWindowSlot`
552
+ // must run the same collision scan + one-retry clearance the main
553
+ // rebuild runs, with the cluster stamped into the SLOT's tokens.
554
+ const fixture = try CaptionWinFixture.create(false);
555
+ defer fixture.destroy();
556
+
557
+ // The trailing status text ends at (or before) the cluster's
558
+ // leading edge (182 = 320 - 138); without the reservation it ended
559
+ // at the window's right edge, inside the cluster.
560
+ const status = try fixture.textFrame(fixture.settings_id, settings_canvas_label, "recording");
561
+ try std.testing.expect(status.maxX() <= 182 + 0.01);
562
+ // The leading title and the body below the band are untouched.
563
+ const title = try fixture.textFrame(fixture.settings_id, settings_canvas_label, "Tools");
564
+ try std.testing.expectEqual(@as(f32, 0), title.x);
565
+
566
+ // The stamp lived in a LOCAL copy of the slot's tokens: the main
567
+ // canvas (no drag header, no collision) keeps an unstamped stored
568
+ // set and its own layout.
569
+ const main_stored = try fixture.harness.runtime.canvasWidgetDesignTokens(1, canvas_label);
570
+ try std.testing.expect(main_stored.window_controls == null);
571
+ const main_body = try fixture.textFrame(1, canvas_label, "main body");
572
+ try std.testing.expectEqual(@as(f32, 12), main_body.x);
573
+ }
574
+
575
+ test "a padded secondary drag header keeps its layout (no double reservation)" {
576
+ // The contract shape: the header's own trailing spacer already
577
+ // clears the cluster, so nothing collides and the slot retry must
578
+ // NOT fire — a double reservation would shove the status text a
579
+ // full cluster-width further left (near 182 - 138 = 44).
580
+ const fixture = try CaptionWinFixture.create(true);
581
+ defer fixture.destroy();
582
+
583
+ const status = try fixture.textFrame(fixture.settings_id, settings_canvas_label, "recording");
584
+ try std.testing.expect(@abs(status.maxX() - 182) < 0.5);
585
+ }
586
+
405
587
  // ------------------------------------------------- window-action effects
406
588
 
407
589
  const VerbModel = struct {