123 lines
4.4 KiB
Swift
123 lines
4.4 KiB
Swift
import CoreGraphics
|
|
import Foundation
|
|
import HelderCore
|
|
import HelderServices
|
|
|
|
extension WorkspaceModel {
|
|
// MARK: Overlays
|
|
|
|
func overlayChanged(from old: Overlay, to new: Overlay) {
|
|
switch old {
|
|
case .diff:
|
|
editor?.closeDiff()
|
|
default:
|
|
break
|
|
}
|
|
if new == .none, cleanCopy?.pending != nil {
|
|
Task { @MainActor [weak self] in self?.showCleanCopy() }
|
|
}
|
|
}
|
|
|
|
/// `.clean-copy` changed on disk: new text opens the popup, or waits for the open overlay to close.
|
|
func cleanCopyChanged() async {
|
|
guard let cleanCopy else { return }
|
|
let current = generation
|
|
guard await cleanCopy.fileChanged(), generation == current, self.cleanCopy === cleanCopy else { return }
|
|
showCleanCopy()
|
|
}
|
|
|
|
/// Shows the pending clean-copy text when nothing else floats over the workspace.
|
|
func showCleanCopy() {
|
|
guard let cleanCopy, cleanCopy.pending != nil else { return }
|
|
switch overlay.current {
|
|
case .none:
|
|
cleanCopy.takePending()
|
|
overlay.present(.cleanCopy)
|
|
case .cleanCopy:
|
|
cleanCopy.refreshOpenPopup()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
/// Copies the popup's text as plain text, closes it and confirms with a toast.
|
|
func copyCleanCopy() {
|
|
guard let cleanCopy, overlay.current == .cleanCopy else { return }
|
|
app.system.copy(cleanCopy.text)
|
|
overlay.dismiss()
|
|
toasts.show("Copied", detail: "\(cleanCopy.characterCount) characters")
|
|
}
|
|
|
|
/// The recent projects other than this one, for ⇧⌘O.
|
|
public var otherRecentProjects: [RecentProject] {
|
|
recentProjects.filter { $0.path != root?.path }
|
|
}
|
|
|
|
/// ⇧⌘O: the recent-project list; the recents are re-read, since another window may have added one.
|
|
func showRecentProjects() {
|
|
overlay.present(.launcher)
|
|
track {
|
|
self.recentProjects = await self.app.recentProjects.list()
|
|
}
|
|
}
|
|
|
|
func activateLauncher() {
|
|
switch launcher.selectedChoice {
|
|
case .chooseFolder: perform(.openFolder)
|
|
case let .open(path): perform(.openRecentProject(path))
|
|
case nil: break
|
|
}
|
|
}
|
|
|
|
// MARK: Focus and the row cursor
|
|
|
|
func focus(_ panel: Panel) {
|
|
let previous = layout.activePanel
|
|
layout.focus(panel)
|
|
guard layout.activePanel == panel else { return }
|
|
focusRequest = FocusRequest(.panel(panel))
|
|
if panel == .editor { editor?.requestTextFocus() }
|
|
if panel == .sourceControl && previous != .sourceControl { scheduleRefresh(.git) }
|
|
}
|
|
|
|
func moveRowCursor(by step: Int) {
|
|
switch layout.activePanel {
|
|
case .sourceControl: sourceControl?.moveSelection(by: step)
|
|
case .explorer: explorer?.moveSelection(by: step, showHidden: layout.showHidden)
|
|
default: break
|
|
}
|
|
}
|
|
|
|
func openSelectedRow() {
|
|
switch layout.activePanel {
|
|
case .sourceControl:
|
|
guard let row = selectedGitRow() else { return }
|
|
open(row.change.path, OpenOptions(side: row.change.staged ? .staged : .unstaged))
|
|
case .explorer:
|
|
guard let explorer, let row = selectedTreeRow() else { return }
|
|
if row.row.kind == .directory { explorer.toggle(row.row.path) } else { open(row.row.path) }
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
// MARK: Editor
|
|
|
|
func passSelection(_ info: TextSelectionInfo?) {
|
|
guard let editor, let path = editor.activePath else { return }
|
|
if let info {
|
|
let reference = AgentReference(path: path, lines: info.startLine...max(info.startLine, info.endLine))
|
|
overlay.present(.passOn(PassOnRequest(reference: reference.description, code: info.text, anchor: defaultAnchor)))
|
|
} else if let request = editor.passOnSelectionRequest(anchor: defaultAnchor) {
|
|
overlay.present(.passOn(request))
|
|
}
|
|
}
|
|
|
|
func editorSelectionFirstLine() -> String {
|
|
guard let editor, let path = editor.activePath, let selection = editor.selection, selection.path == path else { return "" }
|
|
let lines = editor.text(for: path).split(separator: "\n", omittingEmptySubsequences: false)
|
|
guard selection.start >= 1, selection.start <= lines.count else { return "" }
|
|
return lines[selection.start - 1].trimmingCharacters(in: .whitespaces)
|
|
}
|
|
}
|