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
use geom::CornerRadii;

use super::ButtonStyle;
use crate::{
    include_labeled_bytes, ButtonBuilder, ControlState, EventCtx, ScreenDims, Style, Widget,
};

pub trait StyledButtons<'a> {
    fn btn_solid_dark(&self) -> ButtonBuilder<'a>;
    fn btn_solid_dark_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_solid_dark().label_text(text)
    }
    fn btn_solid_dark_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_solid_dark().image_path(image_path))
    }
    fn btn_solid_dark_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_solid_dark()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_outline_dark(&self) -> ButtonBuilder<'a>;
    fn btn_outline_dark_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_dark().label_text(text)
    }
    fn btn_outline_dark_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_outline_dark().image_path(image_path))
    }
    fn btn_outline_dark_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_dark()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_solid_light(&self) -> ButtonBuilder<'a>;
    fn btn_solid_light_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_solid_light().label_text(text)
    }
    fn btn_solid_light_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_solid_light().image_path(image_path))
    }
    fn btn_solid_light_icon_bytes(&self, labeled_bytes: (&'a str, &'a [u8])) -> ButtonBuilder<'a> {
        icon_button(self.btn_solid_light().image_bytes(labeled_bytes))
    }
    fn btn_solid_light_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_solid_light()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_outline_light(&self) -> ButtonBuilder<'a>;
    fn btn_outline_light_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_light().label_text(text)
    }
    fn btn_outline_light_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_outline_light().image_path(image_path))
    }
    fn btn_outline_light_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_light()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_plain_dark(&self) -> ButtonBuilder<'a>;
    fn btn_plain_dark_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_plain_dark().label_text(text)
    }
    fn btn_plain_dark_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_plain_dark().image_path(image_path))
    }
    fn btn_plain_dark_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_plain_dark()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_plain_light(&self) -> ButtonBuilder<'a>;
    fn btn_plain_light_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_plain_light().label_text(text)
    }
    fn btn_plain_light_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_plain_light().image_path(image_path))
    }
    fn btn_plain_light_icon_bytes(&self, labeled_bytes: (&'a str, &'a [u8])) -> ButtonBuilder<'a> {
        icon_button(self.btn_plain_light().image_bytes(labeled_bytes))
    }
    fn btn_plain_light_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_plain_light()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_plain_destructive(&self) -> ButtonBuilder<'a>;
    fn btn_plain_destructive_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_plain_destructive().label_text(text)
    }
    fn btn_plain_destructive_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_plain_destructive().image_path(image_path))
    }

    fn btn_solid_destructive(&self) -> ButtonBuilder<'a>;
    fn btn_solid_destructive_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_solid_destructive().label_text(text)
    }
    fn btn_solid_destructive_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_solid_destructive().image_path(image_path))
    }
    fn btn_solid_destructive_icon_text(
        &self,
        image_path: &'a str,
        text: &'a str,
    ) -> ButtonBuilder<'a> {
        self.btn_solid_destructive()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    fn btn_outline_destructive(&self) -> ButtonBuilder<'a>;
    fn btn_outline_destructive_text(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_destructive().label_text(text)
    }
    fn btn_outline_destructive_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
        icon_button(self.btn_outline_destructive().image_path(image_path))
    }
    fn btn_outline_destructive_icon_text(
        &self,
        image_path: &'a str,
        text: &'a str,
    ) -> ButtonBuilder<'a> {
        self.btn_outline_destructive()
            .label_text(text)
            .image_path(image_path)
            .image_dims(ScreenDims::square(18.0))
    }

    // Specific UI Elements

    /// title: name of previous screen, which you'll return to
    fn btn_light_back(&self, title: &'a str) -> ButtonBuilder<'a> {
        back_button(self.btn_plain_light(), title)
    }

    /// title: name of previous screen, which you'll return to
    fn btn_dark_back(&self, title: &'a str) -> ButtonBuilder<'a> {
        back_button(self.btn_plain_dark(), title)
    }

    fn btn_solid_light_dropdown(&self) -> ButtonBuilder<'a> {
        dropdown_button(self.btn_solid_light())
    }

    fn btn_outline_light_dropdown(&self) -> ButtonBuilder<'a> {
        dropdown_button(self.btn_outline_light())
    }

    fn btn_solid_dark_dropdown(&self) -> ButtonBuilder<'a> {
        dropdown_button(self.btn_solid_dark())
    }

    fn btn_outline_dark_dropdown(&self) -> ButtonBuilder<'a> {
        dropdown_button(self.btn_outline_dark())
    }

    fn btn_outline_light_popup(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_light_dropdown().label_text(text)
    }

    fn btn_outline_dark_popup(&self, text: &'a str) -> ButtonBuilder<'a> {
        self.btn_outline_dark_dropdown().label_text(text)
    }

    /// A right facing caret, like ">", suitable for paging to the "next" set of results
    fn btn_next(&self) -> ButtonBuilder<'a> {
        self.btn_plain_light_icon_bytes(include_labeled_bytes!("../../icons/next.svg"))
    }

    /// A left facing caret, like "<", suitable for paging to the "next" set of results
    fn btn_prev(&self) -> ButtonBuilder<'a> {
        self.btn_plain_light_icon_bytes(include_labeled_bytes!("../../icons/prev.svg"))
    }

    /// An "X" button to close the current state. Bound to the escape key.
    fn btn_close(&self) -> ButtonBuilder<'a> {
        self.btn_plain_light_icon_bytes(include_labeled_bytes!("../../icons/close.svg"))
            .hotkey(Key::Escape)
    }

    /// An "X" button to close the current state. Bound to the escape key and aligned to the right,
    /// usually after a title.
    fn btn_close_widget(&self, ctx: &EventCtx) -> Widget {
        self.btn_close().build_widget(ctx, "close").align_right()
    }

    /// A button which renders its hotkey for discoverability along with its label.
    fn btn_solid_light_hotkey(&self, label: &str, key: Key) -> ButtonBuilder<'a>;

    /// A button which renders its hotkey for discoverability along with its label.
    fn btn_solid_dark_hotkey(&self, label: &str, key: Key) -> ButtonBuilder<'a>;
}

use crate::{Key, Line, Text};

impl<'a> StyledButtons<'a> for Style {
    fn btn_solid_dark(&self) -> ButtonBuilder<'a> {
        self.btn_solid(&self.btn_solid_dark)
    }

    fn btn_outline_dark(&self) -> ButtonBuilder<'a> {
        self.btn_outline(&self.btn_outline_dark)
    }

    fn btn_plain_dark(&self) -> ButtonBuilder<'a> {
        self.btn_plain(&self.btn_outline_dark)
    }

    fn btn_solid_light(&self) -> ButtonBuilder<'a> {
        self.btn_solid(&self.btn_solid_light)
    }

    fn btn_outline_light(&self) -> ButtonBuilder<'a> {
        self.btn_outline(&self.btn_outline_light)
    }

    fn btn_plain_light(&self) -> ButtonBuilder<'a> {
        self.btn_plain(&self.btn_outline_light)
    }

    fn btn_plain_destructive(&self) -> ButtonBuilder<'a> {
        self.btn_plain(&self.btn_outline_destructive)
    }

    fn btn_solid_destructive(&self) -> ButtonBuilder<'a> {
        self.btn_solid(&self.btn_solid_destructive)
    }

    fn btn_outline_destructive(&self) -> ButtonBuilder<'a> {
        self.btn_outline(&self.btn_solid_destructive)
    }

    fn btn_solid_light_hotkey(&self, label: &str, key: Key) -> ButtonBuilder<'a> {
        self.btn_hotkey(&self.btn_solid_light, label, key)
    }

    fn btn_solid_dark_hotkey(&self, label: &str, key: Key) -> ButtonBuilder<'a> {
        self.btn_hotkey(&self.btn_solid_dark, label, key)
    }
}

impl<'a> Style {
    pub fn btn_plain(&self, button_style: &ButtonStyle) -> ButtonBuilder<'a> {
        ButtonBuilder::new()
            .label_color(button_style.fg, ControlState::Default)
            .label_color(button_style.fg_disabled, ControlState::Disabled)
            .image_color(button_style.fg, ControlState::Default)
            .image_color(button_style.fg_disabled, ControlState::Disabled)
            .bg_color(button_style.bg, ControlState::Default)
            .bg_color(button_style.bg_hover, ControlState::Hovered)
            .bg_color(button_style.bg_disabled, ControlState::Disabled)
    }

    pub fn btn_solid(&self, button_style: &ButtonStyle) -> ButtonBuilder<'a> {
        self.btn_plain(button_style).outline(
            self.outline_thickness,
            button_style.outline,
            ControlState::Default,
        )
    }

    pub fn btn_outline(&self, button_style: &ButtonStyle) -> ButtonBuilder<'a> {
        self.btn_plain(button_style).outline(
            self.outline_thickness,
            button_style.outline,
            ControlState::Default,
        )
    }

    pub fn btn_hotkey(
        &self,
        button_style: &ButtonStyle,
        label: &str,
        key: Key,
    ) -> ButtonBuilder<'a> {
        let default = {
            let mut txt = Text::new();
            let key_txt = Line(key.describe()).fg(button_style.fg_hotkey);
            txt.append(key_txt);
            let label_text = Line(format!(" - {}", label)).fg(button_style.fg);
            txt.append(label_text);
            txt
        };

        let disabled = {
            let mut txt = Text::new();
            let key_txt = Line(key.describe()).fg(button_style.fg_hotkey.alpha(0.3));
            txt.append(key_txt);
            let label_text = Line(format!(" - {}", label)).fg(button_style.fg_disabled);
            txt.append(label_text);
            txt
        };

        self.btn_solid(button_style)
            .label_styled_text(default, ControlState::Default)
            .label_styled_text(disabled, ControlState::Disabled)
            .hotkey(key)
    }

    pub fn btn_light_popup_icon_text(
        &self,
        icon_path: &'a str,
        text: &'a str,
    ) -> ButtonBuilder<'a> {
        let outline_style = &self.btn_outline_light;
        let solid_style = &self.btn_solid_dark;

        // The text is styled like an "outline" button, while the image is styled like a "solid"
        // button.
        self.btn_outline(outline_style)
            .label_text(text)
            .image_path(icon_path)
            .image_dims(25.0)
            .image_color(solid_style.fg, ControlState::Default)
            .outline(
                self.outline_thickness,
                solid_style.outline,
                ControlState::Default,
            )
            .outline(
                self.outline_thickness,
                solid_style.bg_hover,
                ControlState::Hovered,
            )
            .image_bg_color(solid_style.bg, ControlState::Default)
            .image_bg_color(solid_style.bg_hover, ControlState::Hovered)
            // Move the padding from the *entire button* to just the image, so we get a colored
            // padded area around the image.
            .padding(0)
            .image_padding(8.0)
            // ...though we still need to pad between the text and button edge
            .padding_right(8.0)
            // Round the button's image's exterior corners so they don't protrude past the button's
            // corners. However, per design, we want the images interior corners to be
            // unrounded.
            .image_corner_rounding(CornerRadii {
                top_left: 2.0,
                top_right: 0.0,
                bottom_right: 0.0,
                bottom_left: 2.0,
            })
    }
}

// Captures some constants for uniform styling of icon-only buttons
fn icon_button<'a>(builder: ButtonBuilder<'a>) -> ButtonBuilder<'a> {
    builder.padding(8.0).image_dims(25.0)
}

fn back_button<'a>(builder: ButtonBuilder<'a>, title: &'a str) -> ButtonBuilder<'a> {
    // DESIGN REVIEW: this button seems absurdly large
    builder
        .image_bytes(include_labeled_bytes!("../../icons/nav_back.svg"))
        .label_text(title)
        .padding_left(8.0)
        .font_size(30)
}

fn dropdown_button<'a>(builder: ButtonBuilder<'a>) -> ButtonBuilder<'a> {
    builder
        .image_bytes(include_labeled_bytes!("../../icons/arrow_drop_down.svg"))
        .image_dims(12.0)
        .stack_spacing(12.0)
        .label_first()
}