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
16 changes: 10 additions & 6 deletions Sources/Bonsplit/Internal/Views/TabBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1955,9 +1955,10 @@ private struct TabBarHoverTrackingView: NSViewRepresentable {

/// Background view that provides window dragging from empty space, hover
/// tracking, and one geometry-routed tab-drag source monitor. The monitor does
/// not replace normal click dispatch; it consumes only the drag event that
/// crosses the reorder threshold and starts an AppKit session.
private struct TabBarDragAndHoverView: NSViewRepresentable {
/// not replace normal click dispatch; it starts an AppKit session when a drag
/// crosses the reorder threshold and forwards that event so SwiftUI can cancel
/// the pending tab press.
struct TabBarDragAndHoverView: NSViewRepresentable {
typealias BeginTabDrag = @MainActor (
_ tabId: UUID,
_ sourceView: NSView,
Expand Down Expand Up @@ -2161,7 +2162,7 @@ private struct TabBarDragAndHoverView: NSViewRepresentable {
pendingTabDrag = nil
}

private func handleTabDragEvent(_ event: NSEvent) -> NSEvent? {
func handleTabDragEvent(_ event: NSEvent) -> NSEvent? {
switch event.type {
case .leftMouseDown:
trackTabMouseDown(event)
Expand Down Expand Up @@ -2219,14 +2220,17 @@ private struct TabBarDragAndHoverView: NSViewRepresentable {
let onBeginTabDrag else {
return event
}
let didBegin = onBeginTabDrag(
_ = onBeginTabDrag(
pendingTabDrag.tabId,
self,
pendingTabDrag.mouseDownEvent,
pendingTabDrag.frame,
dragImage
)
return didBegin ? nil : event
// SwiftUI already received the mouse-down. Forward the threshold-
// crossing move so its pending tap/press fails before AppKit owns
// the native drag session and terminal mouse-up.
return event
}

private func dragImage(for frame: NSRect) -> NSImage? {
Expand Down
68 changes: 68 additions & 0 deletions Tests/BonsplitTests/TabBarDragEventRoutingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import AppKit
import Testing

@testable import Bonsplit

@MainActor
@Suite struct TabBarDragEventRoutingTests {
@Test func dragStartForwardsThresholdMoveToCancelPendingSwiftUIPress() throws {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 240, height: 80),
styleMask: [.borderless],
backing: .buffered,
defer: false
)
defer { window.orderOut(nil) }
let contentView = try #require(window.contentView)
let sourceView = TabBarDragAndHoverView.TabBarBackgroundNSView(frame: contentView.bounds)
let tabView = NSView(frame: NSRect(x: 20, y: 20, width: 120, height: 30))
let tabId = UUID()
let geometryRegistry = TabBarItemGeometryRegistry()
var beganTabId: UUID?

contentView.addSubview(sourceView)
sourceView.addSubview(tabView)
geometryRegistry.register(tabView, for: tabId)
sourceView.geometryRegistry = geometryRegistry
sourceView.tabIds = [tabId]
sourceView.onBeginTabDrag = { tabId, _, _, _, _ in
beganTabId = tabId
return true
}
window.makeKeyAndOrderFront(nil)

let mouseDown = try mouseEvent(
type: .leftMouseDown,
in: sourceView,
at: NSPoint(x: 40, y: 35)
)
let mouseDragged = try mouseEvent(
type: .leftMouseDragged,
in: sourceView,
at: NSPoint(x: 50, y: 45)
)

#expect(sourceView.handleTabDragEvent(mouseDown) === mouseDown)
#expect(sourceView.handleTabDragEvent(mouseDragged) === mouseDragged)
#expect(beganTabId == tabId)
}

private func mouseEvent(
type: NSEvent.EventType,
in view: NSView,
at point: NSPoint
) throws -> NSEvent {
let window = try #require(view.window)
return try #require(NSEvent.mouseEvent(
with: type,
location: view.convert(point, to: nil),
modifierFlags: [],
timestamp: ProcessInfo.processInfo.systemUptime,
windowNumber: window.windowNumber,
context: nil,
eventNumber: 0,
clickCount: 1,
pressure: 1
))
}
}
Loading