feishin/src/renderer/features/settings/components/window/window-settings.tsx

124 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-03-30 06:44:33 -07:00
import isElectron from 'is-electron';
import { Platform } from '/@/renderer/types';
import { useWindowSettings, useSettingsStoreActions } from '../../../../store/settings.store';
import {
2023-07-01 19:10:05 -07:00
SettingsSection,
SettingOption,
2023-03-30 06:44:33 -07:00
} from '/@/renderer/features/settings/components/settings-section';
import { Select, Switch, toast } from '/@/renderer/components';
2023-03-30 06:44:33 -07:00
const WINDOW_BAR_OPTIONS = [
2023-07-01 19:10:05 -07:00
{ label: 'Web (hidden)', value: Platform.WEB },
{ label: 'Windows', value: Platform.WINDOWS },
{ label: 'macOS', value: Platform.MACOS },
{ label: 'Native', value: Platform.LINUX },
2023-03-30 06:44:33 -07:00
];
2023-03-30 08:09:20 -07:00
const localSettings = isElectron() ? window.electron.localSettings : null;
2023-03-30 06:44:33 -07:00
export const WindowSettings = () => {
2023-07-01 19:10:05 -07:00
const settings = useWindowSettings();
const { setSettings } = useSettingsStoreActions();
2023-03-30 06:44:33 -07:00
2023-07-01 19:10:05 -07:00
const windowOptions: SettingOption[] = [
{
control: (
<Select
data={WINDOW_BAR_OPTIONS}
disabled={!isElectron()}
value={settings.windowBarStyle}
onChange={(e) => {
if (!e) return;
2023-07-01 19:10:05 -07:00
// Platform.LINUX is used as the native frame option regardless of the actual platform
const hasFrame = localSettings?.get('window_has_frame') as
| boolean
| undefined;
const isSwitchingToFrame = !hasFrame && e === Platform.LINUX;
const isSwitchingToNoFrame = hasFrame && e !== Platform.LINUX;
2023-05-27 04:20:08 -07:00
2023-07-01 19:10:05 -07:00
const requireRestart = isSwitchingToFrame || isSwitchingToNoFrame;
2023-05-27 04:20:08 -07:00
2023-07-01 19:10:05 -07:00
if (requireRestart) {
toast.info({
autoClose: false,
id: 'restart-toast',
message:
'Restart to apply changes... close the notification to restart Feishin',
onClose: () => {
window.electron.ipc!.send('app-restart');
2023-07-01 19:10:05 -07:00
},
title: 'Restart required',
});
} else {
toast.update({
autoClose: 0,
id: 'restart-toast',
message: '',
onClose: () => {},
}); // clean old toasts
}
2023-07-01 19:10:05 -07:00
localSettings?.set('window_window_bar_style', e as Platform);
setSettings({
window: {
...settings,
windowBarStyle: e as Platform,
},
});
}}
/>
),
description: 'Adjust the style of the application window bar',
isHidden: !isElectron(),
title: 'Window bar style',
},
{
control: (
<Switch
aria-label="Toggle minimize to tray"
defaultChecked={settings.exitToTray}
disabled={!isElectron()}
onChange={(e) => {
if (!e) return;
localSettings?.set('window_minimize_to_tray', e.currentTarget.checked);
setSettings({
window: {
...settings,
minimizeToTray: e.currentTarget.checked,
},
});
}}
/>
),
description: 'Minimize the application to the system tray',
isHidden: !isElectron(),
title: 'Minimize to tray',
},
{
control: (
<Switch
aria-label="Toggle exit to tray"
defaultChecked={settings.exitToTray}
disabled={!isElectron()}
onChange={(e) => {
if (!e) return;
localSettings?.set('window_exit_to_tray', e.currentTarget.checked);
setSettings({
window: {
...settings,
exitToTray: e.currentTarget.checked,
},
});
}}
/>
),
description: 'Exit the application to the system tray',
isHidden: !isElectron(),
title: 'Exit to tray',
},
];
2023-03-30 06:44:33 -07:00
2023-07-01 19:10:05 -07:00
return <SettingsSection options={windowOptions} />;
2023-03-30 06:44:33 -07:00
};