Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions tauri-app/src/OverlayApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
setOverlaySize,
} from "@/lib/tauri";
import type { MonitorInfo } from "@/lib/types";
import { watchOverlayDisplays } from "@/lib/overlay-displays";

const EDGE_PADDING = 4;

Expand Down Expand Up @@ -75,6 +76,7 @@ export default function OverlayApp() {
} | null
>(null);
const [monitors, setMonitors] = useState<MonitorInfo[]>([]);
const [settingsReady, setSettingsReady] = useState(false);
const [isDragging, setIsDragging] = useState(false);
// Pixel Shift: current nudge offset (physical px) and a handle to re-run the
// positioning pass. The offset is folded into apply() only — never persisted —
Expand All @@ -85,8 +87,10 @@ export default function OverlayApp() {
useEffect(() => {
document.documentElement.style.background = "transparent";
document.body.style.background = "transparent";
loadSettings();
getMonitors().then((m) => m && setMonitors(m));
let mounted = true;
void loadSettings().then(() => { if (mounted) setSettingsReady(true); });
const stop = watchOverlayDisplays(getMonitors, setMonitors);
return () => { mounted = false; stop(); };
}, [loadSettings]);

useEffect(() => {
Expand Down Expand Up @@ -116,7 +120,7 @@ export default function OverlayApp() {
// HUD pixels — never freezes the rest of the desktop.
useEffect(() => {
const el = hudRef.current;
if (!el || monitors.length === 0) return;
if (!el || !settingsReady || monitors.length === 0) return;
const monitor = monitors[settings.selectedDisplayIndex] ?? monitors[0];

const apply = () => {
Expand Down Expand Up @@ -171,7 +175,23 @@ export default function OverlayApp() {
apply();
const ro = new ResizeObserver(apply);
ro.observe(el);
return () => ro.disconnect();
// A DPI change can leave CSS dimensions unchanged, so ResizeObserver
// alone does not necessarily recalculate the physical window dimensions.
let dpiQuery: MediaQueryList;
const onDpiChange = () => { watchDpi(); apply(); };
const watchDpi = () => {
dpiQuery?.removeEventListener("change", onDpiChange);
dpiQuery = window.matchMedia(`(resolution: ${window.devicePixelRatio || 1}dppx)`);
dpiQuery.addEventListener("change", onDpiChange);
};
watchDpi();
window.addEventListener("resize", apply);
return () => {
ro.disconnect();
dpiQuery.removeEventListener("change", onDpiChange);
window.removeEventListener("resize", apply);
applyPositionRef.current = () => {};
};
// Only re-fire the IPC moves/sizes when a position-relevant field
// changes — not on every settings update (font sizes, sensor toggles,
// etc. would otherwise round-trip a no-op setOverlayPosition).
Expand All @@ -182,6 +202,7 @@ export default function OverlayApp() {
settings.positionY,
settings.positionIndex,
monitors,
settingsReady,
]);

// Pixel Shift — nudge the HUD 1px every interval to spread OLED wear, within
Expand Down
45 changes: 45 additions & 0 deletions tauri-app/src/lib/overlay-displays.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { watchOverlayDisplays } from "./overlay-displays";
import type { MonitorInfo } from "./types";

const display: MonitorInfo = { name: "Display", x: 0, y: 0, width: 1920, height: 1080, primary: true };
afterEach(() => vi.useRealTimers());

describe("overlay display refresh", () => {
it("recovers from empty boot enumeration and reapplies changed geometry only", async () => {
vi.useFakeTimers();
const read = vi.fn()
.mockResolvedValueOnce([])
.mockResolvedValueOnce([display])
.mockResolvedValueOnce([{ ...display }])
.mockResolvedValue([{ ...display, width: 2560, x: -2560 }]);
const changed = vi.fn();
const stop = watchOverlayDisplays(read, changed);
await vi.advanceTimersByTimeAsync(0);
expect(changed).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(4000);
expect(changed).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(2000);
expect(changed).toHaveBeenLastCalledWith([{ ...display, width: 2560, x: -2560 }]);
stop();
await vi.advanceTimersByTimeAsync(4000);
expect(read).toHaveBeenCalledTimes(4);
});

it("retains geometry on failures and ignores a response after unmount", async () => {
vi.useFakeTimers();
let resolve!: (value: MonitorInfo[]) => void;
const read = vi.fn().mockResolvedValueOnce([display])
.mockRejectedValueOnce(new Error("display unavailable"))
.mockImplementation(() => new Promise<MonitorInfo[]>(r => { resolve = r; }));
const changed = vi.fn();
const stop = watchOverlayDisplays(read, changed);
await vi.advanceTimersByTimeAsync(8000);
expect(read).toHaveBeenCalledTimes(3); // no overlapping requests
expect(changed).toHaveBeenCalledTimes(1);
stop();
resolve([{ ...display, width: 2560 }]);
await vi.advanceTimersByTimeAsync(0);
expect(changed).toHaveBeenCalledTimes(1);
});
});
33 changes: 33 additions & 0 deletions tauri-app/src/lib/overlay-displays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { MonitorInfo } from "./types";

// Display enumeration during Windows sign-in can precede the final topology.
// Keep refreshing, including after resume, without resetting saved coordinates.
export function watchOverlayDisplays(
read: () => Promise<MonitorInfo[] | undefined>,
changed: (monitors: MonitorInfo[]) => void,
intervalMs = 2000,
): () => void {
let stopped = false;
let pending = false;
let previous = "";
const refresh = async () => {
if (stopped || pending) return;
pending = true;
try {
const monitors = await read();
if (stopped || !monitors?.length) return;
const signature = JSON.stringify(monitors);
if (signature !== previous) {
previous = signature;
changed(monitors);
}
} catch {
// Transient enumeration failures retain the last usable geometry.
} finally {
pending = false;
}
};
void refresh();
const timer = setInterval(() => void refresh(), intervalMs);
return () => { stopped = true; clearInterval(timer); };
}
Loading