From a410222e14485d7b7a4b1bdbc5c9368049d039c3 Mon Sep 17 00:00:00 2001 From: Profesor08 Date: Mon, 13 Dec 2021 16:19:59 +0200 Subject: [PATCH] add hide on start option (#1919) * add hide on start option (#1918) * text update * clean up * removed `hideOnStart` from ConfigV1 & ConfigV2 * tests update --- src/common/config/defaultPreferences.ts | 1 + src/common/config/index.ts | 3 +++ src/common/config/upgradePreferences.test.js | 1 + src/main/Validator.test.js | 1 + src/main/Validator.ts | 1 + src/main/windows/mainWindow.test.js | 17 +++++++++++++ src/main/windows/mainWindow.ts | 8 ++++--- src/renderer/components/SettingsPage.tsx | 25 ++++++++++++++++++++ src/types/config.ts | 1 + 9 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/common/config/defaultPreferences.ts b/src/common/config/defaultPreferences.ts index 9e6adcf9..8f6b988c 100644 --- a/src/common/config/defaultPreferences.ts +++ b/src/common/config/defaultPreferences.ts @@ -31,6 +31,7 @@ const defaultPreferences: ConfigV3 = { useSpellChecker: true, enableHardwareAcceleration: true, autostart: true, + hideOnStart: false, spellCheckerLocales: [], darkMode: false, lastActiveTeam: 0, diff --git a/src/common/config/index.ts b/src/common/config/index.ts index 55af906e..f22d54f3 100644 --- a/src/common/config/index.ts +++ b/src/common/config/index.ts @@ -236,6 +236,9 @@ export class Config extends EventEmitter { get autostart() { return this.combinedData?.autostart ?? defaultPreferences.autostart; } + get hideOnStart() { + return this.combinedData?.hideOnStart ?? defaultPreferences.hideOnStart; + } get notifications() { return this.combinedData?.notifications ?? defaultPreferences.notifications; } diff --git a/src/common/config/upgradePreferences.test.js b/src/common/config/upgradePreferences.test.js index a6025598..d36aa7d2 100644 --- a/src/common/config/upgradePreferences.test.js +++ b/src/common/config/upgradePreferences.test.js @@ -100,6 +100,7 @@ describe('common/config/upgradePreferences', () => { useSpellChecker: false, enableHardwareAcceleration: false, autostart: false, + hideOnStart: false, spellCheckerLocale: 'en-CA', darkMode: true, downloadLocation: '/some/folder/name', diff --git a/src/main/Validator.test.js b/src/main/Validator.test.js index d1480297..a15191a5 100644 --- a/src/main/Validator.test.js +++ b/src/main/Validator.test.js @@ -126,6 +126,7 @@ describe('main/Validator', () => { describe('validateV3ConfigData', () => { const config = { autostart: true, + hideOnStart: false, darkMode: false, enableHardwareAcceleration: true, lastActiveTeam: 0, diff --git a/src/main/Validator.ts b/src/main/Validator.ts index 434e9a2c..5cabe840 100644 --- a/src/main/Validator.ts +++ b/src/main/Validator.ts @@ -119,6 +119,7 @@ const configDataSchemaV3 = Joi.object({ useSpellChecker: Joi.boolean().default(true), enableHardwareAcceleration: Joi.boolean().default(true), autostart: Joi.boolean().default(true), + hideOnStart: Joi.boolean().default(false), spellCheckerLocales: Joi.array().items(Joi.string()).default([]), spellCheckerURL: Joi.string().allow(null), darkMode: Joi.boolean().default(false), diff --git a/src/main/windows/mainWindow.test.js b/src/main/windows/mainWindow.test.js index 878af08e..73f51994 100644 --- a/src/main/windows/mainWindow.test.js +++ b/src/main/windows/mainWindow.test.js @@ -162,11 +162,28 @@ describe('main/windows/mainWindow', () => { }; BrowserWindow.mockImplementation(() => window); fs.readFileSync.mockImplementation(() => '{"x":400,"y":300,"width":1280,"height":700,"maximized":true,"fullscreen":false}'); + Config.hideOnStart = false; createMainWindow({}); expect(window.webContents.zoomLevel).toStrictEqual(0); expect(window.maximize).toBeCalled(); }); + it('should not show window on ready-to-show', () => { + const window = { + ...baseWindow, + once: jest.fn().mockImplementation((event, cb) => { + if (event === 'ready-to-show') { + cb(); + } + }), + }; + BrowserWindow.mockImplementation(() => window); + fs.readFileSync.mockImplementation(() => '{"x":400,"y":300,"width":1280,"height":700,"maximized":true,"fullscreen":false}'); + Config.hideOnStart = true; + createMainWindow({}); + expect(window.show).not.toHaveBeenCalled(); + }); + it('should save window state on close if the app will quit', () => { global.willAppQuit = true; const window = { diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index 1643bbe2..76a5b68f 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -107,9 +107,11 @@ function createMainWindow(options: {linuxAppIcon: string}) { mainWindow.once('ready-to-show', () => { mainWindow.webContents.zoomLevel = 0; - mainWindow.show(); - if (windowIsMaximized) { - mainWindow.maximize(); + if (Config.hideOnStart === false) { + mainWindow.show(); + if (windowIsMaximized) { + mainWindow.maximize(); + } } }); diff --git a/src/renderer/components/SettingsPage.tsx b/src/renderer/components/SettingsPage.tsx index 1e3c8801..6f44fbb5 100644 --- a/src/renderer/components/SettingsPage.tsx +++ b/src/renderer/components/SettingsPage.tsx @@ -58,6 +58,7 @@ export default class SettingsPage extends React.PureComponent; showTrayIconRef: React.RefObject; autostartRef: React.RefObject; + hideOnStartRef: React.RefObject; minimizeToTrayRef: React.RefObject; flashWindowRef: React.RefObject; bounceIconRef: React.RefObject; @@ -88,6 +89,7 @@ export default class SettingsPage extends React.PureComponent { + window.timers.setImmediate(this.saveSetting, CONFIG_TYPE_APP_OPTIONS, {key: 'hideOnStart', data: this.hideOnStartRef.current?.checked}); + this.setState({ + hideOnStart: this.hideOnStartRef.current?.checked, + }); + } + handleChangeMinimizeToTray = () => { const shouldMinimizeToTray = this.state.showTrayIcon && this.minimizeToTrayRef.current?.checked; @@ -419,6 +428,22 @@ export default class SettingsPage extends React.PureComponent ); + + options.push( + + + {'Launch app minimized'} + + {'If enabled, the app will start in system tray, and will not show the window on launch.'} + + ); } options.push( diff --git a/src/types/config.ts b/src/types/config.ts index f2f03be9..6be00940 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -34,6 +34,7 @@ export type ConfigV3 = { useSpellChecker: boolean; enableHardwareAcceleration: boolean; autostart: boolean; + hideOnStart: boolean; spellCheckerLocales: string[]; darkMode: boolean; downloadLocation: string;