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
#![allow(clippy::if_same_then_else)]

use crate::{
    color::*,
    math::*,
    paint::{Stroke, TextStyle},
    types::*,
};

/// Specifies the look and feel of a `Ui`.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Style {
    /// Default `TextStyle` for normal text (i.e. for `Label` and `TextEdit`).
    pub body_text_style: TextStyle,

    pub spacing: Spacing,
    pub interaction: Interaction,
    pub visuals: Visuals,

    /// How many seconds a typical animation should last
    pub animation_time: f32,
}

impl Style {
    // TODO: rename style.interact() to maybe... `style.interactive` ?
    /// Use this style for interactive things.
    /// Note that you must already have a response,
    /// i.e. you must allocate space and interact BEFORE painting the widget!
    pub fn interact(&self, response: &Response) -> &WidgetVisuals {
        self.visuals.widgets.style(response)
    }

    /// Style to use for non-interactive widgets.
    pub fn noninteractive(&self) -> &WidgetVisuals {
        &self.visuals.widgets.noninteractive
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Spacing {
    /// Horizontal and vertical spacing between widgets
    pub item_spacing: Vec2,

    /// Horizontal and vertical padding within a window frame.
    pub window_padding: Vec2,

    /// Button size is text size plus this on each side
    pub button_padding: Vec2,

    /// Indent collapsing regions etc by this much.
    pub indent: f32,

    /// Minimum size of e.g. a button.
    /// `interact_size.y` is the default height of button, slider, etc.
    /// Anything clickable should be (at least) this size.
    pub interact_size: Vec2, // TODO: rename min_interact_size ?

    /// Total width of a slider.
    pub slider_width: f32, // TODO: rename big_interact_size ?

    /// Checkboxes, radio button and collapsing headers have an icon at the start.
    /// This is the width/height of this icon.
    pub icon_width: f32,

    /// Checkboxes, radio button and collapsing headers have an icon at the start.
    /// This is the spacing between the icon and the text
    pub icon_spacing: f32,
}

impl Spacing {
    /// Returns small icon rectangle and big icon rectangle
    pub fn icon_rectangles(&self, rect: Rect) -> (Rect, Rect) {
        let box_side = self.icon_width;
        let big_icon_rect = Rect::from_center_size(
            pos2(rect.left() + box_side / 2.0, rect.center().y),
            vec2(box_side, box_side),
        );

        let small_rect_side = 8.0; // TODO: make a parameter
        let small_icon_rect =
            Rect::from_center_size(big_icon_rect.center(), Vec2::splat(small_rect_side));

        (small_icon_rect, big_icon_rect)
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Interaction {
    /// Mouse must be the close to the side of a window to resize
    pub resize_grab_radius_side: f32,

    /// Mouse must be the close to the corner of a window to resize
    pub resize_grab_radius_corner: f32,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Visuals {
    pub widgets: Widgets,

    /// e.g. the background of the slider or text edit,
    /// needs to look different from other interactive stuff.
    pub dark_bg_color: Srgba, // TODO: remove, rename, or clarify what it is for

    pub window_corner_radius: f32,

    pub resize_corner_size: f32,

    /// Blink text cursor by this frequency. If 0, always show the cursor.
    pub cursor_blink_hz: f32,
    pub text_cursor_width: f32,

    /// Allow child widgets to be just on the border and still have a stroke with some thickness
    pub clip_rect_margin: f32,

    // -----------------------------------------------
    // Debug rendering:
    pub debug_widget_rects: bool,
    pub debug_resize: bool,
}

impl Visuals {
    pub fn noninteractive(&self) -> &WidgetVisuals {
        &self.widgets.noninteractive
    }

    pub fn text_color(&self) -> Srgba {
        self.widgets.noninteractive.text_color()
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Widgets {
    /// For an interactive widget that is being interacted with
    pub active: WidgetVisuals,
    /// For an interactive widget that is being hovered
    pub hovered: WidgetVisuals,
    /// For an interactive widget that is "resting"
    pub inactive: WidgetVisuals,
    /// For an otherwise interactive widget that has been disabled
    pub disabled: WidgetVisuals,
    /// For a non-interactive widget
    pub noninteractive: WidgetVisuals,
}

impl Widgets {
    pub fn style(&self, response: &Response) -> &WidgetVisuals {
        if response.active || response.has_kb_focus {
            &self.active
        } else if response.sense == Sense::nothing() {
            &self.disabled
        } else if response.hovered {
            &self.hovered
        } else {
            &self.inactive
        }
    }
}

/// bg = background, fg = foreground.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WidgetVisuals {
    /// Background color of widget
    pub bg_fill: Srgba,

    /// For surrounding rectangle of things that need it,
    /// like buttons, the box of the checkbox, etc.
    pub bg_stroke: Stroke,

    /// Button frames etc
    pub corner_radius: f32,

    /// Fill color of the interactive part of a component (slider grab, checkbox, ...)
    /// When you need a fill.
    pub fg_fill: Srgba,

    /// Stroke and text color of the interactive part of a component (button text, slider grab, check-mark, ...)
    pub fg_stroke: Stroke,
}

impl WidgetVisuals {
    pub fn text_color(&self) -> Srgba {
        self.fg_stroke.color
    }
}

// ----------------------------------------------------------------------------

impl Default for Style {
    fn default() -> Self {
        Self {
            body_text_style: TextStyle::Body,
            spacing: Spacing::default(),
            interaction: Interaction::default(),
            visuals: Visuals::default(),
            animation_time: 1.0 / 15.0,
        }
    }
}

impl Default for Spacing {
    fn default() -> Self {
        Self {
            item_spacing: vec2(8.0, 3.0),
            window_padding: vec2(6.0, 6.0),
            button_padding: vec2(3.0, 1.0),
            indent: 25.0,
            interact_size: vec2(40.0, 20.0),
            slider_width: 140.0,
            icon_width: 16.0,
            icon_spacing: 1.0,
        }
    }
}

impl Default for Interaction {
    fn default() -> Self {
        Self {
            resize_grab_radius_side: 5.0,
            resize_grab_radius_corner: 10.0,
        }
    }
}

impl Default for Visuals {
    fn default() -> Self {
        Self {
            widgets: Default::default(),
            dark_bg_color: Srgba::black_alpha(140),
            window_corner_radius: 10.0,
            resize_corner_size: 12.0,
            cursor_blink_hz: 0.0, // 1.0 looks good
            text_cursor_width: 2.0,
            clip_rect_margin: 3.0,
            debug_widget_rects: false,
            debug_resize: false,
        }
    }
}

impl Default for Widgets {
    fn default() -> Self {
        Self {
            active: WidgetVisuals {
                bg_fill: Srgba::black_alpha(128),
                bg_stroke: Stroke::new(2.0, WHITE),
                corner_radius: 4.0,
                fg_fill: srgba(120, 120, 200, 255),
                fg_stroke: Stroke::new(2.0, WHITE),
            },
            hovered: WidgetVisuals {
                bg_fill: Rgba::luminance_alpha(0.06, 0.5).into(),
                bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.5)),
                corner_radius: 4.0,
                fg_fill: srgba(100, 100, 150, 255),
                fg_stroke: Stroke::new(1.5, Srgba::gray(240)),
            },
            inactive: WidgetVisuals {
                bg_fill: Rgba::luminance_alpha(0.04, 0.5).into(),
                bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.06)), // default window outline. Should be pretty readable
                corner_radius: 4.0,
                fg_fill: srgba(60, 60, 80, 255),
                fg_stroke: Stroke::new(1.0, Srgba::gray(200)), // Should NOT look grayed out!
            },
            disabled: WidgetVisuals {
                bg_fill: TRANSPARENT,
                bg_stroke: Stroke::new(0.5, Srgba::gray(70)),
                corner_radius: 4.0,
                fg_fill: srgba(50, 50, 50, 255),
                fg_stroke: Stroke::new(1.0, Srgba::gray(128)), // Should look grayed out
            },
            noninteractive: WidgetVisuals {
                bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.06)),
                bg_fill: Rgba::luminance_alpha(0.010, 0.975).into(), // window background
                corner_radius: 4.0,
                fg_fill: Default::default(),
                fg_stroke: Stroke::new(1.0, Srgba::gray(160)), // text color
            },
        }
    }
}

// ----------------------------------------------------------------------------

use crate::{widgets::*, Ui};

impl Style {
    pub fn ui(&mut self, ui: &mut crate::Ui) {
        if ui.button("Reset").clicked {
            *self = Default::default();
        }

        let Self {
            body_text_style,
            spacing,
            interaction,
            visuals,
            animation_time,
        } = self;
        ui.horizontal(|ui| {
            ui.label("Default text style:");
            for &value in &[TextStyle::Body, TextStyle::Monospace] {
                ui.radio_value(body_text_style, value, format!("{:?}", value));
            }
        });
        ui.collapsing("Spacing", |ui| spacing.ui(ui));
        ui.collapsing("Interaction", |ui| interaction.ui(ui));
        ui.collapsing("Visuals", |ui| visuals.ui(ui));
        ui.add(Slider::f32(animation_time, 0.0..=1.0).text("animation_time"));
    }
}

impl Spacing {
    pub fn ui(&mut self, ui: &mut crate::Ui) {
        if ui.button("Reset").clicked {
            *self = Default::default();
        }

        let Self {
            item_spacing,
            window_padding,
            button_padding,
            indent,
            interact_size,
            slider_width,
            icon_width,
            icon_spacing,
        } = self;

        ui_slider_vec2(ui, item_spacing, 0.0..=10.0, "item_spacing");
        ui_slider_vec2(ui, window_padding, 0.0..=10.0, "window_padding");
        ui_slider_vec2(ui, button_padding, 0.0..=10.0, "button_padding");
        ui_slider_vec2(ui, interact_size, 0.0..=60.0, "interact_size")
            .on_hover_text("Minimum size of an interactive widget");
        ui.add(Slider::f32(indent, 0.0..=100.0).text("indent"));
        ui.add(Slider::f32(slider_width, 0.0..=1000.0).text("slider_width"));
        ui.add(Slider::f32(icon_width, 0.0..=60.0).text("icon_width"));
        ui.add(Slider::f32(icon_spacing, 0.0..=10.0).text("icon_spacing"));
    }
}

impl Interaction {
    pub fn ui(&mut self, ui: &mut crate::Ui) {
        if ui.button("Reset").clicked {
            *self = Default::default();
        }

        let Self {
            resize_grab_radius_side,
            resize_grab_radius_corner,
        } = self;

        ui.add(Slider::f32(resize_grab_radius_side, 0.0..=20.0).text("resize_grab_radius_side"));
        ui.add(
            Slider::f32(resize_grab_radius_corner, 0.0..=20.0).text("resize_grab_radius_corner"),
        );
    }
}

impl Widgets {
    pub fn ui(&mut self, ui: &mut crate::Ui) {
        if ui.button("Reset").clicked {
            *self = Default::default();
        }

        let Self {
            active,
            hovered,
            inactive,
            disabled,
            noninteractive,
        } = self;

        ui.collapsing("noninteractive", |ui| noninteractive.ui(ui));
        ui.collapsing("interactive & disabled", |ui| disabled.ui(ui));
        ui.collapsing("interactive & inactive", |ui| inactive.ui(ui));
        ui.collapsing("interactive & hovered", |ui| hovered.ui(ui));
        ui.collapsing("interactive & active", |ui| active.ui(ui));
    }
}

impl WidgetVisuals {
    pub fn ui(&mut self, ui: &mut crate::Ui) {
        let Self {
            bg_fill,
            bg_stroke,
            corner_radius,
            fg_fill,
            fg_stroke,
        } = self;

        ui_color(ui, bg_fill, "bg_fill");
        bg_stroke.ui(ui, "bg_stroke");
        ui.add(Slider::f32(corner_radius, 0.0..=10.0).text("corner_radius"));
        ui_color(ui, fg_fill, "fg_fill");
        fg_stroke.ui(ui, "fg_stroke (text)");
    }
}

impl Visuals {
    pub fn ui(&mut self, ui: &mut crate::Ui) {
        if ui.button("Reset").clicked {
            *self = Default::default();
        }

        let Self {
            widgets,
            dark_bg_color,
            window_corner_radius,
            resize_corner_size,
            cursor_blink_hz,
            text_cursor_width,
            clip_rect_margin,
            debug_widget_rects,
            debug_resize,
        } = self;

        ui.collapsing("widgets", |ui| widgets.ui(ui));
        ui_color(ui, dark_bg_color, "dark_bg_color");
        ui.add(Slider::f32(window_corner_radius, 0.0..=20.0).text("window_corner_radius"));
        ui.add(Slider::f32(resize_corner_size, 0.0..=20.0).text("resize_corner_size"));
        ui.add(Slider::f32(cursor_blink_hz, 0.0..=4.0).text("cursor_blink_hz"));
        ui.add(Slider::f32(text_cursor_width, 0.0..=2.0).text("text_cursor_width"));
        ui.add(Slider::f32(clip_rect_margin, 0.0..=20.0).text("clip_rect_margin"));

        ui.checkbox(debug_widget_rects, "Paint debug rectangles around widgets");
        ui.checkbox(debug_resize, "Debug Resize");
    }
}

impl Stroke {
    pub fn ui(&mut self, ui: &mut crate::Ui, text: &str) {
        let Self { width, color } = self;
        ui.horizontal(|ui| {
            ui.add(DragValue::f32(width).speed(0.1).range(0.0..=5.0))
                .on_hover_text("Width");
            ui.color_edit_button_srgba(color);
            ui.label(text);

            // stroke preview:
            let stroke_rect = ui.allocate_space(ui.style().spacing.interact_size);
            let left = stroke_rect.left_center();
            let right = stroke_rect.right_center();
            ui.painter().line_segment([left, right], (*width, *color));
        });
    }
}

// TODO: improve and standardize ui_slider_vec2
fn ui_slider_vec2(
    ui: &mut Ui,
    value: &mut Vec2,
    range: std::ops::RangeInclusive<f32>,
    text: &str,
) -> Response {
    let (_, rect) = ui.horizontal(|ui| {
        /*
        let fsw = full slider_width
        let ssw = small slider_width
        let space = item_spacing.x
        let value = interact_size.x;

        fsw + space + value = ssw + space + value + space + ssw + space + value
        fsw + space + value = 2 * ssw + 3 * space + 2 * value
        fsw + space - value = 2 * ssw + 3 * space
        fsw - 2 * space - value = 2 * ssw
        ssw = fsw / 2 - space - value / 2
        */
        // let spacing = &ui.style().spacing;
        // let space = spacing.item_spacing.x;
        // let value_w = spacing.interact_size.x;
        // let full_slider_width = spacing.slider_width;
        // let small_slider_width = full_slider_width / 2.0 - space - value_w / 2.0;
        // ui.style_mut().spacing.slider_width = small_slider_width;

        ui.add(Slider::f32(&mut value.x, range.clone()).text("w"));
        ui.add(Slider::f32(&mut value.y, range.clone()).text("h"));
        ui.label(text);
    });
    ui.interact_hover(rect)
}

fn ui_color(ui: &mut Ui, srgba: &mut Srgba, text: &str) {
    ui.horizontal(|ui| {
        ui.color_edit_button_srgba(srgba);
        ui.label(text);
    });
}