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
//! The widget identifier type used throughout conrod, along with helper types and macros to
//! simplify the process of generating them.

use daggy;
use graph::Graph;
use std;

/// Unique widget identifier.
///
/// Each widget instance must use its own, uniquely generated `widget::Id` so that it's state can
/// be cached within the `Ui` type.
///
/// Indices are generated consecutively from `0`. This allows us to avoid the need for hashing
/// identifiers in favour of indexing directly into the `Graph`'s underlying node array.
///
/// `widget::Id`s may be generated via the `widget_ids!` macro.
pub type Id = daggy::NodeIndex<u32>;

/// Used for generating new unique `widget::Id`s.
///
/// `Generator` is used by the `widget_ids!` macro and the types and fields that it generates in
/// order to generate unique `widget::Id`s for each of the generated fields.
pub struct Generator<'a> { widget_graph: &'a mut Graph }

/// A list of lazily generated `widget::Id`s.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct List(Vec<Id>);
/// An iterator-like type for producing indices from a `List`.
#[allow(missing_copy_implementations)]
pub struct ListWalk { i: usize }


impl<'a> Generator<'a> {

    /// Constructor for a new `widget::Id` generator.
    pub fn new(widget_graph: &'a mut Graph) -> Self {
        Generator {
            widget_graph: widget_graph,
        }
    }

    /// Generate a new, unique `widget::Id` into a Placeholder node within the widget graph. This
    /// should only be called once for each unique widget needed to avoid unnecessary bloat within
    /// the `Ui`'s widget graph.
    ///
    /// When using this method, be sure to store the returned `widget::Id` somewhere so that it can
    /// be re-used on next update.
    ///
    /// **Panics** if adding another node would exceed the maximum capacity for node indices.
    pub fn next(&mut self) -> Id {
        self.widget_graph.add_placeholder()
    }

}


impl List {

    /// Construct a cache for multiple indices.
    pub fn new() -> Self {
        List(Vec::new())
    }

    /// Produce a walker for producing the `List`'s indices.
    pub fn walk(&self) -> ListWalk {
        ListWalk { i: 0 }
    }

    /// Resizes the `List`'s inner `Vec` to the given target length, using the given `UiCell` to
    /// generate new unique `widget::Id`s if necessary.
    pub fn resize(&mut self, target_len: usize, id_generator: &mut Generator) {
        if self.len() < target_len {
            self.0.reserve(target_len);
            while self.len() < target_len {
                self.0.push(id_generator.next());
            }
        }
        while self.len() > target_len {
            self.0.pop();
        }
    }

}

impl std::ops::Deref for List {
    type Target = [Id];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ListWalk {

    /// Yield the next index, generating one if it does not yet exist.
    pub fn next(&mut self, &mut List(ref mut ids): &mut List, id_gen: &mut Generator) -> Id {
        while self.i >= ids.len() {
            ids.push(id_gen.next());
        }
        let ix = ids[self.i];
        self.i += 1;
        ix
    }

}


/// A macro used to generate a struct with a field for each unique identifier given.
/// Each field can then be used to generate unique `widget::Id`s.
///
/// For example, given the following invocation:
///
/// ```
/// #[macro_use]
/// extern crate conrod_core;
///
/// widget_ids! {
///     struct Ids {
///         button,
///         toggles[],
///     }
/// }
/// # fn main() {}
/// ```
///
/// The following will be produced:
///
/// ```
/// # extern crate conrod_core;
/// # fn main() {}
/// struct Ids {
///     button: conrod_core::widget::Id,
///     toggles: conrod_core::widget::id::List,
/// }
///
/// impl Ids {
///     pub fn new(mut generator: conrod_core::widget::id::Generator) -> Self {
///         Ids {
///             button: generator.next(),
///             toggles: conrod_core::widget::id::List::new(),
///         }
///     }
/// }
/// ```
///
/// In the example above, the generated `Ids` type can be used as follows.
///
/// ```ignore
/// widget::Button::new().set(ids.button, ui);
/// 
/// ids.toggles.resize(5, &mut ui.widget_id_generator());
/// for &id in &ids.toggles {
///     widget::Toggle::new(true).set(id, ui);
/// }
/// ```
#[macro_export]
macro_rules! widget_ids {


    ///////////////////////
    ///// widget_ids! /////
    ///////////////////////

    // The top-level branch, to be invoked by the user.
    ($(#[$attr:meta])*     struct $Ids:ident { $($id:tt)* }) => {
        widget_ids! { implement $(#[$attr])* []    $Ids { $($id)* } }
    };
    // The same as above, but makes the generated `$Ids` type `pub`.
    ($(#[$attr:meta])* pub struct $Ids:ident { $($id:tt)* }) => {
        widget_ids! { implement $(#[$attr])* [pub] $Ids { $($id)* } }
    };

    // Calls the `define_struct` and `constructor` branches, implementing the struct and
    // constructor method respectively.
    (implement $(#[$attr:meta])* [$($public:tt)*] $Ids:ident { $($id:tt)* }) => {
        widget_ids! {
            define_struct $(#[$attr])* [$($public)*] $Ids { {} $($id)* }
        }

        impl $Ids {

            /// Construct a new `widget::Id` container.
            #[allow(unused_mut, unused_variables)]
            pub fn new(mut generator: $crate::widget::id::Generator) -> Self {
                widget_ids! {
                    constructor $Ids, generator { {} $($id)* }
                }
            }

        }
    };


    /////////////////////////
    ///// define_struct /////
    /////////////////////////
    //
    // From
    //
    // ```ignore
    // widget_ids! {
    //     define_struct Ids {
    //         button,
    //         toggles[],
    //     }
    // }
    // ```
    //
    // these branches generate
    //
    // ```ignore
    // struct Ids {
    //     button: conrod_core::widget::Id,
    //     toggles: conrod_core::widget::id::List,
    // }
    // ```

    // Converts `foo[]` tokens to `foo: conrod_core::widget::id::List`.
    (define_struct $(#[$attr:meta])* [$($public:tt)*] $Ids:ident {
        { $($id_field:ident: $T:path,)* } $id:ident[], $($rest:tt)*
    }) => {
        widget_ids! {
            define_struct $(#[$attr])* [$($public)*] $Ids {
                {
                    $($id_field: $T,)*
                    $id: $crate::widget::id::List,
                }
                $($rest)*
            }
        }
    };

    // Converts `foo` tokens to `foo: conrod_core::widget::Id`.
    (define_struct $(#[$attr:meta])* [$($public:tt)*] $Ids:ident {
        { $($id_field:ident: $T:path,)* } $id:ident, $($rest:tt)*
    }) => {
        widget_ids! {
            define_struct $(#[$attr])* [$($public)*] $Ids {
                {
                    $($id_field: $T,)*
                    $id: $crate::widget::Id,
                }
                $($rest)*
            }
        }
    };

    // Same as above but without the trailing comma.
    (define_struct $(#[$attr:meta])* [$($public:tt)*] $Ids:ident {
        { $($id_field:ident: $T:path,)* } $id:ident[]
    }) => {
        widget_ids! {
            define_struct $(#[$attr])* [$($public)*] $Ids { { $($id_field: $T,)* } $id[], }
        }
    };
    (define_struct $(#[$attr:meta])* [$($public:tt)*] $Ids:ident {
        { $($id_field:ident: $T:path,)* } $id:ident
    }) => {
        widget_ids! {
            define_struct $(#[$attr])* [$($public)*] $Ids { { $($id_field: $T,)* } $id, }
        }
    };

    // Generates the struct using all the `ident: path` combinations generated above.
    (define_struct $(#[$attr:meta])* [pub] $Ids:ident { { $($id:ident: $T:path,)* } }) => {
        $(#[$attr])*
        pub struct $Ids {
            $(pub $id: $T,
            )*
        }
    };

    // The same as the previous branch, but private.
    (define_struct $(#[$attr:meta])* [] $Ids:ident { { $($id:ident: $T:path,)* } }) => {
        $(#[$attr])*
        struct $Ids {
            $(pub $id: $T,
            )*
        }
    };


    ///////////////////////
    ///// constructor /////
    ///////////////////////
    //
    // From
    //
    // ```ignore
    // widget_ids! {
    //     constructor Ids, generator {
    //         button,
    //         toggles[],
    //     }
    // }
    // ```
    //
    // these branches generate
    //
    // ```ignore
    // Ids {
    //     button: generator.next(),
    //     toggles: conrod_core::widget::id::List::new(),
    // }
    // ```

    // Converts `foo[]` to `foo: conrod_core::widget::id::List::new()`.
    (constructor $Ids:ident, $generator:ident {
        { $($id_field:ident: $new:expr,)* } $id:ident[], $($rest:tt)*
    }) => {
        widget_ids! {
            constructor $Ids, $generator {
                {
                    $($id_field: $new,)*
                    $id: $crate::widget::id::List::new(),
                }
                $($rest)*
            }
        }
    };

    // Converts `foo` to `foo: generator.next()`.
    (constructor $Ids:ident, $generator:ident {
        { $($id_field:ident: $new:expr,)* } $id:ident, $($rest:tt)*
    }) => {
        widget_ids! {
            constructor $Ids, $generator {
                {
                    $($id_field: $new,)*
                    $id: $generator.next(),
                }
                $($rest)*
            }
        }
    };

    // Same as above but without the trailing comma.
    (constructor $Ids:ident, $generator:ident {
        { $($id_field:ident: $new:expr,)* } $id:ident[]
    }) => {
        widget_ids! { constructor $Ids, $generator { { $($id_field: $new,)* } $id[], } }
    };
    (constructor $Ids:ident, $generator:ident {
        { $($id_field:ident: $new:expr,)* } $id:ident
    }) => {
        widget_ids! { constructor $Ids, $generator { { $($id_field: $new,)* } $id, } }
    };

    // Generates the `$Ids` constructor using the `field: expr`s generated above.
    (constructor $Ids:ident, $generator:ident { { $($id:ident: $new:expr,)* } }) => {
        $Ids {
            $(
                $id: $new,
            )*
        }
    };

}


#[test]
fn test() {
    use ui::UiBuilder;
    use widget::{self, Widget};

    widget_ids! {
        /// Testing generated Ids doc comments.
        #[derive(Clone)]
        pub struct Ids {
            button,
            toggles[],
        }
    }

    let mut ui = UiBuilder::new([800.0, 600.0]).build();
    let mut ids = Ids::new(ui.widget_id_generator());

    for _ in 0..10 {
        let ref mut ui = ui.set_widgets();

        // Single button index.
        widget::Button::new().set(ids.button, ui);

        // Lazily generated toggle indices.
        ids.toggles.resize(5, &mut ui.widget_id_generator());
        for &id in ids.toggles.iter() {
            widget::Toggle::new(true).set(id, ui);
        }
    }
}

#[test]
#[allow(unused_variables)]
fn test_invocation_variations() {
    use ui::UiBuilder;

    widget_ids! { struct A { foo, bar } }
    widget_ids! { pub struct B { foo, bar } }
    widget_ids! { struct C { foo, bar, } }
    widget_ids! { pub struct D { foo, bar, } }
    widget_ids! { struct E { foo[], bar } }
    widget_ids! { pub struct F { foo, bar[] } }
    widget_ids! { struct G { foo[], bar, } }
    widget_ids! { pub struct H { foo, bar[], } }

    let mut ui = UiBuilder::new([800.0, 600.0]).build();
    let mut ui = ui.set_widgets();
    let a = A::new(ui.widget_id_generator());
    let b = B::new(ui.widget_id_generator());
    let c = C::new(ui.widget_id_generator());
    let d = D::new(ui.widget_id_generator());
    let e = E::new(ui.widget_id_generator());
    let f = F::new(ui.widget_id_generator());
    let g = G::new(ui.widget_id_generator());
    let h = H::new(ui.widget_id_generator());
}