Skip to content

Commit

Permalink
Add option to hide tray icon
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisMT committed Jun 24, 2023
1 parent 6512d86 commit eb021d2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/module/hotkey-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/module/preferences-module-preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { contextBridge, ipcRenderer } from "electron";

contextBridge.exposeInMainWorld("preferences", {
getShowInTray: async (): Promise<boolean> => {
return await ipcRenderer.invoke("get-preference", "show-in-tray");
},
showInTrayChanged: (enabled: boolean): void => {
ipcRenderer.send("preference-changed", "show-in-tray", enabled);
}
});
16 changes: 16 additions & 0 deletions src/module/preferences-module.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<label><input type="checkbox" id="show-in-tray"> Show in tray</label>

<p>Some preferences require a restart to take effect.</p>
<p>Press <kbd>CTRL</kbd> + <kbd>P</kbd> to open the preferences from the main window.</p>

<script>
const showInTray = document.querySelector("#show-in-tray");

showInTray.addEventListener("change", (event) => {
preferences.showInTrayChanged(event.target.checked);
});

(async () => {
showInTray.checked = await preferences.getShowInTray();
})()
</script>
64 changes: 64 additions & 0 deletions src/module/preferences-module.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 4 additions & 0 deletions src/module/tray-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 14 additions & 4 deletions src/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ 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";

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,
Expand All @@ -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() {
Expand Down

0 comments on commit eb021d2

Please sign in to comment.