From b691db30db7762f62975f1dac7b442a51aa14668 Mon Sep 17 00:00:00 2001 From: Dmytro Vasin Date: Sun, 17 Nov 2024 19:35:50 +0000 Subject: [PATCH] Multi monitor support --- {assets/web => src/assets}/icon.png | Bin {assets/web => src/assets}/trayIcon.png | Bin .../assets}/trayIconTemplate@2x.png | Bin src/main/index.js | 72 ++++++++++++++++-- src/renderer/about_page/index.html | 2 +- tools/webpack/renderer.js | 4 +- 6 files changed, 67 insertions(+), 11 deletions(-) rename {assets/web => src/assets}/icon.png (100%) rename {assets/web => src/assets}/trayIcon.png (100%) rename {assets/web => src/assets}/trayIconTemplate@2x.png (100%) diff --git a/assets/web/icon.png b/src/assets/icon.png similarity index 100% rename from assets/web/icon.png rename to src/assets/icon.png diff --git a/assets/web/trayIcon.png b/src/assets/trayIcon.png similarity index 100% rename from assets/web/trayIcon.png rename to src/assets/trayIcon.png diff --git a/assets/web/trayIconTemplate@2x.png b/src/assets/trayIconTemplate@2x.png similarity index 100% rename from assets/web/trayIconTemplate@2x.png rename to src/assets/trayIconTemplate@2x.png diff --git a/src/main/index.js b/src/main/index.js index a0e2645..d942497 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -36,6 +36,9 @@ const schema = { type: 'string', default: 'arrow' }, + active_monitor_id: { + type: 'number', + }, }; // app.getPath('userData') + '/config.json' @@ -52,15 +55,15 @@ let aboutWindow let foregroundMode = false if (process.env.NODE_ENV === 'development') { - foregroundMode = true + // foregroundMode = true } let showWhiteboard = store.get('show_whiteboard') let showToolbar = store.get('show_tool_bar') const iconSrc = { - DEFAULT: path.resolve('assets/web/trayIcon.png'), - darwin: path.resolve('assets/web/trayIconTemplate@2x.png'), + DEFAULT: path.resolve(__dirname, '../renderer/assets/trayIcon.png'), + darwin: path.resolve(__dirname, '../renderer/assets/trayIconTemplate@2x.png'), } const trayIcon = iconSrc[process.platform] || iconSrc.DEFAULT @@ -112,6 +115,14 @@ function updateContextMenu() { } }, { type: 'separator' }, + { + label: 'Reset to original', + click: () => { + store.clear() + mainWindow.reload() + } + }, + { type: 'separator' }, { label: 'About DrawPen', click: () => { @@ -134,8 +145,25 @@ function updateContextMenu() { tray.setContextMenu(contextMenu); } +function getActiveMonitor() { + const activeMonitorId = store.get('active_monitor_id') + + screen.getAllDisplays().forEach((display) => { + if (display.id === activeMonitorId) { + return display + } + }) + + const primaryDisplay = screen.getPrimaryDisplay() + store.set('active_monitor_id', primaryDisplay.id) + + return primaryDisplay +} + function createMainWindow () { - let { width, height } = screen.getPrimaryDisplay().workAreaSize; + const mainDisplay = getActiveMonitor() + + let { width, height } = mainDisplay.workAreaSize let isResizable = false if (process.env.NODE_ENV === 'development') { @@ -182,14 +210,19 @@ function createMainWindow () { mainWindow.webContents.on('did-finish-load', () => { if (foregroundMode) { - mainWindow.show() + showWindowOnActiveScreen(); } }) } function createAboutWindow() { + const currentDisplay = screen.getDisplayNearestPoint(screen.getCursorScreenPoint()); + const { x, y } = currentDisplay.workArea; + aboutWindow = new BrowserWindow({ show: false, + x: x, + y: y, width: 250, height: 250, resizable: false, @@ -200,6 +233,7 @@ function createAboutWindow() { preload: ABOUT_WINDOW_PRELOAD_WEBPACK_ENTRY, } }) + aboutWindow.center(); aboutWindow.loadURL(ABOUT_WINDOW_WEBPACK_ENTRY) @@ -217,11 +251,10 @@ function createAboutWindow() { aboutWindow.show() }) - // Open URL in user's browser. aboutWindow.webContents.setWindowOpenHandler((details) => { shell.openExternal(details.url); - return { action: "deny" }; // Prevent the app from opening the URL. + return { action: "deny" }; }) } @@ -347,7 +380,7 @@ function toggleWindow() { } function showDrawWindow() { - mainWindow.show() + showWindowOnActiveScreen() foregroundMode = true updateContextMenu() // Need to rerender the context menu @@ -382,3 +415,26 @@ function toggleWhiteboard() { mainWindow.webContents.send('toggle_whiteboard') updateContextMenu() // Need to rerender the context menu } + +function showWindowOnActiveScreen() { + const currentDisplay = screen.getDisplayNearestPoint(screen.getCursorScreenPoint()); + + if (store.get('active_monitor_id') === currentDisplay.id) { + mainWindow.show() + return + } + + mainWindow.setBounds(currentDisplay.workArea) + + if (process.env.NODE_ENV === 'development') { + mainWindow.setBounds({ + width: 500, + height: 500 + }) + } + + store.set('active_monitor_id', currentDisplay.id) + store.reset('tool_bar_x') + store.reset('tool_bar_y') + mainWindow.reload() +} diff --git a/src/renderer/about_page/index.html b/src/renderer/about_page/index.html index 0bd7462..2665515 100644 --- a/src/renderer/about_page/index.html +++ b/src/renderer/about_page/index.html @@ -43,7 +43,7 @@
- drawpen + drawpen DrawPen Version ... Source code available on GitHub diff --git a/tools/webpack/renderer.js b/tools/webpack/renderer.js index 595311a..b957260 100644 --- a/tools/webpack/renderer.js +++ b/tools/webpack/renderer.js @@ -31,8 +31,8 @@ const copyPlugins = [ new CopyWebpackPlugin({ patterns: [ { - from: path.resolve('assets/web'), - to: path.resolve('.webpack/renderer/web'), + from: path.resolve('src/assets'), + to: path.resolve('.webpack/renderer/assets'), } ] })