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: Git view /// ⌘G or ⌘D: toggles the view, but over the search or the Diff it switches to it. /// Both full-screen views sit under `OverlayHost`, so a toggle beneath them would stay hidden. func toggleView(isOpen: Bool, open: () -> Void, close: () -> Void) { switch overlay.current { case .search, .diff: overlay.dismiss() open() default: isOpen ? close() : open() } } /// Opens the Git view on its message field, re-reads git and fetches origin; an open view only takes the field again. func openGitView() { if !layout.isGitViewOpen { layout.openGitView() scheduleRefresh(.git) if let sourceControl { track { await sourceControl.viewOpened() } } } focusRequest = FocusRequest(.commitMessage) } /// Hands the keyboard back to the column that had it before the view opened. func closeGitView() { guard layout.isGitViewOpen else { return } layout.closeGitView() guard let panel = layout.activePanel else { focusRequest = nil return } focusRequest = FocusRequest(.panel(panel)) if panel == .editor { editor?.requestTextFocus() } } // MARK: Schema view /// Opens the Schema view in a Laravel app and reads the command and dump; anywhere else it does nothing. func openSchemaView() { guard let schema, schema.isLaravelApp, !layout.isSchemaViewOpen else { return } layout.openSchemaView() focusRequest = nil track { await schema.opened() } } /// Hands the keyboard back to the column that had it before the view opened. func closeSchemaView() { guard layout.isSchemaViewOpen else { return } layout.closeSchemaView() guard let panel = layout.activePanel else { focusRequest = nil return } focusRequest = FocusRequest(.panel(panel)) if panel == .editor { editor?.requestTextFocus() } } // MARK: Focus and the row cursor func focus(_ panel: Panel) { layout.focus(panel) guard layout.activePanel == panel else { return } focusRequest = FocusRequest(.panel(panel)) if panel == .editor { editor?.requestTextFocus() } } func moveRowCursor(by step: Int) { guard layout.activePanel == .explorer else { return } explorer?.moveSelection(by: step, showHidden: layout.showHidden) } func openSelectedRow() { guard layout.activePanel == .explorer, let explorer, let row = selectedTreeRow() else { return } if row.row.kind == .directory { explorer.toggle(row.row.path) } else { open(row.row.path) } } // 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) } }