Skip to content

Commit f24c09b

Browse files
Add support for disabling for different amounts of time
1 parent a081ba1 commit f24c09b

File tree

2 files changed

+83
-9
lines changed

2 files changed

+83
-9
lines changed

app/main/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { initBreaks } from "./lib/breaks";
88
import "./lib/ipc";
99
import { showNotification } from "./lib/notifications";
1010
import {
11-
getAppInitialized,
12-
setAppInitialized,
13-
setBreaksEnabled,
11+
getAppInitialized,
12+
setAppInitialized,
13+
setBreaksEnabled
1414
} from "./lib/store";
1515
import { initTray } from "./lib/tray";
1616
import { createSettingsWindow, createSoundsWindow } from "./lib/windows";
@@ -26,12 +26,11 @@ if (!gotTheLock) {
2626
} else if (cliArg === "enable") {
2727
console.log("breaks enabled");
2828
setBreaksEnabled(true);
29-
} else if (process.platform !== "darwin") {
29+
} else {
3030
console.log("app already open, opening settings");
3131
createSettingsWindow();
32-
} else {
33-
log.info("app already running");
3432
}
33+
3534
app.exit();
3635
}
3736

app/main/lib/tray.ts

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ import { createSettingsWindow } from "./windows";
1515

1616
let tray: Tray;
1717
let lastMinsLeft = 0;
18+
let disableTimeout: NodeJS.Timeout | null = null;
19+
let disableEndTime: number | null = null;
20+
21+
function getDisableTimeRemaining(): string {
22+
if (!disableEndTime) {
23+
return "";
24+
}
25+
26+
const remainingMs = disableEndTime - Date.now();
27+
const remainingMinutes = Math.floor(remainingMs / 60000);
28+
const remainingHours = Math.floor(remainingMinutes / 60);
29+
const remainingDisplayMinutes = remainingMinutes % 60;
30+
31+
if (remainingMinutes < 1) {
32+
return "<1m";
33+
} else if (remainingHours > 0) {
34+
return `${remainingHours}h ${remainingDisplayMinutes}m`;
35+
} else {
36+
return `${remainingMinutes}m`;
37+
}
38+
}
1839

1940
export function buildTray(): void {
2041
if (!tray) {
@@ -47,11 +68,33 @@ export function buildTray(): void {
4768
const breaksEnabled = settings.breaksEnabled;
4869

4970
const setBreaksEnabled = (breaksEnabled: boolean): void => {
71+
if (breaksEnabled && disableTimeout) {
72+
clearTimeout(disableTimeout);
73+
disableTimeout = null;
74+
disableEndTime = null;
75+
}
76+
5077
settings = getSettings();
5178
setSettings({ ...settings, breaksEnabled });
5279
buildTray();
5380
};
5481

82+
const disableBreaksFor = (duration: number): void => {
83+
setBreaksEnabled(false);
84+
disableEndTime = Date.now() + duration;
85+
86+
if (disableTimeout) {
87+
clearTimeout(disableTimeout);
88+
}
89+
90+
disableTimeout = setTimeout(() => {
91+
disableEndTime = null;
92+
setBreaksEnabled(true);
93+
}, duration);
94+
95+
buildTray();
96+
};
97+
5598
const createAboutWindow = (): void => {
5699
dialog.showMessageBox({
57100
title: "About",
@@ -87,7 +130,12 @@ export function buildTray(): void {
87130
const contextMenu = Menu.buildFromTemplate([
88131
{
89132
label: nextBreak,
90-
visible: breakTime !== null && inWorkingHours,
133+
visible: breakTime !== null && inWorkingHours && settings.breaksEnabled,
134+
enabled: false,
135+
},
136+
{
137+
label: `Disabled for ${getDisableTimeRemaining()}`,
138+
visible: disableTimeout !== null && !breaksEnabled,
91139
enabled: false,
92140
},
93141
{
@@ -102,8 +150,35 @@ export function buildTray(): void {
102150
},
103151
{ type: "separator" },
104152
{
105-
label: breaksEnabled ? "Disable" : "Enable",
106-
click: setBreaksEnabled.bind(null, !breaksEnabled),
153+
label: "Enable",
154+
click: setBreaksEnabled.bind(null, true),
155+
visible: !breaksEnabled,
156+
},
157+
{
158+
label: "Disable...",
159+
submenu: [
160+
{ label: "Indefinitely", click: setBreaksEnabled.bind(null, false) },
161+
{ label: "30 minutes", click: () => disableBreaksFor(30 * 60 * 1000) },
162+
{ label: "1 hour", click: () => disableBreaksFor(60 * 60 * 1000) },
163+
{ label: "2 hours", click: () => disableBreaksFor(2 * 60 * 60 * 1000) },
164+
{ label: "4 hours", click: () => disableBreaksFor(4 * 60 * 60 * 1000) },
165+
{
166+
label: "Rest of day",
167+
click: () => {
168+
const now = new Date();
169+
const endOfDay = new Date(
170+
now.getFullYear(),
171+
now.getMonth(),
172+
now.getDate(),
173+
23,
174+
59,
175+
59
176+
);
177+
disableBreaksFor(endOfDay.getTime() - now.getTime());
178+
},
179+
},
180+
],
181+
visible: breaksEnabled,
107182
},
108183
{
109184
label: "Start break now",

0 commit comments

Comments
 (0)