From a9f2ec06474e5686e4cf5857bc7fb2ce1d23ce3c Mon Sep 17 00:00:00 2001 From: Louis Matthijssen Date: Sat, 24 Jun 2023 14:14:10 +0200 Subject: [PATCH 1/2] Add option to hide tray icon --- src/module/hotkey-module.ts | 5 ++ src/module/preferences-module-preload.ts | 10 ++++ src/module/preferences-module.html | 16 ++++++ src/module/preferences-module.ts | 64 ++++++++++++++++++++++++ src/module/tray-module.ts | 4 ++ src/whatsapp.ts | 18 +++++-- 6 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/module/preferences-module-preload.ts create mode 100644 src/module/preferences-module.html create mode 100644 src/module/preferences-module.ts diff --git a/src/module/hotkey-module.ts b/src/module/hotkey-module.ts index bee344f..20fc206 100644 --- a/src/module/hotkey-module.ts +++ b/src/module/hotkey-module.ts @@ -78,6 +78,11 @@ export default class HotkeyModule extends Module { control: true, keys: ["Q"], action: () => this.whatsApp.quit() + }, + { + control: true, + keys: ["P"], + action: () => this.whatsApp.preferences.show() } ); } diff --git a/src/module/preferences-module-preload.ts b/src/module/preferences-module-preload.ts new file mode 100644 index 0000000..981fc8d --- /dev/null +++ b/src/module/preferences-module-preload.ts @@ -0,0 +1,10 @@ +import { contextBridge, ipcRenderer } from "electron"; + +contextBridge.exposeInMainWorld("preferences", { + getShowInTray: async (): Promise => { + return await ipcRenderer.invoke("get-preference", "show-in-tray"); + }, + setShowInTray: (enabled: boolean): void => { + ipcRenderer.send("set-preference", "show-in-tray", enabled); + } +}); diff --git a/src/module/preferences-module.html b/src/module/preferences-module.html new file mode 100644 index 0000000..2f671aa --- /dev/null +++ b/src/module/preferences-module.html @@ -0,0 +1,16 @@ + + +

Some preferences require a restart to take effect.

+

Press CTRL + P to open the preferences from the main window.

+ + diff --git a/src/module/preferences-module.ts b/src/module/preferences-module.ts new file mode 100644 index 0000000..5559951 --- /dev/null +++ b/src/module/preferences-module.ts @@ -0,0 +1,64 @@ +import path from "path"; +import { BrowserWindow, ipcMain } from "electron"; + +import WhatsApp from "../whatsapp"; +import Module from "./module"; +import Settings from "../settings"; + +export default class Preferences extends Module { + private readonly window: BrowserWindow; + private readonly settings: Settings; + + constructor( + private readonly whatsApp: WhatsApp + ) { + super(); + + this.settings = new Settings("preferences"); + + this.window = new BrowserWindow({ + title: "WhatsApp - Preferences", + width: 320, + height: 240, + show: false, + minimizable: false, + maximizable: false, + resizable: false, + autoHideMenuBar: true, + webPreferences: { + preload: path.join(__dirname, "preferences-module-preload.js") + } + }); + + this.window.on("close", this.onClose.bind(this)); + + ipcMain.handle("get-preference", this.onGetPreference.bind(this)); + ipcMain.on("set-preference", this.onSetPreference.bind(this)); + + this.window.loadFile("src/module/preferences-module.html"); + } + + get showInTray(): boolean { + return this.settings.get("show-in-tray", true); + } + + show(): void { + this.window.show(); + } + + private onClose(event: Electron.Event): void { + this.window.hide(); + + if (!this.whatsApp.quitting) { + event.preventDefault(); + } + } + + private onGetPreference(event: Electron.IpcMainInvokeEvent, key: string): void { + return this.settings.get(key); + } + + private onSetPreference(event: Electron.IpcMainEvent, key: string, value: any): void { + this.settings.set(key, value); + } +} diff --git a/src/module/tray-module.ts b/src/module/tray-module.ts index a5103f5..335ae79 100644 --- a/src/module/tray-module.ts +++ b/src/module/tray-module.ts @@ -25,6 +25,10 @@ export default class TrayModule extends Module { private updateMenu(unread: number = getUnreadMessages(this.window.title)) { const menu = Menu.buildFromTemplate([ + { + label: "Preferences", + click: () => this.whatsApp.preferences.show() + }, { label: this.window.isVisible() ? "Minimize to tray" : "Show WhatsApp", click: () => this.onClickFirstItem() diff --git a/src/whatsapp.ts b/src/whatsapp.ts index ae39e69..ce50d54 100644 --- a/src/whatsapp.ts +++ b/src/whatsapp.ts @@ -6,6 +6,7 @@ import HotkeyModule from "./module/hotkey-module"; import ModuleManager from "./module/module-manager"; import TrayModule from "./module/tray-module"; import WindowSettingsModule from "./module/window-settings-module"; +import PreferencesModule from "./module/preferences-module"; const USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.9999.0 Safari/537.36"; @@ -13,9 +14,12 @@ export default class WhatsApp { private readonly window: BrowserWindow; private readonly moduleManager: ModuleManager; + public readonly preferences: PreferencesModule; public quitting = false; constructor() { + this.preferences = new PreferencesModule(this); + this.window = new BrowserWindow({ title: "WhatsApp", width: 1100, @@ -29,13 +33,19 @@ export default class WhatsApp { } }); - this.moduleManager = new ModuleManager([ + const modules = [ new Electron21Fix(), new HotkeyModule(this, this.window), - new TrayModule(this, this.window), new WindowSettingsModule(this, this.window), - new ChromeVersionFix(this) - ]); + new ChromeVersionFix(this), + this.preferences + ]; + + if (this.preferences.showInTray) { + modules.push(new TrayModule(this, this.window)); + } + + this.moduleManager = new ModuleManager(modules); } public init() { From 52be8396702c27debc4b205374a7828a50c7c2c6 Mon Sep 17 00:00:00 2001 From: Louis Matthijssen Date: Sat, 24 Jun 2023 14:28:36 +0200 Subject: [PATCH 2/2] Include preferences HTML in build --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index db57fb2..3500b7e 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "files": [ "dist", + "src/module/preferences-module.html", "package.json" ], "extraFiles": [