restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import HelderCore
|
||||
import SwiftUI
|
||||
|
||||
/// The words and target path of the new-file, new-folder and rename prompt.
|
||||
struct NamePromptText: Equatable {
|
||||
var purpose: NamePromptRequest.Purpose
|
||||
var title: String
|
||||
var placeholder: String
|
||||
var previewLabel: String
|
||||
/// The footer's "create file" / "rename folder".
|
||||
var action: String
|
||||
var initialName: String
|
||||
var isFolder: Bool
|
||||
var isRename: Bool
|
||||
|
||||
init(_ request: NamePromptRequest) {
|
||||
purpose = request.purpose
|
||||
switch request.purpose {
|
||||
case .newFile: (isFolder, isRename) = (false, false)
|
||||
case .newFolder: (isFolder, isRename) = (true, false)
|
||||
case let .rename(_, isDirectory): (isFolder, isRename) = (isDirectory, true)
|
||||
}
|
||||
initialName = request.initialName
|
||||
let noun = isFolder ? "folder" : "file"
|
||||
title = isRename ? "Rename \(noun)" : (isFolder ? "New folder" : "New file")
|
||||
placeholder = isFolder ? "folder name…" : "file name…"
|
||||
previewLabel = isRename ? "renames to" : "creates"
|
||||
action = (isRename ? "rename " : "create ") + noun
|
||||
}
|
||||
|
||||
/// The path a submit would produce, from the same rule the Explorer applies.
|
||||
func preview(for name: String) -> String {
|
||||
ExplorerStore.promptTarget(for: purpose, name: name) ?? "…"
|
||||
}
|
||||
|
||||
/// ↵ does nothing for an empty or invalid name, or an unchanged rename.
|
||||
func canSubmit(_ name: String) -> Bool {
|
||||
ExplorerStore.promptTarget(for: purpose, name: name) != nil
|
||||
}
|
||||
|
||||
/// A rename selects the stem only, so typing keeps the extension.
|
||||
var initialSelection: NSRange {
|
||||
let name = initialName as NSString
|
||||
let dot = name.range(of: ".", options: .backwards).location
|
||||
let stem = isRename && dot != NSNotFound && dot > 0 ? dot : name.length
|
||||
return NSRange(location: 0, length: stem)
|
||||
}
|
||||
}
|
||||
|
||||
/// A small anchored prompt for naming a new entry or renaming one.
|
||||
struct NamePromptOverlay: View {
|
||||
let request: NamePromptRequest
|
||||
var onSubmit: (String) -> Void
|
||||
var onClose: () -> Void
|
||||
|
||||
@State private var name: String
|
||||
private let text: NamePromptText
|
||||
|
||||
init(request: NamePromptRequest, onSubmit: @escaping (String) -> Void, onClose: @escaping () -> Void) {
|
||||
self.request = request
|
||||
self.onSubmit = onSubmit
|
||||
self.onClose = onClose
|
||||
text = NamePromptText(request)
|
||||
_name = State(initialValue: request.initialName)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { proxy in
|
||||
ZStack(alignment: .topLeading) {
|
||||
Color.clear.contentShape(Rectangle()).onTapGesture(perform: onClose)
|
||||
prompt.offset(position(in: proxy.size))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var prompt: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
HStack(spacing: 10) {
|
||||
Image(systemName: text.isFolder ? "folder" : "doc")
|
||||
.font(.system(size: 13, weight: .light))
|
||||
.foregroundStyle(Palette.amber)
|
||||
Text(text.title).typeRole(TypeRole(.monoBold, 13)).foregroundStyle(Palette.text1)
|
||||
Spacer()
|
||||
EscChip()
|
||||
}
|
||||
OverlayCommandField(text: $name, placeholder: text.placeholder, font: TypeRole(.monoRegular, 13).nsFont,
|
||||
initialSelection: text.initialSelection, onKey: handle)
|
||||
.padding(.horizontal, 10)
|
||||
.frame(height: 38)
|
||||
.background(Palette.editor, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.focusRing(true)
|
||||
HStack(spacing: 10) {
|
||||
Text(text.previewLabel).typeRole(Typography.panelLabel).foregroundStyle(Palette.text4)
|
||||
Text(text.preview(for: name))
|
||||
.typeRole(Typography.path)
|
||||
.foregroundStyle(Palette.ink)
|
||||
.lineLimit(1)
|
||||
.truncationMode(.middle)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(Palette.amber, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
}
|
||||
OverlayFooter.hint([("↵", text.action), ("esc", "cancel")])
|
||||
.typeRole(Typography.chip)
|
||||
}
|
||||
.padding(14)
|
||||
.frame(width: Metrics.passOnWidth)
|
||||
.background(Palette.panel, in: RoundedRectangle(cornerRadius: Metrics.radiusMedium))
|
||||
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusMedium).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
||||
.shadow(color: Palette.menuShadow, radius: Metrics.menuShadowRadius / 2, y: Metrics.menuShadowY)
|
||||
}
|
||||
|
||||
private func position(in size: CGSize) -> CGSize {
|
||||
let x = min(request.anchor.x, size.width - Metrics.passOnWidth - 8)
|
||||
let y = min(request.anchor.y + 6, size.height - 150)
|
||||
return CGSize(width: max(8, x), height: max(8, y))
|
||||
}
|
||||
|
||||
private func handle(_ key: OverlayKey) -> Bool {
|
||||
switch key {
|
||||
case .enter:
|
||||
// The model closes the prompt itself; closing here first would drop the request it answers.
|
||||
if text.canSubmit(name) { onSubmit(name.trimmingCharacters(in: .whitespacesAndNewlines)) }
|
||||
case .escape: onClose()
|
||||
default: return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user