Skip to content

Commit 51c8016

Browse files
🔧 refactor: Enhance Electron window management and display settings
- Imported `screen` module to accurately position webcam window. - Checked for window destruction before toggling visibility to prevent errors. - Adjusted floating window height for a more compact design. - Utilized display's scaling factor and work area size for dynamic window positioning. - Ensured other windows close when the main window is closed for clean app exit
1 parent b7b12c0 commit 51c8016

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

‎apps/desktop/electron/main.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { app, BrowserWindow, ipcMain, desktopCapturer, dialog, nativeImage, shell, systemPreferences, Tray } from 'electron'
1+
import { app, BrowserWindow, ipcMain, desktopCapturer, dialog, nativeImage, shell, systemPreferences, Tray, screen } from 'electron'
22
import os from 'os';
33
import axios from 'axios';
44
const { getWindows, activateWindow } = require('mac-windows');
@@ -512,7 +512,7 @@ ipcMain.handle('show-camera-window', async (_, show: boolean) => {
512512
});
513513

514514
const toggleCameraWindow = (show: boolean) => {
515-
if (webcamWindow) {
515+
if (webcamWindow && !webcamWindow.isDestroyed()) {
516516
showCameraWindow = show;
517517
if (show) webcamWindow.show();
518518
else webcamWindow.hide();
@@ -847,7 +847,7 @@ const requestPermissions = async (permission: string): Promise<boolean> => {
847847
function createFloatingWindow() {
848848
const floatingWindow = new BrowserWindow({
849849
width: 60,
850-
height: 150,
850+
height: 130,
851851
x: 0,
852852
y: 0,
853853
resizable: false,
@@ -881,15 +881,18 @@ function createFloatingWindow() {
881881

882882
const createWebcamWindow = () => {
883883

884-
const scalingFactor = 1;
885-
const monitorHeight = 1080;
884+
const primaryDisplay = screen.getPrimaryDisplay();
885+
886+
const scalingFactor = primaryDisplay.scaleFactor;
887+
const monitorHeight = primaryDisplay.workAreaSize.height;
888+
// const monitorHeight = 1080;
886889
const windowHeight = 232;
887890
// const windowHeight = 1000;
888891
const windowWidth = 200;
889892
// const windowWidth = 1000;
890893
const x = 100;
891894
const y =
892-
monitorHeight / scalingFactor - windowHeight - 100;
895+
monitorHeight / scalingFactor - windowHeight - 0;
893896

894897
const webcamWindow = new BrowserWindow({
895898
transparent: true,
@@ -930,7 +933,7 @@ function createWindow() {
930933
autoUpdater.forceDevUpdateConfig = true;
931934
autoUpdater.checkForUpdates();
932935

933-
if (mainWindow) {
936+
if (mainWindow && !mainWindow.isDestroyed()) {
934937
mainWindow.focus();
935938
return;
936939
}
@@ -962,6 +965,17 @@ function createWindow() {
962965
mainWindow?.webContents.send('main-process-message', (new Date).toLocaleString())
963966
})
964967

968+
// Close other windows when mainWindow is closed
969+
mainWindow.on('closed', () => {
970+
if (floatingWindow && !floatingWindow.isDestroyed()) {
971+
floatingWindow.close();
972+
}
973+
if (webcamWindow && !webcamWindow.isDestroyed()) {
974+
webcamWindow.close();
975+
}
976+
mainWindow = null; // Dereference the window object
977+
});
978+
965979

966980
// Create the floating window and hide it
967981
floatingWindow = createFloatingWindow();
@@ -1081,7 +1095,6 @@ function createWindow() {
10811095
app.on('window-all-closed', () => {
10821096
if (process.platform !== 'darwin') {
10831097
app.quit()
1084-
mainWindow = null
10851098
}
10861099
})
10871100

0 commit comments

Comments
 (0)