Skip to main content

euv_core/style/text_prop/
impl.rs

1use super::*;
2
3/// Default typography style (`14px` Sans Regular, `1.4` line height).
4impl Default for TextStyle {
5    /// Creates the default UI text style.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default `TextStyle`.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            family: FontFamily::Sans,
14            font_size: 14.0,
15            weight: FontWeight::Normal,
16            line_height: 1.4,
17            letter_spacing: 0.0,
18            align: TextAlign::Left,
19            overflow: TextOverflow::Wrap,
20            color: COLOR_BLACK,
21            italic: false,
22            underline: false,
23            strikethrough: false,
24            text_select: None,
25            selection_color: None,
26        }
27    }
28}
29
30/// Optional selection highlight helpers for `TextStyle`.
31impl TextStyle {
32    /// Returns the explicitly configured `TextSelectMode` on this node, if any.
33    ///
34    /// # Arguments
35    ///
36    /// - `&Self` - Text style reference.
37    ///
38    /// # Returns
39    ///
40    /// - `Option<TextSelectMode>` - Explicit selection mode, or `None` to inherit from ancestors.
41    #[must_use]
42    #[inline(always)]
43    pub const fn text_select_mode(&self) -> Option<TextSelectMode> {
44        let Self { text_select, .. } = *self;
45        text_select
46    }
47
48    /// Returns the local `TextSelectMode` configured on this node (`TextSelectMode::None` when unset).
49    ///
50    /// # Arguments
51    ///
52    /// - `&Self` - Text style reference.
53    ///
54    /// # Returns
55    ///
56    /// - `TextSelectMode` - Local text selection mode.
57    #[must_use]
58    #[inline(always)]
59    pub const fn get_text_select(&self) -> TextSelectMode {
60        match self.text_select_mode() {
61            Some(mode) => mode,
62            None => TextSelectMode::None,
63        }
64    }
65
66    /// Returns whether this node locally enables text selection (`get_text_select() != TextSelectMode::None`).
67    ///
68    /// # Arguments
69    ///
70    /// - `&Self` - Text style reference.
71    ///
72    /// # Returns
73    ///
74    /// - `bool` - `true` when this node locally configures a selectable mode.
75    #[must_use]
76    #[inline(always)]
77    pub const fn get_selectable(&self) -> bool {
78        !matches!(self.get_text_select(), TextSelectMode::None)
79    }
80
81    /// Sets an explicit boolean selectability flag mapped onto `TextSelectMode`.
82    ///
83    /// # Arguments
84    ///
85    /// - `&Self` - Mutable text style reference.
86    /// - `bool` - `true` maps to `TextSelectMode::SelectAndCopy`, `false` maps to `TextSelectMode::None`.
87    ///
88    /// # Returns
89    ///
90    /// - `&mut Self` - Mutable text style reference for chaining.
91    #[inline(always)]
92    pub fn set_selectable(&mut self, selectable: bool) -> &mut Self {
93        let mode: TextSelectMode = if selectable {
94            TextSelectMode::SelectAndCopy
95        } else {
96            TextSelectMode::None
97        };
98        self.set_text_select(Some(mode))
99    }
100
101    /// Returns the optional custom selection highlight fill color.
102    ///
103    /// # Arguments
104    ///
105    /// - `&Self` - Text style reference.
106    ///
107    /// # Returns
108    ///
109    /// - `Option<Color>` - Selection highlight tint if explicitly configured.
110    #[must_use]
111    #[inline(always)]
112    pub fn selection_color(&self) -> Option<Color> {
113        let Self {
114            selection_color, ..
115        } = *self;
116        selection_color
117    }
118
119    /// Returns `true` when `other` differs only in color or selection properties and has identical layout metrics.
120    ///
121    /// # Arguments
122    ///
123    /// - `&Self` - Previous text style.
124    /// - `&Self` - Candidate updated text style.
125    ///
126    /// # Returns
127    ///
128    /// - `bool` - `true` when typography layout metrics are unchanged.
129    #[must_use]
130    #[inline(always)]
131    pub(crate) fn same_layout_metrics(&self, other: &Self) -> bool {
132        self.get_family() == other.get_family()
133            && self.get_font_size() == other.get_font_size()
134            && self.get_weight() == other.get_weight()
135            && self.get_line_height() == other.get_line_height()
136            && self.get_letter_spacing() == other.get_letter_spacing()
137            && self.get_align() == other.get_align()
138            && self.get_overflow() == other.get_overflow()
139            && self.get_italic() == other.get_italic()
140    }
141}
142
143/// Numeric weight helpers for `FontWeight`.
144impl FontWeight {
145    /// Returns the OpenType numeric weight (`300..=700`).
146    ///
147    /// # Arguments
148    ///
149    /// - `&Self` - Font weight variant.
150    ///
151    /// # Returns
152    ///
153    /// - `u16` - OpenType numeric weight value.
154    #[must_use]
155    #[inline(always)]
156    pub const fn numeric(&self) -> u16 {
157        match *self {
158            Self::Light => 300,
159            Self::Normal => 400,
160            Self::Medium => 500,
161            Self::SemiBold => 600,
162            Self::Bold => 700,
163        }
164    }
165
166    /// Returns true when the weight renders bold strokes (`>= 600`).
167    ///
168    /// # Arguments
169    ///
170    /// - `&Self` - Font weight variant.
171    ///
172    /// # Returns
173    ///
174    /// - `bool` - True for `SemiBold` and `Bold`.
175    #[must_use]
176    #[inline(always)]
177    pub const fn is_bold(&self) -> bool {
178        matches!(*self, Self::SemiBold | Self::Bold)
179    }
180}