From f99c10a527c023699050e1a24a4d016cdd1621a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20V=C3=A9lez?= Date: Tue, 29 Oct 2024 14:55:01 +0100 Subject: [PATCH] MM-60233 - fix issue in kde of focus after minimize (#3157) * MM-60233 - fix issue in kde of focus after minimize * fix and add unit tests * enhance condition logic * place the iskde in utils to be reusable, adjust tests * move logic to prevent potential problems with upcoming version * adjust unit test to the new logic --- src/main/utils.ts | 6 ++++++ src/main/windows/mainWindow.test.js | 33 +++++++++++++++++++++++++++++ src/main/windows/mainWindow.ts | 8 ++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/utils.ts b/src/main/utils.ts index 2696bf6d..cf4ca052 100644 --- a/src/main/utils.ts +++ b/src/main/utils.ts @@ -130,3 +130,9 @@ export function openScreensharePermissionsSettingsMacOS() { return exec('open "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture"', {timeout: 1000}); } + +export function isKDE() { + return (process.env.XDG_CURRENT_DESKTOP ?? '').toUpperCase() === 'KDE' || + (process.env.DESKTOP_SESSION ?? '').toLowerCase() === 'plasma' || + (process.env.KDE_FULL_SESSION ?? '').toLowerCase() === 'true'; +} diff --git a/src/main/windows/mainWindow.test.js b/src/main/windows/mainWindow.test.js index a83d4e98..6c6673d0 100644 --- a/src/main/windows/mainWindow.test.js +++ b/src/main/windows/mainWindow.test.js @@ -68,6 +68,7 @@ jest.mock('../contextMenu', () => jest.fn()); jest.mock('../utils', () => ({ isInsideRectangle: jest.fn(), getLocalPreload: jest.fn(), + isKDE: jest.fn(), })); jest.mock('main/i18nManager', () => ({ @@ -98,6 +99,7 @@ describe('main/windows/mainWindow', () => { isMaximized: jest.fn(), isFullScreen: jest.fn(), getBounds: jest.fn(), + isMinimized: jest.fn().mockReturnValue(false), }; beforeEach(() => { @@ -515,6 +517,9 @@ describe('main/windows/mainWindow', () => { }); it('should add override shortcuts for the top menu on Linux to stop it showing up', () => { + const {isKDE} = require('../utils'); + isKDE.mockReturnValue(false); + const originalPlatform = process.platform; Object.defineProperty(process, 'platform', { value: 'linux', @@ -536,6 +541,34 @@ describe('main/windows/mainWindow', () => { }); expect(globalShortcut.registerAll).toHaveBeenCalledWith(['Alt+F', 'Alt+E', 'Alt+V', 'Alt+H', 'Alt+W', 'Alt+P'], expect.any(Function)); }); + + it('should register global shortcuts even when window is minimized on KDE/KWin', () => { + const {isKDE} = require('../utils'); + isKDE.mockReturnValue(true); + + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'linux', + }); + const window = { + ...baseWindow, + isMinimized: jest.fn().mockReturnValue(true), + on: jest.fn().mockImplementation((event, cb) => { + if (event === 'focus') { + cb(); + } + }), + }; + BrowserWindow.mockImplementation(() => window); + const mainWindow = new MainWindow(); + mainWindow.getBounds = jest.fn(); + mainWindow.init(); + + expect(globalShortcut.registerAll).toHaveBeenCalled(); + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); + }); }); describe('show', () => { diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index 63388653..f2f22c73 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -39,7 +39,7 @@ import performanceMonitor from 'main/performanceMonitor'; import type {SavedWindowState} from 'types/mainWindow'; import ContextMenu from '../contextMenu'; -import {getLocalPreload, isInsideRectangle} from '../utils'; +import {getLocalPreload, isInsideRectangle, isKDE} from '../utils'; const log = new Logger('MainWindow'); const ALT_MENU_KEYS = ['Alt+F', 'Alt+E', 'Alt+V', 'Alt+H', 'Alt+W', 'Alt+P']; @@ -329,6 +329,12 @@ export class MainWindow extends EventEmitter { globalShortcut.registerAll(ALT_MENU_KEYS, () => { // do nothing because we want to supress the menu popping up }); + + // check if KDE + windows is minimized to prevent unwanted focus event + // that was causing an error not allowing minimization (MM-60233) + if ((!this.win || this.win.isMinimized()) && isKDE()) { + return; + } } this.emit(MAIN_WINDOW_RESIZED, this.getBounds());