Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
57 changes: 57 additions & 0 deletions Sources/Bonsplit/Internal/Views/TabBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ struct TabBarView: View {
Color.clear
.frame(width: trailing, height: TabBarMetrics.tabHeight)
.contentShape(Rectangle())
.background(
EmptyTabBarDoubleClickMonitorView {
controller.requestNewTab(kind: "terminal", inPane: pane.id)
}
)
.onDrop(of: [.tabTransfer], delegate: TabDropDelegate(
targetIndex: pane.tabs.count,
pane: pane,
Expand Down Expand Up @@ -413,6 +418,11 @@ struct TabBarView: View {
.fill(Color.clear)
.frame(width: 30, height: TabBarMetrics.tabHeight)
.contentShape(Rectangle())
.background(
EmptyTabBarDoubleClickMonitorView {
controller.requestNewTab(kind: "terminal", inPane: pane.id)
}
)
.onDrop(of: [.tabTransfer], delegate: TabDropDelegate(
targetIndex: pane.tabs.count,
pane: pane,
Expand Down Expand Up @@ -568,6 +578,53 @@ private struct SplitActionButtonStyle: ButtonStyle {
}
}

private struct EmptyTabBarDoubleClickMonitorView: NSViewRepresentable {
let onDoubleClick: () -> Void

final class Coordinator {
var onDoubleClick: (() -> Void)?
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 }

coordinator.onDoubleClick?()
return nil
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

return view
}

func updateNSView(_ nsView: NSView, context: Context) {
context.coordinator.view = nsView
context.coordinator.onDoubleClick = onDoubleClick
}
}

enum TabControlShortcutModifier: Equatable {
case control
case command
Expand Down
82 changes: 82 additions & 0 deletions Tests/BonsplitTests/BonsplitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
}
}
Loading