Skip to content

Commit

Permalink
feat: add support for open at login in the settings tab
Browse files Browse the repository at this point in the history
fix #124, #198, #350
  • Loading branch information
roldanjr committed Jul 3, 2023
1 parent f201525 commit f57e133
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/main/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./globalShortcuts";
export * from "./autoUpdater";
export * from "./getTrayIcon";
export * from "./isWindow";
export * from "./isMacOS";
export * from "./getIcon";
export * from "./getFromStorage";
export * from "./contextMenu";
1 change: 1 addition & 0 deletions app/main/src/helpers/isMacOS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isMacOS = () => process.platform === "darwin";
33 changes: 21 additions & 12 deletions app/main/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ import {
RELEASED_NOTES_LINK,
TRAY_ICON_UPDATE,
SET_COMPACT_MODE,
SET_OPEN_AT_LOGIN,
} from "@pomatez/shareables";
import {
activateGlobalShortcuts,
activateAutoUpdate,
blockShortcutKeys,
getIcon,
isWindow,
isMacOS,
getFromStorage,
createContextMenu,
} from "./helpers";
import { activateUser } from "./helpers/analytics";
import store from "./store";
import isDev from "electron-is-dev";
const isWin = process.platform === "win32";
const isMac = process.platform === "darwin";

import "v8-compile-cache";
import {
Expand All @@ -55,7 +55,7 @@ const trayIcon = path.join(__dirname, "./assets/tray-dark.png");

const onlySingleInstance = app.requestSingleInstanceLock();

const applicationMenu = isMac
const applicationMenu = isMacOS()
? Menu.buildFromTemplate([{ role: "appMenu" }, { role: "editMenu" }])
: null;
Menu.setApplicationMenu(applicationMenu);
Expand Down Expand Up @@ -204,10 +204,6 @@ function createMainWindow() {
)
);

win.on("closed", () => {
win = null;
});

createContextMenu(win);
}

Expand All @@ -221,7 +217,7 @@ const contextMenu = Menu.buildFromTemplate([
},
},
{
label: "Exit",
label: "Quit",
click: () => {
app.exit();
},
Expand Down Expand Up @@ -266,6 +262,7 @@ if (!onlySingleInstance) {
const extensions = ["REACT_DEVELOPER_TOOLS", "REDUX_DEVTOOLS"];
const installer = await import("electron-devtools-installer");
console.log(installer);

for (const tool of extensions) {
try {
await installer.default(installer[tool], true);
Expand All @@ -274,6 +271,7 @@ if (!onlySingleInstance) {
}
}
}

createMainWindow();

if (onProduction) {
Expand Down Expand Up @@ -393,9 +391,13 @@ ipcMain.on(SET_MINIMIZE, (e, { minimizeToTray }) => {
});

ipcMain.on(SET_CLOSE, (e, { closeToTray }) => {
app.quit();
if (tray === null && closeToTray) {
createSystemTray();
if (!closeToTray) {
app.exit();
} else {
if (closeToTray && tray === null) {
createSystemTray();
}
app.quit();
}
});

Expand All @@ -414,6 +416,14 @@ ipcMain.on(TRAY_ICON_UPDATE, (e, dataUrl) => {
tray?.setImage(image);
});

ipcMain.on(SET_OPEN_AT_LOGIN, (e, { openAtLogin }) => {
store.set("openAtLogin", openAtLogin);
app.setLoginItemSettings({
openAtLogin: openAtLogin,
openAsHidden: openAtLogin,
});
});

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
Expand All @@ -430,5 +440,4 @@ app.on("will-quit", () => {
globalShortcut.unregisterAll();
});

app.setLoginItemSettings({ openAtLogin: true });
app.setAppUserModelId("com.roldanjr.pomatez");
2 changes: 1 addition & 1 deletion app/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.send(channel, ...args);
}
},
recieve: (channel: string, response: (...args: any[]) => void) => {
receive: (channel: string, response: (...args: any[]) => void) => {
if (FROM_MAIN.includes(channel)) {
ipcRenderer.on(
channel,
Expand Down
22 changes: 22 additions & 0 deletions app/main/src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Store from "electron-store";
import { nativeTheme } from "electron";
import { isWindow } from "./helpers";

type StoreProps = {
userId?: string;
isDarkMode?: boolean;
useNativeTitlebar?: boolean;
compactMode?: boolean;
openAtLogin?: boolean;
};

const store = new Store<StoreProps>({
defaults: {
isDarkMode: nativeTheme.shouldUseDarkColors,
useNativeTitlebar: !isWindow(),
compactMode: false,
openAtLogin: false,
},
});

export default store;
22 changes: 0 additions & 22 deletions app/main/src/store/index.ts

This file was deleted.

9 changes: 9 additions & 0 deletions app/renderer/src/contexts/ElectronContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SET_NATIVE_TITLEBAR,
TRAY_ICON_UPDATE,
SET_COMPACT_MODE,
SET_OPEN_AT_LOGIN,
} from "@pomatez/shareables";

import { AppStateTypes, SettingTypes } from "store";
Expand Down Expand Up @@ -118,6 +119,14 @@ const ElectronProvider: React.FC = ({ children }) => {
}
}, [electron, settings.useNativeTitlebar]);

useEffect(() => {
if (isElectron()) {
electron.send(SET_OPEN_AT_LOGIN, {
openAtLogin: settings.openAtLogin,
});
}
}, [electron, settings.openAtLogin]);

useEffect(() => {
if (isElectron() && timer.playing) {
const canvas = document.createElement("canvas");
Expand Down
9 changes: 9 additions & 0 deletions app/renderer/src/routes/Settings/FeatureSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
setCloseToTray,
setEnableVoiceAssistance,
setEnableCompactMode,
setOpenAtLogin,
} from "store";
import { Toggler, TogglerProps, Collapse, Radio } from "components";
import { ThemeContext } from "contexts";
Expand Down Expand Up @@ -126,6 +127,14 @@ const FeatureSection: React.FC = () => {
);
}, [dispatch, settings.enableVoiceAssistance]),
},
{
id: "open-at-login",
label: "Open At Login",
checked: settings.openAtLogin,
onChange: useCallback(() => {
dispatch(setOpenAtLogin(!settings.openAtLogin));
}, [dispatch, settings.openAtLogin]),
},
];

const onChangeNotificationProps = useCallback(
Expand Down
10 changes: 10 additions & 0 deletions app/renderer/src/store/settings/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
AUTO_START_WORK_TIME,
ENABLE_VOICE_ASSISTANCE,
ENABLE_COMPACT_MODE,
OPEN_AT_LOGIN,
} from "./types";

export const setAlwaysOnTop = (
Expand Down Expand Up @@ -131,6 +132,15 @@ export const setAutoStartWorkTime = (
};
};

export const setOpenAtLogin = (
openAtLogin: SettingTypes["openAtLogin"]
): SettingActionTypes => {
return {
type: OPEN_AT_LOGIN,
payload: openAtLogin,
};
};

export const restoreDefaultSettings = () => ({
type: RESTORE_DEFAULT_SETTINGS,
});
7 changes: 7 additions & 0 deletions app/renderer/src/store/settings/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AUTO_START_WORK_TIME,
ENABLE_VOICE_ASSISTANCE,
ENABLE_COMPACT_MODE,
OPEN_AT_LOGIN,
} from "./types";

const defaultSettings: SettingTypes = {
Expand All @@ -32,6 +33,7 @@ const defaultSettings: SettingTypes = {
minimizeToTray: false,
autoStartWorkTime: false,
useNativeTitlebar: detectOS() === "Windows" ? false : true,
openAtLogin: false,
};

const settings =
Expand Down Expand Up @@ -110,6 +112,11 @@ export const settingReducer = (
...state,
autoStartWorkTime: action.payload,
};
case OPEN_AT_LOGIN:
return {
...state,
openAtLogin: action.payload,
};
case RESTORE_DEFAULT_SETTINGS:
return defaultSettings;
default:
Expand Down
3 changes: 3 additions & 0 deletions app/renderer/src/store/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type SettingTypes = {
minimizeToTray: boolean;
autoStartWorkTime: boolean;
notificationType: "none" | "normal" | "extra";
openAtLogin: boolean;
};

export const ALWAYS_ON_TOP = `${settings} ALWAYS_ON_TOP`;
Expand All @@ -36,6 +37,8 @@ export const CLOSE_TO_TRAY = `${settings} CLOSE_TO_TRAY`;
export const MINIMIZE_TO_TRAY = `${settings} MINIMIZE_TO_TRAY`;
export const AUTO_START_WORK_TIME = `${settings} AUTO_START_WORK_TIME`;

export const OPEN_AT_LOGIN = `${settings} OPEN_AT_LOGIN`;

export const RESTORE_DEFAULT_SETTINGS = `${settings} RESTORE_DEFAULT_SETTINGS`;

export type SettingActionTypes = {
Expand Down
2 changes: 2 additions & 0 deletions app/shareables/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const SET_ALWAYS_ON_TOP = "SET_ALWAYS_ON_TOP";
export const SET_FULLSCREEN_BREAK = "SET_FULLSCREEN_BREAK";
export const SET_COMPACT_MODE = "SET_COMPACT_MODE";
export const SET_NATIVE_TITLEBAR = "SET_NATIVE_TITLEBAR";
export const SET_OPEN_AT_LOGIN = "SET_OPEN_AT_LOGIN";
export const TRAY_ICON_UPDATE = "TRAY_ICON_UPDATE";
export const SET_UI_THEME = "SET_UI_THEME";
export const SET_MINIMIZE = "SET_MINIMIZE";
Expand All @@ -13,6 +14,7 @@ export const TO_MAIN: string[] = [
SET_FULLSCREEN_BREAK,
SET_COMPACT_MODE,
SET_NATIVE_TITLEBAR,
SET_OPEN_AT_LOGIN,
TRAY_ICON_UPDATE,
SET_UI_THEME,
SET_MINIMIZE,
Expand Down

0 comments on commit f57e133

Please sign in to comment.