diff --git a/desktop/App.xaml.cs b/desktop/App.xaml.cs
index ecaacbe..68bbec1 100644
--- a/desktop/App.xaml.cs
+++ b/desktop/App.xaml.cs
@@ -71,6 +71,7 @@ public partial class App : Application
var version = GetType().Assembly.GetName().Version?.ToString() ?? "1.0.0";
_tray = new TrayIconService(_server.BaseUrl, version);
_tray.ShowRequested += ShowAppWindow;
+ _tray.RestartRequested += RestartApplication;
_tray.ExitRequested += ExitApplication;
UpdateTrayStatus();
@@ -280,7 +281,6 @@ public partial class App : Application
_main = new MainWindow(server, _settings, session);
_main.LogoutRequested += OnLogoutRequested;
_main.HiddenToTray += OnHiddenToTray;
- _main.ServiceStateChanged += UpdateTrayStatus;
}
ShowAppWindow();
@@ -317,6 +317,37 @@ public partial class App : Application
$"本机服务在 {_server?.BaseUrl} 继续提供,双击托盘图标可重新打开窗口。");
}
+ ///
+ /// 托盘菜单「重启软件」:先让本进程退出(释放单实例互斥体与 17300 端口),2 秒后再拉起自己。
+ /// 直接用 Process.Start 会撞上单实例检测(新进程把参数转发给正在退出的旧进程后就没了)。
+ ///
+ private void RestartApplication()
+ {
+ if (_exiting) return;
+
+ var exe = Environment.ProcessPath;
+ AppLog.Write($"重启软件:{exe}");
+
+ if (!string.IsNullOrEmpty(exe))
+ {
+ try
+ {
+ Process.Start(new ProcessStartInfo("cmd.exe", $"/c timeout /t 2 /nobreak >nul & start \"\" \"{exe}\"")
+ {
+ CreateNoWindow = true,
+ UseShellExecute = false,
+ WindowStyle = ProcessWindowStyle.Hidden,
+ });
+ }
+ catch (Exception ex)
+ {
+ AppLog.Write($"启动新进程失败:{ex.Message}");
+ }
+ }
+
+ ExitApplication();
+ }
+
// ---------------- 本机服务 ----------------
private void StartLocalServer()
diff --git a/desktop/MainWindow.xaml b/desktop/MainWindow.xaml
index 7ff4c1a..c7d99a0 100644
--- a/desktop/MainWindow.xaml
+++ b/desktop/MainWindow.xaml
@@ -48,13 +48,9 @@
-
-
-
-
-
-
+
+
窗口被隐藏到托盘(用来弹一次气泡提示)
public event Action? HiddenToTray;
- /// 本机服务的启/停状态变了(App 用它同步托盘上的文案)
- public event Action? ServiceStateChanged;
-
public MainWindow(LocalHttpServer server, AppSettings settings, Session session)
{
InitializeComponent();
@@ -76,39 +73,18 @@ public partial class MainWindow : Window
RunQuery();
}
- // ---------------- 服务启停 ----------------
-
- private void StartStop_Click(object sender, RoutedEventArgs e)
- {
- if (_server.IsRunning)
- {
- _server.Stop();
- }
- else
- {
- try
- {
- _server.Start();
- }
- catch (Exception ex)
- {
- AppLog.Write($"启动本机服务失败:{ex.Message}");
- MessageBox.Show(this, ex.Message, "启动失败", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
-
- UpdateServerState();
- }
+ // ---------------- 服务状态 ----------------
+ ///
+ /// 只展示运行状态:本机服务跟随程序生命周期(启动即开、退出即停),
+ /// 不再提供「停止服务」按钮 —— 停了网页就叫不醒,属于误操作。
+ ///
private void UpdateServerState()
{
var running = _server.IsRunning;
StatusText.Text = running ? $"运行中 · {_server.BaseUrl}" : "未启动";
StatusDot.Fill = (Brush)FindResource(running ? "OkBrush" : "BadBrush");
- StartStopButton.Content = running ? "停止服务" : "启动服务";
-
- ServiceStateChanged?.Invoke();
}
// ---------------- 启动协议 ----------------
@@ -183,14 +159,12 @@ public partial class MainWindow : Window
private void CopyAddress_Click(object sender, RoutedEventArgs e) => CopyText(_server.BaseUrl);
- private void CopyLaunchUrl_Click(object sender, RoutedEventArgs e)
- => CopyText(ProtocolHelper.LaunchUrl(QueryBox.Text.Trim()));
-
private void OpenInBrowser_Click(object sender, RoutedEventArgs e)
{
try
{
- Process.Start(new ProcessStartInfo($"{_server.BaseUrl}/api/health") { UseShellExecute = true });
+ // 根路径是本机服务的说明页(/api/health 已移除)
+ Process.Start(new ProcessStartInfo($"{_server.BaseUrl}/") { UseShellExecute = true });
}
catch (Exception ex)
{
diff --git a/desktop/README.md b/desktop/README.md
index 5a2a640..0f74f3c 100644
--- a/desktop/README.md
+++ b/desktop/README.md
@@ -41,10 +41,15 @@ desktop/
| 关窗口(X) | 隐藏到托盘,本机服务继续跑(第一次会冒一个气泡提示) |
| 再次双击 exe / 点网页「启动本机软件」 | 不会开第二个:把 `yuncang://open?code=…` 通过命名管道转给已运行实例 |
| 托盘双击 / 右键「打开主界面」 | 恢复窗口 |
+| 托盘右键「重启软件」 | 等 2 秒后拉起新进程(先退出才能释放单实例互斥体与 17300) |
| 「退出登录」 | 清令牌 → 关闭主窗口 → 回到登录页(取消登录则退出程序) |
| 网页请求唤起登录(`POST /api/activate-login`) | 未登录:登录窗弹到最前;已登录:主界面弹到最前(不影响本机服务) |
| 托盘右键「退出」 | 真正退出(dispose 托盘与本机服务) |
+> 主界面**没有「停止服务 / 启动服务」**:本机服务跟随程序生命周期(启动即开、退出即停)。
+> 留个停止按钮只会让用户误点后网页再也叫不醒软件(显示内容与「未安装」一模一样)。
+> 也**没有「复制唤醒地址」**:唤醒地址由插件生成,不需要在界面上手测。
+
## 环境配置(开发 / 生产)
和前端 `VITE_BACKEND` 一个思路:**由构建决定,不靠用户手填**。
@@ -348,8 +353,8 @@ reg query "HKCU\Software\Classes\yuncang\shell\open\command"
老配置升级时会自动迁移(v3 把协议写在网页模板里的,会改成「打开方式 = 直接唤起 + 网页模板还原」),行为不变。
> 判定依据是「本机服务在不在」,所以**软件已经在跑时不会误报**(这是最常见的场景:
-> 协议只是把参数转给已有实例,窗口不一定抢焦点)。真正的误报只剩一种:软件在跑但用户手动
-> 「停止服务」了 —— 那种情况提示一下也算合理。页面失焦只作为「快速判定成功」的辅助信号。
+> 协议只是把参数转给已有实例,窗口不一定抢焦点)。真正的误报只剩一种:软件还没完全启动就超时 ——
+> 那种情况提示一下也算合理。页面失焦只作为「快速判定成功」的辅助信号。
## ★ 接真实数据
diff --git a/desktop/Services/TrayIconService.cs b/desktop/Services/TrayIconService.cs
index 92d31ae..0507254 100644
--- a/desktop/Services/TrayIconService.cs
+++ b/desktop/Services/TrayIconService.cs
@@ -19,6 +19,9 @@ public sealed class TrayIconService : IDisposable
/// 要显示主界面(双击托盘 / 菜单「打开主界面」)
public event Action? ShowRequested;
+ /// 要从托盘菜单重启程序
+ public event Action? RestartRequested;
+
/// 要退出程序
public event Action? ExitRequested;
@@ -28,6 +31,7 @@ public sealed class TrayIconService : IDisposable
var menu = new WinForms.ContextMenuStrip();
menu.Items.Add(new WinForms.ToolStripMenuItem("打开主界面", null, (_, _) => ShowRequested?.Invoke()));
+ menu.Items.Add(new WinForms.ToolStripMenuItem("重启软件", null, (_, _) => RestartRequested?.Invoke()));
menu.Items.Add(_statusItem);
menu.Items.Add(new WinForms.ToolStripMenuItem($"版本 {version}") { Enabled = false });
menu.Items.Add(new WinForms.ToolStripSeparator());