Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

desktop: fix minimize to system tray not working immediately after enabling (#3734)

Muhammad Ali95db7c63294f
diff --git a/apps/desktop/src/api/os-integration.ts b/apps/desktop/src/api/os-integration.ts
index 03baa3b54..693c610cd 100644
--- a/apps/desktop/src/api/os-integration.ts
+++ b/apps/desktop/src/api/os-integration.ts
@@ -30,6 +30,7 @@ import { resolvePath } from "../utils/resolve-path";
import { observable } from "@trpc/server/observable";
import { AssetManager } from "../utils/asset-manager";
import { isFlatpak } from "../utils";
+import { setupDesktopIntegration } from "../utils/desktop-integration";
const t = initTRPC.create();
@@ -91,6 +92,7 @@ export const osIntegrationRouter = t.router({
AutoLaunch.disable();
}
config.desktopSettings = settings;
+ setupDesktopIntegration();
}),
selectDirectory: t.procedure
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 99c2de4e7..16ccc42fe 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -24,7 +24,6 @@ import { configureAutoUpdater } from "./utils/autoupdater";
import { getBackgroundColor, getTheme, setTheme } from "./utils/theme";
import { setupMenu } from "./utils/menu";
import { WindowState } from "./utils/window-state";
-import { AutoLaunch } from "./utils/autolaunch";
import { setupJumplist } from "./utils/jumplist";
import { setupTray } from "./utils/tray";
import { CLIOptions, parseArguments } from "./cli";
@@ -35,6 +34,7 @@ import { config } from "./utils/config";
import path from "path";
import { bringToFront } from "./utils/bring-to-front";
import { bridge } from "./api/bridge";
+import { setupDesktopIntegration } from "./utils/desktop-integration";
// only run a single instance
if (!MAC_APP_STORE && !app.requestSingleInstanceLock()) {
@@ -177,49 +177,3 @@ function createURL(options: CLIOptions, path = "/") {
return url;
}
-
-function setupDesktopIntegration() {
- const desktopIntegration = config.desktopSettings;
-
- if (
- desktopIntegration.closeToSystemTray ||
- desktopIntegration.minimizeToSystemTray
- ) {
- setupTray();
- }
-
- // when close to system tray is enabled, it becomes nigh impossible
- // to "quit" the app. This is necessary in order to fix that.
- if (desktopIntegration.closeToSystemTray) {
- app.on("before-quit", () => app.exit(0));
- }
-
- globalThis.window?.on("close", (e) => {
- if (config.desktopSettings.closeToSystemTray) {
- e.preventDefault();
- if (process.platform == "darwin") {
- // on macOS window cannot be minimized/hidden if it is already fullscreen
- // so we just close it.
- if (globalThis.window?.isFullScreen()) app.exit(0);
- else app.hide();
- } else {
- globalThis.window?.minimize();
- globalThis.window?.hide();
- }
- }
- });
-
- globalThis.window?.on("minimize", () => {
- if (config.desktopSettings.minimizeToSystemTray) {
- if (process.platform == "darwin") {
- app.hide();
- } else {
- globalThis.window?.hide();
- }
- }
- });
-
- if (desktopIntegration.autoStart) {
- AutoLaunch.enable(!!desktopIntegration.startMinimized);
- }
-}
diff --git a/apps/desktop/src/utils/desktop-integration.ts b/apps/desktop/src/utils/desktop-integration.ts
new file mode 100644
index 000000000..c96751cb9
--- /dev/null
+++ b/apps/desktop/src/utils/desktop-integration.ts
@@ -0,0 +1,70 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { app } from "electron";
+import { config } from "./config";
+import { setupTray } from "./tray";
+import { AutoLaunch } from "./autolaunch";
+
+export function setupDesktopIntegration() {
+ const desktopIntegration = config.desktopSettings;
+
+ if (
+ desktopIntegration.closeToSystemTray ||
+ desktopIntegration.minimizeToSystemTray
+ ) {
+ setupTray();
+ }
+
+ // when close to system tray is enabled, it becomes nigh impossible
+ // to "quit" the app. This is necessary in order to fix that.
+ if (desktopIntegration.closeToSystemTray) {
+ app.on("before-quit", () => app.exit(0));
+ }
+
+ globalThis.window?.on("close", (e) => {
+ if (config.desktopSettings.closeToSystemTray) {
+ e.preventDefault();
+ if (process.platform == "darwin") {
+ // on macOS window cannot be minimized/hidden if it is already fullscreen
+ // so we just close it.
+ if (globalThis.window?.isFullScreen()) app.exit(0);
+ else app.hide();
+ } else {
+ try {
+ globalThis.window?.minimize();
+ globalThis.window?.hide();
+ } catch (error) {}
+ }
+ }
+ });
+
+ globalThis.window?.on("minimize", () => {
+ if (config.desktopSettings.minimizeToSystemTray) {
+ if (process.platform == "darwin") {
+ app.hide();
+ } else {
+ globalThis.window?.hide();
+ }
+ }
+ });
+
+ if (desktopIntegration.autoStart) {
+ AutoLaunch.enable(!!desktopIntegration.startMinimized);
+ }
+}