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