Skip to content

Add toggle setting for Mac tray icon unread count display #1429

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

Open
wants to merge 1 commit 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 app/common/config-schemata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const configSchemata = {
startAtLogin: z.boolean(),
startMinimized: z.boolean(),
trayIcon: z.boolean(),
trayBadgeCount: z.boolean(),
useManualProxy: z.boolean(),
useProxy: z.boolean(),
useSystemProxy: z.boolean(),
Expand Down
1 change: 1 addition & 0 deletions app/common/typed-ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type RendererMessage = {
"toggle-sidebar": (show: boolean) => void;
"toggle-silent": (state: boolean) => void;
"toggle-tray": (state: boolean) => void;
"toggle-tray-badge-count": (newValue: boolean) => void;
toggletray: () => void;
tray: (argument: number) => void;
"update-realm-icon": (serverURL: string, iconURL: string) => void;
Expand Down
30 changes: 30 additions & 0 deletions app/renderer/js/pages/preference/general-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export function initGeneralSection({$root}: GeneralSectionProperties): void {
</div>
<div class="setting-control"></div>
</div>
<div
class="setting-row"
id="tray-badge-count-option"
style="display:${process.platform === "darwin" ? "" : "none"}"
>
<div class="setting-description">
${t.__("Show unread count on tray icon")}
</div>
<div class="setting-control"></div>
</div>
<div
class="setting-row"
id="dock-bounce-option"
Expand Down Expand Up @@ -213,6 +223,7 @@ export function initGeneralSection({$root}: GeneralSectionProperties): void {

updateTrayOption();
updateBadgeOption();
updateTrayBadgeCountOption();
updateSilentOption();
autoUpdateOption();
betaUpdateOption();
Expand Down Expand Up @@ -288,6 +299,25 @@ export function initGeneralSection({$root}: GeneralSectionProperties): void {
});
}

function updateTrayBadgeCountOption(): void {
generateSettingOption({
$element: $root.querySelector(
"#tray-badge-count-option .setting-control",
)!,
value: ConfigUtil.getConfigItem("trayBadgeCount", false),
clickHandler() {
const newValue = !ConfigUtil.getConfigItem("trayBadgeCount", false);
ConfigUtil.setConfigItem("trayBadgeCount", newValue);
ipcRenderer.send(
"forward-message",
"toggle-tray-badge-count",
newValue,
);
updateTrayBadgeCountOption();
},
});
}

function updateDockBouncing(): void {
generateSettingOption({
$element: $root.querySelector("#dock-bounce-option .setting-control")!,
Expand Down
44 changes: 35 additions & 9 deletions app/renderer/js/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ const createTray = function (): void {
}
};

const shouldShowTrayIcon = function (): boolean {
return (
process.platform === "darwin" ||
process.platform === "win32" ||
process.platform === "linux"
);
};

const displayTrayIcon = function (tray: ElectronTray): void {
if (process.platform === "darwin") {
tray.setImage(iconPath());
const showTrayBadgeCount = ConfigUtil.getConfigItem(
"trayBadgeCount",
false,
);
tray.setTitle(showTrayBadgeCount && unread > 0 ? unread.toString() : "");
} else {
const image = renderNativeImage(unread);
tray.setImage(image);
}

tray.setToolTip(`${unread} unread messages`);
};

export function initializeTray(serverManagerView: ServerManagerView) {
ipcRenderer.on("destroytray", () => {
if (!tray) {
Expand All @@ -197,17 +221,15 @@ export function initializeTray(serverManagerView: ServerManagerView) {
return;
}

// We don't want to create tray from unread messages on macOS since it already has dock badges.
if (process.platform === "linux" || process.platform === "win32") {
if (shouldShowTrayIcon()) {
if (argument === 0) {
unread = argument;
tray.setImage(iconPath());
tray.setTitle("");
tray.setToolTip("No unread messages");
} else {
unread = argument;
const image = renderNativeImage(argument);
tray.setImage(image);
tray.setToolTip(`${argument} unread messages`);
displayTrayIcon(tray);
}
}
});
Expand All @@ -225,10 +247,8 @@ export function initializeTray(serverManagerView: ServerManagerView) {
} else {
state = true;
createTray();
if (process.platform === "linux" || process.platform === "win32") {
const image = renderNativeImage(unread);
tray!.setImage(image);
tray!.setToolTip(`${unread} unread messages`);
if (shouldShowTrayIcon()) {
displayTrayIcon(tray!);
}

ConfigUtil.setConfigItem("trayIcon", true);
Expand All @@ -239,6 +259,12 @@ export function initializeTray(serverManagerView: ServerManagerView) {

ipcRenderer.on("toggletray", toggleTray);

ipcRenderer.on("toggle-tray-badge-count", () => {
if (tray && shouldShowTrayIcon()) {
displayTrayIcon(tray);
}
});

if (ConfigUtil.getConfigItem("trayIcon", true)) {
createTray();
}
Expand Down