|
| 1 | +// |
| 2 | +// CompletionTextField.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | +// NSTextField with native macOS autocompletion via custom field editor. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +struct CompletionTextField: NSViewRepresentable { |
| 12 | + @Binding var text: String |
| 13 | + var placeholder: String = "" |
| 14 | + var completions: [String] = [] |
| 15 | + var shouldFocus: Bool = false |
| 16 | + var allowsMultiLine: Bool = false |
| 17 | + var onSubmit: () -> Void = {} |
| 18 | + |
| 19 | + func makeNSView(context: Context) -> CompletionNSTextField { |
| 20 | + let textField = CompletionNSTextField() |
| 21 | + textField.placeholderString = placeholder |
| 22 | + textField.bezelStyle = .roundedBezel |
| 23 | + textField.controlSize = .small |
| 24 | + textField.font = .systemFont(ofSize: ThemeEngine.shared.activeTheme.typography.medium) |
| 25 | + textField.delegate = context.coordinator |
| 26 | + textField.stringValue = text |
| 27 | + textField.completionItems = completions |
| 28 | + |
| 29 | + if allowsMultiLine { |
| 30 | + textField.usesSingleLineMode = false |
| 31 | + textField.cell?.wraps = true |
| 32 | + textField.cell?.isScrollable = false |
| 33 | + textField.lineBreakMode = .byWordWrapping |
| 34 | + textField.maximumNumberOfLines = 0 |
| 35 | + } else { |
| 36 | + textField.lineBreakMode = .byTruncatingTail |
| 37 | + } |
| 38 | + |
| 39 | + if shouldFocus { |
| 40 | + DispatchQueue.main.async { |
| 41 | + textField.window?.makeFirstResponder(textField) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return textField |
| 46 | + } |
| 47 | + |
| 48 | + func updateNSView(_ textField: CompletionNSTextField, context: Context) { |
| 49 | + if textField.stringValue != text { |
| 50 | + textField.stringValue = text |
| 51 | + } |
| 52 | + textField.completionItems = completions |
| 53 | + context.coordinator.onSubmit = onSubmit |
| 54 | + } |
| 55 | + |
| 56 | + func makeCoordinator() -> Coordinator { |
| 57 | + Coordinator(text: $text, onSubmit: onSubmit) |
| 58 | + } |
| 59 | + |
| 60 | + // MARK: - Coordinator |
| 61 | + |
| 62 | + final class Coordinator: NSObject, NSTextFieldDelegate { |
| 63 | + var text: Binding<String> |
| 64 | + var onSubmit: () -> Void |
| 65 | + private var previousTextLength = 0 |
| 66 | + |
| 67 | + init(text: Binding<String>, onSubmit: @escaping () -> Void) { |
| 68 | + self.text = text |
| 69 | + self.onSubmit = onSubmit |
| 70 | + } |
| 71 | + |
| 72 | + func controlTextDidChange(_ notification: Notification) { |
| 73 | + guard let textField = notification.object as? NSTextField else { return } |
| 74 | + let newValue = textField.stringValue |
| 75 | + let grew = newValue.count > previousTextLength |
| 76 | + previousTextLength = newValue.count |
| 77 | + text.wrappedValue = newValue |
| 78 | + |
| 79 | + if grew, !newValue.isEmpty, |
| 80 | + let fieldEditor = textField.currentEditor() as? NSTextView |
| 81 | + { |
| 82 | + fieldEditor.complete(nil) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func controlTextDidEndEditing(_ notification: Notification) { |
| 87 | + previousTextLength = 0 |
| 88 | + } |
| 89 | + |
| 90 | + func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool { |
| 91 | + if commandSelector == #selector(NSResponder.insertNewline(_:)) { |
| 92 | + onSubmit() |
| 93 | + return true |
| 94 | + } |
| 95 | + // Option+Enter → insert newline (standard macOS behavior) |
| 96 | + if commandSelector == #selector(NSResponder.insertNewlineIgnoringFieldEditor(_:)) { |
| 97 | + textView.insertNewlineIgnoringFieldEditor(nil) |
| 98 | + text.wrappedValue = textView.string |
| 99 | + previousTextLength = textView.string.count |
| 100 | + return true |
| 101 | + } |
| 102 | + return false |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// MARK: - NSTextField with Custom Cell |
| 108 | + |
| 109 | +final class CompletionNSTextField: NSTextField { |
| 110 | + var completionItems: [String] = [] { |
| 111 | + didSet { |
| 112 | + (cell as? CompletionTextFieldCell)?.completionItems = completionItems |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + override class var cellClass: AnyClass? { |
| 117 | + get { CompletionTextFieldCell.self } |
| 118 | + set {} |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// MARK: - Custom Cell (provides field editor) |
| 123 | + |
| 124 | +private final class CompletionTextFieldCell: NSTextFieldCell { |
| 125 | + var completionItems: [String] = [] |
| 126 | + private var customFieldEditor: CompletionFieldEditor? |
| 127 | + |
| 128 | + override func fieldEditor(for controlView: NSView) -> NSTextView? { |
| 129 | + if customFieldEditor == nil { |
| 130 | + let editor = CompletionFieldEditor() |
| 131 | + editor.isFieldEditor = true |
| 132 | + customFieldEditor = editor |
| 133 | + } |
| 134 | + customFieldEditor?.completionItems = completionItems |
| 135 | + return customFieldEditor |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// MARK: - Custom Field Editor (native completion) |
| 140 | + |
| 141 | +private final class CompletionFieldEditor: NSTextView { |
| 142 | + var completionItems: [String] = [] |
| 143 | + |
| 144 | + override func completions( |
| 145 | + forPartialWordRange charRange: NSRange, |
| 146 | + indexOfSelectedItem index: UnsafeMutablePointer<Int> |
| 147 | + ) -> [String]? { |
| 148 | + index.pointee = -1 |
| 149 | + |
| 150 | + guard charRange.length > 0 else { return nil } |
| 151 | + |
| 152 | + let partial = (string as NSString).substring(with: charRange).lowercased() |
| 153 | + let matches = completionItems.filter { $0.lowercased().hasPrefix(partial) } |
| 154 | + |
| 155 | + // Don't show popup when the only match is exactly what's typed |
| 156 | + if matches.count == 1, matches[0].lowercased() == partial { |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + return matches.isEmpty ? nil : matches |
| 161 | + } |
| 162 | +} |
0 commit comments