Skip to content

Commit

Permalink
feat: added Auto-Start Work Time, Minimize To Tray and Close To Tray …
Browse files Browse the repository at this point in the history
…options to the app settings
  • Loading branch information
roldanjr committed Jun 29, 2020
1 parent 2b5f230 commit 560eea9
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 49 deletions.
97 changes: 62 additions & 35 deletions app/electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
SET_ALWAYS_ON_TOP,
SET_FULLSCREEN_BREAK,
SET_MINIMIZE,
SET_HIDE,
SET_CLOSE,
SET_UI_THEME,
SET_NATIVE_TITLEBAR,
getTrayIcon,
isWindow,
getFromStorage,
} from "./helpers";
import store from "./store";

Expand Down Expand Up @@ -82,10 +83,38 @@ function createMainWindow() {
win?.show();
});

win.on("close", (e) => {
win.on("minimize", async () => {
try {
if (win) {
const data = await getFromStorage(win, "settings");
if (data.minimizeToTray) {
if (!isFullScreen) {
win?.hide();
tray = createSystemTray();
}
}
}
} catch (error) {
console.log(error);
}
});

win.on("close", async (e) => {
e.preventDefault();
if (!isFullScreen) {
win?.hide();
try {
if (win) {
const data = await getFromStorage(win, "settings");
if (!data.closeToTray) {
app.exit();
} else {
if (!isFullScreen) {
win?.hide();
tray = createSystemTray();
}
}
}
} catch (error) {
console.log(error);
}
});

Expand All @@ -96,37 +125,39 @@ function createMainWindow() {
win.on("show", () => {
tray?.destroy();
});
}

win.on("hide", () => {
tray = new Tray(getTrayIcon());
tray.setToolTip("PRODUCTIVITY TIMER");
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: "Show the app",
click: () => {
win?.show();
},
function createSystemTray() {
const tray = new Tray(getTrayIcon());
tray.setToolTip("PRODUCTIVITY TIMER");
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: "Show the app",
click: () => {
win?.show();
},
{
label: "Quit",
click: () => {
app.exit();
},
},
{
label: "Quit",
click: () => {
app.exit();
},
])
);
},
])
);

tray?.on("click", () => {
if (!win?.isVisible()) {
win?.show();
} else {
if (!win?.isFullScreen()) {
win?.hide();
}
tray?.on("click", () => {
if (!win?.isVisible()) {
win?.show();
} else {
if (!win?.isFullScreen()) {
win?.hide();
}
});
}
});

return tray;
}

if (!onlySingleIntance) {
Expand Down Expand Up @@ -251,13 +282,9 @@ ipcMain.on(SET_UI_THEME, (e, { isDarkMode }) => {
store.set("isDarkMode", isDarkMode);
});

ipcMain.on(SET_MINIMIZE, () => {
win?.minimize();
});
ipcMain.on(SET_MINIMIZE, () => win?.minimize());

ipcMain.on(SET_HIDE, () => {
win?.hide();
});
ipcMain.on(SET_CLOSE, () => app.quit());

ipcMain.on(SET_NATIVE_TITLEBAR, (e, { useNativeTitlebar }) => {
if (store.get("useNativeTitlebar") !== useNativeTitlebar) {
Expand Down
4 changes: 2 additions & 2 deletions app/electron/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ export const SET_FULLSCREEN_BREAK = "SET_FULLSCREEN_BREAK";
export const SET_NATIVE_TITLEBAR = "SET_NATIVE_TITLEBAR";
export const SET_UI_THEME = "SET_UI_THEME";
export const SET_MINIMIZE = "SET_MINIMIZE";
export const SET_HIDE = "SET_HIDE";
export const SET_CLOSE = "SET_CLOSE";

export const TO_MAIN: string[] = [
SET_ALWAYS_ON_TOP,
SET_FULLSCREEN_BREAK,
SET_NATIVE_TITLEBAR,
SET_UI_THEME,
SET_MINIMIZE,
SET_HIDE,
SET_CLOSE,
];

export const FROM_MAIN: string[] = [];
17 changes: 17 additions & 0 deletions app/electron/helpers/getFromStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BrowserWindow } from "electron";

const getFromStorage = async (win: BrowserWindow, key: string) => {
try {
const data = await win.webContents.executeJavaScript(
`localStorage.getItem("${key}")`
);
if (data === null) {
return undefined;
}
return JSON.parse(data);
} catch (error) {
return error;
}
};

export { getFromStorage };
1 change: 1 addition & 0 deletions app/electron/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./getTrayIcon";
export * from "./constants";
export * from "./isWindow";
export * from "./getIcon";
export * from "./getFromStorage";
1 change: 0 additions & 1 deletion app/electron/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Store from "electron-store";
import { systemPreferences } from "electron";
import { isWindow } from "../helpers";

type StoreProps = {
isDarkMode: boolean;
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "productivity-timer",
"version": "2.10.4-beta",
"version": "2.13.4-beta",
"private": true,
"license": "MIT",
"main": "public/electron.js",
Expand Down
17 changes: 17 additions & 0 deletions app/src/contexts/CounterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TimerTypes,
SPECIAL_BREAK,
SettingTypes,
setPlay,
} from "store";
import { useNotification, useSleepMode } from "hooks";

Expand Down Expand Up @@ -264,6 +265,11 @@ const CounterProvider: React.FC = ({ children }) => {
case SHORT_BREAK:
dispatch(setTimerType("STAY_FOCUS"));
dispatch(setRound(timer.round + 1));

if (!settings.autoStartWorkTime) {
dispatch(setPlay(false));
}

notification(
"Short Break Finished",
{ body: "It is time to focus and work again." },
Expand All @@ -274,6 +280,11 @@ const CounterProvider: React.FC = ({ children }) => {
case LONG_BREAK:
dispatch(setTimerType("STAY_FOCUS"));
dispatch(setRound(1));

if (!settings.autoStartWorkTime) {
dispatch(setPlay(false));
}

notification(
"Long Break Finished",
{ body: "It is time to focus and work again." },
Expand All @@ -283,6 +294,11 @@ const CounterProvider: React.FC = ({ children }) => {

case SPECIAL_BREAK:
dispatch(setTimerType("STAY_FOCUS"));

if (!settings.autoStartWorkTime) {
dispatch(setPlay(false));
}

notification(
"Special Break Finished",
{ body: "It is time to focus and work again." },
Expand All @@ -304,6 +320,7 @@ const CounterProvider: React.FC = ({ children }) => {
notification,
config.sessionRounds,
settings.notificationProperty,
settings.autoStartWorkTime,
]);

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions app/src/contexts/ElectronContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const SET_FULLSCREEN_BREAK = "SET_FULLSCREEN_BREAK";
export const SET_NATIVE_TITLEBAR = "SET_NATIVE_TITLEBAR";
export const SET_UI_THEME = "SET_UI_THEME";
export const SET_MINIMIZE = "SET_MINIMIZE";
export const SET_HIDE = "SET_HIDE";
export const SET_CLOSE = "SET_CLOSE";

type ElectronProps = {
onMinimizeCallback?: () => void;
Expand All @@ -37,7 +37,7 @@ const ElectronProvider: React.FC = ({ children }) => {

const onExitCallback = useCallback(() => {
if (isElectron()) {
electron.send(SET_HIDE);
electron.send(SET_CLOSE);
}
}, [electron]);

Expand Down
27 changes: 27 additions & 0 deletions app/src/routes/Settings/FeatureSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
setNotificationProperty,
setEnableFullscreenBreak,
setUseNativeTitlebar,
setAutoStartWorkTime,
setMinimizeToTray,
setCloseToTray,
} from "store";

import { Toggler, TogglerProps, Collapse, Radio } from "components";
Expand Down Expand Up @@ -75,6 +78,30 @@ const FeatureSection: React.FC = () => {
dispatch(setEnableTimerAnimation(!settings.enableTimerAnimation));
}, [dispatch, settings.enableTimerAnimation]),
},
{
id: "auto-start-work-time",
label: "Auto-start Work Time",
checked: settings.autoStartWorkTime,
onChange: useCallback(() => {
dispatch(setAutoStartWorkTime(!settings.autoStartWorkTime));
}, [dispatch, settings.autoStartWorkTime]),
},
{
id: "minimize-to-tray",
label: "Minimize To Tray",
checked: settings.minimizeToTray,
onChange: useCallback(() => {
dispatch(setMinimizeToTray(!settings.minimizeToTray));
}, [dispatch, settings.minimizeToTray]),
},
{
id: "close-to-tray",
label: "Close To Tray",
checked: settings.closeToTray,
onChange: useCallback(() => {
dispatch(setCloseToTray(!settings.closeToTray));
}, [dispatch, settings.closeToTray]),
},
];

const onChangeNotificationProps = useCallback(
Expand Down
10 changes: 5 additions & 5 deletions app/src/routes/Timer/Control/Control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const Control: React.FC<Props> = ({ resetTimerAction }) => {
activateWarning();
return;
}
dispatch(setPlay());
dispatch(setPlay(!timer.playing));
}, [dispatch, activateWarning, timer.playing, settings.enableStrictMode]);

const onNotifacationSoundCallback = useCallback(() => {
Expand All @@ -92,24 +92,24 @@ const Control: React.FC<Props> = ({ resetTimerAction }) => {
} else {
dispatch(skipTimer("LONG_BREAK"));
}
if (!timer.playing) dispatch(setPlay());
if (!timer.playing) dispatch(setPlay(!timer.playing));
break;

case SHORT_BREAK:
dispatch(skipTimer("STAY_FOCUS"));
dispatch(setRound(timer.round + 1));
if (!timer.playing) dispatch(setPlay());
if (!timer.playing) dispatch(setPlay(!timer.playing));
break;

case LONG_BREAK:
dispatch(skipTimer("STAY_FOCUS"));
dispatch(setRound(1));
if (!timer.playing) dispatch(setPlay());
if (!timer.playing) dispatch(setPlay(!timer.playing));
break;

case SPECIAL_BREAK:
dispatch(skipTimer("STAY_FOCUS"));
if (!timer.playing) dispatch(setPlay());
if (!timer.playing) dispatch(setPlay(!timer.playing));
break;
}
}, [
Expand Down
30 changes: 30 additions & 0 deletions app/src/store/settings/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
ENABLE_FULLSCREEN_BREAK,
USE_NATIVE_TITLE_BAR,
ENABLE_STRICT_MODE,
CLOSE_TO_TRAY,
MINIMIZE_TO_TRAY,
AUTO_START_WORK_TIME,
} from "./types";

export const setAlwaysOnTop = (
Expand Down Expand Up @@ -81,6 +84,33 @@ export const setNotificationProperty = (
};
};

export const setCloseToTray = (
closeToTray: SettingTypes["closeToTray"]
): SettingActionTypes => {
return {
type: CLOSE_TO_TRAY,
payload: closeToTray,
};
};

export const setMinimizeToTray = (
minimizeToTray: SettingTypes["minimizeToTray"]
): SettingActionTypes => {
return {
type: MINIMIZE_TO_TRAY,
payload: minimizeToTray,
};
};

export const setAutoStartWorkTime = (
autoStartWorkTime: SettingTypes["autoStartWorkTime"]
): SettingActionTypes => {
return {
type: AUTO_START_WORK_TIME,
payload: autoStartWorkTime,
};
};

export const restoreDefaultSettings = () => ({
type: RESTORE_DEFAULT_SETTINGS,
});
Loading

0 comments on commit 560eea9

Please sign in to comment.