Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(tray): macOS 菜单栏补检查更新与导出诊断报告

longmeidao7b39afb72732
diff --git a/src/peach/tray.py b/src/peach/tray.py
index b2e59a990..a557df124 100644
--- a/src/peach/tray.py
+++ b/src/peach/tray.py
@@ -1590,6 +1590,28 @@ def restart_tray_process(
)
+_MACOS_DIAGNOSTICS_LOCK = threading.Lock()
+
+
+def export_macos_diagnostics(notify: Callable[[str, str], None]) -> None:
+ """与 Windows 托盘同一份报告;保存后在访达里选中它,不另做结果窗口。"""
+ if not _MACOS_DIAGNOSTICS_LOCK.acquire(blocking=False):
+ return
+
+ def work() -> None:
+ from .support_bundle import save_bundle
+ try:
+ path = save_bundle()
+ subprocess.run(["open", "-R", str(path)], check=False)
+ notify(f"诊断报告已保存:{path.name}", "Peach 诊断报告")
+ except Exception as error:
+ notify(error_reporting.failure_details("report-export", error), "Peach 诊断报告")
+ finally:
+ _MACOS_DIAGNOSTICS_LOCK.release()
+
+ threading.Thread(target=work, name="PeachDiagnostics", daemon=True).start()
+
+
def run_macos_menu_bar(manager: "ServiceManager", gate: "SetupGate | None" = None, *, silent: bool = False, show_browser: bool = False) -> None:
"""macOS 走原生菜单栏项。
@@ -1690,6 +1712,21 @@ def run_macos_menu_bar(manager: "ServiceManager", gate: "SetupGate | None" = Non
def open_configuration() -> None:
webbrowser.open(gate.open_url() + "configuration")
+ def check_updates() -> None:
+ if not action_lock.acquire(blocking=False):
+ return
+
+ def work() -> None:
+ try:
+ notify("正在检查更新…", "Peach")
+ result = versions.check()
+ state["snapshot"] = result.snapshot
+ notify(result.message, "Peach 版本与更新")
+ finally:
+ action_lock.release()
+
+ threading.Thread(target=work, name="PeachUpdate", daemon=True).start()
+
menu = MenuBarApp(
create_icon(template=True),
"Peach · 蜜桃",
@@ -1706,10 +1743,13 @@ def run_macos_menu_bar(manager: "ServiceManager", gate: "SetupGate | None" = Non
lambda label, action: (label, action), sync_ledger, take_ownership),
("重启服务", restart),
("查看日志", lambda: subprocess.run(["open", str(LOG_DIR)], check=False)),
- ("版本信息", Submenu(lambda: [
+ ("导出诊断报告", lambda: export_macos_diagnostics(notify)),
+ ("版本与更新", Submenu(lambda: [
(f"Peach {state['snapshot'].package_version}", None),
(menu_text(state['snapshot'].build_label), None),
(menu_text(state['snapshot'].channel_label), None),
+ (None, None),
+ ("检查更新", check_updates),
])),
(None, None),
("退出 Peach", quit_now),
diff --git a/tests/test_tray.py b/tests/test_tray.py
index f65f6d9cf..655b875b8 100644
--- a/tests/test_tray.py
+++ b/tests/test_tray.py
@@ -1120,6 +1120,59 @@ class MacMenuBarTests(unittest.TestCase):
self.assertTrue(str(specs["https"].verify).endswith("peach-local-ca.crt"))
+class MacMenuBarActionTests(unittest.TestCase):
+ """菜单栏用替身 AppKit 层构造,处理函数在任何系统上都能直接调用。"""
+
+ def test_menu_bar_offers_update_check_and_diagnostics_like_the_windows_tray(self):
+ """Windows 托盘有的「检查更新」「导出诊断报告」,菜单栏项同样要有,且真的接上实现。"""
+ import types
+ built = {}
+
+ class Submenu:
+ def __init__(self, items):
+ self.items = items
+
+ class MenuBarApp:
+ def __init__(self, _icon, _title, items):
+ built["items"] = items
+ self.notices = built.setdefault("notices", [])
+
+ def notify(self, message, title):
+ self.notices.append((title, message))
+
+ def run(self):
+ pass
+
+ def stop(self):
+ pass
+
+ def run_handler(label, *, submenu=None):
+ items = built["items"]
+ if submenu:
+ items = next(handler for title, handler in items if title == submenu).items()
+ handler = next(handler for title, handler in items if title == label)
+ handler()
+ for thread in threading.enumerate():
+ if thread.name in {"PeachUpdate", "PeachDiagnostics"}:
+ thread.join(5)
+
+ versions = Mock()
+ versions.inspect.return_value = Mock(package_version="0.9.0", build_label="独立测试包", channel_label="测试版")
+ versions.check.return_value = UpdateResult("available", "当前版本:0.9.0。最新测试版:1.0.0。有新版本可下载。",
+ versions.inspect.return_value)
+ fake = types.ModuleType("peach.menubar")
+ fake.MenuBarApp, fake.Submenu = MenuBarApp, Submenu
+ with patch.dict(sys.modules, {"peach.menubar": fake}), patch.object(tray_module, "VersionManager", return_value=versions), patch.object(tray_module, "standalone", return_value=True):
+ tray_module.run_macos_menu_bar(Mock(), Mock(waiting=False))
+ with tempfile.TemporaryDirectory() as directory, patch("peach.support_bundle.save_bundle", return_value=Path(directory) / "peach-support.zip"), patch.object(tray_module.subprocess, "run") as opened:
+ run_handler("导出诊断报告")
+ run_handler("检查更新", submenu="版本与更新")
+ self.assertEqual(opened.call_args.args[0][:2], ["open", "-R"])
+ versions.check.assert_called_once()
+ self.assertIn(("Peach 诊断报告", "诊断报告已保存:peach-support.zip"), built["notices"])
+ self.assertIn(("Peach 版本与更新", versions.check.return_value.message), built["notices"])
+
+
class TemplateHookTests(unittest.TestCase):
def test_apply_template_is_a_no_op_without_a_backing_nsimage(self):
"""拿不到底层 NSImage 就安静跳过:菜单栏项本身仍然可用。"""