remove magnification and text selection on iOS, also replaced TextView URL handling

This commit is contained in:
Mauro Romito
2025-09-29 14:29:13 +02:00
committed by Mauro
parent 89c9e3112f
commit f8a175ff35
+29 -15
View File
@@ -8,14 +8,25 @@
import MatrixRustSDK
import SwiftUI
final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate {
final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate {
var timelineContext: TimelineViewModel.Context?
var updateClosure: (() -> Void)?
private var pillViews = NSHashTable<UIView>.weakObjects()
override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
// We don't need to change the behaviour on MacOS
if !ProcessInfo.processInfo.isiOSAppOnMac {
gestureRecognizer.delegate = self
}
super.addGestureRecognizer(gestureRecognizer)
}
// This prevents the magnifying glass from showing up
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
gestureRecognizer as? UILongPressGestureRecognizer == nil
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if otherGestureRecognizer is UILongPressGestureRecognizer {
return false
}
return true
}
func invalidateTextAttachmentsDisplay() {
@@ -72,6 +83,7 @@ struct MessageText: UIViewRepresentable {
textView.adjustsFontForContentSizeCategory = true
// Required to allow tapping links
// We disable selection at delegate level
textView.isSelectable = true
textView.isUserInteractionEnabled = true
@@ -125,20 +137,22 @@ struct MessageText: UIViewRepresentable {
self.openURLAction = openURLAction
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
switch interaction {
case .invokeDefaultAction:
openURLAction.callAsFunction(URL)
return false
case .preview:
// We don't want to show a URL preview for permalinks
return parseMatrixEntityFrom(uri: URL.absoluteString) == nil
default:
return true
func textViewDidChangeSelection(_ textView: UITextView) {
guard !ProcessInfo.processInfo.isiOSAppOnMac else {
return
}
textView.selectedTextRange = nil
}
@available(iOS 17.0, *)
func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
if case .link(let url) = textItem.content {
return .init(title: defaultAction.title) { [weak self] _ in
self?.openURLAction.callAsFunction(url)
}
}
return defaultAction
}
func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? {
switch textItem.content {
case let .link(url):