diff --git a/Loop/Extensions/NSScreen+Extensions.swift b/Loop/Extensions/NSScreen+Extensions.swift index 18c7d0be..d1582513 100644 --- a/Loop/Extensions/NSScreen+Extensions.swift +++ b/Loop/Extensions/NSScreen+Extensions.swift @@ -15,7 +15,7 @@ extension NSScreen { return self.deviceDescription[key] as? CGDirectDisplayID } - func screenWithMouse() -> NSScreen? { + static var screenWithMouse: NSScreen? { let mouseLocation = NSEvent.mouseLocation let screens = NSScreen.screens let screenWithMouse = (screens.first { NSMouseInRect(mouseLocation, $0.frame, false) }) diff --git a/Loop/Helpers/LoopManager.swift b/Loop/Helpers/LoopManager.swift index a6cffc46..e13a9376 100644 --- a/Loop/Helpers/LoopManager.swift +++ b/Loop/Helpers/LoopManager.swift @@ -21,6 +21,7 @@ class LoopManager { private var currentResizingDirection: WindowDirection = .noAction private var isLoopShown: Bool = false private var frontmostWindow: AXUIElement? + private var screenWithMouse: NSScreen? func startObservingKeys() { NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.flagsChanged) { event -> Void in @@ -82,12 +83,12 @@ class LoopManager { // Loop will only open if accessibility access has been granted if accessibilityAccessManager.getStatus() { self.frontmostWindow = windowEngine.getFrontmostWindow() + self.screenWithMouse = NSScreen.screenWithMouse if Defaults[.previewVisibility] == true && frontmostWindow != nil { previewController.show() } radialMenuController.show(frontmostWindow: frontmostWindow) - keybindMonitor.start() isLoopShown = true @@ -102,6 +103,7 @@ class LoopManager { keybindMonitor.stop() if self.frontmostWindow != nil && + self.screenWithMouse != nil && forceClose == false && self.isLoopShown == true && self.currentResizingDirection != .noAction { @@ -111,7 +113,7 @@ class LoopManager { isLoopShown = false if willResizeWindow { - windowEngine.resize(window: self.frontmostWindow!, direction: self.currentResizingDirection) + windowEngine.resize(window: self.frontmostWindow!, direction: self.currentResizingDirection, screen: self.screenWithMouse!) NotificationCenter.default.post( name: Notification.Name.finishedLooping, diff --git a/Loop/Helpers/WindowEngine.swift b/Loop/Helpers/WindowEngine.swift index e3e71b51..3b144932 100644 --- a/Loop/Helpers/WindowEngine.swift +++ b/Loop/Helpers/WindowEngine.swift @@ -13,8 +13,9 @@ struct WindowEngine { private let kAXFullscreenAttribute = "AXFullScreen" func resizeFrontmostWindow(direction: WindowDirection) { - guard let frontmostWindow = self.getFrontmostWindow() else { return } - resize(window: frontmostWindow, direction: direction) + guard let frontmostWindow = self.getFrontmostWindow(), + let screenWithMouse = NSScreen.screenWithMouse else { return } + resize(window: frontmostWindow, direction: direction, screen: screenWithMouse) } func getFrontmostWindow() -> AXUIElement? { @@ -38,11 +39,11 @@ struct WindowEngine { return window } - func resize(window: AXUIElement, direction: WindowDirection) { + func resize(window: AXUIElement, direction: WindowDirection, screen: NSScreen) { self.setFullscreen(element: window, state: false) let windowFrame = getRect(element: window) - guard let screenFrame = getActiveScreenFrame(), + guard let screenFrame = getScreenFrame(screen: screen), let newWindowFrame = generateWindowFrame(windowFrame, screenFrame, direction) else { return } @@ -121,8 +122,7 @@ struct WindowEngine { return CGRect(origin: getPosition(element: element), size: getSize(element: element)) } - private func getActiveScreenFrame() -> CGRect? { - guard let screen = NSScreen().screenWithMouse() else { return nil } + private func getScreenFrame(screen: NSScreen) -> CGRect? { guard let displayID = screen.displayID else { return nil } let menubarHeight = screen.frame.size.height - screen.visibleFrame.size.height var screenFrame = CGDisplayBounds(displayID) diff --git a/Loop/Preview Window/PreviewController.swift b/Loop/Preview Window/PreviewController.swift index d4b610b8..98d142c1 100644 --- a/Loop/Preview Window/PreviewController.swift +++ b/Loop/Preview Window/PreviewController.swift @@ -30,7 +30,7 @@ class PreviewController { panel.alphaValue = 0 panel.orderFrontRegardless() - guard let screen = NSScreen().screenWithMouse() else { return } + guard let screen = NSScreen.screenWithMouse else { return } let menubarHeight = screen.frame.size.height - screen.visibleFrame.size.height let screenWidth = screen.frame.size.width diff --git a/Loop/Radial Menu/RadialMenuView.swift b/Loop/Radial Menu/RadialMenuView.swift index 288cca02..bca3e595 100644 --- a/Loop/Radial Menu/RadialMenuView.swift +++ b/Loop/Radial Menu/RadialMenuView.swift @@ -124,17 +124,9 @@ struct RadialMenuView: View { } private func refreshCurrentAngle() { - let currentAngleToMouse = Angle( - radians: initialMousePosition.angle(to: CGPoint(x: NSEvent.mouseLocation.x, - y: NSEvent.mouseLocation.y)) - ) - - let currentDistanceToMouse = initialMousePosition.distanceSquared( - to: CGPoint( - x: NSEvent.mouseLocation.x, - y: NSEvent.mouseLocation.y - ) - ) + let currentMouseLocation = NSEvent.mouseLocation + let currentAngleToMouse = Angle(radians: initialMousePosition.angle(to: currentMouseLocation)) + let currentDistanceToMouse = initialMousePosition.distanceSquared(to: currentMouseLocation) if (currentAngleToMouse == angleToMouse) && (currentDistanceToMouse == distanceToMouse) { return