From 2674c8f8dbade6643a647b2f22bf7aa9e8b41ddc Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Thu, 21 May 2020 19:34:06 +0900 Subject: [PATCH] fix: show windows which are opened in fullscreen (closes #335) --- src/api-wrappers/AXUIElement.swift | 7 +++++-- src/logic/DebugProfile.swift | 4 ++-- src/logic/Window.swift | 10 +++++----- src/logic/events/AccessibilityEvents.swift | 5 ++--- src/ui/App.swift | 3 +-- src/ui/main-window/ThumbnailView.swift | 4 ++-- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/api-wrappers/AXUIElement.swift b/src/api-wrappers/AXUIElement.swift index d6c70f16..c7edf714 100644 --- a/src/api-wrappers/AXUIElement.swift +++ b/src/api-wrappers/AXUIElement.swift @@ -49,8 +49,11 @@ extension AXUIElement { return attribute(kAXWindowsAttribute, [AXUIElement].self) } - func isTabbed(_ window: AXUIElement) -> Bool { - return windows()?.first { $0 == window } == nil + func isTabbed(_ app: AXUIElement, _ spaceId: CGSSpaceID) -> Bool { + // we can only detect tabs for windows on the current space, as AXUIElement.windows() only reports current space windows + // also, windows that start in fullscreen will have the wrong spaceID at that point in time, so we check if they are fullscreen too + return spaceId == Spaces.currentSpaceId && !isFullScreen() && + app.windows()?.first { $0 == self } == nil } func isMinimized() -> Bool { diff --git a/src/logic/DebugProfile.swift b/src/logic/DebugProfile.swift index 3115ad22..4ce83b1c 100644 --- a/src/logic/DebugProfile.swift +++ b/src/logic/DebugProfile.swift @@ -81,8 +81,8 @@ class DebugProfile { ("isMinimized", String(window.isMinimized)), ("isHidden", String(window.isHidden)), ("isOnAllSpaces", String(window.isOnAllSpaces)), - ("spaceId", window.spaceId.flatMap { String($0) } ?? "nil"), - ("spaceIndex", window.spaceIndex.flatMap { String($0) } ?? "nil"), + ("spaceId", String(window.spaceId)), + ("spaceIndex", String(window.spaceIndex)), ]) } diff --git a/src/logic/Window.swift b/src/logic/Window.swift index 1b39acec..40f5cf7f 100644 --- a/src/logic/Window.swift +++ b/src/logic/Window.swift @@ -10,8 +10,8 @@ class Window { var isHidden: Bool var isMinimized: Bool var isOnAllSpaces: Bool - var spaceId: CGSSpaceID? - var spaceIndex: SpaceIndex? + var spaceId: CGSSpaceID + var spaceIndex: SpaceIndex var axUiElement: AXUIElement var application: Application var axObserver: AXObserver? @@ -30,12 +30,12 @@ class Window { self.axUiElement = axUiElement self.application = application self.cgWindowId = axUiElement.cgWindowId() + self.spaceId = Spaces.currentSpaceId + self.spaceIndex = Spaces.currentSpaceIndex self.icon = application.runningApplication.icon - self.isTabbed = application.axUiElement!.isTabbed(axUiElement) + self.isTabbed = axUiElement.isTabbed(application.axUiElement!, spaceId) self.isHidden = application.runningApplication.isHidden self.isMinimized = axUiElement.isMinimized() - self.spaceId = Spaces.currentSpaceId - self.spaceIndex = Spaces.currentSpaceIndex self.isOnAllSpaces = false self.title = Window.bestEffortTitle(axUiElement, cgWindowId, application) debugPrint("Adding window", cgWindowId, title, application.runningApplication.bundleIdentifier ?? "nil", Spaces.currentSpaceId, Spaces.currentSpaceIndex) diff --git a/src/logic/events/AccessibilityEvents.swift b/src/logic/events/AccessibilityEvents.swift index 650492ad..9a9bcb14 100644 --- a/src/logic/events/AccessibilityEvents.swift +++ b/src/logic/events/AccessibilityEvents.swift @@ -21,11 +21,10 @@ func axObserverCallback(observer: AXObserver, element: AXUIElement, notification private func focusedUiElementChanged(_ element: AXUIElement, _ applicationPointer: UnsafeMutableRawPointer?) { // this event is the only opportunity we have to check if a window became a tab, or a tab became a window - // we can only detect tabs for windows on the current space, as AXUIElement.windows() only reports current space windows let application = Unmanaged.fromOpaque(applicationPointer!).takeUnretainedValue() Windows.list.forEach { - if $0.spaceId == Spaces.currentSpaceId && $0.application == application { - $0.isTabbed = application.axUiElement!.isTabbed($0.axUiElement) + if $0.application == application { + $0.isTabbed = $0.axUiElement.isTabbed(application.axUiElement!, $0.spaceId) } } } diff --git a/src/ui/App.swift b/src/ui/App.swift index f3a1d014..da6bad54 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -189,9 +189,8 @@ class App: NSApplication, NSApplicationDelegate { debugPrint("showUiOrCycleSelection: isFirstSummon") isFirstSummon = false if Windows.list.count == 0 || CGWindow.isMissionControlActive() { hideUi(); return } - // TODO: find a way to update isSingleSpace by listening to space creation, instead of on every trigger + // TODO: find a way to update space info when spaces are changed, instead of on every trigger Spaces.idsAndIndexes = Spaces.allIdsAndIndexes() - // TODO: find a way to update space index when windows are moved to another space, instead of on every trigger Windows.updateSpaces() let screen = Screen.preferred() Windows.refreshWhichWindowsToShowTheUser(screen) diff --git a/src/ui/main-window/ThumbnailView.swift b/src/ui/main-window/ThumbnailView.swift index 7df6814f..cc12d51a 100644 --- a/src/ui/main-window/ThumbnailView.swift +++ b/src/ui/main-window/ThumbnailView.swift @@ -79,12 +79,12 @@ class ThumbnailView: NSStackView { } assignIfDifferent(&hiddenIcon.isHidden, !element.isHidden) assignIfDifferent(&minimizedIcon.isHidden, !element.isMinimized) - assignIfDifferent(&spaceIcon.isHidden, element.spaceIndex == nil || Spaces.isSingleSpace || Preferences.hideSpaceNumberLabels) + assignIfDifferent(&spaceIcon.isHidden, Spaces.isSingleSpace || Preferences.hideSpaceNumberLabels) if !spaceIcon.isHidden { if element.isOnAllSpaces { spaceIcon.setStar() } else { - spaceIcon.setNumber(UInt32(element.spaceIndex!)) + spaceIcon.setNumber(UInt32(element.spaceIndex)) } } assignIfDifferent(&frame.size.width, max(thumbnail.frame.size.width + Preferences.intraCellPadding * 2, ThumbnailView.widthMin(screen)))