/
Window.swift
308 lines (265 loc) · 11.7 KB
/
Window.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import AXSwift
import PromiseKit
// MARK: - Window
/// A window.
public final class Window {
internal let delegate: WindowDelegate
// A Window holds a strong reference to the Application and therefore the ApplicationDelegate.
// It should not be held internally by delegates, or it would create a reference cycle.
fileprivate var application_: Application!
internal init(delegate: WindowDelegate, application: Application) {
self.delegate = delegate
application_ = application
}
/// This initializer fails only if the ApplicationDelegate is no longer reachable (because the
/// application terminated, which means this window no longer exists), or the StateDelegate has
/// been destroyed.
internal convenience init?(delegate: WindowDelegate) {
guard let appDelegate = delegate.appDelegate else {
// The application terminated.
log.debug("Window for delegate \(delegate) failed to initialize because of unreachable "
+ "ApplicationDelegate")
return nil
}
guard let app = Application(delegate: appDelegate) else {
log.debug("Window for delegate \(delegate) failed to initialize because Application "
+ "failed to initialize")
return nil
}
self.init(delegate: delegate, application: app)
}
/// The application the window belongs to.
public var application: Application { return application_ }
/// The screen that (most of) the window is on. `nil` if the window is completely off-screen.
public var screen: Screen? {
let screenIntersectSizes =
application.swindlerState.screens.lazy
.map { screen in (screen, screen.frame.intersection(self.rect)) }
.filter { _, intersect in !intersect.isNull }
.map { screen, intersect in (screen, intersect.size.width * intersect.size.height) }
let bestScreen = screenIntersectSizes.max { lhs, rhs in lhs.1 < rhs.1 }?.0
return bestScreen
}
fileprivate var rect: CGRect { return CGRect(origin: position.value, size: size.value) }
/// Whether or not the window referred to by this type remains valid. Windows usually become
/// invalid because they are destroyed (in which case a WindowDestroyedEvent will be emitted).
/// They can also become invalid because they do not have all the required properties, or
/// because the application that owns them is otherwise not giving a well-behaved response.
public var isValid: Bool { return delegate.isValid }
/// The position of the top-left corner of the window in screen coordinates.
public var position: WriteableProperty<OfType<CGPoint>> { return delegate.position }
/// The size of the window in screen coordinates.
public var size: WriteableProperty<OfType<CGSize>> { return delegate.size }
/// The window title.
public var title: Property<OfType<String>> { return delegate.title }
/// Whether the window is minimized.
public var isMinimized: WriteableProperty<OfType<Bool>> { return delegate.isMinimized }
/// Whether the window is fullscreen or not.
public var isFullscreen: WriteableProperty<OfType<Bool>> { return delegate.isFullscreen }
}
public func ==(lhs: Window, rhs: Window) -> Bool {
return lhs.delegate.equalTo(rhs.delegate)
}
extension Window: Equatable {}
extension Window: CustomDebugStringConvertible {
public var debugDescription: String {
return "Window(\"\(title.value.truncate(length: 30))\")"
//+ "app=\(application.bundleIdentifier ?? "<unknown>"))"
}
}
extension String {
func truncate(length: Int, trailing: String = "…") -> String {
if self.count > length {
return String(self.prefix(length)) + trailing
} else {
return self
}
}
}
protocol WindowDelegate: class {
var isValid: Bool { get }
// Optional because a WindowDelegate shouldn't hold a strong reference to its parent
// ApplicationDelegate.
var appDelegate: ApplicationDelegate? { get }
var position: WriteableProperty<OfType<CGPoint>>! { get }
var size: WriteableProperty<OfType<CGSize>>! { get }
var title: Property<OfType<String>>! { get }
var isMinimized: WriteableProperty<OfType<Bool>>! { get }
var isFullscreen: WriteableProperty<OfType<Bool>>! { get }
func equalTo(_ other: WindowDelegate) -> Bool
}
// MARK: - OSXWindowDelegate
/// Implements WindowDelegate using the AXUIElement API.
final class OSXWindowDelegate<
UIElement, ApplicationElement: ApplicationElementType, Observer: ObserverType
>: WindowDelegate
where Observer.UIElement == UIElement, ApplicationElement.UIElement == UIElement {
typealias Object = Window
fileprivate weak var notifier: EventNotifier?
fileprivate var initialized: Promise<Void>!
let axElement: UIElement
fileprivate(set) var isValid: Bool = true
fileprivate var axProperties: [PropertyType]!
fileprivate var watchedAxProperties: [AXSwift.AXNotification: [PropertyType]]!
weak var appDelegate: ApplicationDelegate?
var position: WriteableProperty<OfType<CGPoint>>!
var size: WriteableProperty<OfType<CGSize>>!
var title: Property<OfType<String>>!
var isMinimized: WriteableProperty<OfType<Bool>>!
var isFullscreen: WriteableProperty<OfType<Bool>>!
private init(appDelegate: ApplicationDelegate,
notifier: EventNotifier?,
axElement: UIElement,
observer: Observer) throws {
self.appDelegate = appDelegate
self.notifier = notifier
self.axElement = axElement
// Create a promise for the attribute dictionary we'll get from getMultipleAttributes.
let (initPromise, fulfill, reject) = Promise<[AXSwift.Attribute: Any]>.pending()
// Initialize all properties.
position = WriteableProperty(
AXPropertyDelegate(axElement, .position, initPromise),
withEvent: WindowPosChangedEvent.self,
receivingObject: Window.self,
notifier: self)
size = WriteableProperty(
AXPropertyDelegate(axElement, .size, initPromise),
withEvent: WindowSizeChangedEvent.self,
receivingObject: Window.self,
notifier: self)
title = Property(
AXPropertyDelegate(axElement, .title, initPromise),
withEvent: WindowTitleChangedEvent.self,
receivingObject: Window.self,
notifier: self)
isMinimized = WriteableProperty(
AXPropertyDelegate(axElement, .minimized, initPromise),
withEvent: WindowMinimizedChangedEvent.self,
receivingObject: Window.self,
notifier: self)
isFullscreen = WriteableProperty(
AXPropertyDelegate(axElement, .fullScreen, initPromise),
notifier: self)
axProperties = [
position,
size,
title,
isMinimized,
isFullscreen
]
// Map notifications on this element to the corresponding property.
watchedAxProperties = [
.moved: [position],
.resized: [position, size, isFullscreen],
.titleChanged: [title],
.windowMiniaturized: [isMinimized],
.windowDeminiaturized: [isMinimized]
]
// Start watching for notifications.
let notifications = watchedAxProperties.keys + [
.uiElementDestroyed
]
let watched = watchWindowElement(axElement,
observer: observer,
notifications: notifications)
// Fetch attribute values.
let attributes = axProperties.map {($0.delegate as! AXPropertyDelegateType).attribute} + [
.subrole
]
fetchAttributes(
attributes, forElement: axElement, after: watched, fulfill: fulfill, reject: reject
)
// Ignore windows with the "AXUnknown" role. This (undocumented) role shows up in several
// places, including Chrome tooltips and OS X fullscreen transitions.
let subroleChecked = initPromise.then { attributeValues -> Void in
if attributeValues[.subrole] as! String? == "AXUnknown" {
log.trace("Window \(axElement) has subrole AXUnknown, unwatching")
self.unwatchWindowElement(
axElement, observer: observer, notifications: notifications
).catch { error in
log.warn("Error while unwatching ignored window \(axElement): \(error)")
}
throw OSXDriverError.windowIgnored(element: axElement)
}
}
initialized = when(fulfilled: initializeProperties(axProperties).asVoid(), subroleChecked)
}
private func watchWindowElement(_ element: UIElement,
observer: Observer,
notifications: [AXNotification]) -> Promise<Void> {
return Promise<Void>(value: ()).then(on: .global()) { () -> Void in
for notification in notifications {
try traceRequest(self.axElement, "addNotification", notification) {
try observer.addNotification(notification, forElement: self.axElement)
}
}
}
}
private func unwatchWindowElement(_ element: UIElement,
observer: Observer,
notifications: [AXNotification]) -> Promise<Void> {
return Promise<Void>(value: ()).then(on: .global()) { () -> Void in
for notification in notifications {
try traceRequest(self.axElement, "removeNotification", notification) {
try observer.removeNotification(notification, forElement: self.axElement)
}
}
}
}
func equalTo(_ rhs: WindowDelegate) -> Bool {
if let other = rhs as? OSXWindowDelegate {
return axElement == other.axElement
} else {
return false
}
}
}
/// Interface used by OSXApplicationDelegate.
extension OSXWindowDelegate {
/// Initializes the window, and returns it in a Promise once it's ready.
static func initialize(
appDelegate: ApplicationDelegate,
notifier: EventNotifier?,
axElement: UIElement,
observer: Observer
) -> Promise<OSXWindowDelegate> {
return firstly { // capture thrown errors in promise
let window = try OSXWindowDelegate(
appDelegate: appDelegate, notifier: notifier, axElement: axElement,
observer: observer
)
return window.initialized.then { window }
}
}
func handleEvent(_ event: AXSwift.AXNotification, observer: Observer) {
switch event {
case .uiElementDestroyed:
isValid = false
default:
if let properties = watchedAxProperties[event] {
properties.forEach { $0.issueRefresh() }
} else {
log.debug("Unknown event on \(self): \(event)")
}
}
}
}
extension OSXWindowDelegate: PropertyNotifier {
func notify<Event: PropertyEventType>(
_ event: Event.Type,
external: Bool,
oldValue: Event.PropertyType,
newValue: Event.PropertyType
) where Event.Object == Window {
guard let window = Window(delegate: self) else {
// Application terminated already; shouldn't send events.
return
}
notifier?.notify(
Event(external: external, object: window, oldValue: oldValue, newValue: newValue)
)
}
func notifyInvalid() {
isValid = false
}
}