Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to hide tray icon #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"files": [
"dist",
"src/module/preferences-module.html",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this directory is the right place for the HTML. Let me know if you have a suggestion for a better place.

"package.json"
],
"extraFiles": [
Expand Down
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");
},
setShowInTray: (enabled: boolean): void => {
ipcRenderer.send("set-preference", "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.setShowInTray(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("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);
}
}
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