Skip to content

Commit

Permalink
Improve disable for feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-james-watson committed Nov 1, 2024
1 parent 48f2a6f commit 2792e4c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
10 changes: 10 additions & 0 deletions app/main/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ const store = new Store<{
settings: Settings;
appInitialized: boolean;
settingsVersion: number;
disableEndTime: number | null;
}>({
defaults: {
settings: defaultSettings,
appInitialized: false,
settingsVersion: 0,
disableEndTime: null,
},
});

Expand Down Expand Up @@ -140,3 +142,11 @@ export function setBreaksEnabled(breaksEnabled: boolean): void {
const settings: Settings = getSettings();
setSettings({ ...settings, breaksEnabled }, false);
}

export function setDisableEndTime(endTime: number | null): void {
store.set("disableEndTime", endTime);
}

export function getDisableEndTime(): number | null {
return store.get("disableEndTime");
}
65 changes: 40 additions & 25 deletions app/main/lib/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ import path from "path";
import packageJson from "../../../package.json";
import { Settings } from "../../types/settings";
import {
checkIdle,
checkInWorkingHours,
createBreak,
getBreakTime,
startBreakNow,
checkIdle,
checkInWorkingHours,
createBreak,
getBreakTime,
startBreakNow
} from "./breaks";
import { getSettings, setSettings } from "./store";
import {
getDisableEndTime,
getSettings,
setDisableEndTime,
setSettings
} from "./store";
import { createSettingsWindow } from "./windows";

let tray: Tray;
let lastMinsLeft = 0;
let disableTimeout: NodeJS.Timeout | null = null;
let disableEndTime: number | null = null;

function checkDisableTimeout() {
const disableEndTime = getDisableEndTime();

if (disableEndTime && Date.now() >= disableEndTime) {
setDisableEndTime(null);
const settings = getSettings();
setSettings({ ...settings, breaksEnabled: true });
buildTray();
}
}

function getDisableTimeRemaining(): string {
const disableEndTime = getDisableEndTime();
if (!disableEndTime) {
return "";
}
Expand Down Expand Up @@ -68,30 +83,18 @@ export function buildTray(): void {
const breaksEnabled = settings.breaksEnabled;

const setBreaksEnabled = (breaksEnabled: boolean): void => {
if (breaksEnabled && disableTimeout) {
clearTimeout(disableTimeout);
disableTimeout = null;
disableEndTime = null;
if (breaksEnabled) {
setDisableEndTime(null);
}

settings = getSettings();
setSettings({ ...settings, breaksEnabled });
buildTray();
};

const disableBreaksFor = (duration: number): void => {
setBreaksEnabled(false);
disableEndTime = Date.now() + duration;

if (disableTimeout) {
clearTimeout(disableTimeout);
}

disableTimeout = setTimeout(() => {
disableEndTime = null;
setBreaksEnabled(true);
}, duration);

const endTime = Date.now() + duration;
setDisableEndTime(endTime);
buildTray();
};

Expand Down Expand Up @@ -127,6 +130,8 @@ export function buildTray(): void {
}
}

const disableEndTime = getDisableEndTime();

const contextMenu = Menu.buildFromTemplate([
{
label: nextBreak,
Expand All @@ -135,7 +140,7 @@ export function buildTray(): void {
},
{
label: `Disabled for ${getDisableTimeRemaining()}`,
visible: disableTimeout !== null && !breaksEnabled,
visible: disableEndTime !== null && !breaksEnabled,
enabled: false,
},
{
Expand Down Expand Up @@ -202,7 +207,17 @@ export function buildTray(): void {

export function initTray(): void {
buildTray();
let lastDisableText = getDisableTimeRemaining();

setInterval(() => {
checkDisableTimeout();

const currentDisableText = getDisableTimeRemaining();
if (currentDisableText !== lastDisableText) {
buildTray();
lastDisableText = currentDisableText;
}

const breakTime = getBreakTime();
if (breakTime === null) {
return;
Expand Down

0 comments on commit 2792e4c

Please sign in to comment.