Hi there, Dezy
Let's take this in smaller chunks
1)
Just a clarification here. - I know you're using SwiftUI based on the context of what you're saying and your last post. But for questions like this that's important context, since how you might do things varies a lot depending on the framework you use.
2)
Another little clarification, "ContentView" isn't strictly speaking a thing. It's just a default name. You can call it anything you want. It's just the default template name for the main View in Apple's template.
3)
Which leads us to: Why do you feel like you need code inside the ContentView struct as well? The menu items can be pretty independent from your main view. If you want you can set up state in the view that gets changed when the menu item is pressed to then update the view, but if you want to show an alert or dialogue box it can be entirely separate from that view so you won't need that.
Really, for the most part you can place code wherever you want as long as you tie things together. That's where the practice of architecture comes into play. Arguably one of the most important aspects of programming; Being able to architect the structure of your code in a manageable way. Nothing is prescribed in advance, the structure is yours to define to a great extend.
4)
More general and abstract response here. Do not think about your code here this is all general and abstract in regards to your broader question; How we can know when something has been clicked.
There are essentially two ways of detecting button presses. Polling and event-driven handling. What you want is almost always event driven, but I'll go through both.
In polling, every CPU timeslice your program gets, it checks to see if the button is being clicked. It's essentially
Code:
while(true) {
if(button.isClicked) {
reactToButtonClick()
}
}
(That was pseudo code)
In event-driven code what you instead do is you set up an event listener or "observer". so that the system lets you know when events happen. It's a pretty clever and powerful trick, where instead of every program checking their own state, the system just checks for button pushes and sends messages to all listeners when events happen.
To bring it a little back to your code, this is already the behaviour you see if you have buttons in your ContentView struct with attached behaviour. They are using an observer pattern behind the scenes. The button object has a list of listeners, and whenever it's clicked, it calls an action method on all its listeners which you can then define for the button.
5)
The concrete:
If you want to add a menu bar item that shows an alert to your app, here's one approach
Swift:
//
// SwiftUITest2App.swift
// SwiftUITest2
//
// Created by Casper Sørensen on 22/07/2021.
//
[B]import[/B] SwiftUI
[B]@main
struct[/B] SwiftUITest2App: App {
@State [B]private[/B] [B]var[/B] isShowingAlert = [B]false[/B]
[B]var[/B] body: [B]some[/B] Scene {
WindowGroup {
ContentView().alert(isPresented: $isShowingAlert, content: {
Alert(title: Text("UhOh! You clicked the button!"), message: Text("Now you'll have to click OK to get it to go away again"), dismissButton: .default(Text("OK")))
})
}
.commands {
CommandMenu("Click Me") {
Button("This will trigger alert…") {
isShowingAlert = [B]true[/B]
}
}
}
}
}
Sorry for the annoying formatting, it happens when I spy from Xcode.
Now there's a little catch here. - This only works when the ContentView is visible. If you close the view's window, no alert will be shown when the menu bar item is clicked, because the alert is tied to the content view. Unfortunately SwiftUI cannot present stand alone alerts not tied to a view at this time. A workaround would be to, instead of using an "Alert", you can build an Alert yourself and just instantiate that view when the command button is clicked instead. That should work globally. But this will present the alert when the menu bar item is clicked and the main view is not closed.