@overmvp/kumi 0.3.0 → 0.5.0

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.
Files changed (89) hide show
  1. package/animation/index.ts +4 -0
  2. package/animation/motion-policy.ts +20 -0
  3. package/animation/slide.ts +61 -0
  4. package/animation/spring.ts +75 -0
  5. package/animation/staggered-reveal.ts +34 -0
  6. package/builder/css-gradients.mjs +74 -0
  7. package/builder/css-mask-assets.mjs +52 -0
  8. package/builder/css-mask-assets.test.mjs +31 -0
  9. package/builder/css-v1.mjs +73 -22
  10. package/builder/css-v1.test.mjs +42 -2
  11. package/builder/module-declarations.mjs +2 -2
  12. package/builder/module-declarations.test.mjs +4 -2
  13. package/builder/src/compiler.rs +25 -0
  14. package/builder/src/lua_upvalues.rs +641 -0
  15. package/builder/src/main.rs +1 -0
  16. package/builder/style-modules.mjs +21 -3
  17. package/builder/style-modules.test.mjs +24 -0
  18. package/builder/style-states.mjs +6 -4
  19. package/package.json +1 -1
  20. package/router/index.ts +1 -0
  21. package/router/link.tsx +32 -0
  22. package/router/router-view.ts +5 -3
  23. package/scripts/addon-build.mjs +17 -2
  24. package/scripts/addon-project.mjs +22 -11
  25. package/scripts/kumi-cli.mjs +0 -0
  26. package/src/bindings.ts +10 -4
  27. package/src/context.ts +31 -7
  28. package/src/css-tokens.ts +11 -2
  29. package/src/css-v1.ts +3 -0
  30. package/src/elements.ts +20 -1
  31. package/src/flex-layout.ts +26 -5
  32. package/src/frame-scheduler.ts +3 -1
  33. package/src/index.ts +2 -2
  34. package/src/keyed-list.ts +9 -6
  35. package/src/module-ui.ts +22 -2
  36. package/src/module.ts +26 -7
  37. package/src/reactivity.ts +14 -1
  38. package/src/style-classes.ts +22 -1
  39. package/src/style-states.ts +13 -6
  40. package/state/setup-store.ts +3 -3
  41. package/templates/addon/.agents/skills/kumi-ui/SKILL.md +1 -1
  42. package/templates/addon/.claude/skills/kumi-ui/SKILL.md +1 -1
  43. package/templates/addon/AGENTS.md +1 -1
  44. package/templates/addon/kumi.config.ts +17 -17
  45. package/templates/addon/src/app/app.module.ts +3 -5
  46. package/templates/addon/src/app/layout/{AppWindow.module.scss → AppWindow.module.css} +10 -12
  47. package/templates/addon/src/modules/greeting/greeting.module.ts +2 -2
  48. package/templates/addon/src/modules/greeting/ui/{Greeting.module.scss → Greeting.module.css} +9 -11
  49. package/templates/addon/src/modules/playtime/playtime.module.ts +3 -5
  50. package/templates/addon/src/modules/playtime/ui/{Playtime.module.scss → Playtime.module.css} +3 -5
  51. package/templates/addon/src/shared/styles/tokens.css +13 -0
  52. package/templates/addon/src/shared/ui/{Controls.module.scss → Controls.module.css} +18 -20
  53. package/utils/format.ts +13 -0
  54. package/utils/index.ts +1 -1
  55. package/wow/action-button.ts +26 -1
  56. package/wow/addon-manifest.ts +2 -0
  57. package/wow/animation.ts +19 -18
  58. package/wow/app-ui.ts +150 -31
  59. package/wow/aura-container.ts +132 -38
  60. package/wow/aura-countdown.ts +19 -3
  61. package/wow/aura-follower.ts +32 -0
  62. package/wow/aura-view.ts +49 -0
  63. package/wow/css-background-image.ts +75 -0
  64. package/wow/css-box.ts +14 -9
  65. package/wow/css-image-mask.ts +32 -0
  66. package/wow/css-surfaces.ts +88 -35
  67. package/wow/element-events.ts +33 -13
  68. package/wow/frames.ts +2 -0
  69. package/wow/heal-prediction.ts +2 -4
  70. package/wow/hooks.ts +17 -0
  71. package/wow/hotkeys.ts +221 -0
  72. package/wow/index.ts +11 -6
  73. package/wow/initial-view.ts +99 -0
  74. package/wow/layout-view.ts +19 -10
  75. package/wow/module-replacements.ts +103 -0
  76. package/wow/native.ts +1 -0
  77. package/wow/pixel.ts +25 -0
  78. package/wow/pointer-click.ts +35 -0
  79. package/wow/protected-view.ts +49 -13
  80. package/wow/rounded-texture.ts +9 -4
  81. package/wow/scroll-view.ts +5 -4
  82. package/wow/state-paint.ts +5 -14
  83. package/wow/status-bar.ts +65 -0
  84. package/wow/texture-fill.ts +10 -0
  85. package/wow/unit-button.ts +25 -4
  86. package/wow/unit-cast.ts +6 -4
  87. package/wow/view-nodes.ts +11 -4
  88. package/wow/view.ts +387 -278
  89. package/templates/addon/src/shared/styles/_tokens.scss +0 -12
@@ -1,4 +1,4 @@
1
- import type { CssBorder, CssBorderPaintPlan, CssSide } from "../src/css-v1";
1
+ import type { CssBorder, CssBorderPaintPlan, CssSide, CssV1Style } from "../src/css-v1";
2
2
  import type { OwnedArray, OwnedReadonlyArray } from "../src/owned-collections";
3
3
  import { ref } from "../src/reactivity";
4
4
  import { fail, ok, type Result } from "../src/result";
@@ -6,7 +6,9 @@ import type { RuntimeScope } from "../src/runtime";
6
6
  import type { StatePaint } from "../src/style-states";
7
7
  import { resolveStyleValue, type StyleEnvironment } from "../src/style-variables";
8
8
  import type { RGBA } from "../utils/colors";
9
+ import { createBackgroundPainter } from "./css-background-image";
9
10
  import { readCssColor } from "./css-color";
11
+ import { mountImageMask } from "./css-image-mask";
10
12
  import type { Frame, MaskTexture, Texture } from "./frames";
11
13
  import { isSecret, reveal } from "./native";
12
14
  import { physicalPixelScale, snapPixel } from "./pixel";
@@ -38,12 +40,15 @@ export interface CssSurfaceOptions {
38
40
  readonly environment?: StyleEnvironment;
39
41
  readonly color?: boolean;
40
42
  readonly image?: boolean;
43
+ readonly maskImage?: CssV1Style["maskImage"];
41
44
  readonly borderState?: boolean;
42
45
  /** Fixed authored border. Its widths still follow the physical display scale. */
43
46
  readonly fixedBorder?: CssBorder;
44
47
  /** Immutable solid fill, without an image or a border to clip. */
45
48
  readonly fixedFillRadius?: number;
46
49
  readonly fixedBorderRadius?: number;
50
+ /** A live style can select sliced art whenever its current border is uniform. */
51
+ readonly liveOutline?: boolean;
47
52
  readonly borderPaint?: CssBorderPaintPlan;
48
53
  readonly pixelSize?: (this: void) => number;
49
54
  }
@@ -60,7 +65,7 @@ export interface CssSurface {
60
65
  write(this: void, side: CssSide, red: number, green: number, blue: number, alpha: number): void;
61
66
  };
62
67
  setBackgroundColor(this: void, r: number, g: number, b: number, a: number): void;
63
- setBackgroundImage(this: void, image?: BackgroundImage): void;
68
+ setBackgroundImage(this: void, image?: BackgroundImage): Result<void>;
64
69
  }
65
70
 
66
71
  export const borderSides = ["top", "right", "bottom", "left"] as const;
@@ -154,14 +159,17 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
154
159
  let previousRadius = 0;
155
160
  let artworkRadius: number | undefined;
156
161
  let slicedFill = false;
157
- const nativeOutline = combined && options.image !== true && options.pixelSize !== undefined && options.fixedBorderRadius !== undefined
158
- && uniformBorder.ok && sameInsets(uniformBorder.value.insets, onePixelInsets) ? options.rounded?.borderOutline : undefined;
162
+ const fixedOutline = options.fixedBorderRadius !== undefined && uniformBorder?.ok === true
163
+ && sameInsets(uniformBorder.value.insets, onePixelInsets);
164
+ const nativeOutline = artwork === undefined && options.image !== true && options.pixelSize !== undefined
165
+ && (fixedOutline || options.liveOutline === true) ? options.rounded?.borderOutline : undefined;
159
166
  let canSlice = nativeOutline !== undefined && canPlaceOutline() && typeof frame.GetSize === "function"
160
167
  && typeof frame.HasScript === "function" && typeof frame.HookScript === "function"
161
168
  && reveal(frame.HasScript("OnSizeChanged")) === true;
162
169
  let sliceUnavailable = false;
163
170
  let sliceActive = false;
164
171
  let sliceRadius: number | undefined, sliceScale: number | undefined;
172
+ let sliceFillRadius: number | undefined;
165
173
  let lastSizeWidth: number | undefined, lastSizeHeight: number | undefined;
166
174
  let minOutlineSize: number | undefined;
167
175
 
@@ -173,9 +181,12 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
173
181
 
174
182
  function borderSizeChanged(this: void, width: number, height: number) {
175
183
  const previous = outlineFits();
184
+ const shortSide = lastSizeWidth === undefined || lastSizeHeight === undefined ? undefined : Math.min(lastSizeWidth, lastSizeHeight);
176
185
  lastSizeWidth = reveal(width);
177
186
  lastSizeHeight = reveal(height);
178
- return !sliceUnavailable && minOutlineSize !== undefined && outlineFits() !== previous;
187
+ return !sliceUnavailable && minOutlineSize !== undefined && (outlineFits() !== previous
188
+ || nativeOutline?.resolveFill !== undefined && lastSizeWidth !== undefined && lastSizeHeight !== undefined
189
+ && Math.min(lastSizeWidth, lastSizeHeight) !== shortSide);
179
190
  }
180
191
 
181
192
  function canPlaceOutline() {
@@ -253,7 +264,12 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
253
264
  target.SetAllPoints(frame);
254
265
  hasMaskAnchor = true;
255
266
  }
256
- for (const layer of layers) if (layer !== outline) attach(layer, target);
267
+ for (const layer of layers) {
268
+ if (layer === outline) continue;
269
+ // A square background already fits the frame. Sampling a stretched mask softens its edges.
270
+ if (radius === 0 && (layer === color || layer === image)) detach(layer, target);
271
+ else attach(layer, target);
272
+ }
257
273
  maskRadius = radius;
258
274
  return ok(undefined);
259
275
  }
@@ -374,23 +390,61 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
374
390
  && typeof texture.SetSnapToPixelGrid === "function" && typeof texture.SetTexelSnappingBias === "function";
375
391
  }
376
392
 
393
+ function restoreFill() {
394
+ if (color === undefined || sliceFillRadius === undefined) return;
395
+ const texture = color.texture;
396
+ texture.SetIgnoreParentScale(false);
397
+ texture.SetScale(1);
398
+ texture.SetTextureSliceMargins(0, 0, 0, 0);
399
+ texture.SetColorTexture(1, 1, 1, 1);
400
+ if (colorR !== undefined && colorG !== undefined && colorB !== undefined && colorA !== undefined) {
401
+ texture.SetVertexColor(colorR, colorG, colorB, colorA);
402
+ }
403
+ sliceFillRadius = undefined;
404
+ }
405
+
406
+ function setPixelFill(deviceRadius: number, scale: number) {
407
+ if (color === undefined || nativeOutline === undefined) return;
408
+ const texture = color.texture;
409
+ if (sliceFillRadius !== deviceRadius) {
410
+ const asset = nativeOutline.resolveFill?.(deviceRadius);
411
+ if (asset === undefined || texture.SetTexture(asset) === false) return restoreFill();
412
+ texture.SetIgnoreParentScale(true);
413
+ texture.SetTexelSnappingBias(0);
414
+ texture.SetSnapToPixelGrid(true);
415
+ texture.SetTextureSliceMargins(deviceRadius, deviceRadius, deviceRadius, deviceRadius);
416
+ texture.SetTextureSliceMode(nativeOutline.stretched);
417
+ if (colorR !== undefined && colorG !== undefined && colorB !== undefined && colorA !== undefined) {
418
+ texture.SetVertexColor(colorR, colorG, colorB, colorA);
419
+ }
420
+ }
421
+ if (sliceFillRadius !== deviceRadius || sliceScale !== scale) texture.SetScale(scale);
422
+ sliceFillRadius = deviceRadius;
423
+ }
424
+
377
425
  function setPixelOutline(pixel: number, radius: number) {
378
426
  minOutlineSize = undefined;
379
427
  if (nativeOutline === undefined || sliceUnavailable || radius <= pixel || !canPlaceOutline()) return false;
380
428
  const scale = physicalPixelScale();
381
429
  if (scale === undefined) return false;
382
- const deviceRadius = Math.round(radius / pixel);
383
- const asset = nativeOutline.resolve(deviceRadius);
384
- if (asset === undefined) return false;
430
+ let deviceRadius = Math.round(radius / pixel);
385
431
  if (lastSizeWidth === undefined || lastSizeHeight === undefined) {
386
432
  const [width, height] = frame.GetSize();
387
433
  lastSizeWidth = reveal(width);
388
434
  lastSizeHeight = reveal(height);
389
435
  }
436
+ // A small surface can share a slightly smaller radius between fill and outline.
437
+ if (nativeOutline.resolveFill !== undefined && lastSizeWidth !== undefined && lastSizeHeight !== undefined
438
+ && lastSizeWidth > 0 && lastSizeHeight > 0) {
439
+ const shortSide = Math.min(lastSizeWidth, lastSizeHeight);
440
+ deviceRadius = Math.min(deviceRadius, Math.floor((shortSide / pixel - 1) / 2));
441
+ }
390
442
  // Both caps must fit even after the client rounds the vertices to display pixels.
391
- minOutlineSize = (2 * deviceRadius + 1) * pixel;
443
+ minOutlineSize = (2 * Math.max(2, deviceRadius) + 1) * pixel;
392
444
  const fit = outlineFits();
393
445
  if (fit !== "ready") return fit === "pending";
446
+ const asset = nativeOutline.resolve(deviceRadius);
447
+ if (asset === undefined) return false;
394
448
 
395
449
  if (outline === undefined) {
396
450
  outline = makeLayer("BORDER", -8, false, false);
@@ -412,6 +466,7 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
412
466
  texture.SetTextureSliceMode(nativeOutline.stretched);
413
467
  sliceRadius = deviceRadius;
414
468
  }
469
+ setPixelFill(deviceRadius, scale);
415
470
  if (sliceScale !== scale) {
416
471
  texture.SetScale(scale);
417
472
  sliceScale = scale;
@@ -462,13 +517,17 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
462
517
  const insets: Insets = pixel === undefined ? logical : [snap(logical[0]), snap(logical[1]), snap(logical[2]), snap(logical[3])];
463
518
  const configured = scope.protect(() => {
464
519
  const edge = colors.top;
465
- if (canSlice && pixel !== undefined && edge !== undefined) {
520
+ const uniform = edge !== undefined && (combined
521
+ || borderSides.every(side => colors[side]?.literal === edge.literal));
522
+ if (canSlice && pixel !== undefined && edge !== undefined && uniform && sameInsets(logical, onePixelInsets)) {
466
523
  const sliced = scope.protect(() => setPixelOutline(pixel, radius));
467
524
  if (!sliced.ok) sliceUnavailable = true;
468
525
  if (sliced.ok && sliced.value) {
469
- const masked = setMask(color === undefined ? undefined : radius);
526
+ const masked = setMask(color === undefined || sliceFillRadius !== undefined ? undefined : radius);
470
527
  if (!masked.ok) return masked;
471
- if (!sliceActive) for (const part of edges.top as OwnedArray<SurfaceTexture>) shown(part, false);
528
+ if (!sliceActive) {
529
+ for (const side of borderSides) for (const part of edges[side] as OwnedArray<SurfaceTexture>) shown(part, false);
530
+ }
472
531
  sliceActive = true;
473
532
  if (outline !== undefined) {
474
533
  const [r, g, b, a] = borderState?.drawn.top ?? edge.rgba;
@@ -481,11 +540,13 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
481
540
  }
482
541
  const switching = sliceActive;
483
542
  if (switching && outline !== undefined) shown(outline, false);
543
+ restoreFill();
484
544
  sliceActive = false;
545
+ const canOverflow = logical.some(width => width > 1);
546
+ const masked = setMask(radius > 0 ? radius : canOverflow ? 0 : undefined);
547
+ if (!masked.ok) return masked;
485
548
  if (switching || radius !== previousRadius || !sameInsets(previousInsets, insets)) {
486
- const masked = setMask(radius > 0 ? radius : insets.some(value => value > 0) ? 0 : undefined);
487
- if (!masked.ok) return masked;
488
- // Keep the side bands and outer mask. The corner texture provides only the inner cutout.
549
+ // A rounded corner texture provides only the inner cutout. The mask defines the outer edge.
489
550
  const onePixel = pixel !== undefined && pixel > 0 && radius > pixel && combined && options.borderPaint?.width === 1
490
551
  && logical[0] === 1 && logical[1] === 1 && logical[2] === 1 && logical[3] === 1;
491
552
  const art = onePixel ? options.rounded?.resolveBorderCorner?.(Math.round(radius / pixel)) : undefined;
@@ -525,7 +586,7 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
525
586
  drawn[2] = blue;
526
587
  drawn[3] = alpha;
527
588
  if (sliceActive || artwork !== undefined) {
528
- if (outline !== undefined) tint(outline, red, green, blue, alpha);
589
+ if (outline !== undefined && side === "top") tint(outline, red, green, blue, alpha);
529
590
  return;
530
591
  }
531
592
  const parts = edges[side] as OwnedArray<SurfaceTexture>;
@@ -544,24 +605,6 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
544
605
  colorA = a;
545
606
  }
546
607
 
547
- let path: string | undefined, atlas: string | undefined;
548
- function setBackgroundImage(next?: BackgroundImage) {
549
- if (!scope.isActive() || image === undefined) return;
550
- const nextPath = next !== undefined && "path" in next ? next.path : undefined;
551
- const nextAtlas = next !== undefined && "atlas" in next ? next.atlas : undefined;
552
- if (path === nextPath && atlas === nextAtlas) return;
553
- if (nextPath !== undefined) {
554
- const local = nextPath.startsWith("./") ? nextPath.slice(2) : nextPath;
555
- if (atlas !== undefined) image.texture.SetTexCoord(0, 1, 0, 1);
556
- shown(image, image.texture.SetTexture(local.split("/").join("\\")) !== false);
557
- } else if (nextAtlas !== undefined) {
558
- image.texture.SetAtlas(nextAtlas, false);
559
- shown(image, true);
560
- } else shown(image, false);
561
- path = nextPath;
562
- atlas = nextAtlas;
563
- }
564
-
565
608
  function release() {
566
609
  for (const layer of layers) {
567
610
  shown(layer, false);
@@ -595,6 +638,16 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
595
638
  scope.protect(release);
596
639
  return cleanup;
597
640
  }
641
+ const background = image;
642
+ const paintImage = background === undefined ? undefined
643
+ : createBackgroundPainter(background.texture, visible => shown(background, visible), options.environment);
644
+ if (background !== undefined && options.maskImage !== undefined) {
645
+ const applied = mountImageMask(scope, frame, background.texture, options.maskImage);
646
+ if (!applied.ok) return applied;
647
+ }
648
+ function setBackgroundImage(next?: BackgroundImage): Result<void> {
649
+ return scope.isActive() && paintImage !== undefined ? paintImage(next) : ok(undefined);
650
+ }
598
651
  const responsiveBorder = canSlice ? {
599
652
  resize: borderSizeChanged,
600
653
  useFallback: () => { canSlice = false; },
@@ -9,6 +9,7 @@ import type { ElementEventCoordinator } from "./element-event-coordinator";
9
9
  import type { Button, CommonScripts, Frame } from "./frames";
10
10
  import { hookButtonClick, hookScript } from "./native-hooks";
11
11
  import { api, isSecret, reveal } from "./native";
12
+ import { createPointerClickGate } from "./pointer-click";
12
13
  import { createProtectedLayoutUpdate } from "./protected-layout";
13
14
  import { protect } from "./protect";
14
15
  import { hitViewNode, type LogicalNode } from "./view-nodes";
@@ -27,7 +28,7 @@ export interface ElementStyleInput {
27
28
  set(this: void, state: StyleState, active: boolean): void;
28
29
  }
29
30
 
30
- type ElementInput = ElementEvents & { onShow?(this: void): void; onHide?(this: void): void };
31
+ type ElementInput = ElementEvents & { clickMaxDuration?: number; onShow?(this: void): void; onHide?(this: void): void };
31
32
  type HitRegion = Pick<Frame, "IsVisible" | "IsMouseOver" | "IsForbidden">;
32
33
  type HitCandidate = Partial<HitRegion> & { mouseHit?(this: object): boolean | undefined; value?: HitCandidate };
33
34
  type ScopeFactory = (this: void) => RuntimeScope;
@@ -104,6 +105,12 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
104
105
  if (!hasElementInput(input) && !hover && !activeStyle && disabledStyle === undefined) return;
105
106
  if (!canChange(frame)) return fail("Element input target is inaccessible or restricted");
106
107
  if (input.tooltip !== undefined && host?.tooltip === undefined) return fail("Element tooltip requires the host tooltip port");
108
+ const clickMaxDuration = input.clickMaxDuration;
109
+ if (clickMaxDuration !== undefined && (!Number.isFinite(clickMaxDuration) || clickMaxDuration <= 0)) {
110
+ return fail("Button clickMaxDuration must be a positive number of seconds");
111
+ }
112
+ const gate = button !== undefined && input.onClick !== undefined && clickMaxDuration !== undefined
113
+ ? createPointerClickGate(clickMaxDuration) : undefined;
107
114
  const outside = input.onClickOutside !== undefined || input.onDismiss !== undefined || input.ignoreOutside === true;
108
115
  const coordinator = host?.coordinator;
109
116
  if (outside && coordinator === undefined) return fail("Outside dismissal requires the shared element event coordinator");
@@ -121,14 +128,14 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
121
128
 
122
129
  const restorers = ownedArray<Cancel>();
123
130
  const motion = hover || activeStyle || input.onPointerEnter !== undefined || input.onPointerLeave !== undefined || input.tooltip !== undefined;
124
- const pressed = activeStyle || (button === undefined && input.onClick !== undefined) ? ownedSet<MouseButton>() : undefined;
131
+ const pressed = activeStyle || gate !== undefined || (button === undefined && input.onClick !== undefined) ? ownedSet<MouseButton>() : undefined;
125
132
  const observesVisibility = pressed !== undefined || outside || input.onShow !== undefined || input.onHide !== undefined
126
133
  || input.tooltip !== undefined || hover;
127
134
  let hoveredTip: Cancel | undefined;
128
135
  let visible = false;
129
136
  let active = true;
130
137
  let ownCleanup: RuntimeScope | undefined;
131
- let request: ((this: void) => Result<void>) | undefined;
138
+ let deferred: { readonly scope: RuntimeScope; readonly request: (this: void) => Result<void> } | undefined;
132
139
 
133
140
  function live() { return active && owner.isActive(); }
134
141
 
@@ -147,6 +154,7 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
147
154
  const visibleValue = typeof frame.IsVisible === "function" ? reveal(frame.IsVisible()) : reveal(frame.IsShown());
148
155
  const shown = visibleValue === true;
149
156
  if (!shown) {
157
+ gate?.clear();
150
158
  if (pressed !== undefined) pressed.clear();
151
159
  if (activeStyle) style?.set("active", false);
152
160
  if (hover) style?.set("hover", false);
@@ -185,7 +193,7 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
185
193
  restorers.length = 0;
186
194
  const temporary = ownCleanup;
187
195
  ownCleanup = undefined;
188
- request = undefined;
196
+ deferred = undefined;
189
197
  temporary?.dispose();
190
198
  return failure;
191
199
  }
@@ -215,7 +223,7 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
215
223
  handler => hookButtonClick(button, handler, owner), (_self, raw) => {
216
224
  if (!live() || isSecret(raw)) return;
217
225
  const event = readClickEvent(raw === undefined ? "LeftButton" : raw);
218
- if (event !== undefined) click(event);
226
+ if (event !== undefined && (gate === undefined || gate.accept(event.button))) click(event);
219
227
  }, hookOnly, restorers);
220
228
  if (failure !== undefined) return failure;
221
229
  }
@@ -224,16 +232,20 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
224
232
  if (!live() || reveal(frame.IsMouseOver()) !== true) return;
225
233
  const key = readMouseButton(raw);
226
234
  if (key === undefined) return;
235
+ gate?.press(key);
227
236
  pressed.add(key);
228
237
  if (activeStyle) style?.set("active", true);
229
238
  }) ?? listen("OnDragStart", () => {
230
239
  if (!live()) return;
240
+ gate?.drag();
231
241
  pressed.clear();
232
242
  if (activeStyle) style?.set("active", false);
233
243
  }) ?? listen("OnMouseUp", (_self, raw) => {
234
244
  if (!live()) return;
235
245
  const key = readMouseButton(raw);
236
- if (key === undefined || !pressed.delete(key)) return;
246
+ if (key === undefined) return;
247
+ gate?.release(key);
248
+ if (!pressed.delete(key)) return;
237
249
  const over = reveal(frame.IsMouseOver()) === true;
238
250
  if (activeStyle) style?.set("active", over && pressed.size > 0);
239
251
  if (button !== undefined || input.onClick === undefined || !over || reveal(frame.IsDragging?.()) === true) return;
@@ -292,13 +304,18 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
292
304
 
293
305
  function reconcile(): Result<void> {
294
306
  if (protectedFrame(frame)) {
295
- if (request === undefined) {
296
- const lifetime = coordinator?.scope ?? (ownCleanup = newScope());
307
+ if (deferred === undefined || !deferred.scope.isActive()) {
308
+ const hostScope = coordinator?.scope;
309
+ const lifetime = hostScope?.isActive() === true ? hostScope : (ownCleanup = newScope());
297
310
  const queue = createProtectedLayoutUpdate(update, { scope: lifetime, label: "Element input scripts" });
298
- if (!queue.ok) { ownCleanup?.dispose(); return queue; }
299
- request = queue.value.request;
311
+ if (!queue.ok) {
312
+ ownCleanup?.dispose();
313
+ ownCleanup = undefined;
314
+ return queue;
315
+ }
316
+ deferred = { scope: lifetime, request: queue.value.request };
300
317
  }
301
- return request();
318
+ return deferred.request();
302
319
  }
303
320
  const configured = protect(update);
304
321
  return configured.ok ? configured.value ?? ok(undefined) : configured;
@@ -308,8 +325,11 @@ export function bindElementEvents(this: void, frame: Frame, input: ElementInput,
308
325
  active = false;
309
326
  closeTip();
310
327
  lease?.cancel();
311
- if (restorers.length === 0 && request === undefined) return;
312
- report(request?.() ?? reconcile());
328
+ if (restorers.length === 0) {
329
+ report(restore());
330
+ return;
331
+ }
332
+ report(deferred?.scope.isActive() === true ? deferred.request() : reconcile());
313
333
  });
314
334
  if (!cleanup.ok) return cleanup;
315
335
  const result = reconcile();
package/wow/frames.ts CHANGED
@@ -18,6 +18,8 @@ export type CommonScripts = {
18
18
  OnMouseDown: [button: string];
19
19
  OnMouseUp: [button: string];
20
20
  OnMouseWheel: [delta: number];
21
+ OnKeyDown: [key: string];
22
+ OnKeyUp: [key: string];
21
23
  OnDragStart: [button: string];
22
24
  OnDragStop: [];
23
25
  };
@@ -3,6 +3,7 @@ import { fail, ok, type Result } from "../src/result";
3
3
  import { createFrame, type Frame, type FrameParent, type StatusBar, type Texture } from "./frames";
4
4
  import { api, isSecret, reveal, type SecretValue } from "./native";
5
5
  import { protectNative } from "./protect";
6
+ import { setTextureFill } from "./texture-fill";
6
7
 
7
8
  type Amount = number | SecretValue<number>;
8
9
  type Mask = Parameters<Texture["AddMaskTexture"]>[0];
@@ -63,10 +64,7 @@ function paint(overlay: Overlay, fallback: Color, style?: HealPredictionStyle):
63
64
  const assetChanged = color === undefined || overlay.asset !== asset;
64
65
  if (assetChanged) {
65
66
  const tiled = asset !== undefined;
66
- if (tiled) overlay.fill.SetTexture(asset, "REPEAT", "REPEAT");
67
- else overlay.fill.SetTexture(white);
68
- overlay.fill.SetHorizTile(tiled);
69
- overlay.fill.SetVertTile(tiled);
67
+ setTextureFill(overlay.fill, asset ?? white, tiled, tiled);
70
68
  overlay.asset = asset;
71
69
  }
72
70
  if (assetChanged || color === undefined || color[0] !== r || color[1] !== g || color[2] !== b || color[3] !== a) {
package/wow/hooks.ts CHANGED
@@ -4,8 +4,10 @@ import type { OwnedReadonlyArray } from "../src/owned-collections";
4
4
  import type { RuntimeScope } from "../src/runtime";
5
5
  import { appContext } from "./app-ui";
6
6
  import type { EventOptions, WoWEvent, WoWEventMap } from "./contract";
7
+ import type { Frame } from "./frames";
7
8
  import { setRawField } from "./lua-table";
8
9
  import { api, reveal } from "./native";
10
+ import { physicalPixel } from "./pixel";
9
11
 
10
12
  function owner(name: string): RuntimeScope {
11
13
  const scope = currentScope();
@@ -35,6 +37,21 @@ export function onCleanup(this: void, cleanup: (this: void) => void): void {
35
37
  if (!registered.ok) throw new Error(registered.reason);
36
38
  }
37
39
 
40
+ /** Follow UIParent size and the size of one display pixel in its coordinate space. */
41
+ export function useScreenSize(this: void) {
42
+ const screen = api.UIParent as Frame;
43
+ function read(this: void) {
44
+ const [width, height] = screen.GetSize();
45
+ return { width: reveal(width), height: reveal(height), pixel: physicalPixel(screen) };
46
+ }
47
+ const size = ref(read());
48
+ function refresh(this: void): void { size.value = read(); }
49
+ useEvent("UI_SCALE_CHANGED", refresh);
50
+ useEvent("DISPLAY_SIZE_CHANGED", refresh);
51
+ useEvent("PLAYER_ENTERING_WORLD", refresh);
52
+ return { size, refresh };
53
+ }
54
+
38
55
  interface Clock {
39
56
  readonly time: SignalRef<number>;
40
57
  users: number;
package/wow/hotkeys.ts ADDED
@@ -0,0 +1,221 @@
1
+ import { ownedArray, ownedSet, type OwnedRecord } from "../src/owned-collections";
2
+ import { currentScope } from "../src/reactive-scope";
3
+ import type { RuntimeScope } from "../src/runtime";
4
+ import { canAccessRegion } from "./frame-access";
5
+ import { frameStrata, type FrameStrata } from "./frame-layer";
6
+ import { createFrame, type Frame } from "./frames";
7
+ import { luaString } from "./lua-string";
8
+ import { rawField, setRawField } from "./lua-table";
9
+ import { hookScript } from "./native-hooks";
10
+ import { api, isSecret, reveal } from "./native";
11
+ import { createProtectedLayoutUpdate } from "./protected-layout";
12
+
13
+ type Action = (this: void) => void;
14
+ type FrameHotkeyRef = (this: void, frame?: Frame) => void;
15
+ export type HotkeyActions = Readonly<Record<string, Action>>;
16
+ type HotkeyLookup = OwnedRecord<Record<string, Action | undefined>>;
17
+
18
+ interface HotkeyCondition {
19
+ /** Decide whether this scope accepts input. Read current state here. */
20
+ enabled?(this: void): boolean;
21
+ }
22
+
23
+ export interface FrameHotkeyOptions extends HotkeyCondition {
24
+ readonly scope: "frame";
25
+ }
26
+
27
+ export interface GlobalHotkeyOptions extends HotkeyCondition {
28
+ readonly scope: "global";
29
+ /** Addon-unique Lua function name that Bindings.xml calls with a command ID. */
30
+ readonly dispatcher: string;
31
+ }
32
+
33
+ interface FrameHotkeys {
34
+ readonly actions: HotkeyLookup;
35
+ readonly options: FrameHotkeyOptions;
36
+ target?: Frame;
37
+ }
38
+
39
+ const frames = ownedArray<FrameHotkeys>();
40
+ const held = ownedSet<string>();
41
+ let listener: Frame | undefined;
42
+
43
+ function chord(key: string, alt: boolean, ctrl: boolean, shift: boolean, meta: boolean): string {
44
+ return (alt ? "ALT-" : "") + (ctrl ? "CTRL-" : "") + (shift ? "SHIFT-" : "") + (meta ? "META-" : "") + key;
45
+ }
46
+
47
+ function normalizeChord(input: string): string | undefined {
48
+ const parts = input.toUpperCase().split("-");
49
+ const key = parts.pop();
50
+ if (key === undefined || luaString.match(key, "^[A-Z0-9_]+$") === undefined) return;
51
+ const modifiers = ownedSet<string>();
52
+ for (const part of parts) {
53
+ if (part !== "ALT" && part !== "CTRL" && part !== "SHIFT" && part !== "META" || modifiers.has(part)) return;
54
+ modifiers.add(part);
55
+ }
56
+ return chord(key, modifiers.has("ALT"), modifiers.has("CTRL"), modifiers.has("SHIFT"), modifiers.has("META"));
57
+ }
58
+
59
+ function collect(actions: HotkeyActions, local: boolean) {
60
+ const lookup = {} as HotkeyLookup;
61
+ for (const [name, action] of Object.entries(actions as OwnedRecord<HotkeyActions>)) {
62
+ const key = local ? normalizeChord(name) : luaString.match(name, "^[A-Za-z0-9_]+$");
63
+ if (key === undefined || lookup[key] !== undefined || typeof action !== "function") throw new Error("Invalid or repeated hotkey: " + name);
64
+ lookup[key] = action;
65
+ }
66
+ return lookup;
67
+ }
68
+
69
+ function visible(target: Frame | undefined): target is Frame {
70
+ return canAccessRegion(target) && reveal(target.IsVisible()) === true;
71
+ }
72
+
73
+ function foreground(): FrameHotkeys | undefined {
74
+ let highest: FrameHotkeys | undefined;
75
+ let highestStrata = -1;
76
+ let highestLevel = -1;
77
+ for (const entry of frames) {
78
+ const target = entry.target;
79
+ if (!visible(target) || entry.options.enabled?.() === false) continue;
80
+ const strata = frameStrata.indexOf(reveal(target.GetFrameStrata()) as FrameStrata);
81
+ const level = reveal(target.GetFrameLevel());
82
+ if (strata < 0 || level === undefined || strata < highestStrata || strata === highestStrata && level < highestLevel) continue;
83
+ highest = entry;
84
+ highestStrata = strata;
85
+ highestLevel = level;
86
+ }
87
+ return highest;
88
+ }
89
+
90
+ function refreshListener() {
91
+ const shown = frames.some(entry => visible(entry.target));
92
+ if (!shown) clearHeld();
93
+ listener?.SetShown(shown);
94
+ }
95
+
96
+ function inputAvailable() {
97
+ if (reveal(api.InCombatLockdown()) !== false) return false;
98
+ const focus = rawField<(this: void) => unknown>(api, "GetCurrentKeyBoardFocus");
99
+ if (focus === undefined) return false;
100
+ const target = focus();
101
+ return !isSecret(target) && target === undefined;
102
+ }
103
+
104
+ function clearHeld() {
105
+ held.clear();
106
+ listener?.SetScript("OnUpdate");
107
+ }
108
+
109
+ function releaseKey(this: void, _: Frame, input: string) {
110
+ const key = reveal(input);
111
+ if (key !== undefined) held.delete(key);
112
+ if (held.size === 0) listener?.SetScript("OnUpdate");
113
+ }
114
+
115
+ function refreshHeld(this: void) {
116
+ // The edit box can consume KeyUp after a shortcut moves focus into it.
117
+ for (const key of held) if (reveal(api.IsKeyDown(key, true)) === false) held.delete(key);
118
+ if (held.size === 0) listener?.SetScript("OnUpdate");
119
+ }
120
+
121
+ function onKeyDown(this: void, frame: Frame, input: string) {
122
+ const key = reveal(input);
123
+ if (typeof key !== "string" || !inputAvailable() || held.has(key)) return;
124
+ const alt = reveal(api.IsAltKeyDown()), ctrl = reveal(api.IsControlKeyDown());
125
+ const shift = reveal(api.IsShiftKeyDown()), meta = reveal(api.IsMetaKeyDown());
126
+ if (typeof alt !== "boolean" || typeof ctrl !== "boolean" || typeof shift !== "boolean" || typeof meta !== "boolean") return;
127
+ const action = foreground()?.actions[chord(key, alt, ctrl, shift, meta)];
128
+ if (action === undefined) return;
129
+ held.add(key);
130
+ frame.SetScript("OnUpdate", refreshHeld);
131
+ action();
132
+ }
133
+
134
+ function prepareListener() {
135
+ if (listener === undefined && frames.some(entry => visible(entry.target))) {
136
+ const frame = createFrame("Frame", undefined, api.UIParent);
137
+ frame.Hide();
138
+ frame.EnableMouse(false);
139
+ frame.EnableKeyboard(true);
140
+ frame.SetPropagateKeyboardInput(true);
141
+ if (reveal(frame.GetPropagateKeyboardInput()) !== true) {
142
+ frame.EnableKeyboard(false);
143
+ throw new Error("Could not set keyboard propagation");
144
+ }
145
+ frame.SetScript("OnKeyDown", onKeyDown);
146
+ frame.SetScript("OnKeyUp", releaseKey);
147
+ listener = frame;
148
+ }
149
+ refreshListener();
150
+ }
151
+
152
+ function requireSuccess(result: { ok: true } | { ok: false; reason: string }) {
153
+ if (!result.ok) throw new Error(result.reason);
154
+ }
155
+
156
+ function scopedHotkeys(scope: RuntimeScope, actions: FrameHotkeys["actions"], options: FrameHotkeyOptions): FrameHotkeyRef {
157
+ const entry: FrameHotkeys = { actions, options };
158
+ const update = createProtectedLayoutUpdate(prepareListener, { scope, label: "Hotkey listener" });
159
+ if (!update.ok) throw new Error(update.reason);
160
+ const { request } = update.value;
161
+ let attachment: RuntimeScope | undefined;
162
+ function refresh() {
163
+ if (listener === undefined) requireSuccess(request());
164
+ else refreshListener();
165
+ }
166
+ requireSuccess(scope.onDispose(() => {
167
+ const index = frames.indexOf(entry);
168
+ if (index >= 0) frames.splice(index, 1);
169
+ entry.target = undefined;
170
+ refreshListener();
171
+ }));
172
+ frames.push(entry);
173
+ return frame => {
174
+ if (!scope.isActive() || frame === entry.target) return;
175
+ if (attachment !== undefined) requireSuccess(attachment.dispose());
176
+ attachment = undefined;
177
+ const target = frame !== undefined && canAccessRegion(frame) ? frame : undefined;
178
+ entry.target = undefined;
179
+ if (target !== undefined) {
180
+ const own = scope.fork();
181
+ const shown = hookScript(target, "OnShow", refresh, own);
182
+ const hidden = shown.ok ? hookScript(target, "OnHide", refresh, own) : shown;
183
+ if (!hidden.ok) {
184
+ own.dispose();
185
+ refresh();
186
+ throw new Error(hidden.reason);
187
+ }
188
+ attachment = own;
189
+ entry.target = target;
190
+ }
191
+ refresh();
192
+ };
193
+ }
194
+
195
+ function globalHotkeys(scope: RuntimeScope, actions: FrameHotkeys["actions"], options: GlobalHotkeyOptions): void {
196
+ const name = options.dispatcher;
197
+ if (luaString.match(name, "^[A-Za-z_][A-Za-z0-9_]*$") === undefined) throw new Error("Invalid hotkey dispatcher name");
198
+ const existing = rawField(api, name);
199
+ if (isSecret(existing) || existing !== undefined) throw new Error("Hotkey dispatcher name is already in use: " + name);
200
+
201
+ function dispatch(this: void, id: unknown) {
202
+ if (isSecret(id) || typeof id !== "string" || !scope.isActive() || options.enabled?.() === false) return;
203
+ const action = actions[id];
204
+ action?.();
205
+ }
206
+ requireSuccess(scope.onDispose(() => {
207
+ if (reveal(rawField(api, name)) === dispatch) setRawField(api, name, undefined);
208
+ }));
209
+ setRawField(api, name, dispatch);
210
+ }
211
+
212
+ /** Observe chords on the foremost bound frame, or dispatch player-assigned WoW bindings. */
213
+ export function useHotkeys(this: void, actions: HotkeyActions, options: FrameHotkeyOptions): FrameHotkeyRef;
214
+ export function useHotkeys(this: void, actions: HotkeyActions, options: GlobalHotkeyOptions): void;
215
+ export function useHotkeys(this: void, actions: HotkeyActions, options: FrameHotkeyOptions | GlobalHotkeyOptions): FrameHotkeyRef | void {
216
+ const scope = currentScope();
217
+ if (scope === undefined || !scope.isActive()) throw new Error("useHotkeys requires an active scope");
218
+ const lookup = collect(actions, options.scope === "frame");
219
+ if (options.scope === "frame") return scopedHotkeys(scope, lookup, options);
220
+ globalHotkeys(scope, lookup, options);
221
+ }