|
| 1 | +// |
| 2 | +// TagCopyMenuView.swift |
| 3 | +// PresentationShared |
| 4 | +// |
| 5 | +// Created by opfic on 8/9/26. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import UIKit |
| 10 | + |
| 11 | +struct TagCopyMenuView: UIViewRepresentable { |
| 12 | + let tagText: String |
| 13 | + |
| 14 | + func makeCoordinator() -> TagCopyMenuCoordinator { |
| 15 | + TagCopyMenuCoordinator() |
| 16 | + } |
| 17 | + |
| 18 | + func makeUIView(context: Context) -> UIView { |
| 19 | + let view = UIView() |
| 20 | + view.backgroundColor = .clear |
| 21 | + view.isAccessibilityElement = false |
| 22 | + context.coordinator.install(on: view) |
| 23 | + context.coordinator.tagText = tagText |
| 24 | + return view |
| 25 | + } |
| 26 | + |
| 27 | + func updateUIView(_ view: UIView, context: Context) { |
| 28 | + context.coordinator.tagText = tagText |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +final class TagCopyMenuCoordinator: NSObject { |
| 33 | + var tagText = "" |
| 34 | + private weak var view: UIView? |
| 35 | + private var editMenuInteraction: UIEditMenuInteraction? |
| 36 | + |
| 37 | + func install(on view: UIView) { |
| 38 | + self.view = view |
| 39 | + |
| 40 | + let editMenuInteraction = UIEditMenuInteraction(delegate: self) |
| 41 | + view.addInteraction(editMenuInteraction) |
| 42 | + self.editMenuInteraction = editMenuInteraction |
| 43 | + |
| 44 | + let longPressGesture = UILongPressGestureRecognizer( |
| 45 | + target: self, |
| 46 | + action: #selector(handleLongPressGesture(_:)) |
| 47 | + ) |
| 48 | + longPressGesture.allowedTouchTypes = [ |
| 49 | + NSNumber(value: UITouch.TouchType.direct.rawValue), |
| 50 | + NSNumber(value: UITouch.TouchType.indirectPointer.rawValue) |
| 51 | + ] |
| 52 | + longPressGesture.cancelsTouchesInView = false |
| 53 | + longPressGesture.delegate = self |
| 54 | + view.addGestureRecognizer(longPressGesture) |
| 55 | + |
| 56 | + let secondaryClickGesture = UITapGestureRecognizer( |
| 57 | + target: self, |
| 58 | + action: #selector(handleSecondaryClickGesture(_:)) |
| 59 | + ) |
| 60 | + secondaryClickGesture.allowedTouchTypes = [ |
| 61 | + NSNumber(value: UITouch.TouchType.indirectPointer.rawValue) |
| 62 | + ] |
| 63 | + secondaryClickGesture.buttonMaskRequired = .secondary |
| 64 | + secondaryClickGesture.cancelsTouchesInView = false |
| 65 | + secondaryClickGesture.delegate = self |
| 66 | + view.addGestureRecognizer(secondaryClickGesture) |
| 67 | + } |
| 68 | + |
| 69 | + @objc |
| 70 | + private func handleLongPressGesture(_ gesture: UILongPressGestureRecognizer) { |
| 71 | + guard gesture.state == .began, let view else { return } |
| 72 | + |
| 73 | + presentEditMenu(at: gesture.location(in: view)) |
| 74 | + } |
| 75 | + |
| 76 | + @objc |
| 77 | + private func handleSecondaryClickGesture(_ gesture: UITapGestureRecognizer) { |
| 78 | + guard gesture.state == .ended, let view else { return } |
| 79 | + |
| 80 | + presentEditMenu(at: gesture.location(in: view)) |
| 81 | + } |
| 82 | + |
| 83 | + private func presentEditMenu(at sourcePoint: CGPoint) { |
| 84 | + let configuration = UIEditMenuConfiguration( |
| 85 | + identifier: nil, |
| 86 | + sourcePoint: sourcePoint |
| 87 | + ) |
| 88 | + editMenuInteraction?.presentEditMenu(with: configuration) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +extension TagCopyMenuCoordinator: UIEditMenuInteractionDelegate { |
| 93 | + func editMenuInteraction( |
| 94 | + _ interaction: UIEditMenuInteraction, |
| 95 | + menuFor configuration: UIEditMenuConfiguration, |
| 96 | + suggestedActions: [UIMenuElement] |
| 97 | + ) -> UIMenu? { |
| 98 | + let tagText = tagText |
| 99 | + let copyAction = UIAction(title: String(localized: "common_copy")) { _ in |
| 100 | + UIPasteboard.general.string = tagText |
| 101 | + } |
| 102 | + return UIMenu(children: [copyAction]) |
| 103 | + } |
| 104 | + |
| 105 | + func editMenuInteraction( |
| 106 | + _ interaction: UIEditMenuInteraction, |
| 107 | + targetRectFor configuration: UIEditMenuConfiguration |
| 108 | + ) -> CGRect { |
| 109 | + view?.bounds ?? .zero |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +extension TagCopyMenuCoordinator: UIGestureRecognizerDelegate { |
| 114 | + func gestureRecognizer( |
| 115 | + _ gestureRecognizer: UIGestureRecognizer, |
| 116 | + shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer |
| 117 | + ) -> Bool { |
| 118 | + true |
| 119 | + } |
| 120 | +} |
0 commit comments