fix: exotic scenario where the os reports no main screen (closes #330)

This commit is contained in:
Louis Pontoise authored and lwouis committed 2020-05-11 04:50:26 +09:00
1 parent d843781d7a
commit f83ef40c42
2 files changed
+19 -11

No files matched your search

+6 -2
View File
@@ -21,10 +21,14 @@ class Screen {
window.setFrameOrigin(NSPoint(x: x, y: y))
}
static func uuid(_ screen: NSScreen) -> ScreenUuid {
static func uuid(_ screen: NSScreen) -> ScreenUuid? {
let screenNumber = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! UInt32
let screenUuid = CGDisplayCreateUUIDFromDisplayID(screenNumber).takeRetainedValue()
return CFUUIDCreateString(nil, screenUuid)!
if screenUuid != nil,
let uuid = CFUUIDCreateString(nil, screenUuid) {
return uuid
}
return nil
}
}
+13 -9
View File
@@ -18,8 +18,12 @@ class Spaces {
}
static func refreshCurrentSpaceId() {
currentSpaceId = CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, Screen.uuid(NSScreen.main!))
debugPrint("currentSpaceId", currentSpaceId)
// it seems that in some rare scenarios, some of these values are nil; we wrap to avoid crashing
if let mainScreen = NSScreen.main,
let uuid = Screen.uuid(mainScreen) {
currentSpaceId = CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, uuid)
debugPrint("currentSpaceId", currentSpaceId)
}
}
static func initialDiscovery() {
@@ -38,13 +42,13 @@ class Spaces {
static func allIdsAndIndexes() -> [(CGSSpaceID, SpaceIndex)] {
return (CGSCopyManagedDisplaySpaces(cgsMainConnectionId) as! [NSDictionary])
.map { (display: NSDictionary) -> [NSDictionary] in
display["Spaces"] as! [NSDictionary]
}
.joined().enumerated()
.map { (space: (offset: Int, element: NSDictionary)) -> (CGSSpaceID, SpaceIndex) in
(space.element["id64"]! as! CGSSpaceID, space.offset + 1)
}
.map { (display: NSDictionary) -> [NSDictionary] in
display["Spaces"] as! [NSDictionary]
}
.joined().enumerated()
.map { (space: (offset: Int, element: NSDictionary)) -> (CGSSpaceID, SpaceIndex) in
(space.element["id64"]! as! CGSSpaceID, space.offset + 1)
}
}
static func otherSpaces() -> [CGSSpaceID] {