Skip to content

chore: switch from polling to event for screen change detection #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
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
67 changes: 41 additions & 26 deletions apps/electron/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
} from 'electron';
import path from 'node:path';
import fsPromises from 'node:fs/promises'; // For reading the audio file (async)
import fs from 'node:fs'; // For synchronous file operations like accessSync
import { exec, spawn, ChildProcessWithoutNullStreams, execSync } from 'node:child_process'; // For executing system commands
import dotenv from 'dotenv';
import started from 'electron-squirrel-startup';
import Store from 'electron-store';
Expand Down Expand Up @@ -40,8 +38,8 @@ let audioCapture: AudioCapture | null = null;
let aiService: AiService | null = null;
let swiftIOBridgeClientInstance: SwiftIOBridge | null = null;
let openAiApiKey: string | null = null;
let currentWindowDisplayId: number | null = null; // ADDED for tracking display
let screenPollInterval: NodeJS.Timeout | null = null; // ADDED for polling screen changes
let currentWindowDisplayId: number | null = null; // For tracking current display
let activeSpaceChangeSubscriptionId: number | null = null; // For display change notifications

interface StoreSchema {
'openai-api-key': string;
Expand Down Expand Up @@ -319,40 +317,57 @@ app.on('ready', async () => {

setupApplicationMenu(createOrShowSettingsWindow);

// Start polling for screen changes to move the floatingButtonWindow
if (screenPollInterval) clearInterval(screenPollInterval);
console.log('Main: Starting screen polling interval...');
screenPollInterval = setInterval(() => {
//console.log('Main: Polling for screen changes...');
if (floatingButtonWindow && !floatingButtonWindow.isDestroyed()) {
try {
const cursorPoint = screen.getCursorScreenPoint();
//console.log('Main: Cursor point:', cursorPoint);
const displayForCursor = screen.getDisplayNearestPoint(cursorPoint);
//console.log('Main: Display for cursor:', displayForCursor);
if (currentWindowDisplayId !== displayForCursor.id) {
console.log(`[Main Process] Cursor moved to display ID: ${displayForCursor.id}. Updating floatingButtonWindow.`);
floatingButtonWindow.setBounds(displayForCursor.workArea);
currentWindowDisplayId = displayForCursor.id;
if (process.platform === 'darwin') {
try {
console.log("Main: Setting up display change notifications");

activeSpaceChangeSubscriptionId = systemPreferences.subscribeWorkspaceNotification(
'NSWorkspaceActiveDisplayDidChangeNotification',
() => {
if (floatingButtonWindow && !floatingButtonWindow.isDestroyed()) {
try {
const cursorPoint = screen.getCursorScreenPoint();
const displayForCursor = screen.getDisplayNearestPoint(cursorPoint);
if (currentWindowDisplayId !== displayForCursor.id) {
console.log(
`[Main Process] Moving floating window to display ID: ${displayForCursor.id}`
);
floatingButtonWindow.setBounds(displayForCursor.workArea);
currentWindowDisplayId = displayForCursor.id;
}
} catch (error) {
console.warn('[Main Process] Error handling display change:', error);
}
}
}
} catch (error) {
console.warn('[Main Process] Error in screen polling interval (safe to ignore if sporadic):', error);
);

if (activeSpaceChangeSubscriptionId !== undefined && activeSpaceChangeSubscriptionId >= 0) {
console.log(`Main: Successfully subscribed to display change notifications`);
} else {
console.error('Main: Failed to subscribe to display change notifications');
}
} catch (e) {
console.error('Main: Error during subscription to display notifications:', e);
activeSpaceChangeSubscriptionId = null;
}
}, 500); // Check every 500ms
} else {
console.log('Main: Display change tracking is a macOS-only feature');
}
});

// Unregister all shortcuts when the app is about to quit
// Clean up intervals and subscriptions
app.on('will-quit', () => {
// globalShortcut.unregisterAll();
globalShortcut.unregisterAll();
if (swiftIOBridgeClientInstance) {
console.log('Main: Stopping Swift helper...');
swiftIOBridgeClientInstance.stopHelper();
}
if (screenPollInterval) { // Clear the interval
clearInterval(screenPollInterval);
screenPollInterval = null;
if (process.platform === 'darwin' && activeSpaceChangeSubscriptionId !== null) {
systemPreferences.unsubscribeWorkspaceNotification(activeSpaceChangeSubscriptionId);
console.log('Main: Unsubscribed from display change notifications');
activeSpaceChangeSubscriptionId = null;
}
});

Expand Down