Skip to content

Commit

Permalink
bak
Browse files Browse the repository at this point in the history
  • Loading branch information
TC999 committed Dec 11, 2024
1 parent b7e7bac commit 8e45a57
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod logger; // 引入日志模块
mod move_module; // 移动文件夹,使用 mklink 指令
mod open; // 调用资源管理器打开文件夹
mod scanner; // 引入扫盘模块
mod subscription;
mod ui; // 引入 ui 模块
mod utils; // 文件夹大小计算模块
mod yaml_loader; // 文件描述
Expand Down
81 changes: 81 additions & 0 deletions src/subscription.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use eframe::egui;
use std::collections::HashMap;

pub struct SubscriptionManager {
pub is_open: bool,
pub subscriptions: HashMap<String, String>, // 显示已订阅规则,键为名称,值为描述或文件路径
pub download_progress: Option<f32>, // 下载进度
pub download_status: Option<String>, // 下载状态信息
}

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());
}
}
19 changes: 15 additions & 4 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +28,7 @@ pub struct AppDataCleaner {
move_module: move_module::MoveModule, // 移动模块实例
folder_descriptions: Option<FolderDescriptions>,
yaml_error_logged: bool, // 新增字段,用于标记是否已经记录过错误
subscription_manager: SubscriptionManager,
}

impl Default for AppDataCleaner {
Expand All @@ -47,6 +49,7 @@ impl Default for AppDataCleaner {
move_module: Default::default(),
folder_descriptions: None,
yaml_error_logged: false, // 初始时假定未记录过错误
subscription_manager: Default::default(),
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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, "启用日志");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -247,5 +256,7 @@ impl eframe::App for AppDataCleaner {

// 显示移动窗口
self.move_module.show_move_window(ctx);
// 显示订阅规则窗口
self.subscription_manager.show_window(ctx);
}
}
29 changes: 12 additions & 17 deletions src/yaml_loader.rs
Original file line number Diff line number Diff line change
@@ -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<String, String>,
pub Local: HashMap<String, String>,
pub LocalLow: HashMap<String, String>,
pub name: String, // 规则名称
pub folders: HashMap<String, String>, // 文件夹描述
}

impl FolderDescriptions {
pub fn load_from_yaml(file_path: &str) -> Result<Self, String> {
// 加载 YAML 文件,支持传入路径
pub fn load_from_yaml(file_path: &str) -> Result<Vec<Self>, 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<FolderDescriptions> = 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<String> {
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<String> {
self.folders.get(folder_name).cloned()
}
}

0 comments on commit 8e45a57

Please sign in to comment.