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
use {
    CharacterCache,
    Color,
    Colorable,
    Dimension,
    FontSize,
    Frameable,
    FramedRectangle,
    FramedRectangleStyle,
    IndexSlot,
    Labelable,
    Mouse,
    Positionable,
    Scalar,
    Sizeable,
    Text,
    TextStyle,
    Ui,
};
use widget::{self, Widget};


/// A simple title bar widget that automatically sizes itself to the top of some other widget.
pub struct TitleBar<'a, F> {
    /// Data necessary and common for all widget builder types.
    pub common: widget::CommonBuilder,
    /// Unique styling for the **FramedRectangle**.
    pub style: Style,
    /// A label displayed in the middle of the TitleBar.
    pub label: &'a str,
    /// Some function used to react to interactions with the TitleBar.
    pub maybe_react: Option<F>,
}

/// Unique state for the **TitleBar** widget.
#[derive(Clone, Debug, PartialEq)]
pub struct State {
    interaction: Interaction,
    rectangle_idx: IndexSlot,
    label_idx: IndexSlot,
}

/// Unique styling for the **TitleBar** widget.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Style {
    /// Shape styling for the rectangle.
    pub framed_rectangle: FramedRectangleStyle,
    /// Styling for the label.
    pub text: TextStyle,
}

/// Some interaction with the **TitleBar**.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Interaction {
    Normal,
    Highlighted,
    Clicked,
}

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

/// The padding between the edge of the title bar and the title bar's label.
///
/// This is used to determine the size of the TitleBar.
const LABEL_PADDING: f64 = 4.0;


impl Style {

    /// A new default Style.
    pub fn new() -> Self {
        Style {
            framed_rectangle: FramedRectangleStyle::new(),
            text: TextStyle::new(),
        }
    }

}


/// Check the current state of the button.
fn get_new_interaction(is_over: bool, prev: Interaction, mouse: Mouse) -> Interaction {
    use mouse::ButtonPosition::{Down, Up};
    use self::Interaction::{Normal, Highlighted, Clicked};
    match (is_over, prev, mouse.left.position) {
        (true,  Normal,  Down) => Normal,
        (true,  _,       Down) => Clicked,
        (true,  _,       Up)   => Highlighted,
        (false, Clicked, Down) => Clicked,
        _                      => Normal,
    }
}


impl<'a, F> TitleBar<'a, F>
    where F: FnOnce(Interaction),
{

    /// Construct a new TitleBar widget and attach it to the widget at the given index.
    pub fn new<I>(label: &'a str, idx: I) -> Self
        where I: Into<widget::Index> + Copy,
    {
        TitleBar {
            common: widget::CommonBuilder::new(),
            style: Style::new(),
            label: label,
            maybe_react: None,
        }.width_of(idx).mid_top_of(idx)
    }

    /// Pass some styling for the **TitleBar**'s **Label**.
    pub fn label_style(mut self, style: TextStyle) -> Self {
        self.style.text = style;
        self
    }

    /// Pass some styling for the **TitleBar**'s **FramedRectangle**.
    pub fn rect_style(mut self, style: FramedRectangleStyle) -> Self {
        self.style.framed_rectangle = style;
        self
    }

    /// Pass the title bar some function to call upon interaction changes.
    pub fn react(mut self, f: F) -> Self {
        self.maybe_react = Some(f);
        self
    }

}


/// Calculate the default height for the **TitleBar**'s rect.
pub fn calc_height(font_size: FontSize) -> Scalar {
    font_size as Scalar + LABEL_PADDING * 2.0
}


impl<'a, F> Widget for TitleBar<'a, F>
    where F: FnOnce(Interaction),
{
    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 {
            rectangle_idx: IndexSlot::new(),
            label_idx: IndexSlot::new(),
            interaction: Interaction::Normal,
        }
    }

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

    fn default_y_dimension<C: CharacterCache>(&self, ui: &Ui<C>) -> Dimension {
        let font_size = self.style.text.font_size(&ui.theme);
        let h = calc_height(font_size);
        Dimension::Absolute(h)
    }

    fn update<C: CharacterCache>(self, args: widget::UpdateArgs<Self, C>) {
        let widget::UpdateArgs { idx, state, rect, style, mut ui, .. } = args;
        let TitleBar { label, maybe_react, .. } = self;

        // Check whether or not a new interaction has occurred.
        let new_interaction = match ui.input().maybe_mouse {
            None => Interaction::Normal,
            Some(mouse) => {
                let is_over = rect.is_over(mouse.xy);
                get_new_interaction(is_over, state.view().interaction, mouse)
            },
        };

        // FramedRectangle widget.
        let rectangle_idx = state.view().rectangle_idx.get(&mut ui);
        let dim = rect.dim();
        FramedRectangle::new(dim)
            .with_style(style.framed_rectangle)
            .middle_of(idx)
            .graphics_for(idx)
            .set(rectangle_idx, &mut ui);

        // Label widget.
        let label_idx = state.view().label_idx.get(&mut ui);
        Text::new(label)
            .with_style(style.text)
            .middle_of(rectangle_idx)
            .graphics_for(idx)
            .set(label_idx, &mut ui);

        if state.view().interaction != new_interaction {
            if let Some(react) = maybe_react {
                // If there's been some change in interaction and we have some react function, call
                // the react function with our new interaction.
                react(new_interaction);
            }
            state.update(|state| state.interaction = new_interaction);
        }
    }

}


impl<'a, F> Colorable for TitleBar<'a, F> {
    fn color(mut self, color: Color) -> Self {
        self.style.framed_rectangle.maybe_color = Some(color);
        self
    }
}

impl<'a, F> Frameable for TitleBar<'a, F> {
    fn frame(mut self, width: f64) -> Self {
        self.style.framed_rectangle.maybe_frame = Some(width);
        self
    }
    fn frame_color(mut self, color: Color) -> Self {
        self.style.framed_rectangle.maybe_frame_color = Some(color);
        self
    }
}

impl<'a, F> Labelable<'a> for TitleBar<'a, F> {
    fn label(mut self, text: &'a str) -> Self {
        self.label = text;
        self
    }

    fn label_color(mut self, color: Color) -> Self {
        self.style.text.maybe_color = Some(color);
        self
    }

    fn label_font_size(mut self, size: FontSize) -> Self {
        self.style.text.maybe_font_size = Some(size);
        self
    }
}