From 9b37c1a77a6765133715633e15bd0451dd429e3f Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 6 Nov 2023 01:42:56 +0400 Subject: [PATCH] Electron: Properly handle toggling window visibility on Windows (#3902) --- src/electron/deeplink.ts | 14 +++----------- src/electron/tray.ts | 8 ++------ src/electron/utils.ts | 8 ++++++++ src/electron/window.ts | 2 +- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/electron/deeplink.ts b/src/electron/deeplink.ts index 47fdbfb37..0cf4f31a7 100644 --- a/src/electron/deeplink.ts +++ b/src/electron/deeplink.ts @@ -4,7 +4,7 @@ import path from 'path'; import { ElectronEvent } from '../types/electron'; import { - getLastWindow, IS_LINUX, IS_MAC_OS, IS_WINDOWS, + focusLastWindow, getLastWindow, IS_LINUX, IS_MAC_OS, IS_WINDOWS, } from './utils'; const TG_PROTOCOL = 'tg'; @@ -12,8 +12,6 @@ const TG_PROTOCOL = 'tg'; let deeplinkUrl: string | undefined; export function initDeeplink() { - const window = getLastWindow(); - if (process.defaultApp) { if (process.argv.length >= 2) { app.setAsDefaultProtocolClient(TG_PROTOCOL, process.execPath, [path.resolve(process.argv[1])]); @@ -35,6 +33,7 @@ export function initDeeplink() { event.preventDefault(); deeplinkUrl = url; processDeeplink(); + focusLastWindow(); }); }); @@ -50,14 +49,7 @@ export function initDeeplink() { } processDeeplink(); - - if (window) { - if (window.isMinimized()) { - window.restore(); - } - - window.focus(); - } + focusLastWindow(); }); } diff --git a/src/electron/tray.ts b/src/electron/tray.ts index 37365288b..4834e2857 100644 --- a/src/electron/tray.ts +++ b/src/electron/tray.ts @@ -4,7 +4,7 @@ import { import path from 'path'; import { - forceQuit, getAppTitle, getLastWindow, store, + focusLastWindow, forceQuit, getAppTitle, store, } from './utils'; const TRAY_ICON_SETTINGS_KEY = 'trayIcon'; @@ -52,11 +52,7 @@ const tray: TrayHelper = { this.instance = new Tray(icon); const handleOpenFromTray = () => { - if (BrowserWindow.getAllWindows().every((window) => !window.isVisible())) { - BrowserWindow.getAllWindows().forEach((window) => window.show()); - } else { - getLastWindow()?.focus(); - } + focusLastWindow(); }; const handleCloseFromTray = () => { diff --git a/src/electron/utils.ts b/src/electron/utils.ts index 4aa218b42..e754b4dea 100644 --- a/src/electron/utils.ts +++ b/src/electron/utils.ts @@ -38,6 +38,14 @@ export function reloadWindows(isAutoUpdateEnabled = true): void { }); } +export function focusLastWindow(): void { + if (BrowserWindow.getAllWindows().every((window) => !window.isVisible())) { + BrowserWindow.getAllWindows().forEach((window) => window.show()); + } else { + getLastWindow()?.focus(); + } +} + export function getAppTitle(chatTitle?: string): string { const appName = app.getName(); diff --git a/src/electron/window.ts b/src/electron/window.ts index bbf264c8d..2bec304c6 100644 --- a/src/electron/window.ts +++ b/src/electron/window.ts @@ -93,7 +93,7 @@ export function createWindow(url?: string) { }); window.on('close', (event) => { - if (IS_MAC_OS || IS_WINDOWS) { + if (IS_MAC_OS || (IS_WINDOWS && tray.isEnabled)) { if (forceQuit.isEnabled) { app.exit(0); forceQuit.disable();