Electron: Properly handle toggling window visibility on Windows (#3902)

This commit is contained in:
Alexander Zinchuk 2023-11-06 01:42:56 +04:00
parent 15f44b1fd2
commit 9b37c1a77a
4 changed files with 14 additions and 18 deletions

View File

@ -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();
});
}

View File

@ -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 = () => {

View File

@ -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();

View File

@ -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();