From 2f92936848b3346692c2a53e0a9c27896beb46d5 Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Mon, 21 Dec 2020 13:03:08 +0900 Subject: [PATCH] feat: display windows partially on screen correctly (closes #727) --- src/logic/Spaces.swift | 10 +++++----- src/logic/Windows.swift | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/logic/Spaces.swift b/src/logic/Spaces.swift index 8e1a0e87..b429d4d9 100644 --- a/src/logic/Spaces.swift +++ b/src/logic/Spaces.swift @@ -2,7 +2,7 @@ import Cocoa class Spaces { static var currentSpaceId = CGSSpaceID(1) - static var visibleSpaceIds = [CGSSpaceID]() + static var screenToVisibleSpaceMap = [ScreenUuid: CGSSpaceID]() static var currentSpaceIndex = SpaceIndex(1) static var isSingleSpace = true static var idsAndIndexes: [(CGSSpaceID, SpaceIndex)] = allIdsAndIndexes() @@ -14,7 +14,7 @@ class Spaces { updateCurrentSpace() refreshVisibleSpaces() }) - NSWorkspace.shared.notificationCenter.addObserver(forName: NSApplication.didChangeScreenParametersNotification , object: nil, queue: nil, using: { _ in + NSWorkspace.shared.notificationCenter.addObserver(forName: NSApplication.didChangeScreenParametersNotification, object: nil, queue: nil, using: { _ in debugPrint("OS event", "didChangeScreenParametersNotification") refreshVisibleSpaces() }) @@ -29,11 +29,11 @@ class Spaces { } static func refreshVisibleSpaces() { - visibleSpaceIds = NSScreen.screens.compactMap { + screenToVisibleSpaceMap.removeAll() + NSScreen.screens.forEach { if let uuid = $0.uuid() { - return CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, uuid) + screenToVisibleSpaceMap[uuid] = CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, uuid) } - return nil } } diff --git a/src/logic/Windows.swift b/src/logic/Windows.swift index 865d34d6..ef8848d5 100644 --- a/src/logic/Windows.swift +++ b/src/logic/Windows.swift @@ -196,17 +196,18 @@ class Windows { !(!(Preferences.showFullscreenWindows[App.app.shortcutIndex] != .hide) && window.isFullscreen) && !(!(Preferences.showMinimizedWindows[App.app.shortcutIndex] != .hide) && window.isMinimized) && !(Preferences.spacesToShow[App.app.shortcutIndex] == .active && window.spaceId != Spaces.currentSpaceId) && - !(Preferences.spacesToShow[App.app.shortcutIndex] == .visible && !Spaces.visibleSpaceIds.contains(window.spaceId)) && + !(Preferences.spacesToShow[App.app.shortcutIndex] == .visible && !Spaces.screenToVisibleSpaceMap.values.contains(window.spaceId)) && !(Preferences.screensToShow[App.app.shortcutIndex] == .showingAltTab && !isOnScreen(window, screen)) && (Preferences.showTabsAsWindows || !window.isTabbed)) } static func isOnScreen(_ window: Window, _ screen: NSScreen) -> Bool { - if let topLeftCorner = window.position, let size = window.size { + if let topLeftCorner = window.position, let size = window.size, let screenUuid = screen.uuid(), let screenSpaceId = Spaces.screenToVisibleSpaceMap[screenUuid] { var screenFrameInQuartzCoordinates = screen.frame screenFrameInQuartzCoordinates.origin.y = NSMaxY(NSScreen.screens[0].frame) - NSMaxY(screen.frame) let windowRect = CGRect(origin: topLeftCorner, size: size) - return windowRect.intersects(screenFrameInQuartzCoordinates) + debugPrint(windowRect.intersects(screenFrameInQuartzCoordinates), screenSpaceId, window.spaceId) + return windowRect.intersects(screenFrameInQuartzCoordinates) && screenSpaceId == window.spaceId } return true }