From c94143dfaeb89f26421d87fddd21f5cd2e76980e Mon Sep 17 00:00:00 2001 From: shen <> Date: Mon, 12 Oct 2026 18:46:53 -0700 Subject: [PATCH] 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. --- .../macos/Runner.xcodeproj/project.pbxproj | 6 +-- .../xcshareddata/xcschemes/Runner.xcscheme | 8 ++-- .../macos/Runner/AppDelegate.swift | 46 ++++++++++++++++++- .../macos/Runner/MainFlutterWindow.swift | 7 +++ .../macos/RunnerTests/RunnerTests.swift | 43 +++++++++++++++-- docs/development-validation.md | 8 ++++ 6 files changed, 107 insertions(+), 11 deletions(-) diff --git a/apps/meshport_lab/macos/Runner.xcodeproj/project.pbxproj b/apps/meshport_lab/macos/Runner.xcodeproj/project.pbxproj index 540db7c..5f19385 100644 --- a/apps/meshport_lab/macos/Runner.xcodeproj/project.pbxproj +++ b/apps/meshport_lab/macos/Runner.xcodeproj/project.pbxproj @@ -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; }; diff --git a/apps/meshport_lab/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/apps/meshport_lab/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 88e0329..87feba2 100644 --- a/apps/meshport_lab/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/apps/meshport_lab/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -15,7 +15,7 @@ @@ -31,7 +31,7 @@ @@ -66,7 +66,7 @@ @@ -83,7 +83,7 @@ diff --git a/apps/meshport_lab/macos/Runner/AppDelegate.swift b/apps/meshport_lab/macos/Runner/AppDelegate.swift index b3c1761..69658ce 100644 --- a/apps/meshport_lab/macos/Runner/AppDelegate.swift +++ b/apps/meshport_lab/macos/Runner/AppDelegate.swift @@ -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 } diff --git a/apps/meshport_lab/macos/Runner/MainFlutterWindow.swift b/apps/meshport_lab/macos/Runner/MainFlutterWindow.swift index fb20150..08c1783 100644 --- a/apps/meshport_lab/macos/Runner/MainFlutterWindow.swift +++ b/apps/meshport_lab/macos/Runner/MainFlutterWindow.swift @@ -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 diff --git a/apps/meshport_lab/macos/RunnerTests/RunnerTests.swift b/apps/meshport_lab/macos/RunnerTests/RunnerTests.swift index 61f3bd1..9de16c1 100644 --- a/apps/meshport_lab/macos/RunnerTests/RunnerTests.swift +++ b/apps/meshport_lab/macos/RunnerTests/RunnerTests.swift @@ -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) } } diff --git a/docs/development-validation.md b/docs/development-validation.md index c04126f..dec5b5e 100644 --- a/docs/development-validation.md +++ b/docs/development-validation.md @@ -118,3 +118,11 @@ MI 8 Lite(Android 15 / API 35、ARM64、4 KB 页面)完成新一轮真机验 ### Android 扫码方向修复 用户反馈点击相机扫码后变成横屏。ZXing 4.3.0 的 CaptureActivity 库 manifest 默认 `sensorLandscape`;原有 `setOrientationLocked(false)` 只关闭额外锁定,不覆盖 manifest 的横屏要求。宿主 manifest 现在覆盖为 `fullUser`,跟随用户旋转设置,保留原有关闭方向锁定选项。合并 manifest 已确认 CaptureActivity 为 fullUser;构建、签名和 16 KB zipalign 检查通过。`outputs/android/MeshPort-0.1.0-1791812247-debug.apk` 已覆盖安装并打开,保留现有配置。此次只修改方向配置,未自动开启手机相机或扫描用户二维码。 + +### macOS 关闭窗口与菜单栏驻留 + +MainFlutterWindow.close 改为隐藏窗口,保留 Flutter 控制器和网络会话;取消最后窗口关闭时退出。AppDelegate 强持有主窗口与状态栏项,顶部图标提供“打开 MeshPort”和“退出 MeshPort”;Dock 重新打开同样恢复原窗口,不创建第二个引擎。关闭主窗口时取消正在进行的相机扫码。 + +原生 XCTest 两项通过:performClose 隐藏窗口但保留同一控制器,菜单打开动作恢复保留的窗口,退出项明确指向 NSApplication.terminate。修复了原有 RunnerTests 仍引用 meshport_lab.app 的 TEST_HOST 和 scheme 名称。使用隔离预览验证关闭后进程保留、明确退出后进程消失;界面工具无法直接读取状态栏项,其打开动作由原生检查覆盖。未在本轮切换真实 VPN 连接。 + +普通入口产物 `outputs/macos/MeshPort-0.1.0-menubar.app` 及 ZIP;未开启 MESHPORT_UI_PREVIEW。构建和深度严格签名验证通过,本机开发签名。测试预览已退出,原有配置未改动。