feishin/src/renderer/layouts/default-layout.tsx

92 lines
2.9 KiB
TypeScript
Raw Normal View History

import { HotkeyItem, useHotkeys } from '@mantine/hooks';
import clsx from 'clsx';
2022-12-19 15:59:14 -08:00
import isElectron from 'is-electron';
import { lazy } from 'react';
import { useNavigate } from 'react-router';
import styles from './default-layout.module.css';
import { CommandPalette } from '/@/renderer/features/search/components/command-palette';
import { MainContent } from '/@/renderer/layouts/default-layout/main-content';
import { PlayerBar } from '/@/renderer/layouts/default-layout/player-bar';
import { useCommandPalette } from '/@/renderer/store';
2023-05-18 02:15:03 -07:00
import {
2023-07-01 19:10:05 -07:00
useGeneralSettings,
useHotkeySettings,
useSettingsStore,
2023-07-01 19:10:05 -07:00
useSettingsStoreActions,
useWindowSettings,
2023-05-18 02:15:03 -07:00
} from '/@/renderer/store/settings.store';
2025-05-20 19:23:36 -07:00
import { Platform, PlaybackType } from '/@/shared/types/types';
2022-12-19 15:59:14 -08:00
if (!isElectron()) {
2023-07-01 19:10:05 -07:00
useSettingsStore.getState().actions.setSettings({
playback: {
...useSettingsStore.getState().playback,
type: PlaybackType.WEB,
},
});
2022-12-19 15:59:14 -08:00
}
2023-03-28 23:59:51 -07:00
const WindowBar = lazy(() =>
2023-07-01 19:10:05 -07:00
import('/@/renderer/layouts/window-bar').then((module) => ({
default: module.WindowBar,
})),
2023-03-28 23:59:51 -07:00
);
2022-12-19 15:59:14 -08:00
interface DefaultLayoutProps {
2023-07-01 19:10:05 -07:00
shell?: boolean;
2022-12-19 15:59:14 -08:00
}
export const DefaultLayout = ({ shell }: DefaultLayoutProps) => {
2023-07-01 19:10:05 -07:00
const { windowBarStyle } = useWindowSettings();
const { opened, ...handlers } = useCommandPalette();
const { bindings } = useHotkeySettings();
const navigate = useNavigate();
const localSettings = isElectron() ? window.api.localSettings : null;
2023-07-01 19:10:05 -07:00
const settings = useGeneralSettings();
const { setSettings } = useSettingsStoreActions();
2023-05-18 02:15:03 -07:00
2023-07-01 19:10:05 -07:00
const updateZoom = (increase: number) => {
const newVal = settings.zoomFactor + increase;
if (newVal > 300 || newVal < 50 || !isElectron()) return;
setSettings({
general: {
...settings,
zoomFactor: newVal,
},
});
localSettings?.setZoomFactor(settings.zoomFactor);
};
2023-06-11 19:45:13 +00:00
localSettings?.setZoomFactor(settings.zoomFactor);
2023-07-01 19:10:05 -07:00
const zoomHotkeys: HotkeyItem[] = [
[bindings.zoomIn.hotkey, () => updateZoom(5)],
[bindings.zoomOut.hotkey, () => updateZoom(-5)],
];
2023-06-11 19:45:13 +00:00
2023-07-01 19:10:05 -07:00
useHotkeys([
[bindings.globalSearch.hotkey, () => handlers.open()],
[bindings.browserBack.hotkey, () => navigate(-1)],
[bindings.browserForward.hotkey, () => navigate(1)],
2023-07-01 19:10:05 -07:00
...(isElectron() ? zoomHotkeys : []),
]);
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
return (
<>
<div
className={clsx(styles.layout, {
[styles.macos]: windowBarStyle === Platform.MACOS,
[styles.windows]: windowBarStyle === Platform.WINDOWS,
})}
2023-07-01 19:10:05 -07:00
id="default-layout"
>
{windowBarStyle !== Platform.WEB && <WindowBar />}
<MainContent shell={shell} />
<PlayerBar />
</div>
2023-07-01 19:10:05 -07:00
<CommandPalette modalProps={{ handlers, opened }} />
</>
);
2022-12-19 15:59:14 -08:00
};