fix: refactor tray handling to use shared state for task and countdown visibility

This commit is contained in:
Johannes Millan 2025-11-28 18:14:53 +01:00
parent 3b0fbd9efc
commit e212f9a9ba
3 changed files with 50 additions and 34 deletions

View file

@ -1,8 +1,6 @@
import { App, ipcMain, Menu, nativeTheme, Tray } from 'electron';
import { IPC } from './shared-with-frontend/ipc-events.const';
import { getSettings } from './get-settings';
import { getWin } from './main-window';
import { GlobalConfigState } from '../src/app/features/config/global-config.model';
import { getIsTrayShowCurrentTask, getIsTrayShowCurrentCountdown } from './shared-state';
import { TaskCopy } from '../src/app/features/tasks/task.model';
import { release } from 'os';
import {
@ -112,40 +110,37 @@ function initListeners(): void {
currentFocusSessionTime || 0,
);
const mainWin = getWin();
getSettings(mainWin, (settings: GlobalConfigState) => {
const isTrayShowCurrentTask = settings.misc.isTrayShowCurrentTask;
const isTrayShowCurrentCountdown = settings.misc.isTrayShowCurrentCountdown;
const isTrayShowCurrentTask = getIsTrayShowCurrentTask();
const isTrayShowCurrentCountdown = getIsTrayShowCurrentCountdown();
const msg =
isTrayShowCurrentTask && currentTask
? createIndicatorMessage(
currentTask,
isPomodoroEnabled,
currentPomodoroSessionTime,
isTrayShowCurrentCountdown,
)
: '';
const msg =
isTrayShowCurrentTask && currentTask
? createIndicatorMessage(
currentTask,
isPomodoroEnabled,
currentPomodoroSessionTime,
isTrayShowCurrentCountdown,
)
: '';
if (tray) {
// tray handling
if (currentTask && currentTask.title) {
tray.setTitle(msg);
if (!IS_MAC) {
// NOTE apparently this has no effect for gnome
tray.setToolTip(msg);
}
} else {
tray.setTitle('');
if (!IS_MAC) {
// NOTE apparently this has no effect for gnome
tray.setToolTip(msg);
}
const suf = shouldUseDarkColors ? '-d.png' : '-l.png';
setTrayIcon(tray, DIR + `stopped${suf}`);
if (tray) {
// tray handling
if (currentTask && currentTask.title) {
tray.setTitle(msg);
if (!IS_MAC) {
// NOTE apparently this has no effect for gnome
tray.setToolTip(msg);
}
} else {
tray.setTitle('');
if (!IS_MAC) {
// NOTE apparently this has no effect for gnome
tray.setToolTip(msg);
}
const suf = shouldUseDarkColors ? '-d.png' : '-l.png';
setTrayIcon(tray, DIR + `stopped${suf}`);
}
});
}
},
);

View file

@ -20,7 +20,12 @@ import { exec } from 'child_process';
import { getWin } from './main-window';
import { quitApp, showOrFocus } from './various-shared';
import { loadSimpleStoreAll, saveSimpleStore } from './simple-store';
import { getIsLocked, setIsMinimizeToTray } from './shared-state';
import {
getIsLocked,
setIsMinimizeToTray,
setIsTrayShowCurrentTask,
setIsTrayShowCurrentCountdown,
} from './shared-state';
import { BACKUP_DIR, BACKUP_DIR_WINSTORE } from './backup';
import { pluginNodeExecutor } from './plugin-node-executor';
import { GlobalConfigState } from '../src/app/features/config/global-config.model';
@ -65,6 +70,8 @@ export const initIpcInterfaces = (): void => {
ipcMain.on(IPC.OPEN_EXTERNAL, (ev, url: string) => shell.openExternal(url));
ipcMain.on(IPC.TRANSFER_SETTINGS_TO_ELECTRON, (ev, cfg: GlobalConfigState) => {
setIsMinimizeToTray(cfg.misc.isMinimizeToTray);
setIsTrayShowCurrentTask(cfg.misc.isTrayShowCurrentTask);
setIsTrayShowCurrentCountdown(cfg.misc.isTrayShowCurrentCountdown);
});
ipcMain.handle(IPC.SAVE_FILE_DIALOG, async (ev, { filename, data }) => {

View file

@ -6,6 +6,8 @@
let isQuiting = false;
let isLocked = false;
let isMinimizeToTray = false;
let isTrayShowCurrentTask = false;
let isTrayShowCurrentCountdown = false;
export const getIsQuiting = (): boolean => isQuiting;
@ -24,3 +26,15 @@ export const getIsMinimizeToTray = (): boolean => isMinimizeToTray;
export const setIsMinimizeToTray = (value: boolean): void => {
isMinimizeToTray = value;
};
export const getIsTrayShowCurrentTask = (): boolean => isTrayShowCurrentTask;
export const setIsTrayShowCurrentTask = (value: boolean): void => {
isTrayShowCurrentTask = value;
};
export const getIsTrayShowCurrentCountdown = (): boolean => isTrayShowCurrentCountdown;
export const setIsTrayShowCurrentCountdown = (value: boolean): void => {
isTrayShowCurrentCountdown = value;
};