feat: configure a persistent global launcher shortcut
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import AppKit
|
||||
import ConjureKit
|
||||
|
||||
@main
|
||||
@MainActor
|
||||
@@ -8,7 +9,7 @@ struct ConjureApp {
|
||||
let delegate = ApplicationDelegate()
|
||||
application.delegate = delegate
|
||||
application.setActivationPolicy(.regular)
|
||||
application.mainMenu = makeMainMenu()
|
||||
application.mainMenu = makeMainMenu(delegate: delegate)
|
||||
|
||||
// NSApplication holds its delegate weakly.
|
||||
withExtendedLifetime(delegate) {
|
||||
@@ -16,7 +17,7 @@ struct ConjureApp {
|
||||
}
|
||||
}
|
||||
|
||||
private static func makeMainMenu() -> NSMenu {
|
||||
private static func makeMainMenu(delegate: ApplicationDelegate) -> NSMenu {
|
||||
let menu = NSMenu()
|
||||
let applicationItem = NSMenuItem(title: "Conjure", action: nil, keyEquivalent: "")
|
||||
let applicationMenu = NSMenu(title: "Conjure")
|
||||
@@ -28,6 +29,12 @@ struct ConjureApp {
|
||||
action: #selector(NSApplication.orderFrontStandardAboutPanel(_:)),
|
||||
keyEquivalent: ""
|
||||
)
|
||||
let settings = applicationMenu.addItem(
|
||||
withTitle: "Settings…",
|
||||
action: #selector(ApplicationDelegate.showSettings(_:)),
|
||||
keyEquivalent: ","
|
||||
)
|
||||
settings.target = delegate
|
||||
applicationMenu.addItem(.separator())
|
||||
applicationMenu.addItem(
|
||||
withTitle: "Hide Conjure",
|
||||
@@ -84,9 +91,16 @@ struct ConjureApp {
|
||||
@MainActor
|
||||
private final class ApplicationDelegate: NSObject, NSApplicationDelegate {
|
||||
private lazy var launcher = LauncherWindowController()
|
||||
private lazy var shortcut = GlobalShortcut { [weak self] in self?.launcher.toggle() }
|
||||
private lazy var settings = SettingsWindowController(shortcut: shortcut)
|
||||
|
||||
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||
launcher.present()
|
||||
do {
|
||||
try shortcut.start()
|
||||
} catch {
|
||||
settings.present(error: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool {
|
||||
@@ -94,6 +108,14 @@ private final class ApplicationDelegate: NSObject, NSApplicationDelegate {
|
||||
return false
|
||||
}
|
||||
|
||||
@objc func showSettings(_ sender: Any?) {
|
||||
settings.present()
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ notification: Notification) {
|
||||
shortcut.stop()
|
||||
}
|
||||
|
||||
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
||||
false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user