diff --git a/Sources/Bonsplit/Internal/Views/TabBarView.swift b/Sources/Bonsplit/Internal/Views/TabBarView.swift index 4f2a5d38..8c33b393 100644 --- a/Sources/Bonsplit/Internal/Views/TabBarView.swift +++ b/Sources/Bonsplit/Internal/Views/TabBarView.swift @@ -139,6 +139,13 @@ struct TabBarView: View { Color.clear .frame(width: trailing, height: TabBarMetrics.tabHeight) .contentShape(Rectangle()) + .background( + EmptyTabBarDoubleClickMonitorView { + guard splitViewController.isInteractive else { return false } + controller.requestNewTab(kind: "terminal", inPane: pane.id) + return true + } + ) .onDrop(of: [.tabTransfer], delegate: TabDropDelegate( targetIndex: pane.tabs.count, pane: pane, @@ -413,6 +420,13 @@ struct TabBarView: View { .fill(Color.clear) .frame(width: 30, height: TabBarMetrics.tabHeight) .contentShape(Rectangle()) + .background( + EmptyTabBarDoubleClickMonitorView { + guard splitViewController.isInteractive else { return false } + controller.requestNewTab(kind: "terminal", inPane: pane.id) + return true + } + ) .onDrop(of: [.tabTransfer], delegate: TabDropDelegate( targetIndex: pane.tabs.count, pane: pane, @@ -568,6 +582,53 @@ private struct SplitActionButtonStyle: ButtonStyle { } } +private struct EmptyTabBarDoubleClickMonitorView: NSViewRepresentable { + let onDoubleClick: () -> Bool + + final class Coordinator { + var onDoubleClick: (() -> Bool)? + weak var view: NSView? + var monitor: Any? + + deinit { + if let monitor { + NSEvent.removeMonitor(monitor) + } + } + } + + func makeCoordinator() -> Coordinator { Coordinator() } + + func makeNSView(context: Context) -> NSView { + let view = NSView(frame: .zero) + view.wantsLayer = true + view.layer?.backgroundColor = NSColor.clear.cgColor + + context.coordinator.view = view + context.coordinator.onDoubleClick = onDoubleClick + + let coordinator = context.coordinator + coordinator.monitor = NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) { [weak coordinator] event in + guard event.clickCount >= 2 else { return event } + guard let coordinator, let view = coordinator.view, let window = view.window else { return event } + guard event.window === window else { return event } + + let point = view.convert(event.locationInWindow, from: nil) + guard view.bounds.contains(point) else { return event } + + guard coordinator.onDoubleClick?() == true else { return event } + return nil + } + + return view + } + + func updateNSView(_ nsView: NSView, context: Context) { + context.coordinator.view = nsView + context.coordinator.onDoubleClick = onDoubleClick + } +} + enum TabControlShortcutModifier: Equatable { case control case command diff --git a/Tests/BonsplitTests/BonsplitTests.swift b/Tests/BonsplitTests/BonsplitTests.swift index 6ad08e61..08ec881d 100644 --- a/Tests/BonsplitTests/BonsplitTests.swift +++ b/Tests/BonsplitTests/BonsplitTests.swift @@ -66,6 +66,16 @@ final class BonsplitTests: XCTestCase { } } + private final class NewTabRequestDelegateSpy: BonsplitDelegate { + var requestedKind: String? + var requestedPaneId: PaneID? + + func splitTabBar(_ controller: BonsplitController, didRequestNewTab kind: String, inPane pane: PaneID) { + requestedKind = kind + requestedPaneId = pane + } + } + @MainActor func testControllerCreation() { let controller = BonsplitController() @@ -497,6 +507,52 @@ final class BonsplitTests: XCTestCase { XCTAssertEqual(spy.paneId, pane) } + @MainActor + func testDoubleClickingEmptyTrailingTabBarSpaceRequestsNewTerminalTab() { + let appearance = BonsplitConfiguration.Appearance(showSplitButtons: false) + let configuration = BonsplitConfiguration(appearance: appearance) + let controller = BonsplitController(configuration: configuration) + let pane = controller.internalController.rootNode.allPanes.first! + let spy = NewTabRequestDelegateSpy() + controller.delegate = spy + + let hostingView = NSHostingView( + rootView: TabBarView(pane: pane, isFocused: true, showSplitButtons: false) + .environment(controller) + .environment(controller.internalController) + ) + let window = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: 480, height: 60), + styleMask: [.titled, .closable], + backing: .buffered, + defer: false + ) + defer { window.orderOut(nil) } + guard let contentView = window.contentView else { + XCTFail("Expected content view") + return + } + + hostingView.frame = contentView.bounds + hostingView.autoresizingMask = [.width, .height] + contentView.addSubview(hostingView) + + window.makeKeyAndOrderFront(nil) + contentView.layoutSubtreeIfNeeded() + RunLoop.current.run(until: Date().addingTimeInterval(0.05)) + contentView.layoutSubtreeIfNeeded() + + let clickPoint = NSPoint(x: hostingView.bounds.maxX - 12, y: hostingView.bounds.midY) + guard let event = try? makeLeftMouseDownEvent(in: hostingView, at: clickPoint, clickCount: 2) else { + XCTFail("Expected mouse event") + return + } + NSApp.sendEvent(event) + + XCTAssertEqual(spy.requestedKind, "terminal") + XCTAssertEqual(spy.requestedPaneId, pane.id) + } + func testIconSaturationKeepsRasterFaviconInColorWhenInactive() { XCTAssertEqual( TabItemStyling.iconSaturation(hasRasterIcon: true, tabSaturation: 0.0), @@ -937,4 +993,30 @@ final class BonsplitTests: XCTestCase { y < bitmap.pixelsHigh else { return nil } return bitmap.colorAt(x: x, y: y) } + + @MainActor + private func makeLeftMouseDownEvent( + in view: NSView, + at point: NSPoint, + clickCount: Int + ) throws -> NSEvent { + guard let window = view.window else { + throw NSError(domain: "BonsplitTests", code: 1, userInfo: [NSLocalizedDescriptionKey: "Missing window"]) + } + let pointInWindow = view.convert(point, to: nil) + guard let event = NSEvent.mouseEvent( + with: .leftMouseDown, + location: pointInWindow, + modifierFlags: [], + timestamp: ProcessInfo.processInfo.systemUptime, + windowNumber: window.windowNumber, + context: nil, + eventNumber: 0, + clickCount: clickCount, + pressure: 1 + ) else { + throw NSError(domain: "BonsplitTests", code: 2, userInfo: [NSLocalizedDescriptionKey: "Failed to create mouse event"]) + } + return event + } }