hdrshot-gui 2.7.0

HDR image tool: desktop app for converting HDR screenshots and images
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! The app's look: colors taken from the icon (slate and a bright blue),
//! the Inter typeface, rounded widgets, and a few shared widgets built
//! from them.

use std::sync::Arc;

use egui::{
    Color32, CornerRadius, FontData, FontFamily, FontId, RichText, Sense, Stroke, StrokeKind,
    Theme, Vec2, Visuals,
};

use crate::icons;

/// Height of buttons, segmented choices and setting rows.
pub const ROW_HEIGHT: f32 = 26.0;

const RADIUS: u8 = 7;

struct Palette {
    panel: Color32,
    card: Color32,
    extreme: Color32,
    inactive: Color32,
    hovered: Color32,
    active: Color32,
    border: Color32,
    text: Color32,
    /// Secondary text, such as hints and setting labels.
    weak: Color32,
    accent: Color32,
    error: Color32,
}

// Slate with a blue cast and the teal of the icon's sky, as flat colors.
const DARK: Palette = Palette {
    panel: Color32::from_rgb(12, 17, 24),
    card: Color32::from_rgb(20, 28, 39),
    extreme: Color32::from_rgb(8, 12, 18),
    inactive: Color32::from_rgb(29, 40, 54),
    hovered: Color32::from_rgb(39, 53, 71),
    active: Color32::from_rgb(49, 66, 87),
    border: Color32::from_rgb(33, 45, 60),
    text: Color32::from_rgb(234, 239, 245),
    weak: Color32::from_rgb(140, 154, 171),
    accent: Color32::from_rgb(36, 136, 194),
    error: Color32::from_rgb(242, 104, 96),
};

const LIGHT: Palette = Palette {
    panel: Color32::from_rgb(243, 246, 250),
    card: Color32::from_rgb(255, 255, 255),
    extreme: Color32::from_rgb(255, 255, 255),
    inactive: Color32::from_rgb(229, 236, 243),
    hovered: Color32::from_rgb(216, 227, 238),
    active: Color32::from_rgb(201, 215, 230),
    border: Color32::from_rgb(213, 222, 232),
    text: Color32::from_rgb(17, 29, 44),
    weak: Color32::from_rgb(84, 99, 118),
    accent: Color32::from_rgb(22, 114, 168),
    error: Color32::from_rgb(196, 43, 28),
};

fn visuals(base: Visuals, p: &Palette) -> Visuals {
    let mut v = base;
    let radius = CornerRadius::same(RADIUS);
    v.panel_fill = p.panel;
    v.window_fill = p.card;
    v.faint_bg_color = p.card;
    v.extreme_bg_color = p.extreme;
    v.code_bg_color = p.inactive;
    v.window_stroke = Stroke::new(1.0, p.border);
    v.window_corner_radius = CornerRadius::same(12);
    v.menu_corner_radius = CornerRadius::same(10);
    v.weak_text_color = Some(p.weak);
    v.error_fg_color = p.error;
    v.hyperlink_color = p.accent;
    v.selection.bg_fill = p.accent;
    v.selection.stroke = Stroke::new(1.0, Color32::WHITE);
    v.slider_trailing_fill = true;
    v.collapsing_header_frame = false;
    v.indent_has_left_vline = false;

    let w = &mut v.widgets;
    w.noninteractive.bg_fill = p.card;
    w.noninteractive.weak_bg_fill = p.card;
    w.noninteractive.bg_stroke = Stroke::new(1.0, p.border);
    w.noninteractive.fg_stroke = Stroke::new(1.0, p.text);
    for (state, fill) in [
        (&mut w.inactive, p.inactive),
        (&mut w.hovered, p.hovered),
        (&mut w.active, p.active),
        (&mut w.open, p.hovered),
    ] {
        state.bg_fill = fill;
        state.weak_bg_fill = fill;
        state.bg_stroke = Stroke::NONE;
        state.fg_stroke.color = p.text;
        state.expansion = 0.0;
    }
    w.hovered.bg_stroke = Stroke::new(1.0, p.border);
    // Pressed and keyboard-focused widgets get an accent outline, so Tab
    // shows where it is.
    w.active.bg_stroke = Stroke::new(1.5, p.accent);
    for state in [
        &mut w.noninteractive,
        &mut w.inactive,
        &mut w.hovered,
        &mut w.active,
        &mut w.open,
    ] {
        state.corner_radius = radius;
    }
    v
}

/// The family of [`semibold`].
const SEMIBOLD: &str = "semibold";

/// Inter for all text, in a regular and a semibold weight. Both are
/// subsets with the Latin letters, and digits of equal width so counts
/// don't jiggle; other characters, such as Cyrillic or Chinese file names,
/// come from egui's fonts and the system's.
fn fonts() -> egui::FontDefinitions {
    let mut fonts = egui::FontDefinitions::default();
    for (name, data) in [
        (
            "Inter",
            &include_bytes!("../assets/fonts/Inter-Regular.ttf")[..],
        ),
        (
            "Inter SemiBold",
            &include_bytes!("../assets/fonts/Inter-SemiBold.ttf")[..],
        ),
    ] {
        fonts
            .font_data
            .insert(name.to_owned(), Arc::new(FontData::from_static(data)));
    }
    let proportional = fonts.families.entry(FontFamily::Proportional).or_default();
    let fallbacks = proportional.clone();
    proportional.insert(0, "Inter".to_owned());
    let mut semibold = vec!["Inter SemiBold".to_owned()];
    semibold.extend(fallbacks);
    fonts
        .families
        .insert(FontFamily::Name(SEMIBOLD.into()), semibold);
    fonts
}

/// Inter SemiBold, for titles, headings and the main button.
pub fn semibold(size: f32) -> FontId {
    FontId::new(size, FontFamily::Name(SEMIBOLD.into()))
}

/// Sets the fonts, colors and spacing for both the dark and light theme;
/// the system's choice decides which one shows.
pub fn apply(ctx: &egui::Context) {
    ctx.set_fonts(fonts());
    ctx.set_visuals_of(Theme::Dark, visuals(Visuals::dark(), &DARK));
    ctx.set_visuals_of(Theme::Light, visuals(Visuals::light(), &LIGHT));
    ctx.all_styles_mut(|style| {
        style.spacing.item_spacing = Vec2::new(8.0, 8.0);
        style.spacing.button_padding = Vec2::new(12.0, 4.0);
        style.spacing.interact_size.y = ROW_HEIGHT - 4.0;
        style.spacing.menu_margin = egui::Margin::same(8);
        style.interaction.selectable_labels = false;
    });
}

/// The accent color of the current theme.
pub fn accent(ui: &egui::Ui) -> Color32 {
    ui.visuals().selection.bg_fill
}

/// A rounded card holding a group of controls.
pub fn card<R>(ui: &mut egui::Ui, add_contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
    let visuals = ui.visuals();
    egui::Frame::new()
        .fill(visuals.faint_bg_color)
        .stroke(visuals.widgets.noninteractive.bg_stroke)
        .corner_radius(12.0)
        .inner_margin(egui::Margin::symmetric(14, 12))
        .show(ui, |ui| {
            ui.set_width(ui.available_width());
            add_contents(ui)
        })
        .inner
}

/// The label of a setting row, vertically centered on the row's controls.
pub fn row_label(ui: &mut egui::Ui, text: &str) {
    ui.allocate_ui_with_layout(
        Vec2::new(0.0, ROW_HEIGHT),
        egui::Layout::left_to_right(egui::Align::Center),
        |ui| ui.label(RichText::new(text).color(ui.visuals().weak_text_color())),
    );
}

/// A small "i" in a circle, drawn rather than taken from a font, that
/// explains something: hovering it shows `text` as a tooltip, and clicking
/// it shows `text` in a box that stays until the next click elsewhere.
///
/// The box keeps its open state itself rather than in egui's memory, which
/// holds one open popup at a time: opening it there closed the settings
/// menu the icon sits in. It also counts as that menu's open submenu, so
/// the click that closes the box leaves the menu open.
pub fn info_icon(ui: &mut egui::Ui, text: &str) -> egui::Response {
    let size = ui.spacing().interact_size.y.min(16.0);
    let (rect, response) = ui.allocate_exact_size(Vec2::splat(size), egui::Sense::click());
    let popup_id = response.id.with("info");
    let mut open = ui.data(|d| d.get_temp::<bool>(popup_id).unwrap_or(false));
    if response.clicked() {
        open = !open;
    }
    let shown = egui::Popup::from_response(&response)
        .id(popup_id)
        .open_bool(&mut open)
        .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
        .width(320.0)
        .show(|ui| ui.label(text))
        .is_some();
    if shown {
        egui::containers::menu::MenuState::from_id(ui.ctx(), ui.layer_id().id, |menu| {
            menu.open_item = Some(popup_id);
        });
    }
    ui.data_mut(|d| d.insert_temp(popup_id, open));
    focus_ring(ui, &response, size / 2.0);
    let color = if response.hovered() || open {
        ui.visuals().strong_text_color()
    } else {
        ui.visuals().weak_text_color()
    };
    let painter = ui.painter();
    painter.circle_stroke(
        rect.center(),
        size / 2.0 - 1.0,
        egui::Stroke::new(1.2, color),
    );
    let x = rect.center().x;
    painter.circle_filled(
        egui::pos2(x, rect.center().y - size * 0.2),
        size * 0.07,
        color,
    );
    painter.line_segment(
        [
            egui::pos2(x, rect.center().y - size * 0.05),
            egui::pos2(x, rect.center().y + size * 0.25),
        ],
        egui::Stroke::new(1.5, color),
    );
    let response = response.on_hover_cursor(egui::CursorIcon::PointingHand);
    if open {
        response
    } else {
        response.on_hover_text(text)
    }
}

/// The main action: filled with the accent color, white text.
pub fn primary_button(text: &str) -> egui::Button<'static> {
    egui::Button::new(
        RichText::new(text.to_owned())
            .font(semibold(14.5))
            .color(Color32::WHITE),
    )
    .min_size(Vec2::new(150.0, BIG_HEIGHT))
    .corner_radius(9.0)
}

/// A button as large as [`primary_button`], without the accent fill.
pub fn secondary_button(text: &str) -> egui::Button<'static> {
    egui::Button::new(RichText::new(text.to_owned()).font(semibold(14.5)))
        .min_size(Vec2::new(90.0, BIG_HEIGHT))
        .corner_radius(9.0)
}

/// Height of the main button and those beside it.
pub const BIG_HEIGHT: f32 = 34.0;

/// A wide button-shaped progress bar: the accent fills it from the left as
/// `fraction` grows, and `text` shows white over the filled part.
pub fn progress_bar(ui: &mut egui::Ui, width: f32, fraction: f32, text: &str) -> egui::Response {
    let (rect, response) = ui.allocate_exact_size(Vec2::new(width, BIG_HEIGHT), Sense::hover());
    let accent = accent(ui);
    let text_color = ui.visuals().strong_text_color();
    let painter = ui.painter();
    painter.rect_filled(rect, 8.0, accent.gamma_multiply(0.22));
    let split = rect.left() + rect.width() * fraction.clamp(0.0, 1.0);
    let filled = rect.with_max_x(split);
    if filled.width() > 0.5 {
        painter
            .with_clip_rect(filled)
            .rect_filled(rect, 8.0, accent);
    }
    let galley = painter.layout_no_wrap(text.to_owned(), semibold(14.0), Color32::PLACEHOLDER);
    let pos = rect.center() - galley.size() / 2.0;
    painter
        .with_clip_rect(filled)
        .galley_with_override_text_color(pos, galley.clone(), Color32::WHITE);
    painter
        .with_clip_rect(rect.with_min_x(split))
        .galley_with_override_text_color(pos, galley, text_color);
    response
}

/// The outline around a custom widget that has keyboard focus.
pub fn focus_ring(ui: &egui::Ui, response: &egui::Response, radius: f32) {
    if response.has_focus() {
        ui.painter().rect_stroke(
            response.rect.expand(2.0),
            radius + 2.0,
            Stroke::new(1.5, accent(ui)),
            StrokeKind::Outside,
        );
    }
}

/// Applies [`primary_button`]'s accent fill, which needs the theme.
pub fn primary(ui: &egui::Ui, button: egui::Button<'static>) -> egui::Button<'static> {
    button.fill(accent(ui))
}

/// Room on each side of a segment's label.
const SEGMENT_PAD: f32 = 12.0;

fn segment_galleys<'a>(
    ui: &egui::Ui,
    labels: impl Iterator<Item = &'a str>,
) -> Vec<std::sync::Arc<egui::Galley>> {
    let font = egui::FontId::proportional(13.5);
    labels
        .map(|label| {
            ui.painter()
                .layout_no_wrap(label.to_owned(), font.clone(), Color32::PLACEHOLDER)
        })
        .collect()
}

fn segments_width(galleys: &[std::sync::Arc<egui::Galley>]) -> f32 {
    galleys
        .iter()
        .map(|g| g.size().x + 2.0 * SEGMENT_PAD)
        .sum::<f32>()
        + 4.0
}

/// How wide [`segmented`] is with these labels.
pub fn segmented_width<'a>(ui: &egui::Ui, labels: impl IntoIterator<Item = &'a str>) -> f32 {
    segments_width(&segment_galleys(ui, labels.into_iter()))
}

/// One of several choices, shown as a row of joined segments with the
/// chosen one filled. Returns whether the choice changed.
pub fn segmented<T: PartialEq + Copy>(
    ui: &mut egui::Ui,
    value: &mut T,
    options: &[(T, &str, &str)],
) -> bool {
    let galleys = segment_galleys(ui, options.iter().map(|(_, label, _)| *label));
    let width = segments_width(&galleys);
    let pad = SEGMENT_PAD;
    let base_id = ui.next_auto_id();
    let (rect, _) = ui.allocate_exact_size(Vec2::new(width, ROW_HEIGHT), Sense::hover());
    let enabled = ui.is_enabled();
    let visuals = ui.visuals().clone();
    ui.painter().rect(
        rect,
        RADIUS,
        visuals.widgets.inactive.bg_fill,
        Stroke::NONE,
        StrokeKind::Inside,
    );
    let mut changed = false;
    let mut x = rect.left() + 2.0;
    for (index, ((option, _, hint), galley)) in options.iter().zip(galleys).enumerate() {
        let segment = egui::Rect::from_min_size(
            egui::pos2(x, rect.top() + 2.0),
            Vec2::new(galley.size().x + 2.0 * pad, ROW_HEIGHT - 4.0),
        );
        x = segment.right();
        let id = base_id.with(index);
        let response = ui
            .interact(segment, id, Sense::click())
            .on_hover_text(*hint);
        focus_ring(ui, &response, f32::from(RADIUS - 2));
        let selected = *value == *option;
        if response.clicked() && !selected {
            *value = *option;
            changed = true;
        }
        let t = fade(ui, id, response.hovered() && enabled);
        let fill = if selected {
            visuals.selection.bg_fill
        } else {
            visuals.widgets.hovered.bg_fill.gamma_multiply(t)
        };
        let mut text = if selected {
            Color32::WHITE
        } else {
            visuals.text_color()
        };
        if !enabled {
            text = text.gamma_multiply(0.45);
        }
        let painter = ui.painter();
        painter.rect_filled(segment, RADIUS - 2, fill);
        painter.galley_with_override_text_color(
            segment.center() - galley.size() / 2.0,
            galley,
            text,
        );
    }
    changed
}

/// A square button with a drawn icon (see [`icons`]), as the bundled
/// fonts have no icon glyphs.
pub fn icon_button(ui: &mut egui::Ui, paint: icons::Icon) -> egui::Response {
    let size = Vec2::splat(ROW_HEIGHT + 4.0);
    let (rect, response) = ui.allocate_exact_size(size, Sense::click());
    let t = fade(ui, response.id, response.hovered());
    let visuals = ui.visuals();
    let fill = mix(
        visuals.widgets.inactive.bg_fill,
        visuals.widgets.hovered.bg_fill,
        t,
    );
    let color = mix(visuals.text_color(), visuals.strong_text_color(), t);
    ui.painter().rect_filled(rect, RADIUS, fill);
    paint(ui.painter(), rect.shrink(8.0), color);
    focus_ring(ui, &response, f32::from(RADIUS));
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

/// The camera button: an [`icon_button`] filled with the warm yellow of
/// the icon's sun, so taking a screenshot is the action that stands out.
pub fn capture_button(ui: &mut egui::Ui, paint: icons::Icon) -> egui::Response {
    let size = Vec2::splat(ROW_HEIGHT + 4.0);
    let (rect, response) = ui.allocate_exact_size(size, Sense::click());
    let t = fade(ui, response.id, response.hovered());
    let (fill, hover) = if ui.visuals().dark_mode {
        (
            Color32::from_rgb(242, 204, 104),
            Color32::from_rgb(250, 220, 140),
        )
    } else {
        (
            Color32::from_rgb(240, 196, 76),
            Color32::from_rgb(228, 180, 52),
        )
    };
    let ink = Color32::from_rgb(46, 36, 10);
    ui.painter().rect_filled(rect, RADIUS, mix(fill, hover, t));
    paint(ui.painter(), rect.shrink(8.0), ink);
    focus_ring(ui, &response, f32::from(RADIUS));
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

/// A small icon button without a background until hovered, for the
/// actions on a file's row.
pub fn small_icon_button(ui: &mut egui::Ui, paint: icons::Icon) -> egui::Response {
    let (rect, response) = ui.allocate_exact_size(Vec2::splat(24.0), Sense::click());
    let t = fade(ui, response.id, response.hovered());
    let visuals = ui.visuals();
    if t > 0.0 {
        ui.painter()
            .rect_filled(rect, 6.0, visuals.widgets.hovered.bg_fill.gamma_multiply(t));
    }
    let color = mix(visuals.text_color(), visuals.strong_text_color(), t);
    paint(ui.painter(), rect.shrink(6.0), color);
    focus_ring(ui, &response, 6.0);
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

/// A button with a drawn icon before its text.
pub fn icon_text_button(ui: &mut egui::Ui, paint: icons::Icon, text: &str) -> egui::Response {
    let font = FontId::proportional(13.5);
    let galley = ui
        .painter()
        .layout_no_wrap(text.to_owned(), font, Color32::PLACEHOLDER);
    let icon = 12.0;
    let size = Vec2::new(galley.size().x + icon + 6.0 + 20.0, ROW_HEIGHT);
    let (rect, response) = ui.allocate_exact_size(size, Sense::click());
    let enabled = ui.is_enabled();
    let t = fade(ui, response.id, response.hovered() && enabled);
    let visuals = ui.visuals();
    let fill = mix(
        visuals.widgets.inactive.bg_fill,
        visuals.widgets.hovered.bg_fill,
        t,
    );
    let mut color = visuals.text_color();
    if !enabled {
        color = color.gamma_multiply(0.45);
    }
    let painter = ui.painter();
    painter.rect_filled(rect, RADIUS, fill);
    let icon_rect = egui::Rect::from_center_size(
        egui::pos2(rect.left() + 10.0 + icon / 2.0, rect.center().y),
        Vec2::splat(icon),
    );
    paint(painter, icon_rect, color);
    let text_pos = egui::pos2(
        icon_rect.right() + 6.0,
        rect.center().y - galley.size().y / 2.0,
    );
    painter.galley_with_override_text_color(text_pos, galley, color);
    focus_ring(ui, &response, f32::from(RADIUS));
    response
}

/// The heading above a card of settings.
pub fn section(ui: &mut egui::Ui, text: &str) {
    ui.add_space(6.0);
    ui.label(
        RichText::new(text)
            .font(semibold(12.5))
            .color(ui.visuals().weak_text_color()),
    );
}

/// How long hover effects take to fade in and out, in seconds.
const FADE: f32 = 0.12;

/// How far the widget `id` has faded towards its hovered look, from 0 to 1.
pub fn fade(ui: &egui::Ui, id: egui::Id, hovered: bool) -> f32 {
    ui.ctx()
        .animate_bool_with_time(id.with("hover fade"), hovered, FADE)
}

/// The color `t` of the way from `a` to `b`.
pub fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
    a.lerp_to_gamma(b, t.clamp(0.0, 1.0))
}

/// A raised surface, such as a file's row or a screenshot's tile: `fill`
/// with a hairline edge in `edge`. In the dark theme a faint light line
/// along the top edge lifts it off the window; in the light theme a small
/// shadow does.
pub fn surface(
    painter: &egui::Painter,
    rect: egui::Rect,
    radius: f32,
    fill: Color32,
    edge: Color32,
    dark: bool,
) {
    if !dark {
        let shadow = egui::Shadow {
            offset: [0, 1],
            blur: 3,
            spread: 0,
            color: Color32::from_black_alpha(14),
        };
        painter.add(shadow.as_shape(rect, radius));
    }
    painter.rect(
        rect,
        radius,
        fill,
        Stroke::new(1.0, edge),
        StrokeKind::Inside,
    );
    if dark {
        let y = rect.top() + 1.5;
        painter.line_segment(
            [
                egui::pos2(rect.left() + radius, y),
                egui::pos2(rect.right() - radius, y),
            ],
            Stroke::new(1.0, Color32::from_white_alpha(7)),
        );
    }
}

/// One part of a [`strip`]: a small label over its value.
pub struct StripItem {
    pub label: &'static str,
    pub value: String,
    /// Shows the value in the accent color, for settings that need a look.
    pub accented: bool,
    /// Its menu is open.
    pub open: bool,
}

/// Height of a [`strip`].
pub const STRIP_HEIGHT: f32 = 42.0;

/// A row of settings in one rounded box, split by thin lines, each part a
/// small label over its value; each part is a button. The parts share the
/// width by how long their values are, and values too long for a narrow
/// window end in "…".
pub fn strip(ui: &mut egui::Ui, items: &[StripItem]) -> Vec<egui::Response> {
    let pad = 10.0;
    let label_font = FontId::proportional(10.5);
    let value_font = FontId::proportional(12.5);
    let enabled = ui.is_enabled();
    let width = ui.available_width();
    let (rect, _) = ui.allocate_exact_size(Vec2::new(width, STRIP_HEIGHT), Sense::hover());
    let base_id = ui.scope_id().with("strip");
    let natural: Vec<f32> = items
        .iter()
        .map(|item| {
            let measure = |text: &str, font: &FontId| {
                ui.painter()
                    .layout_no_wrap(text.to_owned(), font.clone(), Color32::PLACEHOLDER)
                    .size()
                    .x
            };
            measure(&item.value, &value_font).max(measure(item.label, &label_font)) + 2.0 * pad
        })
        .collect();
    let total: f32 = natural.iter().sum();
    // Extra room goes to every part alike; missing room comes off the
    // widest ones first.
    let widths: Vec<f32> = if total <= width {
        let extra = (width - total) / items.len().max(1) as f32;
        natural.iter().map(|w| w + extra).collect()
    } else {
        let mut cap = natural.iter().cloned().fold(0.0_f32, f32::max);
        let fits = |cap: f32| natural.iter().map(|w| w.min(cap)).sum::<f32>() <= width;
        while cap > 40.0 && !fits(cap) {
            cap -= 1.0;
        }
        natural.iter().map(|w| w.min(cap)).collect()
    };
    let visuals = ui.visuals().clone();
    let edge = visuals.widgets.noninteractive.bg_stroke.color;
    let radius = 10.0;
    surface(
        ui.painter(),
        rect,
        radius,
        visuals.faint_bg_color,
        edge,
        visuals.dark_mode,
    );
    let mut responses = Vec::new();
    let mut x = rect.left();
    let count = items.len();
    for (index, (item, w)) in items.iter().zip(widths).enumerate() {
        let part = egui::Rect::from_min_size(egui::pos2(x, rect.top()), Vec2::new(w, STRIP_HEIGHT));
        x += w;
        let response = ui.interact(part, base_id.with(index), Sense::click());
        let t = fade(
            ui,
            response.id,
            (response.hovered() && enabled) || item.open,
        );
        let corner = |first: bool, last: bool| egui::CornerRadius {
            nw: if first { radius as u8 } else { 0 },
            sw: if first { radius as u8 } else { 0 },
            ne: if last { radius as u8 } else { 0 },
            se: if last { radius as u8 } else { 0 },
        };
        let painter = ui.painter();
        if t > 0.0 {
            painter.rect_filled(
                part.shrink(1.0),
                corner(index == 0, index + 1 == count),
                visuals.widgets.hovered.bg_fill.gamma_multiply(t),
            );
        }
        if index > 0 {
            painter.line_segment(
                [
                    egui::pos2(part.left(), part.top() + 9.0),
                    egui::pos2(part.left(), part.bottom() - 9.0),
                ],
                Stroke::new(1.0, edge),
            );
        }
        let mut weak = visuals.weak_text_color();
        let mut value = if item.accented {
            visuals.selection.bg_fill
        } else {
            visuals.text_color()
        };
        if !enabled {
            weak = weak.gamma_multiply(0.45);
            value = value.gamma_multiply(0.45);
        }
        let max = (w - 2.0 * pad).max(10.0);
        let elided = |text: &str, font: &FontId, color: Color32| {
            let mut job =
                egui::text::LayoutJob::simple_singleline(text.to_owned(), font.clone(), color);
            job.wrap = egui::text::TextWrapping::truncate_at_width(max);
            painter.layout_job(job)
        };
        let label = elided(item.label, &label_font, weak);
        let shown = elided(&item.value, &value_font, value);
        let top = part.center().y - (label.size().y + 1.0 + shown.size().y) / 2.0;
        painter.galley(egui::pos2(part.left() + pad, top), label.clone(), weak);
        painter.galley(
            egui::pos2(part.left() + pad, top + label.size().y + 1.0),
            shown,
            value,
        );
        focus_ring(ui, &response, 6.0);
        responses.push(response.on_hover_cursor(egui::CursorIcon::PointingHand));
    }
    responses
}