Skip to content

Commit

Permalink
fix: occasional wrong window order after focusing a window (closes #484)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Aug 3, 2020
1 parent 34c1754 commit d6b1fb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Application: NSObject {
var n = [
kAXApplicationActivatedNotification,
kAXMainWindowChangedNotification,
kAXFocusedWindowChangedNotification,
kAXWindowCreatedNotification,
kAXApplicationHiddenNotification,
kAXApplicationShownNotification,
Expand Down
23 changes: 13 additions & 10 deletions src/logic/events/AccessibilityEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func retryAxCallUntilTimeout(_ group: DispatchGroup? = nil, _ fn: @escaping () t
}

func retryAxCallUntilTimeout_(_ group: DispatchGroup?, _ fn: @escaping () throws -> Void, _ startTime: DispatchTime = DispatchTime.now()) {
do {
try fn()
group?.leave()
} catch {
let timePassedInSeconds = Double(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds) / 1_000_000_000
if timePassedInSeconds < Double(AXUIElement.globalTimeoutInSeconds) {
BackgroundWork.axCallsQueue.asyncAfter(deadline: .now() + .milliseconds(10)) {
do {
try fn()
group?.leave()
} catch {
let timePassedInSeconds = Double(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds) / 1_000_000_000
if timePassedInSeconds < Double(AXUIElement.globalTimeoutInSeconds) {
BackgroundWork.axCallsQueue.asyncAfter(deadline: .now() + .milliseconds(10)) {
retryAxCallUntilTimeout_(group, fn, startTime)
}
}
Expand All @@ -36,7 +36,8 @@ func handleEvent(_ type: String, _ element: AXUIElement) throws {
case kAXApplicationHiddenNotification,
kAXApplicationShownNotification: try applicationHiddenOrShown(element, pid, type)
case kAXWindowCreatedNotification: try windowCreated(element, pid)
case kAXMainWindowChangedNotification: try focusedWindowChanged(element, pid)
case kAXMainWindowChangedNotification,
kAXFocusedWindowChangedNotification: try focusedWindowChanged(element, pid)
case kAXUIElementDestroyedNotification: try windowDestroyed(element)
case kAXWindowMiniaturizedNotification,
kAXWindowDeminiaturizedNotification: try windowMiniaturizedOrDeminiaturized(element, type)
Expand Down Expand Up @@ -130,8 +131,10 @@ private func focusedWindowChanged(_ element: AXUIElement, _ pid: pid_t) throws {
let position = try element.position()
DispatchQueue.main.async {
if let existingIndex = Windows.list.firstIndexThatMatches(element, wid) {
Windows.list.insertAndScaleRecycledPool(Windows.list.remove(at: existingIndex), at: 0)
App.app.refreshOpenUi([Windows.list[0], Windows.list[existingIndex]])
if existingIndex != 0 {
Windows.list.insertAndScaleRecycledPool(Windows.list.remove(at: existingIndex), at: 0)
App.app.refreshOpenUi([Windows.list[0], Windows.list[existingIndex]])
}
} else if let runningApp = NSRunningApplication(processIdentifier: pid),
element.isActualWindow(runningApp, wid, isOnNormalLevel, axTitle, subrole, role),
let app = (Applications.list.first { $0.runningApplication.processIdentifier == pid }) {
Expand Down

0 comments on commit d6b1fb4

Please sign in to comment.