-
-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathdisplayManager.js
More file actions
89 lines (75 loc) · 2.62 KB
/
displayManager.js
File metadata and controls
89 lines (75 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { screen } from 'electron'
import log from 'electron-log/main.js'
class DisplayManager {
constructor (settings) {
this.settings = settings
}
getDisplayCount () {
return screen.getAllDisplays().length
}
getTargetDisplay (displayID = -1) {
let targetScreen
// If not using all screens, check screen preference
if (!this.settings.get('allScreens')) {
const screenSetting = this.settings.get('screen')
if (screenSetting === 'primary') {
targetScreen = screen.getPrimaryDisplay()
} else if (screenSetting === 'cursor') {
targetScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
} else {
displayID = parseInt(screenSetting)
}
}
// If we already have a target screen from settings, return it
if (targetScreen) {
return targetScreen
}
// Handle displayID-based selection
if (displayID === -1) {
targetScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
} else if (displayID >= this.getDisplayCount() || displayID < 0) {
log.warn(`Stretchly: invalid displayID ${displayID}, falling back to cursor display`)
targetScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
} else {
const screens = screen.getAllDisplays()
targetScreen = screens[displayID]
}
return targetScreen
}
getDisplayBounds (displayID = -1) {
return this.getTargetDisplay(displayID).bounds
}
getDisplayX (displayID = -1, width = 800, fullscreen = false) {
const bounds = this.getDisplayBounds(displayID)
if (fullscreen) {
return Math.floor(bounds.x)
} else {
return Math.floor(bounds.x + ((bounds.width - width) / 2))
}
}
getDisplayY (displayID = -1, height = 600, fullscreen = false) {
const bounds = this.getDisplayBounds(displayID)
if (fullscreen) {
return Math.floor(bounds.y)
} else {
return Math.floor(bounds.y + ((bounds.height - height) / 2))
}
}
getDisplayWidth (displayID = -1) {
const bounds = this.getDisplayBounds(displayID)
return Math.floor(bounds.width)
}
getDisplayHeight (displayID = -1) {
const bounds = this.getDisplayBounds(displayID)
return Math.floor(bounds.height)
}
getWindowPosition (displayID = -1, { width = 800, height = 600, fullscreen = false } = {}) {
return {
x: this.getDisplayX(displayID, width, fullscreen),
y: this.getDisplayY(displayID, height, fullscreen),
width: fullscreen ? this.getDisplayWidth(displayID) : width,
height: fullscreen ? this.getDisplayHeight(displayID) : height
}
}
}
export default DisplayManager