优化,贴图

This commit is contained in:
zhongluofeng
2026-08-07 18:09:13 +08:00
parent e09b0567d6
commit d09599fa95
40 changed files with 1627 additions and 493 deletions
+80
View File
@@ -132,6 +132,41 @@ pub fn is_thing_elevated() -> bool {
false
}
// ===================== 监控设置持久化 =====================
/// 监控模块设置(存储于 {app_data_dir}/monitor/settings.json
/// 参照代理模块 ProxySettings 的模式,仅包含需要持久化的运行时开关。
#[derive(Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct MonitorSettings {
/// 应用启动时自动启动监控内核(默认 false)
#[serde(default)]
pub auto_start: bool,
}
/// 监控设置文件路径: {app_data_dir}/monitor/settings.json
fn settings_path(app_data_dir: &std::path::Path) -> PathBuf {
app_data_dir.join("monitor").join("settings.json")
}
/// 读取监控设置(文件不存在或解析失败时返回默认值)
pub fn load_monitor_settings(app_data_dir: &std::path::Path) -> MonitorSettings {
fs::read_to_string(settings_path(app_data_dir))
.ok()
.and_then(|s| serde_json::from_str::<MonitorSettings>(&s).ok())
.unwrap_or_default()
}
/// 写入监控设置
pub fn save_monitor_settings(app_data_dir: &std::path::Path, settings: &MonitorSettings) -> Result<(), String> {
let path = settings_path(app_data_dir);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
}
let content = serde_json::to_string_pretty(settings).map_err(|e| format!("序列化设置失败: {}", e))?;
fs::write(path, content).map_err(|e| format!("写入设置失败: {}", e))
}
// ===================== 永久提权标志持久化 =====================
/// 永久提权标志文件路径: {app_data_dir}/monitor/elevate.json
@@ -295,6 +330,32 @@ impl MonitorKernel {
write_elevate_flag(&self.root.join("elevate.json"), enabled)
}
/// 读取监控设置(auto_start 等)
pub fn load_settings(&self) -> MonitorSettings {
load_monitor_settings(&self.root.parent().unwrap_or(&self.root).to_path_buf())
}
/// 写入监控设置
pub fn save_settings(&self, settings: &MonitorSettings) -> Result<(), String> {
save_monitor_settings(&self.root.parent().unwrap_or(&self.root).to_path_buf(), settings)
}
/// 应用启动时检查是否需要自动启动监控内核
pub fn auto_start_on_launch(&self, app: &AppHandle) {
let settings = self.load_settings();
if !settings.auto_start {
return;
}
let monitor = self.clone();
let app_handle = app.clone();
tauri::async_runtime::spawn(async move {
match monitor.start_with_subscription(&app_handle).await {
Ok(info) => crate::logger::log_info("monitor", &format!("自动启动成功, pid={:?}", info.pid)),
Err(e) => crate::logger::log_warn("monitor", &format!("自动启动跳过: {}", e)),
}
});
}
fn cores_dir(&self) -> PathBuf {
self.root.join("cores")
}
@@ -950,6 +1011,25 @@ pub fn monitor_set_elevate_on_launch(
state.set_elevate_on_launch(enabled)
}
/// 查询"应用启动时自动启动监控内核"是否已启用
#[tauri::command]
pub fn monitor_get_auto_start(
state: tauri::State<'_, MonitorKernel>,
) -> bool {
state.load_settings().auto_start
}
/// 设置/清除"应用启动时自动启动监控内核"开关
#[tauri::command]
pub fn monitor_set_auto_start(
state: tauri::State<'_, MonitorKernel>,
enabled: bool,
) -> Result<(), String> {
let mut settings = state.load_settings();
settings.auto_start = enabled;
state.save_settings(&settings)
}
/// 查询硬件监控配置(透传 Kernel GET /config/hardware)。
/// 返回当前配置 + 可用硬件/传感器类型清单,供前端 Dialog 渲染。
#[tauri::command]