-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New implementation of NSScrollView, cleaned up some
- Loading branch information
Showing
5 changed files
with
129 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// CustomMacTextView.swift | ||
// | ||
// Foundation by Marc Maset - 2021 | ||
// Changes inspired from MacEditorTextView by Thiago Holanda | ||
// | ||
// Modified by Anthony Ingle - 2022 | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct CustomMacTextView: NSViewRepresentable { | ||
|
||
var placeholderText: String? | ||
@Binding var text: String | ||
var font: NSFont = .systemFont(ofSize: 14, weight: .regular) | ||
|
||
var onSubmit : () -> Void = {} | ||
var onTextChange : (String) -> Void = { _ in } | ||
var onEditingChanged: () -> Void = {} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
func makeNSView(context: Context) -> NSScrollView { | ||
let theTextView = PlaceholderNSTextView.scrollableTextView() | ||
let textView = (theTextView.documentView as! PlaceholderNSTextView) | ||
textView.delegate = context.coordinator | ||
textView.string = text | ||
textView.drawsBackground = false | ||
textView.font = font | ||
textView.placeholderText = placeholderText | ||
theTextView.hasVerticalScroller = false | ||
|
||
return theTextView | ||
} | ||
|
||
func updateNSView(_ view: NSScrollView, context: Context) { | ||
guard let textView = view.documentView as? NSTextView else { | ||
return | ||
} | ||
|
||
textView.string = text | ||
} | ||
|
||
} | ||
|
||
extension CustomMacTextView { | ||
|
||
class Coordinator: NSObject, NSTextViewDelegate { | ||
|
||
var parent: CustomMacTextView | ||
var affectedCharRange: NSRange? | ||
|
||
init(_ parent: CustomMacTextView) { | ||
self.parent = parent | ||
} | ||
func textDidBeginEditing(_ notification: Notification) { | ||
guard let textView = notification.object as? NSTextView else { | ||
return | ||
} | ||
|
||
self.parent.text = textView.string | ||
self.parent.onEditingChanged() | ||
} | ||
|
||
func textDidChange(_ notification: Notification) { | ||
guard let textView = notification.object as? NSTextView else { | ||
return | ||
} | ||
|
||
// Update text | ||
self.parent.text = textView.string | ||
self.parent.onTextChange(textView.string) | ||
} | ||
|
||
func textDidEndEditing(_ notification: Notification) { | ||
guard let textView = notification.object as? NSTextView else { | ||
return | ||
} | ||
|
||
self.parent.text = textView.string | ||
self.parent.onSubmit() | ||
} | ||
|
||
|
||
func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: String?) -> Bool { | ||
return true | ||
} | ||
|
||
// handles commands | ||
func textView(_ textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool { | ||
if (commandSelector == #selector(NSResponder.insertNewline(_:))) { | ||
// Do something against ENTER key | ||
self.parent.onSubmit() | ||
return true | ||
} | ||
|
||
// return true if the action was handled; otherwise false | ||
return false | ||
} | ||
|
||
} | ||
} | ||
|
||
// for setting a proper placeholder text on an NSTextView | ||
fileprivate class PlaceholderNSTextView: NSTextView { | ||
@objc private var placeholderAttributedString: NSAttributedString? | ||
var placeholderText: String? { | ||
didSet { | ||
var attributes = [NSAttributedString.Key: AnyObject]() | ||
attributes[.font] = font | ||
attributes[.foregroundColor] = NSColor.gray | ||
let captionAttributedString = NSAttributedString(string: placeholderText ?? "", attributes: attributes) | ||
placeholderAttributedString = captionAttributedString | ||
} | ||
} | ||
} |
Oops, something went wrong.