Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
12 changes: 6 additions & 6 deletions Sources/TerminalWindowPortal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,12 @@ final class WindowTerminalHostView: NSView {
performHitTest(at: point, currentEvent: NSApp.currentEvent)
}

// Test seam: production calls go through `hitTest(_:)` which reads
// `NSApp.currentEvent`; tests can call this directly with a synthetic
// pointer event so the typing-latency guard doesn't gate them out.
func performHitTest(at point: NSPoint, currentEvent: NSEvent?) -> NSView? {
let routingContext = WindowInputRoutingContext(event: currentEvent)
let eventType = routingContext.eventType

if routingContext.allowsPortalPointerHitTesting {
if shouldPassThroughToTitlebar(at: point) {
if shouldPassThroughToTitlebar(at: point, preservingHostedTerminal: true) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
clearActiveDividerCursor(restoreArrow: false)
return nil
}
Expand Down Expand Up @@ -202,10 +199,13 @@ final class WindowTerminalHostView: NSView {
return hitView === self ? nil : hitView
}

private func shouldPassThroughToTitlebar(at point: NSPoint) -> Bool {
private func shouldPassThroughToTitlebar(at point: NSPoint, preservingHostedTerminal: Bool = false) -> Bool {
guard let window else { return false }
let windowPoint = convert(point, to: nil)
return windowPoint.y >= BonsplitTabBarPassThrough.titlebarInteractionBandMinY(in: window)
guard windowPoint.y >= BonsplitTabBarPassThrough.titlebarInteractionBandMinY(in: window) else { return false }
return isMinimalModeTitlebarControlHit(window: window, locationInWindow: windowPoint)
|| !preservingHostedTerminal
|| hostedTerminalHitView(at: point) == nil
}

private func shouldPassThroughToPaneTabBar(
Expand Down
14 changes: 5 additions & 9 deletions Sources/WindowDragHandleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1349,13 +1349,6 @@ struct WindowDragHandleView: NSViewRepresentable {
}
}

private func titlebarDoubleClickMonitorShouldDeferToRegisteredControl(
window: NSWindow,
locationInWindow: NSPoint
) -> Bool {
isMinimalModeTitlebarControlHit(window: window, locationInWindow: locationInWindow)
}

/// Local monitor that guarantees double-clicks in custom titlebar surfaces trigger
/// the standard macOS titlebar action even when the visible strip is hosted by
/// higher-level SwiftUI/AppKit container views.
Expand Down Expand Up @@ -1395,7 +1388,7 @@ struct TitlebarDoubleClickMonitorView: NSViewRepresentable {
coordinator.lastClick = nil
return event
}
guard !titlebarDoubleClickMonitorShouldDeferToRegisteredControl(
guard !minimalModeTitlebarDoubleClickShouldDefer(
window: window,
locationInWindow: event.locationInWindow
) else {
Expand Down Expand Up @@ -1686,7 +1679,10 @@ struct MinimalModeTitlebarEventSurfaceView: NSViewRepresentable {
lastTitlebarClick = nil
return event
}
guard !isMinimalModeTitlebarControlHit(window: window, locationInWindow: locationInWindow) else {
guard !minimalModeTitlebarDoubleClickShouldDefer(
window: window,
locationInWindow: locationInWindow
) else {
lastTitlebarClick = nil
return event
}
Expand Down
46 changes: 46 additions & 0 deletions Sources/WindowTerminalHostViewHitTesting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import AppKit

extension WindowTerminalHostView {
func hasHostedTerminal(at point: NSPoint) -> Bool {
for subview in subviews.reversed() {
guard let hostedView = subview as? GhosttySurfaceScrollView,
!hostedView.isHidden,
hostedView.alphaValue > 0,
hostedView.frame.contains(point) else { continue }
return true
}
return false
}

static func hasHostedTerminal(atWindowPoint windowPoint: NSPoint, in window: NSWindow) -> Bool {
guard let rootView = window.contentView?.superview ?? window.contentView else { return false }
return hasHostedTerminal(atWindowPoint: windowPoint, in: rootView)
}

private static func hasHostedTerminal(atWindowPoint windowPoint: NSPoint, in view: NSView) -> Bool {
guard !view.isHidden, view.alphaValue > 0 else { return false }

if let hostView = view as? WindowTerminalHostView {
let pointInHost = hostView.convert(windowPoint, from: nil)
if hostView.bounds.contains(pointInHost), hostView.hasHostedTerminal(at: pointInHost) {
return true
}
}

for subview in view.subviews.reversed() {
if hasHostedTerminal(atWindowPoint: windowPoint, in: subview) {
return true
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return false
}
}

func minimalModeTitlebarDoubleClickShouldDefer(
window: NSWindow,
locationInWindow: NSPoint
) -> Bool {
isMinimalModeTitlebarControlHit(window: window, locationInWindow: locationInWindow)
|| WindowTerminalHostView.hasHostedTerminal(atWindowPoint: locationInWindow, in: window)
}
8 changes: 8 additions & 0 deletions cmux.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
A9F20000000000000000000A /* TerminalSurfaceCmuxContextEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9F10000000000000000000A /* TerminalSurfaceCmuxContextEnvironment.swift */; };
5154BEAB50364B86A9E36E4B /* TerminalViewportUITestRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31B04B98376C9BA8B9E44DCB /* TerminalViewportUITestRecorder.swift */; };
A5001532 /* TerminalWindowPortal.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001531 /* TerminalWindowPortal.swift */; };
606600020000000000000001 /* WindowTerminalHostViewHitTesting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 606600020000000000000002 /* WindowTerminalHostViewHitTesting.swift */; };
D0B10006A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B10007A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift */; };
C0DE7B100000000000000001 /* TextBoxInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE7B100000000000000002 /* TextBoxInput.swift */; };
C0DE7B310000000000000001 /* TextBoxMentionCachedIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE7B310000000000000002 /* TextBoxMentionCachedIndex.swift */; };
Expand Down Expand Up @@ -778,6 +779,7 @@
D0B10024A1B2C3D4E5F60001 /* WindowInputRoutingContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B10025A1B2C3D4E5F60001 /* WindowInputRoutingContext.swift */; };
313585583035A1E685010A97 /* WindowKeyDownReplayGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B8CFAF8FCA4231C702D16A /* WindowKeyDownReplayGuard.swift */; };
8770D0F2D2BB45359D6BE5E3 /* WindowKeyDownReplayGuardTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B79482F1ECA54E98BE5C8953 /* WindowKeyDownReplayGuardTests.swift */; };
606600010000000000000001 /* WindowTerminalHostViewTitlebarHitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 606600010000000000000002 /* WindowTerminalHostViewTitlebarHitTests.swift */; };
604500100000000000000001 /* WindowTitleTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604500100000000000000002 /* WindowTitleTemplate.swift */; };
604500200000000000000001 /* WindowTitleTemplateContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604500200000000000000002 /* WindowTitleTemplateContext.swift */; };
604500100000000000000003 /* WindowTitleTemplateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604500100000000000000004 /* WindowTitleTemplateTests.swift */; };
Expand Down Expand Up @@ -1557,6 +1559,7 @@
A9F10000000000000000000A /* TerminalSurfaceCmuxContextEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalSurfaceCmuxContextEnvironment.swift; sourceTree = "<group>"; };
31B04B98376C9BA8B9E44DCB /* TerminalViewportUITestRecorder.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = TerminalViewportUITestRecorder.swift; sourceTree = "<group>"; };
A5001531 /* TerminalWindowPortal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalWindowPortal.swift; sourceTree = "<group>"; };
606600020000000000000002 /* WindowTerminalHostViewHitTesting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTerminalHostViewHitTesting.swift; sourceTree = "<group>"; };
D0B10007A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalWindowPortalDebug.swift; sourceTree = "<group>"; };
C0DE7B100000000000000002 /* TextBoxInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxInput.swift; sourceTree = "<group>"; };
C0DE7B310000000000000002 /* TextBoxMentionCachedIndex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxMentionCachedIndex.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1604,6 +1607,7 @@
D0B10025A1B2C3D4E5F60001 /* WindowInputRoutingContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowInputRoutingContext.swift; sourceTree = "<group>"; };
81B8CFAF8FCA4231C702D16A /* WindowKeyDownReplayGuard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App/WindowKeyDownReplayGuard.swift; sourceTree = "<group>"; };
B79482F1ECA54E98BE5C8953 /* WindowKeyDownReplayGuardTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowKeyDownReplayGuardTests.swift; sourceTree = "<group>"; };
606600010000000000000002 /* WindowTerminalHostViewTitlebarHitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTerminalHostViewTitlebarHitTests.swift; sourceTree = "<group>"; };
604500100000000000000002 /* WindowTitleTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App/WindowTitleTemplate.swift; sourceTree = "<group>"; };
604500200000000000000002 /* WindowTitleTemplateContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App/WindowTitleTemplateContext.swift; sourceTree = "<group>"; };
604500100000000000000004 /* WindowTitleTemplateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTitleTemplateTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2089,6 +2093,7 @@
D0B10001A1B2C3D4E5F60001 /* TerminalPaneDropTargetView.swift */,
D0B1001DA1B2C3D4E5F60001 /* PaneDropRoutingSupport.swift */,
A5001531 /* TerminalWindowPortal.swift */,
606600020000000000000002 /* WindowTerminalHostViewHitTesting.swift */,
D0B10007A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift */,
D0B1001FA1B2C3D4E5F60001 /* BrowserPaneDropTargetView.swift */,
A5001533 /* BrowserWindowPortal.swift */,
Expand Down Expand Up @@ -2480,6 +2485,7 @@
D0B1000DA1B2C3D4E5F60001 /* CmuxWebViewDragRoutingTests.swift */,
D0B1000FA1B2C3D4E5F60001 /* BrowserPaneDropRoutingTests.swift */,
02FC74F2C27127CC565B3E8C /* TerminalAndGhosttyTests.swift */,
606600010000000000000002 /* WindowTerminalHostViewTitlebarHitTests.swift */,
C0DE53360000000000000002 /* TerminalSearchOverlayMouseReleaseTests.swift */,
C35610000000000000000002 /* FinderFileDropRegressionTests.swift */,
C44550000000000000000002 /* PanelOwnedNativeViewSessionTests.swift */,
Expand Down Expand Up @@ -3446,6 +3452,7 @@
A9F20000000000000000000A /* TerminalSurfaceCmuxContextEnvironment.swift in Sources */,
5154BEAB50364B86A9E36E4B /* TerminalViewportUITestRecorder.swift in Sources */,
A5001532 /* TerminalWindowPortal.swift in Sources */,
606600020000000000000001 /* WindowTerminalHostViewHitTesting.swift in Sources */,
D0B10006A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift in Sources */,
C0DE7B100000000000000001 /* TextBoxInput.swift in Sources */,
C0DE7B310000000000000001 /* TextBoxMentionCachedIndex.swift in Sources */,
Expand Down Expand Up @@ -3793,6 +3800,7 @@
A59170000000000000000001 /* WindowAppearanceSnapshotPaneBackgroundTests.swift in Sources */,
F4200000A1B2C3D4E5F60718 /* WindowAppearanceSnapshotTests.swift in Sources */,
8770D0F2D2BB45359D6BE5E3 /* WindowKeyDownReplayGuardTests.swift in Sources */,
606600010000000000000001 /* WindowTerminalHostViewTitlebarHitTests.swift in Sources */,
604500100000000000000003 /* WindowTitleTemplateTests.swift in Sources */,
7F0A0E04A8CADC84BB4F1DF7 /* WorkspaceActionDispatcherTests.swift in Sources */,
D7AB00000000000000000011 /* WorkspaceAdjacentPaneMoveTests.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions cmuxTests/TitlebarInteractiveControlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ struct TitlebarInteractiveControlTests {

let insideControl = NSPoint(x: region.frame.midX, y: region.frame.midY)
#expect(
isMinimalModeTitlebarControlHit(window: window, locationInWindow: insideControl),
minimalModeTitlebarDoubleClickShouldDefer(window: window, locationInWindow: insideControl),
"A double-click on a titlebarInteractiveControl must register as a control hit so the synthetic titlebar double-click (zoom/minimize) is suppressed."
)

let emptyTitlebar = NSPoint(x: 220, y: 24)
#expect(
!isMinimalModeTitlebarControlHit(window: window, locationInWindow: emptyTitlebar),
!minimalModeTitlebarDoubleClickShouldDefer(window: window, locationInWindow: emptyTitlebar),
"Empty titlebar chrome away from any interactive control must still trigger the standard titlebar double-click action."
)
}
Expand Down
141 changes: 141 additions & 0 deletions cmuxTests/WindowTerminalHostViewTitlebarHitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import AppKit
import Testing

#if canImport(cmux_DEV)
@testable import cmux_DEV
#elseif canImport(cmux)
@testable import cmux
#endif

@MainActor
@Suite("Window terminal host titlebar hit testing")
struct WindowTerminalHostViewTitlebarHitTests {
@Test func hostViewKeepsTerminalTopRowClickableInsideTitlebarBand() throws {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 420, height: 260),
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)
defer { window.orderOut(nil) }
let contentView = try #require(window.contentView, "Expected window content view")
let container = try #require(contentView.superview, "Expected window content container")

let host = WindowTerminalHostView(frame: container.convert(contentView.bounds, from: contentView))
let hostedView = makeHostedTerminalView(frame: host.bounds)
host.addSubview(hostedView)
container.addSubview(host, positioned: .above, relativeTo: contentView)

let pointInHostedView = NSPoint(x: hostedView.bounds.midX, y: hostedView.bounds.maxY - 0.5)
let pointInWindow = hostedView.convert(pointInHostedView, to: nil)
let pointInHost = host.convert(pointInWindow, from: nil)
let event = try makeMouseDownEvent(at: pointInWindow, window: window)

try #require(
pointInWindow.y >= BonsplitTabBarPassThrough.titlebarInteractionBandMinY(in: window),
"The regression point must exercise the fixed-height titlebar pass-through band"
)
assertHitFallsInsideHostedTerminal(
host.performHitTest(at: pointInHost, currentEvent: event),
hostedView: hostedView,
message: "Terminal content inside the titlebar band should keep receiving top-row mouse-downs"
)
}

@Test func titlebarDoubleClickMonitorDefersToTerminalTopRowInsideTitlebarBand() throws {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 420, height: 260),
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)
defer { window.orderOut(nil) }
let contentView = try #require(window.contentView, "Expected window content view")
let container = try #require(contentView.superview, "Expected window content container")

let host = WindowTerminalHostView(frame: container.convert(contentView.bounds, from: contentView))
let hostedView = makeHostedTerminalView(frame: host.bounds)
host.addSubview(hostedView)
container.addSubview(host, positioned: .above, relativeTo: contentView)

let pointInHostedView = NSPoint(x: hostedView.bounds.midX, y: hostedView.bounds.maxY - 0.5)
let pointInWindow = hostedView.convert(pointInHostedView, to: nil)

try #require(
pointInWindow.y >= BonsplitTabBarPassThrough.titlebarInteractionBandMinY(in: window),
"The regression point must exercise the fixed-height titlebar pass-through band"
)
#expect(
minimalModeTitlebarDoubleClickShouldDefer(window: window, locationInWindow: pointInWindow),
"Synthetic titlebar double-click handling must yield to hosted terminal content in the top row"
)
}

@Test func hostViewPassesThroughRegisteredTitlebarControlsAboveTerminal() throws {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 420, height: 260),
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)
defer { window.orderOut(nil) }
let contentView = try #require(window.contentView, "Expected window content view")
let container = try #require(contentView.superview, "Expected window content container")

let host = WindowTerminalHostView(frame: container.convert(contentView.bounds, from: contentView))
host.addSubview(makeHostedTerminalView(frame: host.bounds))
container.addSubview(host, positioned: .above, relativeTo: contentView)

let region = TitlebarInteractiveControlRegion.RegisteredView(
frame: NSRect(x: 24, y: contentView.bounds.maxY - 24, width: 18, height: 18)
)
contentView.addSubview(region)

let pointInWindow = contentView.convert(NSPoint(x: region.frame.midX, y: region.frame.midY), to: nil)
let pointInHost = host.convert(pointInWindow, from: nil)
let event = try makeMouseDownEvent(at: pointInWindow, window: window)

try #require(
pointInWindow.y >= BonsplitTabBarPassThrough.titlebarInteractionBandMinY(in: window),
"The control point must sit inside the fixed titlebar interaction band"
)
#expect(
host.performHitTest(at: pointInHost, currentEvent: event) == nil,
"Registered titlebar controls must keep receiving clicks even when terminal content underlaps them"
)
}

private func makeHostedTerminalView(frame: NSRect) -> GhosttySurfaceScrollView {
let surfaceView = GhosttyNSView(frame: frame)
let hostedView = GhosttySurfaceScrollView(surfaceView: surfaceView)
hostedView.frame = frame
hostedView.autoresizingMask = [.width, .height]
return hostedView
}

private func makeMouseDownEvent(at locationInWindow: NSPoint, window: NSWindow) throws -> NSEvent {
try #require(NSEvent.mouseEvent(
with: .leftMouseDown,
location: locationInWindow,
modifierFlags: [],
timestamp: ProcessInfo.processInfo.systemUptime,
windowNumber: window.windowNumber,
context: nil,
eventNumber: 0,
clickCount: 1,
pressure: 1.0
), "Failed to create leftMouseDown event")
}

private func assertHitFallsInsideHostedTerminal(
_ hitView: NSView?,
hostedView: GhosttySurfaceScrollView,
message: String
) {
guard let hitView else {
Issue.record(Comment(rawValue: message))
return
}
#expect(hitView === hostedView || hitView.isDescendant(of: hostedView), Comment(rawValue: message))
}
}
Loading