aboutsummaryrefslogtreecommitdiff
path: root/Map/Views/MapTextEditor.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Map/Views/MapTextEditor.swift')
-rw-r--r--Map/Views/MapTextEditor.swift71
1 files changed, 71 insertions, 0 deletions
diff --git a/Map/Views/MapTextEditor.swift b/Map/Views/MapTextEditor.swift
new file mode 100644
index 0000000..7251c59
--- /dev/null
+++ b/Map/Views/MapTextEditor.swift
@@ -0,0 +1,71 @@
+import Cocoa
+import SwiftUI
+
+class MapTextEditorController: NSViewController {
+
+ @Binding var text: String
+
+ init(text: Binding<String>) {
+ self._text = text
+ super.init(nibName: nil, bundle: nil)
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ override func loadView() {
+ let scrollView = NSTextView.scrollableTextView()
+ let textView = scrollView.documentView as! NSTextView
+
+ scrollView.translatesAutoresizingMaskIntoConstraints = false
+
+ textView.delegate = self
+ textView.string = self.text
+ textView.isEditable = true
+ textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular)
+ self.view = scrollView
+ }
+
+ override func viewDidAppear() {
+ self.view.window?.makeFirstResponder(self.view)
+ }
+}
+
+extension MapTextEditorController: NSTextViewDelegate {
+
+ func textDidChange(_ obj: Notification) {
+ if let textField = obj.object as? NSTextView {
+ self.text = textField.string
+ }
+ }
+
+ func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
+ {
+ let range = Range(shouldChangeTextIn, in: view.string)
+ let target = view.string[range!]
+
+ if target == "--" {
+ return false
+ }
+
+ return true
+ }
+}
+
+struct MapTextEditor: NSViewControllerRepresentable {
+
+ @Binding var text: String
+
+ func makeNSViewController(
+ context: NSViewControllerRepresentableContext<MapTextEditor>
+ ) -> MapTextEditorController {
+ return MapTextEditorController(text: $text)
+ }
+
+ func updateNSViewController(
+ _ nsViewController: MapTextEditorController,
+ context: NSViewControllerRepresentableContext<MapTextEditor>
+ ) {
+ }
+}