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
1 change: 1 addition & 0 deletions Copy/Shelf/ShelfRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ private struct ShelfItemsRow: View {
// Gives the drag-select the row's scroll view, which it scrolls while
// the pointer is held at an edge.
.background(CardRowScrollViewFinder { viewModel.cardSweep.scrollView = $0 })
.background(ShelfWheelScrollBridge())
.coordinateSpace(name: CardSweep.contentSpace)
.onGeometryChange(for: CGPoint.self) {
$0.frame(in: .named(CardSweep.viewportSpace)).origin
Expand Down
50 changes: 50 additions & 0 deletions Copy/Shelf/ShelfWheelScroll.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import AppKit
import SwiftUI

enum ShelfWheelScroll {
static func horizontalOffset(horizontal: CGFloat, vertical: CGFloat) -> CGFloat? {
guard horizontal == 0, vertical != 0 else { return nil }
return -vertical
}
}

struct ShelfWheelScrollBridge: NSViewRepresentable {
func makeNSView(context: Context) -> NSView { ShelfWheelScrollView() }

func updateNSView(_ nsView: NSView, context: Context) {}
}

private final class ShelfWheelScrollView: NSView {
private var monitor: Any?

override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
installMonitor()
}

deinit {
if let monitor { NSEvent.removeMonitor(monitor) }
}

private func installMonitor() {
guard monitor == nil else { return }
monitor = NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) { [weak self] event in
guard let self, let scroll = self.enclosingScrollView,
event.window === self.window,
let offset = ShelfWheelScroll.horizontalOffset(
horizontal: event.scrollingDeltaX, vertical: event.scrollingDeltaY),
self.contains(event: event, in: scroll) else { return event }

let maximum = max(0, (scroll.documentView?.frame.width ?? 0) - scroll.contentSize.width)
let x = min(max(0, scroll.contentView.bounds.origin.x + offset), maximum)
scroll.contentView.scroll(to: NSPoint(x: x, y: scroll.contentView.bounds.origin.y))
scroll.reflectScrolledClipView(scroll.contentView)
return nil
}
}

private func contains(event: NSEvent, in scroll: NSScrollView) -> Bool {
let point = scroll.contentView.convert(event.locationInWindow, from: nil)
return scroll.contentView.bounds.contains(point)
}
}
20 changes: 20 additions & 0 deletions CopyTests/ShelfWheelScrollTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import AppKit
import XCTest

final class ShelfWheelScrollTests: XCTestCase {
func testDownwardWheelMovementBecomesRightwardShelfMovement() {
XCTAssertEqual(ShelfWheelScroll.horizontalOffset(horizontal: 0, vertical: -3), 3)
}

func testUpwardWheelMovementBecomesLeftwardShelfMovement() {
XCTAssertEqual(ShelfWheelScroll.horizontalOffset(horizontal: 0, vertical: 3), -3)
}

func testHorizontalTrackpadMovementStaysWithAppKit() {
XCTAssertNil(ShelfWheelScroll.horizontalOffset(horizontal: 2, vertical: 0))
}

func testCombinedAxisTrackpadMovementStaysWithAppKit() {
XCTAssertNil(ShelfWheelScroll.horizontalOffset(horizontal: 2, vertical: -3))
}
}
1 change: 1 addition & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ targets:
sources:
- CopyTests
- Copy/PasteStack/PasteStackModel.swift
- Copy/Shelf/ShelfWheelScroll.swift
- Copy/PasteStack/PasteStackBatchConfirmation.swift
dependencies:
- package: CopyCore
Expand Down
Loading