Custom alert: show menu when long press moves onto menu button

This commit is contained in:
Shadowfacts 2024-07-06 17:19:30 -07:00
parent 5abd265195
commit d1af911241
1 changed files with 24 additions and 0 deletions

View File

@ -159,6 +159,7 @@ class CustomAlertActionsView: UIControl {
}() }()
#endif #endif
private var currentSelectedActionIndex: Int? private var currentSelectedActionIndex: Int?
private var showPressedMenuWorkItem: DispatchWorkItem?
init(config: CustomAlertController.Configuration, dismiss: @escaping () -> Void) { init(config: CustomAlertController.Configuration, dismiss: @escaping () -> Void) {
self.dismiss = dismiss self.dismiss = dismiss
@ -327,11 +328,34 @@ class CustomAlertActionsView: UIControl {
generator.selectionChanged() generator.selectionChanged()
} }
#endif #endif
if let showPressedMenuWorkItem {
showPressedMenuWorkItem.cancel()
self.showPressedMenuWorkItem = nil
}
} }
currentSelectedActionIndex = selectedButton?.offset currentSelectedActionIndex = selectedButton?.offset
selectedButton?.element.backgroundColor = .secondarySystemFill 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) #if !os(visionOS)
generator.prepare() generator.prepare()
#endif #endif