From 2f22951e9f977af33a0bb1b42e5cdf338ddc4fb6 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Fri, 26 Jan 2024 14:14:12 +0100 Subject: [PATCH] Add "Always on top" setting which makes Heynote stay on top of other programs --- electron/config.js | 2 ++ electron/main/index.ts | 37 +++++++++++++++++++++++++--- src/components/settings/Settings.vue | 12 ++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/electron/config.js b/electron/config.js index 592dc41f..7c443ead 100644 --- a/electron/config.js +++ b/electron/config.js @@ -33,6 +33,7 @@ const schema = { "bufferPath" : {type: "string", default: ""}, "showInDock": {type: "boolean", default: true}, "showInMenu": {type: "boolean", default: false}, + "alwaysOnTop": {type: "boolean", default: false}, "bracketClosing": {type: "boolean", default: false}, // when default font settings are used, fontFamily and fontSize is not specified in the @@ -67,6 +68,7 @@ const defaults = { bufferPath: "", showInDock: true, showInMenu: false, + alwaysOnTop: false, bracketClosing: false, }, theme: "system", diff --git a/electron/main/index.ts b/electron/main/index.ts index 3cd7dc02..dd7a26f4 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -225,14 +225,18 @@ function registerGlobalHotkey() { return } if (win.isFocused()) { - if (!!app.hide) { + if (isMac) { // app.hide() only available on macOS // We want to use app.hide() so that the menu bar also gets changed app?.hide() + if (CONFIG.get("settings.alwaysOnTop")) { + // if alwaysOnTop is on, calling app.hide() won't hide the window + win.hide() + } } else { win.blur() - if (CONFIG.get("settings.showInMenu")) { - // if we're using a tray icon we want to completely hide the window + if (CONFIG.get("settings.showInMenu") || CONFIG.get("settings.alwaysOnTop")) { + // if we're using a tray icon, or alwaysOnTop is on, we want to completely hide the window win.hide() } } @@ -275,11 +279,34 @@ function registerShowInMenu() { } } +function registerAlwaysOnTop() { + if (CONFIG.get("settings.alwaysOnTop")) { + const disableAlwaysOnTop = () => { + win.setAlwaysOnTop(true, "floating"); + win.setVisibleOnAllWorkspaces(true, {visibleOnFullScreen: true}); + win.setFullScreenable(false); + } + // if we're in fullscreen mode, we need to exit fullscreen before we can set alwaysOnTop + if (win.isFullScreen()) { + // on Mac setFullScreen happens asynchronously, so we need to wait for the event before we can disable alwaysOnTop + win.once("leave-full-screen", disableAlwaysOnTop) + win.setFullScreen(false) + } else { + disableAlwaysOnTop() + } + } else { + win.setAlwaysOnTop(false); + win.setVisibleOnAllWorkspaces(false); + win.setFullScreenable(true); + } +} + app.whenReady().then(createWindow).then(async () => { initializeAutoUpdate(win) registerGlobalHotkey() registerShowInDock() registerShowInMenu() + registerAlwaysOnTop() }) app.on("before-quit", () => { @@ -328,6 +355,7 @@ ipcMain.handle('settings:set', async (event, settings) => { let showInDockChanged = settings.showInDock !== CONFIG.get("settings.showInDock"); let showInMenuChanged = settings.showInMenu !== CONFIG.get("settings.showInMenu"); let bufferPathChanged = settings.bufferPath !== CONFIG.get("settings.bufferPath"); + let alwaysOnTopChanged = settings.alwaysOnTop !== CONFIG.get("settings.alwaysOnTop"); CONFIG.set("settings", settings) win?.webContents.send(SETTINGS_CHANGE_EVENT, settings) @@ -341,6 +369,9 @@ ipcMain.handle('settings:set', async (event, settings) => { if (showInMenuChanged) { registerShowInMenu() } + if (alwaysOnTopChanged) { + registerAlwaysOnTop() + } if (bufferPathChanged) { const buffer = loadBuffer() if (buffer.exists()) { diff --git a/src/components/settings/Settings.vue b/src/components/settings/Settings.vue index 7869277a..42c9ee6f 100644 --- a/src/components/settings/Settings.vue +++ b/src/components/settings/Settings.vue @@ -33,6 +33,7 @@ globalHotkey: this.initialSettings.globalHotkey, showInDock: this.initialSettings.showInDock, showInMenu: this.initialSettings.showInMenu, + alwaysOnTop: this.initialSettings.alwaysOnTop, bracketClosing: this.initialSettings.bracketClosing, autoUpdate: this.initialSettings.autoUpdate, bufferPath: this.initialSettings.bufferPath, @@ -82,6 +83,7 @@ globalHotkey: this.globalHotkey, showInDock: this.showInDock, showInMenu: this.showInMenu || !this.showInDock, + alwaysOnTop: this.alwaysOnTop, autoUpdate: this.autoUpdate, bracketClosing: this.bracketClosing, bufferPath: this.bufferPath, @@ -184,7 +186,7 @@
-

Show In

+

Window / Application

+