Skip to main content

alchemy_styles/
styles.rs

1/// Implements the various `Style` types used for computing Flexbox layouts,
2/// along with appearance-based styles (`Color`s, etc).
3
4#[cfg(feature="tokenize")]
5use proc_macro2::{TokenStream, Ident, Span};
6
7#[cfg(feature="tokenize")]
8use quote::{quote, ToTokens};
9
10pub use crate::color::Color;
11
12pub use crate::stretch::geometry::{Point, Rect, Size};
13pub use crate::stretch::number::Number;
14pub use crate::stretch::result::Layout;
15
16pub use crate::stretch::style::{
17    Style,
18    AlignContent, AlignItems, AlignSelf, Dimension, Direction, Display,
19    FlexDirection, JustifyContent, Overflow, PositionType, FlexWrap
20};
21
22/// Describes the backface-visibility for a view. This may be removed in a later release.
23#[derive(Copy, Clone, PartialEq, Debug)]
24pub enum BackfaceVisibility {
25    Visible,
26    Hidden
27}
28
29impl Default for BackfaceVisibility {
30    fn default() -> BackfaceVisibility {
31        BackfaceVisibility::Visible
32    }
33}
34
35/// Describes a font style.
36#[derive(Copy, Clone, PartialEq, Debug)]
37pub enum FontStyle {
38    Normal,
39    Italic,
40    Oblique
41}
42
43impl Default for FontStyle {
44    fn default() -> FontStyle {
45        FontStyle::Normal
46    }
47}
48
49/// Describes a font weight.
50#[derive(Copy, Clone, PartialEq, Debug)]
51pub enum FontWeight {
52    Normal,
53    Bold
54}
55
56impl Default for FontWeight {
57    fn default() -> FontWeight {
58        FontWeight::Normal
59    }
60}
61
62/// Describes how text should be aligned.
63#[derive(Copy, Clone, PartialEq, Debug)]
64pub enum TextAlignment {
65    Auto,
66    Left,
67    Right,
68    Center,
69    Justify
70}
71
72impl Default for TextAlignment {
73    fn default() -> TextAlignment {
74        TextAlignment::Auto
75    }
76}
77
78/// Describes a border style.
79#[derive(Copy, Clone, PartialEq, Debug)]
80pub enum BorderStyle {
81    None, // The CSS value is None, but it's a reserved term in Rust ;P
82    Hidden,
83    Solid
84}
85
86impl Default for BorderStyle {
87    fn default() -> BorderStyle {
88        BorderStyle::None
89    }
90}
91
92/// Describes how a Font Family
93#[derive(Copy, Clone, PartialEq, Debug)]
94pub enum FontFamily {
95    SansSerif // @TODO This is tricky because of &str/String/Copy. Revisit later.
96}
97
98impl Default for FontFamily {
99    fn default() -> Self {
100        FontFamily::SansSerif
101    }
102}
103
104/// When applying layout to a backing view, you'll get two calls - one with a `Layout`, 
105/// which contains the computed frame, and one with an `Appearance`, which contains things 
106/// like colors, fonts, and so on.
107pub struct Appearance {
108    pub background_color: Color,
109    pub font_size: f32,
110    pub font_style: FontStyle,
111    pub font_weight: FontWeight,
112    pub opacity: f32,
113    pub text_alignment: TextAlignment,
114    pub text_color: Color,
115    pub text_decoration_color: Color,
116    pub text_shadow_color: Color,
117    pub tint_color: Color
118}
119
120impl Default for Appearance {
121    fn default() -> Appearance {
122        Appearance {
123            background_color: Color::transparent(),
124            // @TODO: We can definitely judge a default value better here. 
125            font_size: 14.,
126            font_style: FontStyle::default(),
127            font_weight: FontWeight::default(),
128            opacity: 1.,
129            text_alignment: TextAlignment::default(),
130            text_color: Color::transparent(),
131            text_decoration_color: Color::transparent(),
132            text_shadow_color: Color::transparent(),
133            tint_color: Color::transparent()
134        }
135    }
136}
137
138/// These exist purely for use in the parser code.
139///
140/// A `Style` is what's used for a node; `Styles` are what's parsed and stored.
141/// At render-time, the rendering engine takes n styles and reduces them down into 1 `Style`
142/// that's applied to the node in question.
143#[derive(Debug)]
144pub enum Styles {
145    AlignContent(AlignContent),
146    AlignItems(AlignItems),
147    AlignSelf(AlignSelf),
148    AspectRatio(Number),
149    BackfaceVisibility(BackfaceVisibility),
150    BackgroundColor(Color),
151
152    BorderColor(Color),
153    BorderEndColor(Color),
154    BorderBottomColor(Color),
155    BorderLeftColor(Color),
156    BorderRightColor(Color),
157    BorderTopColor(Color),
158    BorderStartColor(Color),
159    
160    BorderStyle(BorderStyle),
161    BorderEndStyle(BorderStyle),
162    BorderBottomStyle(BorderStyle),
163    BorderLeftStyle(BorderStyle),
164    BorderRightStyle(BorderStyle),
165    BorderTopStyle(BorderStyle),
166    BorderStartStyle(BorderStyle),
167    
168    BorderWidth(f32),
169    BorderEndWidth(f32),
170    BorderBottomWidth(f32),
171    BorderLeftWidth(f32),
172    BorderRightWidth(f32),
173    BorderTopWidth(f32),
174    BorderStartWidth(f32),
175
176    BorderRadius(f32),
177    BorderBottomEndRadius(f32),
178    BorderBottomLeftRadius(f32),
179    BorderBottomRightRadius(f32),
180    BorderBottomStartRadius(f32),
181    BorderTopLeftRadius(f32),
182    BorderTopRightRadius(f32),
183    BorderTopEndRadius(f32),
184    BorderTopStartRadius(f32),
185    
186    Bottom(f32),
187    Direction(Direction),
188    Display(Display),
189    End(f32),
190    FlexBasis(f32),
191    FlexDirection(FlexDirection),
192    FlexGrow(f32),
193    FlexShrink(f32),
194    FlexWrap(FlexWrap),
195    FontFamily(FontFamily),
196    FontLineHeight(f32),
197    FontSize(f32),
198    FontStyle(FontStyle),
199    FontWeight(FontWeight),
200    Height(f32),
201    JustifyContent(JustifyContent),
202    Left(f32),
203    MarginBottom(f32),
204    MarginEnd(f32),
205    MarginLeft(f32),
206    MarginRight(f32),
207    MarginStart(f32),
208    MarginTop(f32),
209    MaxHeight(f32),
210    MaxWidth(f32),
211    MinHeight(f32),
212    MinWidth(f32),
213    Opacity(f32),
214    Overflow(Overflow),
215    PaddingBottom(f32),
216    PaddingEnd(f32),
217    PaddingLeft(f32),
218    PaddingRight(f32),
219    PaddingStart(f32),
220    PaddingTop(f32),
221    PositionType(PositionType),
222    Right(f32),
223    Start(f32),
224    TextAlignment(TextAlignment),
225    TextColor(Color),
226    TextDecorationColor(Color),
227    TextShadowColor(Color),
228    TintColor(Color),
229    Top(f32),
230    Width(f32)
231}
232
233/// A method for tokenizing a `Color` for a given attribute (e.g, `BackgroundColor`).
234#[cfg(feature="tokenize")]
235fn color_tokens(tokens: &mut TokenStream, color: &Color, style: &str) {
236    let red = color.red;
237    let green = color.green;
238    let blue = color.blue;
239    let alpha = color.alpha;
240    let s = Ident::new(style, Span::call_site());
241
242    tokens.extend(quote!(Styles::#s(Color {
243        red: #red,
244        green: #green,
245        blue: #blue,
246        alpha: #alpha
247    })));
248}
249
250/// Converts `Styles` into tokenized `Styles` representations, for use in the `styles! {}` macro.
251#[cfg(feature="tokenize")]
252impl ToTokens for Styles {
253    fn to_tokens(&self, tokens: &mut TokenStream) { match self {
254        Styles::AlignContent(align_content) => { match align_content {
255            AlignContent::FlexStart => tokens.extend(quote!(Styles::AlignContent(AlignContent::FlexStart))),
256            AlignContent::FlexEnd => tokens.extend(quote!(Styles::AlignContent(AlignContent::FlexEnd))),
257            AlignContent::Center => tokens.extend(quote!(Styles::AlignContent(AlignContent::Center))),
258            AlignContent::Stretch => tokens.extend(quote!(Styles::AlignContent(AlignContent::Stretch))),
259            AlignContent::SpaceAround => tokens.extend(quote!(Styles::AlignContent(AlignContent::SpaceAround))),
260            AlignContent::SpaceBetween => tokens.extend(quote!(Styles::AlignContent(AlignContent::SpaceBetween)))
261        }},
262
263        Styles::AlignItems(align_items) => { match align_items {
264            AlignItems::FlexStart => tokens.extend(quote!(Styles::AlignItems(AlignItems::FlexStart))),
265            AlignItems::FlexEnd => tokens.extend(quote!(Styles::AlignItems(AlignItems::FlexEnd))),
266            AlignItems::Center => tokens.extend(quote!(Styles::AlignItems(AlignItems::Center))),
267            AlignItems::Baseline => tokens.extend(quote!(Styles::AlignItems(AlignItems::Baseline))),
268            AlignItems::Stretch => tokens.extend(quote!(Styles::AlignItems(AlignItems::Stretch)))
269        }},
270
271        Styles::AlignSelf(align_self) => { match align_self {
272            AlignSelf::Auto => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Auto))),
273            AlignSelf::FlexStart => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::FlexStart))),
274            AlignSelf::FlexEnd => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::FlexEnd))),
275            AlignSelf::Center => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Center))),
276            AlignSelf::Baseline => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Baseline))),
277            AlignSelf::Stretch => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Stretch)))
278        }},
279
280        Styles::AspectRatio(_) => {},
281        
282        Styles::BackfaceVisibility(visibility) => { match visibility {
283            BackfaceVisibility::Visible => tokens.extend(quote!(Styles::BackfaceVisibility(BackfaceVisibility::Visible))),
284            BackfaceVisibility::Hidden => tokens.extend(quote!(Styles::BackfaceVisibility(BackfaceVisibility::Hidden)))
285        }},
286        
287        Styles::BackgroundColor(color) => color_tokens(tokens, color, "BackgroundColor"),
288        Styles::BorderColor(color) => color_tokens(tokens, color, "BorderColor"),
289        Styles::BorderEndColor(color) => color_tokens(tokens, color, "BorderEndColor"),
290        Styles::BorderBottomColor(color) => color_tokens(tokens, color, "BorderBottomColor"),
291        Styles::BorderLeftColor(color) => color_tokens(tokens, color, "BorderLeftColor"),
292        Styles::BorderRightColor(color) => color_tokens(tokens, color, "BorderRightColor"),
293        Styles::BorderTopColor(color) => color_tokens(tokens, color, "BorderTopColor"),
294        Styles::BorderStartColor(color) => color_tokens(tokens, color, "BorderStartColor"),
295        Styles::BorderStyle(_) => {},
296        Styles::BorderEndStyle(_) => {},
297        Styles::BorderBottomStyle(_) => {},
298        Styles::BorderLeftStyle(_) => {},
299        Styles::BorderRightStyle(_) => {},
300        Styles::BorderTopStyle(_) => {},
301        Styles::BorderStartStyle(_) => {},
302        Styles::BorderWidth(border_width) => tokens.extend(quote!(Styles::BorderWidth(#border_width))),
303        Styles::BorderEndWidth(border_end_width) => tokens.extend(quote!(Styles::BorderEndWidth(#border_end_width))),
304        Styles::BorderBottomWidth(border_bottom_width) => tokens.extend(quote!(Styles::BorderBottomWidth(#border_bottom_width))),
305        Styles::BorderLeftWidth(border_left_width) => tokens.extend(quote!(Styles::BorderLeftWidth(#border_left_width))),
306        Styles::BorderRightWidth(border_right_width) => tokens.extend(quote!(Styles::BorderRightWidth(#border_right_width))),
307        Styles::BorderTopWidth(border_top_width) => tokens.extend(quote!(Styles::BorderTopWidth(#border_top_width))),
308        Styles::BorderStartWidth(border_start_width) => tokens.extend(quote!(Styles::BorderStartWidth(#border_start_width))),
309        Styles::BorderRadius(border_radius) => tokens.extend(quote!(Styles::BorderRadius(#border_radius))),
310        Styles::BorderBottomEndRadius(border_bottom_end_radius) => tokens.extend(quote!(Styles::BorderBottomEndRadius(#border_bottom_end_radius))),
311        Styles::BorderBottomLeftRadius(border_bottom_left_radius) => tokens.extend(quote!(Styles::BorderBottomLeftRadius(#border_bottom_left_radius))),
312        Styles::BorderBottomRightRadius(border_bottom_right_radius) => tokens.extend(quote!(Styles::BorderBottomRightRadius(#border_bottom_right_radius))),
313        Styles::BorderBottomStartRadius(border_bottom_start_radius) => tokens.extend(quote!(Styles::BorderBottomStartRadius(#border_bottom_start_radius))),
314        Styles::BorderTopLeftRadius(border_top_left_radius) => tokens.extend(quote!(Styles::BorderTopLeftRadius(#border_top_left_radius))),
315        Styles::BorderTopRightRadius(border_top_right_radius) => tokens.extend(quote!(Styles::BorderTopRightRadius(#border_top_right_radius))),
316        Styles::BorderTopEndRadius(border_top_end_radius) => tokens.extend(quote!(Styles::BorderTopEndRadius(#border_top_end_radius))),
317        Styles::BorderTopStartRadius(border_top_start_radius) => tokens.extend(quote!(Styles::BorderTopStartRadius(#border_top_start_radius))),
318        Styles::Bottom(bottom) => tokens.extend(quote!(Styles::Bottom(#bottom))),
319        
320        Styles::Direction(direction) => { match direction {
321            Direction::Inherit => tokens.extend(quote!(Styles::Direction(Direction::Inherit))),
322            Direction::LTR => tokens.extend(quote!(Styles::Direction(Direction::LTR))),
323            Direction::RTL => tokens.extend(quote!(Styles::Direction(Direction::RTL)))
324        }},
325        
326        Styles::Display(display) => { match display {
327            Display::Flex => tokens.extend(quote!(Styles::Display(Display::Flex))),
328            Display::None => tokens.extend(quote!(Styles::Display(Display::None)))
329        }},
330
331        Styles::End(end) => tokens.extend(quote!(Styles::End(#end))),
332        Styles::FlexBasis(flex_basis) => tokens.extend(quote!(Styles::FlexBasis(#flex_basis))),
333        
334        Styles::FlexDirection(direction) => { match direction {
335            FlexDirection::Row => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::Row))),
336            FlexDirection::Column => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::Column))),
337            FlexDirection::RowReverse => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::RowReverse))),
338            FlexDirection::ColumnReverse => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::ColumnReverse)))
339        }},
340        
341        Styles::FlexGrow(flex_grow) => tokens.extend(quote!(Styles::FlexGrow(#flex_grow))),
342        Styles::FlexShrink(flex_shrink) => tokens.extend(quote!(Styles::FlexShrink(#flex_shrink))),
343        
344        Styles::FlexWrap(wrap) => { match wrap {
345            FlexWrap::NoWrap => tokens.extend(quote!(Styles::FlexWrap(FlexWrap::NoWrap))),
346            FlexWrap::Wrap => tokens.extend(quote!(Styles::FlexWrap(FlexWrap::Wrap))),
347            FlexWrap::WrapReverse => tokens.extend(quote!(Styles::FlexWrap(FlexWrap::WrapReverse)))
348        }},
349        
350        Styles::FontFamily(_family) => {},
351        Styles::FontLineHeight(line_height) => tokens.extend(quote!(Styles::LineHeight(#line_height))),
352        Styles::FontSize(font_size) => tokens.extend(quote!(Styles::FontSize(#font_size))),
353        Styles::FontStyle(_style) => {},
354        Styles::FontWeight(_weight) => {},
355        Styles::Height(height) => tokens.extend(quote!(Styles::Height(#height))),
356        
357        Styles::JustifyContent(justify) => { match justify {
358            JustifyContent::FlexStart => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::FlexStart))),
359            JustifyContent::FlexEnd => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::FlexEnd))),
360            JustifyContent::Center => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::Center))),
361            JustifyContent::SpaceBetween => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::SpaceBetween))),
362            JustifyContent::SpaceAround => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::SpaceAround))),
363            JustifyContent::SpaceEvenly => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::SpaceEvenly)))
364        }},
365        
366        Styles::Left(left) => tokens.extend(quote!(Styles::Left(#left))),
367        Styles::MarginBottom(margin_bottom) => tokens.extend(quote!(Styles::MarginBottom(#margin_bottom))),
368        Styles::MarginEnd(margin_end) => tokens.extend(quote!(Styles::MarginEnd(#margin_end))),
369        Styles::MarginLeft(margin_left) => tokens.extend(quote!(Styles::MarginLeft(#margin_left))),
370        Styles::MarginRight(margin_right) => tokens.extend(quote!(Styles::MarginRight(#margin_right))),
371        Styles::MarginStart(margin_start) => tokens.extend(quote!(Styles::MarginStart(#margin_start))),
372        Styles::MarginTop(top) => tokens.extend(quote!(Styles::Top(#top))),
373        Styles::MaxHeight(max_height) => tokens.extend(quote!(Styles::MaxHeight(#max_height))),
374        Styles::MaxWidth(max_width) => tokens.extend(quote!(Styles::MaxWidth(#max_width))),
375        Styles::MinHeight(min_height) => tokens.extend(quote!(Styles::MinHeight(#min_height))),
376        Styles::MinWidth(min_width) => tokens.extend(quote!(Styles::MinWidth(#min_width))),
377        Styles::Opacity(opacity) => tokens.extend(quote!(Styles::Opacity(#opacity))),
378        
379        Styles::Overflow(overflow) => { match overflow {
380            Overflow::Visible => tokens.extend(quote!(Styles::Overflow(Overflow::Visible))),
381            Overflow::Hidden => tokens.extend(quote!(Styles::Overflow(Overflow::Hidden))),
382            Overflow::Scroll => tokens.extend(quote!(Styles::Overflow(Overflow::Scroll)))
383        }},
384        
385        Styles::PaddingBottom(padding_bottom) => tokens.extend(quote!(Styles::PaddingBottom(#padding_bottom))),
386        Styles::PaddingEnd(padding_end) => tokens.extend(quote!(Styles::PaddingEnd(#padding_end))),
387        Styles::PaddingLeft(padding_left) => tokens.extend(quote!(Styles::PaddingLeft(#padding_left))),
388        Styles::PaddingRight(padding_right) => tokens.extend(quote!(Styles::PaddingRight(#padding_right))),
389        Styles::PaddingStart(padding_start) => tokens.extend(quote!(Styles::PaddingStart(#padding_start))),
390        Styles::PaddingTop(padding_top) => tokens.extend(quote!(Styles::PaddingTop(#padding_top))),
391        
392        Styles::PositionType(position_type) => { match position_type {
393            PositionType::Relative => tokens.extend(quote!(Styles::PositionType(PositionType::Relative))),
394            PositionType::Absolute => tokens.extend(quote!(Styles::PositionType(PositionType::Absolute)))
395        }},
396        
397        Styles::Right(right) => tokens.extend(quote!(Styles::Right(#right))),
398        Styles::Start(start) => tokens.extend(quote!(Styles::Start(#start))),
399        
400        Styles::TextAlignment(alignment) => { match alignment {
401            TextAlignment::Auto => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Auto))),
402            TextAlignment::Left => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Left))),
403            TextAlignment::Right => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Right))),
404            TextAlignment::Center => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Center))),
405            TextAlignment::Justify => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Justify)))
406        }},
407
408        Styles::TextColor(color) => color_tokens(tokens, color, "TextColor"),
409        Styles::TextDecorationColor(color) => color_tokens(tokens, color, "TextDecorationColor"),
410        Styles::TextShadowColor(color) => color_tokens(tokens, color, "TextShadowColor"),
411        Styles::TintColor(color) => color_tokens(tokens, color, "TintColor"),
412        Styles::Top(top) => tokens.extend(quote!(Styles::Top(#top))),
413        Styles::Width(width) => tokens.extend(quote!(Styles::Width(#width)))
414    }}
415}