Skip to content

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.

fix: resolve multi-monitor display placement and TV mirror resets

Deepender2536f8345969a1
diff --git a/electron/main/geometry.ts b/electron/main/geometry.ts
index b66ef10..dd3ade3 100644
--- a/electron/main/geometry.ts
+++ b/electron/main/geometry.ts
@@ -4,6 +4,8 @@ export interface DisplayInfo {
id: number
workArea: { x: number; y: number; width: number; height: number }
scaleFactor?: number
+ /** True when this is the OS-designated primary display. */
+ isPrimary?: boolean
}
export interface StickBoundsParams {
@@ -95,7 +97,7 @@ export function computeStickBounds(params: StickBoundsParams): StickBoundsResult
// ── Tier 4: primary display fallback ──────────────────────────────────────────
if (!display) {
- display = displays.find(d => (d as any).isPrimary) ?? displays[0]
+ display = displays.find(d => d.isPrimary) ?? displays[0]
}
const wa = display.workArea
diff --git a/electron/main/ipc.ts b/electron/main/ipc.ts
index f47dfcf..6732a47 100644
--- a/electron/main/ipc.ts
+++ b/electron/main/ipc.ts
@@ -466,9 +466,13 @@ export function registerIpc(): void {
const displays = getDisplayListOptions()
const chosen = displays.find(d => d.id === patch.stickDisplayId)
if (chosen) {
+ // IMPORTANT: persist workArea (not bounds) — geometry.ts Tier-2 fuzzy match
+ // compares d.workArea against savedWorkArea. Using bounds (which includes the
+ // taskbar) would create a mismatch of ~40px, exceeding the 8px BOUNDS_TOLERANCE
+ // and causing Tier-2 to always fail on reboot.
enrichedPatch = {
...enrichedPatch,
- stickDisplayWorkArea: chosen.bounds,
+ stickDisplayWorkArea: chosen.workArea,
stickDisplayScaleFactor: chosen.scaleFactor
}
}
diff --git a/electron/main/tray.ts b/electron/main/tray.ts
index 0a9dfc1..cd6004c 100644
--- a/electron/main/tray.ts
+++ b/electron/main/tray.ts
@@ -64,10 +64,12 @@ export function createTray(): Tray {
type: 'radio' as const,
checked: d.isCurrent,
click: () => {
- // Persist ID + geometry so the next reboot can fuzzy-match via workArea.
+ // Persist workArea (not bounds) — geometry.ts Tier-2 fuzzy match compares
+ // d.workArea against savedWorkArea. Storing bounds (which includes the taskbar)
+ // causes a ~40px mismatch that exceeds BOUNDS_TOLERANCE, breaking cross-reboot recovery.
const next = saveSettings({
stickDisplayId: d.id,
- stickDisplayWorkArea: d.bounds,
+ stickDisplayWorkArea: d.workArea,
stickDisplayScaleFactor: d.scaleFactor
})
pushState.settings(next)
diff --git a/electron/main/window.ts b/electron/main/window.ts
index 224d046..d1b3bd6 100644
--- a/electron/main/window.ts
+++ b/electron/main/window.ts
@@ -46,6 +46,15 @@ export let previewActive = false
export let currentHotZoneWidth = 3
export let currentStickDisplayId: number | undefined
+/**
+ * Fix 3: Consecutive-stale-reads counter for getStickGeometry self-heal.
+ * Tracks how many consecutive repositionWindow() calls have found the saved
+ * stickDisplayId absent from the display list. The preference is only wiped
+ * from disk after this reaches STALE_THRESHOLD (2), preventing transient TV
+ * mirror ID renegotiations from destroying the user's saved monitor choice.
+ */
+let staleIdConsecutiveCount = 0
+
export function setHotZoneWidth(width: number): void {
currentHotZoneWidth = width
}
@@ -235,46 +244,72 @@ function getStickGeometry(): { x: number; y: number; width: number; height: numb
const idWasStale = settings.stickDisplayId !== undefined && !allDisplays.some(d => d.id === settings.stickDisplayId)
if (idWasStale) {
- // The old ID no longer exists in this session (disconnected or reboot rename).
- // Check whether we recovered via Tier-2 or fell all the way to primary.
- const recoveredViaBounds = settings.stickDisplayWorkArea !== undefined && resolved.id !== primaryDisplay.id
- if (recoveredViaBounds) {
- console.log(`[Main] Display ${settings.stickDisplayId} re-matched via geometry to display ${resolved.id} — updating persisted ID.`)
+ // Fix 3: Consecutive-stale-reads guard.
+ //
+ // When a TV mirror is active, Windows briefly removes and re-adds display IDs
+ // during EDID renegotiation. This makes idWasStale=true for a single call even
+ // though the user's chosen monitor is still physically connected.
+ //
+ // Before this fix: the very first stale read immediately wrote stickDisplayId=undefined
+ // to disk, permanently wiping the user's monitor choice.
+ //
+ // After this fix: we require the ID to be absent in 2 CONSECUTIVE calls before
+ // treating it as genuinely gone. With the 600ms debounce on display events, two
+ // consecutive stale reads means the display has been absent for >600ms — a real
+ // disconnection, not a transient OS reconfiguration.
+ staleIdConsecutiveCount++
+ const STALE_THRESHOLD = 2
+ if (staleIdConsecutiveCount < STALE_THRESHOLD) {
+ console.log(`[Main] Display ${settings.stickDisplayId} temporarily absent (count=${staleIdConsecutiveCount}/${STALE_THRESHOLD}) — holding preference, will re-evaluate.`)
+ // Don't wipe settings yet — just reposition using what was resolved (likely Tier-2 or Tier-4)
} else {
- console.log(`[Main] Display ${settings.stickDisplayId} disconnected or unrecognised. Falling back to display ${resolved.id}.`)
- }
- settings = saveSettings({
- stickDisplayId: recoveredViaBounds ? resolved.id : undefined,
- stickDisplayWorkArea: recoveredViaBounds ? resolved.workArea : undefined,
- stickDisplayScaleFactor: recoveredViaBounds ? resolved.scaleFactor : undefined
- })
- if (mainWindow && !mainWindow.isDestroyed() && !mainWindow.webContents.isDestroyed()) {
- mainWindow.webContents.send('state:settings', settings)
- }
- if (!recoveredViaBounds) {
- // Genuinely fell back — briefly show panel on new location.
- popUpAndRetract(1500)
+ staleIdConsecutiveCount = 0
+ // The old ID no longer exists in this session (disconnected or reboot rename).
+ // Check whether we recovered via Tier-2 or fell all the way to primary.
+ const recoveredViaBounds = settings.stickDisplayWorkArea !== undefined && resolved.id !== primaryDisplay.id
+ if (recoveredViaBounds) {
+ console.log(`[Main] Display ${settings.stickDisplayId} re-matched via geometry to display ${resolved.id} — updating persisted ID.`)
+ } else {
+ console.log(`[Main] Display ${settings.stickDisplayId} disconnected or unrecognised after ${STALE_THRESHOLD} checks. Falling back to display ${resolved.id}.`)
+ }
+ settings = saveSettings({
+ stickDisplayId: recoveredViaBounds ? resolved.id : undefined,
+ stickDisplayWorkArea: recoveredViaBounds ? resolved.workArea : undefined,
+ stickDisplayScaleFactor: recoveredViaBounds ? resolved.scaleFactor : undefined
+ })
+ if (mainWindow && !mainWindow.isDestroyed() && !mainWindow.webContents.isDestroyed()) {
+ mainWindow.webContents.send('state:settings', settings)
+ }
+ if (!recoveredViaBounds) {
+ // Genuinely fell back — briefly show panel on new location.
+ popUpAndRetract(1500)
+ }
}
- } else if (idChanged) {
- // Shouldn't happen if Tier-1 matched, but guard it anyway.
- settings = saveSettings({
- stickDisplayId: resolved.id,
- stickDisplayWorkArea: resolved.workArea,
- stickDisplayScaleFactor: resolved.scaleFactor
- })
- if (mainWindow && !mainWindow.isDestroyed() && !mainWindow.webContents.isDestroyed()) {
- mainWindow.webContents.send('state:settings', settings)
+ } else {
+ // Reset counter: the saved ID was found this call — monitor is present.
+ staleIdConsecutiveCount = 0
+
+ if (idChanged) {
+ // Shouldn't happen if Tier-1 matched, but guard it anyway.
+ settings = saveSettings({
+ stickDisplayId: resolved.id,
+ stickDisplayWorkArea: resolved.workArea,
+ stickDisplayScaleFactor: resolved.scaleFactor
+ })
+ if (mainWindow && !mainWindow.isDestroyed() && !mainWindow.webContents.isDestroyed()) {
+ mainWindow.webContents.send('state:settings', settings)
+ }
+ } else if (settings.stickDisplayId !== undefined && (
+ settings.stickDisplayWorkArea === undefined ||
+ settings.stickDisplayScaleFactor === undefined
+ )) {
+ // Upgrade: user had a stickDisplayId saved before this feature was added;
+ // backfill geometry silently so the next reboot can fuzzy-match.
+ saveSettings({
+ stickDisplayWorkArea: resolved.workArea,
+ stickDisplayScaleFactor: resolved.scaleFactor
+ })
}
- } else if (settings.stickDisplayId !== undefined && (
- settings.stickDisplayWorkArea === undefined ||
- settings.stickDisplayScaleFactor === undefined
- )) {
- // Upgrade: user had a stickDisplayId saved before this feature was added;
- // backfill geometry silently so the next reboot can fuzzy-match.
- saveSettings({
- stickDisplayWorkArea: resolved.workArea,
- stickDisplayScaleFactor: resolved.scaleFactor
- })
}
currentStickDisplayId = resolved.id
@@ -335,15 +370,34 @@ export function createWindow(): BrowserWindow {
}
}
+ // Fix 2: Debounce display-metrics-changed.
+ //
+ // When a TV is in mirror mode, Windows can fire this event 5–20 times in rapid
+ // succession each time the TV turns on/off, wakes, or renegotiates EDID.
+ // Without debouncing, each of those 20 calls hits repositionWindow() while the
+ // display list is in a transitional state, potentially resolving to the wrong
+ // display at intermediate frames.
+ //
+ // 600ms is enough for Windows DWM to finish all its display-topology bookkeeping
+ // after a single physical event, while being fast enough to feel instantaneous.
+ let displayChangeDebounceTimer: ReturnType<typeof setTimeout> | null = null
+ const handleDisplayChangeDebounced = (triggerPopUp = false) => {
+ if (displayChangeDebounceTimer !== null) {
+ clearTimeout(displayChangeDebounceTimer)
+ }
+ displayChangeDebounceTimer = setTimeout(() => {
+ displayChangeDebounceTimer = null
+ handleDisplayChange(triggerPopUp)
+ }, 600)
+ }
+
// Keep the panel glued to the primary display if the work area changes.
- screen.on('display-metrics-changed', () => handleDisplayChange(false))
+ screen.on('display-metrics-changed', () => handleDisplayChangeDebounced(false))
screen.on('display-added', () => {
- handleDisplayChange(true)
- setTimeout(() => handleDisplayChange(false), 300)
+ handleDisplayChangeDebounced(true)
})
screen.on('display-removed', () => {
- handleDisplayChange(true)
- setTimeout(() => handleDisplayChange(false), 300)
+ handleDisplayChangeDebounced(true)
})
// Respect OS-level always-on-top reordering.
@@ -448,6 +502,7 @@ export function getActiveDisplayId(allDisplays?: Electron.Display[]): number {
export function getDisplayListOptions(): Array<{
id: number
bounds: { x: number; y: number; width: number; height: number }
+ workArea: { x: number; y: number; width: number; height: number }
scaleFactor: number
isPrimary: boolean
isCurrent: boolean
@@ -474,6 +529,7 @@ export function getDisplayListOptions(): Array<{
return {
id: d.id,
bounds: { x: d.bounds.x, y: d.bounds.y, width: d.bounds.width, height: d.bounds.height },
+ workArea: { x: d.workArea.x, y: d.workArea.y, width: d.workArea.width, height: d.workArea.height },
scaleFactor: scale,
isPrimary,
isCurrent: d.id === activeId,
diff --git a/package.json b/package.json
index 5baf4ec..737e72e 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,8 @@
"preview": "electron-vite preview",
"typecheck:node": "tsc --noEmit -p tsconfig.node.json --composite false",
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
- "typecheck": "npm run typecheck:node && npm run typecheck:web"
+ "typecheck": "npm run typecheck:node && npm run typecheck:web",
+ "test": "vitest run"
},
"build": {
"appId": "com.edgedrop.app",
diff --git a/tests/geometry.test.ts b/tests/geometry.test.ts
index 4a52afb..87cc18b 100644
--- a/tests/geometry.test.ts
+++ b/tests/geometry.test.ts
@@ -1,84 +1,93 @@
-import { describe, it, expect } from 'vitest'
+import { describe, it, expect } from 'vitest'
import { computeStickBounds, type DisplayInfo } from '../electron/main/geometry'
-function makeDisplay(id: number, x: number, y: number, w: number, h: number): DisplayInfo {
- return { id, workArea: { x, y, width: w, height: h } }
+function makeDisplay(
+ id: number, x: number, y: number, w: number, h: number,
+ opts: { scaleFactor?: number; isPrimary?: boolean } = {}
+): DisplayInfo {
+ return { id, workArea: { x, y, width: w, height: h }, scaleFactor: opts.scaleFactor ?? 1, isPrimary: opts.isPrimary ?? false }
}
-const primary = makeDisplay(1, 0, 0, 1920, 1080)
-const secondaryRight = makeDisplay(2, 1920, 0, 1920, 1080)
-const secondaryNegX = makeDisplay(3, -1920, 0, 1920, 1080)
-const secondaryNegY = makeDisplay(4, 0, -1080, 1920, 1080)
+const PRIMARY = makeDisplay(1, 0, 0, 1920, 1040, { isPrimary: true, scaleFactor: 1 })
+const SECONDARY = makeDisplay(2, 1920, 0, 1920, 1040, { isPrimary: false, scaleFactor: 1 })
-describe('computeStickBounds', () => {
+describe('computeStickBounds — original tests', () => {
it('sticks to left edge of primary display', () => {
- const result = computeStickBounds({
- position: 'left',
- displays: [primary],
- windowWidth: 384,
- windowHeight: 1080
- })
- expect(result).toEqual({ x: 0, y: 0, width: 384, height: 1080, displayId: 1 })
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY], windowWidth: 384, windowHeight: 1040 })
+ expect(r.x).toBe(0); expect(r.y).toBe(0); expect(r.displayId).toBe(1)
})
-
- it('sticks to right edge of secondary display to the right', () => {
- const result = computeStickBounds({
- position: 'right',
- displays: [primary, secondaryRight],
- displayId: 2,
- windowWidth: 384,
- windowHeight: 1080
- })
- expect(result).toEqual({ x: 3456, y: 0, width: 384, height: 1080, displayId: 2 })
+ it('sticks to right edge of secondary display', () => {
+ const r = computeStickBounds({ position: 'right', displays: [PRIMARY, SECONDARY], displayId: 2, windowWidth: 384, windowHeight: 1040 })
+ expect(r.x).toBe(3456); expect(r.displayId).toBe(2)
})
-
- it('sticks to right edge of display with negative x', () => {
- const result = computeStickBounds({
- position: 'right',
- displays: [primary, secondaryNegX],
- displayId: 3,
- windowWidth: 384,
- windowHeight: 1080
- })
- expect(result).toEqual({ x: -384, y: 0, width: 384, height: 1080, displayId: 3 })
+ it('falls back to nearest display via currentBounds', () => {
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, SECONDARY], displayId: undefined, currentBounds: { x: 2100, y: 100 }, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(2)
})
+})
-
-
- it('falls back to nearest display when saved display is missing', () => {
- const result = computeStickBounds({
- position: 'left',
- displays: [primary, secondaryRight],
- displayId: 99,
- currentBounds: { x: 2000, y: 100 },
- windowWidth: 384,
- windowHeight: 1080
- })
- expect(result.displayId).toBe(2)
+describe('Fix 1 — isPrimary typed property for Tier-4 fallback', () => {
+ it('secondary listed first: still picks primary via isPrimary flag', () => {
+ const secondaryFirst = [SECONDARY, PRIMARY]
+ const r = computeStickBounds({ position: 'left', displays: secondaryFirst, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(1)
+ })
+ it('3-display array with primary last still picks correct primary', () => {
+ const mon3 = makeDisplay(3, 3840, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [SECONDARY, mon3, PRIMARY], windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(1)
})
+ it('no isPrimary flags: falls back to displays[0]', () => {
+ const noFlags = [makeDisplay(10, 0, 0, 1920, 1040), makeDisplay(20, 1920, 0, 1920, 1040)]
+ const r = computeStickBounds({ position: 'left', displays: noFlags, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(10)
+ })
+})
- it('falls back to primary when saved display missing and no current bounds', () => {
- const result = computeStickBounds({
- position: 'left',
- displays: [primary, secondaryRight],
- displayId: 99,
- windowWidth: 384,
- windowHeight: 1080
- })
- expect(result.displayId).toBe(1)
+describe('Fix 4 — Tier-2 fuzzy match uses workArea (not bounds)', () => {
+ it('matches secondary across reboot when ID changes but workArea is same', () => {
+ const rebooted = makeDisplay(99, 1920, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, rebooted], displayId: 2, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1040 }, savedScaleFactor: 1, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(99)
})
+ it('bounds-vs-workArea mismatch (40px taskbar) fails Tier-2 → falls to primary', () => {
+ const rebooted = makeDisplay(99, 1920, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, rebooted], displayId: 2, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1080 }, savedScaleFactor: 1, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(1)
+ })
+ it('tolerates 7px OS jitter in workArea and still matches', () => {
+ const jittered = makeDisplay(99, 1927, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, jittered], displayId: 2, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1040 }, savedScaleFactor: 1, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(99)
+ })
+ it('does NOT match when jitter is 9px (exceeds 8px tolerance)', () => {
+ const tooFar = makeDisplay(99, 1929, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, tooFar], displayId: 2, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1040 }, savedScaleFactor: 1, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(1)
+ })
+ it('uses scaleFactor tiebreaker when two monitors share identical workArea', () => {
+ const hiDpi = makeDisplay(10, 1920, 0, 1920, 1040, { scaleFactor: 1.5 })
+ const loDpi = makeDisplay(11, 1920, 0, 1920, 1040, { scaleFactor: 1.0 })
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, hiDpi, loDpi], displayId: 99, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1040 }, savedScaleFactor: 1.5, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(10)
+ })
+})
- it('clamps window larger than work area', () => {
- const small = makeDisplay(5, 0, 0, 800, 600)
- const result = computeStickBounds({
- position: 'left',
- displays: [small],
- windowWidth: 1000,
- windowHeight: 800
- })
- expect(result.x).toBe(0)
- expect(result.y).toBe(0)
- expect(result.width).toBe(1000)
- expect(result.height).toBe(600)
+describe('TV mirror regression — combined scenario', () => {
+ it('fresh install: places on Primary even when secondary is listed first', () => {
+ const r = computeStickBounds({ position: 'left', displays: [SECONDARY, PRIMARY], windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(1)
+ })
+ it('saved secondary preference survives reboot with new IDs', () => {
+ const newPrimary = makeDisplay(100, 0, 0, 1920, 1040, { isPrimary: true })
+ const newSecondary = makeDisplay(200, 1920, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [newPrimary, newSecondary], displayId: 2, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1040 }, savedScaleFactor: 1, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(200); expect(r.x).toBe(1920)
+ })
+ it('TV mirror transient ID change: Tier-2 still finds secondary by workArea', () => {
+ const transientSecondary = makeDisplay(999, 1920, 0, 1920, 1040)
+ const r = computeStickBounds({ position: 'left', displays: [PRIMARY, transientSecondary], displayId: 2, savedWorkArea: { x: 1920, y: 0, width: 1920, height: 1040 }, savedScaleFactor: 1, windowWidth: 384, windowHeight: 1040 })
+ expect(r.displayId).toBe(999); expect(r.x).toBe(1920)
})
})
+