Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Loop/Core/WindowDragManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class WindowDragManager {
private var leftMouseDraggedMonitor: EventMonitor?
private var leftMouseUpMonitor: EventMonitor?

private var currentMousePosition: CGPoint {
NSEvent.mouseLocation.flipY(screen: NSScreen.screens[0])
}

func addObservers() {
leftMouseDraggedMonitor = NSEventMonitor(scope: .all, eventMask: .leftMouseDragged) { event in
// Process window (only ONCE during a window drag)
Expand Down Expand Up @@ -75,11 +79,9 @@ class WindowDragManager {
}

private func setCurrentDraggingWindow() {
let mousePosition = NSEvent.mouseLocation.flipY(screen: NSScreen.screens[0])

do {
guard
let draggingWindow = try WindowUtility.windowAtPosition(mousePosition),
let draggingWindow = try WindowUtility.windowAtPosition(currentMousePosition),
!draggingWindow.isAppExcluded
else {
return
Expand Down Expand Up @@ -109,22 +111,22 @@ class WindowDragManager {
if let screen = NSScreen.screenWithMouse {
var newWindowFrame = window.frame
newWindowFrame.size = initialFrame.size
newWindowFrame = newWindowFrame.pushInside(screen.frame)
newWindowFrame = newWindowFrame.pushInside(screen.displayBounds)
window.setFrame(newWindowFrame)
} else {
window.size = initialFrame.size
}

// If the window doesn't contain the cursor, keep the original maxX
if let cursorLocation = CGEvent.mouseLocation, !window.frame.contains(cursorLocation) {
if !window.frame.contains(currentMousePosition) {
var newFrame = window.frame

newFrame.origin.x = startFrame.maxX - newFrame.width
window.setFrame(newFrame)

// If it still doesn't contain the cursor, move the window to be centered with the cursor
if !newFrame.contains(cursorLocation) {
newFrame.origin.x = cursorLocation.x - (newFrame.width / 2)
if !newFrame.contains(currentMousePosition) {
newFrame.origin.x = currentMousePosition.x - (newFrame.width / 2)
window.setFrame(newFrame)
}
}
Expand Down