import AppKit import SwiftUI import LichtkastenCore /// Replaces a code right before the cursor (PM-style live code replacement) and keeps the cursor in place. /// Returns true if something was replaced. Undo (⌘Z) restores the code. @MainActor func applyCodeExpansion(_ tv: NSTextView, codes: CodeReplacements?) -> Bool { guard let codes else { return false } let sel = tv.selectedRange() guard sel.length == 0, let (range, rep) = codes.pendingExpansion(in: tv.string, cursor: sel.location) else { return false } let afterReplaced = sel.location - (range.location + range.length) // e.g. the typed space after "w10" tv.insertText(rep, replacementRange: range) let newCursor = range.location + (rep as NSString).length + afterReplaced tv.setSelectedRange(NSRange(location: min(newCursor, (tv.string as NSString).length), length: 0)) return true } /// Multi-line field (caption, keywords …) with live code replacement. struct CodeTextView: NSViewRepresentable { @Binding var text: String var codes: CodeReplacements? func makeCoordinator() -> Coordinator { Coordinator(self) } func makeNSView(context: Context) -> NSScrollView { let sv = NSTextView.scrollableTextView() sv.borderType = .bezelBorder sv.hasVerticalScroller = true guard let tv = sv.documentView as? NSTextView else { return sv } tv.delegate = context.coordinator tv.isRichText = false tv.importsGraphics = false tv.allowsUndo = true tv.font = .systemFont(ofSize: 13) tv.isAutomaticQuoteSubstitutionEnabled = false tv.isAutomaticDashSubstitutionEnabled = false tv.isAutomaticTextReplacementEnabled = false tv.isContinuousSpellCheckingEnabled = true tv.textContainerInset = NSSize(width: 3, height: 4) tv.string = text return sv } func updateNSView(_ sv: NSScrollView, context: Context) { context.coordinator.parent = self guard let tv = sv.documentView as? NSTextView, tv.string != text, !context.coordinator.editing else { return } tv.string = text } final class Coordinator: NSObject, NSTextViewDelegate { var parent: CodeTextView var expanding = false var editing = false init(_ p: CodeTextView) { parent = p } func textDidChange(_ notification: Notification) { guard let tv = notification.object as? NSTextView else { return } if !expanding { expanding = true MainActor.assumeIsolated { _ = applyCodeExpansion(tv, codes: parent.codes) } expanding = false } editing = true parent.text = tv.string DispatchQueue.main.async { self.editing = false } } } } /// Single-line field with live code replacement (event, city, test field …). struct CodeTextField: NSViewRepresentable { @Binding var text: String var codes: CodeReplacements? var placeholder: String = "" func makeCoordinator() -> Coordinator { Coordinator(self) } func makeNSView(context: Context) -> NSTextField { let f = NSTextField(string: text) f.delegate = context.coordinator f.placeholderString = placeholder f.bezelStyle = .roundedBezel f.lineBreakMode = .byTruncatingTail f.cell?.isScrollable = true f.font = .systemFont(ofSize: 13) return f } func updateNSView(_ f: NSTextField, context: Context) { context.coordinator.parent = self f.placeholderString = placeholder if f.stringValue != text, f.currentEditor() == nil { f.stringValue = text } } final class Coordinator: NSObject, NSTextFieldDelegate { var parent: CodeTextField var expanding = false init(_ p: CodeTextField) { parent = p } func controlTextDidChange(_ obj: Notification) { guard let f = obj.object as? NSTextField else { return } if !expanding, let editor = f.currentEditor() as? NSTextView { expanding = true MainActor.assumeIsolated { _ = applyCodeExpansion(editor, codes: parent.codes) } expanding = false } parent.text = f.stringValue } } }