@overmvp/kumi 0.0.0-stage → 0.1.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 (205) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +14 -2
  3. package/animation/color-transition.ts +196 -0
  4. package/animation/cycle.ts +151 -0
  5. package/animation/ease.ts +29 -0
  6. package/animation/index.ts +13 -0
  7. package/animation/package.json +12 -0
  8. package/animation/presets.ts +376 -0
  9. package/animation/spec.ts +197 -0
  10. package/builder/Cargo.lock +192 -0
  11. package/builder/Cargo.toml +7 -0
  12. package/builder/css-v1.mjs +334 -0
  13. package/builder/css-v1.test.mjs +321 -0
  14. package/builder/generated-output.mjs +61 -0
  15. package/builder/generated-output.test.mjs +132 -0
  16. package/builder/jsx-form.mjs +93 -0
  17. package/builder/jsx-form.test.mjs +48 -0
  18. package/builder/jsx-lists.mjs +209 -0
  19. package/builder/link-addon-package.mjs +86 -0
  20. package/builder/lua-assets.mjs +74 -0
  21. package/builder/lua-builtin-values.mjs +57 -0
  22. package/builder/lua-builtin-values.test.mjs +155 -0
  23. package/builder/lua-loop-closures.mjs +183 -0
  24. package/builder/lua-loop-closures.test.mjs +89 -0
  25. package/builder/lua-truthiness.mjs +61 -0
  26. package/builder/lua-truthiness.test.mjs +113 -0
  27. package/builder/module-declarations.mjs +594 -0
  28. package/builder/module-declarations.test.mjs +444 -0
  29. package/builder/native-kernels.mjs +60 -0
  30. package/builder/native-lua.d.ts +57 -0
  31. package/builder/native-routes.mjs +51 -0
  32. package/builder/no-static-ref-read.mjs +93 -0
  33. package/builder/no-static-ref-read.test.mjs +181 -0
  34. package/builder/secure-snippet.mjs +317 -0
  35. package/builder/secure-snippet.test.mjs +101 -0
  36. package/builder/shared-lua-runtime.mjs +150 -0
  37. package/builder/src/addon-link.lua +29 -0
  38. package/builder/src/arrays.lua +260 -0
  39. package/builder/src/assets.rs +177 -0
  40. package/builder/src/classes.lua +75 -0
  41. package/builder/src/collections.lua +190 -0
  42. package/builder/src/compiler.rs +2164 -0
  43. package/builder/src/config.rs +42 -0
  44. package/builder/src/entries.rs +166 -0
  45. package/builder/src/errors.lua +15 -0
  46. package/builder/src/lua_data.rs +231 -0
  47. package/builder/src/main.rs +367 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +47 -0
  50. package/builder/src/numbers.lua +62 -0
  51. package/builder/src/project-front.mjs +3331 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +127 -0
  54. package/builder/src/signal-kernel.fingerprint +1 -0
  55. package/builder/src/strings.lua +103 -0
  56. package/builder/src/ts-lower.mjs +68 -0
  57. package/builder/src/wow_client.rs +253 -0
  58. package/builder/src/wow_types.rs +564 -0
  59. package/builder/style-states.mjs +100 -0
  60. package/builder/style-states.test.mjs +103 -0
  61. package/builder/vite-assets.mjs +53 -0
  62. package/builder/vite-project.mjs +10 -0
  63. package/config.ts +17 -0
  64. package/form/index.ts +522 -0
  65. package/form/package.json +13 -0
  66. package/motion/index.ts +2 -0
  67. package/motion/package.json +10 -0
  68. package/package.json +22 -4
  69. package/router/index.ts +43 -0
  70. package/router/package.json +12 -0
  71. package/router/routes.ts +64 -0
  72. package/scripts/kumi-cli.mjs +70 -0
  73. package/scripts/link-addons.mjs +51 -0
  74. package/src/bindings.ts +35 -0
  75. package/src/context.ts +15 -0
  76. package/src/css-tokens.ts +234 -0
  77. package/src/css-v1.ts +58 -0
  78. package/src/cvars.ts +91 -0
  79. package/src/element-node.ts +32 -0
  80. package/src/elements.ts +479 -0
  81. package/src/feature-scope.ts +82 -0
  82. package/src/flex-layout.ts +807 -0
  83. package/src/frame-scheduler.ts +334 -0
  84. package/src/grid-tracks.ts +165 -0
  85. package/src/image-imports.d.ts +24 -0
  86. package/src/images.ts +79 -0
  87. package/src/index.ts +28 -0
  88. package/src/jsx-dev-runtime.ts +2 -0
  89. package/src/jsx-runtime.ts +22 -0
  90. package/src/jsx.d.ts +12 -0
  91. package/src/keyed-list.ts +363 -0
  92. package/src/media-query.ts +144 -0
  93. package/src/module.ts +277 -0
  94. package/src/node-selectors.ts +114 -0
  95. package/src/owned-collections.ts +44 -0
  96. package/src/providers.ts +235 -0
  97. package/src/reactive-scope.ts +34 -0
  98. package/src/reactivity.ts +254 -0
  99. package/src/result.ts +58 -0
  100. package/src/runtime.ts +52 -0
  101. package/src/scheduler.ts +185 -0
  102. package/src/signal-kernel.ts +30 -0
  103. package/src/style-classes.ts +51 -0
  104. package/src/style-states.ts +134 -0
  105. package/src/style-variables.ts +53 -0
  106. package/src/text-children.ts +42 -0
  107. package/src/theme.ts +50 -0
  108. package/src/transition.ts +18 -0
  109. package/state/index.ts +3 -0
  110. package/state/package.json +12 -0
  111. package/state/store-bindings.ts +25 -0
  112. package/state/store.ts +82 -0
  113. package/styles/_layers.scss +14 -0
  114. package/templates/addon/README.md +14 -0
  115. package/templates/addon/__ADDON_NAME__.toc +4 -0
  116. package/templates/addon/eslint.config.mjs +12 -0
  117. package/templates/addon/gitignore +8 -0
  118. package/templates/addon/package.json +21 -0
  119. package/templates/addon/scripts/build.mjs +25 -0
  120. package/templates/addon/src/app/main.tsx +13 -0
  121. package/templates/addon/tsconfig.json +24 -0
  122. package/translate/index.ts +14 -0
  123. package/translate/package.json +9 -0
  124. package/utils/colors.ts +76 -0
  125. package/utils/format.ts +37 -0
  126. package/utils/index.ts +4 -0
  127. package/utils/number.ts +17 -0
  128. package/utils/package.json +7 -0
  129. package/utils/text.ts +6 -0
  130. package/validate/index.ts +119 -0
  131. package/validate/package.json +9 -0
  132. package/wow/absolute-position.ts +142 -0
  133. package/wow/animation.ts +428 -0
  134. package/wow/aura-button.ts +76 -0
  135. package/wow/aura-container.ts +591 -0
  136. package/wow/aura-countdown.ts +201 -0
  137. package/wow/aura-slot.ts +153 -0
  138. package/wow/bar-shade.ts +93 -0
  139. package/wow/borrowed-slot.ts +216 -0
  140. package/wow/button-drag.ts +48 -0
  141. package/wow/cast-clock.ts +153 -0
  142. package/wow/cast-text.ts +83 -0
  143. package/wow/class-colors.ts +126 -0
  144. package/wow/clock.ts +7 -0
  145. package/wow/color-picker.ts +128 -0
  146. package/wow/commands.ts +17 -0
  147. package/wow/contained-frame.ts +128 -0
  148. package/wow/contract.ts +42 -0
  149. package/wow/css-box.ts +110 -0
  150. package/wow/css-color.ts +12 -0
  151. package/wow/css-style.ts +109 -0
  152. package/wow/css-surfaces.ts +398 -0
  153. package/wow/css-text.ts +399 -0
  154. package/wow/cvar-schema.ts +32 -0
  155. package/wow/cvars.ts +98 -0
  156. package/wow/dropdown-art.ts +39 -0
  157. package/wow/element-click.ts +20 -0
  158. package/wow/element-event-coordinator.ts +219 -0
  159. package/wow/element-events.ts +317 -0
  160. package/wow/event-dispatcher.ts +151 -0
  161. package/wow/events.ts +50 -0
  162. package/wow/font-cache.ts +92 -0
  163. package/wow/frame-access.ts +31 -0
  164. package/wow/frame-layer.ts +5 -0
  165. package/wow/frames.ts +236 -0
  166. package/wow/heal-prediction.ts +305 -0
  167. package/wow/image-layers.ts +55 -0
  168. package/wow/index.ts +97 -0
  169. package/wow/layout-view.ts +651 -0
  170. package/wow/lua-string.ts +18 -0
  171. package/wow/lua-table.ts +18 -0
  172. package/wow/macro-book.ts +84 -0
  173. package/wow/map-position.ts +27 -0
  174. package/wow/media-query.ts +65 -0
  175. package/wow/module-events.ts +11 -0
  176. package/wow/native-hooks.ts +168 -0
  177. package/wow/native-placement.ts +88 -0
  178. package/wow/native.ts +135 -0
  179. package/wow/number-format.ts +8 -0
  180. package/wow/number-input.ts +10 -0
  181. package/wow/protect.ts +24 -0
  182. package/wow/protected-layout.ts +133 -0
  183. package/wow/range-art.ts +74 -0
  184. package/wow/reparent-frame.ts +99 -0
  185. package/wow/rounded-texture.ts +156 -0
  186. package/wow/saved-variables.ts +60 -0
  187. package/wow/scroll-view.ts +64 -0
  188. package/wow/secret-ref.ts +14 -0
  189. package/wow/secure-bindings.ts +89 -0
  190. package/wow/secure-clicks.ts +432 -0
  191. package/wow/secure-snippet.ts +235 -0
  192. package/wow/secure-state.ts +130 -0
  193. package/wow/secure-target-visibility.ts +89 -0
  194. package/wow/secure-transaction.ts +331 -0
  195. package/wow/special-window.ts +269 -0
  196. package/wow/start-addon.ts +99 -0
  197. package/wow/state-paint.ts +256 -0
  198. package/wow/transition.ts +86 -0
  199. package/wow/unit-button.ts +39 -0
  200. package/wow/unit-cast.ts +92 -0
  201. package/wow/unit-health-text.ts +47 -0
  202. package/wow/view-nodes.ts +304 -0
  203. package/wow/view.ts +1746 -0
  204. package/wow/visible-frame-job.ts +34 -0
  205. package/wow/width-reserve.ts +108 -0
@@ -0,0 +1,201 @@
1
+ import type {
2
+ Cooldown, Data_DurationTextBindingColorOptions, Data_NumericRuleFormatBreakpoint, Data_NumericRuleFormatComponent,
3
+ LuaColorCurveObjectAPI, NumericFormatterAPI,
4
+ } from "@kumi/wow-generated";
5
+ import { fail, type Result } from "../src/result";
6
+ import type { FontString, FrameParent } from "./frames";
7
+ import { luaString } from "./lua-string";
8
+ import { rawField } from "./lua-table";
9
+ import { api, reveal } from "./native";
10
+ import { protect } from "./protect";
11
+
12
+ type CountdownStyle = "compact" | "minutes" | "seconds";
13
+ type Countdown = Partial<Pick<Cooldown, "SetCountdownFormatter">>;
14
+ // The client inherits the breakpoint rounding when its component omits the override.
15
+ type Rule = Omit<Data_NumericRuleFormatBreakpoint, "components"> & {
16
+ components?: (Omit<Data_NumericRuleFormatComponent, "rounding"> & Partial<Pick<Data_NumericRuleFormatComponent, "rounding">>)[];
17
+ };
18
+ type Formatter = NumericFormatterAPI & { SetBreakpoints(this: object, rules: Rule[]): void };
19
+
20
+ declare function tonumber(this: void, value: unknown): number | undefined;
21
+
22
+ const formatters: Record<CountdownStyle, Partial<Record<number, NumericFormatterAPI>>> = {
23
+ compact: {},
24
+ minutes: {},
25
+ seconds: {},
26
+ };
27
+
28
+ function createRuleFormatter(rules: Rule[]): NumericFormatterAPI | undefined {
29
+ const create = rawField<(this: void) => Formatter | undefined>(api.C_StringUtil, "CreateNumericRuleFormatter");
30
+ if (typeof create !== "function") return undefined;
31
+
32
+ const created = protect(() => {
33
+ const formatter = create();
34
+ if (typeof formatter?.SetBreakpoints !== "function") return undefined;
35
+ formatter.SetBreakpoints(rules);
36
+ return formatter;
37
+ });
38
+ return created.ok ? created.value : undefined;
39
+ }
40
+
41
+ function countdownFormatter(decimals?: number, style?: string): NumericFormatterAPI | undefined {
42
+ const value = tonumber(reveal(decimals)) ?? 0;
43
+ const precision = Number.isNaN(value) ? 0 : Math.max(0, Math.min(2, Math.floor(value)));
44
+ const mode = reveal(style);
45
+ const format: CountdownStyle = mode === "minutes" || mode === "seconds" ? mode : "compact";
46
+ const byPrecision = formatters[format];
47
+ const cached = byPrecision[precision];
48
+ if (cached !== undefined) return cached;
49
+
50
+ const rounding = api.Enum?.NumericRuleFormatRounding;
51
+ if (rounding?.Up === undefined || rounding.Nearest === undefined) return undefined;
52
+
53
+ const rules: Rule[] = [{
54
+ threshold: 0,
55
+ format: "%." + precision + "f",
56
+ rounding: precision === 0 ? rounding.Up : rounding.Nearest,
57
+ step: precision === 0 ? 1 : undefined,
58
+ }];
59
+ if (format === "minutes") {
60
+ rules.push({ threshold: 59, format: "%dm", rounding: rounding.Up, step: 1,
61
+ components: [{ div: 60 }] });
62
+ } else if (format === "compact") {
63
+ rules.push({ threshold: 60, format: "%d:%02d", rounding: rounding.Up, step: 1,
64
+ components: [{ div: 60 }, { mod: 60 }] });
65
+ }
66
+ if (format !== "seconds") {
67
+ rules.push({ threshold: 3600, format: "%dh", rounding: rounding.Up, step: 1, components: [{ div: 3600 }] });
68
+ rules.push({ threshold: 86400, format: "%dd", rounding: rounding.Up, step: 1, components: [{ div: 86400 }] });
69
+ }
70
+
71
+ const formatter = createRuleFormatter(rules);
72
+ if (formatter !== undefined) byPrecision[precision] = formatter;
73
+ return formatter;
74
+ }
75
+
76
+ /** Set how the client displays a countdown, including restricted aura durations. */
77
+ export function setAuraCountdownFormat(cooldown: Countdown, decimals?: number, style?: string): Result<void> {
78
+ return protect(() => {
79
+ if (typeof cooldown.SetCountdownFormatter !== "function") return;
80
+ if (decimals === undefined && style === undefined) {
81
+ cooldown.SetCountdownFormatter(undefined);
82
+ return;
83
+ }
84
+
85
+ cooldown.SetCountdownFormatter(countdownFormatter(decimals, style));
86
+ });
87
+ }
88
+
89
+ type WarningColor = readonly [number, number, number];
90
+ type ColorCurve = LuaColorCurveObjectAPI & { SetType(this: object, kind: number): void };
91
+ type DurationBinding = { textFormatter?: NumericFormatterAPI; textColor?: Data_DurationTextBindingColorOptions };
92
+ type DurationButton = FrameParent & { SetDurationText?(this: object, text: FontString, options: DurationBinding): void };
93
+ type StackButton = { SetApplicationCount?(this: object, text: FontString, options?: { formatter: NumericFormatterAPI }): void };
94
+
95
+ export interface AuraDurationTextOptions {
96
+ decimals?: number;
97
+ format?: string;
98
+ warningSeconds?: number;
99
+ warningColor?: WarningColor;
100
+ }
101
+
102
+ export interface AuraStackTextOptions {
103
+ showSingle?: boolean;
104
+ warningThreshold?: number;
105
+ warningColor?: WarningColor;
106
+ }
107
+
108
+ const defaultWarningColor: WarningColor = [1, 0.25, 0.25];
109
+ const warningCurves: Partial<Record<string, ColorCurve>> = {};
110
+ const stackFormatters: Partial<Record<string, NumericFormatterAPI>> = {};
111
+
112
+ function getWarningColor(color: WarningColor | undefined): WarningColor {
113
+ const rgb = reveal(color);
114
+ if (rgb === undefined) return defaultWarningColor;
115
+ const [r, g, b] = rgb;
116
+ const red = reveal(r), green = reveal(g), blue = reveal(b);
117
+ if (red === undefined || green === undefined || blue === undefined) return defaultWarningColor;
118
+ if (!Number.isFinite(red) || !Number.isFinite(green) || !Number.isFinite(blue)) return defaultWarningColor;
119
+ return [red, green, blue];
120
+ }
121
+
122
+ function warningCurve(seconds: number, color: WarningColor): ColorCurve | undefined {
123
+ const create = api.C_CurveUtil?.CreateColorCurve;
124
+ const createColor = rawField<(this: void, red: number, green: number, blue: number, alpha: number) => unknown>(api, "CreateColor");
125
+ const curveTypes = api.Enum?.LuaCurveType;
126
+ const step = curveTypes?.Step;
127
+ if (create === undefined || createColor === undefined || step === undefined) return undefined;
128
+ const [r, g, b] = color;
129
+ const key = seconds + ":" + r + "," + g + "," + b;
130
+ const cached = warningCurves[key];
131
+ if (cached !== undefined) return cached;
132
+ const result = protect(() => {
133
+ const curve = create() as ColorCurve;
134
+ curve.SetType(step);
135
+ curve.AddPoint(0, createColor(r, g, b, 1));
136
+ curve.AddPoint(seconds + 0.001, createColor(1, 1, 1, 1));
137
+ return curve;
138
+ });
139
+ if (!result.ok) return undefined;
140
+ warningCurves[key] = result.value;
141
+ return result.value;
142
+ }
143
+
144
+ /** Let a native aura button write its remaining duration into a text region. */
145
+ export function bindAuraDurationText(button: DurationButton, text: FontString, {
146
+ decimals, format, warningSeconds, warningColor,
147
+ }: AuraDurationTextOptions = {}): Result<void> {
148
+ const bind = button.SetDurationText;
149
+ if (bind === undefined) return fail("Aura button does not accept duration text");
150
+ const seconds = tonumber(reveal(warningSeconds)) ?? 0;
151
+ const properties = api.Enum?.DurationTextBindingProperty;
152
+ const property = properties?.RemainingDuration;
153
+ const curve = property !== undefined && Number.isFinite(seconds) && seconds > 0
154
+ ? warningCurve(seconds, getWarningColor(warningColor))
155
+ : undefined;
156
+ const result = protect(() => bind.call(button, text, {
157
+ textFormatter: countdownFormatter(decimals, format),
158
+ textColor: property !== undefined && curve !== undefined ? { curve, property } : undefined,
159
+ }));
160
+ return result.ok ? result : protect(() => bind.call(button, text, {}));
161
+ }
162
+
163
+ function stackFormatter(showSingle: boolean, threshold: number, color: WarningColor): NumericFormatterAPI | undefined {
164
+ const [r, g, b] = color;
165
+ const key = String(showSingle) + ":" + threshold + ":" + r + "," + g + "," + b;
166
+ const cached = stackFormatters[key];
167
+ if (cached !== undefined) return cached;
168
+ const roundings = api.Enum?.NumericRuleFormatRounding;
169
+ const rounding = roundings?.Down;
170
+ if (rounding === undefined) return undefined;
171
+
172
+ const first = { threshold: showSingle ? 1 : 2, format: "%d", rounding, step: 1 };
173
+ const rules: Rule[] = [{ threshold: 0, format: "", rounding }, first];
174
+ if (threshold > 0) {
175
+ const hex = luaString.format("%02x%02x%02x", Math.floor(r * 255 + 0.5), Math.floor(g * 255 + 0.5), Math.floor(b * 255 + 0.5));
176
+ const format = "|cff" + hex + "%d|r";
177
+ const warning = Math.max(first.threshold, threshold);
178
+ if (warning === first.threshold) first.format = format;
179
+ else rules.push({ threshold: warning, format, rounding, step: 1 });
180
+ }
181
+ const formatter = createRuleFormatter(rules);
182
+ if (formatter !== undefined) stackFormatters[key] = formatter;
183
+ return formatter;
184
+ }
185
+
186
+ /** Let a native aura button write its application count into a text region. */
187
+ export function bindAuraStackText(button: StackButton, text: FontString, {
188
+ showSingle, warningThreshold, warningColor,
189
+ }: AuraStackTextOptions = {}): Result<void> {
190
+ const bind = button.SetApplicationCount;
191
+ if (bind === undefined) return fail("Aura button does not accept stack text");
192
+ const threshold = tonumber(reveal(warningThreshold)) ?? 0;
193
+ const warning = Number.isFinite(threshold) ? Math.max(0, Math.floor(threshold)) : 0;
194
+ const showOne = reveal(showSingle) === true;
195
+ if (!showOne && warning === 0) return protect(() => bind.call(button, text));
196
+
197
+ const formatter = stackFormatter(showOne, warning, getWarningColor(warningColor));
198
+ if (formatter === undefined) return protect(() => bind.call(button, text));
199
+ const result = protect(() => bind.call(button, text, { formatter }));
200
+ return result.ok ? result : protect(() => bind.call(button, text));
201
+ }
@@ -0,0 +1,153 @@
1
+ import { fail, ok, type Failure, type Result } from "../src/result";
2
+ import type { AuraButton } from "./aura-container";
3
+ import type { Frame } from "./frames";
4
+ import { rawField } from "./lua-table";
5
+ import { api, isSecret } from "./native";
6
+ import { protect } from "./protect";
7
+
8
+ type SpellIDs = Readonly<Partial<Record<number, true>>>;
9
+
10
+ export interface AuraSlotOptions {
11
+ readonly filter: string;
12
+ readonly frameLevel: number;
13
+ readonly sort?: "ExpirationOnly";
14
+ readonly direction?: "Reverse";
15
+ readonly candidateFilters?: {
16
+ readonly includeSpellIDs?: SpellIDs;
17
+ readonly excludeSpellIDs?: SpellIDs;
18
+ };
19
+ readonly initializeFrame: (this: void, button: AuraButton) => void;
20
+ /** Handle a reread in the host. Return false to use the native update. */
21
+ reread?(this: void, frame: Frame): boolean;
22
+ /** Observe a successful activation. */
23
+ onActivated?(this: void, frame: Frame, unit: string): void;
24
+ }
25
+
26
+ export interface AuraSlot {
27
+ readonly frame: Frame;
28
+ /** Bind and reread even when the new occupant uses the same unit token. */
29
+ activate(this: void, unit: string): Result<void>;
30
+ /** Refresh an active slot without binding its unit again. */
31
+ reread(this: void): Result<void>;
32
+ disable(this: void): Result<void>;
33
+ retire(this: void): Result<void>;
34
+ }
35
+
36
+ interface NativeSlotOptions {
37
+ sortMethod?: number;
38
+ sortDirection?: number;
39
+ candidateFilters?: AuraSlotOptions["candidateFilters"];
40
+ initializeFrame(this: void, button: AuraButton): void;
41
+ }
42
+
43
+ type NativeContainer = Frame & {
44
+ AddAuraSlot?(this: object, key: string, filter: string, options: NativeSlotOptions): void;
45
+ SetUnit(this: object, unit: string): void;
46
+ SetEnabled(this: object, enabled: boolean): void;
47
+ UpdateAllAuras?(this: object): void;
48
+ };
49
+
50
+ type Create = (this: void, kind: "AuraContainer", name: undefined, parent: Frame,
51
+ template: "CustomAuraContainerTemplate") => NativeContainer | undefined;
52
+
53
+ const noUnit = "none";
54
+
55
+ export function createAuraSlot(parent: Frame, options: AuraSlotOptions): Result<AuraSlot> {
56
+ const create = rawField<Create>(api, "CreateFrame");
57
+ if (create === undefined) return fail("Native aura slots are unavailable");
58
+ const created = protect(() => create("AuraContainer", undefined, parent, "CustomAuraContainerTemplate"));
59
+ if (!created.ok) return created;
60
+ if (created.value === undefined) return fail("The client did not create the aura slot");
61
+ const container = created.value;
62
+ const sortMethods = rawField<Partial<Record<NonNullable<AuraSlotOptions["sort"]>, number>>>(api, "AuraContainerSortMethod");
63
+ const sortDirections = rawField<Partial<Record<NonNullable<AuraSlotOptions["direction"]>, number>>>(api, "AuraContainerSortDirection");
64
+
65
+ const { reread: requestReread, onActivated } = options;
66
+
67
+ let active = false;
68
+ let retired = false;
69
+ let initializationFailure: Failure | undefined;
70
+
71
+ function disable(this: void): Result<void> {
72
+ active = false;
73
+ const stopped = protect(() => container.SetEnabled(false));
74
+ const hidden = protect(() => container.Hide());
75
+ return stopped.ok ? hidden : stopped;
76
+ }
77
+
78
+ function retire(this: void): Result<void> {
79
+ retired = true;
80
+ const disabled = disable();
81
+ const released = protect(() => container.SetUnit(noUnit));
82
+ return disabled.ok ? released : disabled;
83
+ }
84
+
85
+ function reread(this: void): Result<void> {
86
+ if (retired) return fail("Aura slot is retired");
87
+ if (!active) return ok(undefined);
88
+
89
+ const handled = requestReread === undefined ? undefined : protect(() => requestReread(container));
90
+ const updated = handled?.ok && handled.value ? ok(undefined) : protect(() => container.UpdateAllAuras?.());
91
+ if (initializationFailure !== undefined) {
92
+ disable();
93
+ return initializationFailure;
94
+ }
95
+ return updated;
96
+ }
97
+
98
+ function activate(this: void, unit: string): Result<void> {
99
+ if (retired) return fail("Aura slot is retired");
100
+ if (isSecret(unit) || unit === "" || unit === noUnit) return fail("Invalid aura unit token");
101
+ if (initializationFailure !== undefined) {
102
+ disable();
103
+ return initializationFailure;
104
+ }
105
+
106
+ active = false;
107
+ const armed = protect(() => {
108
+ container.SetUnit(noUnit);
109
+ container.SetUnit(unit);
110
+ container.SetEnabled(true);
111
+ container.Show();
112
+ });
113
+ if (!armed.ok || initializationFailure !== undefined) {
114
+ disable();
115
+ return initializationFailure ?? armed;
116
+ }
117
+ active = true;
118
+ // Rebinding already refreshed the unit. The additional native update is best effort.
119
+ reread();
120
+ if (initializationFailure !== undefined) return initializationFailure;
121
+ if (onActivated !== undefined) protect(() => onActivated(container, unit));
122
+ return ok(undefined);
123
+ }
124
+
125
+ const idle = disable();
126
+ if (!idle.ok) {
127
+ retire();
128
+ return idle;
129
+ }
130
+ const configured = protect(() => {
131
+ if (container.AddAuraSlot === undefined) return false;
132
+ container.SetAllPoints(parent);
133
+ container.SetFrameLevel(options.frameLevel);
134
+ container.SetUnit(noUnit);
135
+ container.AddAuraSlot("Aura", options.filter, {
136
+ sortMethod: options.sort === undefined ? undefined : sortMethods?.[options.sort],
137
+ sortDirection: options.direction === undefined ? undefined : sortDirections?.[options.direction],
138
+ candidateFilters: options.candidateFilters,
139
+ initializeFrame: button => {
140
+ if (retired || initializationFailure !== undefined) return;
141
+ const result = protect(() => options.initializeFrame(button));
142
+ if (!result.ok) initializationFailure = result;
143
+ },
144
+ });
145
+ return true;
146
+ });
147
+ if (!configured.ok || !configured.value || initializationFailure !== undefined) {
148
+ retire();
149
+ if (!configured.ok) return configured;
150
+ return initializationFailure ?? fail("Native aura slots are unavailable");
151
+ }
152
+ return ok({ frame: container, activate, reread, disable, retire });
153
+ }
@@ -0,0 +1,93 @@
1
+ import { fail, ok, type Result } from "../src/result";
2
+ import type { StatusBar, Texture } from "./frames";
3
+ import { api } from "./native";
4
+ import { protect } from "./protect";
5
+
6
+ type Mask = Parameters<Texture["AddMaskTexture"]>[0];
7
+ type Side = "bottom" | "top" | "left" | "right";
8
+ type Color = readonly [red: number, green: number, blue: number];
9
+
10
+ export interface BarShadeOptions {
11
+ readonly mask?: Mask;
12
+ readonly sublevel?: number;
13
+ }
14
+
15
+ export interface BarShadeStyle {
16
+ readonly shown: boolean;
17
+ readonly side?: Side;
18
+ readonly color?: Color;
19
+ readonly opacity?: number;
20
+ }
21
+
22
+ export interface BarShade {
23
+ readonly texture: Texture;
24
+ setMask(this: void, mask?: Mask): void;
25
+ follow(this: void, fill: Texture): void;
26
+ paint(this: void, style: BarShadeStyle): void;
27
+ }
28
+
29
+ const white = "Interface\\Buttons\\WHITE8X8";
30
+ const black: Color = [0, 0, 0];
31
+
32
+ /** Shade a native fill using its moving edge as the anchor. */
33
+ export function createBarShade(bar: StatusBar, fill: Texture, options: BarShadeOptions = {}): Result<BarShade> {
34
+ let allocated: Texture | undefined;
35
+ const setup = protect((): Result<BarShade> => {
36
+ const texture = bar.CreateTexture(undefined, "ARTWORK", undefined, options.sublevel ?? 7);
37
+ allocated = texture;
38
+ texture.Hide();
39
+ if (texture.SetTexture(white) === false) return fail("The client could not set the bar shade texture");
40
+
41
+ let mask = options.mask;
42
+ if (mask !== undefined) texture.AddMaskTexture(mask);
43
+ let followedFill: Texture | undefined;
44
+ let painted: readonly [Side, number, number, number, number] | undefined;
45
+
46
+ function setMask(this: void, next?: Mask): void {
47
+ if (mask === next) return;
48
+ const previous = mask;
49
+ if (previous !== undefined && !protect(() => texture.RemoveMaskTexture(previous)).ok) return;
50
+
51
+ mask = undefined;
52
+ if (next !== undefined && protect(() => texture.AddMaskTexture(next)).ok) mask = next;
53
+ }
54
+
55
+ function follow(this: void, next: Texture): void {
56
+ if (followedFill === next) return;
57
+ followedFill = undefined;
58
+ texture.ClearAllPoints();
59
+ texture.SetPoint("TOPLEFT", bar, "TOPLEFT");
60
+ texture.SetPoint("BOTTOMRIGHT", next, "BOTTOMRIGHT");
61
+ followedFill = next;
62
+ }
63
+
64
+ function paint(this: void, style: BarShadeStyle): void {
65
+ texture.SetShown(style.shown);
66
+ if (!style.shown) return;
67
+
68
+ const side = style.side ?? "bottom";
69
+ const [r, g, b] = style.color ?? black;
70
+ const opacity = Math.max(0, Math.min(1, style.opacity ?? 0.4));
71
+ if (painted?.[0] === side && painted[1] === r && painted[2] === g && painted[3] === b && painted[4] === opacity) return;
72
+
73
+ const createColor = api.CreateColor;
74
+ if (createColor !== undefined && texture.SetGradient !== undefined) {
75
+ const orientation = side === "top" || side === "bottom" ? "VERTICAL" : "HORIZONTAL";
76
+ // Native gradients run bottom to top or left to right.
77
+ const first = side === "bottom" || side === "left" ? opacity : 0;
78
+ const last = side === "top" || side === "right" ? opacity : 0;
79
+ texture.SetGradient(orientation, createColor(r, g, b, first), createColor(r, g, b, last));
80
+ } else {
81
+ texture.SetVertexColor(r, g, b, opacity / 2);
82
+ }
83
+ painted = [side, r, g, b, opacity];
84
+ }
85
+
86
+ follow(fill);
87
+ return ok({ texture, setMask, follow, paint });
88
+ });
89
+
90
+ if (setup.ok) return setup.value;
91
+ protect(() => allocated?.Hide());
92
+ return setup;
93
+ }
@@ -0,0 +1,216 @@
1
+ import type { CssTokenRecipe } from "../src/css-v1";
2
+ import type { Element } from "../src/elements";
3
+ import type { OwnedReadonlyArray, OwnedRecord } from "../src/owned-collections";
4
+ import type { ReadonlyRef } from "../src/reactivity";
5
+ import { fail, type Failure } from "../src/result";
6
+ import type { RuntimeScope } from "../src/runtime";
7
+ import { bindAbsolutePosition } from "./absolute-position";
8
+ import type { Frame } from "./frames";
9
+ import { readLayoutValue, validateLayoutParent } from "./layout-view";
10
+ import { rawField, setRawField, weakKeys } from "./lua-table";
11
+ import { isSecret, reveal } from "./native";
12
+ import { clearLogicalGroup, mountLogicalGroup, mountViewNode, type LogicalGroup } from "./view-nodes";
13
+
14
+ export type BorrowedSlot = ReadonlyRef<{ readonly frame: Frame; readonly placementOnly?: true } | undefined>;
15
+
16
+ const attachments = weakKeys({});
17
+ const positioning = new Set(["position", "left", "right", "top", "bottom", "margin", "autoMargin"]);
18
+
19
+ function show(frame: Frame): Failure | undefined {
20
+ if (reveal(frame.IsShown()) === true) return;
21
+ frame.Show();
22
+ if (reveal(frame.IsShown()) !== true) return fail("The client did not show the borrowed frame");
23
+ }
24
+
25
+ function hide(frame: Frame): Failure | undefined {
26
+ if (reveal(frame.IsShown()) === false) return;
27
+ frame.Hide();
28
+ if (reveal(frame.IsShown()) !== false) return fail("The client did not hide the borrowed frame");
29
+ }
30
+
31
+ function confirmRightInset(frame: Frame, parent: Frame, inset: number): Failure | undefined {
32
+ const [point, relative, edge, x] = frame.GetPoint(1);
33
+ const ownPoint = reveal(point);
34
+ const offset = reveal(x);
35
+ if (reveal(frame.GetNumPoints()) !== 1 || ownPoint === undefined || !ownPoint.endsWith("RIGHT")
36
+ || reveal(relative) !== parent || reveal(edge) !== ownPoint
37
+ || offset === undefined || !Number.isFinite(offset) || Math.abs(offset + inset) > Math.max(1e-6, Math.abs(inset) * 1e-6)) {
38
+ return fail("The client did not apply the borrowed frame right inset");
39
+ }
40
+ }
41
+
42
+ function rightOnly(recipes?: readonly CssTokenRecipe[]) {
43
+ if (recipes === undefined) return false;
44
+ return (recipes as OwnedReadonlyArray<CssTokenRecipe>).every(recipe => {
45
+ const path = recipe.path as OwnedReadonlyArray<string | number>;
46
+ return path.length === 1 && path[0] === "right";
47
+ });
48
+ }
49
+
50
+ function invalidSide(side: unknown) {
51
+ return side !== undefined && (isSecret(side) || typeof side !== "number" || !Number.isFinite(side));
52
+ }
53
+
54
+ /** Place a caller-owned frame on request. Moving it to another slot cancels the previous layout. */
55
+ export function bindBorrowedSlot(this: void, parent: Frame, source: BorrowedSlot, scope: RuntimeScope, group: LogicalGroup,
56
+ props: Element<"slot">["props"], animated: boolean) {
57
+ const { style, anchor, width, height, fullWidth, opacity, shown, nodeRef } = props;
58
+ const rawRight = style?.right;
59
+ const liveRight = !isSecret(rawRight) && typeof rawRight === "object" && rawRight !== null;
60
+ if (style?.position !== "absolute" || Object.entries(style).some(([key, value]) => value !== undefined && !positioning.has(key)
61
+ && !(key === "styleTokens" && liveRight && rightOnly(style.styleTokens)))
62
+ || animated || anchor !== undefined || props.cssVars !== undefined
63
+ || width !== undefined || height !== undefined || fullWidth || opacity !== undefined || shown !== undefined || nodeRef !== undefined) {
64
+ return fail("A borrowed slot supports only static CSS absolute positioning");
65
+ }
66
+ const { left, right, top, bottom, margin, autoMargin } = style;
67
+ const centered = Array.isArray(autoMargin) ? autoMargin as unknown as readonly [boolean, boolean, boolean, boolean] : undefined;
68
+ if ((liveRight && left !== undefined)
69
+ || invalidSide(left) || (!liveRight && invalidSide(right)) || invalidSide(top) || invalidSide(bottom)
70
+ || (margin !== undefined && margin !== 0 && (!Array.isArray(margin)
71
+ || (margin as unknown as OwnedReadonlyArray<unknown>).some(side => side !== 0)))
72
+ || (autoMargin !== undefined && centered === undefined)
73
+ || (left !== undefined && right !== undefined && !(centered?.[1] && centered[3]))
74
+ || (top !== undefined && bottom !== undefined && !(centered?.[0] && centered[2]))) {
75
+ return fail("A borrowed slot must retain the frame's own dimensions");
76
+ }
77
+ const branch = mountLogicalGroup(scope, group);
78
+ let active: { frame: Frame; owner: RuntimeScope; placementOnly: boolean } | undefined;
79
+ let handled: BorrowedSlot["value"];
80
+ let placedRight: number | undefined;
81
+
82
+ function release(this: void) {
83
+ const previous = active;
84
+ if (previous === undefined) return;
85
+ active = undefined;
86
+ setRawField(attachments, previous.frame, undefined);
87
+ clearLogicalGroup(branch);
88
+ previous.owner.dispose();
89
+ }
90
+
91
+ const cleanup = scope.onDispose(() => {
92
+ const frame = active?.placementOnly === false ? active.frame : undefined;
93
+ release();
94
+ if (frame !== undefined && reveal(frame.GetParent()) === parent) frame.Hide();
95
+ });
96
+ if (!cleanup.ok) return cleanup;
97
+ const observed = scope.watchEffect(() => {
98
+ const request = source.value;
99
+ // A transfer leaves the prior request in the caller's ref but retires this slot's lease.
100
+ if (request === handled && active === undefined) return;
101
+ const nextRight = liveRight && request !== undefined ? readLayoutValue(right) : undefined;
102
+ if (nextRight?.ok === false) return nextRight;
103
+ const inset = nextRight?.value;
104
+ if (nextRight !== undefined && (request?.placementOnly !== true || typeof inset !== "number" || !Number.isFinite(inset))) {
105
+ return fail("A live borrowed slot right inset requires placement-only ownership and a finite number");
106
+ }
107
+ if (request === handled && inset === placedRight) return;
108
+ if (request === undefined) {
109
+ const previous = active;
110
+ if (previous !== undefined && !previous.placementOnly) {
111
+ const { frame } = previous;
112
+ const currentParent = frame.GetParent();
113
+ if (isSecret(currentParent)) return fail("The borrowed frame parent is unreadable");
114
+ if (currentParent === parent) {
115
+ const shown = reveal(frame.IsShown());
116
+ if (shown === undefined) return fail("The borrowed frame visibility is unreadable");
117
+ const hidden = scope.protect(() => hide(frame));
118
+ const failure = hidden.ok ? hidden.value : hidden;
119
+ if (failure !== undefined) {
120
+ if (!shown || active !== previous || reveal(frame.GetParent()) !== parent) return failure;
121
+ const restored = scope.protect(() => show(frame));
122
+ const restoreFailure = restored.ok ? restored.value : restored;
123
+ return restoreFailure === undefined ? failure : fail(failure.reason + ". Could not show the borrowed frame: " + restoreFailure.reason);
124
+ }
125
+ }
126
+ }
127
+ if (active === previous) release();
128
+ handled = request;
129
+ placedRight = undefined;
130
+ return;
131
+ }
132
+ const { frame } = request;
133
+ const anchor = validateLayoutParent(frame, parent);
134
+ if (!anchor.ok) return anchor;
135
+ const previousParent = frame.GetParent();
136
+ if (isSecret(previousParent)) return fail("The borrowed frame parent is unreadable");
137
+ const placementOnly = request.placementOnly === true;
138
+ const retained = liveRight && active?.frame === frame && active.placementOnly && placementOnly && previousParent === parent;
139
+ const previousShown = placementOnly ? undefined : reveal(frame.IsShown());
140
+ if (!placementOnly && previousShown === undefined) return fail("The borrowed frame visibility is unreadable");
141
+ if (active?.frame === frame && active.placementOnly === false && previousParent === parent && previousShown === true) {
142
+ handled = request;
143
+ return;
144
+ }
145
+ type FramePoint = Parameters<Frame["SetPoint"]>;
146
+ const points: { point: FramePoint[0]; relative: FramePoint[1]; edge: FramePoint[2]; x: number | undefined; y: number | undefined }[] = [];
147
+ const count = reveal(frame.GetNumPoints());
148
+ if (count === undefined) return fail("The borrowed frame anchors are unreadable");
149
+ for (let index = 1; index <= count; index++) {
150
+ const [rawPoint, relative, edge, x, y] = frame.GetPoint(index);
151
+ const point = reveal(rawPoint);
152
+ if (point === undefined || isSecret(relative) || isSecret(edge) || isSecret(x) || isSecret(y)) {
153
+ return fail("The borrowed frame anchor is unreadable");
154
+ }
155
+ points.push({ point, relative: reveal(relative) as Frame | undefined, edge: reveal(edge), x: reveal(x), y: reveal(y) });
156
+ }
157
+ const owner = scope.fork();
158
+ const replaced = active?.placementOnly === false && active.frame !== frame ? active.frame : undefined;
159
+ let changedPoints = false;
160
+ let hidReplaced = false;
161
+ const placed = scope.protect(() => {
162
+ if (previousParent !== parent) frame.SetParent(anchor.value);
163
+ if (reveal(frame.GetParent()) !== parent) return fail("The client did not adopt the frame for the native slot");
164
+ changedPoints = true;
165
+ frame.ClearAllPoints();
166
+ const fixed = liveRight ? { ...style as OwnedRecord<typeof style>, right: inset } : style;
167
+ const failure = bindAbsolutePosition(frame, parent, owner, fixed, undefined, undefined)
168
+ ?? (inset === undefined ? undefined : confirmRightInset(frame, parent, inset))
169
+ ?? (placementOnly ? undefined : show(frame));
170
+ if (failure !== undefined) return failure;
171
+ if (!scope.isActive()) return fail("The borrowed slot was disposed during attachment");
172
+ if (reveal(frame.GetParent()) !== parent) return fail("The borrowed frame moved during attachment");
173
+ if (replaced !== undefined && active?.frame === replaced && reveal(replaced.GetParent()) === parent) {
174
+ const shown = reveal(replaced.IsShown());
175
+ if (shown === undefined) return fail("The replaced frame visibility is unreadable");
176
+ if (shown) {
177
+ hidReplaced = true;
178
+ const failure = hide(replaced);
179
+ if (failure !== undefined) return failure;
180
+ }
181
+ }
182
+ if (!scope.isActive()) return fail("The borrowed slot was disposed during replacement");
183
+ });
184
+ const failure = placed.ok ? placed.value : placed;
185
+ if (failure !== undefined) {
186
+ owner.dispose();
187
+ const restored = scope.protect(() => {
188
+ const currentParent = frame.GetParent();
189
+ if (isSecret(currentParent) || currentParent !== previousParent) frame.SetParent(reveal(previousParent) as Frame | undefined);
190
+ const restoredParent = frame.GetParent();
191
+ if (isSecret(restoredParent) || restoredParent !== previousParent) return fail("The original parent was not restored");
192
+ if (changedPoints) {
193
+ frame.ClearAllPoints();
194
+ for (const { point, relative, edge, x, y } of points) frame.SetPoint(point, relative, edge, x, y);
195
+ }
196
+ if (previousShown !== undefined && reveal(frame.IsShown()) !== previousShown) frame.SetShown(previousShown);
197
+ if (hidReplaced && replaced !== undefined && active?.frame === replaced && reveal(replaced.GetParent()) === parent) {
198
+ return show(replaced);
199
+ }
200
+ });
201
+ const restoreFailure = restored.ok ? restored.value : restored;
202
+ return restoreFailure === undefined ? failure : fail(failure.reason + ". Could not restore the borrowed frame: " + restoreFailure.reason);
203
+ }
204
+ handled = request;
205
+ placedRight = inset;
206
+ if (retained) owner.dispose();
207
+ else {
208
+ rawField<(this: void) => void>(attachments, frame)?.();
209
+ release();
210
+ setRawField(attachments, frame, release);
211
+ active = { frame, owner, placementOnly };
212
+ mountViewNode(owner, branch, "slot", frame, props);
213
+ }
214
+ });
215
+ return observed.ok ? undefined : observed;
216
+ }