oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2import AppKit
3import SwiftUI
4
5/// Position the field editor's caret as part of gaining focus, before another
6/// keyboard event can replace the character that started cell editing.
7struct SheetCellEditor: NSViewRepresentable {
8 @Binding var text: String
9 let enabled: Bool
10 let label: String
11 var typeAhead: SheetTypeAhead? = nil
12 /// Returns true when the edit ended (it may not, if the value was refused).
13 let commit: (Bool) -> Bool
14 let cancel: () -> Void
15 func makeCoordinator() -> Coordinator { Coordinator(self) }
16 func makeNSView(context: Context) -> CellTextField {
17 let field = CellTextField()
18 field.isBordered = false; field.drawsBackground = false
19 // The cell already draws its selection border around the full cell.
20 field.focusRingType = .none
21 field.font = .systemFont(ofSize: 13)
22 field.delegate = context.coordinator
23 field.stringValue = text
24 field.setAccessibilityLabel(label)
25 field.typeAhead = typeAhead
26 return field
27 }
28 func updateNSView(_ field: CellTextField, context: Context) {
29 context.coordinator.parent = self
30 let regainedEditing = enabled && !field.isEnabled
31 field.isEnabled = enabled
32 if field.stringValue != text {
33 field.stringValue = text
34 if let editor = field.currentEditor() as? NSTextView {
35 editor.setSelectedRange(NSRange(location: text.utf16.count, length: 0))
36 }
37 }
38 if regainedEditing { field.window?.makeFirstResponder(field) }
39 }
40 final class CellTextField: NSTextField {
41 /// Holds the keystroke that opened this field (SheetTypeAhead).
42 weak var typeAhead: SheetTypeAhead?
43 override func viewDidMoveToWindow() {
44 super.viewDidMoveToWindow()
45 guard let window else { return }
46 // The representable must be attached before it can become responder.
47 DispatchQueue.main.async { [weak self, weak window] in
48 guard let self, let window, self.window === window, self.isEnabled else { return }
49 window.makeFirstResponder(self)
50 }
51 }
52 override func becomeFirstResponder() -> Bool {
53 let accepted = super.becomeFirstResponder()
54 if accepted, let editor = currentEditor() as? NSTextView {
55 editor.setSelectedRange(NSRange(location: editor.string.utf16.count, length: 0))
56 }
57 if accepted { typeAhead?.fieldFocused(self) }
58 return accepted
59 }
60 }
61 @MainActor final class Coordinator: NSObject, NSTextFieldDelegate {
62 var parent: SheetCellEditor
63 init(_ parent: SheetCellEditor) { self.parent = parent }
64 func controlTextDidChange(_ notification: Notification) {
65 guard let field = notification.object as? NSTextField else { return }
66 parent.text = field.stringValue
67 }
68 func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool {
69 let ended: Bool
70 switch selector {
71 case #selector(NSResponder.insertNewline(_:)): ended = parent.commit(false)
72 case #selector(NSResponder.insertTab(_:)): ended = parent.commit(true)
73 case #selector(NSResponder.cancelOperation(_:)): parent.cancel(); ended = true
74 default: return false
75 }
76 // Let go of the keyboard now, not when SwiftUI next removes this
77 // field: a key typed in between belongs to the next cell, and the
78 // grid's type-ahead takes it from here.
79 if ended { control.window?.makeFirstResponder(nil) }
80 return true
81 }
82 }
83}
84#endif
85