Skip to content

Commit

Permalink
feat: window boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanimodori committed Feb 22, 2024
1 parent e92a480 commit 5f8aee9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/main/src/libs/windowOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
33 changes: 32 additions & 1 deletion packages/preload/src/libs/windowOps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BrowserWindow} from 'electron';
import type { BrowserWindow } from 'electron';
import { ipcRenderer } from 'electron';

export const minimize = () => {
Expand Down Expand Up @@ -28,3 +28,34 @@ export const setIgnoreMouseEvents = (...values: Parameters<BrowserWindow['setIgn
export const setFocusable = (value: boolean) => {
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<string, Callback[]>();

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);
}
}
};
25 changes: 24 additions & 1 deletion packages/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
</script>

<style>
Expand Down
14 changes: 10 additions & 4 deletions packages/renderer/src/store/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ export const useSettingsStore = defineStore(
order: [],
});

const windowStyle = ref({
const mainWindowBound = ref({
x: 0,
y: 0,
width: 800,
height: 600,
});

const damageWindowBound = ref({
x: 0,
y: 0,
width: 700,
height: 500,
});

const shortcut = ref({
Expand All @@ -42,7 +47,8 @@ export const useSettingsStore = defineStore(
connection,
damageStyle,
damageCols,
windowStyle,
mainWindowBound,
damageWindowBound,
shortcut,
};
},
Expand Down

0 comments on commit 5f8aee9

Please sign in to comment.