waft_widgets/widgets/icon_data.rs
1//! Flutter counterpart: `widgets/icon_data.dart`.
2//!
3//! `IconDataProperty` (diagnostics) and the `staticIconProvider` tree-shaker annotation
4//! wait; neither changes what an icon is.
5
6use std::fmt;
7
8/// A description of an icon fulfilled by a font glyph.
9///
10/// See `Icons` for a number of predefined icons available for material design applications.
11///
12/// In release builds, the Flutter tool only includes the glyphs used by an app's `IconData`s
13/// in the font; here the font is installed whole.
14#[derive(Clone, PartialEq, Eq, Hash)]
15pub struct IconData {
16 /// The Unicode code point at which this icon is stored in the icon font.
17 pub code_point: u32,
18 /// The font family from which the glyph for the [`code_point`](Self::code_point) will be
19 /// selected.
20 pub font_family: Option<String>,
21 /// The name of the package from which the font family is included.
22 ///
23 /// The name is used by the `Icon` widget when configuring the `TextStyle` so that the
24 /// given [`font_family`](Self::font_family) is obtained from the appropriate asset.
25 ///
26 /// See also:
27 ///
28 /// * `TextStyle`, which describes how to use fonts from other packages.
29 pub font_package: Option<String>,
30 /// Whether this icon should be automatically mirrored in right-to-left environments.
31 ///
32 /// The `Icon` widget respects this value by mirroring the icon when the `Directionality`
33 /// is `TextDirection.rtl`.
34 pub match_text_direction: bool,
35 /// The ordered list of font families to fall back on when a glyph cannot be found in a
36 /// higher priority font family.
37 ///
38 /// For more details, refer to the documentation of `TextStyle`.
39 pub font_family_fallback: Option<Vec<String>>,
40}
41
42impl IconData {
43 /// Creates icon data.
44 ///
45 /// Rarely used directly. Instead, consider using one of the predefined icons like the
46 /// `Icons` collection.
47 ///
48 /// The [`font_package`](Self::font_package) argument must be non-null when using a font
49 /// family that is included in a package. This is used when selecting the font.
50 pub const fn new(code_point: u32) -> IconData {
51 IconData {
52 code_point,
53 font_family: None,
54 font_package: None,
55 match_text_direction: false,
56 font_family_fallback: None,
57 }
58 }
59
60 /// Dart `IconData(fontFamily:)`.
61 pub fn font_family(mut self, font_family: impl Into<String>) -> IconData {
62 self.font_family = Some(font_family.into());
63 self
64 }
65
66 /// Dart `IconData(fontPackage:)`.
67 pub fn font_package(mut self, font_package: impl Into<String>) -> IconData {
68 self.font_package = Some(font_package.into());
69 self
70 }
71
72 /// Dart `IconData(matchTextDirection:)`.
73 pub const fn match_text_direction(mut self, match_text_direction: bool) -> IconData {
74 self.match_text_direction = match_text_direction;
75 self
76 }
77
78 /// Dart `IconData(fontFamilyFallback:)`.
79 pub fn font_family_fallback(mut self, font_family_fallback: Vec<String>) -> IconData {
80 self.font_family_fallback = Some(font_family_fallback);
81 self
82 }
83}
84
85impl fmt::Debug for IconData {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 write!(f, "IconData(U+{:05X})", self.code_point)
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn equality_covers_every_field_and_debug_prints_the_code_point() {
97 let a = IconData::new(0xe900)
98 .font_family("CupertinoIcons")
99 .font_package("cupertino_icons");
100 let b = IconData::new(0xe900)
101 .font_family("CupertinoIcons")
102 .font_package("cupertino_icons");
103 assert_eq!(a, b);
104 assert_ne!(a, b.clone().match_text_direction(true));
105 assert_ne!(a, IconData::new(0xe901));
106 assert_eq!(format!("{a:?}"), "IconData(U+0E900)");
107 }
108}