How to change content of SwiftUI menu if option was pressed during tap

Viewed 126

I would like to create a SwiftUI Menu that has a dynamic content (very easy to do), but I would like to sort the items if the option-key was pressed when the Menu was clicked. I have explored using gestures, but I can’t figure out how to change my state from the gesture in a way that won’t interfere with the operation of the Menu itself.

Is there a way to update a State variable based on tap+modifier key, that will occur before the Menu subviews are created, and won’t interfere with the normal operation of the Menu?

The Menu will have “recents” and might contain a few dozen entries. Being able to sort the history will make it faster to find a specific entry. I want to use a Menu because it’s very compact when not engaged.

The skeleton of what I am looking for is:

Menu("My Menu") {
    if <option-key pressed?> {
        // sorted  order
        Button("A") { }
        Button("B") { }
        Button("C") { }
        Button("D") { }
    } else {
        // natural order
        Button("B") { }
        Button("D") { }
        Button("A") { }
        Button("C") { }
    }
}

This is for MacOS, thanks!

0 Answers
Related