GORBIE 0.13.2

GORBIE! Is a minimalist notebook library for Rust.
Documentation
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
use eframe::egui::{
    self, pos2, vec2, Color32, Key, NumExt as _, Rect, Response, Sense, Stroke, TextStyle, Ui,
    Widget, WidgetInfo, WidgetText, WidgetType,
};

use crate::themes::{
    GorbieButtonStyle, GorbieChoiceToggleStyle, GorbieRadioStyle,
};

/// A momentary or toggle button with optional LED indicator.
///
/// - Momentary (default): click returns `response.clicked()`
/// - Toggle: pass `on(&mut bool)` to toggle state on click
/// - Disabled = flush (no shadow), not grayed out
/// - Always fills available container width
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
pub struct Button<'a> {
    text: WidgetText,
    on: Option<&'a mut bool>,
    selected: bool,
    fill: Option<Color32>,
    light: Option<Color32>,
    latched: bool,
    min_height: Option<f32>,
    gorbie_style: Option<GorbieButtonStyle>,
}

impl<'a> Button<'a> {
    /// Create a new momentary button with the given label text.
    pub fn new(text: impl Into<WidgetText>) -> Self {
        Self {
            text: text.into(),
            on: None,
            selected: false,
            fill: None,
            light: None,
            latched: false,
            min_height: None,
            gorbie_style: None,
        }
    }

    /// Make this a toggle button that flips `on` when clicked.
    pub fn on(mut self, on: &'a mut bool) -> Self {
        self.on = Some(on);
        self
    }

    /// Mark the button as visually selected (accent-colored outline).
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    /// Override the button's background fill color.
    pub fn fill(mut self, fill: Color32) -> Self {
        self.fill = Some(fill);
        self
    }

    /// Set the LED indicator color. When set, a thin LED strip is drawn at the top of the button.
    pub fn light(mut self, color: Color32) -> Self {
        self.light = Some(color);
        self
    }

    /// Hold the button visually pressed (e.g. while an action is in progress).
    pub fn latched(mut self, latched: bool) -> Self {
        self.latched = latched;
        self
    }

    /// Set the height in grid modules (1 module = 12px).
    /// Default is 2 modules. The shadow is included in the total.
    pub fn modules(mut self, n: u32) -> Self {
        self.min_height = Some(n as f32 * crate::card_ctx::GRID_ROW_MODULE);
        self
    }
}

impl Widget for Button<'_> {
    fn ui(self, ui: &mut Ui) -> Response {
        let Self {
            text,
            on,
            selected,
            fill,
            light,
            latched,
            min_height,
            gorbie_style,
        } = self;

        let enabled = ui.is_enabled();
        let gstyle = gorbie_style.unwrap_or_else(|| GorbieButtonStyle::from(ui.style().as_ref()));
        let shadow_offset = gstyle.shadow_offset;
        let shadow_inset = vec2(shadow_offset.x.max(0.0), shadow_offset.y.max(0.0));
        let padding = ui.spacing().button_padding;

        let label_text = text.text().to_string();
        let max_text_width =
            (ui.available_width() - padding.x * 2.0 - shadow_inset.x).at_least(0.0);
        let galley = text.into_galley(
            ui,
            Some(egui::TextWrapMode::Truncate),
            max_text_width,
            TextStyle::Button,
        );

        let mut body_size = galley.size() + padding * 2.0;
        let target_h = min_height
            .unwrap_or(2.0 * crate::card_ctx::GRID_ROW_MODULE)
            .max(ui.spacing().interact_size.y);
        body_size.y = body_size.y.at_least(target_h - shadow_inset.y);
        body_size.x = body_size.x.max(ui.available_width() - shadow_inset.x);
        let desired_size = body_size + shadow_inset;

        let (outer_rect, mut response) = ui.allocate_exact_size(desired_size, Sense::click());

        // Toggle behavior.
        let mut toggled_on = false;
        if let Some(on) = on {
            if response.clicked() && enabled {
                *on = !*on;
                response.mark_changed();
            }
            toggled_on = *on;
        }

        response.widget_info(move || {
            WidgetInfo::labeled(WidgetType::Button, enabled, label_text.as_str())
        });

        if !ui.is_rect_visible(outer_rect) {
            return response;
        }

        let outline = gstyle.outline;
        let accent = gstyle.accent;
        let shadow_color = gstyle.shadow;
        let base_fill = fill.unwrap_or(gstyle.fill);

        let keyboard_down = response.has_focus()
            && ui.input(|input| input.key_down(Key::Space) || input.key_down(Key::Enter));
        let is_down = enabled
            && (response.is_pointer_button_down_on() || keyboard_down || latched || toggled_on);
        let prepress =
            enabled && !is_down && (response.hovered() || response.has_focus());

        let stroke_color = if enabled && selected { accent } else { outline };

        let body_rect_up =
            Rect::from_min_max(outer_rect.min, outer_rect.max - shadow_inset).intersect(outer_rect);

        // Disabled = flush (no shadow, no offset).
        let (body_rect, show_shadow) = if !enabled {
            (body_rect_up, false)
        } else if is_down {
            (body_rect_up.translate(shadow_offset), false)
        } else if prepress {
            (body_rect_up.translate(shadow_offset * 0.5), true)
        } else {
            (body_rect_up, true)
        };

        let rounding = gstyle.rounding;
        let painter = ui.painter();

        if show_shadow {
            let shadow_off = if prepress {
                shadow_offset * 0.5
            } else {
                shadow_offset
            };
            painter.rect_filled(body_rect.translate(shadow_off), rounding, shadow_color);
        }

        painter.rect_filled(body_rect, rounding, base_fill);
        painter.rect_stroke(
            body_rect,
            rounding,
            Stroke::new(1.0, stroke_color),
            egui::StrokeKind::Inside,
        );

        let text_color = crate::themes::ral(9011);
        let text_pos = pos2(
            body_rect.center().x - galley.size().x / 2.0,
            body_rect.center().y - galley.size().y / 2.0,
        );
        painter.galley(text_pos, galley, text_color);

        // LED indicator.
        if light.is_some() || toggled_on {
            let led_height = 4.0;
            let led_inset_x = 2.0;
            let led_inset_y = 2.0;
            let led_rect = Rect::from_min_max(
                pos2(body_rect.left() + led_inset_x, body_rect.top() + led_inset_y),
                pos2(
                    body_rect.right() - led_inset_x,
                    (body_rect.top() + led_inset_y + led_height).min(body_rect.bottom()),
                ),
            );
            if led_rect.is_positive() {
                let led_fill = light.unwrap_or_else(|| {
                    if toggled_on {
                        crate::themes::button_light_on()
                    } else {
                        gstyle.shadow // dim
                    }
                });
                painter.rect_filled(led_rect, 1.0, led_fill);
            }
        }

        response
    }
}

impl crate::themes::Styled for Button<'_> {
    type Style = GorbieButtonStyle;

    fn set_style(&mut self, style: Option<Self::Style>) {
        self.gorbie_style = style;
    }
}


/// A radio button that selects one value from a group.
///
/// Click sets `value` to the given `option`. The selected state is shown with
/// an accent outline and a lit indicator dot.
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
pub struct RadioButton<'a, T> {
    value: &'a mut T,
    option: T,
    text: WidgetText,
    min_height: Option<f32>,
    fill: Option<Color32>,
    light: Option<Color32>,
    gorbie_style: Option<GorbieRadioStyle>,
}

impl<'a, T> RadioButton<'a, T> {
    /// Create a radio button that sets `value` to `option` when clicked.
    pub fn new(value: &'a mut T, option: T, text: impl Into<WidgetText>) -> Self {
        Self {
            value,
            option,
            text: text.into(),
            min_height: None,
            fill: None,
            light: None,
            gorbie_style: None,
        }
    }

    /// Set the height in grid modules (1 module = 12px). Default is 2.
    pub fn modules(mut self, n: u32) -> Self {
        self.min_height = Some(n as f32 * crate::card_ctx::GRID_ROW_MODULE);
        self
    }

    /// Override the indicator's background fill color.
    pub fn fill(mut self, fill: Color32) -> Self {
        self.fill = Some(fill);
        self
    }

    /// Override the indicator dot color when selected.
    pub fn light(mut self, color: Color32) -> Self {
        self.light = Some(color);
        self
    }
}

impl<T> Widget for RadioButton<'_, T>
where
    T: Clone + PartialEq,
{
    fn ui(self, ui: &mut Ui) -> Response {
        let Self {
            value,
            option,
            text,
            min_height,
            fill,
            light,
            gorbie_style,
        } = self;

        let selected = *value == option;
        let enabled = ui.is_enabled();
        let gstyle = gorbie_style.unwrap_or_else(|| GorbieRadioStyle::from(ui.style().as_ref()));
        let shadow_offset = gstyle.shadow_offset;
        let shadow_inset = vec2(shadow_offset.x.max(0.0), shadow_offset.y.max(0.0));

        let padding = ui.spacing().button_padding;
        let indicator_size = (ui.spacing().interact_size.y - 6.0).at_least(14.0);
        let gap = (padding.x * 0.8).at_least(6.0);
        let label_text = text.text().to_string();
        let max_text_width =
            (ui.available_width() - padding.x * 2.0 - indicator_size - gap - shadow_inset.x)
                .at_least(0.0);
        let galley = text.into_galley(
            ui,
            Some(egui::TextWrapMode::Truncate),
            max_text_width,
            TextStyle::Button,
        );

        let content_height = galley.size().y.max(indicator_size);
        let min_body_height = {
            let target_h = min_height
                .unwrap_or(2.0 * crate::card_ctx::GRID_ROW_MODULE)
                .max(ui.spacing().interact_size.y);
            target_h - shadow_inset.y
        };
        let body_height = (content_height + padding.y * 2.0).at_least(min_body_height);
        let mut body_width = padding.x + indicator_size + gap + galley.size().x + padding.x;
        body_width = body_width.max(ui.available_width() - shadow_inset.x);
        let desired_size = vec2(body_width, body_height) + shadow_inset;

        let (outer_rect, mut response) = ui.allocate_exact_size(desired_size, Sense::click());
        if response.clicked() && enabled && !selected {
            *value = option;
            response.mark_changed();
        }

        response.widget_info(move || {
            WidgetInfo::labeled(WidgetType::RadioButton, enabled, label_text.as_str())
        });

        if !ui.is_rect_visible(outer_rect) {
            return response;
        }

        let visuals = ui.visuals();
        let outline = gstyle.outline;
        let shadow_color = gstyle.shadow;

        let base_fill = fill.unwrap_or(gstyle.fill);
        let disabled_fill = crate::themes::blend(base_fill, visuals.window_fill, 0.65);

        let keyboard_down = response.has_focus()
            && ui.input(|input| input.key_down(Key::Space) || input.key_down(Key::Enter));
        let is_down = enabled && (response.is_pointer_button_down_on() || keyboard_down);
        let prepress = enabled && !is_down && (response.hovered() || response.has_focus());

        let fill = if enabled { base_fill } else { disabled_fill };
        let stroke_color = if enabled && selected {
            gstyle.accent
        } else {
            outline
        };

        let body_rect_up =
            Rect::from_min_max(outer_rect.min, outer_rect.max - shadow_inset).intersect(outer_rect);
        let press_offset = if is_down {
            shadow_offset
        } else if prepress || selected {
            shadow_offset * 0.5
        } else {
            vec2(0.0, 0.0)
        };

        let indicator_rect_up = Rect::from_min_size(
            pos2(
                body_rect_up.left() + padding.x,
                body_rect_up.center().y - indicator_size / 2.0,
            ),
            vec2(indicator_size, indicator_size),
        );
        let indicator_rect = if press_offset != vec2(0.0, 0.0) {
            indicator_rect_up.translate(press_offset)
        } else {
            indicator_rect_up
        };
        let shadow_drop = vec2(
            (shadow_offset.x - press_offset.x).max(0.0),
            (shadow_offset.y - press_offset.y).max(0.0),
        );

        let painter = ui.painter();
        if enabled && shadow_drop != vec2(0.0, 0.0) {
            painter.rect_filled(
                indicator_rect.translate(shadow_drop),
                gstyle.rounding,
                shadow_color,
            );
        }

        painter.rect_filled(indicator_rect, gstyle.rounding, fill);
        painter.rect_stroke(
            indicator_rect,
            gstyle.rounding,
            Stroke::new(1.0, stroke_color),
            egui::StrokeKind::Inside,
        );

        let on_color = light.unwrap_or(gstyle.indicator_on);
        let off_color =
            crate::themes::blend(gstyle.rail_bg, fill, gstyle.indicator_off_towards_fill);
        let mut dot_color = if selected { on_color } else { off_color };
        if !enabled {
            dot_color = crate::themes::blend(dot_color, visuals.window_fill, 0.6);
        }
        let dot_radius = (indicator_rect.height() * 0.22).at_least(2.0);
        painter.circle_filled(indicator_rect.center(), dot_radius, dot_color);

        let text_color = if enabled {
            crate::themes::ral(9011)
        } else {
            crate::themes::blend(crate::themes::ral(9011), visuals.window_fill, 0.55)
        };
        let text_pos = pos2(
            indicator_rect_up.right() + gap,
            body_rect_up.center().y - galley.size().y / 2.0,
        );
        painter.galley(text_pos, galley, text_color);

        response
    }
}

impl<T> crate::themes::Styled for RadioButton<'_, T> {
    type Style = GorbieRadioStyle;

    fn set_style(&mut self, style: Option<Self::Style>) {
        self.gorbie_style = style;
    }
}

struct ChoiceToggleOption<T> {
    value: T,
    text: WidgetText,
}

/// A segmented selector where each option is a labelled button in a shared rail.
///
/// Only one option is active at a time. The active segment is shown pressed with
/// a lit LED strip; inactive segments sit raised.
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
pub struct ChoiceToggle<'a, T> {
    value: &'a mut T,
    options: Vec<ChoiceToggleOption<T>>,
    min_height: Option<f32>,
    fill: Option<Color32>,
    light: Option<Color32>,
    gorbie_style: Option<GorbieChoiceToggleStyle>,
}

impl<'a, T> ChoiceToggle<'a, T> {
    /// Create an empty choice toggle bound to `value`. Add options with [`Self::choice`].
    pub fn new(value: &'a mut T) -> Self {
        Self {
            value,
            options: Vec::new(),
            min_height: None,
            fill: None,
            light: None,
            gorbie_style: None,
        }
    }

    /// Append a selectable option with the given value and label.
    pub fn choice(mut self, value: T, text: impl Into<WidgetText>) -> Self {
        self.options.push(ChoiceToggleOption {
            value,
            text: text.into(),
        });
        self
    }

    /// Set the height in grid modules (1 module = 12px). Default is 2.
    pub fn modules(mut self, n: u32) -> Self {
        self.min_height = Some(n as f32 * crate::card_ctx::GRID_ROW_MODULE);
        self
    }

    /// Override the segment background fill color.
    pub fn fill(mut self, fill: Color32) -> Self {
        self.fill = Some(fill);
        self
    }

    /// Override the LED indicator color for the active segment.
    pub fn light(mut self, color: Color32) -> Self {
        self.light = Some(color);
        self
    }
}

impl<'a> ChoiceToggle<'a, bool> {
    /// A two-position selector that renders both options explicitly.
    ///
    /// `false` corresponds to the left/off option, and `true` corresponds to the right/on option.
    pub fn binary(
        value: &'a mut bool,
        off_text: impl Into<WidgetText>,
        on_text: impl Into<WidgetText>,
    ) -> Self {
        ChoiceToggle::new(value)
            .choice(false, off_text)
            .choice(true, on_text)
    }
}

impl<T> Widget for ChoiceToggle<'_, T>
where
    T: Clone + PartialEq,
{
    fn ui(self, ui: &mut Ui) -> Response {
        let Self {
            value,
            options,
            min_height,
            fill,
            light,
            gorbie_style,
        } = self;

        if options.is_empty() {
            let (_rect, response) = ui.allocate_exact_size(vec2(0.0, 0.0), Sense::hover());
            return response;
        }
        let enabled = ui.is_enabled();
        let gstyle =
            gorbie_style.unwrap_or_else(|| GorbieChoiceToggleStyle::from(ui.style().as_ref()));
        let shadow_offset = gstyle.shadow_offset;
        let shadow_inset = vec2(shadow_offset.x.max(0.0), shadow_offset.y.max(0.0));
        let padding = ui.spacing().button_padding;
        let text_style = TextStyle::Button;

        let wrap_mode = Some(egui::TextWrapMode::Truncate);
        let max_text_width = ui.available_width().at_least(0.0);

        struct RenderedChoice<T> {
            value: T,
            galley: std::sync::Arc<egui::Galley>,
        }

        let mut label_text = String::new();
        let mut choices: Vec<RenderedChoice<T>> = Vec::with_capacity(options.len());

        for (idx, option) in options.into_iter().enumerate() {
            if idx > 0 {
                label_text.push('/');
            }
            label_text.push_str(option.text.text().as_ref());

            let galley = option
                .text
                .into_galley(ui, wrap_mode, max_text_width, text_style.clone());
            choices.push(RenderedChoice {
                value: option.value,
                galley,
            });
        }

        let segment_count = choices.len();
        let segment_gap = gstyle.segment_gap;

        let mut segment_size = vec2(0.0, 0.0);
        for choice in &choices {
            segment_size = segment_size.max(choice.galley.size());
        }
        segment_size += padding * 2.0;

        let target_h = min_height
            .unwrap_or(2.0 * crate::card_ctx::GRID_ROW_MODULE)
            .max(ui.spacing().interact_size.y);
        segment_size.y = segment_size.y.at_least(target_h - shadow_inset.y);

        let body_width = segment_size.x * segment_count as f32
            + segment_gap * (segment_count.saturating_sub(1) as f32);
        let body_size = vec2(body_width, segment_size.y);
        let desired_size = body_size + shadow_inset;
        let (outer_rect, outer_response) = ui.allocate_exact_size(desired_size, Sense::hover());

        let mut response = outer_response;
        response.widget_info(move || {
            WidgetInfo::labeled(WidgetType::Button, enabled, label_text.as_str())
        });

        if !ui.is_rect_visible(outer_rect) {
            return response;
        }

        let visuals = ui.visuals();
        let outline = gstyle.outline;
        let accent = gstyle.accent;
        let shadow_color = gstyle.shadow;

        let base_fill = fill.unwrap_or(gstyle.fill);
        let disabled_fill = crate::themes::blend(base_fill, visuals.window_fill, 0.65);
        let disabled_slot_fill = crate::themes::blend(gstyle.rail_bg, visuals.window_fill, 0.65);

        let slot_rect =
            Rect::from_min_max(outer_rect.min, outer_rect.max - shadow_inset).intersect(outer_rect);
        let slot_fill = if enabled {
            gstyle.rail_bg
        } else {
            disabled_slot_fill
        };

        let mut segment_slots = Vec::with_capacity(segment_count);
        let mut segment_responses = Vec::with_capacity(segment_count);

        for idx in 0..segment_count {
            let x0 = slot_rect.left() + idx as f32 * (segment_size.x + segment_gap);
            let x1 = (x0 + segment_size.x).min(slot_rect.right());

            let segment_slot =
                Rect::from_min_max(pos2(x0, slot_rect.top()), pos2(x1, slot_rect.bottom()));
            segment_slots.push(segment_slot);

            let id = ui.make_persistent_id((response.id, "choice-toggle", idx));
            segment_responses.push(ui.interact(segment_slot, id, Sense::click()));
        }

        let pointer_pressed = enabled && ui.input(|i| i.pointer.any_pressed());
        let keyboard_down =
            enabled && ui.input(|input| input.key_down(Key::Space) || input.key_down(Key::Enter));

        let mut changed = false;
        if pointer_pressed {
            for (idx, segment_response) in segment_responses.iter().enumerate() {
                if segment_response.is_pointer_button_down_on() {
                    let next_value = &choices[idx].value;
                    if next_value != &*value {
                        *value = next_value.clone();
                        changed = true;
                    }
                    break;
                }
            }
        }

        // Fallback for non-pointer activation (e.g. keyboard).
        if enabled && !changed {
            for (idx, segment_response) in segment_responses.iter().enumerate() {
                if segment_response.clicked() {
                    let next_value = &choices[idx].value;
                    if next_value != &*value {
                        *value = next_value.clone();
                        changed = true;
                    }
                    break;
                }
            }
        }

        let slot_rounding = gstyle.slot_rounding;
        let segment_rounding = gstyle.segment_rounding;
        let painter = ui.painter();

        painter.rect_filled(slot_rect, slot_rounding, slot_fill);
        painter.rect_stroke(
            slot_rect,
            slot_rounding,
            Stroke::new(1.0, outline),
            egui::StrokeKind::Inside,
        );

        let segment_margin = shadow_offset.x.max(shadow_offset.y).max(2.0);

        #[derive(Clone, Copy)]
        struct MaskStroke {
            left: bool,
            right: bool,
        }

        let draw_segment = |face_up: Rect,
                            rounding: egui::CornerRadius,
                            mask_stroke: MaskStroke,
                            galley: std::sync::Arc<egui::Galley>,
                            hovered: bool,
                            is_down: bool,
                            is_active: bool| {
            let painter = ui.painter();
            let fill = if enabled { base_fill } else { disabled_fill };
            let is_pressed = is_down || is_active;
            let prepress = enabled && !is_pressed && hovered;

            let pressed_offset = vec2(0.0, shadow_offset.y.max(0.0));
            let face_rect = if is_pressed {
                face_up.translate(pressed_offset)
            } else if prepress {
                face_up.translate(pressed_offset * 0.5)
            } else {
                face_up
            };

            if enabled && !is_pressed {
                let shadow_offset = if prepress {
                    pressed_offset * 0.5
                } else {
                    shadow_offset
                };
                painter.rect_filled(face_rect.translate(shadow_offset), rounding, shadow_color);
            }

            let stroke_color = if enabled && is_active {
                accent
            } else {
                outline
            };

            painter.rect_filled(face_rect, rounding, fill);
            painter.rect_stroke(
                face_rect,
                rounding,
                Stroke::new(1.0, stroke_color),
                egui::StrokeKind::Inside,
            );
            let stroke_width = 1.0;

            if mask_stroke.left {
                let mask_rect = Rect::from_min_max(
                    pos2(face_rect.left(), face_rect.top()),
                    pos2(
                        (face_rect.left() + stroke_width).min(face_rect.right()),
                        face_rect.bottom(),
                    ),
                );
                if mask_rect.is_positive() {
                    painter.rect_filled(mask_rect, 0, fill);
                }
            }
            if mask_stroke.right {
                let mask_rect = Rect::from_min_max(
                    pos2(
                        (face_rect.right() - stroke_width).max(face_rect.left()),
                        face_rect.top(),
                    ),
                    pos2(face_rect.right(), face_rect.bottom()),
                );
                if mask_rect.is_positive() {
                    painter.rect_filled(mask_rect, 0, fill);
                }
            }

            let text_color = if enabled {
                crate::themes::ral(9011)
            } else {
                crate::themes::blend(crate::themes::ral(9011), fill, 0.55)
            };

            let text_pos = pos2(
                face_rect.center().x - galley.size().x / 2.0,
                face_rect.center().y - galley.size().y / 2.0,
            );
            painter.galley(text_pos, galley, text_color);

            let led_height = 4.0;
            let led_inset_x = 2.0;
            let led_inset_y = 2.0;
            let led_rect = Rect::from_min_max(
                pos2(
                    face_rect.left() + led_inset_x,
                    face_rect.top() + led_inset_y,
                ),
                pos2(
                    face_rect.right() - led_inset_x,
                    (face_rect.top() + led_inset_y + led_height).min(face_rect.bottom()),
                ),
            );
            if led_rect.is_positive() {
                let on_color = light.unwrap_or(gstyle.led_on);
                let off_color =
                    crate::themes::blend(gstyle.rail_bg, fill, gstyle.led_off_towards_fill);

                let mut led_fill = if is_active { on_color } else { off_color };
                if !enabled {
                    led_fill = crate::themes::blend(led_fill, ui.visuals().window_fill, 0.6);
                }
                painter.rect_filled(led_rect, 1, led_fill);
            }
        };

        let active_index = choices.iter().position(|choice| choice.value == *value);

        let mut draw_order: Vec<usize> = if shadow_offset.x >= 0.0 {
            (0..segment_count).collect()
        } else {
            (0..segment_count).rev().collect()
        };
        if let Some(active_index) = active_index {
            if let Some(pos) = draw_order.iter().position(|&idx| idx == active_index) {
                let active = draw_order.remove(pos);
                draw_order.push(active);
            }
        }

        for idx in draw_order {
            let slot = segment_slots[idx];
            let left_inset = if idx == 0 { segment_margin } else { 0.0 };
            let right_inset = if idx + 1 == segment_count {
                segment_margin
            } else {
                0.0
            };

            let face_up = Rect::from_min_max(
                pos2(slot.left() + left_inset, slot.top() + segment_margin),
                pos2(
                    (slot.right() - right_inset).max(slot.left() + left_inset),
                    (slot.bottom() - segment_margin).max(slot.top() + segment_margin),
                ),
            );

            let rounding = egui::CornerRadius {
                nw: if idx == 0 { segment_rounding } else { 0 },
                ne: if idx + 1 == segment_count {
                    segment_rounding
                } else {
                    0
                },
                sw: if idx == 0 { segment_rounding } else { 0 },
                se: if idx + 1 == segment_count {
                    segment_rounding
                } else {
                    0
                },
            };

            let hovered = segment_responses[idx].hovered() || segment_responses[idx].has_focus();
            let is_down = enabled
                && (segment_responses[idx].is_pointer_button_down_on()
                    || (segment_responses[idx].has_focus() && keyboard_down));
            let is_active = active_index == Some(idx);

            let mask_stroke = MaskStroke {
                left: idx > 0,
                right: idx + 1 < segment_count,
            };

            draw_segment(
                face_up,
                rounding,
                mask_stroke,
                choices[idx].galley.clone(),
                hovered,
                is_down,
                is_active,
            );
        }

        for segment_response in segment_responses {
            response |= segment_response;
        }

        if changed {
            response.mark_changed();
        }

        response
    }
}

impl<T> crate::themes::Styled for ChoiceToggle<'_, T> {
    type Style = GorbieChoiceToggleStyle;

    fn set_style(&mut self, style: Option<Self::Style>) {
        self.gorbie_style = style;
    }
}