Skip to content

Commit bd65edd

Browse files
committed
main: Revert DND on startup if expiration has passed.
Checks the dndExpiration config key on app startup. If the timestamp has passed, DND is toggled off. If still active, schedules a timeout to revert DND later.
1 parent d24a44b commit bd65edd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

app/common/config-schemata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const dndSettingsSchemata = {
88

99
export const configSchemata = {
1010
...dndSettingsSchemata,
11+
dndExpiration: z.number().nullable().default(null),
1112
appLanguage: z.string().nullable(),
1213
autoHideMenubar: z.boolean(),
1314
autoUpdate: z.boolean(),

app/main/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ const toggleApp = (): void => {
7070
}
7171
};
7272

73+
function checkDndExpirationOnStartup(page: WebContents): void {
74+
const expiration = ConfigUtil.getConfigItem("dndExpiration", null);
75+
76+
if (expiration && Date.now() > expiration) {
77+
const revert = DNDUtil.toggle();
78+
send(page, "toggle-dnd", revert.dnd, revert.newSettings);
79+
ConfigUtil.removeConfigItem("dndExpiration");
80+
} else if (expiration) {
81+
const timeLeft = expiration - Date.now();
82+
dndRevertTimeout = setTimeout(() => {
83+
const revert = DNDUtil.toggle();
84+
send(page, "toggle-dnd", revert.dnd, revert.newSettings);
85+
ConfigUtil.removeConfigItem("dndExpiration");
86+
}, timeLeft);
87+
}
88+
}
89+
7390
function createMainWindow(): BrowserWindow {
7491
// Load the previous state with fallback to defaults
7592
mainWindowState = windowStateKeeper({
@@ -272,7 +289,7 @@ function createMainWindow(): BrowserWindow {
272289
}
273290

274291
const page = mainWindow.webContents;
275-
292+
checkDndExpirationOnStartup(page);
276293
page.on("dom-ready", () => {
277294
if (ConfigUtil.getConfigItem("startMinimized", false)) {
278295
mainWindow.hide();
@@ -417,16 +434,21 @@ function createMainWindow(): BrowserWindow {
417434
if (result.dnd && duration && !Number.isNaN(duration)) {
418435
if (dndRevertTimeout) clearTimeout(dndRevertTimeout);
419436

437+
const expirationTime = Date.now() + duration * 60 * 1000;
438+
ConfigUtil.setConfigItem("dndExpiration", expirationTime);
439+
420440
dndRevertTimeout = setTimeout(
421441
() => {
422442
const revert = DNDUtil.toggle();
423443
send(_event.sender, "toggle-dnd", revert.dnd, revert.newSettings);
444+
ConfigUtil.removeConfigItem("dndExpiration");
424445
},
425446
duration * 60 * 1000,
426447
);
427448
} else if (dndRevertTimeout) {
428449
clearTimeout(dndRevertTimeout);
429450
dndRevertTimeout = null;
451+
ConfigUtil.removeConfigItem("dndExpiration");
430452
}
431453

432454
return;

0 commit comments

Comments
 (0)