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 content: BrowserSurfaceContentPresentation | null;
14 readonly fittedSourceContent: BrowserSurfaceContentPresentation | null;
15 readonly fitSourceContent: boolean;
16 readonly cornerRadius: number;
17 readonly updatedAt: number;
18 readonly owner: symbol | null;
19}
20
21export interface BrowserSurfaceContentPresentation {
22 readonly x: number;
23 readonly y: number;
24 readonly width: number;
25 readonly height: number;
26 readonly scale: number;
27 readonly scrollLeft: number;
28 readonly scrollTop: number;
29}
30
31interface BrowserSurfaceStoreState {
32 readonly byTabId: Record<string, BrowserSurfacePresentation>;
33 readonly claim: (tabId: string, owner: symbol, fitSourceContent: boolean) => void;
34 readonly present: (
35 tabId: string,
36 owner: symbol,
37 rect: BrowserSurfaceRect,
38 visible: boolean,
39 cornerRadius: number,
40 ) => void;
41 readonly presentContent: (tabId: string, content: BrowserSurfaceContentPresentation) => void;
42 readonly release: (tabId: string, owner: symbol) => void;
43}
44
45export interface BrowserSurfaceLease {
46 readonly present: (rect: BrowserSurfaceRect, visible: boolean, cornerRadius?: number) => boolean;
47 readonly release: () => void;
48}
49
50export function resolveBrowserSurfacePanelRect(
51 byTabId: Readonly<Record<string, BrowserSurfacePresentation>>,
52 tabId: string,
53): BrowserSurfaceRect | null {
54 const current = byTabId[tabId];
55 return current?.rect ?? null;
56}
57
58const rectEquals = (left: BrowserSurfaceRect | null, right: BrowserSurfaceRect): boolean =>
59 left !== null &&
60 left.x === right.x &&
61 left.y === right.y &&
62 left.width === right.width &&
63 left.height === right.height;
64
65export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) => ({
66 byTabId: {},
67 claim: (tabId, owner, fitSourceContent) =>
68 set((state) => {
69 const current = state.byTabId[tabId];
70 if (current?.owner === owner) return state;
71 return {
72 byTabId: {
73 ...state.byTabId,
74 [tabId]: {
75 rect: current?.rect ?? null,
76 visible: false,
77 content: current?.content ?? null,
78 fittedSourceContent: fitSourceContent ? (current?.content ?? null) : null,
79 fitSourceContent,
80 cornerRadius: current?.cornerRadius ?? 0,
81 updatedAt: Date.now(),
82 owner,
83 },
84 },
85 };
86 }),
87 present: (tabId, owner, rect, visible, cornerRadius) =>
88 set((state) => {
89 const current = state.byTabId[tabId];
90 if (current?.owner !== owner) return state;
91 if (
92 current &&
93 current.visible === visible &&
94 current.cornerRadius === cornerRadius &&
95 rectEquals(current.rect, rect)
96 ) {
97 return state;
98 }
99 return {
100 byTabId: {
101 ...state.byTabId,
102 [tabId]: { ...current, rect, visible, cornerRadius, updatedAt: Date.now() },
103 },
104 };
105 }),
106 presentContent: (tabId, content) =>
107 set((state) => {
108 const current = state.byTabId[tabId];
109 if (!current) {
110 return {
111 byTabId: {
112 ...state.byTabId,
113 [tabId]: {
114 rect: null,
115 visible: false,
116 content,
117 fittedSourceContent: null,
118 fitSourceContent: false,
119 cornerRadius: 0,
120 updatedAt: Date.now(),
121 owner: null,
122 },
123 },
124 };
125 }
126 const previous = current.content;
127 if (
128 previous &&
129 previous.x === content.x &&
130 previous.y === content.y &&
131 previous.width === content.width &&
132 previous.height === content.height &&
133 previous.scale === content.scale &&
134 previous.scrollLeft === content.scrollLeft &&
135 previous.scrollTop === content.scrollTop
136 ) {
137 return state;
138 }
139 return {
140 byTabId: {
141 ...state.byTabId,
142 [tabId]: {
143 ...current,
144 content,
145 fittedSourceContent:
146 current.fitSourceContent && current.fittedSourceContent === null
147 ? content
148 : current.fittedSourceContent,
149 updatedAt: Date.now(),
150 },
151 },
152 };
153 }),
154 release: (tabId, owner) =>
155 set((state) => {
156 const current = state.byTabId[tabId];
157 if (current?.owner !== owner) return state;
158 return {
159 byTabId: {
160 ...state.byTabId,
161 [tabId]: {
162 ...current,
163 visible: false,
164 fittedSourceContent: null,
165 fitSourceContent: false,
166 updatedAt: Date.now(),
167 owner: null,
168 },
169 },
170 };
171 }),
172}));
173
174export function acquireBrowserSurface(
175 tabId: string,
176 fitSourceContent = false,
177): BrowserSurfaceLease {
178 const owner = Symbol(`browser-surface:${tabId}`);
179 let released = false;
180 useBrowserSurfaceStore.getState().claim(tabId, owner, fitSourceContent);
181
182 return {
183 present: (rect, visible, cornerRadius = 0) => {
184 if (released) return false;
185 if (useBrowserSurfaceStore.getState().byTabId[tabId]?.owner !== owner) return false;
186 useBrowserSurfaceStore.getState().present(tabId, owner, rect, visible, cornerRadius);
187 return true;
188 },
189 release: () => {
190 if (released) return;
191 released = true;
192 useBrowserSurfaceStore.getState().release(tabId, owner);
193 },
194 };
195}