adds Clean Copy Support

This commit is contained in:
2026-10-12 09:43:57 +02:00
parent 188e078055
commit 281f5465a6
28 changed files with 579 additions and 120 deletions
@@ -0,0 +1,43 @@
import SwiftUI
/// Text the agent wrote to `.clean-copy`: edit it, then copy it as plain text.
struct CleanCopyOverlay: View {
let store: CleanCopyStore
var onCopy: () -> Void
var onClose: () -> Void
var body: some View {
OverlayShell(.sized(width: 780), onDismiss: onClose) {
VStack(spacing: 0) {
OverlayHeader(systemImage: "doc.on.clipboard", title: "From Claude", detail: CleanCopyStore.fileName) {
Button(action: onCopy) {
HStack(spacing: 8) {
Text("Copy")
Keycap("⌘↩", style: .onAmber, small: true)
}
}
.buttonStyle(.primary)
}
OverlayTextEditor(text: Binding(get: { store.text }, set: { store.text = $0 }), onKey: handle)
.frame(maxWidth: .infinity, maxHeight: .infinity)
Text("copied as plain text · \(store.characterCount) characters")
.typeRole(Typography.meta)
.foregroundStyle(Palette.text4)
.padding(.horizontal, 20)
.frame(maxWidth: .infinity, alignment: .leading)
.frame(height: 34)
.hairline(.top)
}
.frame(maxHeight: .infinity)
}
}
private func handle(_ key: OverlayKey) -> Bool {
switch key {
case .commandEnter: onCopy()
case .escape: onClose()
default: return false
}
return true
}
}
+2 -97
View File
@@ -1,4 +1,3 @@
import AppKit
import SwiftUI
/// ⌘N: the project's plain-text note. Closing it in any way saves it.
@@ -19,7 +18,8 @@ struct NoteOverlay: View {
}
.buttonStyle(.primary)
}
NoteEditor(text: Binding(get: { note.text }, set: { note.text = $0 }), onKey: handle)
OverlayTextEditor(text: Binding(get: { note.text }, set: { note.text = $0 }),
placeholder: "Anything you want to keep next to this project…", onKey: handle)
.frame(maxWidth: .infinity, maxHeight: .infinity)
Text("saved on focus loss · \(note.characterCount) characters")
.typeRole(Typography.meta)
@@ -43,98 +43,3 @@ struct NoteOverlay: View {
return true
}
}
/// Mono 14/1.7 plain-text editor with an amber caret, focused with the caret at the end.
private struct NoteEditor: NSViewRepresentable {
@Binding var text: String
var onKey: (OverlayKey) -> Bool
func makeCoordinator() -> Coordinator { Coordinator(self) }
func makeNSView(context: Context) -> NSScrollView {
let scroll = NSScrollView()
scroll.drawsBackground = false
scroll.hasVerticalScroller = true
scroll.autohidesScrollers = true
let textView = NoteTextView(frame: .zero)
textView.pointerLayer = .overlay
textView.isVerticallyResizable = true
textView.isHorizontallyResizable = false
textView.autoresizingMask = [.width]
textView.textContainer?.widthTracksTextView = true
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.fillsClipView = true
scroll.documentView = textView
let role = Typography.note
let paragraph = NSMutableParagraphStyle()
paragraph.minimumLineHeight = role.lineHeight ?? 23.8
paragraph.maximumLineHeight = role.lineHeight ?? 23.8
textView.drawsBackground = false
textView.isRichText = false
textView.allowsUndo = true
textView.isAutomaticQuoteSubstitutionEnabled = false
textView.isAutomaticDashSubstitutionEnabled = false
textView.isAutomaticSpellingCorrectionEnabled = false
textView.isContinuousSpellCheckingEnabled = false
textView.font = role.nsFont
textView.textColor = NSColor(hex: 0xE4E7E6)
textView.insertionPointColor = NSColor(hex: 0xE8913A)
textView.defaultParagraphStyle = paragraph
textView.typingAttributes = [.font: role.nsFont, .foregroundColor: NSColor(hex: 0xE4E7E6), .paragraphStyle: paragraph]
textView.textContainerInset = NSSize(width: 20, height: 18)
textView.string = text
textView.delegate = context.coordinator
textView.onKey = onKey
textView.placeholder = "Anything you want to keep next to this project…"
DispatchQueue.main.async {
guard let window = textView.window, window.makeFirstResponder(textView) else { return }
textView.setSelectedRange(NSRange(location: (textView.string as NSString).length, length: 0))
textView.scrollRangeToVisible(textView.selectedRange())
}
return scroll
}
func updateNSView(_ scroll: NSScrollView, context: Context) {
context.coordinator.parent = self
guard let textView = scroll.documentView as? NoteTextView else { return }
textView.onKey = onKey
if textView.string != text { textView.string = text }
}
final class Coordinator: NSObject, NSTextViewDelegate {
var parent: NoteEditor
init(_ parent: NoteEditor) {
self.parent = parent
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
parent.text = textView.string
textView.needsDisplay = true
}
func textView(_ textView: NSTextView, doCommandBy selector: Selector) -> Bool {
selector == #selector(NSResponder.cancelOperation(_:)) ? parent.onKey(.escape) : false
}
}
}
/// Catches ⌘P before the menu bar's ⌘P (push) and draws the placeholder.
private final class NoteTextView: PointerBoundTextView {
var onKey: (OverlayKey) -> Bool = { _ in false }
var placeholder = ""
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if window?.firstResponder === self, OverlayKey(event: event) == .commandP, onKey(.commandP) { return true }
return super.performKeyEquivalent(with: event)
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard string.isEmpty, !placeholder.isEmpty else { return }
let attributes: [NSAttributedString.Key: Any] = [.font: font ?? Typography.note.nsFont, .foregroundColor: NSColor(hex: 0x6C7783)]
let origin = NSPoint(x: textContainerInset.width + (textContainer?.lineFragmentPadding ?? 0), y: textContainerInset.height)
(placeholder as NSString).draw(at: origin, withAttributes: attributes)
}
}
@@ -36,6 +36,10 @@ public struct OverlayHost: View {
if let note = model.note {
NoteOverlay(note: note, onPassOn: { model.perform(.passNote) }, onClose: dismiss)
}
case .cleanCopy:
if let cleanCopy = model.cleanCopy {
CleanCopyOverlay(store: cleanCopy, onCopy: { model.perform(.copyCleanCopy) }, onClose: dismiss)
}
case let .recentFiles(initialSelection):
RecentFilesOverlay(history: model.editor?.history ?? [], initialSelection: initialSelection,
changedPaths: model.changedPaths,
+14 -1
View File
@@ -4,7 +4,7 @@ import SwiftUI
/// The keys the overlays react to, from a text field or a bare key catcher.
enum OverlayKey: Equatable {
case up, down, commandUp, commandDown, commandLeft, commandRight, enter, escape
case commandP
case commandP, commandEnter
/// Maps the text system's commands back to the keys that produced them.
init?(selector: Selector) {
@@ -21,6 +21,19 @@ enum OverlayKey: Equatable {
}
}
/// ⌘P or ⌘↩ from a text view, which sees them as key equivalents before any key handling.
init?(equivalent event: NSEvent) {
let modifiers = event.modifierFlags.intersection([.command, .shift, .option, .control])
guard modifiers == .command else { return nil }
if event.keyCode == 36 || event.keyCode == 76 {
self = .commandEnter
} else if event.charactersIgnoringModifiers?.lowercased() == "p" {
self = .commandP
} else {
return nil
}
}
/// Maps a raw key event, for overlays without a text field.
init?(event: NSEvent) {
let command = event.modifierFlags.contains(.command)
@@ -0,0 +1,98 @@
import AppKit
import SwiftUI
/// Mono 14/1.7 plain-text editor with an amber caret, focused with the caret at the end.
struct OverlayTextEditor: NSViewRepresentable {
@Binding var text: String
var placeholder = ""
var onKey: (OverlayKey) -> Bool
func makeCoordinator() -> Coordinator { Coordinator(self) }
func makeNSView(context: Context) -> NSScrollView {
let scroll = NSScrollView()
scroll.drawsBackground = false
scroll.hasVerticalScroller = true
scroll.autohidesScrollers = true
let textView = OverlayTextView(frame: .zero)
textView.pointerLayer = .overlay
textView.isVerticallyResizable = true
textView.isHorizontallyResizable = false
textView.autoresizingMask = [.width]
textView.textContainer?.widthTracksTextView = true
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.fillsClipView = true
scroll.documentView = textView
let role = Typography.note
let paragraph = NSMutableParagraphStyle()
paragraph.minimumLineHeight = role.lineHeight ?? 23.8
paragraph.maximumLineHeight = role.lineHeight ?? 23.8
textView.drawsBackground = false
textView.isRichText = false
textView.allowsUndo = true
textView.isAutomaticQuoteSubstitutionEnabled = false
textView.isAutomaticDashSubstitutionEnabled = false
textView.isAutomaticSpellingCorrectionEnabled = false
textView.isContinuousSpellCheckingEnabled = false
textView.font = role.nsFont
textView.textColor = NSColor(hex: 0xE4E7E6)
textView.insertionPointColor = NSColor(hex: 0xE8913A)
textView.defaultParagraphStyle = paragraph
textView.typingAttributes = [.font: role.nsFont, .foregroundColor: NSColor(hex: 0xE4E7E6), .paragraphStyle: paragraph]
textView.textContainerInset = NSSize(width: 20, height: 18)
textView.string = text
textView.delegate = context.coordinator
textView.onKey = onKey
textView.placeholder = placeholder
DispatchQueue.main.async {
guard let window = textView.window, window.makeFirstResponder(textView) else { return }
textView.setSelectedRange(NSRange(location: (textView.string as NSString).length, length: 0))
textView.scrollRangeToVisible(textView.selectedRange())
}
return scroll
}
func updateNSView(_ scroll: NSScrollView, context: Context) {
context.coordinator.parent = self
guard let textView = scroll.documentView as? OverlayTextView else { return }
textView.onKey = onKey
if textView.string != text { textView.string = text }
}
final class Coordinator: NSObject, NSTextViewDelegate {
var parent: OverlayTextEditor
init(_ parent: OverlayTextEditor) {
self.parent = parent
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
parent.text = textView.string
textView.needsDisplay = true
}
func textView(_ textView: NSTextView, doCommandBy selector: Selector) -> Bool {
selector == #selector(NSResponder.cancelOperation(_:)) ? parent.onKey(.escape) : false
}
}
}
/// Catches ⌘P and ⌘↩ before the menu bar sees them, and draws the placeholder.
private final class OverlayTextView: PointerBoundTextView {
var onKey: (OverlayKey) -> Bool = { _ in false }
var placeholder = ""
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if window?.firstResponder === self, let key = OverlayKey(equivalent: event), onKey(key) { return true }
return super.performKeyEquivalent(with: event)
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard string.isEmpty, !placeholder.isEmpty else { return }
let attributes: [NSAttributedString.Key: Any] = [.font: font ?? Typography.note.nsFont, .foregroundColor: NSColor(hex: 0x6C7783)]
let origin = NSPoint(x: textContainerInset.width + (textContainer?.lineFragmentPadding ?? 0), y: textContainerInset.height)
(placeholder as NSString).draw(at: origin, withAttributes: attributes)
}
}
@@ -310,7 +310,7 @@ struct SearchOverlay: View {
case .down, .commandDown: park(); model.moveRow(by: 1)
case .enter: if let target = model.target() { open(target) }
case .escape: onClose()
case .commandP: return false
case .commandP, .commandEnter: return false
}
return true
}