How to change body of Menu if option-key pressed during tap?

I would like to create a menu that has a dynamic content (very easy to do), but I would like to sort them 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?

This is for MacOS, thanks!

    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") { }
        }
    }

You should take a look at the human interface guidelines. Maintaining the order of a menu even if the availability of actions has changed is specifically called out. From my own experience I haven't noticed any apps that do this.

Further, this question is specific to SwiftUI, which is Apple's private framework and is off-topic for this forum. If you have further questions about SwiftUI, I'd suggest that you ask instead over Apple Developer Forums, or even stack overflow.

Although I agree with you about SwiftUI and Apple frameworks, and the guidelines for these forums, it seems to me that this cat is out of the bag. For both Combine and SwiftUI, the leniency early on when they were first introduced has led, in my opinion, to the situation where "Using Swift" has become more of a place where people are more and more coming to get Apple programming help and tutorials, rather than about using Swift the language. I think the core team should delineate what is considered an appropriate question/comment about SwiftUI and Combine, and what should be discouraged by referring the author to Apple developer forums, developer sites, etc..

1 Like

I don’t know what cat out of the bag means in this context, but I don’t agree that early leniency justifies continual leniency if that’s the angle you’re going for.

The thing about SwiftUI being off-topic is much less about this is a pure Swift forum and more about the targeted recipients won’t be seeing this. While of course there are SwiftUI devs here on this forum, it uses Swift after all, I beckon there are many more on the official forum. This just risk fragmenting the SwiftUI community into to two lesser parts.

1 Like

The only reason I asked my original question here, was I saw the “swiftui” tag, and thought it would be appropriate.

What kinds of questions are on topic for use of the provided tag “swiftui”? Perhaps to avoid confusion, that and the “combine” tag should be replaced with something mentioning function builders and the underlying swift technology?

You can easily create your own tag. So :woman_shrugging:.

1 Like