Files
Starlight_Lancher/apps/app/src/api/settings.rs
T
AxTps ead942abb2
Axolotl desktop CI / guardrails (push) Has been cancelled
Axolotl desktop CI / desktop (macos-latest) (push) Has been cancelled
Axolotl desktop CI / desktop (ubuntu-latest) (push) Has been cancelled
Axolotl desktop CI / desktop (windows-latest) (push) Has been cancelled
Axolotl desktop CI / website (push) Has been cancelled
Repository checks / typos (push) Has been cancelled
Repository checks / tombi (push) Has been cancelled
Rust checks / shear (push) Has been cancelled
Sync source to CNB / Sync Git ref (push) Has been cancelled
feat: 移植忽略 SSL 证书错误选项(设置开关 + 重建 HTTP 客户端)
2026-10-02 21:20:26 +08:00

124 lines
3.3 KiB
Rust

use crate::api::Result;
use tauri::{Emitter, Manager, Runtime};
use theseus::prelude::*;
use theseus::{ProxyConfig, ProxyTestResult};
pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
tauri::plugin::Builder::new("settings")
.invoke_handler(tauri::generate_handler![
settings_get,
settings_set,
download_engine_set,
cancel_directory_change,
proxy_get,
proxy_set,
proxy_test,
set_ui_scale
])
.build()
}
// Get full settings
// invoke('plugin:settings|settings_get')
#[tauri::command]
pub async fn settings_get() -> Result<Settings> {
let res = settings::get().await?;
Ok(res)
}
// Set full settings
// invoke('plugin:settings|settings_set', settings)
#[tauri::command]
pub async fn settings_set(
app: tauri::AppHandle<impl Runtime>,
settings: Settings,
) -> Result<()> {
settings::set(settings.clone()).await?;
let state = State::get().await?;
state.update_http_client_for_settings(&settings).await?;
let _ = app.emit("settings", ());
Ok(())
}
#[tauri::command]
pub async fn set_ui_scale(
app: tauri::AppHandle<impl Runtime>,
factor: f64,
) -> Result<()> {
if let Some(window) = app.get_webview_window("main") {
let _ = window.set_zoom(factor);
}
Ok(())
}
#[tauri::command]
pub async fn download_engine_set(
engine: settings::DownloadEngine,
) -> Result<()> {
settings::set_download_engine(engine).await?;
Ok(())
}
#[tauri::command]
pub async fn cancel_directory_change<R: Runtime>(
app: tauri::AppHandle<R>,
) -> Result<()> {
let identifier = &app.config().identifier;
settings::cancel_directory_change(identifier).await?;
Ok(())
}
#[tauri::command]
pub async fn proxy_get() -> Result<ProxyConfig> {
let state = State::get().await?;
Ok(state.proxy_config().await?)
}
#[tauri::command]
pub async fn proxy_set(config: ProxyConfig) -> Result<()> {
let state = State::get().await?;
config.validate()?;
state.update_proxy_config(&config).await?;
Ok(())
}
#[tauri::command]
pub async fn proxy_test(config: ProxyConfig) -> Result<ProxyTestResult> {
if let Err(e) = config.validate() {
return Ok(ProxyTestResult {
success: false,
latency_ms: None,
message: e.to_string(),
});
}
let client = theseus::build_proxied_client(&config);
let started = std::time::Instant::now();
match client
.get("http://connect.rom.miui.com/generate_204")
.send()
.await
{
Ok(response) if response.status().is_success() => {
let latency_ms = started.elapsed().as_millis() as u64;
Ok(ProxyTestResult {
success: true,
latency_ms: Some(latency_ms),
message: format!("Connection successful ({latency_ms} ms)"),
})
}
Ok(response) => {
let latency_ms = started.elapsed().as_millis() as u64;
Ok(ProxyTestResult {
success: false,
latency_ms: Some(latency_ms),
message: format!("HTTP {}", response.status()),
})
}
Err(e) => Ok(ProxyTestResult {
success: false,
latency_ms: None,
message: format!("{e}"),
}),
}
}