I have a menubar icon, and when I click on it, I want a new window to be opened. This part works. But the window is sometimes covered by other windows. How do I set the window to be on top of other windows?
I'd like to keep using as much as possible from SwiftUI.
Here's what I tried so far:
import SwiftData
import SwiftUI
@main
struct ScreenOnTimeApp: App {
@Environment(\.dismissWindow) private var dismissWindow
@Environment(\.openWindow) private var openWindow
var body: some Scene {
Window("State changes", id: "another-window-id") {
PowerChangesWindow().onAppear {
dismissWindow(id: "another-window-id")
}
}
Window("foobar", id: "foobar") {
VStack {
Text("Some Text ...")
}
.frame(width: 350, height: 250)
}
MenuBarExtra("1", systemImage: "1.circle") {
Button("One") {
// i want the window to be always above other windows...
dismissWindow(id: "foobar")
openWindow(id: "foobar")
}
Button("Two") {
NSApp.activate(ignoringOtherApps: true)
dismissWindow(id: "another-window-id")
openWindow(id: "another-window-id")
}
}
}
}
If you want a "floating" window, in macOS 15 you can use .windowLevel(.floating)
on the content view (or VStack in your case).
For earlier versions theres a solution here: https://stackoverflow.com/a/77184303/896907