alfred_workflow_rust_project/
icon.rs

1use crate::common::EnumIdent;
2use serde::{Deserialize, Serialize};
3
4pub enum IconType {
5    IconFile,
6    FileType,
7    ImageSelf,
8}
9
10impl EnumIdent for IconType {
11    fn name(&self) -> &'static str {
12        match self {
13            IconType::IconFile => "fileicon",
14            IconType::FileType => "filetype",
15            IconType::ImageSelf => "",
16        }
17    }
18}
19
20#[derive(Serialize, Deserialize)]
21pub struct Icon {
22    #[serde(rename = "type")]
23    icon_type: String,
24    path: String,
25}
26
27impl Icon {
28    pub fn new(path: &str, icon_type: Option<IconType>) -> Icon {
29        Icon {
30            icon_type: if icon_type.is_none() {
31                IconType::ImageSelf.name().to_string()
32            } else {
33                icon_type.unwrap().name().to_string()
34            },
35            path: path.to_string(),
36        }
37    }
38}
39
40pub enum BuiltinIcon {
41    ACCOUNT,
42    BURN,
43    CLOCK,
44    COLOR,
45    COLOUR,
46    EJECT,
47    ERROR,
48    FAVORITE,
49    FAVOURITE,
50    GROUP,
51    HELP,
52    HOME,
53    INFO,
54    NETWORK,
55    NOTE,
56    SETTINGS,
57    SWIRL,
58    SWITCH,
59    SYNC,
60    TRASH,
61    USER,
62    WARNING,
63    WEB,
64}
65
66fn build_icon_path(icon_full_name: &str) -> String {
67    let mut sys_path =
68        "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/".to_string();
69    sys_path.push_str(icon_full_name);
70    sys_path
71}
72
73impl BuiltinIcon {
74    pub fn get_icon(self) -> Icon {
75        match self {
76            BuiltinIcon::ACCOUNT => Icon::new(
77                build_icon_path(&"Accounts.icns").as_str(),
78                None,
79            ),
80            BuiltinIcon::BURN => Icon::new(
81                build_icon_path(&"BurningIcon.icns").as_str(),
82                None,
83            ),
84            BuiltinIcon::CLOCK => Icon::new(
85                build_icon_path(&"Clock.icns").as_str(),
86                None,
87            ),
88            BuiltinIcon::COLOR => Icon::new(
89                build_icon_path(&"ProfileBackgroundColor.icns").as_str(),
90                None,
91            ),
92            BuiltinIcon::COLOUR => Icon::new(
93                build_icon_path(&"ProfileBackgroundColor.icns").as_str(),
94                None,
95            ),
96            BuiltinIcon::EJECT => Icon::new(
97                build_icon_path(&"EjectMediaIcon.icns").as_str(),
98                None,
99            ),
100            BuiltinIcon::ERROR => Icon::new(
101                build_icon_path(&"AlertStopIcon.icns").as_str(),
102                None,
103            ),
104            BuiltinIcon::FAVORITE => Icon::new(
105                build_icon_path(&"ToolbarFavoritesIcon.icns").as_str(),
106                None,
107            ),
108            BuiltinIcon::FAVOURITE => Icon::new(
109                build_icon_path(&"ToolbarFavoritesIcon.icns").as_str(),
110                None,
111            ),
112            BuiltinIcon::GROUP => Icon::new(
113                build_icon_path(&"GroupIcon.icns").as_str(),
114                None,
115            ),
116            BuiltinIcon::HELP => Icon::new(
117                build_icon_path(&"HelpIcon.icns").as_str(),
118                None,
119            ),
120            BuiltinIcon::HOME => Icon::new(
121                build_icon_path(&"HomeFolderIcon.icns").as_str(),
122                None,
123            ),
124            BuiltinIcon::INFO => Icon::new(
125                build_icon_path(&"ToolbarInfo.icns").as_str(),
126                None,
127            ),
128            BuiltinIcon::NETWORK => Icon::new(
129                build_icon_path(&"GenericNetworkIcon.icns").as_str(),
130                None,
131            ),
132            BuiltinIcon::NOTE => Icon::new(
133                build_icon_path(&"AlertNoteIcon.icns").as_str(),
134                None,
135            ),
136            BuiltinIcon::SETTINGS => Icon::new(
137                build_icon_path(&"ToolbarAdvanced.icns").as_str(),
138                None,
139            ),
140            BuiltinIcon::SWIRL => Icon::new(
141                build_icon_path(&"ErasingIcon.icns").as_str(),
142                None,
143            ),
144            BuiltinIcon::SWITCH => Icon::new(
145                build_icon_path(&"General.icns").as_str(),
146                None,
147            ),
148            BuiltinIcon::SYNC => Icon::new(
149                build_icon_path(&"Sync.icns").as_str(),
150                None,
151            ),
152            BuiltinIcon::TRASH => Icon::new(
153                build_icon_path(&"TrashIcon.icns").as_str(),
154                None,
155            ),
156            BuiltinIcon::USER => Icon::new(
157                build_icon_path(&"UserIcon.icns").as_str(),
158                None,
159            ),
160            BuiltinIcon::WARNING => Icon::new(
161                build_icon_path(&"AlertCautionIcon.icns").as_str(),
162                None,
163            ),
164            BuiltinIcon::WEB => Icon::new(
165                build_icon_path(&"BookmarkIcon.icns").as_str(),
166                None,
167            ),
168        }
169    }
170}
171
172// System icons