-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |