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
use {
    Align,
    Backend,
    Color,
    Colorable,
    FontSize,
    Frameable,
    FramedRectangle,
    IndexSlot,
    Positionable,
    Range,
    Rect,
    Scalar,
    Sizeable,
    TextEdit,
    Widget,
};
use event;
use input;
use widget;

/// A widget for displaying and mutating a small, one-line field of text, given by the user in the
/// form of a `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,
}

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

widget_style!{
    KIND;
    /// Unique graphical styling for the TextBox.
    style Style {
        /// The length of the gap between the bounding rectangle's frame and the edge of the text.
        - text_padding: Scalar { 5.0 }
        /// Color of the rectangle behind the text.
        ///
        /// If you don't want to see the rectangle, either set the color with a zeroed alpha or use
        /// the `TextEdit` widget directly.
        - color: Color { theme.shape_color }
        /// The width of the bounding `FramedRectangle` frame.
        - frame: Scalar { theme.frame_width }
        /// The color of the `FramedRecangle`'s frame.
        - frame_color: Color { theme.frame_color }
        /// The color of the `TextEdit` widget.
        - text_color: Color { theme.label_color }
        /// The font size for the text.
        - font_size: FontSize { theme.font_size_medium }
        /// The horizontal alignment of the text.
        - x_align: Align { Align::Start }
    }
}

/// The `State` of the `TextBox` widget that will be cached within the `Ui`.
#[derive(Clone, Debug, PartialEq)]
pub struct State {
    text_edit_idx: IndexSlot,
    rectangle_idx: IndexSlot,
}

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

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

    /// Align the text to the left of its bounding **Rect**'s *x* axis range.
    pub fn align_text_left(self) -> Self {
        self.x_align_text(Align::Start)
    }

    /// Align the text to the middle of its bounding **Rect**'s *x* axis range.
    pub fn align_text_middle(self) -> Self {
        self.x_align_text(Align::Middle)
    }

    /// Align the text to the right of its bounding **Rect**'s *x* axis range.
    pub fn align_text_right(self) -> Self {
        self.x_align_text(Align::End)
    }

    builder_methods!{
        pub font_size { style.font_size = Some(FontSize) }
        pub react { maybe_react = Some(F) }
        pub x_align_text { style.x_align = Some(Align) }
        pub pad_text { style.text_padding = Some(Scalar) }
    }

}

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 {
            text_edit_idx: IndexSlot::new(),
            rectangle_idx: IndexSlot::new(),
        }
    }

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

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

        let font_size = style.font_size(ui.theme());
        let frame = style.frame(ui.theme());
        let text_padding = style.text_padding(ui.theme());
        let x_align = style.x_align(ui.theme());

        let text_rect = {
            let w = rect.x.pad(frame + text_padding).len();
            let h = font_size as Scalar + 1.0;
            let x = Range::new(0.0, w).align_middle_of(rect.x);
            let y = Range::new(0.0, h).align_middle_of(rect.y);
            Rect { x: x, y: y }
        };

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

        let text_edit_idx = state.text_edit_idx.get(&mut ui);
        let text_color = style.text_color(ui.theme());
        TextEdit::new(text)
            .wh(text_rect.dim())
            .xy(text_rect.xy())
            .font_size(font_size)
            .color(text_color)
            .x_align_text(x_align)
            .parent(idx)
            .react(|_text: &mut String| {})
            .set(text_edit_idx, &mut ui);

        // React to any `Enter`/`Return` presses.
        //
        // TODO: We should be doing this via the `TextEdit` widget.
        for widget_event in ui.widget_input(text_edit_idx).events() {
            match widget_event {
                event::Widget::Press(press) => match press.button {
                    event::Button::Keyboard(key) => match key {
                        input::Key::Return => {
                            if let Some(mut react) = maybe_react.take() {
                                react(text);
                            }
                        },
                        _ => ()
                    },
                    _ => (),
                },
                _ => (),
            }
        }
    }

}

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

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