Add whatsapp terminal command and single-instance window focus
This commit is contained in:
1 parent
2b7460d976
commit
510daa9c9a
3 files changed
+36
-5
No files matched your search
@@ -18,6 +18,14 @@ const WHATSAPP_URL = 'https://web.whatsapp.com/';
|
||||
const WHATSAPP_ORIGIN = new URL(WHATSAPP_URL).origin;
|
||||
const PARTITION = 'persist:whatsapp';
|
||||
|
||||
let mainWindow = null;
|
||||
|
||||
// Launching the app again (e.g. `whatsapp` in a terminal) should focus the
|
||||
// window we already have instead of starting a second client.
|
||||
if (!app.requestSingleInstanceLock()) {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
function isWhatsAppUrl(rawUrl) {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
@@ -176,10 +184,16 @@ function createWindow() {
|
||||
|
||||
downloadToast.setWindow(win);
|
||||
|
||||
mainWindow = win;
|
||||
|
||||
// Keep WhatsApp notifying while another app has focus.
|
||||
notifications.install(win);
|
||||
|
||||
win.on('closed', () => {
|
||||
if (mainWindow === win) {
|
||||
mainWindow = null;
|
||||
}
|
||||
|
||||
downloadToast.closeAll();
|
||||
});
|
||||
|
||||
@@ -209,6 +223,20 @@ function createWindow() {
|
||||
return win;
|
||||
}
|
||||
|
||||
app.on('second-instance', () => {
|
||||
if (!mainWindow || mainWindow.isDestroyed()) {
|
||||
createWindow();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mainWindow.isMinimized()) {
|
||||
mainWindow.restore();
|
||||
}
|
||||
|
||||
mainWindow.show();
|
||||
mainWindow.focus();
|
||||
});
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const whatsappSession = session.fromPartition(PARTITION);
|
||||
|
||||
@@ -241,10 +269,12 @@ app.whenReady().then(() => {
|
||||
downloadToast.registerIpc();
|
||||
notifications.registerIpc();
|
||||
|
||||
createWindow();
|
||||
if (!mainWindow) {
|
||||
createWindow();
|
||||
}
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
if (!mainWindow) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user