Skip to content

Commit 5afd7b4

Browse files
committed
Better implementation of dragging
1 parent c035d3f commit 5afd7b4

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public final class SuggestionWidgetController: NSObject {
9797
private lazy var chatWindow = {
9898
let it = ChatWindow(
9999
contentRect: .zero,
100-
styleMask: [.borderless, .resizable],
100+
styleMask: [.resizable],
101101
backing: .buffered,
102102
defer: false
103103
)
@@ -574,6 +574,26 @@ extension SuggestionWidgetController: NSWindowDelegate {
574574
UserDefaults.shared.set(true, for: \.chatPanelInASeparateWindow)
575575
}
576576
}
577+
578+
public func windowDidBecomeKey(_ notification: Notification) {
579+
guard (notification.object as? NSWindow) === chatWindow else { return }
580+
let screenFrame = NSScreen.screens.first(where: { $0.frame.origin == .zero })?.frame ?? .zero
581+
var mouseLocation = NSEvent.mouseLocation
582+
let windowFrame = chatWindow.frame
583+
if mouseLocation.y > windowFrame.maxY - 40 {
584+
mouseLocation.y = screenFrame.size.height - mouseLocation.y
585+
if let cgEvent = CGEvent(
586+
mouseEventSource: nil,
587+
mouseType: .leftMouseDown,
588+
mouseCursorPosition: mouseLocation,
589+
mouseButton: .left
590+
),
591+
let event = NSEvent(cgEvent: cgEvent)
592+
{
593+
chatWindow.performDrag(with: event)
594+
}
595+
}
596+
}
577597
}
578598

579599
class CanBecomeKeyWindow: NSWindow {
@@ -585,34 +605,12 @@ class CanBecomeKeyWindow: NSWindow {
585605
class ChatWindow: NSWindow {
586606
override var canBecomeKey: Bool { true }
587607
override var canBecomeMain: Bool { true }
588-
var dragStartLocation = NSPoint.zero
589608

590-
override func mouseDragged(with event: NSEvent) {
591-
let screenVisibleFrame = NSScreen.main?.visibleFrame
609+
override func mouseDown(with event: NSEvent) {
592610
let windowFrame = frame
593-
var newOrigin = windowFrame.origin
594-
595611
let currentLocation = event.locationInWindow
596-
if dragStartLocation.y > windowFrame.size.height - 40 {
597-
newOrigin.x += (currentLocation.x - dragStartLocation.x)
598-
newOrigin.y += (currentLocation.y - dragStartLocation.y)
599-
600-
if (newOrigin.y + windowFrame.size.height) >
601-
(screenVisibleFrame?.origin.y ?? 0) + (screenVisibleFrame?.size.height ?? 0)
602-
{
603-
newOrigin.y = screenVisibleFrame?.origin
604-
.y ?? 0 + (screenVisibleFrame?.size.height ?? 0 - windowFrame.size.height)
605-
}
606-
UserDefaults.shared.set(true, for: \.chatPanelInASeparateWindow)
607-
setFrameOrigin(newOrigin)
612+
if currentLocation.y > windowFrame.size.height - 40 {
613+
performDrag(with: event)
608614
}
609615
}
610-
611-
override func mouseUp(with event: NSEvent) {
612-
dragStartLocation = .zero
613-
}
614-
615-
override func mouseDown(with event: NSEvent) {
616-
dragStartLocation = event.locationInWindow
617-
}
618616
}

0 commit comments

Comments
 (0)