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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
mod base;
mod themes;

#[doc(hidden)]
pub use ::core::default::Default;
#[doc(hidden)]
pub use ::paste::paste;
#[doc(hidden)]
pub use ::std::borrow::Cow;
pub use themes::*;

/// Alias for `Cow::Borrowed`, because that's used a million times so shortening it is nice.
/// Makes the code more readable.
#[macro_export]
macro_rules! cow_borrowed {
    ($val:expr) => {
        $crate::Cow::Borrowed($val)
    };
}

/// Example usage:
///
/// ```rust
/// # use crate::freya_hooks::define_theme;
/// # use crate::freya_hooks::FontTheme;
/// # use crate::freya_hooks::FontThemeWith;
/// # #[derive(Clone, Debug, PartialEq, Eq)]
/// # struct Bar;
/// # #[derive(Clone, Debug, PartialEq, Eq)]
/// # struct Foo;
/// define_theme! {
///     %[component]
///     pub Test {
///         %[cows]
///         cow_string: str,
///         %[subthemes]
///         font_theme: FontTheme,
///     }
/// }
/// ```
#[macro_export]
macro_rules! define_theme {
    (NOTHING=) => {};

    (
        $(#[$attrs:meta])*
        $(%[component$($component_attr_control:tt)?])?
        $vis:vis $name:ident $(<$lifetime:lifetime>)? {
            $(
                %[cows$($cows_attr_control:tt)?]
                $(
                    $(#[$cow_field_attrs:meta])*
                    $cow_field_name:ident: $cow_field_ty:ty,
                )*
            )?
            $(
                %[subthemes$($subthemes_attr_control:tt)?]
                $(
                    $(#[$subtheme_field_attrs:meta])*
                    $subtheme_field_name:ident: $subtheme_field_ty_name:ident $(<$subtheme_field_ty_lifetime:lifetime>)?,
                )*
            )?
    }) => {
        $crate::define_theme!(NOTHING=$($($component_attr_control)?)?);
        $crate::define_theme!(NOTHING=$($($cows_attr_control)?)?);
        $crate::define_theme!(NOTHING=$($($subthemes_attr_control)?)?);
        $crate::paste! {
            #[derive(Default, Clone, Debug, PartialEq, Eq)]
            #[doc = "You can use this to change a theme for only one component, with the `theme` property."]
            $(#[$attrs])*
            $vis struct [<$name ThemeWith>] $(<$lifetime>)? {
                $($(
                    $(#[$subtheme_field_attrs])*
                    pub $subtheme_field_name: Option< [<$subtheme_field_ty_name With>] $(<$subtheme_field_ty_lifetime>)? >,
                )*)?
                $($(
                    $(#[$cow_field_attrs])*
                    pub $cow_field_name: Option<$crate::Cow<'static, $cow_field_ty>>,
                )*)?
            }

            #[derive(Clone, Debug, PartialEq, Eq)]
            $(#[doc = "Theming properties for the `" $name "` component."] $($component_attr_control)?)?
            $(#[$attrs])*
            $vis struct [<$name Theme>] $(<$lifetime>)? {
                $($(
                    $(#[$subtheme_field_attrs])*
                    pub $subtheme_field_name: $subtheme_field_ty_name $(<$subtheme_field_ty_lifetime>)?,
                )*)?
                $($(
                    $(#[$cow_field_attrs])*
                    pub $cow_field_name: $crate::Cow<'static, $cow_field_ty>,
                )*)?
            }

            impl $(<$lifetime>)? [<$name Theme>] $(<$lifetime>)? {

                pub fn apply_colors(&mut self, colors: &$crate::ColorsSheet) {
                    $($(
                        self.$subtheme_field_name.apply_colors(colors);
                    )*)?

                    $($(
                        self.$cow_field_name = colors.resolve(self.$cow_field_name.clone());
                    )*)?
                }

                #[doc = "Checks each field in `optional` and if it's `Some`, it overwrites the corresponding `self` field."]
                pub fn apply_optional(&mut self, optional: & $($lifetime)? [<$name ThemeWith>]) {
                    $($(
                        if let Some($subtheme_field_name) = &optional.$subtheme_field_name {
                            self.$subtheme_field_name.apply_optional($subtheme_field_name);
                        }
                    )*)?

                    $($(
                        if let Some($cow_field_name) = &optional.$cow_field_name {
                            self.$cow_field_name = $cow_field_name.clone();
                        }
                    )*)?
                }
            }
        }
    };
}

/// Create `FooThemeWith` structs without having to deal with the verbose syntax.
///
/// # Examples
///
/// Without the macro:
///
/// ```no_run
/// # use freya::prelude::*;
/// # fn theme_with_example_no_macro() -> Element {
/// rsx! {
///     Button {
///         theme: ButtonThemeWith {
///             background: Some("blue".into()),
///             font_theme: FontThemeWith {
///                 color: Some("white".into()),
///                 ..Default::default()
///             }.into(),
///             ..Default::default()
///         }
///     }
/// }
/// # }
/// ```
///
/// With the macro:
///
/// ```no_run
/// # use freya::prelude::*;
/// # fn theme_with_example_no_macro() -> Element {
/// rsx! {
///     Button {
///         theme: theme_with!(ButtonTheme {
///             background: "blue".into(),
///             font_theme: theme_with!(FontTheme {
///                 color: "white".into(),
///             }),
///         })
///     }
/// }
/// # }
/// ```
#[macro_export]
macro_rules! theme_with {
    ($theme_name:ident {
        $(
            $theme_field_name:ident: $theme_field_val:expr
        ),* $(,)?
    }) => {
        $crate::paste! {
            #[allow(clippy::needless_update)]
            [<$theme_name With>] {
                $($theme_field_name: Some($theme_field_val),)*
                ..$crate::Default::default()
            }
        }
    };
}

define_theme! {
    %[component]
    pub Dropdown {
        %[cows]
        width: str,
        margin: str,
        dropdown_background: str,
        background_button: str,
        hover_background: str,
        border_fill: str,
        arrow_fill: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub DropdownItem {
        %[cows]
        background: str,
        select_background: str,
        hover_background: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub Button {
        %[cows]
        background: str,
        hover_background: str,
        border_fill: str,
        focus_border_fill: str,
        shadow: str,
        margin: str,
        corner_radius: str,
        width: str,
        height: str,
        padding: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub Input {
        %[cows]
        background: str,
        hover_background: str,
        border_fill: str,
        shadow: str,
        width: str,
        margin: str,
        corner_radius: str,
        %[subthemes]
        font_theme: FontTheme,
        placeholder_font_theme: FontTheme,
    }
}

define_theme! {
    /// Theming properties for Fonts.
    pub Font {
        %[cows]
        color: str,
    }
}

define_theme! {
    %[component]
    pub Switch {
        %[cows]
        margin: str,
        background: str,
        thumb_background: str,
        enabled_background: str,
        enabled_thumb_background: str,
        focus_border_fill: str,
        enabled_focus_border_fill: str,
    }
}

define_theme! {
    %[component]
    pub ScrollBar {
        %[cows]
        background: str,
        thumb_background: str,
        hover_thumb_background: str,
        active_thumb_background: str,
        size: str,
    }
}

define_theme! {
    %[component]
    pub Body {
        %[cows]
        background: str,
        color: str,
        padding: str,
    }
}

define_theme! {
    %[component]
    pub Slider {
        %[cows]
        background: str,
        thumb_background: str,
        thumb_inner_background: str,
        border_fill: str,
    }
}

define_theme! {
    %[component]
    pub Tooltip {
        %[cows]
        background: str,
        color: str,
        border_fill: str,
    }
}

define_theme! {
    %[component]
    pub Accordion {
        %[cows]
        color: str,
        background: str,
        border_fill: str,
    }
}

define_theme! {
    %[component]
    pub Loader {
        %[cows]
        primary_color: str,
    }
}

define_theme! {
    %[component]
    pub Link {
        %[cows]
        highlight_color: str,
    }
}

define_theme! {
    %[component]
    pub ProgressBar {
        %[cows]
        color: str,
        background: str,
        progress_background: str,
        width: str,
        height: str,
    }
}

define_theme! {
    %[component]
    pub Table {
        %[cows]
        background: str,
        arrow_fill: str,
        alternate_row_background: str,
        row_background: str,
        divider_fill: str,
        height: str,
        corner_radius: str,
        shadow: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub Canvas {
        %[cows]
        width: str,
        height: str,
        background: str,
    }
}

define_theme! {
    %[component]
    pub Graph {
        %[cows]
        width: str,
        height: str,
    }
}

define_theme! {
    %[component]
    pub NetworkImage {
        %[cows]
        width: str,
        height: str,
    }
}

define_theme! {
    %[component]
    pub Icon {
        %[cows]
        margin: str,
        width: str,
        height: str,
    }
}

define_theme! {
    %[component]
    pub Sidebar {
        %[cows]
        spacing: str,
        background: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub SidebarItem {
        %[cows]
        margin: str,
        background: str,
        hover_background: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub Tile {
        %[cows]
        padding: str,
    }
}

define_theme! {
    %[component]
    pub MenuItem {
        %[cows]
        hover_background: str,
        corner_radius: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub MenuContainer {
        %[cows]
        background: str,
        padding: str,
        shadow: str,
    }
}

define_theme! {
    %[component]
    pub SnackBar {
        %[cows]
        background: str,
        color: str,
     }
}

define_theme! {
    %[component]
    pub Radio {
        %[cows]
        unselected_fill: str,
        selected_fill: str,
        border_fill: str,
    }
}

define_theme! {
    %[component]
    pub Checkbox {
        %[cows]
        unselected_fill: str,
        selected_fill: str,
        selected_icon_fill: str,
        border_fill: str,
    }
}

define_theme! {
    %[component]
    pub Popup {
        %[cows]
        background: str,
        color: str,
        cross_fill: str,
        width: str,
        height: str,
    }
}

define_theme! {
    %[component]
    pub Tab {
        %[cows]
        background: str,
        hover_background: str,
        border_fill: str,
        focus_border_fill: str,
        width: str,
        height: str,
        padding: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

define_theme! {
    %[component]
    pub BottomTab {
        %[cows]
        background: str,
        hover_background: str,
        width: str,
        height: str,
        padding: str,
        %[subthemes]
        font_theme: FontTheme,
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColorsSheet {
    pub primary: Cow<'static, str>,
    pub secondary: Cow<'static, str>,
    pub tertiary: Cow<'static, str>,
    pub surface: Cow<'static, str>,
    pub secondary_surface: Cow<'static, str>,
    pub neutral_surface: Cow<'static, str>,
    pub focused_surface: Cow<'static, str>,
    pub opposite_surface: Cow<'static, str>,
    pub secondary_opposite_surface: Cow<'static, str>,
    pub tertiary_opposite_surface: Cow<'static, str>,
    pub background: Cow<'static, str>,
    pub focused_border: Cow<'static, str>,
    pub solid: Cow<'static, str>,
    pub color: Cow<'static, str>,
    pub primary_color: Cow<'static, str>,
    pub placeholder_color: Cow<'static, str>,
    pub highlight_color: Cow<'static, str>,
}

impl ColorsSheet {
    pub fn resolve(&self, val: Cow<'static, str>) -> Cow<'static, str> {
        if val.starts_with("key") {
            let key_val = val.replace("key(", "").replace(")", "");
            match key_val.as_str() {
                "primary" => self.primary.clone(),
                "secondary" => self.secondary.clone(),
                "tertiary" => self.tertiary.clone(),
                "surface" => self.surface.clone(),
                "secondary_surface" => self.secondary_surface.clone(),
                "neutral_surface" => self.neutral_surface.clone(),
                "focused_surface" => self.focused_surface.clone(),
                "opposite_surface" => self.opposite_surface.clone(),
                "secondary_opposite_surface" => self.secondary_opposite_surface.clone(),
                "tertiary_opposite_surface" => self.tertiary_opposite_surface.clone(),
                "background" => self.background.clone(),
                "focused_border" => self.focused_border.clone(),
                "solid" => self.solid.clone(),
                "color" => self.color.clone(),
                "primary_color" => self.primary_color.clone(),
                "placeholder_color" => self.placeholder_color.clone(),
                "highlight_color" => self.highlight_color.clone(),
                _ => self.primary.clone(),
            }
        } else {
            val
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Theme {
    pub name: &'static str,
    pub colors: ColorsSheet,
    pub body: BodyTheme,
    pub button: ButtonTheme,
    pub filled_button: ButtonTheme,
    pub outline_button: ButtonTheme,
    pub switch: SwitchTheme,
    pub scroll_bar: ScrollBarTheme,
    pub slider: SliderTheme,
    pub tooltip: TooltipTheme,
    pub dropdown: DropdownTheme,
    pub dropdown_item: DropdownItemTheme,
    pub accordion: AccordionTheme,
    pub loader: LoaderTheme,
    pub link: LinkTheme,
    pub progress_bar: ProgressBarTheme,
    pub table: TableTheme,
    pub input: InputTheme,
    pub canvas: CanvasTheme,
    pub graph: GraphTheme,
    pub network_image: NetworkImageTheme,
    pub icon: IconTheme,
    pub sidebar: SidebarTheme,
    pub sidebar_item: SidebarItemTheme,
    pub tile: TileTheme,
    pub radio: RadioTheme,
    pub checkbox: CheckboxTheme,
    pub menu_item: MenuItemTheme,
    pub menu_container: MenuContainerTheme,
    pub snackbar: SnackBarTheme,
    pub popup: PopupTheme,
    pub tab: TabTheme,
    pub bottom_tab: BottomTabTheme,
}

impl Default for Theme {
    fn default() -> Self {
        LIGHT_THEME
    }
}