Open History to see the last commit
1import * as Effect from "effect/Effect";
2import type * as Fiber from "effect/Fiber";
3
4import * as Electron from "electron";
5
6const MIN_DURATION_MS = 280;
7const MAX_DURATION_MS = 680;
8const DURATION_DISTANCE_SCALE = 2_000;
9const EASING = "cubic-bezier(.2,.8,.2,1)";
10const TIMEOUT_MS = 6_000;
11
12type SnapShotAnimationDetails = {
13 readonly appName: string;
14 readonly windowTitle: string;
15 readonly appIconDataUrl?: string | undefined;
16};
17
18export type SnapShotAnimationDestination = {
19 readonly frame: Electron.Rectangle;
20 readonly relativeFrame?: Electron.Rectangle | undefined;
21 readonly backgroundColor: string;
22 readonly borderColor: string;
23 readonly borderWidth: number;
24 readonly cornerRadius: number;
25 readonly scaleFactor: number;
26 readonly details?: SnapShotAnimationDetails | undefined;
27};
28
29export function snapShotAnimationDurationMs(
30 source: Electron.Rectangle,
31 target: Electron.Rectangle,
32): number {
33 const distance = Math.hypot(
34 source.x + source.width / 2 - target.x - target.width / 2,
35 source.y + source.height / 2 - target.y - target.height / 2,
36 );
37 return (
38 MAX_DURATION_MS -
39 (MAX_DURATION_MS - MIN_DURATION_MS) * Math.exp(-distance / DURATION_DISTANCE_SCALE)
40 );
41}
42
43type ActiveTransition = {
44 readonly id: string;
45 readonly source: Electron.Rectangle;
46 readonly snapshotDataUrl: string;
47 readonly overlays: Array<{
48 readonly window: Electron.BrowserWindow;
49 readonly requestedBounds: Electron.Rectangle;
50 bounds: Electron.Rectangle;
51 }>;
52 details?: SnapShotAnimationDetails | undefined;
53 timer?: Fiber.Fiber<void> | undefined;
54 flight?: Promise<void> | undefined;
55 landed?: boolean | undefined;
56};
57
58type SnapShotTransitionOptions = {
59 readonly showWindow?: ((window: Electron.BaseWindow) => void) | undefined;
60 readonly boundOverlayToCaptureDisplays?: boolean | undefined;
61 readonly waitForCompositorFrame?: boolean | undefined;
62 readonly alwaysOnTopLevel?:
63 | NonNullable<Parameters<Electron.BrowserWindow["setAlwaysOnTop"]>[1]>
64 | undefined;
65};
66
67export function snapShotAnimationOverlayBounds(
68 displays: ReadonlyArray<Pick<Electron.Display, "bounds">>,
69): Electron.Rectangle {
70 const firstDisplay = displays[0];
71 const first = firstDisplay?.bounds ?? { x: 0, y: 0, width: 1, height: 1 };
72 let left = first.x;
73 let top = first.y;
74 let right = first.x + first.width;
75 let bottom = first.y + first.height;
76 for (const display of displays.slice(1)) {
77 const bounds = display.bounds;
78 left = Math.min(left, bounds.x);
79 top = Math.min(top, bounds.y);
80 right = Math.max(right, bounds.x + bounds.width);
81 bottom = Math.max(bottom, bounds.y + bounds.height);
82 }
83 return { x: left, y: top, width: right - left, height: bottom - top };
84}
85
86export function snapShotAnimationDisplayBounds(
87 displays: ReadonlyArray<Pick<Electron.Display, "bounds">>,
88 source: Electron.Rectangle,
89 destination: Electron.Rectangle,
90): Array<Electron.Rectangle> {
91 const flight = snapShotAnimationOverlayBounds([{ bounds: source }, { bounds: destination }]);
92 return displays
93 .map((display) => display.bounds)
94 .filter(
95 (bounds) =>
96 bounds.x < flight.x + flight.width &&
97 bounds.x + bounds.width > flight.x &&
98 bounds.y < flight.y + flight.height &&
99 bounds.y + bounds.height > flight.y,
100 );
101}
102
103function createWindow(
104 bounds: Electron.Rectangle,
105 alwaysOnTopLevel: SnapShotTransitionOptions["alwaysOnTopLevel"],
106): Electron.BrowserWindow {
107 const window = new Electron.BrowserWindow({
108 ...bounds,
109 alwaysOnTop: true,
110 backgroundColor: "#00000000",
111 focusable: false,
112 frame: false,
113 hasShadow: false,
114 resizable: false,
115 show: false,
116 skipTaskbar: true,
117 title: "T3 Code Snapshot Animation",
118 transparent: true,
119 webPreferences: {
120 backgroundThrottling: false,
121 contextIsolation: true,
122 nodeIntegration: false,
123 sandbox: true,
124 },
125 });
126 if (alwaysOnTopLevel) window.setAlwaysOnTop(true, alwaysOnTopLevel);
127 window.setIgnoreMouseEvents(true);
128 return window;
129}
130
131function transitionHtml(
132 sourceBounds: Electron.Rectangle,
133 overlayBounds: Electron.Rectangle,
134 flash: boolean,
135): string {
136 const source = {
137 x: sourceBounds.x - overlayBounds.x,
138 y: sourceBounds.y - overlayBounds.y,
139 width: sourceBounds.width,
140 height: sourceBounds.height,
141 };
142 return `<!doctype html><style>
143html,body{margin:0;width:100%;height:100%;overflow:hidden;background:transparent}
144#card{position:absolute;left:${source.x}px;top:${source.y}px;width:${source.width}px;height:${source.height}px;contain:strict;overflow:hidden;border-radius:12px;background:#fff;box-shadow:0 24px 70px rgba(0,0,0,.24);transform-origin:center;will-change:transform}
145#content{position:absolute;inset:0;overflow:hidden;border-radius:inherit;transform-origin:center;will-change:transform}
146#snapshot{position:absolute;inset:0;width:100%;height:100%;object-fit:fill;transform-origin:center;will-change:transform}
147#flash{position:absolute;inset:0;background:#fff;opacity:0;${flash ? "" : "display:none"}}
148#details{--scale:1;position:absolute;inset-inline:0;bottom:0;display:flex;align-items:center;gap:calc(6px * var(--scale));padding:calc(24px * var(--scale)) calc(10px * var(--scale)) calc(8px * var(--scale));background:linear-gradient(to top,rgba(0,0,0,.85),rgba(0,0,0,.55),transparent);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;opacity:0}
149#details:not([data-ready]){visibility:hidden}
150#icon,#fallback{width:calc(28px * var(--scale));height:calc(28px * var(--scale));flex:none;border-radius:calc(6px * var(--scale))}
151#icon{display:none;object-fit:cover}
152#fallback{display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.2);color:#fff;font-size:calc(10px * var(--scale));font-weight:500;text-transform:uppercase}
153#copy{min-width:0;flex:1}#app,#title{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;line-height:calc(14px * var(--scale))}
154#app{color:#fff;font-size:calc(11px * var(--scale));font-weight:500}#title{color:rgba(255,255,255,.7);font-size:calc(9px * var(--scale))}
155#border{position:absolute;z-index:2;inset:0;box-sizing:border-box;border:0 solid transparent;border-radius:inherit;opacity:0}
156</style><div id="card"><div id="content"><img id="snapshot"><div id="details"><img id="icon"><div id="fallback"></div><div id="copy"><div id="app"></div><div id="title"></div></div></div></div><div id="border"></div><div id="flash"></div></div><script>
157const source=${JSON.stringify(source)},card=document.getElementById("card"),content=document.getElementById("content"),snapshot=document.getElementById("snapshot"),details=document.getElementById("details"),border=document.getElementById("border");
158const afterPaint=()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)));
159window.setCaptureSnapshot=async src=>{snapshot.src=src;await snapshot.decode();await afterPaint()};
160window.rebaseCaptureSource=next=>{Object.assign(source,next);Object.assign(card.style,{left:source.x+"px",top:source.y+"px",width:source.width+"px",height:source.height+"px"})};
161window.startCaptureFlash=()=>document.getElementById("flash").animate([{opacity:.08},{offset:.38,opacity:.08},{offset:.68,opacity:.02},{opacity:0}],{duration:300,fill:"forwards",easing:"${EASING}"});
162const applyDetails=value=>{
163 if(!value)return;
164 details.dataset.ready="";
165 document.getElementById("app").textContent=value.appName;
166 document.getElementById("title").textContent=value.windowTitle||"Captured window";
167 document.getElementById("fallback").textContent=value.appName.slice(0,1);
168 if(value.appIconDataUrl){
169 const icon=document.getElementById("icon");icon.src=value.appIconDataUrl;icon.style.display="block";document.getElementById("fallback").style.display="none";
170 }
171};
172window.updateCaptureDetails=applyDetails;
173window.prepareCaptureTransition=async destination=>{
174 const target=destination.frame,borderWidth=Math.min(Math.max(0,destination.borderWidth),Math.max(0,(Math.min(target.width,target.height)-1)/2));
175 const inner={width:Math.max(1,target.width-borderWidth*2),height:Math.max(1,target.height-borderWidth*2)};
176 const sourceCenter={x:source.x+source.width/2,y:source.y+source.height/2},targetCenter={x:target.x+target.width/2,y:target.y+target.height/2};
177 const initial="translate3d("+(sourceCenter.x-targetCenter.x)+"px,"+(sourceCenter.y-targetCenter.y)+"px,0) scale("+(source.width/target.width)+","+(source.height/target.height)+")";
178 const imageWidth=Math.max(1,snapshot.naturalWidth||source.width),imageHeight=Math.max(1,snapshot.naturalHeight||source.height),cover=Math.max(inner.width/imageWidth,inner.height/imageHeight);
179 Object.assign(card.style,{left:target.x+"px",top:target.y+"px",width:target.width+"px",height:target.height+"px",borderRadius:destination.cornerRadius+"px",backgroundColor:destination.backgroundColor,boxShadow:"none"});
180 Object.assign(content.style,{inset:borderWidth+"px",borderRadius:Math.max(0,destination.cornerRadius-borderWidth)+"px"});
181 Object.assign(border.style,{borderWidth:borderWidth+"px",borderColor:destination.borderColor});
182 details.style.setProperty("--scale",String(destination.scaleFactor));
183 applyDetails(destination.details);
184 const options={duration:destination.durationMs,fill:"forwards",easing:"${EASING}"};
185 const animations=[
186 card.animate([{transform:initial},{transform:"translate3d(0,0,0) scale(1)"}],options),
187 content.animate([{transform:"scale("+(target.width/inner.width)+","+(target.height/inner.height)+")"},{transform:"scale(1)"}],options),
188 snapshot.animate([{transform:"scale(1)"},{transform:"scale("+((imageWidth*cover)/inner.width)+","+((imageHeight*cover)/inner.height)+")"}],options),
189 border.animate([{opacity:0},{opacity:1}],options),
190 details.animate([{opacity:0},{opacity:1}],{delay:Math.max(0,destination.durationMs-125),duration:125,fill:"both",easing:"ease-in"})
191 ];
192 for(const animation of animations){animation.pause();animation.currentTime=0}
193 window.playCaptureTransition=async()=>{
194 for(const animation of animations)animation.play();
195 await Promise.all(animations.map(animation=>animation.finished.catch(()=>undefined)));
196 await new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)));
197 };
198 await afterPaint();
199};
200</script>`;
201}
202
203export class SnapShotTransition {
204 private active: ActiveTransition | undefined;
205 private readonly boundOverlayToCaptureDisplays: boolean;
206 private readonly waitForCompositorFrame: boolean;
207 private readonly alwaysOnTopLevel: SnapShotTransitionOptions["alwaysOnTopLevel"];
208 private readonly showWindow: (window: Electron.BaseWindow) => void;
209
210 get isAnimating(): boolean {
211 return this.active?.flight !== undefined && !this.active.landed;
212 }
213
214 constructor(options: SnapShotTransitionOptions = {}) {
215 this.boundOverlayToCaptureDisplays = options.boundOverlayToCaptureDisplays ?? false;
216 this.waitForCompositorFrame = options.waitForCompositorFrame ?? false;
217 this.alwaysOnTopLevel = options.alwaysOnTopLevel;
218 this.showWindow = options.showWindow ?? ((window) => window.showInactive());
219 }
220
221 async begin(
222 id: string,
223 source: Electron.Rectangle,
224 snapshotDataUrl: string,
225 flash: boolean,
226 destinationWindowBounds?: Electron.Rectangle,
227 ): Promise<void> {
228 this.dispose();
229 const sourceDisplay = Electron.screen.getDisplayMatching(source);
230 const requestedBounds = this.boundOverlayToCaptureDisplays
231 ? snapShotAnimationDisplayBounds(
232 [
233 sourceDisplay,
234 ...Electron.screen.getAllDisplays().filter((d) => d.id !== sourceDisplay.id),
235 ],
236 source,
237 destinationWindowBounds ?? source,
238 )
239 : [snapShotAnimationOverlayBounds(Electron.screen.getAllDisplays())];
240 const active: ActiveTransition = {
241 id,
242 source,
243 snapshotDataUrl,
244 overlays: [],
245 };
246 active.timer = Effect.runFork(
247 Effect.sleep(TIMEOUT_MS).pipe(
248 Effect.andThen(
249 Effect.sync(() => {
250 if (this.active === active) this.dispose();
251 }),
252 ),
253 ),
254 );
255 this.active = active;
256 try {
257 await this.showOverlays(active, requestedBounds, flash);
258 } catch (error) {
259 if (this.active === active) this.dispose();
260 throw error;
261 }
262 }
263
264 private async showOverlays(
265 active: ActiveTransition,
266 requestedBounds: ReadonlyArray<Electron.Rectangle>,
267 flash: boolean,
268 ): Promise<void> {
269 const overlays: ActiveTransition["overlays"] = [];
270 try {
271 for (const bounds of requestedBounds) {
272 const window = createWindow(bounds, this.alwaysOnTopLevel);
273 const overlay = { window, requestedBounds: bounds, bounds: window.getBounds() };
274 active.overlays.push(overlay);
275 overlays.push(overlay);
276 }
277 await Promise.all(
278 overlays.map(async (overlay) => {
279 await overlay.window.loadURL(
280 "data:text/html;charset=utf-8," +
281 encodeURIComponent(transitionHtml(active.source, overlay.bounds, flash)),
282 );
283 if (this.active === active && !overlay.window.isDestroyed()) {
284 await overlay.window.webContents.executeJavaScript(
285 `window.setCaptureSnapshot(${JSON.stringify(active.snapshotDataUrl)})`,
286 );
287 }
288 }),
289 );
290 for (const overlay of overlays) {
291 if (this.active !== active) return;
292 if (overlay.window.isDestroyed()) continue;
293 this.showWindow(overlay.window);
294 const bounds = overlay.window.getBounds();
295 if (
296 bounds.x !== overlay.bounds.x ||
297 bounds.y !== overlay.bounds.y ||
298 bounds.width !== overlay.bounds.width ||
299 bounds.height !== overlay.bounds.height
300 ) {
301 overlay.bounds = bounds;
302 await overlay.window.webContents.executeJavaScript(
303 `window.rebaseCaptureSource(${JSON.stringify({
304 x: active.source.x - bounds.x,
305 y: active.source.y - bounds.y,
306 width: active.source.width,
307 height: active.source.height,
308 })})`,
309 );
310 }
311 }
312 if (this.waitForCompositorFrame) {
313 // Cover the source with a composited snapshot before the caller reveals T3.
314 // Decoding and renderer animation frames alone can leave a transparent gap.
315 await Promise.all(
316 overlays.map(async (overlay) => {
317 if (this.active !== active || overlay.window.isDestroyed()) return;
318 await overlay.window.webContents.capturePage({ x: 0, y: 0, width: 1, height: 1 });
319 }),
320 );
321 }
322 if (this.active !== active) return;
323 if (flash) {
324 await Promise.all(
325 overlays.map(async (overlay) => {
326 if (overlay.window.isDestroyed()) return;
327 await overlay.window.webContents
328 .executeJavaScript("window.startCaptureFlash()")
329 .catch(() => undefined);
330 }),
331 );
332 }
333 } catch (error) {
334 for (const overlay of overlays) {
335 if (!overlay.window.isDestroyed()) overlay.window.destroy();
336 }
337 throw error;
338 }
339 }
340
341 animateTo(id: string, destination: SnapShotAnimationDestination): void {
342 const active = this.active;
343 if (!active || active.id !== id) return;
344 if (destination.details) active.details = destination.details;
345 if (active.flight) {
346 if (destination.details) {
347 for (const overlay of active.overlays) {
348 if (overlay.window.isDestroyed()) continue;
349 void overlay.window.webContents
350 .executeJavaScript(
351 "window.updateCaptureDetails(" + JSON.stringify(destination.details) + ")",
352 )
353 .catch(() => undefined);
354 }
355 }
356 return;
357 }
358 active.flight = this.runFlight(active, destination).finally(() => {
359 active.landed = true;
360 });
361 }
362
363 private async runFlight(
364 active: ActiveTransition,
365 destination: SnapShotAnimationDestination,
366 ): Promise<void> {
367 if (this.active !== active) return;
368 if (this.boundOverlayToCaptureDisplays) {
369 const missingBounds = snapShotAnimationDisplayBounds(
370 Electron.screen.getAllDisplays(),
371 active.source,
372 destination.frame,
373 ).filter(
374 (bounds) =>
375 !active.overlays.some(
376 (overlay) =>
377 !overlay.window.isDestroyed() &&
378 overlay.requestedBounds.x === bounds.x &&
379 overlay.requestedBounds.y === bounds.y &&
380 overlay.requestedBounds.width === bounds.width &&
381 overlay.requestedBounds.height === bounds.height,
382 ),
383 );
384 if (missingBounds.length > 0) {
385 await this.showOverlays(active, missingBounds, false).catch(() => undefined);
386 }
387 }
388 if (this.active !== active) return;
389 const durationMs = snapShotAnimationDurationMs(active.source, destination.frame);
390 const prepared = await Promise.allSettled(
391 active.overlays.map(async (overlay) => {
392 if (overlay.window.isDestroyed()) return;
393 await overlay.window.webContents.executeJavaScript(
394 `window.prepareCaptureTransition(${JSON.stringify({
395 ...destination,
396 frame: {
397 x: destination.frame.x - overlay.bounds.x,
398 y: destination.frame.y - overlay.bounds.y,
399 width: destination.frame.width,
400 height: destination.frame.height,
401 },
402 borderWidth: Math.max(0, destination.borderWidth),
403 cornerRadius: Math.max(0, destination.cornerRadius),
404 scaleFactor: Math.max(0.1, destination.scaleFactor),
405 details: active.details,
406 durationMs,
407 })})`,
408 );
409 if (this.active !== active || overlay.window.isDestroyed()) return;
410 if (this.waitForCompositorFrame) {
411 // Read back one pixel after preparation to flush queued compositor work
412 // before the animation clock starts on any of the display surfaces.
413 await overlay.window.webContents.capturePage({ x: 0, y: 0, width: 1, height: 1 });
414 }
415 return overlay;
416 }),
417 );
418 if (this.active !== active) return;
419 await Promise.allSettled(
420 prepared.map((result) => {
421 if (result.status !== "fulfilled" || !result.value || result.value.window.isDestroyed()) {
422 return;
423 }
424 return result.value.window.webContents.executeJavaScript("window.playCaptureTransition()");
425 }),
426 );
427 }
428
429 async waitForLanding(id: string): Promise<void> {
430 const active = this.active;
431 if (!active || active.id !== id) return;
432 await active.flight?.catch(() => undefined);
433 }
434
435 async complete(id: string): Promise<void> {
436 const active = this.active;
437 if (!active || active.id !== id) return;
438 await this.waitForLanding(id);
439 if (this.active === active) this.dispose();
440 }
441
442 dismiss(id: string): void {
443 if (this.active?.id === id) this.dispose();
444 }
445
446 dispose(): void {
447 const active = this.active;
448 this.active = undefined;
449 if (!active) return;
450 active.timer?.interruptUnsafe();
451 for (const overlay of active.overlays) {
452 if (!overlay.window.isDestroyed()) overlay.window.destroy();
453 }
454 }
455}