-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVerticalMonitorLayout.swift
executable file
·49 lines (43 loc) · 1.99 KB
/
VerticalMonitorLayout.swift
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
#!/usr/bin/env swift
import Cocoa
import Foundation
configure { config in
let mainDisplay = CGMainDisplayID()
var macBookDisplay: CGDirectDisplayID
var externalDisplay: CGDirectDisplayID
if CGDisplayIsBuiltin(mainDisplay) != 0 {
macBookDisplay = mainDisplay
guard let otherDisplay = NSScreen.onlineDisplayIDs.first(where: { $0 != macBookDisplay }) else {
print("No external display detected")
return false
}
externalDisplay = otherDisplay
} else {
externalDisplay = mainDisplay
guard let otherDisplay = NSScreen.onlineDisplayIDs.first(where: { CGDisplayIsBuiltin($0) != 0 }) else {
print("No internal display detected")
return false
}
macBookDisplay = otherDisplay
}
let macBookBounds = CGDisplayBounds(macBookDisplay)
let monitorBounds = CGDisplayBounds(externalDisplay)
print(
"Main Display: x=\(macBookBounds.origin.x) y=\(macBookBounds.origin.y) width=\(macBookBounds.width) height=\(macBookBounds.height)"
)
print(
"External Display: x=\(monitorBounds.origin.x) y=\(monitorBounds.origin.y) width=\(monitorBounds.width) height=\(monitorBounds.height)"
)
if macBookDisplay == mainDisplay {
let monitorX = (max(monitorBounds.width, macBookBounds.width) - min(monitorBounds.width, macBookBounds.width)) / -2
let monitorY = -monitorBounds.height
print("\nNew external display coordinates: x=\(monitorX) y=\(monitorY)")
CGConfigureDisplayOrigin(config, externalDisplay, Int32(monitorX.rounded()), Int32(monitorY.rounded()))
} else {
let monitorX = (max(macBookBounds.width, monitorBounds.width) - min(macBookBounds.width, monitorBounds.width)) / 2
let monitorY = monitorBounds.height
print("\nNew internal display coordinates: x=\(monitorX) y=\(monitorY)")
CGConfigureDisplayOrigin(config, macBookDisplay, Int32(monitorX.rounded()), Int32(monitorY.rounded()))
}
return true
}