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..ff9a121 --- /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"); + }, + showInTrayChanged: (enabled: boolean): void => { + ipcRenderer.send("preference-changed", "show-in-tray", enabled); + } +}); diff --git a/src/module/preferences-module.html b/src/module/preferences-module.html new file mode 100644 index 0000000..5643173 --- /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..b66d865 --- /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("preference-changed", this.onPreferenceChanged.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 onPreferenceChanged(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() {