cranpose-liquid 0.1.164

Liquid UI — cranpose's first-party glass component library (iOS-26-style materials, spring motion)
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
use std::{cell::RefCell, rc::Rc};

use cranpose_macros::composable;
use cranpose_ui::{
    Brush, Color, CornerRadii, Modifier, PointerInputScope, Rect, SemanticsWidgetRole, Size,
    text::{FontWeight, SpanStyle, TextStyle},
    widgets::{
        Box, BoxSpec, BoxWithConstraints, BoxWithConstraintsScope, ContentScale, Image, Painter,
        Row, RowSpec, Text,
    },
};
use cranpose_ui_layout::{Alignment, VerticalAlignment};

use crate::{
    material::{
        Glass, GlassDynamics, GlassFaceResponse, GlassKeyFill, GlassMorph, GlassShadow,
        LiquidModifierExt, neutral_surface_tint,
    },
    motion::LiquidMotion,
    theme::{LiquidTypography, liquid_colors, liquid_typography},
    widgets::content_scope::ScopeContent,
};

/// Visual treatment for a tab icon.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LiquidTabIconStyle {
    #[default]
    Plain,
    AppBadge,
}

/// Artwork displayed in a tab's shared icon frame.
#[derive(Clone, Debug, PartialEq)]
pub enum LiquidTabIcon {
    /// Vector path data in a 24×24 view box.
    Vector(&'static str),
    /// Template artwork drawn at its supplied logical size and tinted with the tab's color.
    Painter {
        /// Image or custom painter supplying the template's alpha mask.
        painter: Painter,
        /// Artwork dimensions in logical points before the tab's optical correction.
        size: Size,
    },
}

/// One tab's artwork, label, and icon treatment.
#[derive(Clone, Debug, PartialEq)]
pub struct LiquidTab {
    /// Vector or template artwork shown above the label.
    pub icon: LiquidTabIcon,
    pub label: &'static str,
    pub icon_style: LiquidTabIconStyle,
    /// Optical correction for symbols whose path bounds do not fill the
    /// shared icon frame uniformly.
    pub icon_scale: f32,
    /// Optical displacement of the artwork inside the shared icon frame, in points.
    pub icon_offset: (f32, f32),
}

impl LiquidTab {
    pub fn new(icon: &'static str, label: &'static str) -> Self {
        Self {
            icon: LiquidTabIcon::Vector(icon),
            label,
            icon_style: LiquidTabIconStyle::Plain,
            icon_scale: 1.0,
            icon_offset: (0.0, 0.0),
        }
    }

    /// A tab using template artwork and its logical size.
    pub fn from_painter(painter: Painter, size: Size, label: &'static str) -> Self {
        Self {
            icon: LiquidTabIcon::Painter { painter, size },
            label,
            icon_style: LiquidTabIconStyle::Plain,
            icon_scale: 1.0,
            icon_offset: (0.0, 0.0),
        }
    }

    pub fn app_badge(icon: &'static str, label: &'static str) -> Self {
        Self {
            icon: LiquidTabIcon::Vector(icon),
            label,
            icon_style: LiquidTabIconStyle::AppBadge,
            icon_scale: 1.0,
            icon_offset: (0.0, 0.0),
        }
    }

    pub fn with_icon_scale(mut self, scale: f32) -> Self {
        self.icon_scale = normalize_icon_scale(scale);
        self
    }

    /// Moves the artwork without changing its cell or hit bounds; nonfinite offsets become zero.
    pub fn with_icon_offset(mut self, x: f32, y: f32) -> Self {
        self.icon_offset = (
            if x.is_finite() { x } else { 0.0 },
            if y.is_finite() { y } else { 0.0 },
        );
        self
    }
}

fn normalize_icon_scale(scale: f32) -> f32 {
    if scale.is_finite() {
        scale.clamp(0.5, 1.5)
    } else {
        1.0
    }
}

fn tab_base_content_color(colors: crate::theme::LiquidColors) -> Color {
    colors.label
}

fn tab_selection_content_color(colors: crate::theme::LiquidColors) -> Color {
    colors.accent
}

const BAR_HEIGHT: f32 = 62.0;
const BLOB_HEIGHT: f32 = 54.0;
const BLOB_MARGIN: f32 = 4.0;
const FLIGHT_LENS_INFLATION: f32 = 16.0;
const TAB_LENS_OVERLAP: f32 = 7.0;
const TAB_WIDTH: f32 = 78.0;
const TAB_ICON_SIZE: f32 = 32.0;
const TAB_LABEL_SIZE: f32 = 10.0;
const TAP_SLOP: f32 = 6.0;
const ACCESSORY_GAP: f32 = 10.0;

/// Layout parameters for a [`LiquidTabBar`].
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LiquidTabBarSpec {
    max_tab_width: f32,
}

impl LiquidTabBarSpec {
    /// Sets the maximum cell allocation; nonfinite values use the default and finite values are at least one point.
    pub fn new(max_tab_width: f32) -> Self {
        Self {
            max_tab_width: if max_tab_width.is_finite() {
                max_tab_width.max(1.0)
            } else {
                TAB_WIDTH
            },
        }
    }

    fn cell_width(self, available_width: f32, count: usize) -> f32 {
        let constrained = available_width - BLOB_MARGIN * 2.0;
        if constrained.is_finite() && constrained > 1.0 {
            (constrained / count as f32).min(self.max_tab_width)
        } else {
            self.max_tab_width
        }
    }
}

impl Default for LiquidTabBarSpec {
    fn default() -> Self {
        Self::new(TAB_WIDTH)
    }
}

fn tab_flight_lens_material(foreground: cranpose_ui_graphics::Color, activity: f32) -> Glass {
    let activity = activity.clamp(0.0, 1.0);
    Glass::lens()
        .face_lighting(false)
        .key_fill(GlassKeyFill {
            height_dp: 1.0,
            curvature: 0.7 + 0.1 * activity,
            angle_radians: std::f32::consts::FRAC_PI_4,
            saturation: 1.5,
            luma_gain: 0.4367,
            offset: 1.125,
            scale_with_surface: true,
        })
        .no_clip()
        .face_response(GlassFaceResponse {
            gain: 1.0 - 0.03 * activity,
            start_dp: 1.0 / 3.0,
            end_dp: 2.0 / 3.0,
            illumination: 0.0,
        })
        .tint(neutral_surface_tint(
            foreground,
            0.07333333 * (1.0 - activity),
            0.1440678 * (1.0 - activity),
        ))
        .blur_radius(0.0)
        .backdrop_blur(4.0, 1.0 - activity)
        .edge_refraction(9.0 * activity)
        .refraction_depth_dp(36.0)
        .refraction_curve(0.25)
        .optical_zoom(1.0)
        .meniscus_absorption(0.0)
        .inner_shadow(GlassShadow::new(
            Color::BLACK.with_alpha(0.12 * activity),
            3.0,
            7.0,
            0.0,
        ))
        .fold_depth(0.0)
        .edge_spectrum(crate::material::GlassSpectrum {
            angle_radians: std::f32::consts::FRAC_PI_2
                - std::f32::consts::PI * 7.0 / 12.0 * activity,
            step_dp: (4.068 / (112.24138 / 36.0)) / (std::f32::consts::PI / 12.0).cos()
                * (-5.0 + 8.684211 * activity)
                / 3.6842105,
            vertical_scale: 0.8265,
            opacity_near: activity,
            opacity_far: 1.0 - activity,
            fade_depth_dp: 14.0 * activity,
            extent_dp: 38.88889 * activity,
        })
        .dispersion(0.0)
        .lift(0.0)
        .highlight(activity)
        .rim_reflection(activity)
        .shadow_style(GlassShadow::new(
            cranpose_ui_graphics::Color::BLACK.with_alpha(0.1 * activity),
            8.0,
            7.0,
            0.0,
        ))
}

fn tab_bar_surface_material(foreground: cranpose_ui_graphics::Color) -> Glass {
    Glass::regular()
        .face_lighting(false)
        .key_fill(GlassKeyFill {
            height_dp: 1.0,
            curvature: 0.7,
            angle_radians: std::f32::consts::FRAC_PI_4,
            saturation: 1.5,
            luma_gain: 0.1,
            offset: 0.9,
            scale_with_surface: false,
        })
        .face_response(GlassFaceResponse {
            gain: 0.97,
            start_dp: 1.0 / 3.0,
            end_dp: 2.0 / 3.0,
            illumination: 0.0,
        })
        .tint(Color::TRANSPARENT)
        .blur_radius(6.0)
        .surface_refraction(31.0)
        .refraction_depth_dp(15.5)
        .transmission_refraction(1.0)
        .saturation(1.0)
        .lift(0.0)
        .contrast(1.0)
        .highlight(1.0)
        .adaptive_frost(foreground, 0.0)
        .adaptive_tone(foreground)
        .shadow_style(GlassShadow::new(
            Color::BLACK.with_alpha(0.162),
            32.0,
            8.0,
            1.5,
        ))
}

fn tab_flight_tint_multiplier(activity: f32) -> f32 {
    1.0 - 0.25 * activity.clamp(0.0, 1.0)
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct TabGeometry {
    width: f32,
    cell_width: f32,
    pitch: f32,
}

impl TabGeometry {
    fn new(allocation: f32, count: usize) -> Self {
        let width = allocation * count.max(1) as f32;
        let cell_width = tab_lens_rest_width(allocation).min(width);
        let pitch = if count > 1 {
            (width - cell_width) / (count - 1) as f32
        } else {
            cell_width
        };
        Self {
            width,
            cell_width,
            pitch,
        }
    }

    fn drag_left(self, pointer_x: f32, count: usize, has_accessory: bool) -> f32 {
        let last_tab = self.pitch * count.saturating_sub(1) as f32;
        let min = if has_accessory {
            -self.pitch * 0.2
        } else {
            0.0
        };
        let max = if has_accessory {
            last_tab + self.pitch * 0.55
        } else {
            last_tab
        };
        (pointer_x - self.cell_width * 0.5).clamp(min, max)
    }

    fn optical_pointer_x(self, pointer_x: f32, press: f32, travel: f32) -> f32 {
        let transform = tab_bar_transform(self.width + 2.0 * BLOB_MARGIN, press, travel);
        let center = self.width * 0.5;
        pointer_x + (pointer_x - center) * (transform.scale_x - 1.0) + transform.translation_x
    }

    fn lens_surface_position(self, position: f32, press: f32, travel: f32) -> f32 {
        let transform = tab_bar_transform(self.width + 2.0 * BLOB_MARGIN, press, travel);
        (position + self.cell_width * 0.5 - self.width * 0.5) * transform.scale_x
    }
}

/// The settled cell origin for a selected index and the distance between cell centers.
pub fn tab_lens_resting_left(selected: usize, tab_pitch: f32, count: usize) -> f32 {
    tab_pitch * selected.min(count.saturating_sub(1)) as f32
}

/// The resting selection width for one destination's allocation in the content strip.
/// Cells overlap; their pitch is the remaining strip width divided by the number of gaps.
pub fn tab_lens_rest_width(tab_allocation: f32) -> f32 {
    tab_allocation + TAB_LENS_OVERLAP
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct AppBadgeGeometry {
    size: Size,
    corner_radius: f32,
    stripe: Rect,
    glyph: Rect,
}

fn app_badge_geometry(optical_scale: f32) -> AppBadgeGeometry {
    let scale = normalize_icon_scale(optical_scale);
    AppBadgeGeometry {
        size: Size::new(20.0 * scale, 32.0 * scale),
        corner_radius: 5.0 * scale,
        stripe: Rect {
            x: 7.0 * scale,
            y: 4.5 * scale,
            width: 6.0 * scale,
            height: 1.5 * scale,
        },
        glyph: Rect {
            x: 3.0 * scale,
            y: 10.0 * scale,
            width: 14.0 * scale,
            height: 14.0 * scale,
        },
    }
}

#[composable]
fn TabIcon(icon: LiquidTabIcon, style: LiquidTabIconStyle, color: Color, optical_scale: f32) {
    const FRAME_HEIGHT: f32 = 32.0;
    Box(
        Modifier::empty().size(Size::new(TAB_ICON_SIZE, FRAME_HEIGHT)),
        BoxSpec::default().content_alignment(Alignment::CENTER),
        move || match style {
            LiquidTabIconStyle::Plain => {
                TabGlyph(icon.clone(), TAB_ICON_SIZE * optical_scale, color);
            }
            LiquidTabIconStyle::AppBadge => {
                let icon = icon.clone();
                let geometry = app_badge_geometry(optical_scale);
                Box(
                    Modifier::empty()
                        .size(geometry.size)
                        .draw_behind(move |scope| {
                            scope.draw_round_rect(
                                Brush::solid(color),
                                CornerRadii::uniform(geometry.corner_radius),
                            );
                            scope.draw_rect_at(geometry.stripe, Brush::solid(Color::WHITE));
                        }),
                    BoxSpec::default(),
                    move || {
                        let icon = icon.clone();
                        Box(
                            Modifier::empty()
                                .offset(geometry.glyph.x, geometry.glyph.y)
                                .size(Size::new(geometry.glyph.width, geometry.glyph.height)),
                            BoxSpec::default(),
                            move || TabGlyph(icon.clone(), geometry.glyph.width, Color::WHITE),
                        );
                    },
                );
            }
        },
    );
}

#[composable]
fn TabGlyph(icon: LiquidTabIcon, size: f32, color: Color) {
    match icon {
        LiquidTabIcon::Vector(path) => crate::icons::Icon(path, None, size, color),
        LiquidTabIcon::Painter {
            painter,
            size: intrinsic,
        } => {
            let scale = size / TAB_ICON_SIZE;
            Image(
                painter,
                None,
                Modifier::empty()
                    .size(Size::new(intrinsic.width * scale, intrinsic.height * scale)),
                Alignment::CENTER,
                ContentScale::FillBounds,
                1.0,
                Some(cranpose_ui_graphics::ColorFilter::tint(color)),
            );
        }
    }
}

fn tab_ink_selection(
    geometry: TabGeometry,
    position: f32,
    activity: f32,
    strain: Size,
    color: Color,
) -> super::vibrancy::InkSelection {
    let (width, height) = tab_lens_base_size(geometry.cell_width, activity);
    let base = Size::new(width, height);
    let deformed = tab_lens_deformed_size(base, strain);
    let width = deformed.width;
    let height = deformed.height;
    super::vibrancy::InkSelection {
        bounds: Rect {
            x: position + geometry.cell_width * 0.5 - width * 0.5,
            y: BLOB_HEIGHT * 0.5 - height * 0.5,
            width,
            height,
        },
        color,
        content_zoom: 1.0 + 0.16 * activity.clamp(0.0, 1.0),
        activity: activity.clamp(0.0, 1.0),
        optical_scale: 1.0,
        projection: Size::new(deformed.width / base.width, deformed.height / base.height),
    }
}

#[derive(Clone, Copy, PartialEq)]
struct TabCellsSpec {
    base_color: Color,
    selection: super::vibrancy::InkSelection,
    selected: usize,
    committed_selection: usize,
}

#[composable]
fn TabCells(
    modifier: Modifier,
    tabs: Rc<Vec<LiquidTab>>,
    typography: LiquidTypography,
    geometry: TabGeometry,
    spec: TabCellsSpec,
    transform: cranpose_ui_graphics::GraphicsLayer,
    on_select: impl Fn(usize) + 'static,
) {
    let on_select: Rc<dyn Fn(usize)> = Rc::new(on_select);
    let size = Size::new(geometry.width + BLOB_MARGIN * 2.0, BAR_HEIGHT);
    let mut selection = spec.selection;
    let center = selection.bounds.x + selection.bounds.width * 0.5 + BLOB_MARGIN;
    selection.bounds.width *= transform.scale_x;
    selection.bounds.height *= transform.scale_y;
    selection.optical_scale = transform.scale_x.min(transform.scale_y);
    selection.bounds.x = (center - size.width * 0.5) * transform.scale_x
        + size.width * 0.5
        + transform.translation_x
        - selection.bounds.width * 0.5;
    selection.bounds.y = (BAR_HEIGHT - selection.bounds.height) * 0.5;
    let grid = super::vibrancy::InkGrid {
        first_center: (
            (BLOB_MARGIN + geometry.cell_width * 0.5 - size.width * 0.5) * transform.scale_x
                + size.width * 0.5
                + transform.translation_x,
            BAR_HEIGHT * 0.5,
        ),
        pitch: geometry.pitch * transform.scale_x,
        count: tabs.len(),
    };
    super::vibrancy::VibrantContent(
        modifier,
        size,
        spec.base_color,
        selection,
        grid,
        move || {
            let tabs = Rc::clone(&tabs);
            let typography = typography.clone();
            let on_select = Rc::clone(&on_select);
            Box(
                Modifier::empty()
                    .size(size)
                    .selectable_group()
                    .role(SemanticsWidgetRole::TabBar)
                    .graphics_layer_value(transform.clone()),
                BoxSpec::default(),
                move || {
                    for (index, tab) in tabs.iter().enumerate() {
                        let color = spec.base_color;
                        let label_for_semantics = tab.label;
                        let icon_offset = tab.icon_offset;
                        let on_select = Rc::clone(&on_select);
                        let cell = Modifier::empty()
                            .offset(BLOB_MARGIN + index as f32 * geometry.pitch, BLOB_MARGIN)
                            .size(Size::new(geometry.cell_width, BLOB_HEIGHT))
                            .semantics(super::selection::selection_semantics(
                                label_for_semantics.to_string(),
                                SemanticsWidgetRole::Tab,
                                index,
                                spec.committed_selection,
                                on_select,
                            ))
                            .focusable();
                        let icon = tab.icon.clone();
                        let icon_style = tab.icon_style;
                        let icon_scale = tab.icon_scale;
                        let label = tab.label;
                        let label_style = TextStyle {
                            span_style: SpanStyle {
                                color: Some(color),
                                font_size: cranpose_ui::text::TextUnit::Sp(TAB_LABEL_SIZE),
                                font_weight: Some(if spec.selected == index {
                                    FontWeight::SEMI_BOLD
                                } else {
                                    FontWeight::MEDIUM
                                }),
                                ..typography.caption1.span_style.clone()
                            },
                            paragraph_style: cranpose_ui::text::ParagraphStyle {
                                line_height: cranpose_ui::text::TextUnit::Sp(12.0),
                                ..typography.caption1.paragraph_style.clone()
                            },
                        };
                        Box(cell, BoxSpec::default(), move || {
                            let label_style = label_style.clone();
                            let icon = icon.clone();
                            Box(
                                Modifier::empty()
                                    .offset(
                                        (geometry.cell_width - TAB_ICON_SIZE) * 0.5 + icon_offset.0,
                                        3.0 + icon_offset.1,
                                    )
                                    .size(Size::new(TAB_ICON_SIZE, TAB_ICON_SIZE)),
                                BoxSpec::default(),
                                move || TabIcon(icon.clone(), icon_style, color, icon_scale),
                            );
                            Box(
                                Modifier::empty()
                                    .offset(0.0, 35.0)
                                    .size(Size::new(geometry.cell_width, 12.0)),
                                BoxSpec::default().content_alignment(Alignment::CENTER),
                                move || {
                                    Text(label, Modifier::empty(), label_style.clone());
                                },
                            );
                        });
                    }
                },
            );
        },
    );
}

fn tab_lens_node_top(node_height: f32) -> f32 {
    (BAR_HEIGHT - node_height) * 0.5
}

#[derive(Clone, Copy)]
struct TabBarTouch {
    position: cranpose_core::MutableState<(f32, f32)>,
    origin: cranpose_core::MutableState<Option<f32>>,
    travel: cranpose_core::MutableState<f32>,
}

impl TabBarTouch {
    fn pressed(self, down: bool) {
        if down {
            self.origin.set(None);
            self.travel.set(0.0);
        }
    }

    fn update(self, x: f32, y: f32, span: f32) {
        self.position.set((x + BLOB_MARGIN, y + BLOB_MARGIN));
        if let Some(origin) = self.origin.get() {
            self.travel
                .set(((x - origin) / span.max(1.0)).clamp(-1.0, 1.0));
        } else {
            self.origin.set(Some(x));
        }
    }
}

#[composable]
fn remember_tab_bar_touch() -> TabBarTouch {
    TabBarTouch {
        position: cranpose_core::remember(|| cranpose_core::mutableStateOf((0.0f32, 0.0f32)))
            .with(|state| *state),
        origin: cranpose_core::remember(|| cranpose_core::mutableStateOf(None::<f32>))
            .with(|state| *state),
        travel: cranpose_core::remember(|| cranpose_core::mutableStateOf(0.0f32))
            .with(|state| *state),
    }
}

fn tab_bar_transform(width: f32, press: f32, travel: f32) -> cranpose_ui_graphics::GraphicsLayer {
    let press = press.max(0.0);
    let travel = travel.clamp(-1.0, 1.0);
    let rise = 14.14 / width.max(1.0) * press;
    let strain = 2.63 / width.max(1.0) * travel.abs() * press;
    cranpose_ui_graphics::GraphicsLayer {
        scale_x: 1.0 + rise + strain,
        scale_y: 1.0 + rise - strain,
        translation_x: 2.756 * travel * press,
        ..Default::default()
    }
}

fn tab_bar_accessory_gap(has_accessory: bool) -> f32 {
    if has_accessory { ACCESSORY_GAP } else { 0.0 }
}

fn accessory_surfaces_touch(edge_gap: f32) -> bool {
    edge_gap <= 0.0
}

fn tab_lens_base_size(cell_width: f32, activity: f32) -> (f32, f32) {
    let inflation = FLIGHT_LENS_INFLATION * activity.clamp(0.0, 1.0);
    (cell_width + inflation, BLOB_HEIGHT + inflation)
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct TabFlightGeometry {
    center: (f32, f32),
    base_size: Size,
    strain: Size,
    lens_position: f32,
    lens_activity: f32,
    resting_tint: Color,
    accessory_center: Option<(f32, f32)>,
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct TabFlightNode {
    origin: (f32, f32),
    size: Size,
}

fn tab_lens_deformed_size(base: Size, strain: Size) -> Size {
    Size::new(
        base.width * (1.0 + strain.width.clamp(-0.35, 0.35)),
        base.height * (1.0 + strain.height.clamp(-0.35, 0.35)),
    )
}

fn tab_flight_dynamics(geometry: TabFlightGeometry, node: TabFlightNode) -> GlassDynamics {
    let activity = geometry.lens_activity.clamp(0.0, 1.0);
    let size = geometry.base_size;
    let deformed = tab_lens_deformed_size(size, geometry.strain);
    let radius = size.width.min(size.height) * 0.5;
    let glue = 20.0;
    let shapes = geometry
        .accessory_center
        .filter(|(x, _)| {
            let edge_gap = (*x - geometry.center.0).abs() - size.width * 0.5 - BAR_HEIGHT * 0.5;
            accessory_surfaces_touch(edge_gap)
        })
        .map(|(x, y)| {
            vec![(
                x - node.origin.0,
                y - node.origin.1,
                BAR_HEIGHT,
                BAR_HEIGHT,
                -1.0,
            )]
        })
        .unwrap_or_default();
    GlassDynamics {
        edge_return_depth_dp: Some(7.0 * activity),
        optical_projection: Some((deformed.width / size.width, deformed.height / size.height)),
        morph: Some(GlassMorph {
            node_size: (node.size.width, node.size.height),
            primary: (
                geometry.center.0 - node.origin.0,
                geometry.center.1 - node.origin.1,
                size.width,
                size.height,
                radius,
            ),
            shapes,
            glue,
            wobble_amplitude: 0.0,
            wobble_phase: geometry.lens_position * 0.045,
            bulge_amplitude: 0.0,
            bulge_direction: 0.0,
            ellipse_blend: 0.0,
            capsule_smoothing_dp: ((size.width - size.height).abs() * 0.5).min(radius * 0.6)
                * activity,
            deformation: None,
            zoom_anchor: (0.0, 0.0),
        }),
        activity: Some(1.0),
        resting_tint: Some(geometry.resting_tint),
        tint_alpha_multiplier: Some(tab_flight_tint_multiplier(geometry.lens_activity)),
        ..Default::default()
    }
}

/// The scope a tab bar's destinations are declared in.
///
/// Each call adds one destination, in the order it is made, which is also the
/// order the indices passed to `on_select` count in.
pub struct LiquidTabBarScope {
    tabs: ScopeContent<LiquidTab>,
}

impl LiquidTabBarScope {
    /// A destination showing `icon` above `label`.
    pub fn tab(&self, icon: &'static str, label: &'static str) {
        self.push(LiquidTab::new(icon, label));
    }

    /// A destination whose icon is drawn as an application badge.
    pub fn app_badge(&self, icon: &'static str, label: &'static str) {
        self.push(LiquidTab::app_badge(icon, label));
    }

    /// A destination built out, for the optical corrections a particular
    /// symbol needs — see [`LiquidTab`].
    pub fn push(&self, tab: LiquidTab) {
        self.tabs.push(tab);
    }
}

fn collect_tabs(content: impl FnOnce(&LiquidTabBarScope)) -> Vec<LiquidTab> {
    ScopeContent::collect(|tabs| LiquidTabBarScope { tabs }, content)
}

/// A unified floating glass tab bar with every destination inside one pill.
///
/// ```rust,ignore
/// LiquidTabBar(Modifier::empty(), LiquidTabBarSpec::default(), selected, on_select, |scope| {
///     scope.tab(icons::DOCUMENT, "Today");
///     scope.tab(icons::SEARCH, "Search");
/// });
/// ```
#[composable]
pub fn LiquidTabBar(
    modifier: Modifier,
    spec: LiquidTabBarSpec,
    selected: usize,
    on_select: impl Fn(usize) + 'static,
    content: impl FnOnce(&LiquidTabBarScope),
) {
    LiquidTabBarLayout(
        modifier,
        spec,
        collect_tabs(content),
        selected,
        on_select,
        false,
        || {},
    );
}

/// A floating glass tab bar with a detached accessory to its right.
#[composable]
pub fn LiquidTabBarWithAccessory(
    modifier: Modifier,
    spec: LiquidTabBarSpec,
    selected: usize,
    on_select: impl Fn(usize) + 'static,
    content: impl FnOnce(&LiquidTabBarScope),
    accessory: impl FnMut() + 'static,
) {
    LiquidTabBarLayout(
        modifier,
        spec,
        collect_tabs(content),
        selected,
        on_select,
        true,
        accessory,
    );
}

#[composable]
fn LiquidTabBarLayout(
    modifier: Modifier,
    spec: LiquidTabBarSpec,
    tabs: Vec<LiquidTab>,
    selected: usize,
    on_select: impl Fn(usize) + 'static,
    has_accessory: bool,
    accessory: impl FnMut() + 'static,
) {
    let colors = liquid_colors();
    let typography = liquid_typography();
    let tint_amount = crate::theme::liquid_glass_tint_amount();
    let count = tabs.len().max(1);
    let selected = selected.min(count - 1);
    let on_select: Rc<dyn Fn(usize)> = Rc::new(on_select);
    let tabs = Rc::new(tabs);
    let accessory = Rc::new(RefCell::new(accessory));

    Row(
        modifier,
        RowSpec::default().vertical_alignment(VerticalAlignment::CenterVertically),
        move || {
            let tabs = Rc::clone(&tabs);
            let typography = typography.clone();
            let on_select = Rc::clone(&on_select);
            let accessory = Rc::clone(&accessory);

            let lens_x_outer = cranpose_core::remember(move || {
                cranpose_core::mutableStateOf((
                    0.0f32,
                    0.0f32,
                    0.0f32,
                    Size::new(0.0, 0.0),
                    selected,
                ))
            })
            .with(|state| *state);
            let bar_touch = remember_tab_bar_touch();
            let contact_motion = super::tab_motion::remember_tab_contact_motion();
            let bar_press = contact_motion.bar_state();
            let glow = contact_motion.glow_state();
            let local_glow_factor = contact_motion.local_glow_factor_state();
            let travel = cranpose_animation::animateFloatAsState(
                bar_touch.travel.get(),
                cranpose_animation::spring(0.85, 650.0),
                "tabbar-travel",
            );
            let bar_lift = Modifier::empty().graphics_layer(move || {
                let width = lens_x_outer.get().2 * count as f32 + 2.0 * BLOB_MARGIN;
                tab_bar_transform(width, bar_press.get(), travel.get())
            });
            Box(
                Modifier::empty().height(BAR_HEIGHT),
                BoxSpec::default(),
                move || {
                    let tabs = Rc::clone(&tabs);
                    let typography = typography.clone();
                    let on_select = Rc::clone(&on_select);
                    let semantic_selection = Rc::clone(&on_select);
                    let contact_motion = Rc::clone(&contact_motion);
                    let pill = bar_lift.clone().height(BAR_HEIGHT);
                    Box(pill, BoxSpec::default(), move || {
                        let on_select = Rc::clone(&on_select);
                        let contact_motion = Rc::clone(&contact_motion);
                        BoxWithConstraints(Modifier::empty(), move |scope| {
                            let on_select = Rc::clone(&on_select);
                            let tab_width = spec.cell_width(scope.constraints().max_width, count);

                            let geometry = TabGeometry::new(tab_width, count);
                            Box(
                                Modifier::empty()
                                    .glass_effect(
                                        tab_bar_surface_material(colors.label)
                                            .tint_amount(tint_amount),
                                    )
                                    .size(Size::new(
                                        geometry.width + BLOB_MARGIN * 2.0,
                                        BAR_HEIGHT,
                                    )),
                                BoxSpec::default(),
                                || {},
                            );
                            let lens_pressed =
                                cranpose_core::remember(|| cranpose_core::mutableStateOf(false))
                                    .with(|state| *state);
                            let resting_lens_x =
                                tab_lens_resting_left(selected, geometry.pitch, count);
                            let lens_axis = crate::motion::remember_liquid_follow_axis(
                                resting_lens_x,
                                cranpose_animation::spring(0.85, 650.0),
                            );
                            if !lens_pressed.get() {
                                lens_axis.settle_to(resting_lens_x, LiquidMotion::glide());
                            }
                            let lens_x = lens_axis.value();
                            let lens_shape = super::tab_motion::remember_tab_lens_shape();
                            let strain = lens_shape.sample(geometry.lens_surface_position(
                                lens_x,
                                bar_press.get(),
                                travel.get(),
                            ));
                            let lens_activity_anim = contact_motion.lens_state();
                            let lens_activity = lens_activity_anim.get().clamp(0.0, 1.0);
                            let visual_index = crate::motion::liquid_visual_index(
                                selected,
                                lens_x,
                                geometry.pitch,
                                count,
                                crate::motion::liquid_axis_owns_visual_selection(
                                    lens_pressed.get(),
                                    lens_x,
                                    resting_lens_x,
                                    geometry.pitch,
                                ),
                            );
                            if bar_touch.travel.get().abs() > 0.001 && visual_index != selected {
                                contact_motion.crossed_tab();
                            }
                            let row_width = geometry.width;
                            let gesture = Modifier::empty()
                                .offset(BLOB_MARGIN, BLOB_MARGIN)
                                .size(Size::new(row_width, BLOB_HEIGHT))
                                .pointer_input(selected, {
                                    let on_select = Rc::clone(&on_select);
                                    let lens_axis = Rc::clone(&lens_axis);
                                    let contact_motion = Rc::clone(&contact_motion);
                                    move |scope: PointerInputScope| {
                                        let on_select = Rc::clone(&on_select);
                                        let lens_axis = Rc::clone(&lens_axis);
                                        let contact_motion = Rc::clone(&contact_motion);
                                        crate::motion::liquid_lens_gesture(
                                            scope,
                                            crate::motion::LiquidLensGesture {
                                                axis: lens_axis,
                                                cell_width: geometry.pitch,
                                                cell_offset: (geometry.cell_width - geometry.pitch)
                                                    * 0.5,
                                                count,
                                                tap_slop: TAP_SLOP,
                                                drag_left: Rc::new(move |x| {
                                                    geometry.drag_left(
                                                        geometry.optical_pointer_x(
                                                            x,
                                                            bar_press.get(),
                                                            travel.get(),
                                                        ),
                                                        count,
                                                        has_accessory,
                                                    )
                                                }),
                                                rest_left: Rc::new(move |index| {
                                                    tab_lens_resting_left(
                                                        index,
                                                        geometry.pitch,
                                                        count,
                                                    )
                                                }),
                                                selected,
                                                on_pressed: Rc::new(move |down, time| {
                                                    lens_pressed.set(down);
                                                    contact_motion.pressed(down, time);
                                                    bar_touch.pressed(down);
                                                }),
                                                on_touch: Rc::new(move |x, y| {
                                                    bar_touch.update(
                                                        geometry.optical_pointer_x(
                                                            x,
                                                            bar_press.get(),
                                                            travel.get(),
                                                        ),
                                                        y,
                                                        geometry.pitch
                                                            * count.saturating_sub(1) as f32,
                                                    );
                                                }),
                                                on_select,
                                            },
                                        )
                                    }
                                });
                            Box(gesture, BoxSpec::default(), || {});

                            let published =
                                (lens_x, lens_activity, tab_width, strain, visual_index);
                            if lens_x_outer.get() != published {
                                lens_x_outer.set(published);
                            }
                        });
                    });

                    let (lens_px, lens_activity, lens_tab_w, strain, visual_index) =
                        lens_x_outer.get();
                    let cells = TabGeometry::new(lens_tab_w, count);
                    let (lens_w, lens_h) = tab_lens_base_size(cells.cell_width, 1.0);
                    let deformation_headroom =
                        crate::dynamics::STRETCH_MAX.max(1.0 / crate::dynamics::STRETCH_MIN);
                    let node_w = lens_w * deformation_headroom + crate::dynamics::BULGE_MAX + 20.0;
                    let node_h = lens_h * deformation_headroom + crate::dynamics::BULGE_MAX + 16.0;
                    let pill_w = cells.width + 2.0 * BLOB_MARGIN;
                    let transform = tab_bar_transform(pill_w, bar_press.get(), travel.get());
                    let local_center_x = BLOB_MARGIN + lens_px + cells.cell_width * 0.5;
                    let lens_center_x = local_center_x;
                    let node_x = lens_center_x - node_w * 0.5;
                    let node_top = tab_lens_node_top(node_h);
                    let (base_w, base_h) = tab_lens_base_size(cells.cell_width, lens_activity);
                    let geometry = TabFlightGeometry {
                        center: (lens_center_x, BAR_HEIGHT * 0.5),
                        base_size: Size::new(base_w, base_h),
                        strain,
                        lens_position: lens_px,
                        lens_activity,
                        resting_tint: colors.fill,
                        accessory_center: has_accessory.then_some((
                            pill_w + tab_bar_accessory_gap(true) + BAR_HEIGHT * 0.5,
                            BAR_HEIGHT * 0.5,
                        )),
                    };
                    let lens_node = TabFlightNode {
                        origin: (node_x, node_top),
                        size: Size::new(node_w, node_h),
                    };

                    let lens_geometry = geometry;
                    let mut lens_transform = transform.clone();
                    lens_transform.translation_x +=
                        (local_center_x - pill_w * 0.5) * (transform.scale_x - 1.0);
                    let layers = Rc::new(crate::material::cached_glass_content_layers(
                        tab_flight_lens_material(colors.label, lens_activity).resolve(&colors),
                    ));
                    let lens_node_modifier = Modifier::empty()
                        .required_size(lens_node.size)
                        .offset(node_x, node_top)
                        .graphics_layer_value(lens_transform);
                    let background_layers = Rc::clone(&layers);
                    let lens = lens_node_modifier.clone().graphics_layer(move || {
                        background_layers(
                            cranpose_ui::current_density(),
                            tab_flight_dynamics(lens_geometry, lens_node),
                        )[0]
                        .clone()
                    });
                    super::tab_lighting::TabLighting(
                        Size::new(pill_w, BAR_HEIGHT),
                        transform.clone(),
                        bar_touch.position,
                        glow,
                        local_glow_factor,
                    );
                    Box(lens, BoxSpec::default(), || {});
                    TabCells(
                        Modifier::empty(),
                        Rc::clone(&tabs),
                        typography,
                        cells,
                        TabCellsSpec {
                            base_color: tab_base_content_color(colors),
                            selection: tab_ink_selection(
                                cells,
                                lens_px,
                                lens_activity,
                                strain,
                                tab_selection_content_color(colors),
                            ),
                            selected: visual_index,
                            committed_selection: selected,
                        },
                        transform,
                        move |index| semantic_selection(index),
                    );
                    Box(
                        lens_node_modifier.graphics_layer(move || {
                            layers(
                                cranpose_ui::current_density(),
                                tab_flight_dynamics(lens_geometry, lens_node),
                            )[1]
                            .clone()
                        }),
                        BoxSpec::default(),
                        || {},
                    );
                },
            );

            if has_accessory {
                Box(
                    Modifier::empty().width(tab_bar_accessory_gap(true)),
                    BoxSpec::default(),
                    || {},
                );
                (accessory.borrow_mut())();
            }
        },
    );
}

/// The standard detached accessory: a circular glass search button.
#[composable]
pub fn LiquidTabBarSearchAccessory(on_click: impl Fn() + 'static) {
    crate::widgets::GlassIconButton(
        Modifier::empty().content_description("Search"),
        crate::widgets::GlassButtonSpec::glass(),
        BAR_HEIGHT * 0.94,
        on_click,
        crate::icons::SEARCH,
    );
}

#[cfg(test)]
#[path = "tests/tab_bar_tests.rs"]
mod tests;