118 lines
4.5 KiB
Swift
118 lines
4.5 KiB
Swift
import AppKit
|
|
import HelderCore
|
|
import SwiftUI
|
|
|
|
/// ⇧⌘O: the other recent projects; ↵ or a click switches this window to one.
|
|
struct RecentProjectsOverlay: View {
|
|
let model: WorkspaceModel
|
|
|
|
@State private var selection = 0
|
|
@State private var parkedPointer: CGPoint?
|
|
|
|
private var projects: [RecentProject] { model.otherRecentProjects }
|
|
|
|
var body: some View {
|
|
OverlayShell(.sized(width: 560), onDismiss: close) {
|
|
VStack(spacing: 0) {
|
|
OverlayHeader(systemImage: "folder", title: "Open recent project", iconColor: Palette.text4, showsEscChip: false) {
|
|
HStack(spacing: 4) {
|
|
Text("\(projects.count) project\(projects.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 projects.isEmpty {
|
|
Text("No other recent projects")
|
|
.typeRole(Typography.tooltip)
|
|
.foregroundStyle(Palette.text4)
|
|
.padding(EdgeInsets(top: 12, leading: 14, bottom: 12, trailing: 14))
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
ForEach(Array(projects.enumerated()), id: \.element.id) { index, project in
|
|
row(project, selected: index == selection)
|
|
.id(index)
|
|
.onHover { inside in if inside { hover(index) } }
|
|
.onTapGesture { open(project) }
|
|
}
|
|
}
|
|
.padding(.vertical, 6)
|
|
}
|
|
.frame(height: min(460, CGFloat(max(projects.count, 1)) * Metrics.launcherRow + 12 + (projects.isEmpty ? 12 : 0)))
|
|
.onChange(of: selection) { _, row in proxy.scrollTo(row) }
|
|
}
|
|
}
|
|
|
|
private func row(_ project: RecentProject, selected: Bool) -> some View {
|
|
HStack(spacing: 10) {
|
|
Image(systemName: "folder")
|
|
.font(.system(size: 13, weight: .light))
|
|
.foregroundStyle(selected ? Palette.amber : Palette.text4)
|
|
.frame(width: 16)
|
|
VStack(alignment: .leading, spacing: 1) {
|
|
Text(project.name)
|
|
.typeRole(Typography.rowSelected)
|
|
.foregroundStyle(selected ? Palette.text1 : Palette.text2)
|
|
.lineLimit(1)
|
|
Text(project.path)
|
|
.typeRole(Typography.meta)
|
|
.foregroundStyle(Palette.text4)
|
|
.lineLimit(1)
|
|
.truncationMode(.head)
|
|
}
|
|
Spacer(minLength: 0)
|
|
}
|
|
.padding(.horizontal, 14)
|
|
.frame(height: Metrics.launcherRow)
|
|
.contentShape(Rectangle())
|
|
.selectableRow(isSelected: selected)
|
|
.help(project.path)
|
|
}
|
|
|
|
private func handle(_ key: OverlayKey) -> Bool {
|
|
switch key {
|
|
case .up, .commandUp: park(); selection = ListCursor.step(selection, by: -1, count: projects.count)
|
|
case .down, .commandDown: park(); selection = ListCursor.step(selection, by: 1, count: projects.count)
|
|
case .enter: if projects.indices.contains(selection) { open(projects[selection]) }
|
|
case .escape: close()
|
|
default: return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// Arrow keys park the pointer, so a list scrolling under a still mouse does not steal the selection.
|
|
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 close() {
|
|
model.perform(.dismissOverlay)
|
|
}
|
|
|
|
private func open(_ project: RecentProject) {
|
|
close()
|
|
model.perform(.openRecentProject(project.path))
|
|
}
|
|
}
|