BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / button / button_icon.rs

button_icon.rs

button_icon.rs 5.8 KiB Download Raw
1use crate::{Icon, Sizable, Size, progress::ProgressCircle, spinner::Spinner};
2use gpui::{App, IntoElement, RenderOnce, Window, prelude::FluentBuilder};
3
4/// Button icon which can be an Icon, Spinner, or Progress use for `icon` method of Button.
5#[doc(hidden)]
6#[derive(IntoElement)]
7pub struct ButtonIcon {
8 icon: ButtonIconVariant,
9 loading_icon: Option<Icon>,
10 loading: bool,
11 size: Size,
12}
13
14impl<T> From<T> for ButtonIcon
15where
16 T: Into<ButtonIconVariant>,
17{
18 fn from(icon: T) -> Self {
19 ButtonIcon::new(icon)
20 }
21}
22
23impl ButtonIcon {
24 /// Creates a new ButtonIcon with the given icon.
25 pub fn new(icon: impl Into<ButtonIconVariant>) -> Self {
26 Self {
27 icon: icon.into(),
28 loading_icon: None,
29 loading: false,
30 size: Size::Medium,
31 }
32 }
33
34 pub(crate) fn loading_icon(mut self, icon: Option<Icon>) -> Self {
35 self.loading_icon = icon;
36 self
37 }
38
39 pub(crate) fn loading(mut self, loading: bool) -> Self {
40 self.loading = loading;
41 self
42 }
43}
44
45impl Sizable for ButtonIcon {
46 fn with_size(mut self, size: impl Into<crate::Size>) -> Self {
47 self.size = size.into();
48 self
49 }
50}
51
52/// Button icon which can be an Icon, Spinner, Progress, or ProgressCircle use for `icon` method of Button.
53#[doc(hidden)]
54#[derive(IntoElement)]
55pub enum ButtonIconVariant {
56 Icon(Icon),
57 Spinner(Spinner),
58 Progress(ProgressCircle),
59}
60
61impl<T> From<T> for ButtonIconVariant
62where
63 T: Into<Icon>,
64{
65 fn from(icon: T) -> Self {
66 Self::Icon(icon.into())
67 }
68}
69
70impl From<Spinner> for ButtonIconVariant {
71 fn from(spinner: Spinner) -> Self {
72 Self::Spinner(spinner)
73 }
74}
75
76impl From<ProgressCircle> for ButtonIconVariant {
77 fn from(progress: ProgressCircle) -> Self {
78 Self::Progress(progress)
79 }
80}
81
82impl ButtonIconVariant {
83 /// Returns true if the ButtonIconKind is an Icon.
84 #[inline]
85 pub(crate) fn is_spinner(&self) -> bool {
86 matches!(self, Self::Spinner(_))
87 }
88
89 /// Returns true if the ButtonIconKind is a Progress or ProgressCircle.
90 #[inline]
91 pub(crate) fn is_progress(&self) -> bool {
92 matches!(self, Self::Progress(_))
93 }
94}
95
96impl Sizable for ButtonIconVariant {
97 fn with_size(self, size: impl Into<crate::Size>) -> Self {
98 match self {
99 Self::Icon(icon) => Self::Icon(icon.with_size(size)),
100 Self::Spinner(spinner) => Self::Spinner(spinner.with_size(size)),
101 Self::Progress(progress) => Self::Progress(progress.with_size(size)),
102 }
103 }
104}
105
106impl RenderOnce for ButtonIconVariant {
107 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
108 match self {
109 Self::Icon(icon) => icon.into_any_element(),
110 Self::Spinner(spinner) => spinner.into_any_element(),
111 Self::Progress(progress) => progress.into_any_element(),
112 }
113 }
114}
115
116impl RenderOnce for ButtonIcon {
117 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
118 if self.loading {
119 if self.icon.is_spinner() || self.icon.is_progress() {
120 self.icon.with_size(self.size).into_any_element()
121 } else {
122 Spinner::new()
123 .when_some(self.loading_icon, |this, icon| this.icon(icon))
124 .with_size(self.size)
125 .into_any_element()
126 }
127 } else {
128 self.icon.with_size(self.size).into_any_element()
129 }
130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136 use crate::IconName;
137
138 #[gpui::test]
139 fn test_button_icon_builder(_cx: &mut gpui::TestAppContext) {
140 let custom_icon = Icon::new(IconName::Loader);
141 let icon = ButtonIcon::new(IconName::Plus)
142 .loading(true)
143 .loading_icon(Some(custom_icon))
144 .large();
145
146 assert!(icon.loading);
147 assert!(icon.loading_icon.is_some());
148 assert_eq!(icon.size, Size::Large);
149 }
150
151 #[gpui::test]
152 fn test_button_icon_variant_types(_cx: &mut gpui::TestAppContext) {
153 // Test Icon variant
154 let icon_variant = ButtonIconVariant::Icon(Icon::new(IconName::Plus));
155 assert!(!icon_variant.is_spinner());
156 assert!(!icon_variant.is_progress());
157
158 // Test Spinner variant
159 let spinner_variant = ButtonIconVariant::Spinner(Spinner::new());
160 assert!(spinner_variant.is_spinner());
161 assert!(!spinner_variant.is_progress());
162
163 // Test Progress variant
164 let progress_variant = ButtonIconVariant::Progress(ProgressCircle::new(75));
165 assert!(!progress_variant.is_spinner());
166 assert!(progress_variant.is_progress());
167 }
168
169 #[test]
170 fn test_custom_data_icon_converts_through_component_slots() {
171 use crate::{icon::IconSource, menu::PopupMenuItem};
172
173 struct Search;
174 impl From<Search> for Icon {
175 fn from(_: Search) -> Self {
176 Icon::default().data(include_bytes!("../../../assets/assets/icons/search.svg"))
177 }
178 }
179
180 let button = ButtonIcon::from(Search)
181 .loading(true)
182 .loading_icon(Some(Search.into()));
183 let ButtonIconVariant::Icon(icon) = button.icon else {
184 panic!("custom data icons must use the standard icon variant");
185 };
186 assert!(matches!(icon.source_ref(), IconSource::Data(_)));
187 assert!(button.loading);
188 assert!(matches!(
189 button.loading_icon.unwrap().source_ref(),
190 IconSource::Data(_)
191 ));
192
193 let menu = PopupMenuItem::new("Search").icon(Search);
194 let PopupMenuItem::Item {
195 icon: Some(icon), ..
196 } = menu
197 else {
198 panic!("custom data icons must be accepted by menu slots");
199 };
200 assert!(matches!(icon.source_ref(), IconSource::Data(_)));
201 }
202}