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            // 托盘图标使用高清源,供 Windows 缩放到 32x32 呈现
28            ("tray_icon", "128x128@2x.png"),
29            ("web_pages", "web_pages.png"),
30            ("tips", "tips.png"),
31            ("terminal", "terminal.png"),
32            ("settings", "settings.png"),
33            ("refresh", "refresh.png"),
34            ("register", "register.png"),
35            ("game", "game.png"),
36            ("exit", "exit.png"),
37        ];
38
39        for (name, filename) in icons {
40            let path = icons_dir_path.join(filename);
41            icon_paths.insert(name.to_string(), path.to_string_lossy().to_string());
42        }
43
44        Self {
45            icon_paths,
46            icons_dir,
47        }
48    }
49
50    /// 根据图标名称获取对应的文件系统路径。
51    ///
52    /// 参数:
53    /// - name: 图标名称(如 "tray_icon", "web_pages", "tips" 等)
54    ///
55    /// 返回:若图标已注册则返回路径,否则返回 None。
56    pub fn get_icon_path(&self, name: &str) -> Option<String> {
57        self.icon_paths.get(name).map(|v| v.value().clone())
58    }
59
60    /// 获取图标资源目录。
61    ///
62    /// 返回:图标目录路径字符串引用。
63    pub fn icons_dir(&self) -> &str {
64        &self.icons_dir
65    }
66}