1use std::fmt;
4
5#[allow(unused_imports)]
6use std::borrow::Cow;
7
8#[allow(unused_imports)]
9use crate::model::icons::{icon_name, system_icon_set, system_icon_theme};
10use crate::model::{AnimatedIcon, IconData, IconProvider, IconRole, IconSet};
11#[allow(unused_imports)]
12use crate::model::{bundled_icon_by_name, bundled_icon_svg};
13
14#[derive(Debug)]
32#[non_exhaustive]
33pub enum IconId<'a> {
34 Role(IconRole),
36 Name(&'a str),
38 Custom(&'a dyn IconProvider),
40}
41
42impl From<IconRole> for IconId<'_> {
43 fn from(role: IconRole) -> Self {
44 IconId::Role(role)
45 }
46}
47
48impl<'a> From<&'a str> for IconId<'a> {
49 fn from(name: &'a str) -> Self {
50 IconId::Name(name)
51 }
52}
53
54impl<'a> From<&'a dyn IconProvider> for IconId<'a> {
55 fn from(provider: &'a dyn IconProvider) -> Self {
56 IconId::Custom(provider)
57 }
58}
59
60#[derive(Debug)]
98#[must_use]
99pub struct FreedesktopLoader<'a> {
100 #[allow(dead_code)]
102 id: IconId<'a>,
103 #[allow(dead_code)]
104 size: u16,
105 #[allow(dead_code)]
106 fg_color: Option<[u8; 3]>,
107 #[allow(dead_code)]
108 theme: Option<&'a str>,
109}
110
111impl<'a> FreedesktopLoader<'a> {
112 pub fn new(id: impl Into<IconId<'a>>) -> Self {
114 Self {
115 id: id.into(),
116 size: 24,
117 fg_color: None,
118 theme: None,
119 }
120 }
121
122 pub fn size(mut self, size: u16) -> Self {
124 self.size = size;
125 self
126 }
127
128 pub fn color(mut self, rgb: [u8; 3]) -> Self {
130 self.fg_color = Some(rgb);
131 self
132 }
133
134 pub fn color_opt(mut self, rgb: Option<[u8; 3]>) -> Self {
136 self.fg_color = rgb;
137 self
138 }
139
140 pub fn theme(mut self, theme: &'a str) -> Self {
143 self.theme = Some(theme);
144 self
145 }
146
147 #[must_use]
149 #[allow(unused_variables)]
150 pub fn load(self) -> Option<IconData> {
151 #[cfg(all(target_os = "linux", feature = "system-icons"))]
152 {
153 let detected;
154 let theme: &str = match self.theme {
155 Some(t) => t,
156 None => {
157 detected = system_icon_theme();
158 &detected
159 }
160 };
161 match self.id {
162 IconId::Role(role) => {
163 let name = icon_name(role, IconSet::Freedesktop)?;
164 crate::freedesktop::load_freedesktop_icon_by_name(
165 name,
166 theme,
167 self.size,
168 self.fg_color,
169 )
170 }
171 IconId::Name(name) => crate::freedesktop::load_freedesktop_icon_by_name(
172 name,
173 theme,
174 self.size,
175 self.fg_color,
176 ),
177 IconId::Custom(provider) => {
178 if let Some(name) = provider.icon_name(IconSet::Freedesktop)
179 && let Some(data) = crate::freedesktop::load_freedesktop_icon_by_name(
180 name,
181 theme,
182 self.size,
183 self.fg_color,
184 )
185 {
186 return Some(data);
187 }
188 provider.icon_svg(IconSet::Freedesktop).map(IconData::Svg)
189 }
190 }
191 }
192 #[cfg(not(all(target_os = "linux", feature = "system-icons")))]
193 {
194 None
195 }
196 }
197
198 #[must_use]
204 #[allow(unused_variables)]
205 pub fn load_indicator(theme: Option<&str>) -> Option<AnimatedIcon> {
206 #[cfg(all(target_os = "linux", feature = "system-icons"))]
207 {
208 crate::freedesktop::load_freedesktop_spinner(theme)
209 }
210 #[cfg(not(all(target_os = "linux", feature = "system-icons")))]
211 {
212 None
213 }
214 }
215}
216
217#[derive(Debug)]
222#[must_use]
223pub struct SfSymbolsLoader<'a> {
224 #[allow(dead_code)] id: IconId<'a>,
226}
227
228impl<'a> SfSymbolsLoader<'a> {
229 pub fn new(id: impl Into<IconId<'a>>) -> Self {
231 Self { id: id.into() }
232 }
233
234 #[must_use]
236 #[allow(unused_variables)]
237 pub fn load(self) -> Option<IconData> {
238 #[cfg(all(target_os = "macos", feature = "system-icons"))]
239 {
240 match self.id {
241 IconId::Role(role) => {
242 let name = icon_name(role, IconSet::SfSymbols)?;
243 crate::sficons::load_sf_icon_by_name(name)
244 }
245 IconId::Name(n) => crate::sficons::load_sf_icon_by_name(n),
246 IconId::Custom(p) => {
247 if let Some(n) = p.icon_name(IconSet::SfSymbols)
248 && let Some(data) = crate::sficons::load_sf_icon_by_name(n)
249 {
250 return Some(data);
251 }
252 p.icon_svg(IconSet::SfSymbols).map(IconData::Svg)
253 }
254 }
255 }
256 #[cfg(not(all(target_os = "macos", feature = "system-icons")))]
257 {
258 None
259 }
260 }
261 }
263
264#[derive(Debug)]
268#[must_use]
269pub struct SegoeIconsLoader<'a> {
270 #[allow(dead_code)] id: IconId<'a>,
272}
273
274impl<'a> SegoeIconsLoader<'a> {
275 pub fn new(id: impl Into<IconId<'a>>) -> Self {
277 Self { id: id.into() }
278 }
279
280 #[must_use]
282 #[allow(unused_variables)]
283 pub fn load(self) -> Option<IconData> {
284 #[cfg(all(target_os = "windows", feature = "system-icons"))]
285 {
286 match self.id {
287 IconId::Role(role) => {
288 let name = icon_name(role, IconSet::SegoeIcons)?;
289 crate::winicons::load_windows_icon_by_name(name)
290 }
291 IconId::Name(n) => crate::winicons::load_windows_icon_by_name(n),
292 IconId::Custom(p) => {
293 if let Some(n) = p.icon_name(IconSet::SegoeIcons)
294 && let Some(data) = crate::winicons::load_windows_icon_by_name(n)
295 {
296 return Some(data);
297 }
298 p.icon_svg(IconSet::SegoeIcons).map(IconData::Svg)
299 }
300 }
301 }
302 #[cfg(not(all(target_os = "windows", feature = "system-icons")))]
303 {
304 None
305 }
306 }
307 }
309
310#[derive(Debug)]
314#[must_use]
315pub struct MaterialLoader<'a> {
316 #[allow(dead_code)] id: IconId<'a>,
318}
319
320impl<'a> MaterialLoader<'a> {
321 pub fn new(id: impl Into<IconId<'a>>) -> Self {
323 Self { id: id.into() }
324 }
325
326 #[must_use]
328 #[allow(unused_variables)]
329 pub fn load(self) -> Option<IconData> {
330 #[cfg(feature = "material-icons")]
331 {
332 match self.id {
333 IconId::Role(role) => bundled_icon_svg(role, IconSet::Material)
334 .map(|b| IconData::Svg(Cow::Borrowed(b))),
335 IconId::Name(n) => bundled_icon_by_name(n, IconSet::Material)
336 .map(|b| IconData::Svg(Cow::Borrowed(b))),
337 IconId::Custom(p) => {
338 if let Some(n) = p.icon_name(IconSet::Material)
339 && let Some(b) = bundled_icon_by_name(n, IconSet::Material)
340 {
341 return Some(IconData::Svg(Cow::Borrowed(b)));
342 }
343 p.icon_svg(IconSet::Material).map(IconData::Svg)
344 }
345 }
346 }
347 #[cfg(not(feature = "material-icons"))]
348 {
349 None
350 }
351 }
352
353 #[must_use]
355 pub fn load_indicator() -> Option<AnimatedIcon> {
356 #[cfg(feature = "material-icons")]
357 {
358 Some(crate::spinners::material_spinner())
359 }
360 #[cfg(not(feature = "material-icons"))]
361 {
362 None
363 }
364 }
365}
366
367#[derive(Debug)]
369#[must_use]
370pub struct LucideLoader<'a> {
371 #[allow(dead_code)] id: IconId<'a>,
373}
374
375impl<'a> LucideLoader<'a> {
376 pub fn new(id: impl Into<IconId<'a>>) -> Self {
378 Self { id: id.into() }
379 }
380
381 #[must_use]
383 #[allow(unused_variables)]
384 pub fn load(self) -> Option<IconData> {
385 #[cfg(feature = "lucide-icons")]
386 {
387 match self.id {
388 IconId::Role(role) => {
389 bundled_icon_svg(role, IconSet::Lucide).map(|b| IconData::Svg(Cow::Borrowed(b)))
390 }
391 IconId::Name(n) => bundled_icon_by_name(n, IconSet::Lucide)
392 .map(|b| IconData::Svg(Cow::Borrowed(b))),
393 IconId::Custom(p) => {
394 if let Some(n) = p.icon_name(IconSet::Lucide)
395 && let Some(b) = bundled_icon_by_name(n, IconSet::Lucide)
396 {
397 return Some(IconData::Svg(Cow::Borrowed(b)));
398 }
399 p.icon_svg(IconSet::Lucide).map(IconData::Svg)
400 }
401 }
402 }
403 #[cfg(not(feature = "lucide-icons"))]
404 {
405 None
406 }
407 }
408
409 #[must_use]
411 pub fn load_indicator() -> Option<AnimatedIcon> {
412 #[cfg(feature = "lucide-icons")]
413 {
414 Some(crate::spinners::lucide_spinner())
415 }
416 #[cfg(not(feature = "lucide-icons"))]
417 {
418 None
419 }
420 }
421}
422
423#[must_use]
432pub fn load_icon<'a>(id: impl Into<IconId<'a>>, set: IconSet) -> Option<IconData> {
433 let id = id.into();
434 match set {
435 IconSet::Freedesktop => FreedesktopLoader::new(id).load(),
436 IconSet::SfSymbols => SfSymbolsLoader::new(id).load(),
437 IconSet::SegoeIcons => SegoeIconsLoader::new(id).load(),
438 IconSet::Material => MaterialLoader::new(id).load(),
439 IconSet::Lucide => LucideLoader::new(id).load(),
440 }
441}
442
443#[must_use]
449pub fn load_icon_indicator(set: IconSet) -> Option<AnimatedIcon> {
450 match set {
451 IconSet::Freedesktop => FreedesktopLoader::load_indicator(None),
452 IconSet::Material => MaterialLoader::load_indicator(),
453 IconSet::Lucide => LucideLoader::load_indicator(),
454 IconSet::SfSymbols | IconSet::SegoeIcons => None,
455 }
456}
457
458#[must_use]
466pub fn is_freedesktop_theme_available(theme: &str) -> bool {
467 #[cfg(target_os = "linux")]
468 {
469 let data_dirs = std::env::var("XDG_DATA_DIRS")
470 .unwrap_or_else(|_| "/usr/share:/usr/local/share".to_string());
471 for dir in data_dirs.split(':') {
472 if std::path::Path::new(dir)
473 .join("icons")
474 .join(theme)
475 .join("index.theme")
476 .exists()
477 {
478 return true;
479 }
480 }
481 let data_home = std::env::var("XDG_DATA_HOME").unwrap_or_else(|_| {
482 std::env::var("HOME")
483 .map(|h| format!("{h}/.local/share"))
484 .unwrap_or_default()
485 });
486 if !data_home.is_empty() {
487 return std::path::Path::new(&data_home)
488 .join("icons")
489 .join(theme)
490 .join("index.theme")
491 .exists();
492 }
493 false
494 }
495 #[cfg(not(target_os = "linux"))]
496 {
497 false
498 }
499}
500
501#[derive(Debug, Clone, PartialEq, Eq)]
512pub enum IconSetChoice {
513 Default(String),
525
526 System,
533
534 Freedesktop(String),
540
541 Material,
543
544 Lucide,
546}
547
548impl fmt::Display for IconSetChoice {
549 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
550 match self {
551 Self::Default(name) => write!(f, "default ({name})"),
552 Self::System => {
553 let name = system_icon_theme();
554 write!(f, "system ({name})")
555 }
556 Self::Freedesktop(name) => write!(f, "{name}"),
557 Self::Material => write!(f, "Material (bundled)"),
558 Self::Lucide => write!(f, "Lucide (bundled)"),
559 }
560 }
561}
562
563impl IconSetChoice {
564 #[must_use]
572 pub fn effective_icon_set(&self, theme_icon_set: IconSet) -> IconSet {
573 match self {
574 Self::Default(_) => theme_icon_set,
575 Self::System => system_icon_set(),
576 Self::Freedesktop(_) => IconSet::Freedesktop,
577 Self::Material => IconSet::Material,
578 Self::Lucide => IconSet::Lucide,
579 }
580 }
581
582 #[must_use]
594 pub fn freedesktop_theme(&self) -> Option<&str> {
595 match self {
596 Self::Default(name) | Self::Freedesktop(name) => Some(name),
597 Self::System | Self::Material | Self::Lucide => None,
598 }
599 }
600
601 #[must_use]
606 pub fn follows_preset(&self) -> bool {
607 matches!(self, Self::Default(_))
608 }
609}
610
611#[must_use]
622pub fn default_icon_choice(icon_set: IconSet, icon_theme: Option<&str>) -> IconSetChoice {
623 let Some(theme) = icon_theme else {
624 return IconSetChoice::System;
625 };
626 let available = match icon_set {
631 IconSet::Material | IconSet::Lucide => true,
632 IconSet::Freedesktop => is_freedesktop_theme_available(theme),
633 IconSet::SfSymbols | IconSet::SegoeIcons => true,
634 };
635 if available {
636 IconSetChoice::Default(theme.to_string())
637 } else {
638 IconSetChoice::System
639 }
640}
641
642#[must_use]
659pub fn list_freedesktop_themes() -> Vec<String> {
660 #[cfg(target_os = "linux")]
661 {
662 use std::collections::BTreeSet;
663 use std::io::BufRead;
664
665 let mut themes = BTreeSet::new();
666
667 let mut icon_dirs = Vec::new();
669
670 let data_dirs = std::env::var("XDG_DATA_DIRS")
671 .unwrap_or_else(|_| "/usr/share:/usr/local/share".to_string());
672 for dir in data_dirs.split(':') {
673 if !dir.is_empty() {
674 icon_dirs.push(std::path::PathBuf::from(dir).join("icons"));
675 }
676 }
677
678 let data_home = std::env::var("XDG_DATA_HOME").unwrap_or_else(|_| {
679 std::env::var("HOME")
680 .map(|h| format!("{h}/.local/share"))
681 .unwrap_or_default()
682 });
683 if !data_home.is_empty() {
684 icon_dirs.push(std::path::PathBuf::from(&data_home).join("icons"));
685 }
686
687 for icon_dir in &icon_dirs {
688 let entries = match std::fs::read_dir(icon_dir) {
689 Ok(e) => e,
690 Err(_) => continue,
691 };
692 for entry in entries.filter_map(|e| e.ok()) {
693 let path = entry.path();
694 if !path.is_dir() {
695 continue;
696 }
697 let name = match entry.file_name().into_string() {
698 Ok(n) => n,
699 Err(_) => continue,
700 };
701
702 if name == "hicolor" || name == "default" {
704 continue;
705 }
706
707 let index_path = path.join("index.theme");
708 let file = match std::fs::File::open(&index_path) {
709 Ok(f) => f,
710 Err(_) => continue,
711 };
712
713 let reader = std::io::BufReader::new(file);
716 let has_directories = reader
717 .lines()
718 .map_while(Result::ok)
719 .any(|line| line.starts_with("Directories="));
720
721 if has_directories {
722 themes.insert(name);
723 }
724 }
725 }
726
727 themes.into_iter().collect()
728 }
729 #[cfg(not(target_os = "linux"))]
730 {
731 Vec::new()
732 }
733}
734
735#[cfg(test)]
740#[allow(clippy::unwrap_used, clippy::expect_used)]
741mod load_icon_tests {
742 use super::*;
743
744 #[test]
745 #[cfg(all(target_os = "linux", feature = "system-icons"))]
746 fn freedesktop_loader_theme_override_honored_for_name_lookup() {
747 let result = FreedesktopLoader::new("format-text-rich")
754 .theme("Adwaita")
755 .size(24)
756 .load();
757 assert!(
758 result.is_some(),
759 "theme('Adwaita') must resolve 'format-text-rich' in Adwaita, regardless of system theme"
760 );
761 }
762
763 #[test]
764 #[cfg(feature = "material-icons")]
765 fn load_icon_material_returns_svg() {
766 let result = MaterialLoader::new(IconRole::ActionCopy).load();
767 assert!(result.is_some(), "material ActionCopy should return Some");
768 match result.unwrap() {
769 IconData::Svg(ref cow) => {
770 let s = String::from_utf8_lossy(cow);
771 assert!(s.contains("<svg"), "should contain SVG data");
772 }
773 _ => panic!("expected IconData::Svg for bundled material icon"),
774 }
775 }
776
777 #[test]
778 #[cfg(feature = "lucide-icons")]
779 fn load_icon_lucide_returns_svg() {
780 let result = LucideLoader::new(IconRole::ActionCopy).load();
781 assert!(result.is_some(), "lucide ActionCopy should return Some");
782 match result.unwrap() {
783 IconData::Svg(ref cow) => {
784 let s = String::from_utf8_lossy(cow);
785 assert!(s.contains("<svg"), "should contain SVG data");
786 }
787 _ => panic!("expected IconData::Svg for bundled lucide icon"),
788 }
789 }
790
791 #[test]
792 #[cfg(feature = "material-icons")]
793 fn load_icon_unknown_theme_no_cross_set_fallback() {
794 let result = FreedesktopLoader::new(IconRole::ActionCopy).load();
798 let _ = result;
802 }
803
804 #[test]
805 #[cfg(feature = "material-icons")]
806 fn load_icon_all_roles_material() {
807 let mut some_count = 0;
809 for role in IconRole::ALL {
810 if MaterialLoader::new(role).load().is_some() {
811 some_count += 1;
812 }
813 }
814 assert_eq!(
816 some_count, 42,
817 "Material should cover all 42 roles via bundled SVGs"
818 );
819 }
820
821 #[test]
822 #[cfg(feature = "lucide-icons")]
823 fn load_icon_all_roles_lucide() {
824 let mut some_count = 0;
825 for role in IconRole::ALL {
826 if LucideLoader::new(role).load().is_some() {
827 some_count += 1;
828 }
829 }
830 assert_eq!(
832 some_count, 42,
833 "Lucide should cover all 42 roles via bundled SVGs"
834 );
835 }
836
837 #[test]
838 fn load_icon_unrecognized_set_no_features() {
839 let _result = SfSymbolsLoader::new(IconRole::ActionCopy).load();
841 }
843
844 #[test]
845 #[cfg(feature = "material-icons")]
846 fn bundled_icon_load_produces_cow_borrowed() {
847 let result = MaterialLoader::new(IconRole::ActionCopy).load();
848 assert!(
849 matches!(result, Some(IconData::Svg(Cow::Borrowed(_)))),
850 "bundled icon should produce Some(IconData::Svg(Cow::Borrowed(_)))"
851 );
852 }
853}
854
855#[cfg(test)]
856#[allow(clippy::unwrap_used, clippy::expect_used)]
857mod load_system_icon_by_name_tests {
858 use super::*;
859
860 #[test]
861 #[cfg(feature = "material-icons")]
862 fn system_icon_by_name_material() {
863 let result = MaterialLoader::new("content_copy").load();
864 assert!(
865 result.is_some(),
866 "content_copy should be found in Material set"
867 );
868 assert!(matches!(result.unwrap(), IconData::Svg(_)));
869 }
870
871 #[test]
872 #[cfg(feature = "lucide-icons")]
873 fn system_icon_by_name_lucide() {
874 let result = LucideLoader::new("copy").load();
875 assert!(result.is_some(), "copy should be found in Lucide set");
876 assert!(matches!(result.unwrap(), IconData::Svg(_)));
877 }
878
879 #[test]
880 #[cfg(feature = "material-icons")]
881 fn system_icon_by_name_unknown_returns_none() {
882 let result = MaterialLoader::new("nonexistent_xyz").load();
883 assert!(result.is_none(), "nonexistent name should return None");
884 }
885
886 #[test]
887 fn system_icon_by_name_sf_on_linux_returns_none() {
888 #[cfg(not(target_os = "macos"))]
890 {
891 let result = SfSymbolsLoader::new("doc.on.doc").load();
892 assert!(
893 result.is_none(),
894 "SF Symbols should return None on non-macOS"
895 );
896 }
897 }
898}
899
900#[cfg(test)]
901#[allow(clippy::unwrap_used, clippy::expect_used)]
902mod load_custom_icon_tests {
903 use super::*;
904
905 #[test]
906 #[cfg(feature = "material-icons")]
907 fn custom_icon_with_icon_role_material() {
908 let provider: &dyn IconProvider = &IconRole::ActionCopy;
909 let result = MaterialLoader::new(provider).load();
910 assert!(
911 result.is_some(),
912 "IconRole::ActionCopy should load via material"
913 );
914 }
915
916 #[test]
917 #[cfg(feature = "lucide-icons")]
918 fn custom_icon_with_icon_role_lucide() {
919 let provider: &dyn IconProvider = &IconRole::ActionCopy;
920 let result = LucideLoader::new(provider).load();
921 assert!(
922 result.is_some(),
923 "IconRole::ActionCopy should load via lucide"
924 );
925 }
926
927 #[test]
928 fn custom_icon_no_cross_set_fallback() {
929 #[derive(Debug)]
931 struct NullProvider;
932 impl IconProvider for NullProvider {
933 fn icon_name(&self, _set: IconSet) -> Option<&str> {
934 None
935 }
936 fn icon_svg(&self, _set: IconSet) -> Option<Cow<'static, [u8]>> {
937 None
938 }
939 }
940
941 let provider: &dyn IconProvider = &NullProvider;
942 let result = MaterialLoader::new(provider).load();
943 assert!(
944 result.is_none(),
945 "NullProvider should return None (no cross-set fallback)"
946 );
947 }
948
949 #[test]
950 fn custom_icon_unknown_set_uses_system() {
951 #[derive(Debug)]
953 struct NullProvider;
954 impl IconProvider for NullProvider {
955 fn icon_name(&self, _set: IconSet) -> Option<&str> {
956 None
957 }
958 fn icon_svg(&self, _set: IconSet) -> Option<Cow<'static, [u8]>> {
959 None
960 }
961 }
962
963 let provider: &dyn IconProvider = &NullProvider;
965 let _result = FreedesktopLoader::new(provider).load();
966 }
967
968 #[test]
969 #[cfg(feature = "material-icons")]
970 fn custom_icon_via_dyn_dispatch() {
971 let boxed: Box<dyn IconProvider> = Box::new(IconRole::ActionCopy);
972 let provider: &dyn IconProvider = &*boxed;
973 let result = MaterialLoader::new(provider).load();
974 assert!(
975 result.is_some(),
976 "dyn dispatch through Box<dyn IconProvider> should work"
977 );
978 }
979
980 #[test]
981 #[cfg(feature = "material-icons")]
982 fn custom_icon_bundled_svg_fallback() {
983 #[derive(Debug)]
985 struct SvgOnlyProvider;
986 impl IconProvider for SvgOnlyProvider {
987 fn icon_name(&self, _set: IconSet) -> Option<&str> {
988 None
989 }
990 fn icon_svg(&self, _set: IconSet) -> Option<Cow<'static, [u8]>> {
991 Some(Cow::Borrowed(b"<svg>test</svg>"))
992 }
993 }
994
995 let provider: &dyn IconProvider = &SvgOnlyProvider;
996 let result = MaterialLoader::new(provider).load();
997 assert!(
998 result.is_some(),
999 "provider with icon_svg should return Some"
1000 );
1001 match result.unwrap() {
1002 IconData::Svg(ref cow) => {
1003 assert_eq!(cow.as_ref(), b"<svg>test</svg>");
1004 }
1005 _ => panic!("expected IconData::Svg"),
1006 }
1007 }
1008}
1009
1010#[cfg(test)]
1011#[allow(clippy::unwrap_used, clippy::expect_used)]
1012mod loading_indicator_tests {
1013 use super::*;
1014
1015 #[test]
1018 #[cfg(feature = "lucide-icons")]
1019 fn loading_indicator_lucide_returns_frames() {
1020 let anim = LucideLoader::load_indicator();
1021 assert!(anim.is_some(), "lucide should return Some");
1022 let anim = anim.unwrap();
1023 assert!(
1024 matches!(anim, AnimatedIcon::Frames(_)),
1025 "lucide should be pre-rotated Frames"
1026 );
1027 if let AnimatedIcon::Frames(data) = &anim {
1028 assert_eq!(data.frames().len(), 24);
1029 assert_eq!(data.frame_duration_ms().get(), 42);
1030 }
1031 }
1032
1033 #[test]
1036 #[cfg(all(target_os = "linux", feature = "system-icons"))]
1037 fn loading_indicator_freedesktop_depends_on_theme() {
1038 let anim = FreedesktopLoader::load_indicator(None);
1039 if let Some(anim) = anim {
1041 match anim {
1042 AnimatedIcon::Frames(data) => {
1043 assert!(
1044 !data.frames().is_empty(),
1045 "Frames variant should have at least one frame"
1046 );
1047 }
1048 AnimatedIcon::Transform(_) => {
1049 }
1051 _ => {}
1052 }
1053 }
1054 }
1055
1056 #[test]
1058 fn loading_indicator_freedesktop_does_not_panic() {
1059 let _result = FreedesktopLoader::load_indicator(None);
1060 }
1061
1062 #[test]
1065 #[cfg(feature = "lucide-icons")]
1066 fn lucide_spinner_is_frames() {
1067 let anim = crate::spinners::lucide_spinner();
1068 assert!(
1069 matches!(anim, AnimatedIcon::Frames(_)),
1070 "lucide should be pre-rotated Frames"
1071 );
1072 }
1073}
1074
1075#[cfg(test)]
1078#[allow(clippy::unwrap_used, clippy::expect_used)]
1079mod icon_loader_tests {
1080 use super::*;
1081
1082 #[test]
1083 #[cfg(feature = "material-icons")]
1084 fn icon_loader_basic_role() {
1085 let icon = MaterialLoader::new(IconRole::ActionCopy).load();
1086 assert!(icon.is_some());
1087 }
1088
1089 #[test]
1090 #[cfg(feature = "material-icons")]
1091 fn icon_loader_by_name() {
1092 let icon = MaterialLoader::new("content_copy").load();
1093 assert!(icon.is_some());
1094 }
1095
1096 #[test]
1097 #[cfg(feature = "material-icons")]
1098 fn icon_loader_custom_provider() {
1099 let provider: &dyn IconProvider = &IconRole::ActionCopy;
1100 let icon = MaterialLoader::new(provider).load();
1101 assert!(icon.is_some());
1102 }
1103
1104 #[test]
1105 #[cfg(all(target_os = "linux", feature = "system-icons"))]
1106 fn freedesktop_loader_builder_chain_compiles() {
1107 let _ = FreedesktopLoader::new(IconRole::ActionSave)
1111 .size(48)
1112 .color([0, 0, 0])
1113 .theme("Adwaita")
1114 .load();
1115 }
1116
1117 #[test]
1118 #[cfg(feature = "material-icons")]
1119 fn icon_loader_load_indicator() {
1120 let anim = MaterialLoader::load_indicator();
1121 assert!(anim.is_some());
1122 }
1123
1124 #[test]
1125 fn load_icon_free_fn_dispatches() {
1126 let _ = load_icon(IconRole::ActionCopy, IconSet::SfSymbols);
1129 }
1130
1131 #[test]
1132 fn load_icon_indicator_free_fn_dispatches() {
1133 assert!(load_icon_indicator(IconSet::SfSymbols).is_none());
1135 assert!(load_icon_indicator(IconSet::SegoeIcons).is_none());
1136 }
1137}
1138
1139#[cfg(all(test, feature = "svg-rasterize"))]
1140#[allow(clippy::unwrap_used, clippy::expect_used)]
1141mod spinner_rasterize_tests {
1142 use super::*;
1143
1144 #[test]
1145 #[cfg(feature = "lucide-icons")]
1146 fn lucide_spinner_icon_rasterizes() {
1147 let anim = crate::spinners::lucide_spinner();
1148 if let AnimatedIcon::Frames(data) = &anim {
1149 let first = data.frames().first();
1150 if let IconData::Svg(cow) = first {
1151 let result = crate::rasterize::rasterize_svg(cow, 24);
1152 assert!(result.is_ok(), "lucide loader should rasterize");
1153 if let Ok(IconData::Rgba { data, .. }) = &result {
1154 assert!(
1155 data.iter().any(|&b| b != 0),
1156 "lucide loader rasterized to empty image"
1157 );
1158 }
1159 } else {
1160 panic!("lucide spinner frame should be Svg");
1161 }
1162 } else {
1163 panic!("lucide spinner should be Frames");
1164 }
1165 }
1166}
1167
1168#[cfg(test)]
1173mod icon_set_choice_tests {
1174 use super::*;
1175
1176 #[test]
1177 fn test_icon_set_choice_display_default() {
1178 let choice = IconSetChoice::Default("Adwaita".to_string());
1179 assert_eq!(choice.to_string(), "default (Adwaita)");
1180 }
1181
1182 #[test]
1183 fn test_icon_set_choice_display_material() {
1184 assert_eq!(IconSetChoice::Material.to_string(), "Material (bundled)");
1185 }
1186
1187 #[test]
1188 fn test_icon_set_choice_display_lucide() {
1189 assert_eq!(IconSetChoice::Lucide.to_string(), "Lucide (bundled)");
1190 }
1191
1192 #[test]
1193 fn test_icon_set_choice_display_freedesktop() {
1194 let choice = IconSetChoice::Freedesktop("breeze".to_string());
1195 assert_eq!(choice.to_string(), "breeze");
1196 }
1197
1198 #[test]
1199 fn test_icon_set_choice_follows_preset() {
1200 assert!(IconSetChoice::Default("Adwaita".to_string()).follows_preset());
1201 assert!(!IconSetChoice::System.follows_preset());
1202 assert!(!IconSetChoice::Freedesktop("breeze".to_string()).follows_preset());
1203 assert!(!IconSetChoice::Material.follows_preset());
1204 assert!(!IconSetChoice::Lucide.follows_preset());
1205 }
1206
1207 #[test]
1208 fn test_icon_set_choice_effective_icon_set() {
1209 let choice = IconSetChoice::Default("Adwaita".to_string());
1211 assert_eq!(
1212 choice.effective_icon_set(IconSet::Freedesktop),
1213 IconSet::Freedesktop
1214 );
1215 assert_eq!(
1216 choice.effective_icon_set(IconSet::Material),
1217 IconSet::Material
1218 );
1219
1220 let sys = IconSetChoice::System.effective_icon_set(IconSet::Material);
1222 assert_eq!(sys, system_icon_set());
1223
1224 let choice = IconSetChoice::Freedesktop("breeze".to_string());
1226 assert_eq!(
1227 choice.effective_icon_set(IconSet::Material),
1228 IconSet::Freedesktop
1229 );
1230
1231 assert_eq!(
1233 IconSetChoice::Material.effective_icon_set(IconSet::Freedesktop),
1234 IconSet::Material
1235 );
1236 assert_eq!(
1237 IconSetChoice::Lucide.effective_icon_set(IconSet::Freedesktop),
1238 IconSet::Lucide
1239 );
1240 }
1241
1242 #[test]
1243 fn test_icon_set_choice_freedesktop_theme() {
1244 assert_eq!(
1245 IconSetChoice::Default("Adwaita".to_string()).freedesktop_theme(),
1246 Some("Adwaita")
1247 );
1248 assert_eq!(
1249 IconSetChoice::Freedesktop("breeze".to_string()).freedesktop_theme(),
1250 Some("breeze")
1251 );
1252 assert_eq!(IconSetChoice::System.freedesktop_theme(), None);
1253 assert_eq!(IconSetChoice::Material.freedesktop_theme(), None);
1254 assert_eq!(IconSetChoice::Lucide.freedesktop_theme(), None);
1255 }
1256
1257 #[test]
1258 fn test_default_icon_choice_none() {
1259 assert_eq!(
1261 default_icon_choice(IconSet::Freedesktop, None),
1262 IconSetChoice::System
1263 );
1264 assert_eq!(
1265 default_icon_choice(IconSet::Material, None),
1266 IconSetChoice::System
1267 );
1268 }
1269
1270 #[test]
1271 fn test_default_icon_choice_bundled() {
1272 assert_eq!(
1274 default_icon_choice(IconSet::Material, Some("any-theme")),
1275 IconSetChoice::Default("any-theme".to_string())
1276 );
1277 assert_eq!(
1278 default_icon_choice(IconSet::Lucide, Some("any-theme")),
1279 IconSetChoice::Default("any-theme".to_string())
1280 );
1281 }
1282
1283 #[test]
1284 fn test_list_freedesktop_themes_no_panic() {
1285 let themes = list_freedesktop_themes();
1287 #[cfg(not(target_os = "linux"))]
1290 assert!(themes.is_empty());
1291 let _ = themes;
1293 }
1294}