adds Clean Copy Support
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Mono 14/1.7 plain-text editor with an amber caret, focused with the caret at the end.
|
||||
struct OverlayTextEditor: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
var placeholder = ""
|
||||
var onKey: (OverlayKey) -> Bool
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator(self) }
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scroll = NSScrollView()
|
||||
scroll.drawsBackground = false
|
||||
scroll.hasVerticalScroller = true
|
||||
scroll.autohidesScrollers = true
|
||||
let textView = OverlayTextView(frame: .zero)
|
||||
textView.pointerLayer = .overlay
|
||||
textView.isVerticallyResizable = true
|
||||
textView.isHorizontallyResizable = false
|
||||
textView.autoresizingMask = [.width]
|
||||
textView.textContainer?.widthTracksTextView = true
|
||||
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
||||
textView.fillsClipView = true
|
||||
scroll.documentView = textView
|
||||
let role = Typography.note
|
||||
let paragraph = NSMutableParagraphStyle()
|
||||
paragraph.minimumLineHeight = role.lineHeight ?? 23.8
|
||||
paragraph.maximumLineHeight = role.lineHeight ?? 23.8
|
||||
textView.drawsBackground = false
|
||||
textView.isRichText = false
|
||||
textView.allowsUndo = true
|
||||
textView.isAutomaticQuoteSubstitutionEnabled = false
|
||||
textView.isAutomaticDashSubstitutionEnabled = false
|
||||
textView.isAutomaticSpellingCorrectionEnabled = false
|
||||
textView.isContinuousSpellCheckingEnabled = false
|
||||
textView.font = role.nsFont
|
||||
textView.textColor = NSColor(hex: 0xE4E7E6)
|
||||
textView.insertionPointColor = NSColor(hex: 0xE8913A)
|
||||
textView.defaultParagraphStyle = paragraph
|
||||
textView.typingAttributes = [.font: role.nsFont, .foregroundColor: NSColor(hex: 0xE4E7E6), .paragraphStyle: paragraph]
|
||||
textView.textContainerInset = NSSize(width: 20, height: 18)
|
||||
textView.string = text
|
||||
textView.delegate = context.coordinator
|
||||
textView.onKey = onKey
|
||||
textView.placeholder = placeholder
|
||||
DispatchQueue.main.async {
|
||||
guard let window = textView.window, window.makeFirstResponder(textView) else { return }
|
||||
textView.setSelectedRange(NSRange(location: (textView.string as NSString).length, length: 0))
|
||||
textView.scrollRangeToVisible(textView.selectedRange())
|
||||
}
|
||||
return scroll
|
||||
}
|
||||
|
||||
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
||||
context.coordinator.parent = self
|
||||
guard let textView = scroll.documentView as? OverlayTextView else { return }
|
||||
textView.onKey = onKey
|
||||
if textView.string != text { textView.string = text }
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextViewDelegate {
|
||||
var parent: OverlayTextEditor
|
||||
|
||||
init(_ parent: OverlayTextEditor) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard let textView = notification.object as? NSTextView else { return }
|
||||
parent.text = textView.string
|
||||
textView.needsDisplay = true
|
||||
}
|
||||
|
||||
func textView(_ textView: NSTextView, doCommandBy selector: Selector) -> Bool {
|
||||
selector == #selector(NSResponder.cancelOperation(_:)) ? parent.onKey(.escape) : false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Catches ⌘P and ⌘↩ before the menu bar sees them, and draws the placeholder.
|
||||
private final class OverlayTextView: PointerBoundTextView {
|
||||
var onKey: (OverlayKey) -> Bool = { _ in false }
|
||||
var placeholder = ""
|
||||
|
||||
override func performKeyEquivalent(with event: NSEvent) -> Bool {
|
||||
if window?.firstResponder === self, let key = OverlayKey(equivalent: event), onKey(key) { return true }
|
||||
return super.performKeyEquivalent(with: event)
|
||||
}
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
super.draw(dirtyRect)
|
||||
guard string.isEmpty, !placeholder.isEmpty else { return }
|
||||
let attributes: [NSAttributedString.Key: Any] = [.font: font ?? Typography.note.nsFont, .foregroundColor: NSColor(hex: 0x6C7783)]
|
||||
let origin = NSPoint(x: textContainerInset.width + (textContainer?.lineFragmentPadding ?? 0), y: textContainerInset.height)
|
||||
(placeholder as NSString).draw(at: origin, withAttributes: attributes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user