1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/// Implements the various `Style` types used for computing Flexbox layouts,
/// along with appearance-based styles (`Color`s, etc).

#[cfg(feature="tokenize")]
use proc_macro2::{TokenStream, Ident, Span};

#[cfg(feature="tokenize")]
use quote::{quote, ToTokens};

pub use crate::color::Color;

pub use crate::stretch::geometry::{Point, Rect, Size};
pub use crate::stretch::number::Number;
pub use crate::stretch::result::Layout;

pub use crate::stretch::style::{
    Style,
    AlignContent, AlignItems, AlignSelf, Dimension, Direction, Display,
    FlexDirection, JustifyContent, Overflow, PositionType, FlexWrap
};

/// Describes the backface-visibility for a view. This may be removed in a later release.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum BackfaceVisibility {
    Visible,
    Hidden
}

impl Default for BackfaceVisibility {
    fn default() -> BackfaceVisibility {
        BackfaceVisibility::Visible
    }
}

/// Describes a font style.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FontStyle {
    Normal,
    Italic,
    Oblique
}

impl Default for FontStyle {
    fn default() -> FontStyle {
        FontStyle::Normal
    }
}

/// Describes a font weight.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FontWeight {
    Normal,
    Bold
}

impl Default for FontWeight {
    fn default() -> FontWeight {
        FontWeight::Normal
    }
}

/// Describes how text should be aligned.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum TextAlignment {
    Auto,
    Left,
    Right,
    Center,
    Justify
}

impl Default for TextAlignment {
    fn default() -> TextAlignment {
        TextAlignment::Auto
    }
}

/// Describes a border style.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum BorderStyle {
    None, // The CSS value is None, but it's a reserved term in Rust ;P
    Hidden,
    Solid
}

impl Default for BorderStyle {
    fn default() -> BorderStyle {
        BorderStyle::None
    }
}

/// Describes how a Font Family
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FontFamily {
    SansSerif // @TODO This is tricky because of &str/String/Copy. Revisit later.
}

impl Default for FontFamily {
    fn default() -> Self {
        FontFamily::SansSerif
    }
}

/// When applying layout to a backing view, you'll get two calls - one with a `Layout`, 
/// which contains the computed frame, and one with an `Appearance`, which contains things 
/// like colors, fonts, and so on.
pub struct Appearance {
    pub background_color: Color,
    pub font_size: f32,
    pub font_style: FontStyle,
    pub font_weight: FontWeight,
    pub opacity: f32,
    pub text_alignment: TextAlignment,
    pub text_color: Color,
    pub text_decoration_color: Color,
    pub text_shadow_color: Color,
    pub tint_color: Color
}

impl Default for Appearance {
    fn default() -> Appearance {
        Appearance {
            background_color: Color::transparent(),
            // @TODO: We can definitely judge a default value better here. 
            font_size: 14.,
            font_style: FontStyle::default(),
            font_weight: FontWeight::default(),
            opacity: 1.,
            text_alignment: TextAlignment::default(),
            text_color: Color::transparent(),
            text_decoration_color: Color::transparent(),
            text_shadow_color: Color::transparent(),
            tint_color: Color::transparent()
        }
    }
}

/// These exist purely for use in the parser code.
///
/// A `Style` is what's used for a node; `Styles` are what's parsed and stored.
/// At render-time, the rendering engine takes n styles and reduces them down into 1 `Style`
/// that's applied to the node in question.
#[derive(Debug)]
pub enum Styles {
    AlignContent(AlignContent),
    AlignItems(AlignItems),
    AlignSelf(AlignSelf),
    AspectRatio(Number),
    BackfaceVisibility(BackfaceVisibility),
    BackgroundColor(Color),

    BorderColor(Color),
    BorderEndColor(Color),
    BorderBottomColor(Color),
    BorderLeftColor(Color),
    BorderRightColor(Color),
    BorderTopColor(Color),
    BorderStartColor(Color),
    
    BorderStyle(BorderStyle),
    BorderEndStyle(BorderStyle),
    BorderBottomStyle(BorderStyle),
    BorderLeftStyle(BorderStyle),
    BorderRightStyle(BorderStyle),
    BorderTopStyle(BorderStyle),
    BorderStartStyle(BorderStyle),
    
    BorderWidth(f32),
    BorderEndWidth(f32),
    BorderBottomWidth(f32),
    BorderLeftWidth(f32),
    BorderRightWidth(f32),
    BorderTopWidth(f32),
    BorderStartWidth(f32),

    BorderRadius(f32),
    BorderBottomEndRadius(f32),
    BorderBottomLeftRadius(f32),
    BorderBottomRightRadius(f32),
    BorderBottomStartRadius(f32),
    BorderTopLeftRadius(f32),
    BorderTopRightRadius(f32),
    BorderTopEndRadius(f32),
    BorderTopStartRadius(f32),
    
    Bottom(f32),
    Direction(Direction),
    Display(Display),
    End(f32),
    FlexBasis(f32),
    FlexDirection(FlexDirection),
    FlexGrow(f32),
    FlexShrink(f32),
    FlexWrap(FlexWrap),
    FontFamily(FontFamily),
    FontLineHeight(f32),
    FontSize(f32),
    FontStyle(FontStyle),
    FontWeight(FontWeight),
    Height(f32),
    JustifyContent(JustifyContent),
    Left(f32),
    MarginBottom(f32),
    MarginEnd(f32),
    MarginLeft(f32),
    MarginRight(f32),
    MarginStart(f32),
    MarginTop(f32),
    MaxHeight(f32),
    MaxWidth(f32),
    MinHeight(f32),
    MinWidth(f32),
    Opacity(f32),
    Overflow(Overflow),
    PaddingBottom(f32),
    PaddingEnd(f32),
    PaddingLeft(f32),
    PaddingRight(f32),
    PaddingStart(f32),
    PaddingTop(f32),
    PositionType(PositionType),
    Right(f32),
    Start(f32),
    TextAlignment(TextAlignment),
    TextColor(Color),
    TextDecorationColor(Color),
    TextShadowColor(Color),
    TintColor(Color),
    Top(f32),
    Width(f32)
}

/// A method for tokenizing a `Color` for a given attribute (e.g, `BackgroundColor`).
#[cfg(feature="tokenize")]
fn color_tokens(tokens: &mut TokenStream, color: &Color, style: &str) {
    let red = color.red;
    let green = color.green;
    let blue = color.blue;
    let alpha = color.alpha;
    let s = Ident::new(style, Span::call_site());

    tokens.extend(quote!(Styles::#s(Color {
        red: #red,
        green: #green,
        blue: #blue,
        alpha: #alpha
    })));
}

/// Converts `Styles` into tokenized `Styles` representations, for use in the `styles! {}` macro.
#[cfg(feature="tokenize")]
impl ToTokens for Styles {
    fn to_tokens(&self, tokens: &mut TokenStream) { match self {
        Styles::AlignContent(align_content) => { match align_content {
            AlignContent::FlexStart => tokens.extend(quote!(Styles::AlignContent(AlignContent::FlexStart))),
            AlignContent::FlexEnd => tokens.extend(quote!(Styles::AlignContent(AlignContent::FlexEnd))),
            AlignContent::Center => tokens.extend(quote!(Styles::AlignContent(AlignContent::Center))),
            AlignContent::Stretch => tokens.extend(quote!(Styles::AlignContent(AlignContent::Stretch))),
            AlignContent::SpaceAround => tokens.extend(quote!(Styles::AlignContent(AlignContent::SpaceAround))),
            AlignContent::SpaceBetween => tokens.extend(quote!(Styles::AlignContent(AlignContent::SpaceBetween)))
        }},

        Styles::AlignItems(align_items) => { match align_items {
            AlignItems::FlexStart => tokens.extend(quote!(Styles::AlignItems(AlignItems::FlexStart))),
            AlignItems::FlexEnd => tokens.extend(quote!(Styles::AlignItems(AlignItems::FlexEnd))),
            AlignItems::Center => tokens.extend(quote!(Styles::AlignItems(AlignItems::Center))),
            AlignItems::Baseline => tokens.extend(quote!(Styles::AlignItems(AlignItems::Baseline))),
            AlignItems::Stretch => tokens.extend(quote!(Styles::AlignItems(AlignItems::Stretch)))
        }},

        Styles::AlignSelf(align_self) => { match align_self {
            AlignSelf::Auto => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Auto))),
            AlignSelf::FlexStart => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::FlexStart))),
            AlignSelf::FlexEnd => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::FlexEnd))),
            AlignSelf::Center => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Center))),
            AlignSelf::Baseline => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Baseline))),
            AlignSelf::Stretch => tokens.extend(quote!(Styles::AlignSelf(AlignSelf::Stretch)))
        }},

        Styles::AspectRatio(_) => {},
        
        Styles::BackfaceVisibility(visibility) => { match visibility {
            BackfaceVisibility::Visible => tokens.extend(quote!(Styles::BackfaceVisibility(BackfaceVisibility::Visible))),
            BackfaceVisibility::Hidden => tokens.extend(quote!(Styles::BackfaceVisibility(BackfaceVisibility::Hidden)))
        }},
        
        Styles::BackgroundColor(color) => color_tokens(tokens, color, "BackgroundColor"),
        Styles::BorderColor(color) => color_tokens(tokens, color, "BorderColor"),
        Styles::BorderEndColor(color) => color_tokens(tokens, color, "BorderEndColor"),
        Styles::BorderBottomColor(color) => color_tokens(tokens, color, "BorderBottomColor"),
        Styles::BorderLeftColor(color) => color_tokens(tokens, color, "BorderLeftColor"),
        Styles::BorderRightColor(color) => color_tokens(tokens, color, "BorderRightColor"),
        Styles::BorderTopColor(color) => color_tokens(tokens, color, "BorderTopColor"),
        Styles::BorderStartColor(color) => color_tokens(tokens, color, "BorderStartColor"),
        Styles::BorderStyle(_) => {},
        Styles::BorderEndStyle(_) => {},
        Styles::BorderBottomStyle(_) => {},
        Styles::BorderLeftStyle(_) => {},
        Styles::BorderRightStyle(_) => {},
        Styles::BorderTopStyle(_) => {},
        Styles::BorderStartStyle(_) => {},
        Styles::BorderWidth(border_width) => tokens.extend(quote!(Styles::BorderWidth(#border_width))),
        Styles::BorderEndWidth(border_end_width) => tokens.extend(quote!(Styles::BorderEndWidth(#border_end_width))),
        Styles::BorderBottomWidth(border_bottom_width) => tokens.extend(quote!(Styles::BorderBottomWidth(#border_bottom_width))),
        Styles::BorderLeftWidth(border_left_width) => tokens.extend(quote!(Styles::BorderLeftWidth(#border_left_width))),
        Styles::BorderRightWidth(border_right_width) => tokens.extend(quote!(Styles::BorderRightWidth(#border_right_width))),
        Styles::BorderTopWidth(border_top_width) => tokens.extend(quote!(Styles::BorderTopWidth(#border_top_width))),
        Styles::BorderStartWidth(border_start_width) => tokens.extend(quote!(Styles::BorderStartWidth(#border_start_width))),
        Styles::BorderRadius(border_radius) => tokens.extend(quote!(Styles::BorderRadius(#border_radius))),
        Styles::BorderBottomEndRadius(border_bottom_end_radius) => tokens.extend(quote!(Styles::BorderBottomEndRadius(#border_bottom_end_radius))),
        Styles::BorderBottomLeftRadius(border_bottom_left_radius) => tokens.extend(quote!(Styles::BorderBottomLeftRadius(#border_bottom_left_radius))),
        Styles::BorderBottomRightRadius(border_bottom_right_radius) => tokens.extend(quote!(Styles::BorderBottomRightRadius(#border_bottom_right_radius))),
        Styles::BorderBottomStartRadius(border_bottom_start_radius) => tokens.extend(quote!(Styles::BorderBottomStartRadius(#border_bottom_start_radius))),
        Styles::BorderTopLeftRadius(border_top_left_radius) => tokens.extend(quote!(Styles::BorderTopLeftRadius(#border_top_left_radius))),
        Styles::BorderTopRightRadius(border_top_right_radius) => tokens.extend(quote!(Styles::BorderTopRightRadius(#border_top_right_radius))),
        Styles::BorderTopEndRadius(border_top_end_radius) => tokens.extend(quote!(Styles::BorderTopEndRadius(#border_top_end_radius))),
        Styles::BorderTopStartRadius(border_top_start_radius) => tokens.extend(quote!(Styles::BorderTopStartRadius(#border_top_start_radius))),
        Styles::Bottom(bottom) => tokens.extend(quote!(Styles::Bottom(#bottom))),
        
        Styles::Direction(direction) => { match direction {
            Direction::Inherit => tokens.extend(quote!(Styles::Direction(Direction::Inherit))),
            Direction::LTR => tokens.extend(quote!(Styles::Direction(Direction::LTR))),
            Direction::RTL => tokens.extend(quote!(Styles::Direction(Direction::RTL)))
        }},
        
        Styles::Display(display) => { match display {
            Display::Flex => tokens.extend(quote!(Styles::Display(Display::Flex))),
            Display::None => tokens.extend(quote!(Styles::Display(Display::None)))
        }},

        Styles::End(end) => tokens.extend(quote!(Styles::End(#end))),
        Styles::FlexBasis(flex_basis) => tokens.extend(quote!(Styles::FlexBasis(#flex_basis))),
        
        Styles::FlexDirection(direction) => { match direction {
            FlexDirection::Row => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::Row))),
            FlexDirection::Column => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::Column))),
            FlexDirection::RowReverse => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::RowReverse))),
            FlexDirection::ColumnReverse => tokens.extend(quote!(Styles::FlexDirection(FlexDirection::ColumnReverse)))
        }},
        
        Styles::FlexGrow(flex_grow) => tokens.extend(quote!(Styles::FlexGrow(#flex_grow))),
        Styles::FlexShrink(flex_shrink) => tokens.extend(quote!(Styles::FlexShrink(#flex_shrink))),
        
        Styles::FlexWrap(wrap) => { match wrap {
            FlexWrap::NoWrap => tokens.extend(quote!(Styles::FlexWrap(FlexWrap::NoWrap))),
            FlexWrap::Wrap => tokens.extend(quote!(Styles::FlexWrap(FlexWrap::Wrap))),
            FlexWrap::WrapReverse => tokens.extend(quote!(Styles::FlexWrap(FlexWrap::WrapReverse)))
        }},
        
        Styles::FontFamily(_family) => {},
        Styles::FontLineHeight(line_height) => tokens.extend(quote!(Styles::LineHeight(#line_height))),
        Styles::FontSize(font_size) => tokens.extend(quote!(Styles::FontSize(#font_size))),
        Styles::FontStyle(_style) => {},
        Styles::FontWeight(_weight) => {},
        Styles::Height(height) => tokens.extend(quote!(Styles::Height(#height))),
        
        Styles::JustifyContent(justify) => { match justify {
            JustifyContent::FlexStart => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::FlexStart))),
            JustifyContent::FlexEnd => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::FlexEnd))),
            JustifyContent::Center => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::Center))),
            JustifyContent::SpaceBetween => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::SpaceBetween))),
            JustifyContent::SpaceAround => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::SpaceAround))),
            JustifyContent::SpaceEvenly => tokens.extend(quote!(Styles::JustifyContent(JustifyContent::SpaceEvenly)))
        }},
        
        Styles::Left(left) => tokens.extend(quote!(Styles::Left(#left))),
        Styles::MarginBottom(margin_bottom) => tokens.extend(quote!(Styles::MarginBottom(#margin_bottom))),
        Styles::MarginEnd(margin_end) => tokens.extend(quote!(Styles::MarginEnd(#margin_end))),
        Styles::MarginLeft(margin_left) => tokens.extend(quote!(Styles::MarginLeft(#margin_left))),
        Styles::MarginRight(margin_right) => tokens.extend(quote!(Styles::MarginRight(#margin_right))),
        Styles::MarginStart(margin_start) => tokens.extend(quote!(Styles::MarginStart(#margin_start))),
        Styles::MarginTop(top) => tokens.extend(quote!(Styles::Top(#top))),
        Styles::MaxHeight(max_height) => tokens.extend(quote!(Styles::MaxHeight(#max_height))),
        Styles::MaxWidth(max_width) => tokens.extend(quote!(Styles::MaxWidth(#max_width))),
        Styles::MinHeight(min_height) => tokens.extend(quote!(Styles::MinHeight(#min_height))),
        Styles::MinWidth(min_width) => tokens.extend(quote!(Styles::MinWidth(#min_width))),
        Styles::Opacity(opacity) => tokens.extend(quote!(Styles::Opacity(#opacity))),
        
        Styles::Overflow(overflow) => { match overflow {
            Overflow::Visible => tokens.extend(quote!(Styles::Overflow(Overflow::Visible))),
            Overflow::Hidden => tokens.extend(quote!(Styles::Overflow(Overflow::Hidden))),
            Overflow::Scroll => tokens.extend(quote!(Styles::Overflow(Overflow::Scroll)))
        }},
        
        Styles::PaddingBottom(padding_bottom) => tokens.extend(quote!(Styles::PaddingBottom(#padding_bottom))),
        Styles::PaddingEnd(padding_end) => tokens.extend(quote!(Styles::PaddingEnd(#padding_end))),
        Styles::PaddingLeft(padding_left) => tokens.extend(quote!(Styles::PaddingLeft(#padding_left))),
        Styles::PaddingRight(padding_right) => tokens.extend(quote!(Styles::PaddingRight(#padding_right))),
        Styles::PaddingStart(padding_start) => tokens.extend(quote!(Styles::PaddingStart(#padding_start))),
        Styles::PaddingTop(padding_top) => tokens.extend(quote!(Styles::PaddingTop(#padding_top))),
        
        Styles::PositionType(position_type) => { match position_type {
            PositionType::Relative => tokens.extend(quote!(Styles::PositionType(PositionType::Relative))),
            PositionType::Absolute => tokens.extend(quote!(Styles::PositionType(PositionType::Absolute)))
        }},
        
        Styles::Right(right) => tokens.extend(quote!(Styles::Right(#right))),
        Styles::Start(start) => tokens.extend(quote!(Styles::Start(#start))),
        
        Styles::TextAlignment(alignment) => { match alignment {
            TextAlignment::Auto => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Auto))),
            TextAlignment::Left => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Left))),
            TextAlignment::Right => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Right))),
            TextAlignment::Center => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Center))),
            TextAlignment::Justify => tokens.extend(quote!(Styles::TextAlignment(TextAlignment::Justify)))
        }},

        Styles::TextColor(color) => color_tokens(tokens, color, "TextColor"),
        Styles::TextDecorationColor(color) => color_tokens(tokens, color, "TextDecorationColor"),
        Styles::TextShadowColor(color) => color_tokens(tokens, color, "TextShadowColor"),
        Styles::TintColor(color) => color_tokens(tokens, color, "TintColor"),
        Styles::Top(top) => tokens.extend(quote!(Styles::Top(#top))),
        Styles::Width(width) => tokens.extend(quote!(Styles::Width(#width)))
    }}
}