twill 0.2.0

Idiomatic Rust styling library inspired by Tailwind CSS for GUI
Documentation
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Style builder for composing styles fluently.

use crate::tokens::{
    AnimationToken, BorderRadius, BorderStyle, BorderWidth, Color, Easing, FontFamily, FontSize,
    FontWeight, LetterSpacing, LineHeight, Shadow, Spacing, TextAlign, TextDecoration,
    TextTransform, TransitionDuration,
};
use crate::traits::{Merge, ToCss};
use crate::utilities::{
    Display, FlexContainer, GridContainer, Height, Margin, Overflow, Padding, Position,
    SizeConstraints, Width, ZIndex,
};

/// A comprehensive style builder for composing CSS styles.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Style {
    // Layout
    pub display: Option<Display>,
    pub position: Option<Position>,
    pub z_index: Option<ZIndex>,
    pub overflow: Option<Overflow>,
    pub overflow_x: Option<Overflow>,
    pub overflow_y: Option<Overflow>,

    // Flex/Grid
    pub flex: Option<FlexContainer>,
    pub grid: Option<GridContainer>,

    // Spacing
    pub padding: Option<Padding>,
    pub margin: Option<Margin>,

    // Size
    pub width: Option<Width>,
    pub height: Option<Height>,
    pub constraints: Option<SizeConstraints>,

    // Background
    pub background_color: Option<Color>,
    pub opacity: Option<f32>,

    // Border
    pub border_radius: Option<BorderRadius>,
    pub border_width: Option<BorderWidth>,
    pub border_style: Option<BorderStyle>,
    pub border_color: Option<Color>,

    // Shadow
    pub box_shadow: Option<Shadow>,

    // Typography
    pub font_family: Option<FontFamily>,
    pub font_size: Option<FontSize>,
    pub font_weight: Option<FontWeight>,
    pub letter_spacing: Option<LetterSpacing>,
    pub line_height: Option<LineHeight>,
    pub text_align: Option<TextAlign>,
    pub text_decoration: Option<TextDecoration>,
    pub text_transform: Option<TextTransform>,
    pub text_color: Option<Color>,

    // Motion (optional)
    pub transition_property: Option<String>,
    pub transition_duration: Option<TransitionDuration>,
    pub transition_timing_function: Option<Easing>,
    pub transition_delay: Option<TransitionDuration>,
    pub animation: Option<AnimationToken>,
}

impl Style {
    /// Create a new empty style.
    pub fn new() -> Self {
        Self::default()
    }

    // === Layout ===

    /// Set display type.
    pub fn display(mut self, display: Display) -> Self {
        self.display = Some(display);
        self
    }

    /// Set position type.
    pub fn position(mut self, position: Position) -> Self {
        self.position = Some(position);
        self
    }

    /// Set z-index.
    pub fn z_index(mut self, z: ZIndex) -> Self {
        self.z_index = Some(z);
        self
    }

    /// Set overflow.
    pub fn overflow(mut self, overflow: Overflow) -> Self {
        self.overflow = Some(overflow);
        self
    }

    /// Set flex container properties.
    pub fn flex(mut self, flex: FlexContainer) -> Self {
        self.flex = Some(flex);
        self
    }

    /// Set grid container properties.
    pub fn grid(mut self, grid: GridContainer) -> Self {
        self.grid = Some(grid);
        self
    }

    /// Set gap (for flex/grid).
    pub fn gap(mut self, spacing: Spacing) -> Self {
        if let Some(ref mut flex) = self.flex {
            self.flex = Some(FlexContainer {
                gap: Some(spacing),
                ..flex.clone()
            });
        } else {
            self.flex = Some(FlexContainer::new().gap(spacing));
        }
        self
    }

    // === Spacing ===

    /// Set padding.
    pub fn padding(mut self, padding: Padding) -> Self {
        self.padding = Some(padding);
        self
    }

    /// Set margin.
    pub fn margin(mut self, margin: Margin) -> Self {
        self.margin = Some(margin);
        self
    }

    // === Size ===

    /// Set width.
    pub fn width(mut self, width: Width) -> Self {
        self.width = Some(width);
        self
    }

    /// Set height.
    pub fn height(mut self, height: Height) -> Self {
        self.height = Some(height);
        self
    }

    /// Set size constraints.
    pub fn constraints(mut self, constraints: SizeConstraints) -> Self {
        self.constraints = Some(constraints);
        self
    }

    // === Background ===

    /// Set background color.
    pub fn bg(mut self, color: Color) -> Self {
        self.background_color = Some(color);
        self
    }

    /// Set background color (alias for bg).
    pub fn background(self, color: Color) -> Self {
        self.bg(color)
    }

    /// Set opacity (0.0 - 1.0).
    pub fn opacity(mut self, opacity: f32) -> Self {
        self.opacity = Some(opacity);
        self
    }

    // === Border ===

    /// Set border radius.
    pub fn rounded(mut self, radius: BorderRadius) -> Self {
        self.border_radius = Some(radius);
        self
    }

    /// Set border.
    pub fn border(mut self, width: BorderWidth, style: BorderStyle, color: Color) -> Self {
        self.border_width = Some(width);
        self.border_style = Some(style);
        self.border_color = Some(color);
        self
    }

    // === Shadow ===

    /// Set box shadow.
    pub fn shadow(mut self, shadow: Shadow) -> Self {
        self.box_shadow = Some(shadow);
        self
    }

    // === Typography ===

    /// Set font family.
    pub fn font(mut self, family: FontFamily) -> Self {
        self.font_family = Some(family);
        self
    }

    /// Set font size.
    pub fn text_size(mut self, size: FontSize) -> Self {
        self.font_size = Some(size);
        self
    }

    /// Set font weight.
    pub fn font_weight(mut self, weight: FontWeight) -> Self {
        self.font_weight = Some(weight);
        self
    }

    /// Set letter spacing.
    pub fn tracking(mut self, spacing: LetterSpacing) -> Self {
        self.letter_spacing = Some(spacing);
        self
    }

    /// Set line height.
    pub fn leading(mut self, height: LineHeight) -> Self {
        self.line_height = Some(height);
        self
    }

    /// Set text alignment.
    pub fn text_align(mut self, align: TextAlign) -> Self {
        self.text_align = Some(align);
        self
    }

    /// Set text decoration.
    pub fn underline(mut self) -> Self {
        self.text_decoration = Some(TextDecoration::Underline);
        self
    }

    /// Set text transform.
    pub fn uppercase(mut self) -> Self {
        self.text_transform = Some(TextTransform::Uppercase);
        self
    }

    /// Set text color.
    pub fn text_color(mut self, color: Color) -> Self {
        self.text_color = Some(color);
        self
    }

    /// Set text color (alias).
    pub fn text(self, color: Color) -> Self {
        self.text_color(color)
    }

    // === Motion ===

    /// Set transition property (e.g. "all", "opacity", "transform").
    pub fn transition_property(mut self, property: impl Into<String>) -> Self {
        self.transition_property = Some(property.into());
        self
    }

    /// Set transition duration using Tailwind-aligned duration tokens.
    pub fn transition_duration(mut self, duration: TransitionDuration) -> Self {
        self.transition_duration = Some(duration);
        self
    }

    /// Set transition timing function using Tailwind-aligned easing tokens.
    pub fn transition_ease(mut self, easing: Easing) -> Self {
        self.transition_timing_function = Some(easing);
        self
    }

    /// Set transition delay.
    pub fn transition_delay(mut self, delay: TransitionDuration) -> Self {
        self.transition_delay = Some(delay);
        self
    }

    /// Set animation token.
    pub fn animate(mut self, animation: AnimationToken) -> Self {
        self.animation = Some(animation);
        self
    }

    // === Convenience methods ===

    /// Create a flex container with default settings.
    pub fn flex_row() -> Self {
        Self::new()
            .display(Display::Flex)
            .flex(FlexContainer::row())
    }

    /// Create a flex column with default settings.
    pub fn flex_col() -> Self {
        Self::new()
            .display(Display::Flex)
            .flex(FlexContainer::col())
    }

    /// Create a centered flex row.
    pub fn centered_row() -> Self {
        Self::new()
            .display(Display::Flex)
            .flex(FlexContainer::centered_row())
    }

    /// Create a centered flex column.
    pub fn centered_col() -> Self {
        Self::new()
            .display(Display::Flex)
            .flex(FlexContainer::centered_col())
    }

    /// Hide element.
    pub fn hidden() -> Self {
        Self::new().display(Display::Hidden)
    }

    /// Full width.
    pub fn w_full() -> Self {
        Self::new().width(Width::full())
    }

    /// Full height.
    pub fn h_full() -> Self {
        Self::new().height(Height::full())
    }

    /// Full screen.
    pub fn screen() -> Self {
        Self::new().width(Width::screen()).height(Height::screen())
    }
}

impl Merge<Self> for Style {
    fn merge(&self, other: Self) -> Self {
        Self {
            display: other.display.or(self.display),
            position: other.position.or(self.position),
            z_index: other.z_index.or(self.z_index),
            overflow: other.overflow.or(self.overflow),
            overflow_x: other.overflow_x.or(self.overflow_x),
            overflow_y: other.overflow_y.or(self.overflow_y),
            flex: other.flex.or(self.flex.clone()),
            grid: other.grid.or(self.grid.clone()),
            padding: other.padding.or(self.padding),
            margin: other.margin.or(self.margin),
            width: other.width.or(self.width),
            height: other.height.or(self.height),
            constraints: other.constraints.or(self.constraints),
            background_color: other.background_color.or(self.background_color),
            opacity: other.opacity.or(self.opacity),
            border_radius: other.border_radius.or(self.border_radius),
            border_width: other.border_width.or(self.border_width),
            border_style: other.border_style.or(self.border_style),
            border_color: other.border_color.or(self.border_color),
            box_shadow: other.box_shadow.or(self.box_shadow),
            font_family: other.font_family.or(self.font_family),
            font_size: other.font_size.or(self.font_size),
            font_weight: other.font_weight.or(self.font_weight),
            letter_spacing: other.letter_spacing.or(self.letter_spacing),
            line_height: other.line_height.or(self.line_height),
            text_align: other.text_align.or(self.text_align),
            text_decoration: other.text_decoration.or(self.text_decoration),
            text_transform: other.text_transform.or(self.text_transform),
            text_color: other.text_color.or(self.text_color),
            transition_property: other
                .transition_property
                .or(self.transition_property.clone()),
            transition_duration: other.transition_duration.or(self.transition_duration),
            transition_timing_function: other
                .transition_timing_function
                .or(self.transition_timing_function),
            transition_delay: other.transition_delay.or(self.transition_delay),
            animation: other.animation.or(self.animation),
        }
    }
}

impl ToCss for Style {
    fn to_css(&self) -> String {
        let mut props = Vec::new();

        // Layout
        if let Some(v) = &self.display {
            props.push(format!("display: {}", v.to_css()));
        }
        if let Some(v) = &self.position {
            props.push(format!("position: {}", v.to_css()));
        }
        if let Some(v) = &self.z_index {
            props.push(format!("z-index: {}", v.to_css()));
        }
        if let Some(v) = &self.overflow {
            props.push(format!("overflow: {}", v.to_css()));
        }
        if let Some(v) = &self.overflow_x {
            props.push(format!("overflow-x: {}", v.to_css()));
        }
        if let Some(v) = &self.overflow_y {
            props.push(format!("overflow-y: {}", v.to_css()));
        }

        // Flex/Grid
        if let Some(v) = &self.flex {
            props.push(v.to_css());
        }
        if let Some(v) = &self.grid {
            props.push(v.to_css());
        }

        // Spacing
        if let Some(v) = &self.padding {
            props.push(v.to_css());
        }
        if let Some(v) = &self.margin {
            props.push(v.to_css());
        }

        // Size
        if let Some(v) = &self.width {
            props.push(v.to_css());
        }
        if let Some(v) = &self.height {
            props.push(v.to_css());
        }
        if let Some(v) = &self.constraints {
            props.push(v.to_css());
        }

        // Background
        if let Some(v) = &self.background_color {
            props.push(format!("background-color: {}", v.to_css()));
        }
        if let Some(v) = &self.opacity {
            props.push(format!("opacity: {}", v));
        }

        // Border
        if let Some(v) = &self.border_radius {
            props.push(format!("border-radius: {}", v.to_css()));
        }
        if let Some(v) = &self.border_width {
            props.push(format!("border-width: {}", v.to_css()));
        }
        if let Some(v) = &self.border_style {
            props.push(format!("border-style: {}", v.to_css()));
        }
        if let Some(v) = &self.border_color {
            props.push(format!("border-color: {}", v.to_css()));
        }

        // Shadow
        if let Some(v) = &self.box_shadow {
            props.push(format!("box-shadow: {}", v.to_css()));
        }

        // Typography
        if let Some(v) = &self.font_family {
            props.push(format!("font-family: {}", v.to_css()));
        }
        if let Some(v) = &self.font_size {
            props.push(format!("font-size: {}", v.to_css()));
        }
        if let Some(v) = &self.font_weight {
            props.push(format!("font-weight: {}", v.to_css()));
        }
        if let Some(v) = &self.letter_spacing {
            props.push(format!("letter-spacing: {}", v.to_css()));
        }
        if let Some(v) = &self.line_height {
            props.push(format!("line-height: {}", v.to_css()));
        }
        if let Some(v) = &self.text_align {
            props.push(format!("text-align: {}", v.to_css()));
        }
        if let Some(v) = &self.text_decoration {
            props.push(format!("text-decoration: {}", v.to_css()));
        }
        if let Some(v) = &self.text_transform {
            props.push(format!("text-transform: {}", v.to_css()));
        }
        if let Some(v) = &self.text_color {
            props.push(format!("color: {}", v.to_css()));
        }

        // Motion
        if let Some(v) = &self.transition_property {
            props.push(format!("transition-property: {}", v));
        }
        if let Some(v) = &self.transition_duration {
            props.push(format!("transition-duration: {}", v.to_css()));
        }
        if let Some(v) = &self.transition_timing_function {
            props.push(format!("transition-timing-function: {}", v.to_css()));
        }
        if let Some(v) = &self.transition_delay {
            props.push(format!("transition-delay: {}", v.to_css()));
        }
        if let Some(v) = &self.animation {
            props.push(format!("animation: {}", v.to_css()));
        }

        props.join("; ")
    }
}

impl Style {
    /// Generate inline style string for HTML.
    pub fn to_inline_style(&self) -> String {
        format!("style=\"{}\"", self.to_css())
    }

    /// Generate CSS class content.
    pub fn to_class_content(&self) -> String {
        format!("{{ {} }}", self.to_css())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tokens::{AnimationToken, Easing, Scale, TransitionDuration};

    #[test]
    fn test_style_builder() {
        let style = Style::new()
            .padding(Padding::all(Spacing::S4))
            .bg(Color::blue(Scale::S500))
            .rounded(BorderRadius::Md)
            .text_color(Color::slate(Scale::S50));

        let css = style.to_css();
        assert!(css.contains("padding: 1rem"));
        assert!(css.contains("background-color: #3b82f6"));
        assert!(css.contains("border-radius: 0.375rem"));
    }

    #[test]
    fn test_flex_center() {
        let style = Style::centered_col().gap(Spacing::S2);
        let css = style.to_css();
        assert!(css.contains("flex-direction: column"));
        assert!(css.contains("justify-content: center"));
    }

    #[test]
    fn test_motion_tokens() {
        let style = Style::new()
            .transition_property("opacity")
            .transition_duration(TransitionDuration::Ms300)
            .transition_ease(Easing::InOut)
            .animate(AnimationToken::Pulse);
        let css = style.to_css();
        assert!(css.contains("transition-property: opacity"));
        assert!(css.contains("transition-duration: 300ms"));
        assert!(css.contains("transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1)"));
        assert!(css.contains("animation: pulse 2s"));
    }
}