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)) 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 screenNumber = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! UInt32
let screenUuid = CGDisplayCreateUUIDFromDisplayID(screenNumber).takeRetainedValue() 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() { static func refreshCurrentSpaceId() {
currentSpaceId = CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, Screen.uuid(NSScreen.main!)) // it seems that in some rare scenarios, some of these values are nil; we wrap to avoid crashing
debugPrint("currentSpaceId", currentSpaceId) if let mainScreen = NSScreen.main,
let uuid = Screen.uuid(mainScreen) {
currentSpaceId = CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, uuid)
debugPrint("currentSpaceId", currentSpaceId)
}
} }
static func initialDiscovery() { static func initialDiscovery() {
@@ -38,13 +42,13 @@ class Spaces {
static func allIdsAndIndexes() -> [(CGSSpaceID, SpaceIndex)] { static func allIdsAndIndexes() -> [(CGSSpaceID, SpaceIndex)] {
return (CGSCopyManagedDisplaySpaces(cgsMainConnectionId) as! [NSDictionary]) return (CGSCopyManagedDisplaySpaces(cgsMainConnectionId) as! [NSDictionary])
.map { (display: NSDictionary) -> [NSDictionary] in .map { (display: NSDictionary) -> [NSDictionary] in
display["Spaces"] as! [NSDictionary] display["Spaces"] as! [NSDictionary]
} }
.joined().enumerated() .joined().enumerated()
.map { (space: (offset: Int, element: NSDictionary)) -> (CGSSpaceID, SpaceIndex) in .map { (space: (offset: Int, element: NSDictionary)) -> (CGSSpaceID, SpaceIndex) in
(space.element["id64"]! as! CGSSpaceID, space.offset + 1) (space.element["id64"]! as! CGSSpaceID, space.offset + 1)
} }
} }
static func otherSpaces() -> [CGSSpaceID] { static func otherSpaces() -> [CGSSpaceID] {