From 8e45a57fca26607069fdaff4308a5a9de0321de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=94=9F=E6=9D=82=E7=89=A9=E6=88=BF?= <88823709+TC999@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:36:21 +0800 Subject: [PATCH] bak --- Cargo.toml | 1 + src/main.rs | 1 + src/subscription.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++ src/ui.rs | 19 ++++++++--- src/yaml_loader.rs | 29 +++++++--------- 5 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 src/subscription.rs diff --git a/Cargo.toml b/Cargo.toml index 15a4af6..707e568 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ sha2 = "0.10" native-dialog = "0.7.0" serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.9.34+deprecated" +reqwest = { version = "0.12.9", features = ["blocking", "json"] } diff --git a/src/main.rs b/src/main.rs index 638bd26..2097897 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod logger; // 引入日志模块 mod move_module; // 移动文件夹,使用 mklink 指令 mod open; // 调用资源管理器打开文件夹 mod scanner; // 引入扫盘模块 +mod subscription; mod ui; // 引入 ui 模块 mod utils; // 文件夹大小计算模块 mod yaml_loader; // 文件描述 diff --git a/src/subscription.rs b/src/subscription.rs new file mode 100644 index 0000000..8ec3566 --- /dev/null +++ b/src/subscription.rs @@ -0,0 +1,81 @@ +use eframe::egui; +use std::collections::HashMap; + +pub struct SubscriptionManager { + pub is_open: bool, + pub subscriptions: HashMap, // 显示已订阅规则,键为名称,值为描述或文件路径 + pub download_progress: Option, // 下载进度 + pub download_status: Option, // 下载状态信息 +} + +impl Default for SubscriptionManager { + fn default() -> Self { + Self { + is_open: false, + subscriptions: HashMap::new(), + download_progress: None, + download_status: None, + } + } +} + +impl SubscriptionManager { + pub fn show_window(&mut self, ctx: &egui::Context) { + if self.is_open { + egui::Window::new("订阅规则") + .open(&mut self.is_open) + .show(ctx, |ui| { + ui.horizontal(|ui| { + if ui.button("下载规则").clicked() { + self.start_download_dialog(ui); + } + if ui.button("从文件导入").clicked() { + self.import_from_file(); + } + }); + + ui.separator(); + ui.label("已订阅规则:"); + for (name, description) in &self.subscriptions { + ui.horizontal(|ui| { + ui.label(format!("{} - {}", name, description)); + if ui.button("删除").clicked() { + self.subscriptions.remove(name); + } + }); + } + + if let Some(progress) = self.download_progress { + ui.add(egui::ProgressBar::new(progress).show_percentage()); + } + if let Some(status) = &self.download_status { + ui.label(status); + } + }); + } + } + + fn start_download_dialog(&mut self, ui: &mut egui::Ui) { + ui.label("请输入规则下载链接:"); + let mut url = String::new(); + ui.text_edit_singleline(&mut url); + + if ui.button("确定").clicked() { + self.download_progress = Some(0.0); + self.download_status = Some("开始下载...".to_string()); + // 模拟下载流程 + // TODO: 实现异步下载逻辑,更新进度并完成时移除弹窗 + self.download_progress = Some(1.0); // 下载完成 + self.download_status = Some("下载完成".to_string()); + self.subscriptions + .insert("新规则".to_string(), "从网络下载".to_string()); + } + } + + fn import_from_file(&mut self) { + // TODO: 打开文件选择窗口,解析 YAML 并更新订阅规则 + // 示例添加一个规则 + self.subscriptions + .insert("本地规则".to_string(), "从文件导入".to_string()); + } +} diff --git a/src/ui.rs b/src/ui.rs index ca1e2cb..a0d92a7 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -11,6 +11,7 @@ use crate::yaml_loader::FolderDescriptions; use eframe::egui::{self, Grid, ScrollArea}; use std::collections::HashSet; use std::sync::mpsc::{Receiver, Sender}; +use subscription::SubscriptionManager; pub struct AppDataCleaner { is_scanning: bool, @@ -27,6 +28,7 @@ pub struct AppDataCleaner { move_module: move_module::MoveModule, // 移动模块实例 folder_descriptions: Option, yaml_error_logged: bool, // 新增字段,用于标记是否已经记录过错误 + subscription_manager: SubscriptionManager, } impl Default for AppDataCleaner { @@ -47,6 +49,7 @@ impl Default for AppDataCleaner { move_module: Default::default(), folder_descriptions: None, yaml_error_logged: false, // 初始时假定未记录过错误 + subscription_manager: Default::default(), } } } @@ -81,7 +84,9 @@ impl eframe::App for AppDataCleaner { // 加载描述文件 if self.folder_descriptions.is_none() { match FolderDescriptions::load_from_yaml("folders_description.yaml") { - Ok(descriptions) => self.folder_descriptions = Some(descriptions), + Ok(descriptions) => { + self.folder_descriptions = descriptions.into_iter().next(); + } Err(e) => { if !self.yaml_error_logged { eprintln!("加载 YAML 文件失败: {}", e); @@ -132,6 +137,9 @@ impl eframe::App for AppDataCleaner { self.show_about_window = true; // 打开关于窗口 ui.close_menu(); } + if ui.button("订阅").clicked() { + self.subscription_manager.is_open = true; + } ui.separator(); ui.checkbox(&mut self.is_logging_enabled, "启用日志"); @@ -194,9 +202,10 @@ impl eframe::App for AppDataCleaner { ui.label(utils::format_size(*size)); // 读取描述信息并显示 - let description = self.folder_descriptions.as_ref().and_then(|desc| { - desc.get_description(folder, &self.selected_appdata_folder) - }); + let description = self + .folder_descriptions + .as_ref() + .and_then(|desc| desc.get_description(folder)); if let Some(desc) = description { ui.label(desc); } else { @@ -247,5 +256,7 @@ impl eframe::App for AppDataCleaner { // 显示移动窗口 self.move_module.show_move_window(ctx); + // 显示订阅规则窗口 + self.subscription_manager.show_window(ctx); } } diff --git a/src/yaml_loader.rs b/src/yaml_loader.rs index 6edc5a4..655ebf4 100644 --- a/src/yaml_loader.rs +++ b/src/yaml_loader.rs @@ -1,36 +1,31 @@ use serde::{Deserialize, Serialize}; -use std::collections::HashMap; -use std::fs; -use std::path::Path; +use std::{collections::HashMap, fs, path::Path}; #[derive(Debug, Serialize, Deserialize)] pub struct FolderDescriptions { - pub Roaming: HashMap, - pub Local: HashMap, - pub LocalLow: HashMap, + pub name: String, // 规则名称 + pub folders: HashMap, // 文件夹描述 } impl FolderDescriptions { - pub fn load_from_yaml(file_path: &str) -> Result { + // 加载 YAML 文件,支持传入路径 + pub fn load_from_yaml(file_path: &str) -> Result, String> { let path = Path::new(file_path); if !path.exists() { return Err("YAML 文件未找到".to_string()); } - let content = fs::read_to_string(path).map_err(|e| format!("读取 YAML 文件失败: {}", e))?; + let content = fs::read_to_string(path) + .map_err(|e| format!("读取 YAML 文件失败: {}", e))?; - let descriptions: FolderDescriptions = - serde_yaml::from_str(&content).map_err(|e| format!("解析 YAML 文件失败: {}", e))?; + let descriptions: Vec = serde_yaml::from_str(&content) + .map_err(|e| format!("解析 YAML 文件失败: {}", e))?; Ok(descriptions) } - pub fn get_description(&self, folder_name: &str, folder_type: &str) -> Option { - match folder_type { - "Roaming" => self.Roaming.get(folder_name).cloned(), - "Local" => self.Local.get(folder_name).cloned(), - "LocalLow" => self.LocalLow.get(folder_name).cloned(), - _ => None, - } + // 获取指定文件夹的描述 + pub fn get_description(&self, folder_name: &str) -> Option { + self.folders.get(folder_name).cloned() } }