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
/// Defines a struct called `$Style`.
///
/// Each given `$field_name` `$FieldType` pair will be defined as `Option` fields.
#[doc(hidden)]
#[macro_export]
macro_rules! define_widget_style_struct {

    (
        $(#[$Style_attr:meta])*
        style $Style:ident {
            $( $(#[$field_attr:meta])* - $field_name:ident: $FieldType:ty { $default:expr })*
        }
    ) => {
        $(#[$Style_attr])*
        #[derive(Copy, Clone, Debug, PartialEq)]
        pub struct $Style {
            $(
                $(#[$field_attr])*
                pub $field_name: Option<$FieldType>,
            )*
        }
    };

    (
        $(#[$Style_attr:meta])*
        style $Style:ident {
            $(
                $(#[$field_attr:meta])*
                - $field_name:ident: $FieldType:ty { theme.$($theme_field:ident).+ }
             )*
        }
    ) => {
        define_widget_style_struct!{
            $(#[$Style_attr])*
            style $Style {
                $( $(#[$field_attr])* - $field_name: $FieldType {})*
            }
        }
    };

}


/// Produces a default instance of `$Style` (aka all fields set to `None`).
#[doc(hidden)]
#[macro_export]
macro_rules! default_widget_style_struct {

    (
        $Style:ident {
            $(
                $(#[$field_attr:meta])*
                - $field_name:ident: $FieldType:ty { $default:expr }
             )*
        }
    ) => {
        $Style {
            $(
                $field_name: None,
            )*
        }
    };

    (
        $Style:ident {
            $(
                $(#[$field_attr:meta])*
                - $field_name:ident: $FieldType:ty { theme.$($theme_field:ident).+ }
             )*
        }
    ) => {
        default_widget_style_struct!{
            $Style {
                $(
                    $(#[$field_attr])*
                    - $field_name: $FieldType {}
                )*
            }
        };
    };

}

/// Implements the static method `new` for the `$Style` type.
#[doc(hidden)]
#[macro_export]
macro_rules! impl_widget_style_new {
    ($Style:ident { $($fields:tt)* }) => {
        impl $Style {
            /// Construct the default `Style`, initialising all fields to `None`.
            pub fn new() -> Self {
                default_widget_style_struct!($Style { $($fields)* })
            }
        }
    };
}


#[doc(hidden)]
#[macro_export]
macro_rules! impl_widget_style_retrieval_method {
    ($KIND:ident, $field_name:ident: $FieldType:ty { theme.$($theme_field:ident).+ }) => {
        /// Retrieves the value from the `Style`.
        ///
        /// If the `Style`'s field is `None`, falls back to default specified within the `Theme`.
        pub fn $field_name(&self, theme: &$crate::Theme) -> $FieldType {
            self.$field_name
                .or_else(|| theme.widget_style::<Self>($KIND).and_then(|default| {
                    default.style.$field_name
                }))
                .unwrap_or_else(|| theme.$($theme_field).+)
        }
    };
    ($KIND:ident, $field_name:ident: $FieldType:ty { $default:expr }) => {
        /// Retrieves the value from the `Style`.
        ///
        /// If the `Style`'s field is `None`, falls back to default specified within the `Theme`.
        pub fn $field_name(&self, theme: &$crate::Theme) -> $FieldType {
            self.$field_name
                .or_else(|| theme.widget_style::<Self>($KIND).and_then(|default| {
                    default.style.$field_name
                }))
                .unwrap_or_else(|| $default)
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! impl_widget_style_retrieval_methods {

    // Retrieval methods with a field on the `theme` to use as the default.
    (
        $KIND:ident,
        $(#[$field_attr:meta])*
        - $field_name:ident: $FieldType:ty { theme.$($theme_field:ident).+ } $($rest:tt)*
    ) => {
        impl_widget_style_retrieval_method!($KIND, $field_name: $FieldType { theme.$($theme_field).+});
        impl_widget_style_retrieval_methods!($KIND, $($rest)*);
    };


    // Retrieval methods with an expression to use as the default.
    (
        $KIND:ident,
        $(#[$field_attr:meta])*
        - $field_name:ident: $FieldType:ty { $default:expr } $($rest:tt)*
    ) => {
        impl_widget_style_retrieval_method!($KIND, $field_name: $FieldType { $default });
        impl_widget_style_retrieval_methods!($KIND, $($rest)*);
    };

    // All methods have been implemented.
    ($KIND:ident,) => {};
}




/// A macro for vastly simplifying the definition and implementation of a widget's associated
/// `Style` type.
///
/// For more information on the purpose of this `Style` type, see the associated `type Style` docs
/// in the [`Widget` trait documentation](./trait.Widget.html).
///
/// Using the macro looks like this:
///
/// ```
/// # #[macro_use] extern crate conrod;
/// # fn main() {}
/// # const KIND: conrod::WidgetKind = "Example";
/// widget_style!{
///     KIND;
///     // Doc comment or attr for the generated `Style` struct can go here.
///     style Style {
///         // Fields and their type T (which get converted to `Option<T>` in the struct definition)
///         // along with their default expression go here.
///         // You can also write doc comments or attr above each field.
///         - color: conrod::Color { theme.shape_color }
///         - label_color: conrod::Color { conrod::color::BLUE }
///         // .. more fields.
///     }
/// }
/// ```
///
/// An invocation of the macro expands into two things:
///
/// 1. A struct definition with the given name following the `style` token.
/// 2. An `impl Style` block with a `new` constructor as well as a style retrieval method for each
///    given field. These retrieval methods do the following:
///
///    1. Attempt to use the value at the field.
///    2. If the field is `None`, attempts to retreive a default from the `widget_styling` map in
///       the `Ui`'s current `Theme`.
///    3. If no defaults were found, evaluates the given default expression (or `theme.field`).
///
///
/// # Examples
///
/// The following is a typical usage example for the `widget_style!` macro.
///
/// ```
/// #[macro_use]
/// extern crate conrod;
/// 
/// struct MyWidget {
///     style: Style,
///     // Other necessary fields...
/// }
///
/// /// A unique string used to dynamically identify the widget type.
/// ///
/// /// Conrod uses this to store unique styling for each widget in a `HashMap` within the `Theme`.
/// const KIND: conrod::WidgetKind = "MyWidget";
///
/// widget_style!{
///     KIND;
///     /// Unique, awesome styling for a `MyWidget`.
///     style Style {
///         /// The totally amazing color to use for the `MyWidget`.
///         ///
///         /// If the `color` is unspecified and there is no default given via the `Theme`, the
///         /// `Theme`'s standard `shape_color` field will be used as a fallback.
///         - color: conrod::Color { theme.shape_color }
///         /// The extremely pretty color to use for the `MyWidget`'s label.
///         ///
///         /// If the `label_color` is unspecified and there is no default given via the `Theme`,
///         /// the label will fallback to `conrod::color::PURPLE`.
///         - label_color: conrod::Color { conrod::color::PURPLE }
///     }
/// }
///
/// // We can now retrieve the `color` or `label_color` for a `MyWidget` like so:
/// // let color = my_widget.style.color(&theme);
/// // let label_color = my_widget.style.label_color(&theme);
/// 
/// # fn main() {}
/// ```
///
/// And here is what it expands into:
///
/// ```
/// #[macro_use]
/// extern crate conrod;
///
/// struct MyWidget {
///     style: Style,
///     // Other necessary fields...
/// }
///
/// /// A unique string used to dynamically identify the widget type.
/// ///
/// /// Conrod uses this to store unique styling for each widget in a `HashMap` within the `Theme`.
/// const KIND: conrod::WidgetKind = "MyWidget";
///
/// /// Unique, awesome styling for a `MyWidget`.
/// #[derive(Copy, Clone, Debug, PartialEq)]
/// pub struct Style {
///     /// The totally amazing color to use for the `MyWidget`.
///     ///
///     /// If the `color` is unspecified and there is no default given via the `Theme`, the
///     /// `Theme`'s standard `shape_color` field will be used as a fallback.
///     color: Option<conrod::Color>,
///     /// The extremely pretty color to use for the `MyWidget`'s label.
///     ///
///     /// If the `label_color` is unspecified and there is no default given via the `Theme`,
///     /// the label will fallback to `conrod::color::PURPLE`.
///     label_color: Option<conrod::Color>,
/// }
///
/// impl Style {
///
///     /// Construct the default `Style`, initialising all fields to `None`.
///     pub fn new() -> Self {
///         Style {
///             color: None,
///             label_color: None,
///         }
///     }
/// 
///     /// Retrieves the value from the `Style`.
///     ///
///     /// If the `Style`'s field is `None`, falls back to default specified within the `Theme`.
///     pub fn color(&self, theme: &conrod::Theme) -> conrod::Color {
///         self.color
///             .or_else(|| theme.widget_style::<Self>(KIND).and_then(|default| {
///                 default.style.color
///             }))
///             .unwrap_or_else(|| theme.shape_color)
///     }
///
///     /// Retrieves the value from the `Style`.
///     ///
///     /// If the `Style`'s field is `None`, falls back to default specified within the `Theme`.
///     pub fn label_color(&self, theme: &conrod::Theme) -> conrod::Color {
///         self.label_color
///             .or_else(|| theme.widget_style::<Self>(KIND).and_then(|default| {
///                 default.style.label_color
///             }))
///             .unwrap_or_else(|| conrod::color::PURPLE)
///     }
///
/// }
///
/// // We can now retrieve the `color` or `label_color` for a `MyWidget` like so:
/// // let color = my_widget.style.color(&theme);
/// // let label_color = my_widget.style.label_color(&theme);
///
/// # fn main() {}
/// ```
#[macro_export]
macro_rules! widget_style {
    (
        $KIND:ident;
        $(#[$Style_attr:meta])*
        style $Style:ident {
            $($fields:tt)*
        }
    ) => {

        // The `Style` struct.
        define_widget_style_struct!{
            $(#[$Style_attr])*
            style $Style {
                $($fields)*
            }
        }

        // The `new` method for the `Style` struct.
        impl_widget_style_new!($Style { $($fields)* });

        // The "field, theme or default" retrieval methods.
        impl $Style {
            impl_widget_style_retrieval_methods!($KIND, $($fields)*);
        }
    };
}