@edenapp/solid-kit 0.10.1 → 0.10.2
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/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/overlayLayout.ts +206 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edenapp/solid-kit",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"description": "High-level SolidJS UI primitives for Eden apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"typescript": "^5.3.0",
|
|
31
|
-
"@edenapp/types": "0.10.
|
|
31
|
+
"@edenapp/types": "0.10.2"
|
|
32
32
|
},
|
|
33
33
|
"keywords": [
|
|
34
34
|
"eden",
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { ViewBounds, WindowSize } from "@edenapp/types";
|
|
2
|
+
import {
|
|
3
|
+
type Accessor,
|
|
4
|
+
createEffect,
|
|
5
|
+
createSignal,
|
|
6
|
+
onCleanup,
|
|
7
|
+
onMount,
|
|
8
|
+
} from "solid-js";
|
|
9
|
+
|
|
10
|
+
type MaybeAccessor<T> = T | Accessor<T>;
|
|
11
|
+
|
|
12
|
+
export interface OverlayLayoutContext {
|
|
13
|
+
element: HTMLElement;
|
|
14
|
+
contentRect: DOMRectReadOnly;
|
|
15
|
+
scale: number;
|
|
16
|
+
windowSize: WindowSize;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface OverlayLayoutOptions {
|
|
20
|
+
element: Accessor<HTMLElement | null | undefined>;
|
|
21
|
+
deriveBounds: (
|
|
22
|
+
context: OverlayLayoutContext,
|
|
23
|
+
) => ViewBounds | null | undefined;
|
|
24
|
+
enabled?: MaybeAccessor<boolean>;
|
|
25
|
+
dependencies?: Accessor<unknown>[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface OverlayLayoutController {
|
|
29
|
+
scale: Accessor<number>;
|
|
30
|
+
windowSize: Accessor<WindowSize | null>;
|
|
31
|
+
refresh: () => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const DEFAULT_WINDOW_SIZE: WindowSize = { width: 800, height: 600 };
|
|
35
|
+
|
|
36
|
+
const readMaybeAccessor = <T>(
|
|
37
|
+
value: MaybeAccessor<T> | undefined,
|
|
38
|
+
fallback: T,
|
|
39
|
+
) =>
|
|
40
|
+
typeof value === "function" ? (value as Accessor<T>)() : (value ?? fallback);
|
|
41
|
+
|
|
42
|
+
const sameBounds = (a: ViewBounds | null, b: ViewBounds) =>
|
|
43
|
+
!!a &&
|
|
44
|
+
a.x === b.x &&
|
|
45
|
+
a.y === b.y &&
|
|
46
|
+
a.width === b.width &&
|
|
47
|
+
a.height === b.height;
|
|
48
|
+
|
|
49
|
+
export function createInterfaceScale(): Accessor<number> {
|
|
50
|
+
const [scale, setScale] = createSignal(1);
|
|
51
|
+
|
|
52
|
+
onMount(() => {
|
|
53
|
+
const handleScaleChanged = (data: { scale: number }) => {
|
|
54
|
+
if (Number.isFinite(data.scale)) {
|
|
55
|
+
setScale(data.scale);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
void window.edenAPI
|
|
60
|
+
.shellCommand("view/get-interface-scale", {})
|
|
61
|
+
.then((result) => {
|
|
62
|
+
if (Number.isFinite(result.scale)) {
|
|
63
|
+
setScale(result.scale);
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
.catch((error) => {
|
|
67
|
+
console.error("Failed to load interface scale:", error);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
void window.edenAPI
|
|
71
|
+
.subscribe("view/interface-scale-changed", handleScaleChanged)
|
|
72
|
+
.catch((error) => {
|
|
73
|
+
console.error("Failed to subscribe to interface scale changes:", error);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
onCleanup(() => {
|
|
77
|
+
window.edenAPI.unsubscribe(
|
|
78
|
+
"view/interface-scale-changed",
|
|
79
|
+
handleScaleChanged,
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return scale;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function createOverlayLayout(
|
|
88
|
+
options: OverlayLayoutOptions,
|
|
89
|
+
): OverlayLayoutController {
|
|
90
|
+
const scale = createInterfaceScale();
|
|
91
|
+
const [windowSize, setWindowSize] = createSignal<WindowSize | null>(null);
|
|
92
|
+
const [contentRect, setContentRect] = createSignal<DOMRectReadOnly | null>(
|
|
93
|
+
null,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
let animationFrame: number | null = null;
|
|
97
|
+
let lastBounds: ViewBounds | null = null;
|
|
98
|
+
|
|
99
|
+
const updateBounds = () => {
|
|
100
|
+
animationFrame = null;
|
|
101
|
+
|
|
102
|
+
if (!readMaybeAccessor(options.enabled, true)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const element = options.element();
|
|
107
|
+
const rect = contentRect();
|
|
108
|
+
const size = windowSize();
|
|
109
|
+
if (!element || !rect || !size) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const bounds = options.deriveBounds({
|
|
114
|
+
element,
|
|
115
|
+
contentRect: rect,
|
|
116
|
+
scale: scale(),
|
|
117
|
+
windowSize: size,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (!bounds || sameBounds(lastBounds, bounds)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
lastBounds = bounds;
|
|
125
|
+
void window.edenAPI
|
|
126
|
+
.shellCommand("view/update-bounds", { bounds })
|
|
127
|
+
.catch((error) => {
|
|
128
|
+
console.error("Failed to update overlay bounds:", error);
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const scheduleRefresh = () => {
|
|
133
|
+
if (animationFrame !== null) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
animationFrame = window.requestAnimationFrame(updateBounds);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
createEffect(() => {
|
|
141
|
+
const element = options.element();
|
|
142
|
+
if (!element) {
|
|
143
|
+
setContentRect(null);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
setContentRect(element.getBoundingClientRect());
|
|
148
|
+
scheduleRefresh();
|
|
149
|
+
|
|
150
|
+
const observer = new ResizeObserver((entries) => {
|
|
151
|
+
const entry = entries[0];
|
|
152
|
+
if (!entry) return;
|
|
153
|
+
setContentRect(entry.contentRect);
|
|
154
|
+
scheduleRefresh();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
observer.observe(element);
|
|
158
|
+
onCleanup(() => observer.disconnect());
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
createEffect(() => {
|
|
162
|
+
scale();
|
|
163
|
+
windowSize();
|
|
164
|
+
readMaybeAccessor(options.enabled, true);
|
|
165
|
+
for (const dependency of options.dependencies ?? []) {
|
|
166
|
+
dependency();
|
|
167
|
+
}
|
|
168
|
+
scheduleRefresh();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
onMount(() => {
|
|
172
|
+
const handleBoundsChanged = (data: { windowSize: WindowSize }) => {
|
|
173
|
+
setWindowSize(data.windowSize);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
void window.edenAPI
|
|
177
|
+
.shellCommand("view/window-size", {})
|
|
178
|
+
.then(setWindowSize)
|
|
179
|
+
.catch((error) => {
|
|
180
|
+
console.error("Failed to load window size:", error);
|
|
181
|
+
setWindowSize(DEFAULT_WINDOW_SIZE);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
void window.edenAPI
|
|
185
|
+
.subscribe("view/global-bounds-changed", handleBoundsChanged)
|
|
186
|
+
.catch((error) => {
|
|
187
|
+
console.error("Failed to subscribe to window bounds changes:", error);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
onCleanup(() => {
|
|
191
|
+
window.edenAPI.unsubscribe(
|
|
192
|
+
"view/global-bounds-changed",
|
|
193
|
+
handleBoundsChanged,
|
|
194
|
+
);
|
|
195
|
+
if (animationFrame !== null) {
|
|
196
|
+
window.cancelAnimationFrame(animationFrame);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
scale,
|
|
203
|
+
windowSize,
|
|
204
|
+
refresh: scheduleRefresh,
|
|
205
|
+
};
|
|
206
|
+
}
|