From 4889e0a0f0525d809b68245b365ec622c6661a9a Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 17 Dec 2024 23:45:42 +0100 Subject: [PATCH] factor our gesture detection --- deltachat-ios/Chat/ChatViewController.swift | 9 +++++++ deltachat-ios/Chat/Views/MessageLabel.swift | 28 +++++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/deltachat-ios/Chat/ChatViewController.swift b/deltachat-ios/Chat/ChatViewController.swift index bd412610e..ecf2d6706 100644 --- a/deltachat-ios/Chat/ChatViewController.swift +++ b/deltachat-ios/Chat/ChatViewController.swift @@ -2028,6 +2028,15 @@ extension ChatViewController { return nil } + // Check if the long tap is on a link (or other message text element with custom long tap behavior) + if let msgcell = tableView.cellForRow(at: indexPath) as? BaseMessageCell { + let label = msgcell.messageLabel.label + let localTouchLocation = tableView.convert(point, to: label) + if let (detectorType, value) = label.detectGesture(localTouchLocation) { + print("url: \(detectorType) -- \(value) -- ") + } + } + return UIContextMenuConfiguration( identifier: NSString(string: "\(messageId)"), previewProvider: nil, diff --git a/deltachat-ios/Chat/Views/MessageLabel.swift b/deltachat-ios/Chat/Views/MessageLabel.swift index f23956e40..2f3617354 100644 --- a/deltachat-ios/Chat/Views/MessageLabel.swift +++ b/deltachat-ios/Chat/Views/MessageLabel.swift @@ -452,17 +452,23 @@ open class MessageLabel: UILabel { } - open func handleGesture(_ touchLocation: CGPoint) -> Bool { - - guard let index = stringIndex(at: touchLocation) else { return false } - - for (detectorType, ranges) in rangesForDetectors { - for (range, value) in ranges { - if range.contains(index) { - handleGesture(for: detectorType, value: value) - return true - } - } + internal func detectGesture(_ touchLocation: CGPoint) -> (DetectorType, NewMessageTextCheckingType)? { + guard let index = stringIndex(at: touchLocation) else { return nil } + + for (detectorType, ranges) in rangesForDetectors { + for (range, value) in ranges { + if range.contains(index) { + return (detectorType, value) + } + } + } + return nil + } + + open func handleGesture(_ touchLocation: CGPoint) -> Bool { + if let (detectorType, value) = detectGesture(touchLocation) { + handleGesture(for: detectorType, value: value) + return true } return false }