fix warnings
This commit is contained in:
@@ -447,6 +447,27 @@ private struct FlowBuilder {
|
||||
|
||||
private final class FlowTextView: NSTextView {
|
||||
var onTap: (@MainActor @Sendable () -> Void)?
|
||||
let storage: NSTextStorage
|
||||
private let layout: NSLayoutManager
|
||||
private let container: NSTextContainer
|
||||
|
||||
init() {
|
||||
let storage = NSTextStorage()
|
||||
let layout = NSLayoutManager()
|
||||
let container = NSTextContainer(size: CGSize(width: 0, height: CGFloat.greatestFiniteMagnitude))
|
||||
container.lineFragmentPadding = 0
|
||||
container.widthTracksTextView = true
|
||||
storage.addLayoutManager(layout)
|
||||
layout.addTextContainer(container)
|
||||
self.storage = storage
|
||||
self.layout = layout
|
||||
self.container = container
|
||||
super.init(frame: .zero, textContainer: container)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) is not supported")
|
||||
}
|
||||
|
||||
override func resignFirstResponder() -> Bool {
|
||||
setSelectedRange(NSRange(location: 0, length: 0))
|
||||
@@ -462,19 +483,19 @@ private final class FlowTextView: NSTextView {
|
||||
}
|
||||
|
||||
private func link(at point: NSPoint) -> Bool {
|
||||
guard let lm = unsafe layoutManager, let tc = unsafe textContainer, let ts = unsafe textStorage, ts.length > 0 else {
|
||||
guard storage.length > 0 else {
|
||||
return false
|
||||
}
|
||||
let p = NSPoint(x: point.x - textContainerOrigin.x, y: point.y - textContainerOrigin.y)
|
||||
let g = lm.glyphIndex(for: p, in: tc)
|
||||
if !lm.boundingRect(forGlyphRange: NSRange(location: g, length: 1), in: tc).contains(p) {
|
||||
let g = layout.glyphIndex(for: p, in: container)
|
||||
if !layout.boundingRect(forGlyphRange: NSRange(location: g, length: 1), in: container).contains(p) {
|
||||
return false
|
||||
}
|
||||
let i = lm.characterIndexForGlyph(at: g)
|
||||
if i >= ts.length {
|
||||
let i = layout.characterIndexForGlyph(at: g)
|
||||
if i >= storage.length {
|
||||
return false
|
||||
}
|
||||
let value = unsafe ts.attribute(.link, at: i, effectiveRange: nil)
|
||||
let value = unsafe storage.attribute(.link, at: i, effectiveRange: nil)
|
||||
return value != nil
|
||||
}
|
||||
}
|
||||
@@ -483,15 +504,13 @@ private struct FlowText: NSViewRepresentable {
|
||||
let text: NSAttributedString
|
||||
|
||||
func makeNSView(context: Context) -> FlowTextView {
|
||||
let v = FlowTextView(usingTextLayoutManager: false)
|
||||
let v = FlowTextView()
|
||||
v.isEditable = false
|
||||
v.isSelectable = true
|
||||
v.drawsBackground = false
|
||||
v.isVerticallyResizable = false
|
||||
v.isHorizontallyResizable = false
|
||||
v.textContainerInset = .zero
|
||||
unsafe v.textContainer!.lineFragmentPadding = 0
|
||||
unsafe v.textContainer!.widthTracksTextView = true
|
||||
v.linkTextAttributes = [.cursor: NSCursor.pointingHand]
|
||||
v.delegate = context.coordinator
|
||||
return v
|
||||
@@ -500,11 +519,11 @@ private struct FlowText: NSViewRepresentable {
|
||||
func updateNSView(_ v: FlowTextView, context: Context) {
|
||||
context.coordinator.openURL = context.environment.openURL
|
||||
v.onTap = context.environment.choiceTap
|
||||
if unsafe v.textStorage!.isEqual(to: text) {
|
||||
if v.storage.isEqual(to: text) {
|
||||
return
|
||||
}
|
||||
let selected = v.selectedRanges
|
||||
unsafe v.textStorage!.setAttributedString(text)
|
||||
v.storage.setAttributedString(text)
|
||||
if selected.allSatisfy({ NSMaxRange($0.rangeValue) <= text.length }) {
|
||||
v.selectedRanges = selected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user