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
//! # Prompt widgets for conrod
//! These widgets are used to create prompts for the user to interact with on-screen.
//! The library is organized as a generic `Prompt` widget which has an internal widget
//! which accepts user interaction.
//!
//! Some common user interaction cases are implemented already:
//! - `Ack`: a prompt for when you want the user to see a message and acknowledge it
//! - `YesNo`: a prompt for when you want the user to see a message and indicate whether or not
//! they accept it

#[macro_use]
extern crate conrod_derive;

pub use ack::Ack;
pub use yesno::YesNo;

use conrod_core::{
    self,
    widget_ids,
    widget::{self, Widget},
    text,
    color::{Color, Colorable},
    FontSize,
    Borderable,
    position,
    Positionable,
    Sizeable,
    Labelable,
    Scalar,
    builder_methods,
    builder_method,
};

/// A general widget for displaying a floating prompt to the user.
/// The prompt displays some message and offers the user an opportunity
/// to respond to the message via a contained child widget, which
/// accepts some form of input.
#[derive(Clone, Debug, WidgetCommon)]
pub struct Prompt<'a, I> {
    #[conrod(common_builder)]
    common: widget::CommonBuilder,
    text: &'a str,
    // Input widget wrapped by the prompt.
    input: I,
    style: Style,
}

impl<'a, I> Prompt<'a, I> {
    /// Construct a new Prompt with the given prompt message.
    pub fn new(text: &'a str, input: I) -> Self {
        Prompt {
            common: widget::CommonBuilder::default(),
            text: text,
            input,
            style: Style::default(),
        }
    }

    /// Construct a new Prompt with the given message and style.
    pub fn with_style(text: &'a str, input: I, style: Style) -> Self {
        Prompt {
            common: widget::CommonBuilder::default(),
            text: text,
            input,
            style,
        }
    }

    pub fn label_font_id(mut self, font_id: text::font::Id) -> Self {
        self.style.label_font_id = Some(Some(font_id));
        self
    }
}

impl<'a>  Prompt<'a, ack::Ack<'a>> {
    /// Return a new prompt which awaits user acknowledgment.
    pub fn ack(text: &'a str) -> Self {
        Prompt::new(text, ack::Ack::new())
    }
}

impl<'a> Prompt<'a, yesno::YesNo<'a>> {
    /// Return a new prompt which awaits a yes or no answer from the user.
    pub fn yes_no(text: &'a str) -> Self {
        Prompt::new(text, yesno::YesNo::new())
    }
}

impl<'a> Prompt<'a, widget::TextBox<'a>> {
    /// Return a new prompt which awaits a text response from the user.
    pub fn text_box(text: &'a str, box_text: &'a str) -> Self {
        Prompt::new(text, widget::TextBox::new(box_text))
    }
}

#[derive(Copy, Clone, Debug, Default, PartialEq, WidgetStyle)]
pub struct Style {
    #[conrod(default = "theme.shape_color")]
    pub color: Option<Color>,
    #[conrod(default = "theme.border_width")]
    pub border: Option<Scalar>,
    #[conrod(default = "theme.border_color")]
    pub border_color: Option<Color>,
    #[conrod(default = "theme.label_color")]
    pub label_color: Option<Color>,
    #[conrod(default = "5.0")]
    pub padding: Option<Scalar>,
    #[conrod(default = "theme.font_size_medium")]
    pub label_font_size: Option<FontSize>,
    #[conrod(default = "theme.font_id")]
    pub label_font_id: Option<Option<text::font::Id>>,
    #[conrod(default = "text::Justify::Left")]
    pub label_justify: Option<text::Justify>,
    #[conrod(default = "position::Relative::Align(position::Align::Middle)")]
    pub label_x: Option<position::Relative>,
    #[conrod(default = "position::Relative::Align(position::Align::Middle)")]
    pub label_y: Option<position::Relative>,
}

widget_ids! {
    pub struct Ids {
        container,
        message,
        input,
    }
}

impl<'a, I> Widget for Prompt<'a, I> where I: Widget {
    type State = Ids;
    type Style = Style;
    type Event = I::Event;

    fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
        Ids::new(id_gen)
    }

    fn style(&self) -> Self::Style {
        self.style
    }

    fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
        let widget::UpdateArgs { id, state, style, rect, ui, .. } = args;

        // create the containing canvas
        let dim = rect.dim();
        let color = style.color(&ui.theme);
        let border_color = style.border_color(&ui.theme);
        let border = style.border(&ui.theme);
        widget::Canvas::new()
            .wh(dim)
            .middle_of(id)
            .graphics_for(id)
            .color(color)
            .border(border)
            .border_color(border_color)
            .floating(true)  // make the container float
            .set(state.container, ui);


        let label_color = style.label_color(&ui.theme);
        let font_size = style.label_font_size(&ui.theme);
        let font_id = style.label_font_id(&ui.theme).or(ui.fonts.ids().next());
        let msg_padding = style.padding(&ui.theme);
        let msg_widget = widget::Text::new(self.text)
            .and_then(font_id, widget::Text::font_id)
            .mid_top_of(state.container)
            .padded_w_of(state.container, msg_padding)
            .font_size(font_size)
            .graphics_for(id)
            .color(label_color);
        msg_widget.set(state.message, ui);

        // TODO: any styling on the child?
        let widget_event = self.input
            .parent(state.container)
            .mid_bottom_with_margin_on(state.container, msg_padding)
            .set(state.input, ui);

        widget_event
    }
}

impl<'a, I> Labelable<'a> for Prompt<'a, I> {
    builder_methods! {
        label { text = &'a str }
        label_color { style.label_color = Some(Color) }
        label_font_size { style.label_font_size = Some(FontSize) }
    }
}

impl<'a, I> Colorable for Prompt<'a, I> {
    builder_method!(color { style.color = Some(Color) });
}

impl<'a, I> Borderable for Prompt<'a, I> {
    builder_methods! {
        border { style.border = Some(Scalar) }
        border_color { style.border_color = Some(Color) }
    }
}

/// The `Ack` widget and related items.
pub mod ack {
    use super::*;
    use widget::button::Style;

    widget_ids! {
        pub struct Ids {
            button,
        }
    }

    /// Simply request that the user acknowledge the message.
    /// This is a wrapper around a single Button with the default text "OK".
    #[derive(Debug, WidgetCommon)]
    pub struct Ack<'a> {
        #[conrod(common_builder)]
        common: widget::CommonBuilder,
        label: &'a str,
        style: Style,
    }

    impl<'a> Ack<'a> {
        pub fn new() -> Self {
            Ack {
                common: widget::CommonBuilder::default(),
                label: "OK",
                style: Style::default(),
            }
        }

        pub fn label_font_id(mut self, font_id: text::font::Id) -> Self {
            self.style.label_font_id = Some(Some(font_id));
            self
        }
    }

    impl<'a> Widget for Ack<'a> {
        type State = Ids;
        type Style = Style;
        type Event = Option<()>;

        fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
            Ids::new(id_gen)
        }

        fn style(&self) -> Self::Style {
            self.style
        }

        fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
            let widget::UpdateArgs { id, state, style, rect, ui, .. } = args;

            let dim = rect.dim();
            let label_color = style.label_color(&ui.theme);
            let font_size = style.label_font_size(&ui.theme);
            let font_id = style.label_font_id(&ui.theme).or(ui.fonts.ids().next());
            let times_clicked = widget::Button::new()
                .enabled(true)
                .label(self.label)
                .and_then(font_id, widget::Button::label_font_id)
                .label_color(label_color)
                .label_font_size(font_size)
                .middle_of(id)
                .wh(dim)
                .set(state.button, ui);

            if times_clicked.was_clicked() {
                Some(())
            } else {
                None
            }
        }
    }

    impl<'a> Labelable<'a> for Ack<'a> {
        builder_methods! {
            label { label = &'a str }
            label_color { style.label_color = Some(Color) }
            label_font_size { style.label_font_size = Some(FontSize) }
        }
    }

}

/// The YesNo widget and related items.
pub mod yesno {
    use super::*;
    /// Request that the user respond with yes or no.
    /// This is a wrapper around two side-by-side buttons with the default labels "Yes" and "No".
    #[derive(Debug, WidgetCommon)]
    pub struct YesNo<'a> {
        #[conrod(common_builder)]
        common: widget::CommonBuilder,
        yes_label: &'a str,
        no_label: &'a str,
        style: Style,
    }

    #[derive(Copy, Clone, Debug, Default, PartialEq, WidgetStyle)]
    pub struct Style {
        pub yes: widget::button::Style,
        pub no: widget::button::Style,
    }

    impl<'a> YesNo<'a> {
        pub fn new() -> Self {
            YesNo {
                common: widget::CommonBuilder::default(),
                yes_label: "Yes",
                no_label: "No",
                style: Style::default(),
            }
        }

        pub fn yes_label_font_id(mut self, font_id: text::font::Id) -> Self {
            self.style.yes.label_font_id = Some(Some(font_id));
            self
        }

        pub fn no_label_font_id(mut self, font_id: text::font::Id) -> Self {
            self.style.no.label_font_id = Some(Some(font_id));
            self
        }

        builder_methods! {
            yes_label { yes_label = &'a str }
            yes_label_color { style.yes.label_color = Some(Color) }
            yes_label_font_size { style.yes.label_font_size = Some(FontSize) }
            no_label { no_label = &'a str }
            no_label_color { style.no.label_color = Some(Color) }
            no_label_font_size { style.no.label_font_size = Some(FontSize) }
        }
    }

    widget_ids! {
        pub struct Ids {
            canvas,
            left,
            right,
            yes,
            no,
        }
    }

    impl<'a> Widget for YesNo<'a> {
        type State = Ids;
        type Style = Style;
        type Event = Option<bool>;

        fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
            Ids::new(id_gen)
        }

        fn style(&self) -> Self::Style {
            self.style
        }

        fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
            let widget::UpdateArgs { id, state, style, rect, ui, .. } = args;

            let dim = rect.dim();
            widget::Canvas::new()
                .middle_of(id)
                .graphics_for(id)
                .wh(dim)
                .flow_right(&[
                           (state.left, widget::Canvas::new()),
                           (state.right, widget::Canvas::new()),
                ])
                .set(state.canvas, ui);

            let yes_label_color = style.yes.label_color(&ui.theme);
            let yes_font_size = style.yes.label_font_size(&ui.theme);
            let yes_font_id = style.yes.label_font_id(&ui.theme).or(ui.fonts.ids().next());
            let yes_label_x = style.yes.label_x(&ui.theme);
            let yes_label_y = style.yes.label_y(&ui.theme);
            let yes = widget::Button::new()
                .enabled(true)
                .label(self.yes_label)
                .and_then(yes_font_id, widget::Button::label_font_id)
                .label_color(yes_label_color)
                .label_font_size(yes_font_size)
                .label_x(yes_label_x)
                .label_y(yes_label_y)
                .middle_of(state.left)
                .wh_of(state.left)
                .set(state.yes, ui);

            let no_label_color = style.no.label_color(&ui.theme);
            let no_font_size = style.no.label_font_size(&ui.theme);
            let no_font_id = style.no.label_font_id(&ui.theme).or(ui.fonts.ids().next());
            let no_label_x = style.no.label_x(&ui.theme);
            let no_label_y = style.no.label_y(&ui.theme);
            let no = widget::Button::new()
                .enabled(true)
                .label(self.no_label)
                .and_then(no_font_id, widget::Button::label_font_id)
                .label_color(no_label_color)
                .label_font_size(no_font_size)
                .label_x(no_label_x)
                .label_y(no_label_y)
                .middle_of(state.right)
                .wh_of(state.right)
                .set(state.no, ui);

            if yes.was_clicked() {
                Some(true)
            } else if no.was_clicked() {
                Some(false)
            } else {
                None
            }
        }
    }
}