Skip to content

Commit 684da87

Browse files
committed
main: Handle toggle-dnd-request and auto-revert DND in ipcMain.
When a duration is provided, sets a timer to automatically revert DND mode. Ensures only one timer is active at a time, and cleans up appropriately when DND is turned off early. We import dnd-util.js to handle the DNDUtil.toggle() in our timer logic. Fixes #561.
1 parent c634e1c commit 684da87

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

app/main/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as remoteMain from "@electron/remote/main";
1818
import windowStateKeeper from "electron-window-state";
1919

2020
import * as ConfigUtil from "../common/config-util.js";
21+
import * as DNDUtil from "../common/dnd-util.js";
2122
import {bundlePath, bundleUrl, publicPath} from "../common/paths.js";
2223
import * as t from "../common/translation-util.js";
2324
import type {RendererMessage} from "../common/typed-ipc.js";
@@ -48,6 +49,7 @@ let badgeCount: number;
4849

4950
let isQuitting = false;
5051

52+
let dndRevertTimeout: NodeJS.Timeout | null = null;
5153
// Load this file in main window
5254
const mainUrl = new URL("app/renderer/main.html", bundleUrl).href;
5355

@@ -407,6 +409,29 @@ function createMainWindow(): BrowserWindow {
407409
listener: Channel,
408410
...parameters: Parameters<RendererMessage[Channel]>
409411
) => {
412+
if (listener === "toggle-dnd-request") {
413+
const [duration] = parameters as [number?];
414+
const result = DNDUtil.toggle();
415+
send(_event.sender, "toggle-dnd", result.dnd, result.newSettings);
416+
417+
if (result.dnd && duration && !Number.isNaN(duration)) {
418+
if (dndRevertTimeout) clearTimeout(dndRevertTimeout);
419+
420+
dndRevertTimeout = setTimeout(
421+
() => {
422+
const revert = DNDUtil.toggle();
423+
send(_event.sender, "toggle-dnd", revert.dnd, revert.newSettings);
424+
},
425+
duration * 60 * 1000,
426+
);
427+
} else if (dndRevertTimeout) {
428+
clearTimeout(dndRevertTimeout);
429+
dndRevertTimeout = null;
430+
}
431+
432+
return;
433+
}
434+
410435
send(page, listener, ...parameters);
411436
},
412437
);

0 commit comments

Comments
 (0)