From 9afb385099c8a4897e4917727febe5aaea3ddc27 Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Wed, 22 Jan 2025 10:45:53 +0800 Subject: [PATCH] Fix text selection when dragging window with hidden titlebar --- .../Sources/Ghostty/SurfaceView_AppKit.swift | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index f5cb93580..473503449 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -124,6 +124,12 @@ extension Ghostty { // A timer to fallback to ghost emoji if no title is set within the grace period private var titleFallbackTimer: Timer? + // The height of the titlebar in points + private var titlebarHeight: CGFloat { + guard let window = self.window else { return 0 } + return window.frame.height - window.contentLayoutRect.height + } + /// Event monitor (see individual events for why) private var eventMonitor: Any? = nil @@ -579,12 +585,30 @@ extension Ghostty { return true } - override func mouseDown(with event: NSEvent) { - guard let surface = self.surface else { return } + private func handleSurfaceMouseDown(_ surface: ghostty_surface_t, _ event: NSEvent) { let mods = Ghostty.ghosttyMods(event.modifierFlags) ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_PRESS, GHOSTTY_MOUSE_LEFT, mods) } + override func mouseDown(with event: NSEvent) { + guard let surface = self.surface else { return } + guard let window = self.window else { + handleSurfaceMouseDown(surface, event) + return + } + + // Convert point to view coordinates + let point = self.convert(event.locationInWindow, from: nil) + let isInTitlebarRegion = point.y >= (self.frame.height - titlebarHeight) + + if isInTitlebarRegion { + window.performDrag(with: event) + return + } + + handleSurfaceMouseDown(surface, event) + } + override func mouseUp(with event: NSEvent) { // Always reset our pressure when the mouse goes up prevPressureStage = 0