@overmvp/kumi 0.1.0 → 0.3.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 (175) hide show
  1. package/README.md +59 -6
  2. package/builder/css-v1.mjs +54 -1
  3. package/builder/css-v1.test.mjs +40 -0
  4. package/builder/link-addon-package.mjs +47 -6
  5. package/builder/module-declarations.test.mjs +9 -18
  6. package/builder/native-kernels.mjs +1 -1
  7. package/builder/src/compiler.rs +180 -8
  8. package/builder/src/main.rs +19 -5
  9. package/builder/src/native_ir.rs +1 -1
  10. package/builder/src/native_signal.rs +46 -10
  11. package/builder/src/numbers.lua +14 -0
  12. package/builder/src/project-front.mjs +385 -34
  13. package/builder/src/routes.rs +11 -14
  14. package/builder/src/setup-store.lua +18 -0
  15. package/builder/src/signal-kernel.fingerprint +1 -1
  16. package/builder/src/ts-lower.mjs +2 -2
  17. package/builder/src/wow-reads.lua +22 -0
  18. package/builder/style-modules.mjs +93 -0
  19. package/config.ts +56 -14
  20. package/eslint.mjs +74 -0
  21. package/form/index.ts +15 -11
  22. package/package.json +11 -7
  23. package/router/index.ts +9 -2
  24. package/router/navigation.ts +22 -0
  25. package/router/router-view.ts +134 -0
  26. package/scripts/addon-build.mjs +364 -0
  27. package/scripts/addon-project.mjs +78 -0
  28. package/scripts/kumi-cli.mjs +52 -17
  29. package/scripts/link-addons.mjs +12 -2
  30. package/src/bindings.ts +2 -2
  31. package/src/css-tokens.ts +16 -11
  32. package/src/css-v1.ts +10 -0
  33. package/src/elements.ts +26 -0
  34. package/src/frame-scheduler.ts +3 -1
  35. package/src/index.ts +1 -0
  36. package/src/jsx-runtime.ts +8 -1
  37. package/src/keyed-list.ts +8 -5
  38. package/src/module-ui.ts +147 -0
  39. package/src/module.ts +42 -8
  40. package/src/owned-collections.ts +8 -0
  41. package/src/providers.ts +7 -1
  42. package/src/reactive-scope.ts +3 -3
  43. package/src/reactivity.ts +28 -7
  44. package/src/result.ts +6 -0
  45. package/src/signal-kernel.ts +35 -2
  46. package/state/index.ts +6 -0
  47. package/state/setup-store-persistence.ts +206 -0
  48. package/state/setup-store-view.ts +10 -0
  49. package/state/setup-store.ts +342 -0
  50. package/state/store.ts +2 -0
  51. package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
  52. package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
  53. package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
  54. package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
  55. package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
  56. package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
  57. package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
  58. package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
  59. package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
  60. package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
  61. package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
  62. package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
  63. package/templates/addon/AGENTS.md +34 -0
  64. package/templates/addon/Assets/Art/Corner10.tga +0 -0
  65. package/templates/addon/Assets/Art/Corner11.tga +0 -0
  66. package/templates/addon/Assets/Art/Corner12.tga +0 -0
  67. package/templates/addon/Assets/Art/Corner13.tga +0 -0
  68. package/templates/addon/Assets/Art/Corner14.tga +0 -0
  69. package/templates/addon/Assets/Art/Corner15.tga +0 -0
  70. package/templates/addon/Assets/Art/Corner16.tga +0 -0
  71. package/templates/addon/Assets/Art/Corner2.tga +0 -0
  72. package/templates/addon/Assets/Art/Corner3.tga +0 -0
  73. package/templates/addon/Assets/Art/Corner4.tga +0 -0
  74. package/templates/addon/Assets/Art/Corner5.tga +0 -0
  75. package/templates/addon/Assets/Art/Corner6.tga +0 -0
  76. package/templates/addon/Assets/Art/Corner7.tga +0 -0
  77. package/templates/addon/Assets/Art/Corner8.tga +0 -0
  78. package/templates/addon/Assets/Art/Corner9.tga +0 -0
  79. package/templates/addon/Assets/Art/Dot.tga +0 -0
  80. package/templates/addon/Assets/Art/Gear.tga +0 -0
  81. package/templates/addon/Assets/Art/Grip.tga +0 -0
  82. package/templates/addon/Assets/Art/Outline10.tga +0 -0
  83. package/templates/addon/Assets/Art/Outline12.tga +0 -0
  84. package/templates/addon/Assets/Art/Outline13.tga +0 -0
  85. package/templates/addon/Assets/Art/Outline14.tga +0 -0
  86. package/templates/addon/Assets/Art/Outline15.tga +0 -0
  87. package/templates/addon/Assets/Art/Outline2.tga +0 -0
  88. package/templates/addon/Assets/Art/Outline3.tga +0 -0
  89. package/templates/addon/Assets/Art/Outline5.tga +0 -0
  90. package/templates/addon/Assets/Art/Outline7.tga +0 -0
  91. package/templates/addon/Assets/Art/Outline9.tga +0 -0
  92. package/templates/addon/Assets/Art/Round11.tga +0 -0
  93. package/templates/addon/Assets/Art/Round11Border.tga +0 -0
  94. package/templates/addon/Assets/Art/Round4.tga +0 -0
  95. package/templates/addon/Assets/Art/Round4Border.tga +0 -0
  96. package/templates/addon/Assets/Art/Round6.tga +0 -0
  97. package/templates/addon/Assets/Art/Round6Border.tga +0 -0
  98. package/templates/addon/Assets/Art/Round8.tga +0 -0
  99. package/templates/addon/Assets/Art/Round8Border.tga +0 -0
  100. package/templates/addon/Assets/Art/Search.tga +0 -0
  101. package/templates/addon/CLAUDE.md +1 -0
  102. package/templates/addon/README.md +42 -7
  103. package/templates/addon/eslint.config.mjs +3 -9
  104. package/templates/addon/gitignore +1 -0
  105. package/templates/addon/kumi.config.ts +17 -0
  106. package/templates/addon/package.json +4 -5
  107. package/templates/addon/src/app/app.module.ts +18 -0
  108. package/templates/addon/src/app/commands.store.ts +11 -0
  109. package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
  110. package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
  111. package/templates/addon/src/app/main.ts +5 -0
  112. package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
  113. package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
  114. package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
  115. package/templates/addon/src/app/theme.ts +6 -0
  116. package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
  117. package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
  118. package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
  119. package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
  120. package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
  121. package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
  122. package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
  123. package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
  124. package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
  125. package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
  126. package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
  127. package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
  128. package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
  129. package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
  130. package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
  131. package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
  132. package/templates/addon/src/shared/api/native-art.ts +28 -0
  133. package/templates/addon/src/shared/assets/close.png +0 -0
  134. package/templates/addon/src/shared/lib/format.ts +20 -0
  135. package/templates/addon/src/shared/styles/_tokens.scss +12 -0
  136. package/templates/addon/src/shared/ui/Button.tsx +29 -0
  137. package/templates/addon/src/shared/ui/Choice.tsx +34 -0
  138. package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
  139. package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
  140. package/templates/addon/src/shared/ui/Slider.tsx +25 -0
  141. package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
  142. package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
  143. package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
  144. package/templates/addon/tsconfig.json +49 -10
  145. package/wow/action-bar-layout.ts +108 -0
  146. package/wow/action-button.ts +219 -0
  147. package/wow/action-catalog.ts +98 -0
  148. package/wow/addon-manifest.ts +36 -0
  149. package/wow/app-ui.ts +221 -0
  150. package/wow/cooldown-timing.ts +24 -0
  151. package/wow/css-box.ts +40 -6
  152. package/wow/css-surfaces.ts +229 -24
  153. package/wow/cursor.ts +45 -0
  154. package/wow/frame-access.ts +23 -1
  155. package/wow/frame-timing.ts +13 -0
  156. package/wow/hooks.ts +133 -0
  157. package/wow/index.ts +18 -4
  158. package/wow/layout-view.ts +26 -14
  159. package/wow/locale-font.ts +15 -0
  160. package/wow/native.ts +18 -0
  161. package/wow/panel-host.ts +97 -0
  162. package/wow/pixel.ts +35 -0
  163. package/wow/plugins.ts +246 -0
  164. package/wow/protect.ts +27 -3
  165. package/wow/protected-view.ts +103 -0
  166. package/wow/range-art.ts +7 -0
  167. package/wow/readable-api.ts +120 -0
  168. package/wow/rounded-texture.ts +33 -6
  169. package/wow/saved-variables.ts +67 -18
  170. package/wow/spell-cooldown.ts +36 -0
  171. package/wow/start-addon.ts +31 -8
  172. package/wow/view.ts +201 -83
  173. package/templates/addon/__ADDON_NAME__.toc +0 -4
  174. package/templates/addon/scripts/build.mjs +0 -25
  175. package/templates/addon/src/app/main.tsx +0 -13
@@ -1,5 +1,5 @@
1
- import type { CssBorder, CssSide } from "../src/css-v1";
2
- import type { OwnedArray } from "../src/owned-collections";
1
+ import type { CssBorder, CssBorderPaintPlan, CssSide } from "../src/css-v1";
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";
5
5
  import type { RuntimeScope } from "../src/runtime";
@@ -9,7 +9,8 @@ import type { RGBA } from "../utils/colors";
9
9
  import { readCssColor } from "./css-color";
10
10
  import type { Frame, MaskTexture, Texture } from "./frames";
11
11
  import { isSecret, reveal } from "./native";
12
- import type { RoundedTextures } from "./rounded-texture";
12
+ import { physicalPixelScale, snapPixel } from "./pixel";
13
+ import type { RoundedBorderCorner, RoundedTextures } from "./rounded-texture";
13
14
 
14
15
  type Insets = readonly [top: number, right: number, bottom: number, left: number];
15
16
  type BackgroundImage = StatePaint["backgroundImage"];
@@ -19,6 +20,7 @@ interface SurfaceTexture {
19
20
  texture: Texture;
20
21
  shown: boolean;
21
22
  masked: boolean;
23
+ cutout?: RoundedBorderCorner["asset"];
22
24
  red?: number;
23
25
  green?: number;
24
26
  blue?: number;
@@ -31,15 +33,26 @@ interface NativeBorder {
31
33
  }
32
34
 
33
35
  export interface CssSurfaceOptions {
34
- readonly rounded?: Pick<RoundedTextures, "applyMask"> & Partial<Pick<RoundedTextures, "apply">>;
36
+ readonly rounded?: Pick<RoundedTextures, "applyMask"> & Partial<Pick<RoundedTextures, "apply" | "applyExactFill" | "resolveBorderCorner" | "borderOutline">>;
35
37
  readonly nativeRoundedArt?: boolean;
36
38
  readonly environment?: StyleEnvironment;
37
39
  readonly color?: boolean;
38
40
  readonly image?: boolean;
39
41
  readonly borderState?: boolean;
42
+ /** Fixed authored border. Its widths still follow the physical display scale. */
43
+ readonly fixedBorder?: CssBorder;
44
+ /** Immutable solid fill, without an image or a border to clip. */
45
+ readonly fixedFillRadius?: number;
46
+ readonly fixedBorderRadius?: number;
47
+ readonly borderPaint?: CssBorderPaintPlan;
48
+ readonly pixelSize?: (this: void) => number;
40
49
  }
41
50
 
42
51
  export interface CssSurface {
52
+ readonly responsiveBorder?: {
53
+ resize(this: void, width: number, height: number): boolean;
54
+ useFallback(this: void): void;
55
+ };
43
56
  setBorder(this: void, border?: CssBorder, radius?: number): Result<void>;
44
57
  readonly borderPaint?: {
45
58
  readonly sides?: readonly CssSide[];
@@ -51,7 +64,9 @@ export interface CssSurface {
51
64
  }
52
65
 
53
66
  export const borderSides = ["top", "right", "bottom", "left"] as const;
67
+ const singlePaintSide: readonly CssSide[] = ["top"];
54
68
  const zeroInsets: Insets = [0, 0, 0, 0];
69
+ const onePixelInsets: Insets = [1, 1, 1, 1];
55
70
  const white = "Interface\\Buttons\\WHITE8X8";
56
71
  const maskWrap = "CLAMPTOBLACKADDITIVE";
57
72
 
@@ -64,8 +79,27 @@ export function borderInsets(this: void, border?: CssBorder): Insets {
64
79
  return [width(border.top?.width), width(border.right?.width), width(border.bottom?.width), width(border.left?.width)];
65
80
  }
66
81
 
82
+ function readUniformBorder(border: CssBorder | undefined, plan: CssBorderPaintPlan | undefined): Result<NativeBorder> | undefined {
83
+ if (border === undefined) return;
84
+ const literal = reveal(border.top?.color);
85
+ if (typeof literal !== "string") return;
86
+ for (const side of borderSides) {
87
+ const edge = border[side];
88
+ if (edge === undefined || reveal(edge.color) !== literal) return;
89
+ }
90
+ const insets = borderInsets(border);
91
+ if (insets[0] === 0 || insets[1] === 0 || insets[2] === 0 || insets[3] === 0) return;
92
+ // The final style can override the compiled border. Reuse its decoded color only when it still matches.
93
+ const matches = plan !== undefined && plan.literal === literal && insets[0] === plan.width
94
+ && insets[1] === plan.width && insets[2] === plan.width && insets[3] === plan.width;
95
+ const rgba = matches ? plan.rgba : readCssColor(literal);
96
+ if (rgba === undefined) return;
97
+ return ok({ insets, colors: { top: { literal, rgba } } });
98
+ }
99
+
67
100
  function readBorder(border: CssBorder | undefined, environment: StyleEnvironment | undefined): Result<NativeBorder> {
68
101
  const colors: NativeBorder["colors"] = {};
102
+ let previous: NativeBorder["colors"][CssSide];
69
103
  for (const side of borderSides) {
70
104
  const edge = border?.[side];
71
105
  if (edge === undefined) continue;
@@ -77,9 +111,14 @@ function readBorder(border: CssBorder | undefined, environment: StyleEnvironment
77
111
  const resolved = resolveStyleValue(color, environment);
78
112
  if (!resolved.ok) return resolved;
79
113
  const literal = reveal(resolved.value);
114
+ if (previous !== undefined && previous.literal === literal) {
115
+ colors[side] = previous;
116
+ continue;
117
+ }
80
118
  const rgba = typeof literal === "string" ? readCssColor(literal) : undefined;
81
119
  if (literal === undefined || rgba === undefined) return fail(`Invalid ${side} border color`);
82
- colors[side] = { literal, rgba };
120
+ previous = { literal, rgba };
121
+ colors[side] = previous;
83
122
  }
84
123
  return ok({ insets: borderInsets(border), colors });
85
124
  }
@@ -95,6 +134,10 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
95
134
  if (options.nativeRoundedArt === true && (artwork === undefined || options.color !== true || options.image === true)) {
96
135
  return fail("Native rounded art requires a host texture port and a CSS color fill");
97
136
  }
137
+ const uniformBorder = artwork === undefined ? readUniformBorder(options.fixedBorder, options.borderPaint) : undefined;
138
+ const combined = uniformBorder !== undefined;
139
+ const paintSides = combined || artwork !== undefined ? singlePaintSide : undefined;
140
+ const painted = (paintSides ?? borderSides) as OwnedReadonlyArray<CssSide>;
98
141
  const layers: SurfaceTexture[] = [];
99
142
  const edges: Record<CssSide, SurfaceTexture[]> = { top: [], right: [], bottom: [], left: [] };
100
143
  const used: Record<CssSide, number> = { top: 0, right: 0, bottom: 0, left: 0 };
@@ -110,6 +153,37 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
110
153
  let previousInsets = zeroInsets;
111
154
  let previousRadius = 0;
112
155
  let artworkRadius: number | undefined;
156
+ 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;
159
+ let canSlice = nativeOutline !== undefined && canPlaceOutline() && typeof frame.GetSize === "function"
160
+ && typeof frame.HasScript === "function" && typeof frame.HookScript === "function"
161
+ && reveal(frame.HasScript("OnSizeChanged")) === true;
162
+ let sliceUnavailable = false;
163
+ let sliceActive = false;
164
+ let sliceRadius: number | undefined, sliceScale: number | undefined;
165
+ let lastSizeWidth: number | undefined, lastSizeHeight: number | undefined;
166
+ let minOutlineSize: number | undefined;
167
+
168
+ function outlineFits() {
169
+ if (minOutlineSize === undefined || lastSizeWidth === undefined || lastSizeHeight === undefined) return "fallback";
170
+ if (lastSizeWidth <= 0 || lastSizeHeight <= 0) return "pending";
171
+ return lastSizeWidth >= minOutlineSize && lastSizeHeight >= minOutlineSize ? "ready" : "fallback";
172
+ }
173
+
174
+ function borderSizeChanged(this: void, width: number, height: number) {
175
+ const previous = outlineFits();
176
+ lastSizeWidth = reveal(width);
177
+ lastSizeHeight = reveal(height);
178
+ return !sliceUnavailable && minOutlineSize !== undefined && outlineFits() !== previous;
179
+ }
180
+
181
+ function canPlaceOutline() {
182
+ if (typeof frame.IsProtected !== "function" || typeof frame.IsForbidden !== "function"
183
+ || reveal(frame.IsForbidden()) !== false) return false;
184
+ const [protectedState, explicit] = frame.IsProtected();
185
+ return reveal(protectedState) === false && reveal(explicit) === false;
186
+ }
113
187
 
114
188
  function shown(layer: SurfaceTexture, visible: boolean) {
115
189
  if (layer.shown === visible) return;
@@ -140,13 +214,13 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
140
214
  }
141
215
  }
142
216
 
143
- function makeLayer(layer: "BACKGROUND" | "BORDER", level: number, solid: boolean) {
217
+ function makeLayer(layer: "BACKGROUND" | "BORDER", level: number, solid: boolean, maskable = true) {
144
218
  const texture = frame.CreateTexture(undefined, layer, undefined, level);
145
219
  const entry: SurfaceTexture = { texture, shown: true, masked: false };
146
220
  layers.push(entry);
147
221
  shown(entry, false);
148
222
  if (solid) texture.SetColorTexture(1, 1, 1, 1);
149
- if (mask !== undefined && maskRadius !== undefined) attach(entry, mask);
223
+ if (maskable && mask !== undefined && maskRadius !== undefined) attach(entry, mask);
150
224
  return entry;
151
225
  }
152
226
 
@@ -179,26 +253,36 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
179
253
  target.SetAllPoints(frame);
180
254
  hasMaskAnchor = true;
181
255
  }
182
- for (const layer of layers) attach(layer, target);
256
+ for (const layer of layers) if (layer !== outline) attach(layer, target);
183
257
  maskRadius = radius;
184
258
  return ok(undefined);
185
259
  }
186
260
 
187
- function nextPart(side: CssSide): SurfaceTexture {
261
+ function nextPart(side: CssSide, solid = true): SurfaceTexture {
188
262
  const index = used[side]++;
189
263
  const parts = edges[side] as OwnedArray<SurfaceTexture>;
190
264
  let part = parts[index];
191
265
  if (part === undefined) {
192
- part = makeLayer("BORDER", -8, true);
266
+ part = makeLayer("BORDER", -8, solid);
267
+ if (options.pixelSize !== undefined) {
268
+ part.texture.SetSnapToPixelGrid?.(false);
269
+ part.texture.SetTexelSnappingBias?.(0);
270
+ }
193
271
  parts.push(part);
272
+ } else {
273
+ part.texture.ClearAllPoints();
274
+ if (solid && part.cutout !== undefined) {
275
+ part.texture.SetColorTexture(1, 1, 1, 1);
276
+ part.cutout = undefined;
277
+ part.red = undefined;
278
+ }
194
279
  }
195
- part.texture.ClearAllPoints();
196
280
  return part;
197
281
  }
198
282
 
199
283
  function band(side: CssSide, inset: number, start: Point, end: Point, first: number, last: number, horizontal: boolean) {
200
284
  if (inset === 0) return;
201
- const part = nextPart(side).texture;
285
+ const part = nextPart(combined ? "top" : side).texture;
202
286
  part.SetPoint(start, frame, start, horizontal ? first : 0, horizontal ? 0 : -first);
203
287
  part.SetPoint(end, frame, end, horizontal ? -last : 0, horizontal ? 0 : last);
204
288
  if (horizontal) part.SetHeight(inset);
@@ -222,6 +306,11 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
222
306
  }
223
307
 
224
308
  function paint(top: number, bottom: number, limit: number) {
309
+ // The neighboring spans share a tint and sum to the same masked row.
310
+ if (combined) {
311
+ patch("top", 0, top, limit, bottom - top);
312
+ return;
313
+ }
225
314
  const split = height === 0 ? limit : Math.min(limit, (top + bottom) * width / (2 * height));
226
315
  patch(vertical, 0, top, split, bottom - top);
227
316
  patch(horizontal, split, top, limit - split, bottom - top);
@@ -239,7 +328,24 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
239
328
  }
240
329
  }
241
330
 
242
- function geometry(insets: Insets, radius: number) {
331
+ function bakedCorner(point: Point, radius: number, art: RoundedBorderCorner): boolean {
332
+ const part = nextPart("top", false);
333
+ const texture = part.texture;
334
+ if (part.cutout !== art.asset) {
335
+ part.cutout = art.asset;
336
+ part.red = undefined;
337
+ if (texture.SetTexture(art.asset) === false) return false;
338
+ }
339
+ const { extent } = art;
340
+ const right = point === "TOPRIGHT" || point === "BOTTOMRIGHT";
341
+ const bottom = point === "BOTTOMLEFT" || point === "BOTTOMRIGHT";
342
+ texture.SetTexCoord(right ? extent : 0, right ? 0 : extent, bottom ? extent : 0, bottom ? 0 : extent);
343
+ texture.SetPoint(point, frame, point, 0, 0);
344
+ texture.SetSize(radius, radius);
345
+ return true;
346
+ }
347
+
348
+ function geometry(insets: Insets, radius: number, art?: RoundedBorderCorner) {
243
349
  const [top, right, bottom, left] = insets;
244
350
  const startX = radius > 0 ? Math.max(radius, left) : 0;
245
351
  const endX = radius > 0 ? Math.max(radius, right) : 0;
@@ -250,21 +356,81 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
250
356
  band("bottom", bottom, "BOTTOMLEFT", "BOTTOMRIGHT", startX, endX, true);
251
357
  band("left", left, "TOPLEFT", "BOTTOMLEFT", startY, endY, false);
252
358
  band("right", right, "TOPRIGHT", "BOTTOMRIGHT", startY, endY, false);
359
+ if (art !== undefined) {
360
+ const bands = used.top;
361
+ if (bakedCorner("TOPLEFT", radius, art) && bakedCorner("TOPRIGHT", radius, art)
362
+ && bakedCorner("BOTTOMLEFT", radius, art) && bakedCorner("BOTTOMRIGHT", radius, art)) return;
363
+ used.top = bands;
364
+ }
253
365
  corner("TOPLEFT", "top", "left", top, left, radius);
254
366
  corner("TOPRIGHT", "top", "right", top, right, radius);
255
367
  corner("BOTTOMLEFT", "bottom", "left", bottom, left, radius);
256
368
  corner("BOTTOMRIGHT", "bottom", "right", bottom, right, radius);
257
369
  }
258
370
 
371
+ function supportsPixelOutline(texture: Texture) {
372
+ return typeof texture.SetTextureSliceMargins === "function" && typeof texture.SetTextureSliceMode === "function"
373
+ && typeof texture.SetIgnoreParentScale === "function" && typeof texture.SetScale === "function"
374
+ && typeof texture.SetSnapToPixelGrid === "function" && typeof texture.SetTexelSnappingBias === "function";
375
+ }
376
+
377
+ function setPixelOutline(pixel: number, radius: number) {
378
+ minOutlineSize = undefined;
379
+ if (nativeOutline === undefined || sliceUnavailable || radius <= pixel || !canPlaceOutline()) return false;
380
+ const scale = physicalPixelScale();
381
+ if (scale === undefined) return false;
382
+ const deviceRadius = Math.round(radius / pixel);
383
+ const asset = nativeOutline.resolve(deviceRadius);
384
+ if (asset === undefined) return false;
385
+ if (lastSizeWidth === undefined || lastSizeHeight === undefined) {
386
+ const [width, height] = frame.GetSize();
387
+ lastSizeWidth = reveal(width);
388
+ lastSizeHeight = reveal(height);
389
+ }
390
+ // Both caps must fit even after the client rounds the vertices to display pixels.
391
+ minOutlineSize = (2 * deviceRadius + 1) * pixel;
392
+ const fit = outlineFits();
393
+ if (fit !== "ready") return fit === "pending";
394
+
395
+ if (outline === undefined) {
396
+ outline = makeLayer("BORDER", -8, false, false);
397
+ const texture = outline.texture;
398
+ if (color === undefined && !supportsPixelOutline(texture)) {
399
+ sliceUnavailable = true;
400
+ return false;
401
+ }
402
+ texture.SetIgnoreParentScale(true);
403
+ texture.SetTexelSnappingBias(0);
404
+ texture.SetSnapToPixelGrid(true);
405
+ texture.SetAllPoints(frame);
406
+ }
407
+ const texture = outline.texture;
408
+ if (sliceRadius !== deviceRadius) {
409
+ sliceRadius = undefined;
410
+ if (texture.SetTexture(asset) === false) return false;
411
+ texture.SetTextureSliceMargins(deviceRadius, deviceRadius, deviceRadius, deviceRadius);
412
+ texture.SetTextureSliceMode(nativeOutline.stretched);
413
+ sliceRadius = deviceRadius;
414
+ }
415
+ if (sliceScale !== scale) {
416
+ texture.SetScale(scale);
417
+ sliceScale = scale;
418
+ }
419
+ return true;
420
+ }
421
+
259
422
  function setBorder(border?: CssBorder, radius = 0): Result<void> {
260
423
  if (!scope.isActive()) return fail("CSS surface scope is disposed");
261
424
  if (isSecret(radius) || !Number.isFinite(radius) || radius < 0) return fail("Invalid CSS border radius");
262
- const read = readBorder(border, options.environment);
425
+ if (slicedFill) {
426
+ return border === undefined && radius === options.fixedFillRadius ? ok(undefined) : fail("A fixed CSS fill cannot change shape");
427
+ }
428
+ const read = uniformBorder ?? readBorder(border, options.environment);
263
429
  if (!read.ok) return read;
264
- const { insets, colors } = read.value;
430
+ const { insets: logical, colors } = read.value;
265
431
  if (artwork !== undefined) {
266
432
  const edge = colors.top;
267
- if (border !== undefined && (!insets.every(amount => amount === 1) || edge === undefined
433
+ if (border !== undefined && (!logical.every(amount => amount === 1) || edge === undefined
268
434
  || !borderSides.every(side => colors[side]?.literal === edge.literal))) {
269
435
  return fail("Native rounded art requires a uniform 1px CSS border");
270
436
  }
@@ -290,15 +456,44 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
290
456
  });
291
457
  return configured;
292
458
  }
459
+ const pixel = border === undefined && uniformBorder === undefined ? undefined : options.pixelSize?.();
460
+ const snap = (value: number) => value === 0 || pixel === undefined ? value
461
+ : value === 1 ? pixel : Math.max(pixel, snapPixel(value, pixel));
462
+ const insets: Insets = pixel === undefined ? logical : [snap(logical[0]), snap(logical[1]), snap(logical[2]), snap(logical[3])];
293
463
  const configured = scope.protect(() => {
294
- if (radius !== previousRadius || !sameInsets(previousInsets, insets)) {
464
+ const edge = colors.top;
465
+ if (canSlice && pixel !== undefined && edge !== undefined) {
466
+ const sliced = scope.protect(() => setPixelOutline(pixel, radius));
467
+ if (!sliced.ok) sliceUnavailable = true;
468
+ if (sliced.ok && sliced.value) {
469
+ const masked = setMask(color === undefined ? undefined : radius);
470
+ if (!masked.ok) return masked;
471
+ if (!sliceActive) for (const part of edges.top as OwnedArray<SurfaceTexture>) shown(part, false);
472
+ sliceActive = true;
473
+ if (outline !== undefined) {
474
+ const [r, g, b, a] = borderState?.drawn.top ?? edge.rgba;
475
+ tint(outline, r, g, b, a);
476
+ shown(outline, true);
477
+ }
478
+ if (borderState !== undefined) borderState.base.value = colors;
479
+ return ok(undefined);
480
+ }
481
+ }
482
+ const switching = sliceActive;
483
+ if (switching && outline !== undefined) shown(outline, false);
484
+ sliceActive = false;
485
+ if (switching || radius !== previousRadius || !sameInsets(previousInsets, insets)) {
295
486
  const masked = setMask(radius > 0 ? radius : insets.some(value => value > 0) ? 0 : undefined);
296
487
  if (!masked.ok) return masked;
297
- geometry(insets, radius);
488
+ // Keep the side bands and outer mask. The corner texture provides only the inner cutout.
489
+ const onePixel = pixel !== undefined && pixel > 0 && radius > pixel && combined && options.borderPaint?.width === 1
490
+ && logical[0] === 1 && logical[1] === 1 && logical[2] === 1 && logical[3] === 1;
491
+ const art = onePixel ? options.rounded?.resolveBorderCorner?.(Math.round(radius / pixel)) : undefined;
492
+ geometry(insets, radius, art);
298
493
  previousRadius = radius;
299
494
  previousInsets = insets;
300
495
  }
301
- for (const side of borderSides) {
496
+ for (const side of painted) {
302
497
  const base = colors[side];
303
498
  if (base === undefined && borderState !== undefined) borderState.drawn[side] = undefined;
304
499
  const drawn = base === undefined || borderState === undefined ? undefined : borderState.drawn[side];
@@ -320,7 +515,7 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
320
515
  }
321
516
 
322
517
  const borderPaint: CssSurface["borderPaint"] = borderState === undefined ? undefined : {
323
- sides: artwork === undefined ? undefined : ["top"],
518
+ sides: paintSides,
324
519
  read: side => borderState.base.value[side]?.literal,
325
520
  write(this: void, side, red, green, blue, alpha) {
326
521
  if (!scope.isActive() || borderState.base.value[side] === undefined) return;
@@ -329,8 +524,8 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
329
524
  drawn[1] = green;
330
525
  drawn[2] = blue;
331
526
  drawn[3] = alpha;
332
- if (outline !== undefined) {
333
- tint(outline, red, green, blue, alpha);
527
+ if (sliceActive || artwork !== undefined) {
528
+ if (outline !== undefined) tint(outline, red, green, blue, alpha);
334
529
  return;
335
530
  }
336
531
  const parts = edges[side] as OwnedArray<SurfaceTexture>;
@@ -377,8 +572,14 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
377
572
 
378
573
  const created = scope.protect(() => {
379
574
  if (options.color === true) {
380
- color = makeLayer("BACKGROUND", -8, artwork === undefined);
575
+ const fill = options.fixedFillRadius;
576
+ color = makeLayer("BACKGROUND", -8, artwork === undefined && fill === undefined);
381
577
  color.texture.SetAllPoints(frame);
578
+ if (canSlice && !supportsPixelOutline(color.texture)) canSlice = false;
579
+ if (fill !== undefined) {
580
+ slicedFill = options.rounded?.applyExactFill?.(color.texture, fill) === true;
581
+ if (!slicedFill) color.texture.SetColorTexture(1, 1, 1, 1);
582
+ }
382
583
  }
383
584
  if (options.image === true) {
384
585
  image = makeLayer("BACKGROUND", -7, false);
@@ -394,5 +595,9 @@ export function createCssSurface(this: void, scope: RuntimeScope, frame: Frame,
394
595
  scope.protect(release);
395
596
  return cleanup;
396
597
  }
397
- return ok({ setBorder, borderPaint, setBackgroundColor, setBackgroundImage });
598
+ const responsiveBorder = canSlice ? {
599
+ resize: borderSizeChanged,
600
+ useFallback: () => { canSlice = false; },
601
+ } : undefined;
602
+ return ok({ responsiveBorder, setBorder, borderPaint, setBackgroundColor, setBackgroundImage });
398
603
  }
package/wow/cursor.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { api, reveal, type LuaMultiReturn } from "./native";
2
+
3
+ declare const $multi: (<const Values extends unknown[]>(this: void, ...values: Values) => LuaMultiReturn<Values>) & {
4
+ readonly __tstlExtension: "MultiFunction";
5
+ };
6
+
7
+ /** Read the cursor's first result when the client exposes it. */
8
+ export function readCursorKind(this: void): string | undefined {
9
+ const read = api.GetCursorInfo;
10
+ if (read === undefined) return undefined;
11
+ return reveal(read()[0]);
12
+ }
13
+
14
+ /** A spell, bag item, or collected toy carried by the WoW cursor. */
15
+ export type CursorAction = { readonly kind: "spell" | "item" | "toy"; readonly id: number };
16
+
17
+ function readCursorActionParts(this: void): LuaMultiReturn<[] | [kind: CursorAction["kind"], id: number]> {
18
+ const read = api.GetCursorInfo;
19
+ if (read === undefined) return $multi();
20
+ // WoW returns the spellbook index first and the spell ID last.
21
+ const [rawKind, first, , spell] = read();
22
+ const kind = reveal(rawKind);
23
+ if (kind !== "spell" && kind !== "item" && kind !== "toy") return $multi();
24
+ let rawId = first;
25
+ if (kind === "spell") rawId = spell;
26
+ const id = reveal(rawId);
27
+ if (typeof id !== "number" || !(id > 0 && id <= 2147483647 && Math.floor(id) === id)) return $multi();
28
+ return $multi(kind, id);
29
+ }
30
+
31
+ /** Check whether the cursor carries an assignable action without resolving toy ownership. */
32
+ export function hasCursorAction(this: void): boolean {
33
+ return readCursorActionParts()[0] !== undefined;
34
+ }
35
+
36
+ /** Read an action suitable for assignment to a secure action slot. */
37
+ export function readCursorAction(this: void): CursorAction | undefined {
38
+ const [kind, id] = readCursorActionParts();
39
+ if (kind === undefined) return undefined;
40
+ if (kind === "item") {
41
+ const ownsToy = api.PlayerHasToy;
42
+ if (ownsToy !== undefined && reveal(ownsToy(id)) === true) return { kind: "toy", id };
43
+ }
44
+ return { kind, id };
45
+ }
@@ -1,5 +1,5 @@
1
1
  import { fail, ok, type Result } from "../src/result";
2
- import type { Frame, FrameEventRegistration, Region, Texture } from "./frames";
2
+ import type { FontString, Frame, FrameEventRegistration, FrameMap, Region, Texture } from "./frames";
3
3
  import { reveal, type SecretValue } from "./native";
4
4
  import { protect } from "./protect";
5
5
 
@@ -10,6 +10,28 @@ export function canAccessRegion<T extends NativeRegion>(region: T | SecretValue<
10
10
  return readable !== undefined && reveal(readable.IsForbidden()) === false;
11
11
  }
12
12
 
13
+ export function readFrameWidth(this: void, region: Pick<Frame, "GetWidth">): number | undefined {
14
+ return reveal(region.GetWidth());
15
+ }
16
+
17
+ export function readFrameHeight(this: void, region: Pick<Frame, "GetHeight">): number | undefined {
18
+ return reveal(region.GetHeight());
19
+ }
20
+
21
+ export function readFrameScale(this: void, region: Pick<Frame, "GetEffectiveScale">): number | undefined {
22
+ return reveal(region.GetEffectiveScale());
23
+ }
24
+
25
+ export function readFrameShown(this: void, region: Pick<Frame, "IsShown">): boolean | undefined {
26
+ return reveal(region.IsShown());
27
+ }
28
+
29
+ export function readFontPath(this: void, region: FontString | FrameMap["EditBox"]): string | undefined {
30
+ const [raw] = region.GetFont();
31
+ const path = reveal(raw);
32
+ return typeof path === "string" ? path : undefined;
33
+ }
34
+
13
35
  /** Read a texture from a heterogeneous native region list. */
14
36
  export function asTexture(region: Region | SecretValue<Region> | undefined): Texture | undefined {
15
37
  if (canAccessRegion(region) && reveal(region.IsObjectType("Texture")) === true) {
@@ -0,0 +1,13 @@
1
+ import { createFrameDriver } from "../src/frame-scheduler";
2
+ import { clock } from "./clock";
3
+ import { rawField } from "./lua-table";
4
+ import { api } from "./native";
5
+
6
+ const profileClock = rawField<(this: void) => number>(api, "debugprofilestop");
7
+
8
+ /** The addon's frame clock and combat state for the scheduler. */
9
+ export const frameTiming = {
10
+ nowMs: () => profileClock?.() ?? api.GetTime() * 1000,
11
+ inCombat: () => api.InCombatLockdown() === true,
12
+ };
13
+ export const frameDriver = createFrameDriver(clock, frameTiming);
package/wow/hooks.ts ADDED
@@ -0,0 +1,133 @@
1
+ import { currentScope } from "../src/reactive-scope";
2
+ import { ref, type ReadonlyRef, type SignalRef } from "../src/reactivity";
3
+ import type { OwnedReadonlyArray } from "../src/owned-collections";
4
+ import type { RuntimeScope } from "../src/runtime";
5
+ import { appContext } from "./app-ui";
6
+ import type { EventOptions, WoWEvent, WoWEventMap } from "./contract";
7
+ import { setRawField } from "./lua-table";
8
+ import { api, reveal } from "./native";
9
+
10
+ function owner(name: string): RuntimeScope {
11
+ const scope = currentScope();
12
+ if (scope === undefined) throw new Error(name + " works during the setup of a component or store");
13
+ return scope;
14
+ }
15
+
16
+ /**
17
+ * Run `handler` for a WoW event while the calling component or store is alive.
18
+ *
19
+ * ```ts
20
+ * useEvent("PLAYER_MONEY", () => { money.value = readMoney(); });
21
+ * ```
22
+ */
23
+ export function useEvent<Event extends WoWEvent>(this: void, event: Event,
24
+ handler: (this: void, ...payload: WoWEventMap[Event]) => void, options?: EventOptions): void {
25
+ const scope = owner("useEvent");
26
+ const context = appContext();
27
+ if (context === undefined) throw new Error("useEvent needs a running addon");
28
+ const subscribed = context.events.on(scope, event, handler, options);
29
+ if (!subscribed.ok) throw new Error(subscribed.reason);
30
+ }
31
+
32
+ /** Run `cleanup` when the calling component or store goes away. */
33
+ export function onCleanup(this: void, cleanup: (this: void) => void): void {
34
+ const registered = owner("onCleanup").onDispose(cleanup);
35
+ if (!registered.ok) throw new Error(registered.reason);
36
+ }
37
+
38
+ interface Clock {
39
+ readonly time: SignalRef<number>;
40
+ users: number;
41
+ cancel?: (this: void) => void;
42
+ }
43
+
44
+ const clocks: Record<number, Clock | undefined> = {};
45
+
46
+ function gameTime() {
47
+ const time = reveal(api.GetTime());
48
+ return typeof time === "number" ? time : 0;
49
+ }
50
+
51
+ /**
52
+ * The game time in seconds, updated every `interval` seconds while the caller is alive.
53
+ * Components that use the same interval share one timer.
54
+ */
55
+ export function useNow(this: void, interval = 1): ReadonlyRef<number> {
56
+ const scope = owner("useNow");
57
+ if (!(interval > 0)) throw new Error("useNow needs a positive interval");
58
+ let clock = clocks[interval];
59
+ if (clock === undefined) {
60
+ clock = { time: ref(gameTime()), users: 0 };
61
+ clocks[interval] = clock;
62
+ }
63
+ const shared = clock;
64
+ if (shared.users === 0) {
65
+ shared.time.value = gameTime();
66
+ const ticker = api.C_Timer.NewTicker(interval, () => { shared.time.value = gameTime(); });
67
+ shared.cancel = () => { (ticker as unknown as { Cancel(this: object): void }).Cancel(); };
68
+ }
69
+ shared.users++;
70
+ scope.onDispose(() => {
71
+ shared.users--;
72
+ if (shared.users > 0) return;
73
+ shared.cancel?.();
74
+ shared.cancel = undefined;
75
+ });
76
+ return shared.time;
77
+ }
78
+
79
+ let combat: SignalRef<boolean> | undefined;
80
+ let combatUsers = 0;
81
+
82
+ /** Whether the player is in combat. Protected changes, such as secure buttons or loading plugins, wait for false. */
83
+ export function useCombat(this: void): ReadonlyRef<boolean> {
84
+ const scope = owner("useCombat");
85
+ const context = appContext();
86
+ if (context === undefined) throw new Error("useCombat needs a running addon");
87
+ const state = combat ??= ref(reveal(api.InCombatLockdown()) === true);
88
+ if (combatUsers === 0) state.value = reveal(api.InCombatLockdown()) === true;
89
+ combatUsers++;
90
+ const started = context.events.on(scope, "PLAYER_REGEN_DISABLED", () => { state.value = true; });
91
+ if (!started.ok) throw new Error(started.reason);
92
+ const ended = context.events.on(scope, "PLAYER_REGEN_ENABLED", () => { state.value = false; });
93
+ if (!ended.ok) throw new Error(ended.reason);
94
+ scope.onDispose(() => { combatUsers--; });
95
+ return state;
96
+ }
97
+
98
+ function commandName(name: string): boolean {
99
+ if (name.length < 2 || name.charCodeAt(0) !== 47) return false;
100
+ for (let index = 1; index < name.length; index++) {
101
+ const code = name.charCodeAt(index);
102
+ const letter = code >= 65 && code <= 90 || code >= 97 && code <= 122;
103
+ const digit = code >= 48 && code <= 57;
104
+ if (!letter && !digit && code !== 95 && code !== 45) return false;
105
+ }
106
+ return true;
107
+ }
108
+
109
+ /**
110
+ * Register a slash command while the caller is alive. The first name is the command's ID.
111
+ *
112
+ * ```ts
113
+ * useSlashCommand(["/sessions", "/st"], input => { useWindow().toggle(); });
114
+ * ```
115
+ */
116
+ export function useSlashCommand(this: void, commands: readonly string[], handler: (this: void, input: string) => void): void {
117
+ const scope = owner("useSlashCommand");
118
+ const names = commands as OwnedReadonlyArray<string>;
119
+ if (names.length === 0 || names.some(name => !commandName(name))) {
120
+ throw new Error("useSlashCommand needs names such as /myaddon: a slash, then letters, digits, _ or -");
121
+ }
122
+ const key = "KUMI_" + names[0].slice(1).toUpperCase().split("-").join("_");
123
+ const globals = api as unknown as object;
124
+ const list = api.SlashCmdList;
125
+ if (list === undefined) throw new Error("This client has no slash commands");
126
+ if (list[key] !== undefined) throw new Error("Slash command " + names[0] + " is already registered");
127
+ for (let index = 0; index < names.length; index++) setRawField(globals, "SLASH_" + key + String(index + 1), names[index]);
128
+ list[key] = input => { handler(input.trim()); };
129
+ scope.onDispose(() => {
130
+ list[key] = undefined as never;
131
+ for (let index = 0; index < names.length; index++) setRawField(globals, "SLASH_" + key + String(index + 1), undefined);
132
+ });
133
+ }