Keep macOS client running in the menu bar when its window closes
Retain the main Flutter window, add reopen and explicit quit actions, and preserve its controller while hidden. Fix native test host paths and cover the window and menu lifecycle with XCTest.
This commit is contained in:
@@ -3,10 +3,54 @@ import FlutterMacOS
|
||||
|
||||
@main
|
||||
class AppDelegate: FlutterAppDelegate {
|
||||
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
||||
private(set) var statusItem: NSStatusItem?
|
||||
// Flutter's nib outlet is weak; retain the same window and engine while hidden.
|
||||
private var retainedMainWindow: NSWindow?
|
||||
|
||||
override func applicationDidFinishLaunching(_ notification: Notification) {
|
||||
// FlutterAppDelegate initializes its UI in applicationWillFinishLaunching;
|
||||
// it has no applicationDidFinishLaunching implementation to call via super.
|
||||
retainedMainWindow = mainFlutterWindow
|
||||
mainFlutterWindow?.isReleasedWhenClosed = false
|
||||
|
||||
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
|
||||
let image = NSImage(systemSymbolName: "network", accessibilityDescription: "MeshPort")
|
||||
image?.isTemplate = true
|
||||
item.button?.image = image
|
||||
item.button?.toolTip = "MeshPort"
|
||||
item.button?.setAccessibilityLabel("MeshPort")
|
||||
let menu = NSMenu()
|
||||
let open = NSMenuItem(title: "打开 MeshPort", action: #selector(showMainWindow), keyEquivalent: "")
|
||||
open.target = self
|
||||
menu.addItem(open)
|
||||
menu.addItem(.separator())
|
||||
let quit = NSMenuItem(title: "退出 MeshPort", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
|
||||
quit.target = NSApp
|
||||
menu.addItem(quit)
|
||||
item.menu = menu
|
||||
statusItem = item
|
||||
}
|
||||
|
||||
@objc private func showMainWindow() {
|
||||
guard let window = retainedMainWindow else { return }
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
if window.isMiniaturized { window.deminiaturize(nil) }
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let item = statusItem { NSStatusBar.system.removeStatusItem(item) }
|
||||
}
|
||||
|
||||
override func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
||||
showMainWindow()
|
||||
return true
|
||||
}
|
||||
|
||||
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -14,6 +14,13 @@ class MainFlutterWindow: NSWindow {
|
||||
private var qrScanner: MeshQRScanner?
|
||||
private var importing = false
|
||||
|
||||
override func close() {
|
||||
// Hiding keeps the Flutter engine, drafts and active network sessions alive.
|
||||
// Only the explicit Quit action terminates the application.
|
||||
qrScanner?.cancelScan()
|
||||
orderOut(nil)
|
||||
}
|
||||
|
||||
override func awakeFromNib() {
|
||||
let flutterViewController = FlutterViewController()
|
||||
let windowFrame = self.frame
|
||||
|
||||
Reference in New Issue
Block a user