Skip to main content

zerolaunch_plugin_api/services/resource/
app_resource.rs

1use dashmap::DashMap;
2use std::path::PathBuf;
3
4/// 应用内置资源路径服务。
5/// 使用 Tauri 内置的资源解析能力,替代硬编码的资源目录路径拼接。
6/// 核心程序和插件均可通过 HostApi / PluginHandle 获取图标路径。
7pub struct AppResourceService {
8    /// 图标名称到文件系统路径的映射
9    icon_paths: DashMap<String, String>,
10    /// 图标资源目录
11    icons_dir: String,
12}
13
14impl AppResourceService {
15    /// 创建应用资源服务,注册所有内置图标的路径映射。
16    ///
17    /// 参数:
18    /// - icons_dir: 图标资源目录的完整路径(由调用方通过 Tauri 资源解析获得)
19    ///
20    /// 返回:初始化后的 AppResourceService。
21    pub fn new(icons_dir: String) -> Self {
22        let icon_paths = DashMap::new();
23
24        let icons_dir_path = PathBuf::from(&icons_dir);
25
26        let icons = [
27            ("tray_icon", "32x32.png"),
28            ("web_pages", "web_pages.png"),
29            ("tips", "tips.png"),
30            ("terminal", "terminal.png"),
31            ("settings", "settings.png"),
32            ("refresh", "refresh.png"),
33            ("register", "register.png"),
34            ("game", "game.png"),
35            ("exit", "exit.png"),
36        ];
37
38        for (name, filename) in icons {
39            let path = icons_dir_path.join(filename);
40            icon_paths.insert(name.to_string(), path.to_string_lossy().to_string());
41        }
42
43        Self {
44            icon_paths,
45            icons_dir,
46        }
47    }
48
49    /// 根据图标名称获取对应的文件系统路径。
50    ///
51    /// 参数:
52    /// - name: 图标名称(如 "tray_icon", "web_pages", "tips" 等)
53    ///
54    /// 返回:若图标已注册则返回路径,否则返回 None。
55    pub fn get_icon_path(&self, name: &str) -> Option<String> {
56        self.icon_paths.get(name).map(|v| v.value().clone())
57    }
58
59    /// 获取图标资源目录。
60    ///
61    /// 返回:图标目录路径字符串引用。
62    pub fn icons_dir(&self) -> &str {
63        &self.icons_dir
64    }
65}