Skip to main content

fc_ui/capabilities/primitives/
icon_source.rs

1//! Shared IconSource type for Icon, IconButton, and Button components.
2
3use gpui::SharedString;
4
5/// Enum representing the source of an icon - either a named icon or a file path.
6#[derive(Debug, Clone, PartialEq)]
7pub enum IconSource {
8    /// A named icon that will be resolved using the configured icon base path.
9    /// Example: "search" → "assets/icons/search.svg"
10    Named(String),
11
12    /// A direct file path to an icon.
13    /// Example: "custom/icons/logo.svg"
14    FilePath(SharedString),
15}
16
17/// Strip a trailing ".svg" extension regardless of its casing,
18/// returning the icon name without the extension.
19fn strip_svg_extension(s: &str) -> &str {
20    let trimmed = s.trim_end();
21    if trimmed.to_lowercase().ends_with(".svg") {
22        &trimmed[..trimmed.len() - ".svg".len()]
23    } else {
24        s
25    }
26}
27
28impl From<&str> for IconSource {
29    fn from(s: &str) -> Self {
30        // Check for path separators FIRST (more reliable than .svg check)
31        if s.contains('/') || s.contains('\\') {
32            IconSource::FilePath(SharedString::from(s.to_string()))
33        } else {
34            // If it ends with .svg but has no path separator, treat as named icon
35            // This handles cases like "my-icon.svg" which should be resolved
36            IconSource::Named(strip_svg_extension(s).to_string())
37        }
38    }
39}
40
41impl From<String> for IconSource {
42    fn from(s: String) -> Self {
43        if s.contains('/') || s.contains('\\') {
44            IconSource::FilePath(s.into())
45        } else {
46            IconSource::Named(strip_svg_extension(&s).to_string())
47        }
48    }
49}
50
51impl From<SharedString> for IconSource {
52    fn from(s: SharedString) -> Self {
53        let s_str = s.to_string();
54        if s_str.contains('/') || s_str.contains('\\') {
55            IconSource::FilePath(s)
56        } else {
57            IconSource::Named(strip_svg_extension(&s_str).to_string())
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_icon_source_detection() {
68        // Named icons
69        match IconSource::from("search") {
70            IconSource::Named(name) => assert_eq!(name, "search"),
71            _ => panic!("Should be Named"),
72        }
73
74        // File paths
75        match IconSource::from("custom/icon.svg") {
76            IconSource::FilePath(_) => {}
77            _ => panic!("Should be FilePath"),
78        }
79
80        // Edge case: .svg without path separator (treat as named)
81        match IconSource::from("my-icon.svg") {
82            IconSource::Named(name) => assert_eq!(name, "my-icon"),
83            _ => panic!("Should be Named"),
84        }
85
86        // Uppercase extension is stripped like the lowercase one
87        match IconSource::from("my-icon.SVG") {
88            IconSource::Named(name) => assert_eq!(name, "my-icon"),
89            _ => panic!("Should be Named"),
90        }
91
92        // Windows paths
93        match IconSource::from("custom\\icon.svg") {
94            IconSource::FilePath(_) => {}
95            _ => panic!("Should be FilePath"),
96        }
97    }
98}