fix: read clash mode tolerantly and propagate mode-switch failures

The home mode card read the proxy mode only from mihomo's /configs (strict
BaseConfig); when that response drifted a single field, the whole read
failed and the card showed a bare "communication error". patch_clash_mode
also swallowed backend PATCH failures and always returned Ok, so the UI
could not tell a switch had actually failed.

- add a tolerant get_clash_mode command that reads mode from the saved
  clash config (IClashTemp::get_mode), bypassing strict BaseConfig
  deserialization, and
- make change_clash_mode return Result and have patch_clash_mode propagate
  it; the hotkey/tray callers log-and-ignore as before.
This commit is contained in:
wonfen
2026-06-27 15:30:20 +08:00
parent 1026ba88bb
commit d0d2f4e4f5
6 changed files with 59 additions and 27 deletions
+13 -2
View File
@@ -36,10 +36,21 @@ pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
}
/// 修改Clash模式
///
/// 将 `change_clash_mode` 的失败上抛给前端,使前端 `catch` 能真正感知后端 PATCH 失败
/// 并提示用户(此前命令始终返回 `Ok(())`,吞掉了后端错误)。
#[tauri::command]
pub async fn patch_clash_mode(payload: String) -> CmdResult {
feat::change_clash_mode(payload).await;
Ok(())
feat::change_clash_mode(payload).await
}
/// 获取当前 Clash 模式(容错读取)
///
/// 直接读取已保存的 clash 配置中的 `mode`,绕开 mihomo `/configs` 的严格
/// `BaseConfig` 反序列化,作为主页 mode 显示的兜底来源。
#[tauri::command]
pub async fn get_clash_mode() -> CmdResult<Option<String>> {
Ok(Config::clash().await.data_arc().get_mode().map(Into::into))
}
/// 切换Clash核心