-
Notifications
You must be signed in to change notification settings - Fork 64
/
Window.swift
416 lines (355 loc) · 15.3 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import AXSwift
import Cocoa
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.frame.value)) }
.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
}
/// 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 frame of the window.
///
/// The origin of the frame is the bottom-left corner of the window in screen coordinates.
public var frame: WriteableProperty<OfType<CGRect>> { return delegate.frame }
/// The size of the window in screen coordinates.
public var size: WriteableProperty<OfType<CGSize>> { return delegate.size }
/// The window title.
public var title: Property<OfDefaultedType<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 Window: CustomStringConvertible {
public var description: String {
"\"\(title.value.truncate(length: 30))\""
}
}
extension String {
func truncate(length: Int, trailing: String = "…") -> String {
if self.count > length {
return String(self.prefix(length)) + trailing
} else {
return self
}
}
}
extension String: Defaultable {
public static func defaultValue() -> String { "" }
}
protocol WindowDelegate: AnyObject {
var isValid: Bool { get }
// Optional because a WindowDelegate shouldn't hold a strong reference to its parent
// ApplicationDelegate.
var appDelegate: ApplicationDelegate? { get }
var frame: WriteableProperty<OfType<CGRect>>! { get }
var size: SizeProperty! { get }
var title: Property<OfDefaultedType<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 watchedAxProperties: [AXSwift.AXNotification: [PropertyType]]!
weak var appDelegate: ApplicationDelegate?
var frame: WriteableProperty<OfType<CGRect>>!
var size: SizeProperty!
var title: Property<OfDefaultedType<String>>!
var isMinimized: WriteableProperty<OfType<Bool>>!
var isFullscreen: WriteableProperty<OfType<Bool>>!
private init(_ appDelegate: ApplicationDelegate,
_ notifier: EventNotifier?,
_ axElement: UIElement,
_ observer: Observer,
_ systemScreens: SystemScreenDelegate) throws {
self.appDelegate = appDelegate
self.notifier = notifier
self.axElement = axElement
// Create a promise for the attribute dictionary we'll get from getMultipleAttributes.
let (initPromise, seal) = Promise<[AXSwift.Attribute: Any]>.pending()
// Initialize all properties.
let frameDelegate = FramePropertyDelegate(axElement, initPromise, systemScreens)
frame = WriteableProperty(
frameDelegate,
withEvent: WindowFrameChangedEvent.self,
receivingObject: Window.self,
notifier: self)
size = SizeProperty(
AXPropertyDelegate(axElement, .size, initPromise),
notifier: self,
frame: frame)
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)
let axProperties: [PropertyType] = [
size,
title,
isMinimized,
isFullscreen
]
let allProperties: [PropertyType] = axProperties + [
frame
]
// Map notifications on this element to the corresponding property.
// Note that `size` implicitly updates every time `frame` updates, so it is not listed here.
watchedAxProperties = [
.moved: [frame],
.resized: [frame, 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} + [
.frame,
.subrole
]
fetchAttributes(
attributes, forElement: axElement, after: watched, seal: seal
)
// 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.done { attributeValues 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(allProperties).asVoid(), subroleChecked)
}
private func watchWindowElement(_ element: UIElement,
observer: Observer,
notifications: [AXNotification]) -> Promise<Void> {
return Promise<Void>.value(()).done(on: .global()) {
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(()).done(on: .global()) {
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,
systemScreens: SystemScreenDelegate
) -> Promise<OSXWindowDelegate> {
return firstly { () -> Promise<OSXWindowDelegate> in // capture thrown errors in promise
let window = try OSXWindowDelegate(
appDelegate, notifier, axElement, observer, systemScreens)
return window.initialized.map { 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
}
}
// MARK: PropertyDelegates
/// PropertyAdapter that inverts the y-axis of the point value.
///
/// This is to convert between AXPosition coordinates, which have the origin at
/// the top-left, and Cocoa coordinates, which have it at the bottom-left.
private final class FramePropertyDelegate<UIElement: UIElementType>: PropertyDelegate {
typealias T = CGRect
let frame: AXPropertyDelegate<CGRect, UIElement>
let pos: AXPropertyDelegate<CGPoint, UIElement>
let size: AXPropertyDelegate<CGSize, UIElement>
let systemScreens: SystemScreenDelegate
typealias InitDict = [AXSwift.Attribute: Any]
init(_ element: UIElement, _ initPromise: Promise<InitDict>, _ screens: SystemScreenDelegate) {
frame = AXPropertyDelegate<CGRect, UIElement>(element, .frame, initPromise)
pos = AXPropertyDelegate<CGPoint, UIElement>(element, .position, initPromise)
size = AXPropertyDelegate<CGSize, UIElement>(element, .size, initPromise)
systemScreens = screens
}
func readValue() throws -> T? {
guard let rect = try frame.readValue() else { return nil }
return invert(rect)
}
func writeValue(_ newValue: T) throws {
let rect = invert(newValue)
try pos.writeValue(rect.origin)
try size.writeValue(rect.size)
}
func initialize() -> Promise<T?> {
return frame.initialize().map { rect in
rect.map{ self.invert($0) }
}
}
private func invert(_ rect: CGRect) -> CGRect {
let inverted = CGPoint(x: rect.minX, y: systemScreens.maxY - rect.maxY)
return CGRect(origin: inverted, size: rect.size)
}
}
// MARK: SizeProperty
/// Custom Property class for the `size` property.
///
/// Does not use the backing store at all; delegates to `frame` instead for all reads. This ensures
/// that `frame` and `size` are always consistent with each other.
///
/// The purpose of this property is to support atomic writes to the `size` attribute of a window.
final class SizeProperty: WriteableProperty<OfType<CGSize>> {
let frame: WriteableProperty<OfType<CGRect>>
init<Impl: PropertyDelegate, Notifier: PropertyNotifier>(
_ delegate: Impl, notifier: Notifier, frame: WriteableProperty<OfType<CGRect>>
) where Impl.T == NonOptionalType {
self.frame = frame
super.init(delegate, notifier: notifier)
}
override func initialize<Impl: PropertyDelegate>(_ delegate: Impl) -> Promise<Void> {
return frame.initialized
}
override func getValue() -> PropertyType {
return frame.value.size
}
@discardableResult
public override func refresh() -> Promise<PropertyType> {
return frame.refresh().map { rect in
return rect.size
}
}
public override func set(_ newValue: NonOptionalType) -> Promise<PropertyType> {
// Because we don't have a WindowSizeChangedEvent, we don't have to worry about our own
// events. However, the frame does need to know that we are mutating it from within
// Swindler, so events are correctly marked as internal.
return frame.mutateWith() {
let orig = self.frame.value
try self.delegate_.writeValue(newValue)
return CGRect(origin: orig.origin, size: newValue)
}.map { rect in
return rect.size
}
}
}