Skip to content
Open
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
17 changes: 16 additions & 1 deletion apple/TablePro/Views/Main/EditorTabItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal final class EditorTabItemView: NSView {
private let statusView = NSImageView()
private let spinner = NSProgressIndicator()
private var pressLocation: NSPoint?
private var dragAwaitsRelease = false
private var middlePressed = false

internal init(item: EditorTabItem) {
Expand Down Expand Up @@ -331,6 +332,7 @@ internal final class EditorTabItemView: NSView {

override internal func mouseDown(with event: NSEvent) {
pressLocation = convert(event.locationInWindow, from: nil)
dragAwaitsRelease = false
strip?.activate(item.id, click: EditorTabClick(event: event))
}

Expand All @@ -339,10 +341,23 @@ internal final class EditorTabItemView: NSView {
let point = convert(event.locationInWindow, from: nil)
guard hypot(point.x - start.x, point.y - start.y) >= EditorTabStripLayout.dragThreshold else { return }
pressLocation = nil
strip?.beginDrag(of: self, with: event)
guard let strip else { return }
// Handled after the button already went up: a session would never see the release, so the queued release settles the drag.
guard strip.isPrimaryButtonDown() else {
dragAwaitsRelease = true
strip.beginDeferredDrag()
return
}
strip.beginDrag(of: self, with: event)
}

override internal func mouseUp(with event: NSEvent) {
if dragAwaitsRelease {
dragAwaitsRelease = false
guard let window else { return }
strip?.drop(self, releasedAt: window.convertPoint(toScreen: event.locationInWindow))
return
}
// A drag clears the press, so only a release that stayed put reaches the strip.
guard pressLocation != nil else { return }
pressLocation = nil
Expand Down
112 changes: 94 additions & 18 deletions apple/TablePro/Views/Main/EditorTabStripView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Combine
@MainActor
internal final class EditorTabStripView: NSView {
internal var commands: EditorTabCommands?
// The button's state now, not when the event being handled was queued.
internal var isPrimaryButtonDown: () -> Bool = { NSEvent.pressedMouseButtons & 1 != 0 }

internal private(set) var items: [EditorTabItem] = []
internal private(set) var run = EditorTabRunLayout(viewportWidth: 0, count: 0)
Expand Down Expand Up @@ -534,16 +536,21 @@ internal final class EditorTabStripView: NSView {

// A drag session reports Escape and a release on empty space the same way, so the key is latched as it arrives.
private func watchForDragCancel() {
stopWatchingForDragCancel()
dragWasCancelled = false
dragCancelMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
let isEscape = event.keyCode == KeyCode.escape.rawValue
MainActor.assumeIsolated {
if isEscape { self?.dragWasCancelled = true }
}
let keyCode = event.keyCode
MainActor.assumeIsolated { self?.noteKeyDuringDrag(keyCode) }
return event
}
}

internal var isWatchingForDragCancel: Bool { dragCancelMonitor != nil }

internal func noteKeyDuringDrag(_ keyCode: UInt16) {
if keyCode == KeyCode.escape.rawValue { dragWasCancelled = true }
}

private func stopWatchingForDragCancel() {
guard let dragCancelMonitor else { return }
NSEvent.removeMonitor(dragCancelMonitor)
Expand All @@ -552,7 +559,7 @@ internal final class EditorTabStripView: NSView {

internal func dragDidEnd(of view: EditorTabItemView, at screenPoint: NSPoint, operation: NSDragOperation) {
let id = view.item.id
let wasCancelled = dragWasCancelled || NSEvent.pressedMouseButtons & 1 != 0
let wasCancelled = dragWasCancelled || isPrimaryButtonDown()
stopWatchingForDragCancel()
reorder = nil
dragHasLeftStrip = false
Expand All @@ -568,6 +575,70 @@ internal final class EditorTabStripView: NSView {
commands.tearOff(id, screenPoint)
}

// A drag handled only after its release runs no session, and an Escape still queued before the release cancels it.
internal func beginDeferredDrag() {
pendingRenameId = nil
watchForDragCancel()
}

// The release point settles a deferred drag as a session would have.
internal func drop(_ view: EditorTabItemView, releasedAt screenPoint: NSPoint) {
let wasCancelled = dragWasCancelled
stopWatchingForDragCancel()
guard let commands, !wasCancelled else { return }
let id = view.item.id
if let target = Self.strip(at: screenPoint), let targetWindow = target.window {
target.settleDrop(of: view, atWindowPoint: targetWindow.convertPoint(fromScreen: screenPoint))
return
}
guard EditorTabDragEnding.opensNewWindow(
operation: [],
wasCancelled: false,
isOverTabStrip: commands.isOverTabStrip(screenPoint),
canTearOff: commands.canTearOff(id)
) else { return }
commands.tearOff(id, screenPoint)
}

internal func settleDrop(of source: EditorTabItemView, atWindowPoint point: NSPoint) {
let location = slotLocation(atWindowPoint: point)
guard source.strip === self else {
guard accepts(source) else { return }
updateIncoming(toLocation: location)
_ = receive(source)
return
}
let pinned = Set(items.filter(\.isPinned).map(\.id))
reorder = EditorTabReorder(draggedId: source.item.id, order: items.map(\.id), pinnedIds: pinned)
updateReorder(toLocation: location)
commitReorder()
reorder = nil
layoutTabs(animated: true)
}

// The frontmost window at the point decides, as it would for a session's destination.
private static func strip(at screenPoint: NSPoint) -> EditorTabStripView? {
let number = NSWindow.windowNumber(at: screenPoint, belowWindowWithWindowNumber: 0)
guard let window = NSApp.window(withWindowNumber: number) else { return nil }
return strip(in: window, atWindowPoint: window.convertPoint(fromScreen: screenPoint))
}

// The strip sits in the titlebar, outside the content view, so the hit test starts at the frame view.
internal static func strip(in window: NSWindow, atWindowPoint point: NSPoint) -> EditorTabStripView? {
var view = window.contentView?.superview?.hitTest(point)
while let current = view, !(current is EditorTabStripView) {
view = current.superview
}
return view as? EditorTabStripView
}

private func slotLocation(atWindowPoint point: NSPoint) -> CGFloat {
run.slotLocation(
ofContentX: document.convert(point, from: nil).x,
offset: scrollView.contentView.bounds.minX
)
}

// MARK: - Dragging in

override internal func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
Expand All @@ -581,10 +652,7 @@ internal final class EditorTabStripView: NSView {
let isOwnTab = source.strip === self
guard isOwnTab || accepts(source) else { return [] }
autoscroll(towards: sender.draggingLocation)
let location = run.slotLocation(
ofContentX: document.convert(sender.draggingLocation, from: nil).x,
offset: scrollView.contentView.bounds.minX
)
let location = slotLocation(atWindowPoint: sender.draggingLocation)
if isOwnTab {
updateReorder(toLocation: location)
} else {
Expand All @@ -604,22 +672,30 @@ internal final class EditorTabStripView: NSView {
}

override internal func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
guard let source = sender.draggingSource as? EditorTabItemView, let commands else { return false }
guard let source = sender.draggingSource as? EditorTabItemView, commands != nil else { return false }
if source.strip === self {
guard let reorder, reorder.movedFromOriginal, let destination = reorder.destinationIndex else {
return true
}
// The manager publishes a turn later, and the tab must not slide back in the meantime.
let byId = Dictionary(uniqueKeysWithValues: items.map { ($0.id, $0) })
items = reorder.order.compactMap { byId[$0] }
commands.moveTab(reorder.draggedId, destination)
commitReorder()
return true
}
return receive(source)
}

private func commitReorder() {
guard let commands, let reorder, reorder.movedFromOriginal, let destination = reorder.destinationIndex else {
return
}
// The manager publishes a turn later, and the tab must not slide back in the meantime.
let byId = Dictionary(uniqueKeysWithValues: items.map { ($0.id, $0) })
items = reorder.order.compactMap { byId[$0] }
commands.moveTab(reorder.draggedId, destination)
}

private func receive(_ source: EditorTabItemView) -> Bool {
defer {
incomingIndex = nil
layoutTabs(animated: true)
}
guard accepts(source), let index = incomingIndex else { return false }
guard let commands, accepts(source), let index = incomingIndex else { return false }
return commands.receiveTab(source.item.id, index)
}

Expand Down
9 changes: 7 additions & 2 deletions apple/TableProTests/Helpers/EditorTabFixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal final class EditorTabCommandRecorder {
var renameRequests: [UUID] = []
var renamed: [(UUID, String)] = []
var tornOff: [(UUID, NSPoint?)] = []
var moved: [(UUID, Int)] = []
var received: [(UUID, Int)] = []
var newTabCount = 0
}

Expand Down Expand Up @@ -55,12 +57,15 @@ extension EditorTabCommands {
canCloseOthers: { _ in canCloseOthers },
closeAll: {},
canCloseAll: { canCloseAll },
moveTab: { _, _ in },
moveTab: { recorder.moved.append(($0, $1)) },
canMove: { _, _ in true },
moveBy: { _, _ in },
tearOff: { recorder.tornOff.append(($0, $1)) },
canTearOff: { _ in canTearOff },
receiveTab: { _, _ in true },
receiveTab: { id, index in
recorder.received.append((id, index))
return true
},
isOverTabStrip: { _ in false },
moveToConnection: { recorder.movedToConnection.append($0) },
offersMoveToConnection: { _ in offersMoveToConnection },
Expand Down
Loading
Loading