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
use {
    Backend,
    CharacterCache,
    Color,
    Colorable,
    Dimensions,
    FontSize,
    Frameable,
    FramedRectangle,
    GlyphCache,
    IndexSlot,
    Line,
    Mouse,
    Padding,
    Point,
    Positionable,
    Range,
    Rectangle,
    Scalar,
    Text,
    Widget,
};
use input::keyboard::Key::{Backspace, Left, Right, Return, A, E, LCtrl, RCtrl};
use vecmath::vec2_sub;
use widget::{self, KidArea};


pub type Idx = usize;
pub type CursorX = f64;

const TEXT_PADDING: Scalar = 5.0;

/// A widget for displaying and mutating a given one-line text `String`. It's reaction is
/// triggered upon pressing of the `Enter`/`Return` key.
pub struct TextBox<'a, F> {
    common: widget::CommonBuilder,
    text: &'a mut String,
    /// The reaction for the TextBox.
    ///
    /// If `Some`, this will be triggered upon pressing of the `Enter`/`Return` key.
    pub maybe_react: Option<F>,
    style: Style,
    /// Whether or not user input is enabled for the TextBox.
    pub enabled: bool,
}

/// Unique kind for the widget type.
pub const KIND: widget::Kind = "TextBox";

widget_style!{
    KIND;
    /// Unique graphical styling for the TextBox.
    style Style {
        /// Color of the rectangle behind the text. If you don't want to see the rectangle, set the
        /// color with a zeroed alpha.
        - color: Color { theme.shape_color }
        /// The frame around the rectangle behind the text.
        - frame: Scalar { theme.frame_width }
        /// The color of the frame.
        - frame_color: Color { theme.frame_color }
        /// The font size for the text.
        - font_size: FontSize { 24 }
        /// The color of the text.
        - text_color: Color { theme.label_color }
    }
}

/// The State of the TextBox widget that will be cached within the Ui.
#[derive(Clone, Debug, PartialEq)]
pub struct State {
    interaction: Interaction,
    rectangle_idx: IndexSlot,
    text_idx: IndexSlot,
    cursor_idx: IndexSlot,
    highlight_idx: IndexSlot,
    control_pressed: bool
}

/// Represents the state of the text_box widget.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Interaction {
    Captured(View),
    Uncaptured(Uncaptured),
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct View {
    cursor: Cursor,
    offset: f64,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Cursor {
    anchor: Anchor,
    start: Idx,
    end: Idx,
}

/// The beginning of the cursor's highlighted range.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Anchor {
    None,
    Start,
    End,
}

/// The TextBox's state if it is uncaptured.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Uncaptured {
    Highlighted,
    Normal,
}

/// Represents an element of the TextBox widget.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Elem {
    Char(Idx),
    Nill,
    Rect,
}


impl Interaction {
    /// Return the color associated with the current state.
    fn color(&self, color: Color) -> Color {
        match *self {
            Interaction::Captured(_) => color,
            Interaction::Uncaptured(state) => match state {
                Uncaptured::Highlighted => color.highlighted(),
                Uncaptured::Normal => color,
            }
        }
    }
}


impl Cursor {

    /// Construct a Cursor from a String index.
    fn from_index(idx: Idx) -> Cursor {
        Cursor { anchor: Anchor::Start, start: idx, end: idx }
    }

    /// Construct a Cursor from an index range.
    fn from_range(start: Idx, end: Idx) -> Cursor {
        if start < end {
            Cursor { anchor: Anchor::Start, start: start, end: end }
        } else {
            Cursor { anchor: Anchor::End, start: end, end: start }
        }
    }

    /// Ensure the cursor does not go past end_limit. Returns true if the cursor was
    /// previously too large.
    fn limit_end_to(&mut self, end_limit: usize) -> bool {
        if self.start > end_limit {
            self.start = end_limit;
            self.end = end_limit;
            return true;
        }
        else if self.end > end_limit {
            self.end = end_limit;
            return true
        }
        false
    }

    /// Shift the curosr by the given number of indices.
    fn shift(&mut self, by: i32) {
        if by == 0 {
             return;
        }
        let mut new_idx = self.start as i32 + by;
        if new_idx < 0 {
            new_idx = 0;
        }
        self.start = new_idx as Idx;
        self.end = new_idx as Idx;
    }

    fn is_cursor(&self) -> bool {
        self.start == self.end
    }
}

// widget_fns!(TextBox, State, Kind::TextBox(State::Uncaptured(Uncaptured::Normal)));

/// Find the position of a character in a text box.
fn cursor_position<C: CharacterCache>(glyph_cache: &GlyphCache<C>,
                                      idx: usize,
                                      mut text_start_x: f64,
                                      font_size: FontSize,
                                      text: &str) -> CursorX {
    assert!(idx <= text.chars().count());

    if idx == 0 {
         return text_start_x;
    }

    for (i, ch) in text.chars().enumerate() {
        if i >= idx { break; }
        text_start_x += glyph_cache.char_width(font_size, ch);
    }
    text_start_x
}

/// Check if cursor is over the pad and if so, which
fn over_elem<C: CharacterCache>(glyph_cache: &GlyphCache<C>,
                                mouse_xy: Point,
                                dim: Dimensions,
                                pad_dim: Dimensions,
                                text_start_x: f64,
                                text_w: f64,
                                font_size: FontSize,
                                text: &str) -> Elem {
    use position::is_over_rect;
    if is_over_rect([0.0, 0.0], dim, mouse_xy) {
        if is_over_rect([0.0, 0.0], pad_dim, mouse_xy) {
            let (idx, _) = closest_idx(glyph_cache, mouse_xy, text_start_x, text_w, font_size, text);
            Elem::Char(idx)
        } else {
            Elem::Rect
        }
    } else {
        Elem::Nill
    }
}

/// Check which character is closest to the mouse cursor.
fn closest_idx<C: CharacterCache>(glyph_cache: &GlyphCache<C>,
                                  mouse_xy: Point,
                                  text_start_x: f64,
                                  text_w: f64,
                                  font_size: FontSize,
                                  text: &str) -> (Idx, f64) {
    if mouse_xy[0] <= text_start_x { return (0, text_start_x) }
    let mut left_x = text_start_x;
    let mut x = left_x;
    let mut prev_x = x;
    for (i, ch) in text.chars().enumerate() {
        let char_w = glyph_cache.char_width(font_size, ch);
        x += char_w;
        let right_x = prev_x + char_w / 2.0;
        if mouse_xy[0] > left_x && mouse_xy[0] <= right_x { return (i, prev_x) }
        prev_x = x;
        left_x = right_x;
    }
    (text.chars().count(), text_start_x + text_w)
}

/// Check and return the current state of the TextBox.
fn get_new_interaction(over_elem: Elem, prev_interaction: Interaction, mouse: Mouse) -> Interaction {
    use mouse::ButtonPosition::{Down, Up};
    use self::Interaction::{Captured, Uncaptured};
    use self::Uncaptured::{Normal, Highlighted};

    match prev_interaction {
        Interaction::Captured(mut prev) => match mouse.left.position {
            Down => match over_elem {
                Elem::Nill => if prev.cursor.anchor == Anchor::None {
                    Uncaptured(Normal)
                } else {
                    prev_interaction
                },
                Elem::Rect =>  if prev.cursor.anchor == Anchor::None {
                    prev.cursor = Cursor::from_index(0);
                    Captured(prev)
                } else {
                    prev_interaction
                },
                Elem::Char(idx) => {
                    match prev.cursor.anchor {
                        Anchor::None  => prev.cursor = Cursor::from_index(idx),
                        Anchor::Start => prev.cursor = Cursor::from_range(prev.cursor.start, idx),
                        Anchor::End   => prev.cursor = Cursor::from_range(prev.cursor.end, idx),
                    }
                    Captured(prev)
                },
            },
            Up => {
                prev.cursor.anchor = Anchor::None;
                Captured(prev)
            },
        },

        Interaction::Uncaptured(prev) => match mouse.left.position {
            Down => match over_elem {
                Elem::Nill => Uncaptured(Normal),
                Elem::Rect => match prev {
                    Normal => prev_interaction,
                    Highlighted => Captured(View {
                         cursor: Cursor::from_index(0),
                         offset: 0.0,
                    })
                },
                Elem::Char(idx) =>  match prev {
                    Normal => prev_interaction,
                    Highlighted => Captured(View {
                        cursor: Cursor::from_index(idx),
                        offset: 0.0,
                    })
                },
            },
            Up => match over_elem {
                Elem::Nill => Uncaptured(Normal),
                Elem::Char(_) | Elem::Rect => match prev {
                    Normal => Uncaptured(Highlighted),
                    Highlighted => prev_interaction,
                },
            },
        },
    }
}

impl<'a, F> TextBox<'a, F> {

    /// Construct a TextBox widget.
    pub fn new(text: &'a mut String) -> TextBox<'a, F> {
        TextBox {
            common: widget::CommonBuilder::new(),
            text: text,
            maybe_react: None,
            style: Style::new(),
            enabled: true,
        }
    }

    builder_methods!{
        pub font_size { style.font_size = Some(FontSize) }
        pub react { maybe_react = Some(F) }
        pub enabled { enabled = bool }
    }

}

impl<'a, F> Widget for TextBox<'a, F>
    where F: FnMut(&mut String),
{
    type State = State;
    type Style = Style;

    fn common(&self) -> &widget::CommonBuilder {
        &self.common
    }

    fn common_mut(&mut self) -> &mut widget::CommonBuilder {
        &mut self.common
    }

    fn unique_kind(&self) -> &'static str {
        KIND
    }

    fn init_state(&self) -> State {
        State {
            interaction: Interaction::Uncaptured(Uncaptured::Normal),
            rectangle_idx: IndexSlot::new(),
            text_idx: IndexSlot::new(),
            cursor_idx: IndexSlot::new(),
            highlight_idx: IndexSlot::new(),
            control_pressed: false,
        }
    }

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

    fn kid_area<C: CharacterCache>(&self, args: widget::KidAreaArgs<Self, C>) -> widget::KidArea {
        KidArea {
            rect: args.rect,
            pad: Padding {
                x: Range::new(TEXT_PADDING, TEXT_PADDING),
                y: Range::new(TEXT_PADDING, TEXT_PADDING),
            },
        }
    }

    /// Update the state of the TextBox.
    fn update<B: Backend>(mut self, args: widget::UpdateArgs<Self, B>) {
        let widget::UpdateArgs { idx, state, rect, style, mut ui, .. } = args;

        let (xy, dim) = rect.xy_dim();
        let maybe_mouse = ui.input(idx).maybe_mouse.map(|mouse| mouse.relative_to(xy));
        let frame = style.frame(ui.theme());
        let inner_rect = rect.pad(frame);
        let font_size = style.font_size(ui.theme());
        let pad_dim = vec2_sub(dim, [frame * 2.0; 2]);
        let text_w = ui.glyph_cache().width(font_size, &self.text);
        let text_x = text_w / 2.0 - pad_dim[0] / 2.0 + TEXT_PADDING;
        let text_start_x = text_x - text_w / 2.0;
        let mut new_control_pressed = state.view().control_pressed;
        let mut new_interaction = match (self.enabled, maybe_mouse) {
            (false, _) | (true, None) => Interaction::Uncaptured(Uncaptured::Normal),
            (true, Some(mouse)) => {
                let over_elem = over_elem(ui.glyph_cache(), mouse.xy, dim, pad_dim, text_start_x,
                                          text_w, font_size, &self.text);
                get_new_interaction(over_elem, state.view().interaction, mouse)
            },
        };

        // Check cursor validity (and update new_interaction if necessary).
        if let Interaction::Captured(view) = new_interaction {
            let mut cursor = view.cursor;
            let mut v_offset = view.offset;

            // Ensure the cursor is still valid.
            cursor.limit_end_to(self.text.chars().count());

            // This matters if the text is scrolled with the mouse.
            let cursor_idx = match cursor.anchor {
                Anchor::End => cursor.start,
                Anchor::Start | Anchor::None => cursor.end,
            };
            let cursor_x = cursor_position(ui.glyph_cache(), cursor_idx, text_start_x, font_size,
                                           &self.text);

            if cursor.is_cursor() || cursor.anchor != Anchor::None {
                let cursor_x_view = cursor_x - v_offset;
                let text_right = dim[0] - TEXT_PADDING - frame;

                if cursor_x_view < text_x {
                    v_offset += cursor_x_view - text_x;
                } else if cursor_x_view > text_right {
                    v_offset += cursor_x_view - text_right;
                }
            }

            // Set the updated state.
            new_interaction = Interaction::Captured(View { cursor: cursor, offset: v_offset });
        }

        // If TextBox is captured, check for recent input and update the text accordingly.
        if let Interaction::Captured(captured) = new_interaction {
            let mut cursor = captured.cursor;
            let input = ui.input(idx);

            // Check for entered text.
            for text in input.entered_text {
                if new_control_pressed { break; }
                if text.chars().count() == 0 { continue; }

                let max_w = pad_dim[0] - TEXT_PADDING * 2.0;
                if text_w + ui.glyph_cache().width(font_size, &text) > max_w { continue; }

                let end: String = self.text.chars().skip(cursor.end).collect();
                let start: String = self.text.chars().take(cursor.start).collect();
                self.text.clear();
                self.text.push_str(&start);
                self.text.push_str(&text);
                self.text.push_str(&end);
                cursor.shift(text.chars().count() as i32);
            }

            // Check for control keys.
            for key in input.pressed_keys.iter() {
                match *key {
                    Backspace => if cursor.is_cursor() {
                        if cursor.start > 0 {
                            let start: String = self.text.chars().take(cursor.start-1).collect();
                            let end: String = self.text.chars().skip(cursor.end).collect();
                            self.text.clear();
                            self.text.push_str(&start);
                            self.text.push_str(&end);
                            cursor.shift(-1);
                        }
                    } else {
                        let start: String = self.text.chars().take(cursor.start).collect();
                        let end: String = self.text.chars().skip(cursor.end).collect();
                        self.text.clear();
                        self.text.push_str(&start);
                        self.text.push_str(&end);
                        cursor.end = cursor.start;
                    },
                    Left => if cursor.is_cursor() {
                        cursor.shift(-1);
                    },
                    Right => if cursor.is_cursor() && self.text.chars().count() > cursor.end {
                        cursor.shift(1);
                    },
                    Return => if self.text.chars().count() > 0 {
                        let TextBox { ref mut maybe_react, ref mut text, .. } = self;
                        if let Some(ref mut react) = *maybe_react {
                            react(*text);
                        }
                    },
                    LCtrl | RCtrl if !new_control_pressed => {
                        new_control_pressed = true;
                    },
                    A if new_control_pressed => {
                        if cursor.is_cursor() {
                            cursor.start = 0;
                            cursor.end = self.text.chars().count();
                        }
                    },
                    E if new_control_pressed => {
                        if cursor.is_cursor() {
                            cursor.start = self.text.chars().count();
                            cursor.end = self.text.chars().count();
                        }
                    },
                    _ => (),
                }
            }

            for key in input.released_keys.iter() {
                match *key {
                    LCtrl | RCtrl if new_control_pressed => {
                        new_control_pressed = false;
                    },
                    _ => (),
                }
            }

            // In case the string text was mutated with the `react` function, we need to make sure
            // the cursor is limited to the current number of chars.
            cursor.limit_end_to(self.text.chars().count());

            new_interaction = Interaction::Captured(View { cursor: cursor, .. captured });
        }

        // Check the interactions to determine whether we need to capture or uncapture the keyboard.
        match (state.view().interaction, new_interaction) {
            (Interaction::Uncaptured(_), Interaction::Captured(_)) => { ui.capture_keyboard(idx); },
            (Interaction::Captured(_), Interaction::Uncaptured(_)) => { ui.uncapture_keyboard(idx); },
            _ => (),
        }

        if state.view().interaction != new_interaction {
            state.update(|state| state.interaction = new_interaction);
        }

        if state.view().control_pressed != new_control_pressed {
            state.update(|state| state.control_pressed = new_control_pressed);
        }

        let rectangle_idx = state.view().rectangle_idx.get(&mut ui);
        let frame = style.frame(ui.theme());
        let color = new_interaction.color(style.color(ui.theme()));
        let frame_color = style.frame_color(ui.theme());
        FramedRectangle::new(rect.dim())
            .middle_of(idx)
            .graphics_for(idx)
            .color(color)
            .frame(frame)
            .frame_color(frame_color)
            .set(rectangle_idx, &mut ui);

        let text_color = style.text_color(ui.theme());
        let font_size = style.font_size(ui.theme());
        let text_idx = state.view().text_idx.get(&mut ui);
        Text::new(&self.text)
            .mid_left_of(idx)
            .graphics_for(idx)
            .color(text_color)
            .font_size(font_size)
            .no_line_wrap()
            .set(text_idx, &mut ui);


        if let Interaction::Captured(view) = new_interaction {
            let cursor = view.cursor;
            // This matters if the text is scrolled with the mouse.
            let cursor_idx = match cursor.anchor {
                Anchor::End => cursor.start,
                Anchor::Start | Anchor::None => cursor.end,
            };
            let cursor_x = cursor_position(ui.glyph_cache(), cursor_idx, text_start_x, font_size,
                                           &self.text);

            if cursor.is_cursor() {
                let cursor_idx = state.view().cursor_idx.get(&mut ui);
                let start = [0.0, 0.0];
                let end = [0.0, inner_rect.h()];
                Line::centred(start, end)
                    .x_relative_to(idx, cursor_x)
                    .graphics_for(idx)
                    .parent(idx)
                    .color(text_color)
                    .set(cursor_idx, &mut ui);
            } else {
                let (rel_x, w) = {
                    let (start, end) = (cursor.start, cursor.end);
                    let cursor_x = cursor_position(ui.glyph_cache(), start, text_start_x, font_size,
                                                   &self.text);
                    let highlighted_text = &self.text[start..end];
                    let w = ui.glyph_cache().width(font_size, &highlighted_text);
                    (cursor_x + w / 2.0, w)
                };
                let dim = [w, inner_rect.h()];
                let highlight_idx = state.view().highlight_idx.get(&mut ui);
                Rectangle::fill(dim)
                    .x_relative_to(idx, rel_x)
                    .color(text_color.highlighted().alpha(0.25))
                    .graphics_for(idx)
                    .parent(idx)
                    .set(highlight_idx, &mut ui);
            }
        }
    }

}


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

impl<'a, F> Frameable for TextBox<'a, F> {
    builder_methods!{
        frame { style.frame = Some(Scalar) }
        frame_color { style.frame_color = Some(Color) }
    }
}