style.rs

  1use super::{
  2    rems, AbsoluteLength, Bounds, DefiniteLength, Edges, EdgesRefinement, FontStyle, FontWeight,
  3    Hsla, Length, Pixels, Point, PointRefinement, Rems, Result, RunStyle, SharedString, Size,
  4    SizeRefinement, ViewContext, WindowContext,
  5};
  6use crate::FontCache;
  7use refineable::Refineable;
  8pub use taffy::style::{
  9    AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
 10    Overflow, Position,
 11};
 12
 13#[derive(Clone, Refineable, Debug)]
 14#[refineable(debug)]
 15pub struct Style {
 16    /// What layout strategy should be used?
 17    pub display: Display,
 18
 19    // Overflow properties
 20    /// How children overflowing their container should affect layout
 21    #[refineable]
 22    pub overflow: Point<Overflow>,
 23    /// How much space (in points) should be reserved for the scrollbars of `Overflow::Scroll` and `Overflow::Auto` nodes.
 24    pub scrollbar_width: f32,
 25
 26    // Position properties
 27    /// What should the `position` value of this struct use as a base offset?
 28    pub position: Position,
 29    /// How should the position of this element be tweaked relative to the layout defined?
 30    #[refineable]
 31    pub inset: Edges<Length>,
 32
 33    // Size properies
 34    /// Sets the initial size of the item
 35    #[refineable]
 36    pub size: Size<Length>,
 37    /// Controls the minimum size of the item
 38    #[refineable]
 39    pub min_size: Size<Length>,
 40    /// Controls the maximum size of the item
 41    #[refineable]
 42    pub max_size: Size<Length>,
 43    /// Sets the preferred aspect ratio for the item. The ratio is calculated as width divided by height.
 44    pub aspect_ratio: Option<f32>,
 45
 46    // Spacing Properties
 47    /// How large should the margin be on each side?
 48    #[refineable]
 49    pub margin: Edges<Length>,
 50    /// How large should the padding be on each side?
 51    #[refineable]
 52    pub padding: Edges<DefiniteLength>,
 53    /// How large should the border be on each side?
 54    #[refineable]
 55    pub border_widths: Edges<AbsoluteLength>,
 56
 57    // Alignment properties
 58    /// How this node's children aligned in the cross/block axis?
 59    pub align_items: Option<AlignItems>,
 60    /// How this node should be aligned in the cross/block axis. Falls back to the parents [`AlignItems`] if not set
 61    pub align_self: Option<AlignSelf>,
 62    /// How should content contained within this item be aligned in the cross/block axis
 63    pub align_content: Option<AlignContent>,
 64    /// How should contained within this item be aligned in the main/inline axis
 65    pub justify_content: Option<JustifyContent>,
 66    /// How large should the gaps between items in a flex container be?
 67    #[refineable]
 68    pub gap: Size<DefiniteLength>,
 69
 70    // Flexbox properies
 71    /// Which direction does the main axis flow in?
 72    pub flex_direction: FlexDirection,
 73    /// Should elements wrap, or stay in a single line?
 74    pub flex_wrap: FlexWrap,
 75    /// Sets the initial main axis size of the item
 76    pub flex_basis: Length,
 77    /// The relative rate at which this item grows when it is expanding to fill space, 0.0 is the default value, and this value must be positive.
 78    pub flex_grow: f32,
 79    /// The relative rate at which this item shrinks when it is contracting to fit into space, 1.0 is the default value, and this value must be positive.
 80    pub flex_shrink: f32,
 81
 82    /// The fill color of this element
 83    pub fill: Option<Fill>,
 84
 85    /// The border color of this element
 86    pub border_color: Option<Hsla>,
 87
 88    /// The radius of the corners of this element
 89    #[refineable]
 90    pub corner_radii: CornerRadii,
 91
 92    /// The color of text within this element. Cascades to children unless overridden.
 93    pub text_color: Option<Hsla>,
 94
 95    /// The font size in rems.
 96    pub font_size: Option<Rems>,
 97
 98    pub font_family: Option<SharedString>,
 99
100    pub font_weight: Option<FontWeight>,
101
102    pub font_style: Option<FontStyle>,
103}
104
105#[derive(Refineable, Clone, Debug)]
106#[refineable(debug)]
107pub struct TextStyle {
108    pub color: Hsla,
109    pub font_family: SharedString,
110    pub font_size: Rems,
111    pub font_weight: FontWeight,
112    pub font_style: FontStyle,
113    pub underline: Option<UnderlineStyle>,
114}
115
116impl TextStyle {
117    pub fn highlight(mut self, style: HighlightStyle, _font_cache: &FontCache) -> Result<Self> {
118        if let Some(weight) = style.font_weight {
119            self.font_weight = weight;
120        }
121        if let Some(style) = style.font_style {
122            self.font_style = style;
123        }
124
125        if let Some(color) = style.color {
126            self.color = self.color.blend(color);
127        }
128
129        if let Some(factor) = style.fade_out {
130            self.color.fade_out(factor);
131        }
132
133        if let Some(underline) = style.underline {
134            self.underline = Some(underline);
135        }
136
137        Ok(self)
138    }
139
140    pub fn to_run(&self) -> RunStyle {
141        RunStyle {
142            font_id: todo!(),
143            color: self.color,
144            underline: self.underline.clone(),
145        }
146    }
147}
148
149#[derive(Clone, Debug, Default, PartialEq)]
150pub struct HighlightStyle {
151    pub color: Option<Hsla>,
152    pub font_weight: Option<FontWeight>,
153    pub font_style: Option<FontStyle>,
154    pub underline: Option<UnderlineStyle>,
155    pub fade_out: Option<f32>,
156}
157
158impl Eq for HighlightStyle {}
159
160impl Style {
161    pub fn text_style(&self, _cx: &WindowContext) -> Option<TextStyleRefinement> {
162        if self.text_color.is_none()
163            && self.font_size.is_none()
164            && self.font_family.is_none()
165            && self.font_weight.is_none()
166            && self.font_style.is_none()
167        {
168            return None;
169        }
170
171        Some(TextStyleRefinement {
172            color: self.text_color,
173            font_family: self.font_family.clone(),
174            font_size: self.font_size,
175            font_weight: self.font_weight,
176            font_style: self.font_style,
177            underline: None,
178        })
179    }
180
181    /// Paints the background of an element styled with this style.
182    pub fn paint_background<V: 'static>(&self, _bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
183        let _rem_size = cx.rem_size();
184        if let Some(_color) = self.fill.as_ref().and_then(Fill::color) {
185            todo!();
186        }
187    }
188
189    /// Paints the foreground of an element styled with this style.
190    pub fn paint_foreground<V: 'static>(&self, _bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
191        let rem_size = cx.rem_size();
192
193        if let Some(_color) = self.border_color {
194            let border = self.border_widths.to_pixels(rem_size);
195            if !border.is_empty() {
196                todo!();
197            }
198        }
199    }
200}
201
202impl Default for Style {
203    fn default() -> Self {
204        Style {
205            display: Display::Block,
206            overflow: Point {
207                x: Overflow::Visible,
208                y: Overflow::Visible,
209            },
210            scrollbar_width: 0.0,
211            position: Position::Relative,
212            inset: Edges::auto(),
213            margin: Edges::<Length>::zero(),
214            padding: Edges::<DefiniteLength>::zero(),
215            border_widths: Edges::<AbsoluteLength>::zero(),
216            size: Size::auto(),
217            min_size: Size::auto(),
218            max_size: Size::auto(),
219            aspect_ratio: None,
220            gap: Size::zero(),
221            // Aligment
222            align_items: None,
223            align_self: None,
224            align_content: None,
225            justify_content: None,
226            // Flexbox
227            flex_direction: FlexDirection::Row,
228            flex_wrap: FlexWrap::NoWrap,
229            flex_grow: 0.0,
230            flex_shrink: 1.0,
231            flex_basis: Length::Auto,
232            fill: None,
233            border_color: None,
234            corner_radii: CornerRadii::default(),
235            text_color: None,
236            font_size: Some(rems(1.)),
237            font_family: None,
238            font_weight: None,
239            font_style: None,
240        }
241    }
242}
243
244#[derive(Refineable, Clone, Default, Debug, PartialEq, Eq)]
245#[refineable(debug)]
246pub struct UnderlineStyle {
247    pub thickness: Pixels,
248    pub color: Option<Hsla>,
249    pub squiggly: bool,
250}
251
252#[derive(Clone, Debug)]
253pub enum Fill {
254    Color(Hsla),
255}
256
257impl Fill {
258    pub fn color(&self) -> Option<Hsla> {
259        match self {
260            Fill::Color(color) => Some(*color),
261        }
262    }
263}
264
265impl Default for Fill {
266    fn default() -> Self {
267        Self::Color(Hsla::default())
268    }
269}
270
271impl From<Hsla> for Fill {
272    fn from(color: Hsla) -> Self {
273        Self::Color(color)
274    }
275}
276
277#[derive(Clone, Refineable, Default, Debug)]
278#[refineable(debug)]
279pub struct CornerRadii {
280    top_left: AbsoluteLength,
281    top_right: AbsoluteLength,
282    bottom_left: AbsoluteLength,
283    bottom_right: AbsoluteLength,
284}
285
286impl From<TextStyle> for HighlightStyle {
287    fn from(other: TextStyle) -> Self {
288        Self::from(&other)
289    }
290}
291
292impl From<&TextStyle> for HighlightStyle {
293    fn from(other: &TextStyle) -> Self {
294        Self {
295            color: Some(other.color),
296            font_weight: Some(other.font_weight),
297            font_style: Some(other.font_style),
298            underline: other.underline.clone(),
299            fade_out: None,
300        }
301    }
302}
303
304impl HighlightStyle {
305    pub fn highlight(&mut self, other: HighlightStyle) {
306        match (self.color, other.color) {
307            (Some(self_color), Some(other_color)) => {
308                self.color = Some(Hsla::blend(other_color, self_color));
309            }
310            (None, Some(other_color)) => {
311                self.color = Some(other_color);
312            }
313            _ => {}
314        }
315
316        if other.font_weight.is_some() {
317            self.font_weight = other.font_weight;
318        }
319
320        if other.font_style.is_some() {
321            self.font_style = other.font_style;
322        }
323
324        if other.underline.is_some() {
325            self.underline = other.underline;
326        }
327
328        match (other.fade_out, self.fade_out) {
329            (Some(source_fade), None) => self.fade_out = Some(source_fade),
330            (Some(source_fade), Some(dest_fade)) => {
331                self.fade_out = Some((dest_fade * (1. + source_fade)).clamp(0., 1.));
332            }
333            _ => {}
334        }
335    }
336}
337
338impl From<Hsla> for HighlightStyle {
339    fn from(color: Hsla) -> Self {
340        Self {
341            color: Some(color),
342            ..Default::default()
343        }
344    }
345}