Skip to content

Commit

Permalink
🐛 Only resize windows on the screen they started on
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Aug 31, 2023
1 parent 1d20817 commit 80b8353
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Loop/Extensions/NSScreen+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand Down
6 changes: 4 additions & 2 deletions Loop/Helpers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -102,6 +103,7 @@ class LoopManager {
keybindMonitor.stop()

if self.frontmostWindow != nil &&
self.screenWithMouse != nil &&
forceClose == false &&
self.isLoopShown == true &&
self.currentResizingDirection != .noAction {
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions Loop/Helpers/WindowEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand All @@ -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 }

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Loop/Preview Window/PreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 3 additions & 11 deletions Loop/Radial Menu/RadialMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 80b8353

Please sign in to comment.