From a792a7fc37ad079785359509a23917f842330792 Mon Sep 17 00:00:00 2001 From: "chenjian.358" Date: Thu, 23 Jan 2025 12:55:21 +0800 Subject: [PATCH] fix: 1. fix isOnScreen logic feat: 1. filter no normal type screen (For example, the window when creating a meeting in Lark is abnormal) 2. Optimize the decision logic of similar apps --- src/logic/NSScreen.swift | 1 + src/logic/Spaces.swift | 1 + src/logic/Window.swift | 17 ++++++++++++++++- src/logic/Windows.swift | 17 +++++++++++++++-- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/logic/NSScreen.swift b/src/logic/NSScreen.swift index ea35ca24..ca72007c 100644 --- a/src/logic/NSScreen.swift +++ b/src/logic/NSScreen.swift @@ -5,6 +5,7 @@ extension NSScreen { static func updatePreferred() { preferred = detectPreferred() ?? NSScreen.screens.first! + debugPrint("11111111",preferred) } private static func detectPreferred() -> NSScreen? { diff --git a/src/logic/Spaces.swift b/src/logic/Spaces.swift index b156cb56..7f93c500 100644 --- a/src/logic/Spaces.swift +++ b/src/logic/Spaces.swift @@ -35,6 +35,7 @@ class Spaces { if let mainScreen = NSScreen.main, let uuid = mainScreen.uuid() { currentSpaceId = CGSManagedDisplayGetCurrentSpace(CGS_CONNECTION, uuid) + debugPrint("55|",currentSpaceId," ",mainScreen) } currentSpaceIndex = idsAndIndexes.first { (spaceId: CGSSpaceID, _) -> Bool in spaceId == currentSpaceId diff --git a/src/logic/Window.swift b/src/logic/Window.swift index bac18313..22e76c48 100644 --- a/src/logic/Window.swift +++ b/src/logic/Window.swift @@ -25,6 +25,9 @@ class Window { var application: Application var axObserver: AXObserver? var rowIndex: Int? + + var spaceIds = [UInt64]() + var isNormal = true static let notifications = [ kAXUIElementDestroyedNotification, @@ -36,10 +39,13 @@ class Window { ] init(_ axUiElement: AXUIElement, _ application: Application, _ wid: CGWindowID, _ axTitle: String?, _ isFullscreen: Bool, _ isMinimized: Bool, _ position: CGPoint?, _ size: CGSize?) { + let subRole = try? axUiElement.subrole() + self.isNormal = subRole == "AXStandardWindow" // TODO: make a efficient batched AXUIElementCopyMultipleAttributeValues call once for each window, and store the values self.axUiElement = axUiElement self.application = application cgWindowId = wid + debugPrint("33init") spaceId = Spaces.currentSpaceId spaceIndex = Spaces.currentSpaceIndex self.isFullscreen = isFullscreen @@ -235,7 +241,16 @@ class Window { func isOnScreen(_ screen: NSScreen) -> Bool { if NSScreen.screensHaveSeparateSpaces { if let screenUuid = screen.uuid(), let screenSpaces = Spaces.screenSpacesMap[screenUuid] { - return screenSpaces.contains { $0 == spaceId } + debugPrint("22|",spaceId,screen,screenSpaces,title!) + for v1 in screenSpaces { + for v2 in spaceIds { + if v1 == v2 { + return true + } + } + } + return false +// return screenSpaces.contains { $0 == spaceId } } } else { let referenceWindow = referenceWindowForTabbedWindow() diff --git a/src/logic/Windows.swift b/src/logic/Windows.swift index 64a1cc76..72620178 100644 --- a/src/logic/Windows.swift +++ b/src/logic/Windows.swift @@ -289,6 +289,8 @@ class Windows { // note: for some reason, it behaves differently if you minimize the tab group after moving it to another space if let cgWindowId = window.cgWindowId { let spaceIds = cgWindowId.spaces() + window.spaceIds = spaceIds + debugPrint("44|",window.cgWindowId!,spaceIds,window.spaceId,window.title!,Spaces.currentSpaceId) if spaceIds.count == 1 { window.spaceId = spaceIds.first! window.spaceIndex = Spaces.idsAndIndexes.first { $0.0 == spaceIds.first! }!.1 @@ -298,6 +300,8 @@ class Windows { window.spaceIndex = Spaces.currentSpaceIndex window.isOnAllSpaces = true } + debugPrint("44|",window.cgWindowId!,spaceIds,window.spaceId,window.title!) + } } @@ -345,14 +349,23 @@ class Windows { } private static func refreshIfWindowShouldBeShownToTheUser(_ window: Window) { - window.shouldShowTheUser = + var isSameApp = false + if let a = window.application.runningApplication.bundleIdentifier{ + if let b = NSWorkspace.shared.frontmostApplication?.bundleIdentifier{ + if a.contains(b) || b.contains(a) { + isSameApp = true + } + } + } + window.shouldShowTheUser = window.isNormal && !(window.application.bundleIdentifier.flatMap { id in Preferences.blacklist.contains { id.hasPrefix($0.bundleIdentifier) && ($0.hide == .always || (window.isWindowlessApp && $0.hide != .none)) } } ?? false) && - !(Preferences.appsToShow[App.app.shortcutIndex] == .active && window.application.pid != NSWorkspace.shared.frontmostApplication?.processIdentifier) && + !(Preferences.appsToShow[App.app.shortcutIndex] == .active && + (window.application.pid != NSWorkspace.shared.frontmostApplication?.processIdentifier && !isSameApp)) && !(!(Preferences.showHiddenWindows[App.app.shortcutIndex] != .hide) && window.isHidden) && ((!Preferences.hideWindowlessApps && window.isWindowlessApp) || !window.isWindowlessApp &&