@overmvp/kumi 0.0.0-stage → 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 (319) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -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 +387 -0
  13. package/builder/css-v1.test.mjs +361 -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 +127 -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 +435 -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 +2336 -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 +381 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +83 -0
  50. package/builder/src/numbers.lua +76 -0
  51. package/builder/src/project-front.mjs +3682 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +124 -0
  54. package/builder/src/setup-store.lua +18 -0
  55. package/builder/src/signal-kernel.fingerprint +1 -0
  56. package/builder/src/strings.lua +103 -0
  57. package/builder/src/ts-lower.mjs +68 -0
  58. package/builder/src/wow-reads.lua +22 -0
  59. package/builder/src/wow_client.rs +253 -0
  60. package/builder/src/wow_types.rs +564 -0
  61. package/builder/style-modules.mjs +93 -0
  62. package/builder/style-states.mjs +100 -0
  63. package/builder/style-states.test.mjs +103 -0
  64. package/builder/vite-assets.mjs +53 -0
  65. package/builder/vite-project.mjs +10 -0
  66. package/config.ts +59 -0
  67. package/eslint.mjs +74 -0
  68. package/form/index.ts +526 -0
  69. package/form/package.json +13 -0
  70. package/motion/index.ts +2 -0
  71. package/motion/package.json +10 -0
  72. package/package.json +26 -4
  73. package/router/index.ts +50 -0
  74. package/router/navigation.ts +22 -0
  75. package/router/package.json +12 -0
  76. package/router/router-view.ts +134 -0
  77. package/router/routes.ts +64 -0
  78. package/scripts/addon-build.mjs +364 -0
  79. package/scripts/addon-project.mjs +78 -0
  80. package/scripts/kumi-cli.mjs +105 -0
  81. package/scripts/link-addons.mjs +61 -0
  82. package/src/bindings.ts +35 -0
  83. package/src/context.ts +15 -0
  84. package/src/css-tokens.ts +239 -0
  85. package/src/css-v1.ts +68 -0
  86. package/src/cvars.ts +91 -0
  87. package/src/element-node.ts +32 -0
  88. package/src/elements.ts +505 -0
  89. package/src/feature-scope.ts +82 -0
  90. package/src/flex-layout.ts +807 -0
  91. package/src/frame-scheduler.ts +336 -0
  92. package/src/grid-tracks.ts +165 -0
  93. package/src/image-imports.d.ts +24 -0
  94. package/src/images.ts +79 -0
  95. package/src/index.ts +29 -0
  96. package/src/jsx-dev-runtime.ts +2 -0
  97. package/src/jsx-runtime.ts +29 -0
  98. package/src/jsx.d.ts +12 -0
  99. package/src/keyed-list.ts +366 -0
  100. package/src/media-query.ts +144 -0
  101. package/src/module-ui.ts +147 -0
  102. package/src/module.ts +311 -0
  103. package/src/node-selectors.ts +114 -0
  104. package/src/owned-collections.ts +52 -0
  105. package/src/providers.ts +241 -0
  106. package/src/reactive-scope.ts +34 -0
  107. package/src/reactivity.ts +275 -0
  108. package/src/result.ts +64 -0
  109. package/src/runtime.ts +52 -0
  110. package/src/scheduler.ts +185 -0
  111. package/src/signal-kernel.ts +63 -0
  112. package/src/style-classes.ts +51 -0
  113. package/src/style-states.ts +134 -0
  114. package/src/style-variables.ts +53 -0
  115. package/src/text-children.ts +42 -0
  116. package/src/theme.ts +50 -0
  117. package/src/transition.ts +18 -0
  118. package/state/index.ts +9 -0
  119. package/state/package.json +12 -0
  120. package/state/setup-store-persistence.ts +206 -0
  121. package/state/setup-store-view.ts +10 -0
  122. package/state/setup-store.ts +342 -0
  123. package/state/store-bindings.ts +25 -0
  124. package/state/store.ts +84 -0
  125. package/styles/_layers.scss +14 -0
  126. package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
  127. package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
  128. package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
  129. package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
  130. package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
  131. package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
  132. package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
  133. package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
  134. package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
  135. package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
  136. package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
  137. package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
  138. package/templates/addon/AGENTS.md +34 -0
  139. package/templates/addon/Assets/Art/Corner10.tga +0 -0
  140. package/templates/addon/Assets/Art/Corner11.tga +0 -0
  141. package/templates/addon/Assets/Art/Corner12.tga +0 -0
  142. package/templates/addon/Assets/Art/Corner13.tga +0 -0
  143. package/templates/addon/Assets/Art/Corner14.tga +0 -0
  144. package/templates/addon/Assets/Art/Corner15.tga +0 -0
  145. package/templates/addon/Assets/Art/Corner16.tga +0 -0
  146. package/templates/addon/Assets/Art/Corner2.tga +0 -0
  147. package/templates/addon/Assets/Art/Corner3.tga +0 -0
  148. package/templates/addon/Assets/Art/Corner4.tga +0 -0
  149. package/templates/addon/Assets/Art/Corner5.tga +0 -0
  150. package/templates/addon/Assets/Art/Corner6.tga +0 -0
  151. package/templates/addon/Assets/Art/Corner7.tga +0 -0
  152. package/templates/addon/Assets/Art/Corner8.tga +0 -0
  153. package/templates/addon/Assets/Art/Corner9.tga +0 -0
  154. package/templates/addon/Assets/Art/Dot.tga +0 -0
  155. package/templates/addon/Assets/Art/Gear.tga +0 -0
  156. package/templates/addon/Assets/Art/Grip.tga +0 -0
  157. package/templates/addon/Assets/Art/Outline10.tga +0 -0
  158. package/templates/addon/Assets/Art/Outline12.tga +0 -0
  159. package/templates/addon/Assets/Art/Outline13.tga +0 -0
  160. package/templates/addon/Assets/Art/Outline14.tga +0 -0
  161. package/templates/addon/Assets/Art/Outline15.tga +0 -0
  162. package/templates/addon/Assets/Art/Outline2.tga +0 -0
  163. package/templates/addon/Assets/Art/Outline3.tga +0 -0
  164. package/templates/addon/Assets/Art/Outline5.tga +0 -0
  165. package/templates/addon/Assets/Art/Outline7.tga +0 -0
  166. package/templates/addon/Assets/Art/Outline9.tga +0 -0
  167. package/templates/addon/Assets/Art/Round11.tga +0 -0
  168. package/templates/addon/Assets/Art/Round11Border.tga +0 -0
  169. package/templates/addon/Assets/Art/Round4.tga +0 -0
  170. package/templates/addon/Assets/Art/Round4Border.tga +0 -0
  171. package/templates/addon/Assets/Art/Round6.tga +0 -0
  172. package/templates/addon/Assets/Art/Round6Border.tga +0 -0
  173. package/templates/addon/Assets/Art/Round8.tga +0 -0
  174. package/templates/addon/Assets/Art/Round8Border.tga +0 -0
  175. package/templates/addon/Assets/Art/Search.tga +0 -0
  176. package/templates/addon/CLAUDE.md +1 -0
  177. package/templates/addon/README.md +49 -0
  178. package/templates/addon/eslint.config.mjs +6 -0
  179. package/templates/addon/gitignore +9 -0
  180. package/templates/addon/kumi.config.ts +17 -0
  181. package/templates/addon/package.json +20 -0
  182. package/templates/addon/src/app/app.module.ts +18 -0
  183. package/templates/addon/src/app/commands.store.ts +11 -0
  184. package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
  185. package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
  186. package/templates/addon/src/app/main.ts +5 -0
  187. package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
  188. package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
  189. package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
  190. package/templates/addon/src/app/theme.ts +6 -0
  191. package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
  192. package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
  193. package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
  194. package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
  195. package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
  196. package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
  197. package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
  198. package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
  199. package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
  200. package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
  201. package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
  202. package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
  203. package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
  204. package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
  205. package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
  206. package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
  207. package/templates/addon/src/shared/api/native-art.ts +28 -0
  208. package/templates/addon/src/shared/assets/close.png +0 -0
  209. package/templates/addon/src/shared/lib/format.ts +20 -0
  210. package/templates/addon/src/shared/styles/_tokens.scss +12 -0
  211. package/templates/addon/src/shared/ui/Button.tsx +29 -0
  212. package/templates/addon/src/shared/ui/Choice.tsx +34 -0
  213. package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
  214. package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
  215. package/templates/addon/src/shared/ui/Slider.tsx +25 -0
  216. package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
  217. package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
  218. package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
  219. package/templates/addon/tsconfig.json +63 -0
  220. package/translate/index.ts +14 -0
  221. package/translate/package.json +9 -0
  222. package/utils/colors.ts +76 -0
  223. package/utils/format.ts +37 -0
  224. package/utils/index.ts +4 -0
  225. package/utils/number.ts +17 -0
  226. package/utils/package.json +7 -0
  227. package/utils/text.ts +6 -0
  228. package/validate/index.ts +119 -0
  229. package/validate/package.json +9 -0
  230. package/wow/absolute-position.ts +142 -0
  231. package/wow/action-bar-layout.ts +108 -0
  232. package/wow/action-button.ts +219 -0
  233. package/wow/action-catalog.ts +98 -0
  234. package/wow/addon-manifest.ts +36 -0
  235. package/wow/animation.ts +428 -0
  236. package/wow/app-ui.ts +221 -0
  237. package/wow/aura-button.ts +76 -0
  238. package/wow/aura-container.ts +591 -0
  239. package/wow/aura-countdown.ts +201 -0
  240. package/wow/aura-slot.ts +153 -0
  241. package/wow/bar-shade.ts +93 -0
  242. package/wow/borrowed-slot.ts +216 -0
  243. package/wow/button-drag.ts +48 -0
  244. package/wow/cast-clock.ts +153 -0
  245. package/wow/cast-text.ts +83 -0
  246. package/wow/class-colors.ts +126 -0
  247. package/wow/clock.ts +7 -0
  248. package/wow/color-picker.ts +128 -0
  249. package/wow/commands.ts +17 -0
  250. package/wow/contained-frame.ts +128 -0
  251. package/wow/contract.ts +42 -0
  252. package/wow/cooldown-timing.ts +24 -0
  253. package/wow/css-box.ts +144 -0
  254. package/wow/css-color.ts +12 -0
  255. package/wow/css-style.ts +109 -0
  256. package/wow/css-surfaces.ts +603 -0
  257. package/wow/css-text.ts +399 -0
  258. package/wow/cursor.ts +45 -0
  259. package/wow/cvar-schema.ts +32 -0
  260. package/wow/cvars.ts +98 -0
  261. package/wow/dropdown-art.ts +39 -0
  262. package/wow/element-click.ts +20 -0
  263. package/wow/element-event-coordinator.ts +219 -0
  264. package/wow/element-events.ts +317 -0
  265. package/wow/event-dispatcher.ts +151 -0
  266. package/wow/events.ts +50 -0
  267. package/wow/font-cache.ts +92 -0
  268. package/wow/frame-access.ts +53 -0
  269. package/wow/frame-layer.ts +5 -0
  270. package/wow/frame-timing.ts +13 -0
  271. package/wow/frames.ts +236 -0
  272. package/wow/heal-prediction.ts +305 -0
  273. package/wow/hooks.ts +133 -0
  274. package/wow/image-layers.ts +55 -0
  275. package/wow/index.ts +111 -0
  276. package/wow/layout-view.ts +663 -0
  277. package/wow/locale-font.ts +15 -0
  278. package/wow/lua-string.ts +18 -0
  279. package/wow/lua-table.ts +18 -0
  280. package/wow/macro-book.ts +84 -0
  281. package/wow/map-position.ts +27 -0
  282. package/wow/media-query.ts +65 -0
  283. package/wow/module-events.ts +11 -0
  284. package/wow/native-hooks.ts +168 -0
  285. package/wow/native-placement.ts +88 -0
  286. package/wow/native.ts +153 -0
  287. package/wow/number-format.ts +8 -0
  288. package/wow/number-input.ts +10 -0
  289. package/wow/panel-host.ts +97 -0
  290. package/wow/pixel.ts +35 -0
  291. package/wow/plugins.ts +246 -0
  292. package/wow/protect.ts +48 -0
  293. package/wow/protected-layout.ts +133 -0
  294. package/wow/protected-view.ts +103 -0
  295. package/wow/range-art.ts +81 -0
  296. package/wow/readable-api.ts +120 -0
  297. package/wow/reparent-frame.ts +99 -0
  298. package/wow/rounded-texture.ts +183 -0
  299. package/wow/saved-variables.ts +109 -0
  300. package/wow/scroll-view.ts +64 -0
  301. package/wow/secret-ref.ts +14 -0
  302. package/wow/secure-bindings.ts +89 -0
  303. package/wow/secure-clicks.ts +432 -0
  304. package/wow/secure-snippet.ts +235 -0
  305. package/wow/secure-state.ts +130 -0
  306. package/wow/secure-target-visibility.ts +89 -0
  307. package/wow/secure-transaction.ts +331 -0
  308. package/wow/special-window.ts +269 -0
  309. package/wow/spell-cooldown.ts +36 -0
  310. package/wow/start-addon.ts +122 -0
  311. package/wow/state-paint.ts +256 -0
  312. package/wow/transition.ts +86 -0
  313. package/wow/unit-button.ts +39 -0
  314. package/wow/unit-cast.ts +92 -0
  315. package/wow/unit-health-text.ts +47 -0
  316. package/wow/view-nodes.ts +304 -0
  317. package/wow/view.ts +1864 -0
  318. package/wow/visible-frame-job.ts +34 -0
  319. package/wow/width-reserve.ts +108 -0
@@ -0,0 +1,122 @@
1
+ import { createFrameDriver, type FrameDriver } from "../src/frame-scheduler";
2
+ import { createModuleContainer, moduleInput, type ModuleContainer, type ModuleInput, type ModuleReference } from "../src/module";
3
+ import { ref, type ReadonlyRef } from "../src/reactivity";
4
+ import { fail, formatFailure, ok, type Failure, type Result } from "../src/result";
5
+ import { createRuntime, type Runtime, type RuntimeScope } from "../src/runtime";
6
+ import { clock } from "./clock";
7
+ import type { EventApi } from "./contract";
8
+ import { createEventDispatcher } from "./event-dispatcher";
9
+ import { rawField } from "./lua-table";
10
+ import { moduleEvents } from "./module-events";
11
+ import { api, reveal } from "./native";
12
+ import { protect } from "./protect";
13
+ import { savedVariablesStorage, type SavedValue } from "./saved-variables";
14
+ import { activateStoreHost } from "../state/setup-store";
15
+ import { addonManifest } from "./addon-manifest";
16
+ import { startPlugins } from "./plugins";
17
+ import { installAppUi, type AppTheme } from "./app-ui";
18
+
19
+ import { frameDriver, frameTiming } from "./frame-timing";
20
+
21
+ export { frameDriver, frameTiming };
22
+
23
+ /** The runtime of the addon that resolved a provider. */
24
+ export const addonRuntime = moduleInput<Runtime>("kumi/wow/addon-runtime");
25
+
26
+ export interface AddonContext {
27
+ readonly runtime: Runtime;
28
+ readonly scope: RuntimeScope;
29
+ readonly modules: ModuleContainer;
30
+ readonly events: EventApi;
31
+ }
32
+
33
+ export interface AddonStartOptions {
34
+ /** Immediate activation is for hosts that manage their own lifecycle. */
35
+ readonly phase?: "PLAYER_LOGIN" | "immediate";
36
+ readonly modules?: ModuleContainer;
37
+ readonly driver?: FrameDriver;
38
+ readonly prepare?: (this: void, context: AddonContext) => void;
39
+ readonly onError?: (this: void, failure: Failure) => void;
40
+ /**
41
+ * SavedVariables globals for persisted stores that pass no storage. Each store saves under its ID.
42
+ * Defaults to `addon.savedVariables` and `addon.savedVariablesPerCharacter` from kumi.config.ts.
43
+ */
44
+ readonly savedVariables?: { readonly account?: string; readonly character?: string };
45
+ /** Textures, fonts, control skins and breakpoints for every view and window Kumi mounts. */
46
+ readonly theme?: AppTheme;
47
+ }
48
+
49
+ export interface AddonStart<Exports extends object> extends AddonContext {
50
+ /** Undefined while waiting for login. Retains the result after activation. */
51
+ readonly activation: ReadonlyRef<Result<Readonly<Exports>> | undefined>;
52
+ readonly dispose: RuntimeScope["dispose"];
53
+ }
54
+
55
+ /** Compose one native runtime and activate the root module at the selected lifecycle phase. */
56
+ export function startAddon<Exports extends object>(this: void,
57
+ root: Exclude<ModuleReference<Exports>, ModuleInput<Exports>>, options: AddonStartOptions = {},
58
+ ): Result<AddonStart<Exports>> {
59
+ const existing = options.modules;
60
+ if (existing !== undefined && existing.resolve(addonRuntime).ok === true) return fail("The module container already has an addon runtime");
61
+ const report = options.onError ?? (failure => api.geterrorhandler()(formatFailure(failure)));
62
+ const runtime = createRuntime(clock, report, protect, { driver: options.driver ?? frameDriver });
63
+ const scope = runtime.scope();
64
+ const modules = options.modules ?? createModuleContainer(protect, { forkScope: scope.fork });
65
+ activateStoreHost(modules.stores);
66
+ const manifest = addonManifest();
67
+ const account = options.savedVariables?.account ?? manifest?.savedVariables;
68
+ const character = options.savedVariables?.character ?? manifest?.savedVariablesPerCharacter;
69
+ if (account !== undefined || character !== undefined) {
70
+ modules.stores.storage = (id, defaults, perCharacter) => {
71
+ const global = perCharacter ? character : account;
72
+ return global === undefined ? undefined : savedVariablesStorage({
73
+ global, key: id, defaults: defaults as Record<string, SavedValue>,
74
+ }) as never;
75
+ };
76
+ }
77
+ const registered = modules.resolve(moduleEvents);
78
+ const events = registered.ok ? registered.value : createEventDispatcher();
79
+ const context = { runtime, scope, modules, events };
80
+ installAppUi(runtime, scope, events, options.theme ?? {});
81
+ const activation = ref<Result<Readonly<Exports>> | undefined>(undefined);
82
+
83
+ function release(failure: Failure): Failure {
84
+ const cleaned = scope.dispose();
85
+ return cleaned.ok ? failure : fail(`${failure.reason}\nAddon cleanup: ${cleaned.reason}`);
86
+ }
87
+
88
+ modules.bind(addonRuntime, runtime);
89
+ if (!registered.ok) modules.bind(moduleEvents, events);
90
+ const prepared = protect(() => options.prepare?.(context));
91
+ if (!prepared.ok) return release(prepared);
92
+
93
+ function activate(): Result<Readonly<Exports>> {
94
+ const wake = events.on(scope, "PLAYER_REGEN_ENABLED", runtime.frameJobs.combatChanged);
95
+ const result = wake.ok ? modules.resolve(root) : wake;
96
+ const settled = result.ok ? result : release(result);
97
+ if (settled.ok) startPlugins(modules, scope, report);
98
+ activation.value = settled;
99
+ return settled;
100
+ }
101
+
102
+ if (options.phase === "immediate" || reveal(api.IsLoggedIn?.()) === true) {
103
+ const started = activate();
104
+ if (!started.ok) return started;
105
+ } else {
106
+ const waiting = scope.fork();
107
+ const login = events.on(waiting, "PLAYER_LOGIN", () => {
108
+ if (!waiting.isActive()) return;
109
+ const closed = waiting.dispose();
110
+ if (closed.ok) {
111
+ const started = activate();
112
+ if (!started.ok) report(started);
113
+ } else {
114
+ const failure = release(closed);
115
+ activation.value = failure;
116
+ report(failure);
117
+ }
118
+ });
119
+ if (!login.ok) return release(login);
120
+ }
121
+ return ok({ ...context, activation, dispose: scope.dispose });
122
+ }
@@ -0,0 +1,256 @@
1
+ import { createColorTransitions, type ColorTransitions } from "../animation/color-transition";
2
+ import type { Ease } from "../animation/spec";
3
+ import { resolveCssTokens } from "../src/css-tokens";
4
+ import type { Binding, NativeStyle } from "../src/elements";
5
+ import type { OwnedReadonlyArray } from "../src/owned-collections";
6
+ import { ref, type ReadonlyRef } from "../src/reactivity";
7
+ import { ok, type Failure, type Result } from "../src/result";
8
+ import type { RuntimeScope } from "../src/runtime";
9
+ import { prepareStyleStates, type StatePaint, type StateStyleVariant, type StyleState } from "../src/style-states";
10
+ import { resolveStyleValue, styleValue, type StyleEnvironment } from "../src/style-variables";
11
+ import type { TransitionOptions } from "../src/transition";
12
+ import type { RGBA } from "../utils/colors";
13
+ import { readCssColor } from "./css-color";
14
+ import type { ElementStyleInput } from "./element-events";
15
+ import { borderSides, type CssSurface } from "./css-surfaces";
16
+ import type { ReadCssStyle } from "./css-style";
17
+ import type { Frame, FrameParent, Texture } from "./frames";
18
+ import { api, isSecret, reveal, type LuaMultiReturn } from "./native";
19
+
20
+ export interface NativePaintState extends ElementStyleInput {
21
+ readonly style: NativeStyle;
22
+ readonly usesOpacity: boolean;
23
+ readonly usesColor: boolean;
24
+ readonly usesBackgroundColor: boolean;
25
+ readonly usesBackgroundImage: boolean;
26
+ readonly usesBorderColor: boolean;
27
+ current(this: void): Readonly<StatePaint>;
28
+ evaluate(this: void): Result<Readonly<StatePaint>>;
29
+ isActive(this: void, state: StyleState): boolean;
30
+ colorChange?(this: void): TextColorStateChange | undefined;
31
+ readonly repaint?: ReadonlyRef<number>;
32
+ }
33
+
34
+ export interface TextColorStateChange {
35
+ readonly state: StyleState;
36
+ readonly active: boolean;
37
+ }
38
+
39
+ export interface NativeButtonPaintMotion {
40
+ readonly repaint: ReadonlyRef<number>;
41
+ readonly maxStep?: number;
42
+ duration(this: void, change: TextColorStateChange): number;
43
+ }
44
+
45
+ export interface ButtonPaintTiming extends NativeButtonPaintMotion {
46
+ readonly change?: NativePaintState["colorChange"];
47
+ visible(this: void): boolean;
48
+ }
49
+
50
+ type ColorReader = (this: void) => LuaMultiReturn<[unknown, unknown, unknown, unknown]>;
51
+ type ColorWriter = (this: void, red: number, green: number, blue: number, alpha: number) => void;
52
+
53
+ export interface NativeStateColor {
54
+ readonly base?: Binding<string>;
55
+ readonly fallback?: (this: void) => Result<string | undefined>;
56
+ readonly read?: ColorReader;
57
+ readonly write: ColorWriter;
58
+ readonly restore?: (this: void) => void;
59
+ }
60
+
61
+ type NativeStateSurface = { parent: Frame; anchor: FrameParent; css?: CssSurface; base?: NativeStyle };
62
+ type ThemePaint = {
63
+ environment?: StyleEnvironment;
64
+ motion?: TransitionOptions;
65
+ reduceMotion?: (this: void) => boolean;
66
+ liveStyle?: ReadCssStyle;
67
+ buttonTiming?: ButtonPaintTiming;
68
+ visible?: (this: void) => boolean;
69
+ };
70
+ type Color = Readonly<RGBA>;
71
+ const transparent: Color = [0, 0, 0, 0];
72
+ const white: Color = [1, 1, 1, 1];
73
+ const empty: Readonly<StatePaint> = {};
74
+
75
+ export function createNativePaintState(this: void, style: NativeStyle | undefined, environment?: StyleEnvironment,
76
+ trackChanges = false, repaint?: ReadonlyRef<number>): NativePaintState | undefined {
77
+ const variants = style?.stateStyles as OwnedReadonlyArray<StateStyleVariant> | undefined;
78
+ if (style === undefined || variants === undefined || variants.length === 0) return;
79
+ const state = prepareStyleStates({}, variants);
80
+ const current = ref(state.current());
81
+ const change = trackChanges || repaint !== undefined ? ref<TextColorStateChange | undefined>(undefined) : undefined;
82
+ return {
83
+ style,
84
+ usesOpacity: variants.some(({ paint }) => paint.opacity !== undefined),
85
+ usesColor: variants.some(({ paint }) => paint.color !== undefined),
86
+ usesBackgroundColor: style.backgroundColor !== undefined || variants.some(({ paint }) => paint.backgroundColor !== undefined),
87
+ usesBackgroundImage: variants.some(({ paint }) => paint.backgroundImage !== undefined),
88
+ usesBorderColor: variants.some(({ paint }) => paint.borderColor !== undefined),
89
+ needs: state.needs,
90
+ isActive: state.isActive,
91
+ repaint,
92
+ set(condition, active) {
93
+ const flagChanged = repaint !== undefined && state.needs(condition) && state.isActive(condition) !== active;
94
+ const paint = state.set(condition, active);
95
+ if (change !== undefined && (flagChanged || paint !== undefined)) change.value = { state: condition, active };
96
+ if (paint !== undefined) current.value = paint;
97
+ },
98
+ current: () => current.value,
99
+ evaluate: () => resolveCssTokens(current.value, environment),
100
+ colorChange: change === undefined ? undefined : () => change.value,
101
+ };
102
+ }
103
+
104
+ function readColor(read: ColorReader): Color {
105
+ const [rawR, rawG, rawB, rawA] = read();
106
+ const r = reveal(rawR), g = reveal(rawG), b = reveal(rawB), a = reveal(rawA);
107
+ return typeof r === "number" && typeof g === "number" && typeof b === "number" && typeof a === "number" ? [r, g, b, a] : white;
108
+ }
109
+
110
+ function easing(motion?: TransitionOptions): Ease {
111
+ if (motion?.timing === "ease-in") return "in";
112
+ if (motion?.timing === "ease-in-out") return "inOut";
113
+ return motion?.timing === "linear" ? "linear" : "out";
114
+ }
115
+
116
+ export function bindColorChannel(this: void, scope: RuntimeScope,
117
+ read: (this: void) => Result<Binding<string> | undefined>, fallback: Color | undefined,
118
+ write: ColorWriter, options: ThemePaint, restore?: (this: void) => void): Failure | undefined {
119
+ const key = {};
120
+ const duration = options.motion === undefined ? 0 : options.motion.duration ?? 0.18;
121
+ const timing = options.buttonTiming;
122
+ const ease = timing === undefined ? easing(options.motion) : "out";
123
+ let current: Color | undefined;
124
+ let previous: string | undefined;
125
+ let previousChange: TextColorStateChange | undefined;
126
+ let previousRepaint: number | undefined;
127
+ function seconds(this: void, change?: TextColorStateChange) {
128
+ return timing !== undefined && change !== undefined && timing.visible() && options.visible?.() !== false
129
+ && options.reduceMotion?.() !== true ? timing.duration(change) : 0;
130
+ }
131
+ const transition: ColorTransitions<object> | undefined = duration > 0 || timing !== undefined ? createColorTransitions(scope, {
132
+ now: () => api.GetTime(),
133
+ maxStep: timing?.maxStep ?? 0.05,
134
+ paint: (_, value) => {
135
+ const finish = timing !== undefined && transition?.running(key) === true && seconds(previousChange) <= 0;
136
+ if (finish) transition?.cancel(key);
137
+ const [r, g, b, a] = finish ? current ?? value : value;
138
+ write(r, g, b, a);
139
+ },
140
+ }) : undefined;
141
+ const observed = scope.watchEffect(() => {
142
+ const change = timing?.change?.();
143
+ const changed = change !== undefined && previousChange !== change;
144
+ const repaint = timing?.repaint.value;
145
+ const force = previousRepaint !== undefined && repaint !== previousRepaint;
146
+ previousChange = change;
147
+ previousRepaint = repaint;
148
+ const authored = read();
149
+ if (!authored.ok) return authored;
150
+ let literal: string | undefined;
151
+ if (authored.value !== undefined) {
152
+ const value = styleValue(authored.value, isSecret);
153
+ if (!value.ok) return value;
154
+ if (value.value !== undefined) {
155
+ const resolved = resolveStyleValue(value.value, options.environment);
156
+ if (!resolved.ok) return resolved;
157
+ literal = reveal(resolved.value);
158
+ }
159
+ }
160
+ if (current !== undefined && previous === literal && !changed && !force) return;
161
+ const next = (literal === undefined ? undefined : readCssColor(literal)) ?? fallback;
162
+ if (next === undefined) {
163
+ transition?.cancel(key);
164
+ if (current !== undefined) restore?.();
165
+ current = undefined;
166
+ previous = undefined;
167
+ return;
168
+ }
169
+ const interval = timing === undefined ? duration : seconds(change);
170
+ if (transition !== undefined && current !== undefined && !force && interval > 0 && options.reduceMotion?.() !== true
171
+ && (timing === undefined || changed || transition.running(key))) {
172
+ const failure = timing !== undefined && !changed ? transition.retarget(key, next) : transition.start(key, current, next, interval, ease);
173
+ if (failure !== undefined) return failure;
174
+ } else {
175
+ transition?.cancel(key);
176
+ write(next[0], next[1], next[2], next[3]);
177
+ }
178
+ current = next;
179
+ previous = literal;
180
+ });
181
+ return observed.ok ? undefined : observed;
182
+ }
183
+
184
+ function layer(surface: NativeStateSurface, level: number): Texture {
185
+ const texture = surface.parent.CreateTexture(undefined, "BACKGROUND", undefined, level);
186
+ texture.SetAllPoints(surface.anchor);
187
+ return texture;
188
+ }
189
+
190
+ export function bindStatePaint(this: void, scope: RuntimeScope, state: NativePaintState | undefined, options: ThemePaint,
191
+ surface?: NativeStateSurface, color?: NativeStateColor): Failure | undefined {
192
+ if (color !== undefined) {
193
+ const failure = bindColorChannel(scope, () => {
194
+ if (state?.usesColor === true) {
195
+ const appearance = state.evaluate();
196
+ if (!appearance.ok) return appearance;
197
+ if (appearance.value.color !== undefined) return ok(appearance.value.color);
198
+ }
199
+ return color.fallback?.() ?? ok(color.base ?? state?.style.color);
200
+ }, color.read === undefined ? undefined : readColor(color.read), color.write, options, color.restore);
201
+ if (failure !== undefined) return failure;
202
+ }
203
+ if (surface === undefined) return;
204
+ const base = surface.base ?? state?.style;
205
+ const border = surface.css?.borderPaint;
206
+ if (state?.usesBorderColor === true && border !== undefined && base?.border !== undefined) {
207
+ const appearance = scope.computedResult(state.evaluate, "CSS border state");
208
+ const sides = (border.sides ?? borderSides) as OwnedReadonlyArray<(typeof borderSides)[number]>;
209
+ for (const side of sides) {
210
+ if (base.border[side] === undefined) continue;
211
+ const failure = bindColorChannel(scope, () => {
212
+ const baseColor = border.read(side);
213
+ if (baseColor === undefined) return ok(undefined);
214
+ const current = appearance.value;
215
+ return current.ok ? ok(reveal(current.value.borderColor) ?? baseColor) : current;
216
+ }, undefined, (r, g, b, a) => border.write(side, r, g, b, a), options);
217
+ if (failure !== undefined) return failure;
218
+ }
219
+ }
220
+ if (state?.usesBackgroundColor === true || surface.base?.backgroundColor !== undefined) {
221
+ const background = surface.css === undefined ? layer(surface, -8) : undefined;
222
+ const failure = bindColorChannel(scope, () => {
223
+ const appearance = state?.evaluate() ?? ok(empty);
224
+ if (!appearance.ok) return appearance;
225
+ return ok(appearance.value.backgroundColor ?? (options.liveStyle?.() ?? base)?.backgroundColor);
226
+ }, transparent, surface.css?.setBackgroundColor ?? ((r, g, b, a) => {
227
+ background!.SetColorTexture(r, g, b, a);
228
+ background!.SetShown(a > 0);
229
+ }), options);
230
+ if (failure !== undefined) return failure;
231
+ }
232
+ if (state?.usesBackgroundImage === true || surface.base?.backgroundImage !== undefined) {
233
+ const background = surface.css === undefined ? layer(surface, -7) : undefined;
234
+ let previous: StatePaint["backgroundImage"];
235
+ background?.Hide();
236
+ const observed = scope.watchEffect(() => {
237
+ const appearance = state?.evaluate() ?? ok(empty);
238
+ if (!appearance.ok) return appearance;
239
+ const image = appearance.value.backgroundImage ?? (options.liveStyle?.() ?? base)?.backgroundImage;
240
+ if (image === previous) return;
241
+ previous = image;
242
+ if (surface.css !== undefined) {
243
+ surface.css.setBackgroundImage(image);
244
+ return;
245
+ }
246
+ if (background === undefined) return;
247
+ if (image === undefined) background.Hide();
248
+ else if ("path" in image) background.SetShown(background.SetTexture(image.path.split("/").join("\\")));
249
+ else {
250
+ background.SetAtlas(image.atlas, false);
251
+ background.Show();
252
+ }
253
+ });
254
+ if (!observed.ok) return observed;
255
+ }
256
+ }
@@ -0,0 +1,86 @@
1
+ import type { RuntimeScope } from "../src/runtime";
2
+ import { transitionProgress, type TransitionOptions } from "../src/transition";
3
+ import { createFrame, type FrameParent } from "./frames";
4
+ import { protect } from "./protect";
5
+
6
+ type Region = {
7
+ SetAlpha(this: object, alpha: number): void;
8
+ SetShown(this: object, shown: boolean): void;
9
+ };
10
+
11
+ export function animateVisibility(region: Region, parent: FrameParent, scope: RuntimeScope, motion: TransitionOptions,
12
+ setOffset: (this: void, offset: number) => void) {
13
+ const duration = motion.duration ?? 0.18;
14
+ let driver: ReturnType<typeof createFrame<"Frame">> | undefined;
15
+ let desired: boolean | undefined;
16
+ let running = false;
17
+ let alpha = 1;
18
+ let offset = 0;
19
+
20
+ function draw(opacity: number, y: number) {
21
+ if (alpha !== opacity) region.SetAlpha(Math.max(0, Math.min(1, opacity)));
22
+ if (offset !== y) setOffset(y);
23
+ alpha = opacity;
24
+ offset = y;
25
+ }
26
+
27
+ scope.onDispose(() => {
28
+ running = false;
29
+ driver?.SetScript("OnUpdate", undefined);
30
+ });
31
+
32
+ return (shown: boolean) => {
33
+ if (desired === shown) return;
34
+ const wasHidden = desired !== true && !running;
35
+ desired = shown;
36
+ const edgeAlpha = (shown ? motion.enter : motion.exit) === "none" ? 1 : 0;
37
+ const edgeY = (shown ? motion.enterOffset : motion.exitOffset) ?? 0;
38
+ const toAlpha = shown ? 1 : edgeAlpha;
39
+ const toY = shown ? 0 : edgeY;
40
+ if (duration <= 0 || (!shown && wasHidden)) {
41
+ region.SetShown(shown);
42
+ draw(toAlpha, toY);
43
+ return;
44
+ }
45
+ if (shown && wasHidden) draw(edgeAlpha, edgeY);
46
+ if (alpha === toAlpha && offset === toY) {
47
+ running = false;
48
+ driver?.SetScript("OnUpdate", undefined);
49
+ region.SetShown(shown);
50
+ return;
51
+ }
52
+ region.SetShown(true);
53
+ driver ??= createFrame("Frame", undefined, parent);
54
+ const fromAlpha = alpha;
55
+ const fromY = offset;
56
+ let elapsed = 0;
57
+ running = true;
58
+ function finish() {
59
+ driver?.SetScript("OnUpdate", undefined);
60
+ running = false;
61
+ draw(toAlpha, toY);
62
+ region.SetShown(shown);
63
+ }
64
+ driver.SetScript("OnUpdate", (_, delta) => {
65
+ elapsed += delta;
66
+ if (elapsed >= duration) {
67
+ finish();
68
+ return;
69
+ }
70
+ let progress = elapsed / duration;
71
+ const timing = motion.timing;
72
+ if (typeof timing === "function") {
73
+ const result = protect(() => transitionProgress(timing, progress));
74
+ if (!result.ok) {
75
+ finish();
76
+ scope.nextTick(() => result);
77
+ return;
78
+ }
79
+ progress = result.value;
80
+ } else {
81
+ progress = transitionProgress(timing, progress);
82
+ }
83
+ draw(fromAlpha + (toAlpha - fromAlpha) * progress, fromY + (toY - fromY) * progress);
84
+ });
85
+ };
86
+ }
@@ -0,0 +1,39 @@
1
+ import { fail, type Result } from "../src/result";
2
+ import { createFrame, type Button, type FrameParent } from "./frames";
3
+ import { api, isSecret } from "./native";
4
+ import { protect } from "./protect";
5
+
6
+ export interface SecureUnitButtonOptions {
7
+ unit: string;
8
+ name?: string;
9
+ pingable?: boolean;
10
+ }
11
+
12
+ const secureTemplate = "SecureUnitButtonTemplate";
13
+ const pingTemplate = "PingableUnitFrameTemplate";
14
+ let unitTemplate: string | undefined;
15
+
16
+ function templateFor(pingable: boolean) {
17
+ if (!pingable) return secureTemplate;
18
+ if (unitTemplate === undefined) {
19
+ const info = api.C_XMLUtil?.GetTemplateInfo?.(pingTemplate);
20
+ unitTemplate = info === undefined ? secureTemplate : secureTemplate + ", " + pingTemplate;
21
+ }
22
+ return unitTemplate;
23
+ }
24
+
25
+ /** Mount a WoW unit button with the client's target, menu and optional ping behavior. */
26
+ export function createSecureUnitButton(this: void, parent: FrameParent, { unit, name, pingable = true }: SecureUnitButtonOptions): Result<Button> {
27
+ if (isSecret(unit) || unit.length === 0) return fail("A secure unit button needs a readable unit token");
28
+ if (api.InCombatLockdown()) return fail("Secure unit buttons can only be created out of combat");
29
+
30
+ return protect(() => {
31
+ const button = createFrame("Button", name, parent, templateFor(pingable));
32
+ // The ping mixin must resolve the token from the secure attribute.
33
+ button.SetAttribute("unit", unit);
34
+ button.SetAttribute("*type1", "target");
35
+ button.SetAttribute("*type2", "togglemenu");
36
+ button.RegisterForClicks("AnyUp");
37
+ return button;
38
+ });
39
+ }
@@ -0,0 +1,92 @@
1
+ import type { WoWEvent, WoWEventMap } from "./contract";
2
+ import { api, isSecret, reveal } from "./native";
3
+ import { protect } from "./protect";
4
+
5
+ type CastingInfo = ReturnType<typeof api.UnitCastingInfo>;
6
+ export type UnitCastDuration = NonNullable<ReturnType<typeof api.UnitCastingDuration>>;
7
+ export type UnitCastEvent = Extract<WoWEvent, `UNIT_SPELLCAST_${string}`>;
8
+ export type UnitCastInterrupter = WoWEventMap["UNIT_SPELLCAST_INTERRUPTED"][3]
9
+ | WoWEventMap["UNIT_SPELLCAST_CHANNEL_STOP"][3]
10
+ | WoWEventMap["UNIT_SPELLCAST_EMPOWER_STOP"][4];
11
+
12
+ export interface UnitCast {
13
+ readonly unit: string;
14
+ readonly channel: boolean;
15
+ readonly duration?: UnitCastDuration;
16
+ readonly name?: CastingInfo[0];
17
+ readonly icon?: CastingInfo[2];
18
+ readonly startMS?: CastingInfo[3];
19
+ readonly endMS?: CastingInfo[4];
20
+ readonly notInterruptible?: CastingInfo[7];
21
+ readonly spellID?: CastingInfo[8];
22
+ readonly empowerStages?: number;
23
+ }
24
+
25
+ function present(value: unknown): boolean {
26
+ return isSecret(value) || value !== undefined;
27
+ }
28
+
29
+ export function getUnitCastDuration(this: void, unit: string, channel: boolean, empowered = false): UnitCastDuration | undefined {
30
+ const result = protect(() => empowered ? api.UnitEmpoweredChannelDuration?.(unit, true)
31
+ : channel ? api.UnitChannelDuration?.(unit) : api.UnitCastingDuration?.(unit));
32
+ return result.ok ? result.value : undefined;
33
+ }
34
+
35
+ function readCast(unit: string): UnitCast | undefined {
36
+ let duration = getUnitCastDuration(unit, false);
37
+ let channel = false;
38
+ if (!present(duration)) {
39
+ duration = getUnitCastDuration(unit, true);
40
+ channel = present(duration);
41
+ }
42
+
43
+ if (!channel) {
44
+ const [name, , icon, startMS, endMS, , , notInterruptible, spellID] = api.UnitCastingInfo(unit);
45
+ if (present(name)) return { unit, channel: false, duration, name, icon, startMS, endMS, notInterruptible, spellID };
46
+ }
47
+
48
+ const [name, , icon, startMS, rawEndMS, , notInterruptible, spellID, , rawStages] = api.UnitChannelInfo(unit);
49
+ const stages = reveal(rawStages);
50
+ const empowerStages = stages !== undefined && stages > 0 ? stages : undefined;
51
+ let endMS = rawEndMS;
52
+ if (empowerStages !== undefined) {
53
+ duration = getUnitCastDuration(unit, true, true);
54
+ // Channel info ends at the last charge. Empowered duration and fallback timing include the hold.
55
+ const result = protect(() => api.GetUnitEmpowerHoldAtMaxTime?.(unit));
56
+ const hold = result.ok ? reveal(result.value) : undefined;
57
+ const readableEnd = reveal(rawEndMS);
58
+ if (hold !== undefined && hold > 0 && readableEnd !== undefined) endMS = readableEnd + hold;
59
+ }
60
+
61
+ if (present(name) || present(duration)) {
62
+ return { unit, channel: true, duration, name, icon, startMS, endMS, notInterruptible, spellID, empowerStages };
63
+ }
64
+ }
65
+
66
+ /** Read the active cast or channel while preserving values that the client marks secret. */
67
+ export function readUnitCast(this: void, unit: string): UnitCast | undefined {
68
+ const result = protect(() => readCast(unit));
69
+ return result.ok ? result.value : undefined;
70
+ }
71
+
72
+ const interrupterPosition: Partial<Record<UnitCastEvent, number>> = {
73
+ UNIT_SPELLCAST_INTERRUPTED: 2,
74
+ UNIT_SPELLCAST_CHANNEL_STOP: 2,
75
+ UNIT_SPELLCAST_EMPOWER_STOP: 3,
76
+ };
77
+
78
+ /** The payload starts after the unit token. */
79
+ export function castInterrupter(this: void, event: UnitCastEvent | undefined, ...payload: unknown[]): UnitCastInterrupter | undefined {
80
+ if (event === undefined) return;
81
+ const position = interrupterPosition[event];
82
+ if (position === undefined) return;
83
+ const value = payload[position];
84
+ if (isSecret(value) || typeof value === "string") return value as UnitCastInterrupter;
85
+ }
86
+
87
+ export function castNotInterruptible(this: void, info: Pick<UnitCast, "notInterruptible">,
88
+ event?: UnitCastEvent): UnitCast["notInterruptible"] {
89
+ if (event === "UNIT_SPELLCAST_NOT_INTERRUPTIBLE") return true;
90
+ if (event === "UNIT_SPELLCAST_INTERRUPTIBLE") return false;
91
+ return info.notInterruptible;
92
+ }
@@ -0,0 +1,47 @@
1
+ import { fail, type Result } from "../src/result";
2
+ import type { FontString } from "./frames";
3
+ import { api, reveal, type SecretValue } from "./native";
4
+ import { protect } from "./protect";
5
+
6
+ export type UnitHealthTextMode = "off" | "value" | "percent" | "both";
7
+
8
+ type HealthTextRegion = Pick<FontString, "SetText"> & {
9
+ SetFormattedText(this: object, format: string, percent: ReturnType<typeof api.UnitHealthPercent>): void;
10
+ };
11
+
12
+ export interface UnitHealthTextOptions {
13
+ unit: string;
14
+ primary: HealthTextRegion;
15
+ secondary: HealthTextRegion;
16
+ mode: UnitHealthTextMode;
17
+ percentFormat?: string;
18
+ }
19
+
20
+ // The native formatter accepts the restricted health value and can return restricted text.
21
+ const abbreviateHealth = api.AbbreviateNumbers as (this: void, health: ReturnType<typeof api.UnitHealth>) =>
22
+ string | SecretValue<string>;
23
+
24
+ function renderPercent(text: HealthTextRegion, unit: string, format: string): Result<void> {
25
+ const scale = reveal(api.CurveConstants?.ScaleTo100);
26
+ if (scale === undefined) return fail("The native health percent scale is unavailable");
27
+ return protect(() => text.SetFormattedText(format, api.UnitHealthPercent(unit, true, scale)));
28
+ }
29
+
30
+ function renderSlot(text: HealthTextRegion, unit: string, mode: Exclude<UnitHealthTextMode, "both">, format: string): Result<void> {
31
+ if (mode === "off") return protect(() => text.SetText(""));
32
+ const result = mode === "value"
33
+ ? protect(() => text.SetText(abbreviateHealth(api.UnitHealth(unit))))
34
+ : renderPercent(text, unit, format);
35
+ if (!result.ok) protect(() => text.SetText(""));
36
+ return result;
37
+ }
38
+
39
+ /** Draw health text in two caller-owned font strings without reading restricted health values. */
40
+ export function renderUnitHealthText({ unit, primary, secondary, mode, percentFormat = "%.1f%%" }: UnitHealthTextOptions): Result<void> {
41
+ const readableMode = reveal(mode);
42
+ const mainMode = readableMode === "both" || readableMode === "value" ? "value"
43
+ : readableMode === "percent" ? "percent" : "off";
44
+ const first = renderSlot(primary, unit, mainMode, percentFormat);
45
+ const second = renderSlot(secondary, unit, readableMode === "both" ? "percent" : "off", percentFormat);
46
+ return first.ok ? second : first;
47
+ }