icon.rs

  1use std::sync::Arc;
  2
  3use gpui2::elements::svg;
  4use gpui2::Hsla;
  5use strum::EnumIter;
  6
  7use crate::prelude::*;
  8use crate::theme::theme;
  9use crate::Theme;
 10
 11#[derive(Default, PartialEq, Copy, Clone)]
 12pub enum IconSize {
 13    Small,
 14    #[default]
 15    Large,
 16}
 17
 18#[derive(Default, PartialEq, Copy, Clone)]
 19pub enum IconColor {
 20    #[default]
 21    Default,
 22    Muted,
 23    Disabled,
 24    Placeholder,
 25    Accent,
 26    Error,
 27    Warning,
 28    Success,
 29    Info,
 30}
 31
 32impl IconColor {
 33    pub fn color(self, theme: Arc<Theme>) -> Hsla {
 34        match self {
 35            IconColor::Default => theme.lowest.base.default.foreground,
 36            IconColor::Muted => theme.lowest.variant.default.foreground,
 37            IconColor::Disabled => theme.lowest.base.disabled.foreground,
 38            IconColor::Placeholder => theme.lowest.base.disabled.foreground,
 39            IconColor::Accent => theme.lowest.accent.default.foreground,
 40            IconColor::Error => theme.lowest.negative.default.foreground,
 41            IconColor::Warning => theme.lowest.warning.default.foreground,
 42            IconColor::Success => theme.lowest.positive.default.foreground,
 43            IconColor::Info => theme.lowest.accent.default.foreground,
 44        }
 45    }
 46}
 47
 48#[derive(Default, PartialEq, Copy, Clone, EnumIter)]
 49pub enum Icon {
 50    Ai,
 51    ArrowLeft,
 52    ArrowRight,
 53    ArrowUpRight,
 54    AudioOff,
 55    AudioOn,
 56    Bolt,
 57    ChevronDown,
 58    ChevronLeft,
 59    ChevronRight,
 60    ChevronUp,
 61    Close,
 62    ExclamationTriangle,
 63    File,
 64    FileGeneric,
 65    FileDoc,
 66    FileGit,
 67    FileLock,
 68    FileRust,
 69    FileToml,
 70    FileTree,
 71    Folder,
 72    FolderOpen,
 73    FolderX,
 74    #[default]
 75    Hash,
 76    InlayHint,
 77    MagicWand,
 78    MagnifyingGlass,
 79    Maximize,
 80    Menu,
 81    MessageBubbles,
 82    Mic,
 83    MicMute,
 84    Plus,
 85    Quote,
 86    Screen,
 87    SelectAll,
 88    Split,
 89    SplitMessage,
 90    Terminal,
 91    XCircle,
 92    Copilot,
 93    Envelope,
 94}
 95
 96impl Icon {
 97    pub fn path(self) -> &'static str {
 98        match self {
 99            Icon::Ai => "icons/ai.svg",
100            Icon::ArrowLeft => "icons/arrow_left.svg",
101            Icon::ArrowRight => "icons/arrow_right.svg",
102            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
103            Icon::AudioOff => "icons/speaker-off.svg",
104            Icon::AudioOn => "icons/speaker-loud.svg",
105            Icon::Bolt => "icons/bolt.svg",
106            Icon::ChevronDown => "icons/chevron_down.svg",
107            Icon::ChevronLeft => "icons/chevron_left.svg",
108            Icon::ChevronRight => "icons/chevron_right.svg",
109            Icon::ChevronUp => "icons/chevron_up.svg",
110            Icon::Close => "icons/x.svg",
111            Icon::ExclamationTriangle => "icons/warning.svg",
112            Icon::File => "icons/file.svg",
113            Icon::FileGeneric => "icons/file_icons/file.svg",
114            Icon::FileDoc => "icons/file_icons/book.svg",
115            Icon::FileGit => "icons/file_icons/git.svg",
116            Icon::FileLock => "icons/file_icons/lock.svg",
117            Icon::FileRust => "icons/file_icons/rust.svg",
118            Icon::FileToml => "icons/file_icons/toml.svg",
119            Icon::FileTree => "icons/project.svg",
120            Icon::Folder => "icons/file_icons/folder.svg",
121            Icon::FolderOpen => "icons/file_icons/folder_open.svg",
122            Icon::FolderX => "icons/stop_sharing.svg",
123            Icon::Hash => "icons/hash.svg",
124            Icon::InlayHint => "icons/inlay_hint.svg",
125            Icon::MagicWand => "icons/magic-wand.svg",
126            Icon::MagnifyingGlass => "icons/magnifying_glass.svg",
127            Icon::Maximize => "icons/maximize.svg",
128            Icon::Menu => "icons/menu.svg",
129            Icon::MessageBubbles => "icons/conversations.svg",
130            Icon::Mic => "icons/mic.svg",
131            Icon::MicMute => "icons/mic-mute.svg",
132            Icon::Plus => "icons/plus.svg",
133            Icon::Quote => "icons/quote.svg",
134            Icon::Screen => "icons/desktop.svg",
135            Icon::SelectAll => "icons/select-all.svg",
136            Icon::Split => "icons/split.svg",
137            Icon::SplitMessage => "icons/split_message.svg",
138            Icon::Terminal => "icons/terminal.svg",
139            Icon::XCircle => "icons/error.svg",
140            Icon::Copilot => "icons/copilot.svg",
141            Icon::Envelope => "icons/feedback.svg",
142        }
143    }
144}
145
146#[derive(Element, Clone)]
147pub struct IconElement {
148    icon: Icon,
149    color: IconColor,
150    size: IconSize,
151}
152
153impl IconElement {
154    pub fn new(icon: Icon) -> Self {
155        Self {
156            icon,
157            color: IconColor::default(),
158            size: IconSize::default(),
159        }
160    }
161
162    pub fn color(mut self, color: IconColor) -> Self {
163        self.color = color;
164        self
165    }
166
167    pub fn size(mut self, size: IconSize) -> Self {
168        self.size = size;
169        self
170    }
171
172    fn render<V: 'static>(&mut self, _: &mut V, cx: &mut ViewContext<V>) -> impl IntoElement<V> {
173        let theme = theme(cx);
174        let fill = self.color.color(theme);
175
176        let sized_svg = match self.size {
177            IconSize::Small => svg().size_3p5(),
178            IconSize::Large => svg().size_4(),
179        };
180
181        sized_svg.flex_none().path(self.icon.path()).fill(fill)
182    }
183}