Edge-Drop is a clipboard shelf on the edge of your screen. Hover to open it. Drag files and images into any app, click to copy text and links. Free and open-source for Windows.
TypeScript clipboardclipboard-managerdesktop-appdrag-and-dropelectronopen-sourceproductivityproductivity-toolwindows
perf(drag): stop drag ghosts from freezing the shelf while scrolling and stacking
Deepender25cb350be51d9d
diff --git a/electron/main/drag.ts b/electron/main/drag.tsindex 5408c17..a8ed25a 100644--- a/electron/main/drag.ts+++ b/electron/main/drag.ts@@ -15,13 +15,15 @@*/import { app, nativeImage, type WebContents } from 'electron'import { Resvg } from '@resvg/resvg-js'-import { copyFileSync, mkdirSync, writeFileSync, existsSync, statSync } from 'node:fs'+import { copyFileSync, mkdirSync, writeFileSync, existsSync, statSync, readFileSync, writeFile, readdir, unlink } from 'node:fs'import { join, extname } from 'node:path'-import { getUnpackagedTempDir, toUnpackagedFilePaths } from '../store/paths'+import { createHash } from 'node:crypto'+import { PATHS, getUnpackagedTempDir, toUnpackagedFilePaths } from '../store/paths'import type { DragRequest, ItemData } from '../../shared/types'import { getStore } from './state'-import { fileKindOf } from '../../src/lib/fileKind'+import { fileKindOf, type FileKind } from '../../src/lib/fileKind'import { recordStagedFiles } from './stagedTemp'+import { uiMotionLeftMs } from './thumbnailCache'function stampForFilename(capturedAt?: number): string {const d = capturedAt ? new Date(capturedAt) : new Date()@@ -157,8 +159,41 @@ function getStagedCacheKey(data: ItemData, capturedAt?: number): string {}}+/**+ * How long the pointer must rest on a card before its drag is prepared.+ * Hover sends a request for every card the pointer crosses, also while the+ * list scrolls under a still pointer; preparing each one (file copies, ghost+ * art) on the main thread stalled the scroll, since every frame of this+ * window passes through the main thread. Only the card the pointer settles on+ * is prepared, and never while the panel is still opening or closing; a drag+ * that starts sooner prepares itself in startDragOut.+ */+const PRESTAGE_REST_MS = 150+let prestageTimer: ReturnType<typeof setTimeout> | null = null+let prestageReq: DragRequest | null = null+/** Pre-stage a drag request in the background so drag initiation is 0ms. */export function prestageDrag(req: DragRequest): void {+ prestageReq = req+ schedulePrestage(PRESTAGE_REST_MS)+}++function schedulePrestage(ms: number): void {+ if (prestageTimer !== null) clearTimeout(prestageTimer)+ prestageTimer = setTimeout(() => {+ prestageTimer = null+ const motion = uiMotionLeftMs()+ if (motion > 0) {+ schedulePrestage(motion)+ return+ }+ const next = prestageReq+ prestageReq = null+ if (next) prestageNow(next)+ }, ms)+}++function prestageNow(req: DragRequest): void {try {const resolved = resolveDragData(req)if (!resolved) return@@ -374,7 +409,97 @@ function dragIcon(data: ItemData): Electron.NativeImage {return getFileDragIcon()}-import { buildFileDragSvg, dragSvgHasArt } from './fileSvg'+import { buildFileDragSvg, buildDragSvgFromArt, dragSvgHasArt } from './fileSvg'++/*+ * Ghost rasterizing cost, measured: Resvg loads every installed font unless+ * told otherwise (~150ms per call), and each piece of file art runs blur and+ * grain filters (~50ms). A stack paid both, plus the art three times: 200-550ms+ * on the main thread, a frozen shelf. Now no system fonts are loaded (the count+ * badge gets Segoe UI Bold alone), each kind's art is rasterized once and kept+ * on disk, and ghosts are composed from those bitmaps (single ~5ms, stack ~18ms).+ */+const GHOST_ZOOM = 2+/** Art bitmap width: one stack slot (512 units of the 142px stack ghost) at GHOST_ZOOM. */+const ART_PX = 142 * GHOST_ZOOM+const NO_FONTS = { loadSystemFonts: false }++type GhostFonts = { loadSystemFonts: boolean; fontFiles?: string[]; defaultFontFamily?: string; sansSerifFamily?: string }+const ghostFonts = new Map<string, GhostFonts>()+/**+ * Fonts for ghost text: just the named Segoe UI files from the Windows font+ * folder (each one parsed per render, so only what the text uses); the full+ * system scan only if one is missing.+ */+function ghostTextFonts(names: string[]): GhostFonts {+ const key = names.join('|')+ const hit = ghostFonts.get(key)+ if (hit) return hit+ const dir = join(process.env.WINDIR || 'C:\\Windows', 'Fonts')+ const files = names.map((f) => join(dir, f))+ const fonts = files.every((f) => existsSync(f))+ ? { loadSystemFonts: false, fontFiles: files, defaultFontFamily: 'Segoe UI', sansSerifFamily: 'Segoe UI' }+ : { loadSystemFonts: true }+ ghostFonts.set(key, fonts)+ return fonts+}+/** The stack count badge: bold only. */+const BADGE_FONTS = ['segoeuib.ttf']+/** The text ghost: regular and semibold lines. */+const TEXT_FONTS = ['segoeui.ttf', 'seguisb.ttf']++/** Rasterized art per kind used this session (~60 KB of PNG each, at most 13). */+const artPngCache = new Map<string, Buffer>()++/**+ * One kind's art as a PNG of ART_PX: from memory, else from the disk cache,+ * else rendered once (and saved, so later sessions skip it). Null when no art+ * can be read at all. `remember: false` only makes sure it is on disk.+ */+function kindArtPng(kind: FileKind, remember = true): Buffer | null {+ const svg = buildFileDragSvg([kind], 1)+ if (!dragSvgHasArt(svg)) return null+ const name = `${kind}-${createHash('sha1').update(svg).digest('hex').slice(0, 12)}-${ART_PX}.png`+ const hit = artPngCache.get(name)+ if (hit) return hit+ const dir = PATHS.dragArtDir()+ let png: Buffer+ if (existsSync(join(dir, name))) {+ if (!remember) return null+ try {+ png = readFileSync(join(dir, name))+ } catch {+ return null+ }+ } else {+ try {+ png = new Resvg(svg, { fitTo: { mode: 'width', value: ART_PX }, font: NO_FONTS }).render().asPng()+ } catch {+ return null+ }+ saveArt(dir, name, kind, png)+ }+ if (remember) artPngCache.set(name, png)+ return png+}++/** Write a kind's art to the disk cache (off the main thread) and drop its older versions. */+function saveArt(dir: string, name: string, kind: string, png: Buffer): void {+ try {+ mkdirSync(dir, { recursive: true })+ } catch {+ return+ }+ writeFile(join(dir, name), png, () => {})+ readdir(dir, (err, names) => {+ if (err) return+ for (const n of names) {+ if (n.startsWith(`${kind}-`) && n !== name) unlink(join(dir, n), () => {})+ }+ })+}++const pngUri = (png: Buffer | null): string | null => (png ? `data:image/png;base64,${png.toString('base64')}` : null)/** Generate a custom standalone SVG PNG icon representing file kinds with count badge. */function createFileStackDragIcon(paths: string[], entries?: Array<{ isDirectory?: boolean }>): Electron.NativeImage {@@ -398,15 +523,15 @@ function createFileStackDragIcon(paths: string[], entries?: Array<{ isDirectory?return cached}- const svg = buildFileDragSvg(kinds, count)+ const svg = buildDragSvgFromArt(kinds.map((kind) => pngUri(kindArtPng(kind))), count)// No art could be read: an invisible ghost must not be rasterized, and// above all must not be cached for the rest of the session.if (!dragSvgHasArt(svg)) return getFileDragIcon()try {- const resvg = new Resvg(svg, { fitTo: { mode: 'zoom', value: 2 } })+ const resvg = new Resvg(svg, { fitTo: { mode: 'zoom', value: GHOST_ZOOM }, font: count > 1 ? ghostTextFonts(BADGE_FONTS) : NO_FONTS })const pngData = resvg.render().asPng()- const img = nativeImage.createFromBuffer(pngData, { scaleFactor: 2 })+ const img = nativeImage.createFromBuffer(pngData, { scaleFactor: GHOST_ZOOM })if (!img.isEmpty()) {iconCache.set(cacheKey, img)if (iconCache.size > ICON_CACHE_MAX) {@@ -480,28 +605,36 @@ function createTextDragIcon(text: string): Electron.NativeImage {</svg>`try {- const resvg = new Resvg(svg, { fitTo: { mode: 'zoom', value: 2 } })+ const resvg = new Resvg(svg, { fitTo: { mode: 'zoom', value: GHOST_ZOOM }, font: ghostTextFonts(TEXT_FONTS) })const pngData = resvg.render().asPng()- const img = nativeImage.createFromBuffer(pngData, { scaleFactor: 2 })+ const img = nativeImage.createFromBuffer(pngData, { scaleFactor: GHOST_ZOOM })if (!img.isEmpty()) return img} catch {}return getFileDragIcon()}-/** Pre-warm common drag icons asynchronously in background so first drag is instant. */-export function prewarmDragIcons(): void {- setTimeout(() => {+const ART_KINDS: FileKind[] = [+ 'pdf', 'word', 'excel', 'powerpoint', 'archive', 'text', 'code',+ 'audio', 'video', 'image', 'executable', 'folder', 'file'+]++/**+ * Put every kind's art in the disk cache so no hover or drag ever renders it.+ * Usually nothing to do (it is already there); otherwise one kind per idle+ * moment, only while the shelf is closed, nothing kept in memory.+ */+export function prewarmDragIcons(isBusy: () => boolean): void {+ const pending = [...ART_KINDS]+ const next = (): void => {+ if (pending.length === 0) return+ if (isBusy() || uiMotionLeftMs() > 0) {+ setTimeout(next, 2000)+ return+ }try {- const commonKinds = [- 'pdf', 'word', 'excel', 'powerpoint', 'archive',- 'text', 'code', 'audio', 'video', 'image',- 'executable', 'folder', 'file'- ]- for (const k of commonKinds) {- createFileStackDragIcon([`dummy.${k}`])- }- createFileStackDragIcon(['dummy.image', 'dummy.image'])- createFileStackDragIcon(['dummy.image', 'dummy.image', 'dummy.image'])- } catch {}- }, 400)+ kindArtPng(pending.shift()!, false)+ } catch { /* the next drag of this kind renders it */ }+ setTimeout(next, 300)+ }+ setTimeout(next, 8000)}diff --git a/electron/main/fileSvg.ts b/electron/main/fileSvg.tsindex ac5bed5..f204c9a 100644--- a/electron/main/fileSvg.ts+++ b/electron/main/fileSvg.ts@@ -69,10 +69,9 @@ function kindArtDataUri(kind: FileKind): string | null {}/** One folder icon filling the 512 viewBox. */-function kindImage(kind: FileKind): string {- const uri = kindArtDataUri(kind)- if (!uri) return ''- return `<image href="${uri}" x="0" y="0" width="${ART_SIZE}" height="${ART_SIZE}"/>`+function artImage(href: string | null): string {+ if (!href) return ''+ return `<image href="${href}" x="0" y="0" width="${ART_SIZE}" height="${ART_SIZE}"/>`}/** True when a built drag SVG carries at least one piece of art. */@@ -87,27 +86,39 @@ export function dragSvgHasArt(svg: string): boolean {* to the generic icon when rasterization yields nothing usable).*/export function buildFileDragSvg(kinds: FileKind[], count: number): string {- if (count <= 1 || kinds.length === 1) {- const kind = kinds[0] || 'file'+ const shown = count <= 1 || kinds.length === 1 ? [kinds[0] || 'file'] : kinds.slice(0, 3)+ return dragSvgLayout(shown.map((kind) => kindArtDataUri(kind)), count)+}++/**+ * The same ghost, from art already rasterized (one PNG data-URI per shown+ * file, at most 3). Placing finished bitmaps is far cheaper than running each+ * piece of art's blur and grain filters again for every stack.+ */+export function buildDragSvgFromArt(art: Array<string | null>, count: number): string {+ return dragSvgLayout(count <= 1 ? art.slice(0, 1) : art.slice(0, 3), count)+}++function dragSvgLayout(art: Array<string | null>, count: number): string {+ if (art.length <= 1) {return `<svg xmlns="http://www.w3.org/2000/svg" width="122" height="122" viewBox="0 0 512 512" fill="none">- ${kindImage(kind)}+ ${artImage(art[0] ?? null)}</svg>`}// Multi-file stack (2 or 3 items offset naturally without any bounding card container)- const displayKinds = kinds.slice(0, 3)- const stackSvg = displayKinds- .map((k, i) => ({ kind: k, index: i }))+ const stackSvg = art+ .map((href, i) => ({ href, index: i })).reverse()- .map(({ kind, index }) => {+ .map(({ href, index }) => {const spread = 28- const rot = index * 5 - ((displayKinds.length - 1) * 2.5)- const tx = index * spread - ((displayKinds.length - 1) * spread) / 2+ const rot = index * 5 - ((art.length - 1) * 2.5)+ const tx = index * spread - ((art.length - 1) * spread) / 2const ty = index * 10const scale = 1 - index * 0.04return `<g transform="translate(${tx}, ${ty}) rotate(${rot} 256 256) scale(${scale})">- ${kindImage(kind)}+ ${artImage(href)}</g>`})diff --git a/electron/main/index.ts b/electron/main/index.tsindex cceeffc..72a40c0 100644--- a/electron/main/index.ts+++ b/electron/main/index.ts@@ -11,7 +11,7 @@import { app, BrowserWindow, protocol, session } from 'electron'import { APP_CONFIG, runtime } from './config'import { ensureDirs, PATHS } from '../store/paths'-import { createWindow, getMainWindow, setInteractive, setVisible, startCursorPoll, stopCursorPoll, stopHeartbeat, setHotZoneWidth, registerTaskbarCreatedListener } from './window'+import { createWindow, getMainWindow, setInteractive, setVisible, startCursorPoll, stopCursorPoll, stopHeartbeat, setHotZoneWidth, registerTaskbarCreatedListener, isInteractive } from './window'import { createTray, registerIncognitoApplier, refreshTray } from './tray'import { registerIpc, registerSendListeners } from './ipc'import { reconcileLaunchAtLoginOnStartup } from './loginItems'@@ -120,7 +120,7 @@ app.whenReady().then(() => {registerIpc()registerSendListeners()initState()- prewarmDragIcons()+ prewarmDragIcons(isInteractive)// Reflect settings immediately.let settings = loadSettings()diff --git a/electron/main/thumbnailCache.ts b/electron/main/thumbnailCache.tsindex 80d0c99..49faa14 100644--- a/electron/main/thumbnailCache.ts+++ b/electron/main/thumbnailCache.ts@@ -144,6 +144,11 @@ export function noteUiMotion(durationMs = MOTION_QUIET_MS): void {motionQuietUntil = Math.max(motionQuietUntil, Date.now() + durationMs)}+/** Milliseconds until the current panel/flyout motion is over (0 when none). */+export function uiMotionLeftMs(): number {+ return Math.max(0, motionQuietUntil - Date.now())+}+function pump(): void {if (pumping) returnpumping = truediff --git a/electron/store/paths.ts b/electron/store/paths.tsindex 6757e4a..c4a3d27 100644--- a/electron/store/paths.ts+++ b/electron/store/paths.ts@@ -33,6 +33,11 @@ export const PATHS = {stagedTempRegistryFile: () => join(root(), 'temp-staged.json'),/** Scratch dir for temp files handed to native drag-out. */tempDir: () => join(root(), 'temp'),+ /**+ * File-kind art for drag ghosts, rasterized once (see drag.ts). Not under+ * "cache": on Windows that is Chromium's own Cache folder, which it clears.+ */+ dragArtDir: () => join(root(), 'drag-art'),/** App icon (used by window + native drag image). */icon: () => join(app.getAppPath(), 'resources', 'icon.png'),/** Tray icon (pure white logo without background for dark taskbar). */diff --git a/tests/dragGhostCost.test.ts b/tests/dragGhostCost.test.tsnew file mode 100644index 0000000..5f98911--- /dev/null+++ b/tests/dragGhostCost.test.ts@@ -0,0 +1,87 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { readFileSync } from 'node:fs'+import { join } from 'node:path'++// Drag ghosts used to be rasterized on the main thread on every hover: Resvg+// loaded every installed font and re-ran each piece of file art's filters,+// freezing the shelf (every frame of its window passes through the main+// thread) for 200-550ms while scrolling or before a drag. These pin the fix.++const getItem = vi.fn((_id: string) => undefined)+const motionLeft = { ms: 0 }+vi.mock('../electron/main/state', () => ({ getStore: () => ({ get: getItem, getImagePath: () => '' }) }))+vi.mock('../electron/main/thumbnailCache', () => ({ uiMotionLeftMs: () => motionLeft.ms }))++const { prestageDrag } = await import('../electron/main/drag')+const { buildDragSvgFromArt, buildFileDragSvg, setFileIconAssetDir } = await import('../electron/main/fileSvg')++const src = (p: string) => readFileSync(join(process.cwd(), p), 'utf8')++describe('hover prestaging waits for the pointer to rest', () => {+ beforeEach(() => {+ vi.useFakeTimers()+ getItem.mockClear()+ motionLeft.ms = 0+ })+ afterEach(() => vi.useRealTimers())++ it('cards crossed on the way (or scrolled past) are never prepared, only the one the pointer settles on', () => {+ for (const id of ['a', 'b', 'c', 'd']) {+ prestageDrag({ id })+ vi.advanceTimersByTime(60)+ }+ expect(getItem).not.toHaveBeenCalled()+ vi.advanceTimersByTime(150)+ expect(getItem).toHaveBeenCalledTimes(1)+ expect(getItem).toHaveBeenCalledWith('d')+ })++ it('waits out an opening or closing panel before doing the work', () => {+ motionLeft.ms = 300+ prestageDrag({ id: 'x' })+ vi.advanceTimersByTime(150)+ expect(getItem).not.toHaveBeenCalled()+ motionLeft.ms = 0+ vi.advanceTimersByTime(300)+ expect(getItem).toHaveBeenCalledWith('x')+ })+})++describe('ghosts are composed from art rasterized once', () => {+ setFileIconAssetDir(join(process.cwd(), 'resources', 'file-icons'))++ it('places the given bitmaps in the same layout as the vector ghost', () => {+ const png = (n: number) => `data:image/png;base64,ART${n}`+ const single = buildDragSvgFromArt([png(1)], 1)+ expect(single.match(/<image /g)?.length).toBe(1)+ expect(single).toContain('width="122" height="122"')+ expect(single).not.toContain('<text')++ const stack = buildDragSvgFromArt([png(1), png(2), png(3)], 7)+ expect(stack.match(/<image /g)?.length).toBe(3)+ expect(stack).toContain('width="142" height="142"')+ expect(stack).toContain('+7')+ // same transforms as the vector stack, bitmaps swapped in+ const strip = (s: string) => s.replace(/href="[^"]*"/g, 'href=""')+ expect(strip(stack)).toBe(strip(buildFileDragSvg(['pdf', 'excel', 'image'], 7)))+ expect(strip(single)).toBe(strip(buildFileDragSvg(['pdf'], 1)))+ })++ it('never loads every installed font, and keeps each kind\'s art on disk outside Chromium\'s Cache folder', () => {+ const drag = src('electron/main/drag.ts')+ const renders = drag.match(/new Resvg\([^)]*\)/g) ?? []+ expect(renders.length).toBeGreaterThanOrEqual(3)+ for (const r of renders) expect(r).toMatch(/font: /)+ // the full font scan stays only as the fallback for a missing Segoe UI file+ expect(drag.match(/loadSystemFonts: true/g)?.length).toBe(1)+ expect(drag).toMatch(/files\.every\(\(f\) => existsSync\(f\)\)[\s\S]{0,160}: \{ loadSystemFonts: true \}/)+ expect(drag).toMatch(/const BADGE_FONTS = \['segoeuib\.ttf'\]/)+ expect(drag).toMatch(/readFileSync\(join\(dir, name\)\)/)+ expect(src('electron/store/paths.ts')).toMatch(/dragArtDir: \(\) => join\(root\(\), 'drag-art'\)/)+ })++ it('pre-renders missing art only while the shelf is closed and still', () => {+ expect(src('electron/main/drag.ts')).toMatch(/if \(isBusy\(\) \|\| uiMotionLeftMs\(\) > 0\) \{/)+ expect(src('electron/main/index.ts')).toMatch(/prewarmDragIcons\(isInteractive\)/)+ })+})diff --git a/tests/fileDragArt.test.ts b/tests/fileDragArt.test.tsindex 57b8443..0dae587 100644--- a/tests/fileDragArt.test.ts+++ b/tests/fileDragArt.test.ts@@ -37,6 +37,8 @@ describe('drag ghost art loading', () => {it('the drag code never rasterizes or caches a ghost without art', async () => {const { readFileSync } = await import('node:fs')const drag = readFileSync(join(process.cwd(), 'electron', 'main', 'drag.ts'), 'utf8')- expect(drag).toMatch(/const svg = buildFileDragSvg\(kinds, count\)\s+[\s\S]{0,200}if \(!dragSvgHasArt\(svg\)\) return getFileDragIcon\(\)\s+try \{\s+const resvg = new Resvg/)+ expect(drag).toMatch(/const svg = buildDragSvgFromArt\(kinds\.map\(\(kind\) => pngUri\(kindArtPng\(kind\)\)\), count\)\s+[\s\S]{0,200}if \(!dragSvgHasArt\(svg\)\) return getFileDragIcon\(\)\s+try \{\s+const resvg = new Resvg/)+ // a kind's art is only rasterized (and cached) when it has art+ expect(drag).toMatch(/const svg = buildFileDragSvg\(\[kind\], 1\)\s+if \(!dragSvgHasArt\(svg\)\) return null/)})})