Skip to content
Merged
Show file tree
Hide file tree
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
45 changes: 18 additions & 27 deletions Sources/TerminalWindowPortal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,21 @@ 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.
// Test seam: production calls read `NSApp.currentEvent`; tests pass 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) {
let resolveHostedTerminalHitView = hostedTerminalHitViewResolver(at: point)

if shouldPassThroughToTitlebar(at: point, hostedTerminalHitView: resolveHostedTerminalHitView) {
clearActiveDividerCursor(restoreArrow: false)
return nil
}

if shouldPassThroughToPaneTabBar(at: point, eventType: currentEvent?.type) {
if shouldPassThroughToPaneTabBar(at: point, eventType: currentEvent?.type, hostedTerminalHitView: resolveHostedTerminalHitView) {
clearActiveDividerCursor(restoreArrow: false)
return nil
}
Expand All @@ -142,7 +143,6 @@ final class WindowTerminalHostView: NSView {
return nil
}

// Compute divider hit once and reuse for both cursor update and pass-through.
if let kind = splitDividerCursorKind(at: point) {
activeDividerCursorKind = kind
kind.cursor.set()
Expand Down Expand Up @@ -203,43 +203,34 @@ final class WindowTerminalHostView: NSView {
return hitView === self ? nil : hitView
}

private func shouldPassThroughToTitlebar(at point: NSPoint) -> Bool {
private func shouldPassThroughToTitlebar(at point: NSPoint, hostedTerminalHitView: () -> NSView?) -> 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 }
if isMinimalModeTitlebarControlHit(window: window, locationInWindow: windowPoint) { return true }
return hostedTerminalHitView() == nil
}

private func shouldPassThroughToPaneTabBar(
at point: NSPoint,
eventType: NSEvent.EventType?
eventType: NSEvent.EventType?,
hostedTerminalHitView: () -> NSView?
) -> Bool {
guard let decision = BonsplitTabBarPassThrough.passThroughDecision(
at: point,
in: self,
eventType: eventType
) else { return false }
guard decision.result else { return false }
if decision.registryHit {
return true
}
return hostedTerminalHitView(at: point) == nil
}

private func hostedTerminalHitView(at point: NSPoint) -> NSView? {
for subview in subviews.reversed() {
guard let hostedView = subview as? GhosttySurfaceScrollView,
!hostedView.isHidden,
hostedView.alphaValue > 0,
hostedView.frame.contains(point) else { continue }

return hostedView.hitTest(point) ?? hostedView
}
return nil
if decision.registryHit { return true }
return hostedTerminalHitView() == nil
}

private func shouldPassThroughToChrome(at point: NSPoint, eventType: NSEvent.EventType?) -> Bool {
shouldPassThroughToTitlebar(at: point)
|| shouldPassThroughToPaneTabBar(at: point, eventType: eventType)
let resolveHostedTerminalHitView = hostedTerminalHitViewResolver(at: point)

return shouldPassThroughToTitlebar(at: point, hostedTerminalHitView: resolveHostedTerminalHitView)
|| shouldPassThroughToPaneTabBar(at: point, eventType: eventType, hostedTerminalHitView: resolveHostedTerminalHitView)
}

private func cursorRectIntersectsChromePassThrough(_ rect: NSRect) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Sources/WindowDecorationsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ final class WindowDecorationsController {
lastMinimalModeTitlebarClick = nil
return false
}
guard !isMinimalModeTitlebarControlHit(window: window, locationInWindow: locationInWindow) else {
guard !minimalModeTitlebarDoubleClickShouldDefer(window: window, locationInWindow: locationInWindow) else {
lastMinimalModeTitlebarClick = nil
return false
}
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
76 changes: 76 additions & 0 deletions Sources/WindowTerminalHostViewHitTesting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import AppKit

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

return hostedView.hitTest(point) ?? hostedView
}
return nil
}

func hostedTerminalHitViewResolver(at point: NSPoint) -> () -> NSView? {
var cachedHitView: NSView?
var didResolve = false
return {
if !didResolve {
cachedHitView = self.hostedTerminalHitView(at: point)
didResolve = true
}
return cachedHitView
}
}

func hasHostedTerminal(at point: NSPoint) -> Bool {
hasHostedTerminal(at: point, in: self)
}

private func hasHostedTerminal(at point: NSPoint, in view: NSView) -> Bool {
guard !view.isHidden, view.alphaValue > 0 else { return false }
let pointInView = view.convert(point, from: self)
guard view.bounds.contains(pointInView) else { return false }
if view is GhosttySurfaceScrollView { return true }
for subview in view.subviews.reversed() {
if hasHostedTerminal(at: point, in: subview) { 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 }
let pointInView = view.convert(windowPoint, from: nil)
guard view.bounds.contains(pointInView) else { return false }

if let hostView = view as? WindowTerminalHostView {
let pointInHost = hostView.convert(windowPoint, from: nil)
if 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 @@ -783,6 +783,8 @@
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 */; };
606600020000000000000001 /* WindowTerminalHostViewHitTesting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 606600020000000000000002 /* WindowTerminalHostViewHitTesting.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 @@ -1612,6 +1614,8 @@
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>"; };
606600020000000000000002 /* WindowTerminalHostViewHitTesting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTerminalHostViewHitTesting.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 @@ -2104,6 +2108,7 @@
D0B1001DA1B2C3D4E5F60001 /* PaneDropRoutingSupport.swift */,
A5001531 /* TerminalWindowPortal.swift */,
C750500000000000000000B2 /* TerminalSurfaceRuntimeWiring.swift */,
606600020000000000000002 /* WindowTerminalHostViewHitTesting.swift */,
D0B10007A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift */,
D0B1001FA1B2C3D4E5F60001 /* BrowserPaneDropTargetView.swift */,
A5001533 /* BrowserWindowPortal.swift */,
Expand Down Expand Up @@ -2494,6 +2499,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 @@ -3501,6 +3507,7 @@
807E058A23061EFB70A1B7F8 /* WindowGlassEffect.swift in Sources */,
D0B10024A1B2C3D4E5F60001 /* WindowInputRoutingContext.swift in Sources */,
313585583035A1E685010A97 /* WindowKeyDownReplayGuard.swift in Sources */,
606600020000000000000001 /* WindowTerminalHostViewHitTesting.swift in Sources */,
604500100000000000000001 /* WindowTitleTemplate.swift in Sources */,
604500200000000000000001 /* WindowTitleTemplateContext.swift in Sources */,
A5001209 /* WindowToolbarController.swift in Sources */,
Expand Down Expand Up @@ -3813,6 +3820,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
Loading
Loading