Skip to content

Commit

Permalink
feat: Control whether the application exits when closing the window.
Browse files Browse the repository at this point in the history
Signed-off-by: The1111mp <The1111mp@outlook.com>
  • Loading branch information
1111mp committed Nov 17, 2023
1 parent 87f765e commit 1202d9d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion @types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Themes } from 'types';
import { Closer, Themes } from 'types';
import type { UpdateInfo as ElectronUpdateInfo } from 'electron-updater';

declare global {
Expand All @@ -23,6 +23,7 @@ declare global {
interface Setting {
locale: string;
theme: Themes;
closer: Closer;
directory: string;
mirror: string;
}
Expand Down
12 changes: 12 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,17 @@
"Migration-error": {
"message": "Please close all Node processes and restart the application to complete the update",
"description": "The text of the Migration-error"
},
"When-Closing": {
"message": "When Closing",
"description": "The text of the When-Closing"
},
"Minimize-Window": {
"message": "Minimize To System Tray",
"description": "The text of the Minimize-Window"
},
"Quit-App": {
"message": "Quit App",
"description": "The text of the Quit-App"
}
}
12 changes: 12 additions & 0 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,17 @@
"Migration-error": {
"message": "请关闭所有 Node 进程之后重启应用以完成更新",
"description": "The text of the Migration-error"
},
"When-Closing": {
"message": "关闭时",
"description": "The text of the When-Closing"
},
"Minimize-Window": {
"message": "最小化到系统托盘",
"description": "The text of the Minimize-Window"
},
"Quit-App": {
"message": "退出应用",
"description": "The text of the Quit-App"
}
}
6 changes: 2 additions & 4 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
} from './utils/projects';
import { gt } from 'semver';
import loadLocale from './locale';
import { Themes } from '../types';
import { Closer, Themes } from '../types';

import type { MenuItemConstructorOptions } from 'electron';

Expand Down Expand Up @@ -182,9 +182,7 @@ const createWindow = async (code?: number) => {
*/

app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
if (process.platform !== 'darwin') {
if (setting.closer === Closer.Close) {
app.quit();
}
});
Expand Down
5 changes: 4 additions & 1 deletion src/main/utils/setting.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { pathExists, readJson, writeJson } from 'fs-extra';
import { app } from 'electron';
import { INSTALL_DIR, SETTING_JSONFILE } from '../constants';
import { Themes } from '../../types';
import { Closer, Themes } from '../../types';

export async function getSetting(): Promise<Nvmd.Setting> {
if (!(await pathExists(SETTING_JSONFILE)))
return {
locale: app.getLocale().startsWith('en') ? 'en' : 'zh-CN',
theme: Themes.System,
closer: Closer.Minimize,
directory: INSTALL_DIR,
mirror: 'https://nodejs.org/dist',
};

const setting = await readJson(SETTING_JSONFILE, { throws: false });
if (!setting.directory) setting.directory = INSTALL_DIR;
if (!setting.closer) setting.closer = Closer.Minimize;
return (
setting || {
locale: app.getLocale().startsWith('en') ? 'en' : 'zh-CN',
theme: Themes.System,
closer: Closer.Minimize,
directory: INSTALL_DIR,
mirror: 'https://nodejs.org/dist',
}
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import en from 'antd/locale/en_US';
import zhCN from 'antd/locale/zh_CN';

import { applyTheme } from './util';
import { Themes } from 'types';
import { Closer, Themes } from 'types';

type StateType = {
locale: string;
theme: Themes; // from setting
closer: Closer;
directory: string; // node installation directory
sysTheme: Themes; // system real theme
mirror: string;
Expand All @@ -31,7 +32,7 @@ type StateAction = {
};

export default function App() {
const { locale, theme, directory, mirror, localeMessages } =
const { locale, theme, closer, directory, mirror, localeMessages } =
window.Context.getSettingData();

const [state, dispatch] = useReducer(
Expand All @@ -50,6 +51,7 @@ export default function App() {
{
locale,
theme: theme,
closer,
directory,
sysTheme: window.Context.getSystemTheme() as Themes,
mirror,
Expand Down Expand Up @@ -179,6 +181,7 @@ export default function App() {
value={{
locale: state.locale,
theme: state.theme,
closer: state.closer,
direction: state.directory,
mirror: state.mirror,
getMessage,
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/appContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createContext, useContext } from 'react';
import { Themes } from 'types';
import { Closer, Themes } from 'types';

type AppContextType = {
locale: string;
theme: Themes;
closer: Closer;
direction: string;
mirror: string;
getMessage: I18nFn;
Expand Down
19 changes: 18 additions & 1 deletion src/renderer/pages/home/setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'antd';
import { EditOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
import { useAppContext, useI18n } from 'renderer/appContext';
import { Themes } from 'types';
import { Closer, Themes } from 'types';

export type Ref = {
show: () => void;
Expand Down Expand Up @@ -86,6 +86,7 @@ const Content = forwardRef<ContentRef, {}>(({}, ref) => {
const {
locale,
theme: ctxTheme,
closer: ctxCloser,
direction: ctxDirection,
mirror: ctxMirror,
onUpdateSetting,
Expand All @@ -94,6 +95,7 @@ const Content = forwardRef<ContentRef, {}>(({}, ref) => {

const [language, setLanguage] = useState<string>(() => locale);
const [theme, setTheme] = useState<Themes>(() => ctxTheme);
const [closer, setCloser] = useState<Closer>(() => ctxCloser);
const [directory, setDirectory] = useState<string>(() => ctxDirection);
const [mirror, setMirror] = useState<string>(() => ctxMirror);

Expand All @@ -116,6 +118,7 @@ const Content = forwardRef<ContentRef, {}>(({}, ref) => {
if (
language === locale &&
theme === ctxTheme &&
closer === ctxCloser &&
directory === ctxDirection &&
mirror === ctxMirror
)
Expand All @@ -124,6 +127,7 @@ const Content = forwardRef<ContentRef, {}>(({}, ref) => {
onUpdateSetting({
locale: language,
theme,
closer,
directory,
mirror,
});
Expand Down Expand Up @@ -161,6 +165,19 @@ const Content = forwardRef<ContentRef, {}>(({}, ref) => {
/>
</Descriptions.Item>

<Descriptions.Item label={i18n('When-Closing')}>
<Radio.Group
value={closer}
options={[
{ label: i18n('Minimize-Window'), value: Closer.Minimize },
{ label: i18n('Quit-App'), value: Closer.Close },
]}
onChange={(evt) => {
setCloser(evt.target.value as Closer);
}}
/>
</Descriptions.Item>

<Descriptions.Item
label={
<Space size={4}>
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ export enum Themes {
Light = 'light',
Dark = 'dark',
}

export enum Closer {
Minimize = 'minimize',
Close = 'close',
}

0 comments on commit 1202d9d

Please sign in to comment.