Skip to content

Commit dbb1350

Browse files
authored
Add DisplayManager utility for improved display handling (#1621)
1 parent d639144 commit dbb1350

3 files changed

Lines changed: 376 additions & 148 deletions

File tree

app/main.js

Lines changed: 36 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Command from './utils/commands.js'
2727
import { registerBreakShortcuts } from './utils/breakShortcuts.js'
2828
import defaultSettings from './utils/defaultSettings.js'
2929
import StatusMessages from './utils/statusMessages.js'
30+
import DisplayManager from './utils/displayManager.js'
3031

3132
const __filename = fileURLToPath(import.meta.url)
3233
const __dirname = dirname(__filename)
@@ -59,6 +60,7 @@ let breakIdeas
5960
let breakPlanner
6061
let appIcon = null
6162
let autostartManager = null
63+
let displayManager = null
6264
let processWin = null
6365
let microbreakWins = null
6466
let breakWins = null
@@ -272,6 +274,8 @@ async function initialize (isAppStart = true) {
272274
app
273275
})
274276

277+
displayManager = new DisplayManager(settings, log)
278+
275279
startI18next()
276280
startProcessWin()
277281
createWelcomeWindow()
@@ -378,10 +382,6 @@ function startPowerMonitoring () {
378382
powerMonitor.on('unlock-screen', onResumeOrUnlock)
379383
}
380384

381-
function numberOfDisplays () {
382-
return screen.getAllDisplays().length
383-
}
384-
385385
function closeWindows (windowArray) {
386386
for (const window of windowArray) {
387387
window.hide()
@@ -394,118 +394,6 @@ function closeWindows (windowArray) {
394394
return null
395395
}
396396

397-
function displaysX (displayID = -1, width = 800, fullscreen = false) {
398-
let theScreen
399-
400-
if (!settings.get('allScreens')) {
401-
if (settings.get('screen') === 'primary') {
402-
theScreen = screen.getPrimaryDisplay()
403-
} else if (settings.get('screen') === 'cursor') {
404-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
405-
} else {
406-
displayID = parseInt(settings.get('screen'))
407-
}
408-
}
409-
410-
if (displayID === -1) {
411-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
412-
} else if (displayID >= numberOfDisplays() || displayID < 0) {
413-
log.warn(`Stretchly: invalid displayID ${displayID} to displaysX`)
414-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
415-
} else {
416-
const screens = screen.getAllDisplays()
417-
theScreen = screens[displayID]
418-
}
419-
const bounds = theScreen.bounds
420-
if (fullscreen) {
421-
return Math.ceil(bounds.x)
422-
} else {
423-
return Math.ceil(bounds.x + ((bounds.width - width) / 2))
424-
}
425-
}
426-
427-
function displaysY (displayID = -1, height = 600, fullscreen = false) {
428-
let theScreen
429-
430-
if (!settings.get('allScreens')) {
431-
if (settings.get('screen') === 'primary') {
432-
theScreen = screen.getPrimaryDisplay()
433-
} else if (settings.get('screen') === 'cursor') {
434-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
435-
} else {
436-
displayID = parseInt(settings.get('screen'))
437-
}
438-
}
439-
440-
if (displayID === -1) {
441-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
442-
} else if (displayID >= numberOfDisplays() || displayID < 0) {
443-
log.warn(`Stretchly: invalid displayID ${displayID} to displaysY`)
444-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
445-
} else {
446-
const screens = screen.getAllDisplays()
447-
theScreen = screens[displayID]
448-
}
449-
const bounds = theScreen.bounds
450-
if (fullscreen) {
451-
return Math.ceil(bounds.y)
452-
} else {
453-
return Math.ceil(bounds.y + ((bounds.height - height) / 2))
454-
}
455-
}
456-
457-
function displaysWidth (displayID = -1) {
458-
let theScreen
459-
460-
if (!settings.get('allScreens')) {
461-
if (settings.get('screen') === 'primary') {
462-
theScreen = screen.getPrimaryDisplay()
463-
} else if (settings.get('screen') === 'cursor') {
464-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
465-
} else {
466-
displayID = parseInt(settings.get('screen'))
467-
}
468-
}
469-
470-
if (displayID === -1) {
471-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
472-
} else if (displayID >= numberOfDisplays() || displayID < 0) {
473-
log.warn(`Stretchly: invalid displayID ${displayID} to displaysWidth`)
474-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
475-
} else {
476-
const screens = screen.getAllDisplays()
477-
theScreen = screens[displayID]
478-
}
479-
const bounds = theScreen.bounds
480-
return Math.ceil(bounds.width)
481-
}
482-
483-
function displaysHeight (displayID = -1) {
484-
let theScreen
485-
486-
if (!settings.get('allScreens')) {
487-
if (settings.get('screen') === 'primary') {
488-
theScreen = screen.getPrimaryDisplay()
489-
} else if (settings.get('screen') === 'cursor') {
490-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
491-
} else {
492-
displayID = parseInt(settings.get('screen'))
493-
}
494-
}
495-
496-
if (displayID === -1) {
497-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
498-
} else if (displayID >= numberOfDisplays() || displayID < 0) {
499-
log.warn(`Stretchly: invalid displayID ${displayID} to displaysHeight`)
500-
theScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
501-
} else {
502-
const screens = screen.getAllDisplays()
503-
theScreen = screens[displayID]
504-
}
505-
const bounds = theScreen.bounds
506-
return Math.ceil(bounds.height)
507-
}
508-
509397
function trayIconPath () {
510398
const params = {
511399
paused:
@@ -567,8 +455,8 @@ function createWelcomeWindow (isAppStart = true) {
567455
if (settings.get('isFirstRun') && isAppStart) {
568456
const modalPath = 'file://' + join(__dirname, '/welcome.html')
569457
welcomeWin = new BrowserWindow({
570-
x: displaysX(-1, 1000),
571-
y: displaysY(-1, 750),
458+
x: displayManager.getDisplayX(-1, 1000),
459+
y: displayManager.getDisplayY(-1, 750),
572460
width: 1000,
573461
height: 750,
574462
show: false,
@@ -598,8 +486,8 @@ function createContributorSettingsWindow () {
598486
}
599487
const modalPath = 'file://' + join(__dirname, '/contributor-preferences.html')
600488
contributorPreferencesWin = new BrowserWindow({
601-
x: displaysX(-1, 735),
602-
y: displaysY(),
489+
x: displayManager.getDisplayX(-1, 735),
490+
y: displayManager.getDisplayY(),
603491
width: 735,
604492
show: false,
605493
autoHideMenuBar: true,
@@ -633,8 +521,8 @@ function createSyncPreferencesWindow () {
633521
width: 1000,
634522
height: 700,
635523
icon: windowIconPath(),
636-
x: displaysX(),
637-
y: displaysY(),
524+
x: displayManager.getDisplayX(),
525+
y: displayManager.getDisplayY(),
638526
backgroundColor: 'whitesmoke',
639527
webPreferences: {
640528
preload: join(__dirname, './electron-bridge.mjs'),
@@ -746,10 +634,10 @@ function startMicrobreak () {
746634
calculateBackgroundColor(settings.get('miniBreakColor'))]
747635
})
748636

749-
for (let localDisplayId = 0; localDisplayId < numberOfDisplays(); localDisplayId++) {
637+
for (let localDisplayId = 0; localDisplayId < displayManager.getDisplayCount(); localDisplayId++) {
750638
const windowOptions = {
751-
width: Number.parseInt(displaysWidth(localDisplayId) * settings.get('breakWindowWidth')),
752-
height: Number.parseInt(displaysHeight(localDisplayId) * settings.get('breakWindowHeight')),
639+
width: Math.floor(displayManager.getDisplayWidth(localDisplayId) * settings.get('breakWindowWidth')),
640+
height: Math.floor(displayManager.getDisplayHeight(localDisplayId) * settings.get('breakWindowHeight')),
753641
autoHideMenuBar: true,
754642
icon: windowIconPath(),
755643
resizable: false,
@@ -773,13 +661,13 @@ function startMicrobreak () {
773661
}
774662

775663
if (settings.get('fullscreen') && process.platform !== 'darwin') {
776-
windowOptions.width = displaysWidth(localDisplayId)
777-
windowOptions.height = displaysHeight(localDisplayId)
778-
windowOptions.x = displaysX(localDisplayId, 0, true)
779-
windowOptions.y = displaysY(localDisplayId, 0, true)
664+
windowOptions.width = displayManager.getDisplayWidth(localDisplayId)
665+
windowOptions.height = displayManager.getDisplayHeight(localDisplayId)
666+
windowOptions.x = displayManager.getDisplayX(localDisplayId, 0, true)
667+
windowOptions.y = displayManager.getDisplayY(localDisplayId, 0, true)
780668
} else if (!(settings.get('fullscreen') && process.platform === 'win32')) {
781-
windowOptions.x = displaysX(localDisplayId, windowOptions.width, false)
782-
windowOptions.y = displaysY(localDisplayId, windowOptions.height, false)
669+
windowOptions.x = displayManager.getDisplayX(localDisplayId, windowOptions.width, false)
670+
windowOptions.y = displayManager.getDisplayY(localDisplayId, windowOptions.height, false)
783671
}
784672

785673
let microbreakWinLocal = new BrowserWindow(windowOptions)
@@ -798,7 +686,7 @@ function startMicrobreak () {
798686
microbreakWinLocal.showInactive()
799687
}
800688

801-
log.info(`Stretchly: showing window ${localDisplayId + 1} of ${numberOfDisplays()}`)
689+
log.info(`Stretchly: showing window ${localDisplayId + 1} of ${displayManager.getDisplayCount()}`)
802690
if (process.platform === 'darwin') {
803691
if (showBreaksAsRegularWindows) {
804692
microbreakWinLocal.setFullScreen(settings.get('fullscreen'))
@@ -838,7 +726,7 @@ function startMicrobreak () {
838726
microbreakWins.push(microbreakWinLocal)
839727

840728
if (!settings.get('allScreens')) {
841-
if (numberOfDisplays() > 1) {
729+
if (displayManager.getDisplayCount() > 1) {
842730
log.info('Stretchly: not showing on more Monitors as it is disabled.')
843731
}
844732
break
@@ -895,10 +783,10 @@ function startBreak () {
895783
calculateBackgroundColor(settings.get('mainColor'))]
896784
})
897785

898-
for (let localDisplayId = 0; localDisplayId < numberOfDisplays(); localDisplayId++) {
786+
for (let localDisplayId = 0; localDisplayId < displayManager.getDisplayCount(); localDisplayId++) {
899787
const windowOptions = {
900-
width: Number.parseInt(displaysWidth(localDisplayId) * settings.get('breakWindowWidth')),
901-
height: Number.parseInt(displaysHeight(localDisplayId) * settings.get('breakWindowHeight')),
788+
width: Math.floor(displayManager.getDisplayWidth(localDisplayId) * settings.get('breakWindowWidth')),
789+
height: Math.floor(displayManager.getDisplayHeight(localDisplayId) * settings.get('breakWindowHeight')),
902790
autoHideMenuBar: true,
903791
icon: windowIconPath(),
904792
resizable: false,
@@ -922,13 +810,13 @@ function startBreak () {
922810
}
923811

924812
if (settings.get('fullscreen') && process.platform !== 'darwin') {
925-
windowOptions.width = displaysWidth(localDisplayId)
926-
windowOptions.height = displaysHeight(localDisplayId)
927-
windowOptions.x = displaysX(localDisplayId, 0, true)
928-
windowOptions.y = displaysY(localDisplayId, 0, true)
813+
windowOptions.width = displayManager.getDisplayWidth(localDisplayId)
814+
windowOptions.height = displayManager.getDisplayHeight(localDisplayId)
815+
windowOptions.x = displayManager.getDisplayX(localDisplayId, 0, true)
816+
windowOptions.y = displayManager.getDisplayY(localDisplayId, 0, true)
929817
} else if (!(settings.get('fullscreen') && process.platform === 'win32')) {
930-
windowOptions.x = displaysX(localDisplayId, windowOptions.width, false)
931-
windowOptions.y = displaysY(localDisplayId, windowOptions.height, false)
818+
windowOptions.x = displayManager.getDisplayX(localDisplayId, windowOptions.width, false)
819+
windowOptions.y = displayManager.getDisplayY(localDisplayId, windowOptions.height, false)
932820
}
933821

934822
let breakWinLocal = new BrowserWindow(windowOptions)
@@ -947,7 +835,7 @@ function startBreak () {
947835
breakWinLocal.showInactive()
948836
}
949837

950-
log.info(`Stretchly: showing window ${localDisplayId + 1} of ${numberOfDisplays()}`)
838+
log.info(`Stretchly: showing window ${localDisplayId + 1} of ${displayManager.getDisplayCount()}`)
951839
if (process.platform === 'darwin') {
952840
if (showBreaksAsRegularWindows) {
953841
breakWinLocal.setFullScreen(settings.get('fullscreen'))
@@ -988,7 +876,7 @@ function startBreak () {
988876
breakWins.push(breakWinLocal)
989877

990878
if (!settings.get('allScreens')) {
991-
if (numberOfDisplays() > 1) {
879+
if (displayManager.getDisplayCount() > 1) {
992880
log.info('Stretchly: not showing on more Monitors as it is disabled.')
993881
}
994882
break
@@ -1175,8 +1063,8 @@ function createPreferencesWindow () {
11751063
width: 600,
11761064
height: 530,
11771065
maxHeight: Math.round(maxHeight),
1178-
x: displaysX(-1, 600),
1179-
y: displaysY(-1, 530),
1066+
x: displayManager.getDisplayX(-1, 600),
1067+
y: displayManager.getDisplayY(-1, 530),
11801068
backgroundColor: '#EDEDED',
11811069
webPreferences: {
11821070
preload: join(__dirname, './preferences-preload.mjs'),
@@ -1563,8 +1451,8 @@ ipcMain.on('open-contributor-auth', function (event, provider) {
15631451
width: 1000,
15641452
height: 700,
15651453
icon: windowIconPath(),
1566-
x: displaysX(),
1567-
y: displaysY(),
1454+
x: displayManager.getDisplayX(),
1455+
y: displayManager.getDisplayY(),
15681456
backgroundColor: 'whitesmoke',
15691457
webPreferences: {
15701458
preload: join(__dirname, './electron-bridge.mjs'),

0 commit comments

Comments
 (0)