From d1af911241f540d377017dd129cba94ca5274563 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 6 Jul 2024 17:19:30 -0700 Subject: [PATCH] Custom alert: show menu when long press moves onto menu button --- .../Utilities/CustomAlertController.swift | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Tusker/Screens/Utilities/CustomAlertController.swift b/Tusker/Screens/Utilities/CustomAlertController.swift index 512e1a2aa1..cf3b2bb52f 100644 --- a/Tusker/Screens/Utilities/CustomAlertController.swift +++ b/Tusker/Screens/Utilities/CustomAlertController.swift @@ -159,6 +159,7 @@ class CustomAlertActionsView: UIControl { }() #endif private var currentSelectedActionIndex: Int? + private var showPressedMenuWorkItem: DispatchWorkItem? init(config: CustomAlertController.Configuration, dismiss: @escaping () -> Void) { self.dismiss = dismiss @@ -327,11 +328,34 @@ class CustomAlertActionsView: UIControl { generator.selectionChanged() } #endif + + if let showPressedMenuWorkItem { + showPressedMenuWorkItem.cancel() + self.showPressedMenuWorkItem = nil + } } currentSelectedActionIndex = selectedButton?.offset selectedButton?.element.backgroundColor = .secondarySystemFill + if let currentSelectedActionIndex, + case .menu(_) = reorderedActions[currentSelectedActionIndex].style, + case let button = actionButtons[currentSelectedActionIndex], + let interaction = button.contextMenuInteraction, + showPressedMenuWorkItem == nil { + showPressedMenuWorkItem = DispatchWorkItem { + if #available(iOS 17.4, *) { + button.performPrimaryAction() + } else { + let selector = NSSelectorFromString(["Location:", "At", "Menu", "present", "_"].reversed().joined()) + if interaction.responds(to: selector) { + interaction.perform(selector, with: recognizer.location(in: button)) + } + } + } + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250), execute: showPressedMenuWorkItem!) + } + #if !os(visionOS) generator.prepare() #endif