Open History to see the last commit
1import { create } from "zustand";
2
3export interface BrowserSurfaceRect {
4 readonly x: number;
5 readonly y: number;
6 readonly width: number;
7 readonly height: number;
8}
9
10export interface BrowserSurfacePresentation {
11 readonly rect: BrowserSurfaceRect | null;
12 readonly visible: boolean;
13 readonly zIndex: number;
14 readonly content: BrowserSurfaceContentPresentation | null;
15 readonly fittedSourceContent: BrowserSurfaceContentPresentation | null;
16 readonly fitSourceContent: boolean;
17 readonly cornerRadius: number;
18 readonly updatedAt: number;
19 readonly owner: symbol | null;
20}
21
22export interface BrowserSurfaceContentPresentation {
23 readonly x: number;
24 readonly y: number;
25 readonly width: number;
26 readonly height: number;
27 readonly scale: number;
28 readonly scrollLeft: number;
29 readonly scrollTop: number;
30}
31
32interface BrowserSurfaceStoreState {
33 readonly activityByTabId: Record<string, number>;
34 readonly byTabId: Record<string, BrowserSurfacePresentation>;
35 readonly acquireActivity: (tabId: string) => () => void;
36 readonly claim: (tabId: string, owner: symbol, fitSourceContent: boolean) => void;
37 readonly present: (
38 tabId: string,
39 owner: symbol,
40 rect: BrowserSurfaceRect,
41 visible: boolean,
42 cornerRadius: number,
43 zIndex: number,
44 ) => void;
45 readonly presentContent: (tabId: string, content: BrowserSurfaceContentPresentation) => void;
46 readonly release: (tabId: string, owner: symbol) => void;
47}
48
49export interface BrowserSurfaceLease {
50 readonly present: (
51 rect: BrowserSurfaceRect,
52 visible: boolean,
53 cornerRadius?: number,
54 zIndex?: number,
55 ) => boolean;
56 readonly release: () => void;
57}
58
59/** DOM id of a tab's fixed webview layer, so a slot can claim it with aria-owns. */
60export const browserSurfaceElementId = (tabId: string): string =>
61 `browser-surface-${encodeURIComponent(tabId)}`;
62
63export function resolveBrowserSurfacePanelRect(
64 byTabId: Readonly<Record<string, BrowserSurfacePresentation>>,
65 tabId: string,
66): BrowserSurfaceRect | null {
67 const current = byTabId[tabId];
68 return current?.rect ?? null;
69}
70
71const rectEquals = (left: BrowserSurfaceRect | null, right: BrowserSurfaceRect): boolean =>
72 left !== null &&
73 left.x === right.x &&
74 left.y === right.y &&
75 left.width === right.width &&
76 left.height === right.height;
77
78export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) => ({
79 activityByTabId: {},
80 byTabId: {},
81 acquireActivity: (tabId) => {
82 let released = false;
83 set((state) => ({
84 activityByTabId: {
85 ...state.activityByTabId,
86 [tabId]: (state.activityByTabId[tabId] ?? 0) + 1,
87 },
88 }));
89 return () => {
90 if (released) return;
91 released = true;
92 set((state) => {
93 const count = state.activityByTabId[tabId] ?? 0;
94 const activityByTabId = { ...state.activityByTabId };
95 if (count <= 1) delete activityByTabId[tabId];
96 else activityByTabId[tabId] = count - 1;
97 return { activityByTabId };
98 });
99 };
100 },
101 claim: (tabId, owner, fitSourceContent) =>
102 set((state) => {
103 const current = state.byTabId[tabId];
104 if (current?.owner === owner) return state;
105 return {
106 byTabId: {
107 ...state.byTabId,
108 [tabId]: {
109 rect: current?.rect ?? null,
110 visible: false,
111 zIndex: current?.zIndex ?? 30,
112 content: current?.content ?? null,
113 fittedSourceContent: fitSourceContent ? (current?.content ?? null) : null,
114 fitSourceContent,
115 cornerRadius: current?.cornerRadius ?? 0,
116 updatedAt: Date.now(),
117 owner,
118 },
119 },
120 };
121 }),
122 present: (tabId, owner, rect, visible, cornerRadius, zIndex) =>
123 set((state) => {
124 const current = state.byTabId[tabId];
125 if (current?.owner !== owner) return state;
126 if (
127 current &&
128 current.visible === visible &&
129 current.cornerRadius === cornerRadius &&
130 current.zIndex === zIndex &&
131 rectEquals(current.rect, rect)
132 ) {
133 return state;
134 }
135 return {
136 byTabId: {
137 ...state.byTabId,
138 [tabId]: { ...current, rect, visible, cornerRadius, zIndex, updatedAt: Date.now() },
139 },
140 };
141 }),
142 presentContent: (tabId, content) =>
143 set((state) => {
144 const current = state.byTabId[tabId];
145 if (!current) {
146 return {
147 byTabId: {
148 ...state.byTabId,
149 [tabId]: {
150 rect: null,
151 visible: false,
152 zIndex: 30,
153 content,
154 fittedSourceContent: null,
155 fitSourceContent: false,
156 cornerRadius: 0,
157 updatedAt: Date.now(),
158 owner: null,
159 },
160 },
161 };
162 }
163 const previous = current.content;
164 if (
165 previous &&
166 previous.x === content.x &&
167 previous.y === content.y &&
168 previous.width === content.width &&
169 previous.height === content.height &&
170 previous.scale === content.scale &&
171 previous.scrollLeft === content.scrollLeft &&
172 previous.scrollTop === content.scrollTop
173 ) {
174 return state;
175 }
176 return {
177 byTabId: {
178 ...state.byTabId,
179 [tabId]: {
180 ...current,
181 content,
182 fittedSourceContent:
183 current.fitSourceContent && current.fittedSourceContent === null
184 ? content
185 : current.fittedSourceContent,
186 updatedAt: Date.now(),
187 },
188 },
189 };
190 }),
191 release: (tabId, owner) =>
192 set((state) => {
193 const current = state.byTabId[tabId];
194 if (current?.owner !== owner) return state;
195 return {
196 byTabId: {
197 ...state.byTabId,
198 [tabId]: {
199 ...current,
200 visible: false,
201 fittedSourceContent: null,
202 fitSourceContent: false,
203 updatedAt: Date.now(),
204 owner: null,
205 },
206 },
207 };
208 }),
209}));
210
211export const acquireBrowserSurfaceActivity = (tabId: string): (() => void) =>
212 useBrowserSurfaceStore.getState().acquireActivity(tabId);
213
214export function acquireBrowserSurface(
215 tabId: string,
216 fitSourceContent = false,
217): BrowserSurfaceLease {
218 const owner = Symbol(`browser-surface:${tabId}`);
219 let released = false;
220 useBrowserSurfaceStore.getState().claim(tabId, owner, fitSourceContent);
221
222 return {
223 present: (rect, visible, cornerRadius = 0, zIndex = 30) => {
224 if (released) return false;
225 if (useBrowserSurfaceStore.getState().byTabId[tabId]?.owner !== owner) return false;
226 useBrowserSurfaceStore.getState().present(tabId, owner, rect, visible, cornerRadius, zIndex);
227 return true;
228 },
229 release: () => {
230 if (released) return;
231 released = true;
232 useBrowserSurfaceStore.getState().release(tabId, owner);
233 },
234 };
235}