Skip to content

Commit

Permalink
Fix window closing issue in macos by
Browse files Browse the repository at this point in the history
1. Use a helper function to determine if the window is open by seeing if there's a browser window with shortcut.html loaded
2. Use the  event listener on the window to handle teardown
  • Loading branch information
sabaimran committed Jun 19, 2024
1 parent 82bcc73 commit 81ce0f6
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/interface/desktop/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ const createWindow = (tab = 'chat.html') => {
}
}

const createShortcutWindow = (tab = 'shortcut.html') => {
const createShortcutWindow = (tab = 'shortcut.html') => {
var shortcutWin = new BrowserWindow({
width: 400,
height: 600,
Expand Down Expand Up @@ -539,8 +539,19 @@ const createWindow = (tab = 'chat.html') => {
});

return shortcutWin;
};
app.whenReady().then(() => {
};

function isShortcutWindowOpen() {
const windows = BrowserWindow.getAllWindows();
for (let i = 0; i < windows.length; i++) {
if (windows[i].webContents.getURL().endsWith('shortcut.html')) {
return true;
}
}
return false;
}

app.whenReady().then(() => {
addCSPHeaderToSession();

ipcMain.on('set-title', handleSetTitle);
Expand Down Expand Up @@ -608,9 +619,10 @@ const createWindow = (tab = 'chat.html') => {
console.warn("Desktop app update check failed:", e);
}
})
openShortcut = false;
globalShortcut.register(openShortcutWindowKeyBind, () => {
if(openShortcut) return;
console.log("Shortcut key pressed")
if(isShortcutWindowOpen()) return;

const shortcutWin = createShortcutWindow(); // Create a new shortcut window each time the shortcut is triggered
shortcutWin.setAlwaysOnTop(true, 'screen-saver', 1);
const clipboardText = clipboard.readText();
Expand All @@ -619,24 +631,28 @@ const createWindow = (tab = 'chat.html') => {
shortcutWin.webContents.send('clip', clipboardText);
console.log('Message sent to window'); // Debug log
});
openShortcut = true;

// Register a global shortcut for the Escape key for the shortcutWin
globalShortcut.register('Escape', () => {
if (shortcutWin) {
shortcutWin.close();
}
// Unregister the Escape key shortcut
globalShortcut.unregister('Escape');
openShortcut = false;
});

shortcutWin.on('closed', () => {
// Unregister the Escape key shortcut
globalShortcut.unregister('Escape');
});

ipcMain.on('button-clicked', () => {
openWindow('chat.html');
if (shortcutWin && !shortcutWin.isDestroyed()) {
shortcutWin.close();
}
// Unregister the Escape key shortcut
globalShortcut.unregister('Escape');
openShortcut = false;
});
});
app.on('activate', () => {
Expand Down

0 comments on commit 81ce0f6

Please sign in to comment.