Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Terminal/RowSelection.swift3// Description: A multiple selection over an ordered list of ids (click, ⌘-click, ⇧-click, arrow keys) and the reordering of a list by dragging selected rows. `List(selection:)` does not select on macOS 27, so the app owns this model.4import Foundation56/// The selected rows of an ordered list. The anchor is the row a ⇧-click extends from.7public struct RowSelection: Sendable, Equatable {8 public private(set) var ids: Set<String> = []9 public private(set) var anchor: String?10 /// The end of the range that moves with ⇧-arrows.11 public private(set) var cursor: String?1213 public init() {}14 public var isEmpty: Bool { ids.isEmpty }15 public var count: Int { ids.count }16 public func contains(_ id: String) -> Bool { ids.contains(id) }1718 /// Plain click: this row only.19 public mutating func click(_ id: String) { ids = [id]; anchor = id; cursor = id }2021 /// ⌘-click: adds or removes this row; the others stay.22 public mutating func toggle(_ id: String) {23 if ids.contains(id) { ids.remove(id); if anchor == id { anchor = nil }; cursor = anchor }24 else { ids.insert(id); anchor = id; cursor = id }25 }2627 /// ⇧-click: the range between the anchor and this row replaces the selection (the anchor stays).28 public mutating func extend(to id: String, in order: [String]) {29 guard let a = anchor, let i = order.firstIndex(of: a), let j = order.firstIndex(of: id) else { click(id); return }30 ids = Set(order[min(i, j)...max(i, j)]); cursor = id31 }3233 public mutating func selectAll(_ order: [String]) { ids = Set(order); anchor = order.first; cursor = order.last }34 public mutating func clear() { ids = []; anchor = nil; cursor = nil }3536 /// Drops ids that are no longer in the list (a row removed here or on another device).37 public mutating func prune(to order: [String]) {38 let live = Set(order)39 ids.formIntersection(live)40 if let a = anchor, !live.contains(a) { anchor = nil }41 if let c = cursor, !live.contains(c) { cursor = anchor }42 }4344 /// ↑ / ↓ (delta −1 / +1): moves to the neighbouring row; with ⇧ the range grows or shrinks from the anchor.45 public mutating func move(by delta: Int, in order: [String], extending: Bool = false) {46 guard !order.isEmpty else { return }47 guard let from = (cursor ?? anchor).flatMap({ order.firstIndex(of: $0) }) else { click(delta >= 0 ? order[0] : order[order.count - 1]); return }48 let target = order[min(order.count - 1, max(0, from + delta))]49 if extending { extend(to: target, in: order) } else { click(target) }50 }5152 /// The selected ids in the order of the list.53 public func ordered(in order: [String]) -> [String] { order.filter(ids.contains) }54}5556public enum ListReorder {57 /// Moves `moving` (kept in their relative order) so that they sit just before `target`; a nil target moves them58 /// to the end. Dropping a selection onto one of its own rows changes nothing.59 public static func move(_ items: [String], moving: Set<String>, before target: String?) -> [String] {60 let moved = items.filter(moving.contains)61 guard !moved.isEmpty else { return items }62 if let target, moving.contains(target) { return items }63 var rest = items.filter { !moving.contains($0) }64 let at = target.flatMap { rest.firstIndex(of: $0) } ?? rest.count65 rest.insert(contentsOf: moved, at: at)66 return rest67 }6869 /// The same move expressed with the row the pointer is over and the half of it (drop above or below).70 public static func move(_ items: [String], moving: Set<String>, onto row: String, below: Bool) -> [String] {71 guard below else { return move(items, moving: moving, before: row) }72 guard let i = items.firstIndex(of: row) else { return items }73 // the first row after `row` that is not being moved74 let next = items[(i + 1)...].first { !moving.contains($0) }75 if moving.contains(row) { return items }76 return move(items, moving: moving, before: next)77 }78}79