restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
|
||||
extension WorkspaceModel {
|
||||
/// Runs a context-menu action; one the config or the Explorer guards asks first through the confirm overlay.
|
||||
public func perform(_ action: ContextAction) {
|
||||
let anchor = takeMenuAnchor()
|
||||
if let request = sourceControl?.confirmation(for: action) ?? explorer?.confirmation(for: action) {
|
||||
overlay.present(.confirm(request))
|
||||
} else {
|
||||
execute(action, anchor: anchor)
|
||||
}
|
||||
}
|
||||
|
||||
func showContextMenu(_ target: ContextTarget, anchor: CGPoint) {
|
||||
overlay.present(.contextMenu(ContextMenuRequest(
|
||||
target: target,
|
||||
header: ContextActions.header(for: target, projectName: name),
|
||||
entries: ContextActions.entries(for: target),
|
||||
anchor: anchor
|
||||
)))
|
||||
}
|
||||
|
||||
/// Closes an open context menu and returns where it stood, so a prompt it opens appears in the same place.
|
||||
private func takeMenuAnchor() -> CGPoint {
|
||||
guard case let .contextMenu(menu) = overlay.current else { return defaultAnchor }
|
||||
overlay.dismiss()
|
||||
return menu.anchor
|
||||
}
|
||||
|
||||
var defaultAnchor: CGPoint {
|
||||
CGPoint(x: (layout.span(of: .editor)?.lowerBound ?? 0) + 60, y: 150)
|
||||
}
|
||||
|
||||
func confirmPending() {
|
||||
guard case let .confirm(request) = overlay.current else { return }
|
||||
overlay.dismiss()
|
||||
execute(request.action, anchor: defaultAnchor)
|
||||
}
|
||||
|
||||
// MARK: Row menus
|
||||
|
||||
/// ⌘→ in Source Control or Explorer: the selected row's menu, beside the column at the row's height.
|
||||
func openRowMenu() {
|
||||
switch layout.activePanel {
|
||||
case .sourceControl:
|
||||
guard let row = selectedGitRow() else { return }
|
||||
showContextMenu(.gitRow(path: row.change.path, staged: row.change.staged),
|
||||
anchor: rowMenuAnchor(.sourceControl, frame: rowFrames[.git(row.change.id)],
|
||||
estimate: row.index, rowHeight: Metrics.sourceControlRow))
|
||||
case .explorer:
|
||||
guard let row = selectedTreeRow() else { return }
|
||||
showContextMenu(ExplorerStore.target(for: row.row),
|
||||
anchor: rowMenuAnchor(.explorer, frame: rowFrames[.tree(row.row.path)],
|
||||
estimate: row.index, rowHeight: Metrics.explorerRow))
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func selectedGitRow() -> (change: Change, index: Int)? {
|
||||
guard let sourceControl else { return nil }
|
||||
let rows = sourceControl.rows
|
||||
guard let index = rows.firstIndex(where: { $0.id == sourceControl.selectedRowID }) else { return nil }
|
||||
return (rows[index], index)
|
||||
}
|
||||
|
||||
func selectedTreeRow() -> (row: TreeRow, index: Int)? {
|
||||
guard let explorer else { return nil }
|
||||
let rows = explorer.rows(showHidden: layout.showHidden)
|
||||
guard let index = rows.firstIndex(where: { $0.path == explorer.selectedPath }) else { return nil }
|
||||
return (rows[index], index)
|
||||
}
|
||||
|
||||
/// Row menus open beside the column, never over its rows, level with the row; a row that never
|
||||
/// reported its frame is placed by its index, which is off once the list scrolls.
|
||||
func rowMenuAnchor(_ panel: Panel, frame: CGRect?, estimate index: Int, rowHeight: CGFloat) -> CGPoint {
|
||||
let x = layout.span(of: panel)?.upperBound ?? 220
|
||||
let y = frame.map { $0.minY + 4 } ?? Metrics.titleBar + Metrics.panelHeader + CGFloat(index) * rowHeight + 4
|
||||
return CGPoint(x: x, y: y)
|
||||
}
|
||||
|
||||
// MARK: Running actions
|
||||
|
||||
/// Runs an action that needs no (more) confirmation. The stores are read before any await,
|
||||
/// so an action still running after a project switch never lands in the new project.
|
||||
func execute(_ action: ContextAction, anchor: CGPoint) {
|
||||
let sourceControl = sourceControl, explorer = explorer, editor = editor
|
||||
switch action {
|
||||
case let .passOnReference(reference, code):
|
||||
overlay.present(.passOn(PassOnRequest(reference: reference, code: code, anchor: anchor)))
|
||||
case let .passOnFileName(name):
|
||||
overlay.present(.passOn(PassOnRequest(reference: name, anchor: anchor)))
|
||||
case let .newFile(directory):
|
||||
overlay.present(.namePrompt(NamePromptRequest(purpose: .newFile(inDirectory: directory), anchor: anchor)))
|
||||
case let .newFolder(directory):
|
||||
overlay.present(.namePrompt(NamePromptRequest(purpose: .newFolder(inDirectory: directory), anchor: anchor)))
|
||||
case let .rename(path, isDirectory):
|
||||
overlay.present(.namePrompt(NamePromptRequest(purpose: .rename(path: path, isDirectory: isDirectory),
|
||||
initialName: PathUtils.basename(path), anchor: anchor)))
|
||||
case let .openDiff(path):
|
||||
open(path, OpenOptions(showDiff: true))
|
||||
case let .copyTerminalSelection(kind):
|
||||
if let text = terminal.selection(kind) { app.system.copy(text) }
|
||||
case let .pasteIntoTerminal(kind):
|
||||
if let text = app.system.pasteboardText() { terminal.paste(text, into: kind) }
|
||||
case let .discard(path):
|
||||
track {
|
||||
await sourceControl?.discard(path)
|
||||
await editor?.reloadFromDisk(path)
|
||||
}
|
||||
case let .delete(path, isDirectory):
|
||||
track {
|
||||
guard await explorer?.deleteEntry(path, isDirectory: isDirectory) == true else { return }
|
||||
editor?.forget(path, isDirectory: isDirectory)
|
||||
await sourceControl?.refresh()
|
||||
}
|
||||
default:
|
||||
track {
|
||||
if await sourceControl?.perform(action) == true { return }
|
||||
await explorer?.perform(action)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func submitName(_ name: String) {
|
||||
guard case let .namePrompt(request) = overlay.current else { return }
|
||||
overlay.dismiss()
|
||||
guard let explorer else { return }
|
||||
let sourceControl = sourceControl, editor = editor
|
||||
track {
|
||||
guard let path = await explorer.submitName(name, for: request.purpose) else { return }
|
||||
switch request.purpose {
|
||||
case .newFile: if self.editor === editor { self.open(path) }
|
||||
case .newFolder: break
|
||||
case let .rename(old, isDirectory): editor?.remap(from: old, to: path, isDirectory: isDirectory)
|
||||
}
|
||||
await sourceControl?.refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user