sindresorhus/KeyboardShortcuts

How do I open a new window on MacOS using SwiftUI and this package?

cyrilzakka opened this issue · 3 comments

Hello, hope you're well! How do I open a new Window on MacOS using this package?

I tried the following:

import SwiftUI
import KeyboardShortcuts

@main
struct ChatApp: App {
    
    @StateObject private var appState = AppState()
    
    var body: some Scene {
        MenuBarExtra("", image: "AppGlyph", content: {
            Text("hi")
        }).menuBarExtraStyle(.window)
    
        Window("What's New", id: "whats-new") {
            Text("New in this version…")
        }
        
        Settings {
            SettingsPane()
        }
    }
}

@MainActor
final class AppState: ObservableObject {
    
    @Environment(\.openWindow) var openWindow
    
    init() {
        // Register the listener.
        KeyboardShortcuts.onKeyUp(for: .toggleChatWindow) { [self] in
            openWindow(id: "whats-new")
        }
    }
}

but I get the following:

Accessing Environment<OpenWindowAction>'s value outside of being installed on a View. This will always read the default value and will not update.

I was hoping to have a Mac menu bar icon that open a spotlight-like window with the keyboard shortcut.

Your question has nothing to do with this package. I recommend asking on Stack Overflow instead.

gcstr commented

hey, @cyrilzakka did you find out how to do it?