Branchwell

Jotley / app / macOS / Views / TabViews / SummaryTabView+InlineEditableTextView.swift

1import SwiftUI
2import AppKit
3
4struct InlineEditableSummaryTextView: NSViewRepresentable {
5 @Binding var text: String
6 @FocusState.Binding var isFocused: Bool
7 let accentColor: NSColor
8 let textColor: NSColor
9 var fontSize = NSFont.systemFontSize
10
11 func makeCoordinator() -> Coordinator {
12 Coordinator(parent: self)
13 }
14
15 func makeNSView(context: Context) -> NSTextView {
16 let textContainer = NSTextContainer(size: NSSize(width: 0, height: CGFloat.greatestFiniteMagnitude))
17 textContainer.widthTracksTextView = true
18 textContainer.lineFragmentPadding = 0
19
20 let layoutManager = NSLayoutManager()
21 layoutManager.addTextContainer(textContainer)
22
23 let textStorage = NSTextStorage(string: text)
24 textStorage.addLayoutManager(layoutManager)
25
26 let textView = SummaryInlineEditableTextView(frame: .zero, textContainer: textContainer)
27 textView.delegate = context.coordinator
28 textView.isEditable = true
29 textView.isSelectable = true
30 textView.drawsBackground = false
31 textView.backgroundColor = .clear
32 textView.textContainerInset = NSSize(width: 0, height: 0)
33 // SwiftUI owns the frame. AppKit must not grow it past the field border.
34 textView.isVerticallyResizable = false
35 textView.isHorizontallyResizable = false
36 textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
37 textView.autoresizingMask = [.width]
38 textView.font = NSFont.systemFont(ofSize: fontSize)
39 textView.textColor = textColor
40 textView.allowsUndo = true
41 textView.importsGraphics = false
42 textView.isRichText = false
43
44 applyTheme(to: textView)
45
46 if isFocused {
47 DispatchQueue.main.async {
48 textView.window?.makeFirstResponder(textView)
49 }
50 }
51
52 return textView
53 }
54
55 func updateNSView(_ nsView: NSTextView, context: Context) {
56 guard let textView = nsView as? SummaryInlineEditableTextView else { return }
57 context.coordinator.parent = self
58
59 if textView.string != text {
60 textView.string = text
61 }
62
63 applyTheme(to: textView)
64
65 if isFocused {
66 if textView.window?.firstResponder !== textView {
67 textView.window?.makeFirstResponder(textView)
68 }
69 }
70 }
71
72 func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSTextView, context: Context) -> CGSize? {
73 guard let width = proposal.width, width.isFinite, width > 0,
74 let layoutManager = nsView.layoutManager, let container = nsView.textContainer else { return nil }
75
76 // SwiftUI can probe several widths. Restore the drawing width after
77 // measuring so an unused proposal cannot rewrap the visible text.
78 let drawingSize = container.containerSize
79 defer { container.containerSize = drawingSize }
80 container.containerSize = NSSize(width: width, height: drawingSize.height)
81 layoutManager.ensureLayout(for: container)
82 let height = max(layoutManager.usedRect(for: container).maxY, layoutManager.extraLineFragmentRect.maxY)
83 return CGSize(width: width, height: ceil(height))
84 }
85
86 private func applyTheme(to textView: SummaryInlineEditableTextView) {
87 textView.textColor = textColor
88 textView.insertionPointColor = accentColor
89 textView.selectedTextAttributes = [
90 .backgroundColor: accentColor.withAlphaComponent(0.3),
91 .foregroundColor: textColor
92 ]
93 }
94
95 final class Coordinator: NSObject, NSTextViewDelegate {
96 var parent: InlineEditableSummaryTextView
97
98 init(parent: InlineEditableSummaryTextView) {
99 self.parent = parent
100 }
101
102 func textDidChange(_ notification: Notification) {
103 guard let textView = notification.object as? SummaryInlineEditableTextView else { return }
104 if parent.text != textView.string {
105 parent.text = textView.string
106 }
107 }
108
109 func textDidBeginEditing(_ notification: Notification) {
110 if !parent.isFocused {
111 parent.isFocused = true
112 }
113 }
114
115 func textDidEndEditing(_ notification: Notification) {
116 if parent.isFocused {
117 parent.isFocused = false
118 }
119 }
120 }
121}
122private final class SummaryInlineEditableTextView: NSTextView {
123 override var acceptsFirstResponder: Bool { true }
124 override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
125
126 override func mouseDown(with event: NSEvent) {
127 if window?.firstResponder !== self {
128 window?.makeFirstResponder(self)
129 }
130 super.mouseDown(with: event)
131 }
132}