-
Notifications
You must be signed in to change notification settings - Fork 480
/
CGInfo.swift
198 lines (155 loc) · 5.88 KB
/
CGInfo.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// CGInfo.swift
// Amethyst
//
// Created by Ian Ynda-Hummel on 3/29/19.
// Copyright © 2019 Ian Ynda-Hummel. All rights reserved.
//
import Foundation
import Silica
import SwiftyJSON
/// Windows info as taken from the underlying system.
struct CGWindowsInfo<Window: WindowType> {
/// An array of dictionaries of window information
let descriptions: [[String: AnyObject]]
/**
- Parameters:
- options: Any options for getting info.
- windowID: ID of window to find windows relative to. 0 gets all windows.
*/
init?(options: CGWindowListOption, windowID: CGWindowID) {
guard let cfWindowDescriptions = CGWindowListCopyWindowInfo(options, windowID) else {
return nil
}
guard let windowDescriptions = cfWindowDescriptions as? [[String: AnyObject]] else {
return nil
}
self.descriptions = windowDescriptions
}
/**
- Returns:
The set of windows that are currently active.
*/
func activeIDs() -> Set<CGWindowID> {
var ids: Set<CGWindowID> = Set()
for windowDescription in descriptions {
guard let windowID = windowDescription[kCGWindowNumber as String] as? NSNumber else {
continue
}
ids.insert(CGWindowID(windowID.uint64Value))
}
return ids
}
static func windowIDsArray(_ window: Window) -> NSArray {
return [NSNumber(value: window.cgID() as UInt32)] as NSArray
}
static func windowSpace(_ window: Window) -> Int? {
let windowIDsArray = CGWindowsInfo.windowIDsArray(window)
guard let spaces = CGSCopySpacesForWindows(CGSMainConnectionID(), kCGSAllSpacesMask, windowIDsArray)?.takeRetainedValue() else {
return nil
}
return (spaces as NSArray as? [NSNumber])?.first?.intValue
}
}
struct CGScreensInfo<Window: WindowType> {
typealias Screen = Window.Screen
let descriptions: [JSON]
init?() {
guard let descriptions = Screen.screenDescriptions() else {
return nil
}
self.descriptions = descriptions
}
func space(at index: Int) -> Space {
return CGSpacesInfo<Window>.space(fromScreenDescription: descriptions[index])
}
}
struct CGSpacesInfo<Window: WindowType> {
typealias Screen = Window.Screen
static func spacesForAllScreens(includeOnlyUserSpaces: Bool = false) -> [Space]? {
guard let screenDescriptions = Screen.screenDescriptions() else {
return nil
}
guard !screenDescriptions.isEmpty else {
return nil
}
let spaces = screenDescriptions.map { screenDescription -> [Space] in
return allSpaces(fromScreenDescription: screenDescription) ?? []
}.reduce([], {acc, spaces in acc + spaces})
if includeOnlyUserSpaces {
return spaces.filter { $0.type == CGSSpaceTypeUser }
}
return spaces
}
static func spacesForScreen(_ screen: Screen, includeOnlyUserSpaces: Bool = false) -> [Space]? {
guard let screenDescriptions = Screen.screenDescriptions() else {
return nil
}
guard !screenDescriptions.isEmpty else {
return nil
}
let screenID = screen.screenID()
let spaces: [Space]?
if Screen.screensHaveSeparateSpaces {
spaces = screenDescriptions
.first { $0["Display Identifier"].string == screenID }
.flatMap { screenDescription -> [Space]? in
return allSpaces(fromScreenDescription: screenDescription)
}
} else {
spaces = allSpaces(fromScreenDescription: screenDescriptions[0])
}
if includeOnlyUserSpaces {
return spaces?.filter { $0.type == CGSSpaceTypeUser }
}
return spaces
}
static func spacesForFocusedScreen() -> [Space]? {
guard let focusedWindow = Window.currentlyFocused(), let screen = focusedWindow.screen() else {
return nil
}
return spacesForScreen(screen)
}
static func currentSpaceForScreen(_ screen: Screen) -> Space? {
guard let screenDescriptions = Screen.screenDescriptions(), let screenID = screen.screenID() else {
return nil
}
guard screenDescriptions.count > 0 else {
return nil
}
if Screen.screensHaveSeparateSpaces {
for screenDescription in screenDescriptions {
guard screenDescription["Display Identifier"].string == screenID else {
continue
}
return space(fromScreenDescription: screenDescription)
}
} else {
return space(fromScreenDescription: screenDescriptions[0])
}
return nil
}
static func currentFocusedSpace() -> Space? {
guard let focusedWindow = Window.currentlyFocused(), let screen = focusedWindow.screen() else {
return nil
}
return currentSpaceForScreen(screen)
}
static func screenForSpace(space: Space) -> Screen? {
return Screen.availableScreens.first { spacesForScreen($0)?.contains { $0.id == space.id } ?? false }
}
static func space(fromScreenDescription screenDictionary: JSON) -> Space {
return space(fromSpaceDescription: screenDictionary["Current Space"])
}
static func space(fromSpaceDescription spaceDictionary: JSON) -> Space {
let id: CGSSpaceID = spaceDictionary["ManagedSpaceID"].intValue
let type = CGSSpaceType(rawValue: spaceDictionary["type"].uInt32Value)
let uuid = spaceDictionary["uuid"].stringValue
return Space(id: id, type: type, uuid: uuid)
}
static func allSpaces(fromScreenDescription screenDictionary: JSON) -> [Space]? {
return screenDictionary["Spaces"].array?.map {
space(fromSpaceDescription: $0)
}
}
}