Skip to content

Commit

Permalink
Multi monitor support
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroVasin committed Nov 17, 2024
1 parent 07c69cb commit b691db3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
72 changes: 64 additions & 8 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const schema = {
type: 'string',
default: 'arrow'
},
active_monitor_id: {
type: 'number',
},
};

// app.getPath('userData') + '/config.json'
Expand All @@ -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/[email protected]'),
DEFAULT: path.resolve(__dirname, '../renderer/assets/trayIcon.png'),
darwin: path.resolve(__dirname, '../renderer/assets/[email protected]'),
}

const trayIcon = iconSrc[process.platform] || iconSrc.DEFAULT
Expand Down Expand Up @@ -112,6 +115,14 @@ function updateContextMenu() {
}
},
{ type: 'separator' },
{
label: 'Reset to original',
click: () => {
store.clear()
mainWindow.reload()
}
},
{ type: 'separator' },
{
label: 'About DrawPen',
click: () => {
Expand All @@ -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') {
Expand Down Expand Up @@ -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,
Expand All @@ -200,6 +233,7 @@ function createAboutWindow() {
preload: ABOUT_WINDOW_PRELOAD_WEBPACK_ENTRY,
}
})
aboutWindow.center();

aboutWindow.loadURL(ABOUT_WINDOW_WEBPACK_ENTRY)

Expand All @@ -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" };
})
}

Expand Down Expand Up @@ -347,7 +380,7 @@ function toggleWindow() {
}

function showDrawWindow() {
mainWindow.show()
showWindowOnActiveScreen()

foregroundMode = true
updateContextMenu() // Need to rerender the context menu
Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion src/renderer/about_page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<body>
<div class="container">
<img src="../web/icon.png" width='80' alt='drawpen'>
<img src="../assets/icon.png" width='80' alt='drawpen'>
<span class="title">DrawPen</span>
<span id="version">Version ...</span>
<span class="description">Source code available on <a href="https://github.com/DmytroVasin/DrawPen" target="_blank">GitHub</a></span>
Expand Down
4 changes: 2 additions & 2 deletions tools/webpack/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
}
]
})
Expand Down

0 comments on commit b691db3

Please sign in to comment.