How to determine what triggers a context menu presentation

46 Views Asked by At

I'm working on an app for iPad and Mac Catalyst. I have implemented undo using custom undo managers. This is so there can be a separate undo manager for each layer in the project. I've noticed that sometimes when I long press the COMMAND key, I am getting this context menu.

enter image description here

I've added validate functions in all my objects that are responders in an effort to determine what is triggering it, and I can't find it. In the situation, there is nothing to undo, so I really need to disable the command. How can I determine what is triggering it?

I've added the validate function to all viewControllers, custom views, control subclasses, appDelegate, and sceneDelegate. None of those is triggering it. I should be able to disable the command by using the validate function in the right object, but how do I determine which object it is? Can I trap something in UIResponder like, perhaps with a symbolic breakpoint? Any other ideas? I really don't want this context menu to appear.

One option would be to remove the Edit menu, but I don't want to do that. I actually want it, with its Undo and Redo options, but those options need to be validatable.

1

There are 1 best solutions below

0
Victor Engel On

I don't like this answer because I consider it a kludge. Also, it's specific to this key command. In Appdelegate I added

override var keyCommands: [UIKeyCommand]? {
    [
        UIKeyCommand(title: NSLocalizedString("Undo", comment: ""),
                                          image: nil,
                                          action: #selector(ViewController.undo),
                                          input: "z",
                                          modifierFlags: .command)
    ]
}

And in ViewController I added an empty @objc func undo() (which I may implement with something at some later date. For this scenario, though, there's nothing to undo.

In my validate function, the undo is now captured, so when I disable it, it actually is disabled.

Edit: the above stopped the menu, but the undo operation still happened. To catch that, I listed for NSNotification.Name.NSUndoManagerWillUndoChange. I didn't suppress the undo because that was actually OK. But I had to add a setNeedsDisplay so the view just undone would show the operation that was undone.