@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.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +387 -0
- package/builder/css-v1.test.mjs +361 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +127 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +435 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2336 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +381 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +83 -0
- package/builder/src/numbers.lua +76 -0
- package/builder/src/project-front.mjs +3682 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +124 -0
- package/builder/src/setup-store.lua +18 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow-reads.lua +22 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-modules.mjs +93 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +59 -0
- package/eslint.mjs +74 -0
- package/form/index.ts +526 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +26 -4
- package/router/index.ts +50 -0
- package/router/navigation.ts +22 -0
- package/router/package.json +12 -0
- package/router/router-view.ts +134 -0
- package/router/routes.ts +64 -0
- package/scripts/addon-build.mjs +364 -0
- package/scripts/addon-project.mjs +78 -0
- package/scripts/kumi-cli.mjs +105 -0
- package/scripts/link-addons.mjs +61 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +239 -0
- package/src/css-v1.ts +68 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +505 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +336 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +29 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +29 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +366 -0
- package/src/media-query.ts +144 -0
- package/src/module-ui.ts +147 -0
- package/src/module.ts +311 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +52 -0
- package/src/providers.ts +241 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +275 -0
- package/src/result.ts +64 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +63 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +9 -0
- package/state/package.json +12 -0
- package/state/setup-store-persistence.ts +206 -0
- package/state/setup-store-view.ts +10 -0
- package/state/setup-store.ts +342 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +84 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/AGENTS.md +34 -0
- package/templates/addon/Assets/Art/Corner10.tga +0 -0
- package/templates/addon/Assets/Art/Corner11.tga +0 -0
- package/templates/addon/Assets/Art/Corner12.tga +0 -0
- package/templates/addon/Assets/Art/Corner13.tga +0 -0
- package/templates/addon/Assets/Art/Corner14.tga +0 -0
- package/templates/addon/Assets/Art/Corner15.tga +0 -0
- package/templates/addon/Assets/Art/Corner16.tga +0 -0
- package/templates/addon/Assets/Art/Corner2.tga +0 -0
- package/templates/addon/Assets/Art/Corner3.tga +0 -0
- package/templates/addon/Assets/Art/Corner4.tga +0 -0
- package/templates/addon/Assets/Art/Corner5.tga +0 -0
- package/templates/addon/Assets/Art/Corner6.tga +0 -0
- package/templates/addon/Assets/Art/Corner7.tga +0 -0
- package/templates/addon/Assets/Art/Corner8.tga +0 -0
- package/templates/addon/Assets/Art/Corner9.tga +0 -0
- package/templates/addon/Assets/Art/Dot.tga +0 -0
- package/templates/addon/Assets/Art/Gear.tga +0 -0
- package/templates/addon/Assets/Art/Grip.tga +0 -0
- package/templates/addon/Assets/Art/Outline10.tga +0 -0
- package/templates/addon/Assets/Art/Outline12.tga +0 -0
- package/templates/addon/Assets/Art/Outline13.tga +0 -0
- package/templates/addon/Assets/Art/Outline14.tga +0 -0
- package/templates/addon/Assets/Art/Outline15.tga +0 -0
- package/templates/addon/Assets/Art/Outline2.tga +0 -0
- package/templates/addon/Assets/Art/Outline3.tga +0 -0
- package/templates/addon/Assets/Art/Outline5.tga +0 -0
- package/templates/addon/Assets/Art/Outline7.tga +0 -0
- package/templates/addon/Assets/Art/Outline9.tga +0 -0
- package/templates/addon/Assets/Art/Round11.tga +0 -0
- package/templates/addon/Assets/Art/Round11Border.tga +0 -0
- package/templates/addon/Assets/Art/Round4.tga +0 -0
- package/templates/addon/Assets/Art/Round4Border.tga +0 -0
- package/templates/addon/Assets/Art/Round6.tga +0 -0
- package/templates/addon/Assets/Art/Round6Border.tga +0 -0
- package/templates/addon/Assets/Art/Round8.tga +0 -0
- package/templates/addon/Assets/Art/Round8Border.tga +0 -0
- package/templates/addon/Assets/Art/Search.tga +0 -0
- package/templates/addon/CLAUDE.md +1 -0
- package/templates/addon/README.md +49 -0
- package/templates/addon/eslint.config.mjs +6 -0
- package/templates/addon/gitignore +9 -0
- package/templates/addon/kumi.config.ts +17 -0
- package/templates/addon/package.json +20 -0
- package/templates/addon/src/app/app.module.ts +18 -0
- package/templates/addon/src/app/commands.store.ts +11 -0
- package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
- package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
- package/templates/addon/src/app/main.ts +5 -0
- package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
- package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
- package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
- package/templates/addon/src/app/theme.ts +6 -0
- package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
- package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
- package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
- package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
- package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
- package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
- package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
- package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
- package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
- package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
- package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
- package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
- package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
- package/templates/addon/src/shared/api/native-art.ts +28 -0
- package/templates/addon/src/shared/assets/close.png +0 -0
- package/templates/addon/src/shared/lib/format.ts +20 -0
- package/templates/addon/src/shared/styles/_tokens.scss +12 -0
- package/templates/addon/src/shared/ui/Button.tsx +29 -0
- package/templates/addon/src/shared/ui/Choice.tsx +34 -0
- package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
- package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
- package/templates/addon/src/shared/ui/Slider.tsx +25 -0
- package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
- package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
- package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
- package/templates/addon/tsconfig.json +63 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/action-bar-layout.ts +108 -0
- package/wow/action-button.ts +219 -0
- package/wow/action-catalog.ts +98 -0
- package/wow/addon-manifest.ts +36 -0
- package/wow/animation.ts +428 -0
- package/wow/app-ui.ts +221 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/cooldown-timing.ts +24 -0
- package/wow/css-box.ts +144 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +603 -0
- package/wow/css-text.ts +399 -0
- package/wow/cursor.ts +45 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +53 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frame-timing.ts +13 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/hooks.ts +133 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +111 -0
- package/wow/layout-view.ts +663 -0
- package/wow/locale-font.ts +15 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +153 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/panel-host.ts +97 -0
- package/wow/pixel.ts +35 -0
- package/wow/plugins.ts +246 -0
- package/wow/protect.ts +48 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/protected-view.ts +103 -0
- package/wow/range-art.ts +81 -0
- package/wow/readable-api.ts +120 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +183 -0
- package/wow/saved-variables.ts +109 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/spell-cooldown.ts +36 -0
- package/wow/start-addon.ts +122 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1864 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { ref, type ReadonlyRef } from "../src/reactivity";
|
|
2
|
+
import { fail, ok, type Result } from "../src/result";
|
|
3
|
+
import type { Runtime, RuntimeScope } from "../src/runtime";
|
|
4
|
+
import type { UIElement } from "../src/elements";
|
|
5
|
+
import type { EventApi } from "./contract";
|
|
6
|
+
import type { FrameStrata } from "./frame-layer";
|
|
7
|
+
import { readFrameHeight, readFrameShown, readFrameWidth } from "./frame-access";
|
|
8
|
+
import { createFrame, type Frame } from "./frames";
|
|
9
|
+
import { api } from "./native";
|
|
10
|
+
import { physicalPixel, snapPixel } from "./pixel";
|
|
11
|
+
import { unlockedActionSlot } from "./action-button";
|
|
12
|
+
import { mountNative, type NativeViewOptions } from "./view";
|
|
13
|
+
|
|
14
|
+
interface NativePanel {
|
|
15
|
+
readonly locked: ReadonlyRef<boolean>;
|
|
16
|
+
show(this: void): void;
|
|
17
|
+
hide(this: void): void;
|
|
18
|
+
isShown(this: void): boolean;
|
|
19
|
+
mount(this: void, runtime: Runtime, render: (this: void, scope: RuntimeScope) => UIElement,
|
|
20
|
+
options: NativeViewOptions): ReturnType<typeof mountNative>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Own a native parent from the first mount and center it on display metric changes. */
|
|
24
|
+
export function createNativePanel(this: void, scope: RuntimeScope, events: EventApi, width: number, height: number,
|
|
25
|
+
strata: FrameStrata, offsetY: number): Result<NativePanel> {
|
|
26
|
+
const locked = ref(!unlockedActionSlot());
|
|
27
|
+
const refreshCombat = () => { locked.value = !unlockedActionSlot(); };
|
|
28
|
+
const lockedEvent = events.on(scope, "PLAYER_REGEN_DISABLED", refreshCombat);
|
|
29
|
+
if (!lockedEvent.ok) return lockedEvent;
|
|
30
|
+
const unlockedEvent = events.on(scope, "PLAYER_REGEN_ENABLED", refreshCombat);
|
|
31
|
+
if (!unlockedEvent.ok) return unlockedEvent;
|
|
32
|
+
|
|
33
|
+
const parent = api.UIParent as Frame;
|
|
34
|
+
let host: Frame | undefined;
|
|
35
|
+
let panelWidth = width;
|
|
36
|
+
let anchorX: number | undefined, anchorY: number | undefined;
|
|
37
|
+
let centered: boolean | undefined;
|
|
38
|
+
function place(panel: Frame) {
|
|
39
|
+
const pixel = physicalPixel(panel);
|
|
40
|
+
const screenWidth = readFrameWidth(parent);
|
|
41
|
+
const screenHeight = readFrameHeight(parent);
|
|
42
|
+
const availableWidth = screenWidth === undefined || screenWidth <= 0 ? width : Math.min(width, screenWidth);
|
|
43
|
+
if (panelWidth !== availableWidth) {
|
|
44
|
+
panelWidth = availableWidth;
|
|
45
|
+
panel.SetWidth(panelWidth);
|
|
46
|
+
}
|
|
47
|
+
const center = screenWidth === undefined || screenHeight === undefined;
|
|
48
|
+
const x = center ? 0 : snapPixel((screenWidth - panelWidth) / 2, pixel);
|
|
49
|
+
const y = center ? offsetY : snapPixel((screenHeight - height) / 2 + offsetY, pixel);
|
|
50
|
+
if (center === centered && x === anchorX && y === anchorY) return;
|
|
51
|
+
const point = center ? "CENTER" : "BOTTOMLEFT";
|
|
52
|
+
panel.ClearAllPoints();
|
|
53
|
+
panel.SetPoint(point, parent, point, x, y);
|
|
54
|
+
centered = center;
|
|
55
|
+
anchorX = x;
|
|
56
|
+
anchorY = y;
|
|
57
|
+
}
|
|
58
|
+
function ensureHost(): Result<Frame> {
|
|
59
|
+
if (host !== undefined) return ok(host);
|
|
60
|
+
return scope.protect(() => {
|
|
61
|
+
const panel = createFrame("Frame", undefined, parent);
|
|
62
|
+
panel.Hide();
|
|
63
|
+
panel.SetFrameStrata(strata);
|
|
64
|
+
panel.SetClampedToScreen(true);
|
|
65
|
+
panel.SetSize(width, height);
|
|
66
|
+
panelWidth = width;
|
|
67
|
+
place(panel);
|
|
68
|
+
host = panel;
|
|
69
|
+
return panel;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function refreshPosition() {
|
|
73
|
+
if (host !== undefined) place(host);
|
|
74
|
+
}
|
|
75
|
+
const zoom = events.on(scope, "UI_SCALE_CHANGED", refreshPosition);
|
|
76
|
+
if (!zoom.ok) return zoom;
|
|
77
|
+
const screen = events.on(scope, "DISPLAY_SIZE_CHANGED", refreshPosition);
|
|
78
|
+
if (!screen.ok) return screen;
|
|
79
|
+
const cleanup = scope.onDispose(() => { host?.Hide(); });
|
|
80
|
+
if (!cleanup.ok) return cleanup;
|
|
81
|
+
return ok({
|
|
82
|
+
locked,
|
|
83
|
+
show: () => {
|
|
84
|
+
if (!scope.isActive()) return;
|
|
85
|
+
const ready = ensureHost();
|
|
86
|
+
if (ready.ok) ready.value.Show();
|
|
87
|
+
else api.geterrorhandler()(ready.reason);
|
|
88
|
+
},
|
|
89
|
+
hide: () => { host?.Hide(); },
|
|
90
|
+
isShown: () => host !== undefined && readFrameShown(host) === true,
|
|
91
|
+
mount: (runtime, render, options) => {
|
|
92
|
+
if (!scope.isActive()) return fail("Cannot mount a disposed panel");
|
|
93
|
+
const ready = ensureHost();
|
|
94
|
+
return ready.ok ? mountNative(ready.value, runtime, render, options) : ready;
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
package/wow/pixel.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ok } from "../src/result";
|
|
2
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
3
|
+
import { createEventDispatcher } from "./event-dispatcher";
|
|
4
|
+
import type { Frame } from "./frames";
|
|
5
|
+
import { api, reveal } from "./native";
|
|
6
|
+
|
|
7
|
+
const display = createEventDispatcher();
|
|
8
|
+
|
|
9
|
+
/** Effective scale for a native region whose local unit is one display pixel. */
|
|
10
|
+
export function physicalPixelScale(this: void): number | undefined {
|
|
11
|
+
const [, height] = api.GetPhysicalScreenSize();
|
|
12
|
+
const screen = reveal(height);
|
|
13
|
+
return typeof screen === "number" && Number.isFinite(screen) && screen > 0 ? 768 / screen : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Local UI units occupied by a single physical display pixel. */
|
|
17
|
+
export function physicalPixel(this: void, frame: Pick<Frame, "GetEffectiveScale">): number {
|
|
18
|
+
const [, height] = api.GetPhysicalScreenSize();
|
|
19
|
+
const scale = reveal(frame.GetEffectiveScale());
|
|
20
|
+
const screen = reveal(height);
|
|
21
|
+
return typeof scale === "number" && Number.isFinite(scale) && scale > 0
|
|
22
|
+
&& typeof screen === "number" && Number.isFinite(screen) && screen > 0 ? 768 / screen / scale : 1;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function snapPixel(this: void, value: number, unit: number): number {
|
|
26
|
+
return Math.floor(value / unit + 0.5) * unit;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function watchDisplayMetrics(this: void, scope: RuntimeScope, refresh: (this: void) => void) {
|
|
30
|
+
const scale = display.on(scope, "UI_SCALE_CHANGED", refresh);
|
|
31
|
+
if (!scale.ok) return scale;
|
|
32
|
+
const size = display.on(scope, "DISPLAY_SIZE_CHANGED", refresh);
|
|
33
|
+
if (!size.ok) { scale.value(); return size; }
|
|
34
|
+
return ok(undefined);
|
|
35
|
+
}
|
package/wow/plugins.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type { ModuleContainer, ModuleDeclaration, ModuleInput, ModuleReference } from "../src/module";
|
|
2
|
+
import { computed } from "../src/reactive-scope";
|
|
3
|
+
import { ref, type SignalRef } from "../src/reactivity";
|
|
4
|
+
import { fail, ok, type Failure, type Result } from "../src/result";
|
|
5
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
6
|
+
import { ownedMap, type OwnedMap } from "../src/owned-collections";
|
|
7
|
+
import { defineStore, readonly } from "../state/setup-store";
|
|
8
|
+
import { addonManifest, type AddonManifest, type PluginManifest } from "./addon-manifest";
|
|
9
|
+
import { api, reveal } from "./native";
|
|
10
|
+
import { protect } from "./protect";
|
|
11
|
+
|
|
12
|
+
type LoadableModule = Exclude<ModuleReference<object>, ModuleInput<object>>;
|
|
13
|
+
type ModuleExports<Module> = Module extends ModuleReference<infer Value> ? Value : never;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The plugins declared in kumi.config.ts, keyed by ID. `kumi check` and `kumi build` fill this in from
|
|
17
|
+
* `build/kumi/plugins.d.ts`, so `usePlugins().load("history")` knows the plugin's services.
|
|
18
|
+
*/
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
20
|
+
export interface KumiPlugins {}
|
|
21
|
+
|
|
22
|
+
/** A plugin ID from kumi.config.ts. Any string until the project declares plugins. */
|
|
23
|
+
export type PluginId = [keyof KumiPlugins] extends [never] ? string : Extract<keyof KumiPlugins, string>;
|
|
24
|
+
/** The public services of a plugin's module. */
|
|
25
|
+
export type PluginServices<Id extends string> = Id extends keyof KumiPlugins ? Readonly<ModuleExports<KumiPlugins[Id]>> : Readonly<object>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* - `"off"`: turned off in the addon list.
|
|
29
|
+
* - `"waiting"`: turned on but not running yet. A demand plugin waits for `load()`, a startup plugin for a reload.
|
|
30
|
+
* - `"loading"`, `"ready"`, `"error"`: the plugin is starting, running, or failed with `error`.
|
|
31
|
+
*/
|
|
32
|
+
export type PluginStatus = "off" | "waiting" | "loading" | "ready" | "error";
|
|
33
|
+
|
|
34
|
+
export interface PluginInfo {
|
|
35
|
+
readonly id: PluginId;
|
|
36
|
+
readonly addon: string;
|
|
37
|
+
readonly title: string;
|
|
38
|
+
readonly notes: string;
|
|
39
|
+
readonly load: "startup" | "demand";
|
|
40
|
+
/** The saved choice in the addon list. A change applies after `/reload`, or at `load()` for a demand plugin. */
|
|
41
|
+
readonly enabled: boolean;
|
|
42
|
+
readonly status: PluginStatus;
|
|
43
|
+
readonly error?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface PluginState {
|
|
47
|
+
readonly manifest: PluginManifest;
|
|
48
|
+
readonly enabled: SignalRef<boolean>;
|
|
49
|
+
readonly status: SignalRef<PluginStatus>;
|
|
50
|
+
readonly error: SignalRef<string | undefined>;
|
|
51
|
+
/** Whether the plugin's code was running when the UI loaded. */
|
|
52
|
+
readonly running: boolean;
|
|
53
|
+
services?: object;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface PluginHost {
|
|
57
|
+
readonly modules: ModuleContainer;
|
|
58
|
+
readonly scope: RuntimeScope;
|
|
59
|
+
readonly manifest: AddonManifest;
|
|
60
|
+
readonly states: OwnedMap<string, PluginState>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let host: PluginHost | undefined;
|
|
64
|
+
|
|
65
|
+
function playerName() {
|
|
66
|
+
const read = api.UnitName;
|
|
67
|
+
if (typeof read !== "function") return undefined;
|
|
68
|
+
const [name] = read("player");
|
|
69
|
+
const value = reveal(name);
|
|
70
|
+
return typeof value === "string" && value !== "" ? value : undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function savedEnabled(addon: string) {
|
|
74
|
+
const state = reveal(api.C_AddOns?.GetAddOnEnableState?.(addon, playerName() ?? ""));
|
|
75
|
+
return typeof state === "number" ? state > 0 : true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function inCombat() {
|
|
79
|
+
return reveal(api.InCombatLockdown?.()) === true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function activate(state: PluginState, modules: ModuleContainer, manifest: AddonManifest): Result<object> {
|
|
83
|
+
const module = manifest.registered[state.manifest.id] as LoadableModule | undefined;
|
|
84
|
+
if (module === undefined) {
|
|
85
|
+
const failure = manifest.failures[state.manifest.id];
|
|
86
|
+
return fail(failure === undefined ? "Plugin " + state.manifest.id + " did not register its module"
|
|
87
|
+
: "Plugin " + state.manifest.id + " failed while its file loaded: " + failure + ". Reload the UI after updating it.");
|
|
88
|
+
}
|
|
89
|
+
const attempted = protect(() => modules.resolve(module));
|
|
90
|
+
const resolved = attempted.ok ? attempted.value : attempted;
|
|
91
|
+
if (!resolved.ok) return resolved;
|
|
92
|
+
return ok(resolved.value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function settle(state: PluginState, result: Result<object>) {
|
|
96
|
+
if (result.ok) {
|
|
97
|
+
state.services = result.value;
|
|
98
|
+
state.error.value = undefined;
|
|
99
|
+
state.status.value = "ready";
|
|
100
|
+
} else {
|
|
101
|
+
state.error.value = result.reason;
|
|
102
|
+
state.status.value = "error";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** @internal Called by a plugin's generated entry when WoW runs its file. */
|
|
107
|
+
export function registerPlugin(this: void, id: string, module: LoadableModule): void {
|
|
108
|
+
const manifest = addonManifest();
|
|
109
|
+
if (manifest === undefined) throw new Error("Plugin " + id + " needs the Kumi manifest of its addon");
|
|
110
|
+
if ((module as ModuleDeclaration<object>).kind !== "module") throw new Error("Plugin " + id + " must export a Kumi module");
|
|
111
|
+
if (manifest.registered[id] !== undefined) throw new Error("Plugin " + id + " registered twice");
|
|
112
|
+
manifest.registered[id] = module;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** @internal Start the startup plugins once the root module is active. One failing plugin does not stop the others. */
|
|
116
|
+
export function startPlugins(this: void, modules: ModuleContainer, scope: RuntimeScope, report: (this: void, failure: Failure) => void): void {
|
|
117
|
+
const manifest = addonManifest();
|
|
118
|
+
if (manifest === undefined) return;
|
|
119
|
+
manifest.started = true;
|
|
120
|
+
const states = ownedMap<string, PluginState>();
|
|
121
|
+
for (let index = 0; index < manifest.plugins.length; index++) {
|
|
122
|
+
const plugin = manifest.plugins[index];
|
|
123
|
+
const registered = manifest.registered[plugin.id] !== undefined || manifest.failures[plugin.id] !== undefined;
|
|
124
|
+
const enabled = savedEnabled(plugin.addon);
|
|
125
|
+
states.set(plugin.id, {
|
|
126
|
+
manifest: plugin,
|
|
127
|
+
enabled: ref(enabled),
|
|
128
|
+
status: ref<PluginStatus>(enabled ? "waiting" : "off"),
|
|
129
|
+
error: ref<string | undefined>(undefined),
|
|
130
|
+
running: registered,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
host = { modules, scope, manifest, states };
|
|
134
|
+
for (let index = 0; index < manifest.plugins.length; index++) {
|
|
135
|
+
const plugin = manifest.plugins[index];
|
|
136
|
+
const state = states.get(plugin.id);
|
|
137
|
+
if (state === undefined || plugin.load !== "startup" || !state.running) continue;
|
|
138
|
+
settle(state, activate(state, modules, manifest));
|
|
139
|
+
if (state.status.value === "error") report(fail("Plugin " + plugin.id + ": " + (state.error.value ?? "failed")));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function loadPlugin(id: string): Result<object> {
|
|
144
|
+
const current = host;
|
|
145
|
+
if (current === undefined) return fail("Plugins are available after the addon starts");
|
|
146
|
+
if (!current.scope.isActive()) return fail("The addon has stopped");
|
|
147
|
+
const state = current.states.get(id);
|
|
148
|
+
if (state === undefined) return fail("Unknown plugin " + id + ". Declare it in kumi.config.ts.");
|
|
149
|
+
const status = state.status.value;
|
|
150
|
+
if (status === "ready" && state.services !== undefined) return ok(state.services);
|
|
151
|
+
if (status === "loading") return fail("Plugin " + id + " is already loading");
|
|
152
|
+
if (!state.enabled.value) return fail("Plugin " + id + " is turned off. Turn it on in the plugin list.");
|
|
153
|
+
if (state.manifest.load === "startup" && !state.running) return fail("Plugin " + id + " starts after a UI reload");
|
|
154
|
+
if (inCombat()) return fail("Plugin " + id + " can load after combat");
|
|
155
|
+
const { manifest, modules } = current;
|
|
156
|
+
state.status.value = "loading";
|
|
157
|
+
state.error.value = undefined;
|
|
158
|
+
if (manifest.registered[id] === undefined && manifest.failures[id] === undefined) {
|
|
159
|
+
const loader = api.C_AddOns?.LoadAddOn;
|
|
160
|
+
if (typeof loader !== "function") {
|
|
161
|
+
settle(state, fail("This client cannot load addons on demand"));
|
|
162
|
+
return fail(state.error.value ?? "");
|
|
163
|
+
}
|
|
164
|
+
// WoW reports errors from the plugin's files to its error handler. LoadAddOn itself returns a status.
|
|
165
|
+
manifest.loading = state.manifest.addon;
|
|
166
|
+
const [success, reason] = loader(state.manifest.addon);
|
|
167
|
+
manifest.loading = undefined;
|
|
168
|
+
if (reveal(success) !== true) {
|
|
169
|
+
const failure = fail("Could not load " + state.manifest.addon + ": " + (reveal(reason) ?? "unknown reason"));
|
|
170
|
+
settle(state, failure);
|
|
171
|
+
return failure;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const result = activate(state, modules, manifest);
|
|
175
|
+
settle(state, result);
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function setPluginEnabled(id: string, enabled: boolean, reload: SignalRef<boolean>): Result<void> {
|
|
180
|
+
const current = host;
|
|
181
|
+
const state = current?.states.get(id);
|
|
182
|
+
if (current === undefined || state === undefined) return fail("Unknown plugin " + id + ". Declare it in kumi.config.ts.");
|
|
183
|
+
if (state.enabled.value === enabled) return ok(undefined);
|
|
184
|
+
const addons = api.C_AddOns;
|
|
185
|
+
const write = enabled ? addons?.EnableAddOn : addons?.DisableAddOn;
|
|
186
|
+
if (typeof write !== "function") return fail("This client cannot change the addon list");
|
|
187
|
+
const character = current.manifest.pluginState === "character" ? playerName() : undefined;
|
|
188
|
+
const written = protect(() => {
|
|
189
|
+
if (character === undefined) write(state.manifest.addon);
|
|
190
|
+
else write(state.manifest.addon, character);
|
|
191
|
+
addons?.SaveAddOns?.();
|
|
192
|
+
});
|
|
193
|
+
if (!written.ok) return written;
|
|
194
|
+
state.enabled.value = enabled;
|
|
195
|
+
if (state.status.value === "off" || state.status.value === "waiting") state.status.value = enabled ? "waiting" : "off";
|
|
196
|
+
// A running plugin stops only after a reload. A startup plugin also starts only after one.
|
|
197
|
+
const running = state.status.value === "ready" || state.status.value === "error" || state.running;
|
|
198
|
+
if (running !== enabled && (running || state.manifest.load === "startup")) reload.value = true;
|
|
199
|
+
return ok(undefined);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The addon's plugins and their state. Use it to build a plugin list:
|
|
204
|
+
* `plugins.list` is reactive, `setEnabled` changes the addon list, and `reload()` applies the change.
|
|
205
|
+
*/
|
|
206
|
+
export const usePlugins = defineStore("kumi/plugins", () => {
|
|
207
|
+
const reloadRequired = ref(false);
|
|
208
|
+
const list = computed(() => {
|
|
209
|
+
const rows: PluginInfo[] = [];
|
|
210
|
+
const current = host;
|
|
211
|
+
if (current === undefined) return rows;
|
|
212
|
+
for (let index = 0; index < current.manifest.plugins.length; index++) {
|
|
213
|
+
const plugin = current.manifest.plugins[index];
|
|
214
|
+
const state = current.states.get(plugin.id);
|
|
215
|
+
if (state === undefined) continue;
|
|
216
|
+
rows.push({
|
|
217
|
+
id: plugin.id as PluginId, addon: plugin.addon, title: plugin.title, notes: plugin.notes, load: plugin.load,
|
|
218
|
+
enabled: state.enabled.value, status: state.status.value, error: state.error.value,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return rows;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
/** Load a demand plugin, or return a running plugin's services. Repeated calls return the same services. */
|
|
225
|
+
function load<Id extends PluginId>(this: void, id: Id): Result<PluginServices<Id>> {
|
|
226
|
+
return loadPlugin(id) as Result<PluginServices<Id>>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Turn a plugin on or off in the addon list. Check `reloadRequired` to offer a reload. */
|
|
230
|
+
function setEnabled(this: void, id: PluginId, enabled: boolean): Result<void> {
|
|
231
|
+
return setPluginEnabled(id, enabled, reloadRequired);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function status(this: void, id: PluginId): PluginStatus | undefined {
|
|
235
|
+
return host?.states.get(id)?.status.value;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Reload the interface so plugin changes take effect. */
|
|
239
|
+
function reload(this: void): void {
|
|
240
|
+
const ui = api.C_UI;
|
|
241
|
+
if (typeof ui?.Reload === "function") ui.Reload();
|
|
242
|
+
else (api as { ReloadUI?: (this: void) => void }).ReloadUI?.();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return { list, reloadRequired: readonly(reloadRequired), load, setEnabled, status, reload };
|
|
246
|
+
});
|
package/wow/protect.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ok, type Failure, type Result } from "../src/result";
|
|
2
|
+
import { rawField } from "./lua-table";
|
|
3
|
+
import { api, isSecret } from "./native";
|
|
4
|
+
import type { LuaMultiReturn } from "@kumi/wow-generated";
|
|
5
|
+
|
|
6
|
+
type ProtectedNative<Values extends unknown[]> = LuaMultiReturn<
|
|
7
|
+
[success: true, ...values: Values] | [success: false, error: unknown]
|
|
8
|
+
>;
|
|
9
|
+
|
|
10
|
+
declare function pcall<Receiver extends object, Args extends unknown[], Values extends unknown[]>(
|
|
11
|
+
this: void, run: (this: Receiver, ...args: Args) => LuaMultiReturn<Values>, receiver: Receiver, ...args: Args
|
|
12
|
+
): ProtectedNative<Values>;
|
|
13
|
+
declare function pcall<Args extends unknown[], Value>(
|
|
14
|
+
this: void, run: (this: void, ...args: Args) => Value, ...args: Args
|
|
15
|
+
): ProtectedNative<[Value]>;
|
|
16
|
+
declare function pcall<Receiver extends object, Args extends unknown[], Value>(
|
|
17
|
+
this: void, run: (this: Receiver, ...args: Args) => Value, receiver: Receiver, ...args: Args
|
|
18
|
+
): ProtectedNative<[Value]>;
|
|
19
|
+
|
|
20
|
+
declare function xpcall<Value>(
|
|
21
|
+
this: void, run: (this: void) => Value, onError: (this: void, error: unknown) => Failure
|
|
22
|
+
): LuaMultiReturn<[success: true, value: Value] | [success: false, error: Failure]>;
|
|
23
|
+
|
|
24
|
+
/** Preserve native multiple returns and leave error objects opaque. */
|
|
25
|
+
export const protectNative = pcall;
|
|
26
|
+
|
|
27
|
+
type DebugStack = (this: void, skip: number, top: number, bottom: number) => string;
|
|
28
|
+
type Traceback = (this: void, prefix: string, level: number) => string;
|
|
29
|
+
|
|
30
|
+
function readTrace(this: void): string | undefined {
|
|
31
|
+
const stack = rawField<DebugStack>(api, "debugstack");
|
|
32
|
+
const trace = stack === undefined
|
|
33
|
+
? rawField<Traceback>(rawField(api, "debug"), "traceback")?.("", 3)
|
|
34
|
+
: stack(3, 24, 8);
|
|
35
|
+
return isSecret(trace) || trace === "" ? undefined : trace;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// xpcall invokes the handler while the throwing callback's stack is still available.
|
|
39
|
+
function captureFailure(this: void, error: unknown): Failure {
|
|
40
|
+
const [described, reason] = protectNative(() => isSecret(error) ? "Protected WoW error" : String(error));
|
|
41
|
+
const [captured, trace] = protectNative(readTrace);
|
|
42
|
+
return { ok: false, reason: described ? reason : "WoW callback failed", trace: captured ? trace : undefined };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function protect<T>(run: (this: void) => T): Result<T> {
|
|
46
|
+
const [success, value] = xpcall(run, captureFailure);
|
|
47
|
+
return success ? ok(value) : value;
|
|
48
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { currentScope } from "../src/reactive-scope";
|
|
2
|
+
import { fail, ok, type Result } from "../src/result";
|
|
3
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
4
|
+
import type { Job } from "../src/scheduler";
|
|
5
|
+
import { createFrame, type Frame, type FrameEventRegistration } from "./frames";
|
|
6
|
+
import { api, reveal } from "./native";
|
|
7
|
+
import { protectNative } from "./protect";
|
|
8
|
+
|
|
9
|
+
export interface ProtectedLayoutOptions {
|
|
10
|
+
scope?: RuntimeScope;
|
|
11
|
+
label?: string;
|
|
12
|
+
prewarm?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ProtectedLayoutUpdate {
|
|
16
|
+
request(this: void): Result<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Enqueue = (this: void) => void;
|
|
20
|
+
type CombatEventFrame = FrameEventRegistration & {
|
|
21
|
+
SetScript(this: object, script: "OnEvent", handler: Enqueue): void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const waiting = new Set<Enqueue>();
|
|
25
|
+
const regenEvent = "PLAYER_REGEN_ENABLED";
|
|
26
|
+
let combatEvents: CombatEventFrame | undefined;
|
|
27
|
+
let listening = false;
|
|
28
|
+
|
|
29
|
+
function nativeFailure(problem: unknown, fallback: string) {
|
|
30
|
+
const reason = reveal(problem);
|
|
31
|
+
return fail(typeof reason === "string" ? reason : fallback);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function stopListening() {
|
|
35
|
+
const frame = combatEvents;
|
|
36
|
+
if (frame === undefined || !listening || waiting.size !== 0) return;
|
|
37
|
+
const [success, problem] = protectNative(() => frame.UnregisterEvent(regenEvent));
|
|
38
|
+
if (success) listening = false;
|
|
39
|
+
else {
|
|
40
|
+
const { reason } = nativeFailure(problem, "Could not stop listening for the end of combat");
|
|
41
|
+
protectNative(() => api.geterrorhandler()(reason));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function flushWaiting() {
|
|
46
|
+
const ready = [...waiting];
|
|
47
|
+
waiting.clear();
|
|
48
|
+
stopListening();
|
|
49
|
+
for (const enqueue of ready) enqueue();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function combatFrame() {
|
|
53
|
+
return combatEvents ??= createFrame("Frame") as Frame & CombatEventFrame;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function watchCombat(): Result<void> {
|
|
57
|
+
if (listening) return ok(undefined);
|
|
58
|
+
const [success, problem] = protectNative(() => {
|
|
59
|
+
const frame = combatFrame();
|
|
60
|
+
frame.SetScript("OnEvent", flushWaiting);
|
|
61
|
+
frame.RegisterEvent(regenEvent);
|
|
62
|
+
});
|
|
63
|
+
if (!success) return nativeFailure(problem, "Could not listen for the end of combat");
|
|
64
|
+
listening = true;
|
|
65
|
+
return ok(undefined);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Reconcile protected layout with current state on the owning runtime's frame budget. */
|
|
69
|
+
export function createProtectedLayoutUpdate(this: void, commit: Job,
|
|
70
|
+
{ scope = currentScope(), label = "Protected layout", prewarm = false }: ProtectedLayoutOptions = {}): Result<ProtectedLayoutUpdate> {
|
|
71
|
+
if (scope === undefined || !scope.isActive()) return fail("Protected layout requires an active scope");
|
|
72
|
+
if (prewarm && combatEvents === undefined) {
|
|
73
|
+
const [success, problem] = protectNative(combatFrame);
|
|
74
|
+
if (!success) return nativeFailure(problem, "Could not prepare the protected layout event frame");
|
|
75
|
+
}
|
|
76
|
+
const owner = scope;
|
|
77
|
+
const frames = owner.frameJobs;
|
|
78
|
+
const key = {};
|
|
79
|
+
const options = { lane: "normal", label } as const;
|
|
80
|
+
let waiter: Enqueue | undefined;
|
|
81
|
+
let committing = false;
|
|
82
|
+
|
|
83
|
+
function enqueue() {
|
|
84
|
+
if (owner.isActive()) frames.queue(key, run, options);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function cancel() {
|
|
88
|
+
const pending = waiter;
|
|
89
|
+
waiter = undefined;
|
|
90
|
+
if (pending !== undefined && waiting.delete(pending)) stopListening();
|
|
91
|
+
frames.cancel(key);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function waitForCombat() {
|
|
95
|
+
frames.cancel(key);
|
|
96
|
+
if (waiter !== undefined && waiting.has(waiter)) return ok(undefined);
|
|
97
|
+
const ready = watchCombat();
|
|
98
|
+
if (!ready.ok) return ready;
|
|
99
|
+
const next = () => {
|
|
100
|
+
if (waiter !== next) return;
|
|
101
|
+
waiter = undefined;
|
|
102
|
+
enqueue();
|
|
103
|
+
};
|
|
104
|
+
waiter = next;
|
|
105
|
+
waiting.add(next);
|
|
106
|
+
return ready;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function request(): Result<void> {
|
|
110
|
+
if (!owner.isActive()) return fail("The protected layout scope has been disposed");
|
|
111
|
+
const lockdown = reveal(api.InCombatLockdown());
|
|
112
|
+
if (typeof lockdown !== "boolean" || lockdown) return waitForCombat();
|
|
113
|
+
|
|
114
|
+
cancel();
|
|
115
|
+
if (committing) {
|
|
116
|
+
enqueue();
|
|
117
|
+
return ok(undefined);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
committing = true;
|
|
121
|
+
const [success, result] = protectNative(commit);
|
|
122
|
+
committing = false;
|
|
123
|
+
return success ? result ?? ok(undefined) : nativeFailure(result, "Protected layout update failed");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function run() {
|
|
127
|
+
const result = request();
|
|
128
|
+
if (!result.ok) return result;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const cleanup = owner.onDispose(cancel);
|
|
132
|
+
return cleanup.ok ? ok({ request }) : cleanup;
|
|
133
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { UIElement } from "../src/elements";
|
|
2
|
+
import type { FrameDriver } from "../src/frame-scheduler";
|
|
3
|
+
import type { OwnedRecord } from "../src/owned-collections";
|
|
4
|
+
import { ref, type ReadonlyRef } from "../src/reactivity";
|
|
5
|
+
import { formatFailure, ok, type Failure, type Result } from "../src/result";
|
|
6
|
+
import { createRuntime, type Runtime, type RuntimeScope } from "../src/runtime";
|
|
7
|
+
import { unlockedActionSlot } from "./action-button";
|
|
8
|
+
import { clock } from "./clock";
|
|
9
|
+
import type { Frame } from "./frames";
|
|
10
|
+
import { api } from "./native";
|
|
11
|
+
import { physicalPixel } from "./pixel";
|
|
12
|
+
import { protect } from "./protect";
|
|
13
|
+
import { createProtectedLayoutUpdate } from "./protected-layout";
|
|
14
|
+
import { frameTiming } from "./frame-timing";
|
|
15
|
+
import { mountNative, type NativeViewOptions } from "./view";
|
|
16
|
+
|
|
17
|
+
interface ProtectedView {
|
|
18
|
+
refresh(this: void): Result<void>;
|
|
19
|
+
refreshDisplay(this: void): Result<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function report(error: Failure) {
|
|
23
|
+
api.geterrorhandler()(formatFailure(error));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Run only invalidated JSX effects when a protected view may write to the client. */
|
|
27
|
+
export function mountProtectedView(this: void, host: Runtime, owner: RuntimeScope,
|
|
28
|
+
render: (this: void, scope: RuntimeScope, refresh: ProtectedView["refresh"], pixel: ReadonlyRef<number>) => UIElement,
|
|
29
|
+
options: NativeViewOptions = {}): Result<ProtectedView> {
|
|
30
|
+
type Mount = Extract<ReturnType<typeof mountNative>, { ok: true }>["value"];
|
|
31
|
+
let view: Mount | undefined;
|
|
32
|
+
const pixel = ref(physicalPixel(api.UIParent as Frame));
|
|
33
|
+
const key = {};
|
|
34
|
+
let measure = false;
|
|
35
|
+
let flush = false;
|
|
36
|
+
|
|
37
|
+
function drive() {
|
|
38
|
+
if (!owner.isActive()) return;
|
|
39
|
+
const updated = update.request();
|
|
40
|
+
if (!updated.ok) return updated;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const driver: FrameDriver = {
|
|
44
|
+
nowMs: frameTiming.nowMs,
|
|
45
|
+
remainingMs: owner.frameJobs.remainingMs,
|
|
46
|
+
start: (_step, delay) => {
|
|
47
|
+
const enqueue = () => { if (owner.isActive()) owner.frameJobs.queue(key, drive); };
|
|
48
|
+
const cancel = delay > 0 ? clock(delay, enqueue) : undefined;
|
|
49
|
+
if (cancel === undefined) enqueue();
|
|
50
|
+
return () => {
|
|
51
|
+
cancel?.();
|
|
52
|
+
owner.frameJobs.cancel(key);
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const runtime = createRuntime(clock, report, protect, { driver });
|
|
57
|
+
const guarded = createProtectedLayoutUpdate(() => {
|
|
58
|
+
if (view !== undefined) {
|
|
59
|
+
if (measure) {
|
|
60
|
+
pixel.value = physicalPixel(view.frame as Frame);
|
|
61
|
+
measure = false;
|
|
62
|
+
}
|
|
63
|
+
const urgent = flush;
|
|
64
|
+
flush = false;
|
|
65
|
+
runtime.frameJobs.step(urgent);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const mounted = mountNative(api.UIParent, runtime, scope => render(scope, refresh, pixel), {
|
|
69
|
+
...options as OwnedRecord<NativeViewOptions>, bindings: undefined, initialLayout: "sync",
|
|
70
|
+
});
|
|
71
|
+
if (!mounted.ok) return mounted;
|
|
72
|
+
view = mounted.value;
|
|
73
|
+
const frame = view.frame as Frame;
|
|
74
|
+
frame.SetClampedToScreen(true);
|
|
75
|
+
pixel.value = physicalPixel(frame);
|
|
76
|
+
}, { scope: owner, label: "Protected JSX view" });
|
|
77
|
+
if (!guarded.ok) return guarded;
|
|
78
|
+
const update = guarded.value;
|
|
79
|
+
const refresh = () => { flush = true; return update.request(); };
|
|
80
|
+
|
|
81
|
+
const subscription = owner.onDispose(() => {
|
|
82
|
+
owner.frameJobs.cancel(key);
|
|
83
|
+
const mounted = view;
|
|
84
|
+
view = undefined;
|
|
85
|
+
if (mounted === undefined) return;
|
|
86
|
+
if (unlockedActionSlot()) return mounted.dispose();
|
|
87
|
+
const cleanup = host.scope();
|
|
88
|
+
const waiting = createProtectedLayoutUpdate(() => {
|
|
89
|
+
const disposed = mounted.dispose();
|
|
90
|
+
cleanup.dispose();
|
|
91
|
+
if (!disposed.ok) return disposed;
|
|
92
|
+
}, { scope: cleanup, label: "Dispose protected JSX view" });
|
|
93
|
+
if (waiting.ok) return waiting.value.request();
|
|
94
|
+
cleanup.dispose();
|
|
95
|
+
return waiting;
|
|
96
|
+
});
|
|
97
|
+
if (!subscription.ok) return subscription;
|
|
98
|
+
const mounted = refresh();
|
|
99
|
+
return mounted.ok ? ok({
|
|
100
|
+
refresh,
|
|
101
|
+
refreshDisplay: () => { measure = true; return update.request(); },
|
|
102
|
+
}) : mounted;
|
|
103
|
+
}
|