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.
58 lines
2.0 KiB
Swift
58 lines
2.0 KiB
Swift
import Cocoa
|
|
import FlutterMacOS
|
|
|
|
@main
|
|
class AppDelegate: FlutterAppDelegate {
|
|
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
|
|
}
|
|
}
|