diff --git a/packages/main/src/libs/windowOps.ts b/packages/main/src/libs/windowOps.ts index e7f0445..cef0aac 100644 --- a/packages/main/src/libs/windowOps.ts +++ b/packages/main/src/libs/windowOps.ts @@ -33,6 +33,14 @@ const useWindow = (browserWindow: BrowserWindow) => { ipcMain.on('setFocusable', (_event, value) => { browserWindow.setFocusable(value); }); + + browserWindow.on('will-resize', (event, newBounds) => { + browserWindow.webContents.send('will-resize', newBounds); + }); + + browserWindow.on('will-move', (event, newBounds) => { + browserWindow.webContents.send('will-move', newBounds); + }); }; export default useWindow; diff --git a/packages/preload/src/libs/windowOps.ts b/packages/preload/src/libs/windowOps.ts index 3c29e62..f20a7fe 100644 --- a/packages/preload/src/libs/windowOps.ts +++ b/packages/preload/src/libs/windowOps.ts @@ -1,4 +1,4 @@ -import type { BrowserWindow} from 'electron'; +import type { BrowserWindow } from 'electron'; import { ipcRenderer } from 'electron'; export const minimize = () => { @@ -28,3 +28,34 @@ export const setIgnoreMouseEvents = (...values: Parameters { ipcRenderer.send('setFocusable', value); }; + +type Callback = (newBounds: Electron.Rectangle) => void; +const eventKeys = ['will-resize', 'will-move'] as const; +export type WindowOpsEventKey = (typeof eventKeys)[number]; +const callbackHolder = new Map(); + +for (const eventKey of eventKeys) { + callbackHolder.set(eventKey, []); + ipcRenderer.on(eventKey, (_event, newBounds: Electron.Rectangle) => { + const callbacks = callbackHolder.get(eventKey); + if (callbacks) { + for (const callback of callbacks) { + callback(newBounds); + } + } + }); +} + +export const addEventListener = (eventKey: WindowOpsEventKey, callback: Callback) => { + callbackHolder.get(eventKey)!.push(callback); +}; + +export const removeEventListener = (eventKey: WindowOpsEventKey, callback: Callback) => { + const callbacks = callbackHolder.get(eventKey); + if (callbacks) { + const index = callbacks.indexOf(callback); + if (index !== -1) { + callbacks.splice(index, 1); + } + } +}; diff --git a/packages/renderer/src/App.vue b/packages/renderer/src/App.vue index 1ebd49b..fa0def5 100644 --- a/packages/renderer/src/App.vue +++ b/packages/renderer/src/App.vue @@ -10,20 +10,23 @@ import { onMounted } from 'vue'; import { useSettingsStore } from './store/settings'; import { useRecordStore } from './store/record'; - import { globalShortcut } from '#preload'; + import { globalShortcut, windowOps } from '#preload'; import { useRouter } from 'vue-router'; import { onUnmounted } from 'vue'; + import type { Rectangle } from 'electron'; const settingsStore = useSettingsStore(); const recordStore = useRecordStore(); const router = useRouter(); + // Auto connect onMounted(() => { if (settingsStore.connection.startup) { recordStore.connect(); } }); + // Global shortcut const toggleRoute = () => { if (!settingsStore.shortcut.enabled) { return; @@ -47,6 +50,26 @@ onUnmounted(() => { globalShortcut.unregisterAllGlobalShortcuts(); }); + + // Resize + const onResizeOrMove = (newBounds: Rectangle) => { + const route = router.currentRoute.value; + if (route.path === '/damage') return; + settingsStore.mainWindowBound.x = newBounds.x; + settingsStore.mainWindowBound.y = newBounds.y; + settingsStore.mainWindowBound.width = newBounds.width; + settingsStore.mainWindowBound.height = newBounds.height; + }; + + onMounted(() => { + windowOps.addEventListener('will-resize', onResizeOrMove); + windowOps.addEventListener('will-move', onResizeOrMove); + }); + + onUnmounted(() => { + windowOps.removeEventListener('will-resize', onResizeOrMove); + windowOps.removeEventListener('will-move', onResizeOrMove); + });