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
18 changes: 14 additions & 4 deletions Sources/Pesty/Store/ClipboardStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ final class ClipboardStore {
var searchText: String = "" {
didSet { if searchText != oldValue { clearMultiSelection() } }
}
var selectedID: UUID?
var selectedID: UUID? {
willSet { selectionIsPointerDriven = false }
}
/// Mouse selection must leave the card under the pointer for a second
/// click. Keyboard and programmatic selection still reposition the strip.
private(set) var selectionIsPointerDriven = false
private(set) var multiSelectedIDs: Set<UUID> = []
private var selectionAnchorID: UUID?

Expand Down Expand Up @@ -85,14 +90,16 @@ final class ClipboardStore {
/// Settings can report how much space history actually uses.
var dataDirectory: URL { baseDir }

private init() {
let base = (Settings.shared.iCloudSync ? ClipboardStore.iCloudBase : nil) ?? ClipboardStore.localBase
init(baseDirectory: URL? = nil) {
let base = baseDirectory
?? (Settings.shared.iCloudSync ? ClipboardStore.iCloudBase : nil)
?? ClipboardStore.localBase
baseDir = base
imagesDir = base.appendingPathComponent("images", isDirectory: true)
storeURL = base.appendingPathComponent("store.json")
prepareDirectories()
load()
if Settings.shared.iCloudSync { startWatching() }
if baseDirectory == nil && Settings.shared.iCloudSync { startWatching() }
}

private func prepareDirectories() {
Expand Down Expand Up @@ -533,6 +540,7 @@ final class ClipboardStore {
selectedID = id
selectionAnchorID = id
multiSelectedIDs = []
selectionIsPointerDriven = true
}

func toggleSelection(_ id: UUID) {
Expand All @@ -552,6 +560,7 @@ final class ClipboardStore {
if selectionAnchorID == nil { selectionAnchorID = id }
if multiSelectedIDs.count == 1 { multiSelectedIDs = [] }
}
selectionIsPointerDriven = true
}

func extendSelection(to id: UUID) {
Expand All @@ -567,6 +576,7 @@ final class ClipboardStore {
selectedID = id
selectionAnchorID = anchor
if multiSelectedIDs.count <= 1 { multiSelectedIDs = [] }
selectionIsPointerDriven = true
}

func clearMultiSelection() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pesty/UI/BarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ struct BarView: View {
}
}
.onChange(of: store.selectedID) { _, id in
guard let id else { return }
guard let id, !store.selectionIsPointerDriven else { return }
scrollToSelected(id, proxy: proxy)
}
.onChange(of: settings.selectedClipPosition) { _, _ in
Expand Down
32 changes: 32 additions & 0 deletions Sources/Pesty/UI/ClipCardInteractions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import AppKit
import SwiftUI

/// Select on the first click without waiting for the double-click recognizer.
/// A second click can still paste; modified clicks only change the selection.
struct ClipCardInteractions: ViewModifier {
let select: () -> Void
let extendSelection: () -> Void
let toggleSelection: () -> Void
let paste: () -> Void

private var modifiers: NSEvent.ModifierFlags {
NSApp.currentEvent?.modifierFlags ?? NSEvent.modifierFlags
}

func body(content: Content) -> some View {
content
.onTapGesture(count: 2) {
guard modifiers.intersection([.command, .shift]).isEmpty else { return }
paste()
}
.simultaneousGesture(TapGesture().onEnded {
if modifiers.contains(.command) {
toggleSelection()
} else if modifiers.contains(.shift) {
extendSelection()
} else {
select()
}
})
}
}
9 changes: 5 additions & 4 deletions Sources/Pesty/UI/ClipCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ struct ClipCardView: View {
.animation(.easeOut(duration: 0.14), value: hovering)
.contentShape(Rectangle())
.onHover { hovering = $0 }
.onTapGesture(count: 2) { AppController.shared.pasteItem(item) }
.onTapGesture { store.select(item.id) }
.highPriorityGesture(TapGesture().modifiers(.shift).onEnded { store.extendSelection(to: item.id) })
.highPriorityGesture(TapGesture().modifiers(.command).onEnded { store.toggleSelection(item.id) })
.modifier(ClipCardInteractions(
select: { store.select(item.id) },
extendSelection: { store.extendSelection(to: item.id) },
toggleSelection: { store.toggleSelection(item.id) },
paste: { AppController.shared.pasteItem(item) }))
.onDrag { ClipDragProvider.make(for: item) }
.contextMenu { menu }
.task(id: item.id) { await loadPreview() }
Expand Down
58 changes: 58 additions & 0 deletions Tests/PestyTests/ClipboardSelectionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import XCTest
@testable import Pesty

@MainActor
final class ClipboardSelectionTests: XCTestCase {
private func withStore(_ body: (ClipboardStore, [ClipItem]) -> Void) throws {
let directory = FileManager.default.temporaryDirectory
.appendingPathComponent("pesty-selection-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: directory) }
let store = ClipboardStore(baseDirectory: directory)
let items = (0..<4).map { ClipItem(type: .text, text: "Clip \($0)") }
for item in items.reversed() { store.addCaptured(item) }
body(store, items)
}

func testClickKeepsCardInPlaceAndArrowNavigationResumesScrolling() throws {
try withStore { store, items in
store.select(items[1].id)
XCTAssertEqual(store.selectedID, items[1].id)
XCTAssertTrue(store.selectionIsPointerDriven)

store.moveSelection(by: 1)
XCTAssertEqual(store.selectedID, items[2].id)
XCTAssertFalse(store.selectionIsPointerDriven)
}
}

func testModifiedClicksPreserveMultiSelectionWithoutScrolling() throws {
try withStore { store, items in
store.select(items[0].id)
store.extendSelection(to: items[2].id)
XCTAssertEqual(store.multiSelectedIDs, Set(items.prefix(3).map(\.id)))
XCTAssertTrue(store.selectionIsPointerDriven)

store.toggleSelection(items[1].id)
XCTAssertEqual(store.multiSelectedIDs, Set([items[0].id, items[2].id]))
XCTAssertTrue(store.selectionIsPointerDriven)

store.moveSelection(by: 1)
XCTAssertEqual(store.selectedID, items[3].id)
XCTAssertTrue(store.multiSelectedIDs.isEmpty)
XCTAssertFalse(store.selectionIsPointerDriven)
}
}

func testProgrammaticSelectionResumesScrollingAfterClick() throws {
try withStore { store, items in
store.select(items[2].id)
store.selectFirst()
XCTAssertEqual(store.selectedID, items[0].id)
XCTAssertFalse(store.selectionIsPointerDriven)

store.select(items[3].id)
store.selectedID = items[1].id
XCTAssertFalse(store.selectionIsPointerDriven)
}
}
}