137 lines
5.2 KiB
Swift
137 lines
5.2 KiB
Swift
import AppKit
|
|
import HelderCore
|
|
import SwiftUI
|
|
|
|
/// Keyboard movement through a plain list, clamped at both ends.
|
|
enum ListCursor {
|
|
static func step(_ current: Int, by step: Int, count: Int) -> Int {
|
|
guard count > 0 else { return 0 }
|
|
return min(max(current + step, 0), count - 1)
|
|
}
|
|
|
|
/// ⌘↓ from the editor starts on the previous file (row 1), ⌘↑ on the current one.
|
|
static func recentFilesStart(movingDown: Bool, count: Int) -> Int {
|
|
movingDown ? max(0, min(1, count - 1)) : 0
|
|
}
|
|
}
|
|
|
|
/// ⌘↑/⌘↓ from the editor: the recently opened files, newest first.
|
|
struct RecentFilesOverlay: View {
|
|
let history: [String]
|
|
var changedPaths: Set<String>
|
|
var onOpen: (String) -> Void
|
|
var onClose: () -> Void
|
|
|
|
@State private var selection: Int
|
|
@State private var parkedPointer: CGPoint?
|
|
|
|
init(history: [String], initialSelection: Int, changedPaths: Set<String>,
|
|
onOpen: @escaping (String) -> Void, onClose: @escaping () -> Void) {
|
|
self.history = history
|
|
self.changedPaths = changedPaths
|
|
self.onOpen = onOpen
|
|
self.onClose = onClose
|
|
_selection = State(initialValue: ListCursor.step(initialSelection, by: 0, count: history.count))
|
|
}
|
|
|
|
var body: some View {
|
|
OverlayShell(.sized(width: 560), onDismiss: onClose) {
|
|
VStack(spacing: 0) {
|
|
OverlayHeader(systemImage: "doc", title: "Recent files", iconColor: Palette.text4, showsEscChip: false) {
|
|
HStack(spacing: 4) {
|
|
Text("\(history.count) file\(history.count == 1 ? "" : "s") ·")
|
|
Keycap("⌘↓")
|
|
Keycap("⌘↑")
|
|
Keycap("↵")
|
|
}
|
|
.typeRole(Typography.path)
|
|
.foregroundStyle(Palette.text4)
|
|
.padding(.horizontal, 6)
|
|
.padding(.vertical, 4)
|
|
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
|
}
|
|
list
|
|
}
|
|
.background(OverlayKeyCatcher(onKey: handle))
|
|
}
|
|
}
|
|
|
|
private var list: some View {
|
|
ScrollViewReader { proxy in
|
|
ScrollView {
|
|
LazyVStack(spacing: 0) {
|
|
if history.isEmpty {
|
|
Text("No files opened yet")
|
|
.typeRole(Typography.tooltip)
|
|
.foregroundStyle(Palette.text4)
|
|
.padding(EdgeInsets(top: 12, leading: 14, bottom: 12, trailing: 14))
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
ForEach(Array(history.enumerated()), id: \.element) { index, path in
|
|
row(path, selected: index == selection)
|
|
.id(index)
|
|
.onHover { inside in if inside { hover(index) } }
|
|
.pointerHand()
|
|
.onTapGesture { open(path) }
|
|
}
|
|
}
|
|
.padding(.vertical, 6)
|
|
}
|
|
.frame(height: min(460, CGFloat(max(history.count, 1)) * Metrics.launcherRow + 12 + (history.isEmpty ? 12 : 0)))
|
|
.onChange(of: selection) { _, row in proxy.scrollTo(row) }
|
|
}
|
|
}
|
|
|
|
private func row(_ path: String, selected: Bool) -> some View {
|
|
let directory = PathUtils.dirname(path)
|
|
return HStack(spacing: 10) {
|
|
TypeBadge(path: path)
|
|
VStack(alignment: .leading, spacing: 1) {
|
|
Text(PathUtils.basename(path))
|
|
.typeRole(Typography.rowSelected)
|
|
.foregroundStyle(selected ? Palette.text1 : Palette.text3)
|
|
.lineLimit(1)
|
|
Text(directory.isEmpty ? "" : directory + "/")
|
|
.typeRole(Typography.meta)
|
|
.foregroundStyle(Palette.text4)
|
|
.lineLimit(1)
|
|
.truncationMode(.head)
|
|
}
|
|
Spacer(minLength: 0)
|
|
if changedPaths.contains(path) {
|
|
Text("•").typeRole(Typography.statusLetter).foregroundStyle(Palette.changedDot)
|
|
}
|
|
}
|
|
.padding(.horizontal, 14)
|
|
.frame(height: Metrics.launcherRow)
|
|
.contentShape(Rectangle())
|
|
.selectableRow(isSelected: selected)
|
|
}
|
|
|
|
private func handle(_ key: OverlayKey) -> Bool {
|
|
switch key {
|
|
case .up, .commandUp: park(); selection = ListCursor.step(selection, by: -1, count: history.count)
|
|
case .down, .commandDown: park(); selection = ListCursor.step(selection, by: 1, count: history.count)
|
|
case .enter: if history.indices.contains(selection) { open(history[selection]) }
|
|
case .escape: onClose()
|
|
default: return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
private func park() {
|
|
parkedPointer = NSEvent.mouseLocation
|
|
}
|
|
|
|
private func hover(_ index: Int) {
|
|
if let parked = parkedPointer, parked == NSEvent.mouseLocation { return }
|
|
parkedPointer = nil
|
|
selection = index
|
|
}
|
|
|
|
private func open(_ path: String) {
|
|
onClose()
|
|
onOpen(path)
|
|
}
|
|
}
|