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:
@@ -464,7 +464,7 @@
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.meshportLab.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/meshport_lab.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/meshport_lab";
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MeshPort.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/MeshPort";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
@@ -479,7 +479,7 @@
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.meshportLab.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/meshport_lab.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/meshport_lab";
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MeshPort.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/MeshPort";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
@@ -494,7 +494,7 @@
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.meshportLab.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/meshport_lab.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/meshport_lab";
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MeshPort.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/MeshPort";
|
||||
};
|
||||
name = Profile;
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "33CC10EC2044A3C60003C045"
|
||||
BuildableName = "meshport_lab.app"
|
||||
BuildableName = "MeshPort.app"
|
||||
BlueprintName = "Runner"
|
||||
ReferencedContainer = "container:Runner.xcodeproj">
|
||||
</BuildableReference>
|
||||
@@ -31,7 +31,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "33CC10EC2044A3C60003C045"
|
||||
BuildableName = "meshport_lab.app"
|
||||
BuildableName = "MeshPort.app"
|
||||
BlueprintName = "Runner"
|
||||
ReferencedContainer = "container:Runner.xcodeproj">
|
||||
</BuildableReference>
|
||||
@@ -66,7 +66,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "33CC10EC2044A3C60003C045"
|
||||
BuildableName = "meshport_lab.app"
|
||||
BuildableName = "MeshPort.app"
|
||||
BlueprintName = "Runner"
|
||||
ReferencedContainer = "container:Runner.xcodeproj">
|
||||
</BuildableReference>
|
||||
@@ -83,7 +83,7 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "33CC10EC2044A3C60003C045"
|
||||
BuildableName = "meshport_lab.app"
|
||||
BuildableName = "MeshPort.app"
|
||||
BlueprintName = "Runner"
|
||||
ReferencedContainer = "container:Runner.xcodeproj">
|
||||
</BuildableReference>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,12 +1,49 @@
|
||||
import Cocoa
|
||||
import FlutterMacOS
|
||||
import XCTest
|
||||
@testable import MeshPort
|
||||
|
||||
class RunnerTests: XCTestCase {
|
||||
|
||||
func testExample() {
|
||||
// If you add code to the Runner application, consider adding tests here.
|
||||
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
|
||||
func testCloseRetainsControllerAndCanReopenSameWindow() {
|
||||
let window = MainFlutterWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300),
|
||||
styleMask: [.titled, .closable], backing: .buffered, defer: false)
|
||||
window.isReleasedWhenClosed = false
|
||||
let controller = NSViewController()
|
||||
controller.view = NSView(frame: window.contentLayoutRect)
|
||||
window.contentViewController = controller
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
XCTAssertTrue(window.isVisible)
|
||||
window.performClose(nil)
|
||||
XCTAssertFalse(window.isVisible)
|
||||
XCTAssertTrue(window.contentViewController === controller)
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
XCTAssertTrue(window.isVisible)
|
||||
XCTAssertTrue(window.contentViewController === controller)
|
||||
window.orderOut(nil)
|
||||
}
|
||||
|
||||
func testMenuReopensRetainedWindowAndQuitIsExplicit() {
|
||||
let delegate = AppDelegate()
|
||||
let window = MainFlutterWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300),
|
||||
styleMask: [.titled, .closable], backing: .buffered, defer: false)
|
||||
delegate.mainFlutterWindow = window
|
||||
delegate.applicationDidFinishLaunching(Notification(name: NSApplication.didFinishLaunchingNotification))
|
||||
XCTAssertFalse(delegate.applicationShouldTerminateAfterLastWindowClosed(NSApp))
|
||||
guard let menu = delegate.statusItem?.menu, let open = menu.items.first,
|
||||
let action = open.action, let quit = menu.items.last else {
|
||||
return XCTFail("Menu bar actions missing")
|
||||
}
|
||||
XCTAssertEqual(open.title, "打开 MeshPort")
|
||||
window.close()
|
||||
XCTAssertFalse(window.isVisible)
|
||||
XCTAssertTrue(NSApp.sendAction(action, to: open.target, from: open))
|
||||
XCTAssertTrue(window.isVisible)
|
||||
XCTAssertTrue(delegate.mainFlutterWindow === window)
|
||||
XCTAssertEqual(quit.title, "退出 MeshPort")
|
||||
XCTAssertTrue(quit.target === NSApp)
|
||||
XCTAssertEqual(quit.action, #selector(NSApplication.terminate(_:)))
|
||||
window.orderOut(nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user