1use crate::material::{
6 neutral_surface_lift, neutral_surface_tint, Glass, GlassDynamics, GlassMorph, LiquidModifierExt,
7};
8use crate::motion::LiquidMotion;
9use crate::theme::{liquid_colors, liquid_typography, LiquidTypography};
10use cranpose_macros::composable;
11use cranpose_ui::text::{FontWeight, SpanStyle, TextStyle};
12use cranpose_ui::widgets::{
13 Box, BoxSpec, BoxWithConstraints, BoxWithConstraintsScope, Column, ColumnSpec, Row, RowSpec,
14 Text,
15};
16use cranpose_ui::{Brush, Color, CornerRadii, Modifier, PointerInputScope, Rect, Size};
17use cranpose_ui_layout::{Alignment, HorizontalAlignment, VerticalAlignment};
18use std::cell::RefCell;
19use std::rc::Rc;
20
21#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
23pub enum LiquidTabIconStyle {
24 #[default]
25 Plain,
26 AppBadge,
27}
28
29#[derive(Clone, Debug, PartialEq)]
31pub struct LiquidTab {
32 pub icon: &'static str,
33 pub label: &'static str,
34 pub icon_style: LiquidTabIconStyle,
35 pub icon_scale: f32,
38}
39
40impl LiquidTab {
41 pub fn new(icon: &'static str, label: &'static str) -> Self {
42 Self {
43 icon,
44 label,
45 icon_style: LiquidTabIconStyle::Plain,
46 icon_scale: 1.0,
47 }
48 }
49
50 pub fn app_badge(icon: &'static str, label: &'static str) -> Self {
51 Self {
52 icon,
53 label,
54 icon_style: LiquidTabIconStyle::AppBadge,
55 icon_scale: 1.0,
56 }
57 }
58
59 pub fn with_icon_scale(mut self, scale: f32) -> Self {
60 self.icon_scale = normalize_icon_scale(scale);
61 self
62 }
63}
64
65fn normalize_icon_scale(scale: f32) -> f32 {
66 if scale.is_finite() {
67 scale.clamp(0.5, 1.5)
68 } else {
69 1.0
70 }
71}
72
73fn tab_base_content_color(colors: crate::theme::LiquidColors) -> Color {
74 colors.label
75}
76
77fn tab_selection_content_color(colors: crate::theme::LiquidColors) -> Color {
78 colors.accent
79}
80
81const BAR_HEIGHT: f32 = 64.0;
82const BLOB_HEIGHT: f32 = 56.0;
86const BLOB_MARGIN: f32 = 4.0;
87const FLIGHT_LENS_PROJECTION_SCALE: f32 = 1.375;
93const TAB_LENS_REST_WIDTH_FACTOR: f32 = 1.10;
99const TAB_WIDTH: f32 = 78.0;
101const TAB_ICON_SIZE: f32 = 32.0;
103const TAB_LABEL_SIZE: f32 = 11.0;
104const TAP_SLOP: f32 = 6.0;
106const ACCESSORY_GAP: f32 = 10.0;
107
108#[derive(Clone, Copy, Debug, PartialEq)]
110pub struct LiquidTabBarSpec {
111 max_tab_width: f32,
112}
113
114impl LiquidTabBarSpec {
115 pub fn new(max_tab_width: f32) -> Self {
116 Self {
117 max_tab_width: if max_tab_width.is_finite() {
118 max_tab_width.max(1.0)
119 } else {
120 TAB_WIDTH
121 },
122 }
123 }
124}
125
126impl Default for LiquidTabBarSpec {
127 fn default() -> Self {
128 Self::new(TAB_WIDTH)
129 }
130}
131
132fn tab_flight_lens_material(foreground: cranpose_ui_graphics::Color, accent: Color) -> Glass {
133 Glass::lens()
134 .no_clip()
135 .tint(neutral_surface_tint(foreground, 0.13, 0.10))
136 .ink_recolor(accent, 0.85)
140 .blur_radius(0.0)
141 .refraction_depth(0.26)
147 .refraction_curve(0.25)
148 .dispersion(0.24)
149 .lift(0.12)
155 .highlight(0.35)
156}
157
158fn tab_bar_surface_material(foreground: cranpose_ui_graphics::Color) -> Glass {
159 Glass::regular()
160 .tint(neutral_surface_tint(foreground, 0.0, 0.04))
161 .blur_radius(4.0)
162 .saturation(1.12)
166 .lift(neutral_surface_lift(foreground, 0.35, -0.24))
167 .highlight(0.20)
168 .fold_depth(8.0)
173 .adaptive_frost(foreground, 0.28)
174}
175
176fn tab_flight_tint_multiplier(activity: f32) -> f32 {
177 1.0 - 0.25 * activity.clamp(0.0, 1.0)
178}
179
180fn tab_lens_activity_motion(raised: bool) -> cranpose_animation::AnimationType {
181 if raised {
182 cranpose_animation::spring(0.9, 1400.0)
185 } else {
186 cranpose_animation::spring(1.0, 900.0)
188 }
189}
190
191fn tab_lens_left(pointer_x: f32, tab_width: f32, count: usize, has_accessory: bool) -> f32 {
192 let last_tab = tab_width * count.saturating_sub(1) as f32;
193 let min = if has_accessory { -tab_width * 0.2 } else { 0.0 };
194 let max = if has_accessory {
195 tab_width * (count as f32 - 0.45)
196 } else {
197 last_tab
198 };
199 (pointer_x - tab_width * 0.5).clamp(min, max)
200}
201
202pub fn tab_lens_resting_left(selected: usize, tab_width: f32, count: usize) -> f32 {
209 tab_width * selected.min(count.saturating_sub(1)) as f32
210}
211
212pub fn tab_lens_rest_width(tab_width: f32) -> f32 {
215 tab_width * TAB_LENS_REST_WIDTH_FACTOR
216}
217
218#[derive(Clone, Copy, Debug, PartialEq)]
219struct AppBadgeGeometry {
220 size: Size,
221 corner_radius: f32,
222 stripe: Rect,
223 glyph: Rect,
224}
225
226fn app_badge_geometry(optical_scale: f32) -> AppBadgeGeometry {
227 let scale = normalize_icon_scale(optical_scale);
228 AppBadgeGeometry {
229 size: Size::new(20.0 * scale, 32.0 * scale),
230 corner_radius: 5.0 * scale,
231 stripe: Rect {
232 x: 7.0 * scale,
233 y: 4.5 * scale,
234 width: 6.0 * scale,
235 height: 1.5 * scale,
236 },
237 glyph: Rect {
238 x: 3.0 * scale,
239 y: 10.0 * scale,
240 width: 14.0 * scale,
241 height: 14.0 * scale,
242 },
243 }
244}
245
246#[composable]
247#[allow(non_snake_case)]
248fn TabIcon(icon: &'static str, style: LiquidTabIconStyle, color: Color, optical_scale: f32) {
249 const FRAME_HEIGHT: f32 = 32.0;
250 Box(
251 Modifier::empty().size(Size::new(TAB_ICON_SIZE, FRAME_HEIGHT)),
252 BoxSpec::default().content_alignment(Alignment::CENTER),
253 move || match style {
254 LiquidTabIconStyle::Plain => {
255 crate::icons::Icon(icon, TAB_ICON_SIZE * optical_scale, color)
256 }
257 LiquidTabIconStyle::AppBadge => {
258 let geometry = app_badge_geometry(optical_scale);
259 Box(
260 Modifier::empty()
261 .size(geometry.size)
262 .draw_behind(move |scope| {
263 scope.draw_round_rect(
264 Brush::solid(color),
265 CornerRadii::uniform(geometry.corner_radius),
266 );
267 scope.draw_rect_at(geometry.stripe, Brush::solid(Color::WHITE));
268 }),
269 BoxSpec::default(),
270 move || {
271 Box(
272 Modifier::empty()
273 .offset(geometry.glyph.x, geometry.glyph.y)
274 .size(Size::new(geometry.glyph.width, geometry.glyph.height)),
275 BoxSpec::default(),
276 move || crate::icons::Icon(icon, geometry.glyph.width, Color::WHITE),
277 );
278 },
279 );
280 }
281 },
282 );
283}
284
285#[derive(Clone, Copy, PartialEq)]
286struct TabCellsSpec {
287 base_color: Color,
288 selected: Option<usize>,
289 selected_color: Color,
290 interactive: bool,
291 selection_only: bool,
292}
293
294fn tab_cell_is_visible(index: usize, selected: Option<usize>, selection_only: bool) -> bool {
295 !selection_only || selected == Some(index)
296}
297
298#[composable]
299#[allow(non_snake_case)]
300fn TabCells(
301 modifier: Modifier,
302 tabs: Rc<Vec<LiquidTab>>,
303 typography: LiquidTypography,
304 tab_width: f32,
305 spec: TabCellsSpec,
306) {
307 Row(modifier, RowSpec::default(), move || {
308 for (index, tab) in tabs.iter().enumerate() {
309 let visible = tab_cell_is_visible(index, spec.selected, spec.selection_only);
310 let color = if spec.selected == Some(index) {
311 spec.selected_color
312 } else {
313 spec.base_color
314 };
315 let label_for_semantics = tab.label;
316 let mut cell = Modifier::empty().size(Size::new(tab_width, BLOB_HEIGHT));
317 if spec.interactive {
318 cell = cell.semantics(move |config| {
319 config.is_button = true;
320 config.is_clickable = true;
321 config.content_description = Some(label_for_semantics.to_string());
322 });
323 }
324 let icon = tab.icon;
325 let icon_style = tab.icon_style;
326 let icon_scale = tab.icon_scale;
327 let label = tab.label;
328 let label_style = TextStyle {
329 span_style: SpanStyle {
330 color: Some(color),
331 font_size: cranpose_ui::text::TextUnit::Sp(TAB_LABEL_SIZE),
332 font_weight: Some(FontWeight::MEDIUM),
333 ..typography.caption1.span_style.clone()
334 },
335 ..typography.caption1.clone()
336 };
337 Box(
338 cell,
339 BoxSpec::default().content_alignment(Alignment::CENTER),
340 move || {
341 if !visible {
342 return;
343 }
344 let label_style = label_style.clone();
345 Column(
346 Modifier::empty(),
347 ColumnSpec::default()
348 .horizontal_alignment(HorizontalAlignment::CenterHorizontally),
349 move || {
350 TabIcon(icon, icon_style, color, icon_scale);
351 Text(label, Modifier::empty(), label_style.clone());
352 },
353 );
354 },
355 );
356 }
357 });
358}
359
360fn tab_lens_node_top(node_height: f32) -> f32 {
361 (BAR_HEIGHT - node_height) * 0.5
362}
363
364fn tab_bar_accessory_gap(has_accessory: bool) -> f32 {
365 if has_accessory {
366 ACCESSORY_GAP
367 } else {
368 0.0
369 }
370}
371
372fn accessory_surfaces_touch(edge_gap: f32) -> bool {
373 edge_gap <= 0.0
374}
375
376fn tab_lens_base_size(tab_width: f32, activity: f32) -> (f32, f32) {
377 let activity = activity.clamp(0.0, 1.0);
378 let ease = activity * activity * (3.0 - 2.0 * activity);
379 let rest_width = tab_width * TAB_LENS_REST_WIDTH_FACTOR;
380 let projection = 1.0 + (FLIGHT_LENS_PROJECTION_SCALE - 1.0) * ease;
381 (rest_width * projection, BLOB_HEIGHT * projection)
382}
383
384#[derive(Clone, Copy, Debug, PartialEq)]
385struct TabFlightGeometry {
386 center: (f32, f32),
387 base_size: Size,
388 pose: crate::dynamics::LiquidPose,
389 lens_position: f32,
390 lens_activity: f32,
391 resting_tint: Color,
392 accessory_center: Option<(f32, f32)>,
393}
394
395#[derive(Clone, Copy, Debug, PartialEq)]
396struct TabFlightNode {
397 origin: (f32, f32),
398 size: Size,
399}
400
401fn tab_flight_dynamics(geometry: TabFlightGeometry, node: TabFlightNode) -> GlassDynamics {
402 let activity = geometry.lens_activity.clamp(0.0, 1.0);
403 let energy = geometry.pose.energy() * activity;
404 let radius = geometry.base_size.height * (0.48 + 0.02 * energy);
405 let glue = 20.0;
406 let shapes = geometry
407 .accessory_center
408 .filter(|(x, _)| {
409 let edge_gap = (*x - geometry.center.0).abs()
410 - geometry.base_size.width * geometry.pose.stretch.max(geometry.pose.ortho) * 0.5
411 - BAR_HEIGHT * 0.5;
412 accessory_surfaces_touch(edge_gap)
413 })
414 .map(|(x, y)| {
415 vec![(
416 x - node.origin.0,
417 y - node.origin.1,
418 BAR_HEIGHT,
419 BAR_HEIGHT,
420 -1.0,
421 )]
422 })
423 .unwrap_or_default();
424 GlassDynamics {
425 morph: Some(GlassMorph {
426 node_size: (node.size.width, node.size.height),
427 primary: (
428 geometry.center.0 - node.origin.0,
429 geometry.center.1 - node.origin.1,
430 geometry.base_size.width,
431 geometry.base_size.height,
432 radius,
433 ),
434 shapes,
435 glue,
436 wobble_amplitude: 1.1 * energy,
437 wobble_phase: geometry.lens_position * 0.045,
438 bulge_amplitude: geometry.pose.bulge_amplitude.min(8.0) * activity,
439 bulge_direction: geometry.pose.bulge_direction,
440 ellipse_blend: 0.0,
441 deformation: Some(crate::material::GlassDeformation::incompressible(
442 geometry.pose.axis,
443 1.0 + (geometry.pose.stretch - 1.0) * activity,
444 )),
445 }),
446 activity: Some(activity),
447 resting_tint: Some(geometry.resting_tint),
448 tint_alpha_multiplier: Some(tab_flight_tint_multiplier(geometry.lens_activity)),
449 ..Default::default()
450 }
451}
452
453#[composable]
455#[allow(non_snake_case)]
456pub fn LiquidTabBar(
457 modifier: Modifier,
458 spec: LiquidTabBarSpec,
459 tabs: Vec<LiquidTab>,
460 selected: usize,
461 on_select: impl Fn(usize) + 'static,
462) {
463 LiquidTabBarLayout(modifier, spec, tabs, selected, on_select, false, || {});
464}
465
466#[composable]
468#[allow(non_snake_case)]
469pub fn LiquidTabBarWithAccessory(
470 modifier: Modifier,
471 spec: LiquidTabBarSpec,
472 tabs: Vec<LiquidTab>,
473 selected: usize,
474 on_select: impl Fn(usize) + 'static,
475 accessory: impl FnMut() + 'static,
476) {
477 LiquidTabBarLayout(modifier, spec, tabs, selected, on_select, true, accessory);
478}
479
480#[composable]
481#[allow(non_snake_case)]
482fn LiquidTabBarLayout(
483 modifier: Modifier,
484 spec: LiquidTabBarSpec,
485 tabs: Vec<LiquidTab>,
486 selected: usize,
487 on_select: impl Fn(usize) + 'static,
488 has_accessory: bool,
489 accessory: impl FnMut() + 'static,
490) {
491 let colors = liquid_colors();
492 let typography = liquid_typography();
493 let count = tabs.len().max(1);
494 let selected = selected.min(count - 1);
495 let on_select: Rc<dyn Fn(usize)> = Rc::new(on_select);
496 let tabs = Rc::new(tabs);
497 let accessory = Rc::new(RefCell::new(accessory));
498
499 Row(
500 modifier,
501 RowSpec::default().vertical_alignment(VerticalAlignment::CenterVertically),
502 move || {
503 let tabs = Rc::clone(&tabs);
504 let typography = typography.clone();
505 let on_select = Rc::clone(&on_select);
506 let accessory = Rc::clone(&accessory);
507
508 let lens_x_outer = cranpose_core::remember(|| {
511 cranpose_core::mutableStateOf((
512 0.0f32,
513 0.0f32,
514 0.0f32,
515 crate::dynamics::LiquidPose::default(),
516 ))
517 })
518 .with(|state| *state);
519 let bar_touch =
525 cranpose_core::remember(|| cranpose_core::mutableStateOf((0.0f32, 0.0f32)))
526 .with(|state| *state);
527 let bar_held = cranpose_core::remember(|| cranpose_core::mutableStateOf(false))
528 .with(|state| *state);
529 let bar_press = cranpose_animation::animateFloatAsState(
530 if bar_held.get() { 1.0 } else { 0.0 },
531 cranpose_animation::spring(1.0, 600.0),
532 "tabbar-hold-press",
533 );
534 let bar_lift = Modifier::empty().graphics_layer(move || {
540 let press = bar_press.get().clamp(0.0, 1.0);
541 let rise = 1.0 + 0.03 * press;
542 cranpose_ui_graphics::GraphicsLayer {
543 scale_x: rise,
544 scale_y: rise,
545 translation_y: -2.5 * press,
546 ..Default::default()
547 }
548 });
549 Box(
553 bar_lift.then(Modifier::empty().height(BAR_HEIGHT)),
554 BoxSpec::default(),
555 move || {
556 let tabs = Rc::clone(&tabs);
557 let typography = typography.clone();
558 let on_select = Rc::clone(&on_select);
559 let pill = Modifier::empty()
562 .glass_effect_with(
563 tab_bar_surface_material(colors.label),
568 move || {
569 let press = bar_press.get().clamp(0.0, 1.0);
570 let (touch_x, touch_y) = bar_touch.get();
571 GlassDynamics {
572 highlight_boost: 0.45 * press,
573 saturation_boost: 0.12 * press,
574 touch: (press > 0.01).then_some((touch_x, touch_y, press)),
575 ..Default::default()
576 }
577 },
578 )
579 .height(BAR_HEIGHT);
580 Box(pill, BoxSpec::default(), move || {
581 let tabs = Rc::clone(&tabs);
582 let typography = typography.clone();
583 let on_select = Rc::clone(&on_select);
584 BoxWithConstraints(Modifier::empty().padding(BLOB_MARGIN), move |scope| {
585 let tabs = Rc::clone(&tabs);
586 let typography = typography.clone();
587 let on_select = Rc::clone(&on_select);
588 let constrained = scope.constraints().max_width;
589 let tab_width = if constrained.is_finite() && constrained > 1.0 {
590 (constrained / count as f32).min(spec.max_tab_width)
591 } else {
592 spec.max_tab_width
593 };
594
595 let lens_pressed =
598 cranpose_core::remember(|| cranpose_core::mutableStateOf(false))
599 .with(|state| *state);
600 let resting_lens_x = tab_lens_resting_left(selected, tab_width, count);
601 let lens_axis =
602 crate::motion::remember_liquid_drag_axis(resting_lens_x);
603 if !lens_pressed.get() {
609 lens_axis.settle_to(resting_lens_x, LiquidMotion::glide());
610 }
611 let lens_x = lens_axis.value();
612 let lens_pose = lens_axis.liquid_pose();
613 let lens_in_flight = !lens_axis.is_dragging()
614 && (lens_x - resting_lens_x).abs() > tab_width * 0.15;
615 let lens_raised = lens_pressed.get() || lens_in_flight;
616 let lens_activity_target = if lens_raised { 1.0 } else { 0.0 };
617 let lens_activity_anim = cranpose_animation::animateFloatAsState(
618 lens_activity_target,
619 tab_lens_activity_motion(lens_raised),
620 "tabbar-lens-activity",
621 );
622 let lens_activity = lens_activity_anim.get();
627 TabCells(
635 Modifier::empty(),
636 Rc::clone(&tabs),
637 typography.clone(),
638 tab_width,
639 TabCellsSpec {
640 base_color: tab_base_content_color(colors),
641 selected: Some(selected),
642 selected_color: tab_selection_content_color(colors),
643 interactive: true,
644 selection_only: false,
645 },
646 );
647
648 let row_width = tab_width * count as f32;
652 let gesture = Modifier::empty()
653 .size(Size::new(row_width, BLOB_HEIGHT))
654 .pointer_input(selected, {
655 let on_select = Rc::clone(&on_select);
656 let lens_axis = Rc::clone(&lens_axis);
657 move |scope: PointerInputScope| {
658 let on_select = Rc::clone(&on_select);
659 let lens_axis = Rc::clone(&lens_axis);
660 crate::motion::liquid_lens_gesture(
661 scope,
662 crate::motion::LiquidLensGesture {
663 axis: lens_axis,
664 cell_width: tab_width,
665 count,
666 tap_slop: TAP_SLOP,
667 drag_left: Rc::new(move |x| {
668 tab_lens_left(
669 x,
670 tab_width,
671 count,
672 has_accessory,
673 )
674 }),
675 rest_left: Rc::new(move |index| {
676 tab_lens_resting_left(index, tab_width, count)
677 }),
678 selected,
679 on_pressed: Rc::new(move |down| {
680 lens_pressed.set(down);
681 bar_held.set(down);
682 }),
683 on_touch: Rc::new(move |x, y| {
684 bar_touch
685 .set((x + BLOB_MARGIN, y + BLOB_MARGIN));
686 }),
687 on_select,
688 },
689 )
690 }
691 });
692 Box(gesture, BoxSpec::default(), || {});
693
694 let published = (lens_x, lens_activity, tab_width, lens_pose);
698 if lens_x_outer.get() != published {
699 lens_x_outer.set(published);
700 }
701 });
702 });
703
704 let (lens_px, lens_activity, lens_tab_w, pose) = lens_x_outer.get();
713 let lens_w =
714 lens_tab_w * TAB_LENS_REST_WIDTH_FACTOR * FLIGHT_LENS_PROJECTION_SCALE;
715 let lens_h = BLOB_HEIGHT * FLIGHT_LENS_PROJECTION_SCALE;
716 let deformation_headroom =
719 crate::dynamics::STRETCH_MAX.max(1.0 / crate::dynamics::STRETCH_MIN);
720 let node_w = lens_w * deformation_headroom + crate::dynamics::BULGE_MAX + 20.0;
721 let node_h = lens_h * deformation_headroom + crate::dynamics::BULGE_MAX + 16.0;
722 let lens_center_x = BLOB_MARGIN + lens_px + lens_tab_w * 0.5;
723 let node_x = lens_center_x - node_w * 0.5;
724 let node_top = tab_lens_node_top(node_h);
725 let pill_w = lens_tab_w * count as f32 + 2.0 * BLOB_MARGIN;
726 let (base_w, base_h) = tab_lens_base_size(lens_tab_w, lens_activity);
727 let geometry = TabFlightGeometry {
728 center: (lens_center_x, BAR_HEIGHT * 0.5),
729 base_size: Size::new(base_w, base_h),
730 pose,
731 lens_position: lens_px,
732 lens_activity,
733 resting_tint: colors.fill,
734 accessory_center: has_accessory.then_some((
735 pill_w + tab_bar_accessory_gap(true) + BAR_HEIGHT * 0.5,
736 BAR_HEIGHT * 0.5,
737 )),
738 };
739 let lens_node = TabFlightNode {
740 origin: (node_x, node_top),
741 size: Size::new(node_w, node_h),
742 };
743
744 let lens_geometry = geometry;
745 let lens = Modifier::empty()
746 .required_size(lens_node.size)
751 .offset(node_x, node_top)
752 .glass_effect_with(
753 tab_flight_lens_material(
754 colors.label,
755 tab_selection_content_color(colors),
756 ),
757 move || tab_flight_dynamics(lens_geometry, lens_node),
758 );
759 Box(lens, BoxSpec::default(), || {});
760 },
761 );
762
763 if has_accessory {
764 Box(
765 Modifier::empty().width(tab_bar_accessory_gap(true)),
766 BoxSpec::default(),
767 || {},
768 );
769 (accessory.borrow_mut())();
770 }
771 },
772 );
773}
774
775#[composable]
777#[allow(non_snake_case)]
778pub fn LiquidTabBarSearchAccessory(on_click: impl Fn() + 'static) {
779 crate::widgets::GlassIconButton(
781 Modifier::empty(),
782 crate::widgets::GlassButtonSpec::glass(),
783 BAR_HEIGHT * 0.94,
784 on_click,
785 crate::icons::SEARCH,
786 );
787}
788
789#[cfg(test)]
790mod tests {
791 use super::*;
792
793 #[test]
794 fn tab_bar_spec_normalizes_the_maximum_cell_width() {
795 assert_eq!(LiquidTabBarSpec::default().max_tab_width, TAB_WIDTH);
796 assert_eq!(LiquidTabBarSpec::new(85.0).max_tab_width, 85.0);
797 assert_eq!(LiquidTabBarSpec::new(0.0).max_tab_width, 1.0);
798 assert_eq!(LiquidTabBarSpec::new(f32::NAN).max_tab_width, TAB_WIDTH);
799 }
800
801 #[test]
802 fn drag_pointer_centers_the_lens_and_preserves_end_overdrag() {
803 let width = 100.0;
804 assert_eq!(tab_lens_left(50.0, width, 4, true), 0.0);
805 assert_eq!(tab_lens_left(250.0, width, 4, true), 200.0);
806 assert_eq!(tab_lens_left(-100.0, width, 4, true), -20.0);
807 assert_eq!(tab_lens_left(500.0, width, 4, true), 355.0);
808
809 assert_eq!(tab_lens_left(-100.0, width, 4, false), 0.0);
810 assert_eq!(tab_lens_left(500.0, width, 4, false), 300.0);
811 }
812
813 #[test]
814 fn resting_lens_centers_on_its_cell_and_stays_inside_the_pill() {
815 let tab = 78.0;
816 assert_eq!(tab_lens_resting_left(0, tab, 5), 0.0);
819 assert_eq!(tab_lens_resting_left(1, tab, 5), tab);
820 assert_eq!(tab_lens_resting_left(3, tab, 5), 3.0 * tab);
821 assert_eq!(tab_lens_resting_left(4, tab, 5), 4.0 * tab);
822 assert_eq!(tab_lens_resting_left(9, tab, 5), 4.0 * tab);
823 let overhang = tab * (TAB_LENS_REST_WIDTH_FACTOR - 1.0) * 0.5;
828 assert!(overhang <= BLOB_MARGIN + 1.0e-4);
829 }
830
831 #[test]
832 fn flight_lens_node_is_centered_on_the_bar_axis() {
833 for node_height in [48.0, 64.0, 96.0, 128.0] {
834 let center = tab_lens_node_top(node_height) + node_height * 0.5;
835 assert!((center - BAR_HEIGHT * 0.5).abs() < f32::EPSILON);
836 }
837 }
838
839 #[test]
840 fn liquid_tab_builds_reference_content() {
841 assert_eq!(TAB_ICON_SIZE, 32.0);
842 let tab = LiquidTab::new(crate::icons::STAR, "Discover");
843 assert_eq!(tab.icon, crate::icons::STAR);
844 assert_eq!(tab.label, "Discover");
845 assert_eq!(tab.icon_style, LiquidTabIconStyle::Plain);
846
847 let badge = LiquidTab::app_badge(crate::icons::APPLE, "WWDC");
848 assert_eq!(badge.icon_style, LiquidTabIconStyle::AppBadge);
849
850 let compact = LiquidTab::new(crate::icons::ACCOUNT_CIRCLE, "Account").with_icon_scale(0.72);
851 assert!((compact.icon_scale - 0.72).abs() < f32::EPSILON);
852 assert_eq!(tab.clone().with_icon_scale(f32::NAN).icon_scale, 1.0);
853 assert_eq!(tab.with_icon_scale(2.0).icon_scale, 1.5);
854 }
855
856 #[test]
857 fn app_badge_geometry_honors_the_tab_optical_scale() {
858 let full = app_badge_geometry(1.0);
859 let corrected = app_badge_geometry(0.85);
860 assert_eq!(full.size, Size::new(20.0, 32.0));
861 assert_eq!(corrected.size, Size::new(17.0, 27.2));
862 assert!((corrected.glyph.width - 11.9).abs() < 1.0e-5);
863 assert!((corrected.corner_radius / full.corner_radius - 0.85).abs() < f32::EPSILON);
864 assert!((corrected.stripe.x / full.stripe.x - 0.85).abs() < f32::EPSILON);
865 assert!((corrected.glyph.width / full.glyph.width - 0.85).abs() < f32::EPSILON);
866 }
867
868 #[test]
869 fn base_tab_content_remains_neutral_under_the_moving_selection_layer() {
870 let colors = crate::theme::LiquidColors::light(cranpose_ui_graphics::Color::from_rgb_u8(
871 0, 122, 255,
872 ));
873 assert_eq!(tab_base_content_color(colors), colors.label);
874 assert_eq!(tab_selection_content_color(colors), colors.accent);
875 }
876
877 #[test]
878 fn selection_mask_and_lens_resolve_the_same_global_sdf() {
879 let geometry = TabFlightGeometry {
880 center: (212.0, 32.0),
881 base_size: Size::new(106.0, 64.0),
882 pose: crate::dynamics::LiquidPose::default(),
883 lens_position: 160.0,
884 lens_activity: 1.0,
885 resting_tint: Color::BLACK.with_alpha(0.10),
886 accessory_center: None,
887 };
888 let mask_node = TabFlightNode {
889 origin: (0.0, 0.0),
890 size: Size::new(328.0, 64.0),
891 };
892 let lens_node = TabFlightNode {
893 origin: (132.0, -22.0),
894 size: Size::new(160.0, 108.0),
895 };
896 let mask = tab_flight_dynamics(geometry, mask_node)
897 .morph
898 .expect("selection mask morph");
899 let lens = tab_flight_dynamics(geometry, lens_node)
900 .morph
901 .expect("lens morph");
902 assert_eq!(
903 (
904 mask.primary.0 + mask_node.origin.0,
905 mask.primary.1 + mask_node.origin.1
906 ),
907 geometry.center
908 );
909 assert_eq!(
910 (
911 lens.primary.0 + lens_node.origin.0,
912 lens.primary.1 + lens_node.origin.1
913 ),
914 geometry.center
915 );
916 assert_eq!(
917 (mask.primary.2, mask.primary.3, mask.primary.4),
918 (lens.primary.2, lens.primary.3, lens.primary.4)
919 );
920 assert_eq!(mask.node_size, (328.0, 64.0));
921 assert_eq!(lens.node_size, (160.0, 108.0));
922 }
923
924 #[test]
925 fn unified_bar_has_no_detached_accessory_gap() {
926 assert_eq!(tab_bar_accessory_gap(false), 0.0);
927 assert_eq!(tab_bar_accessory_gap(true), 10.0);
928 }
929
930 #[test]
931 fn flight_lens_only_joins_accessory_after_surface_contact() {
932 assert!(!accessory_surfaces_touch(0.01));
933 assert!(accessory_surfaces_touch(0.0));
934 assert!(accessory_surfaces_touch(-4.0));
935 }
936
937 #[test]
938 fn lens_depth_is_an_isotropic_projection_separate_from_fluid_strain() {
939 let resting = tab_lens_base_size(TAB_WIDTH, 0.0);
940 let raised = tab_lens_base_size(TAB_WIDTH, 1.0);
941 assert_eq!(
942 resting,
943 (TAB_WIDTH * TAB_LENS_REST_WIDTH_FACTOR, BLOB_HEIGHT)
944 );
945 assert!((raised.0 / resting.0 - FLIGHT_LENS_PROJECTION_SCALE).abs() < 0.001);
946 assert!((raised.1 / resting.1 - FLIGHT_LENS_PROJECTION_SCALE).abs() < 0.001);
947 let area_ratio = raised.0 * raised.1 / (resting.0 * resting.1);
952 let projection_area = FLIGHT_LENS_PROJECTION_SCALE * FLIGHT_LENS_PROJECTION_SCALE;
953 assert!(
954 (area_ratio - projection_area).abs() < 0.01,
955 "raised area must be exactly the isotropic projection, got {area_ratio}"
956 );
957 }
958
959 #[test]
960 fn tab_grid_matches_the_reference_pitch() {
961 assert_eq!(TAB_WIDTH, 78.0);
962 }
963
964 #[test]
965 fn tab_grid_matches_the_reference_inner_inset() {
966 assert_eq!(BLOB_MARGIN, 4.0);
969 assert_eq!(BLOB_HEIGHT + 2.0 * BLOB_MARGIN, BAR_HEIGHT);
970 }
971
972 #[test]
973 fn flight_lens_uses_the_clear_wcksrd_contract() {
974 let glass = tab_flight_lens_material(
975 cranpose_ui_graphics::Color::BLACK,
976 cranpose_ui_graphics::Color::from_rgb_u8(0, 122, 255),
977 );
978 let generic_lens = Glass::lens();
979 assert!(glass.lift.is_some_and(|lift| (0.0..=0.15).contains(&lift)));
982 assert!(glass.refraction_depth < generic_lens.refraction_depth);
983 assert!(glass.refraction_curve < generic_lens.refraction_curve);
984 assert!(glass.dispersion < generic_lens.dispersion);
985 assert_eq!(glass.blur_radius, Some(0.0));
986 assert!(glass.highlight < generic_lens.highlight);
987 assert!(
988 glass.shadow,
989 "the moving lens needs its target-visible SDF contact outline"
990 );
991 assert!(glass
992 .tint
993 .is_some_and(|tint| { tint.r() < 0.05 && (0.125..=0.135).contains(&tint.a()) }));
994 assert_eq!(glass.adaptive_frost, 0.0);
995 }
996
997 #[test]
998 fn flight_lens_retains_neutral_tint_through_direct_motion() {
999 assert_eq!(tab_flight_tint_multiplier(0.0), 1.0);
1000 assert!((tab_flight_tint_multiplier(1.0) - 0.75).abs() < f32::EPSILON);
1001 assert_eq!(tab_flight_tint_multiplier(-1.0), 1.0);
1002 assert!((tab_flight_tint_multiplier(2.0) - 0.75).abs() < f32::EPSILON);
1003 }
1004
1005 #[test]
1006 fn bar_surface_adapts_frost_to_its_foreground() {
1007 let glass = tab_bar_surface_material(cranpose_ui_graphics::Color::BLACK);
1008 assert_eq!(glass.blur_radius, Some(4.0));
1009 assert_eq!(glass.saturation, Some(1.12));
1010 assert_eq!(glass.lift, Some(0.35));
1011 assert_eq!(glass.refraction_depth, 0.34);
1012 assert_eq!(glass.adaptive_frost, 0.28);
1013 }
1014
1015 #[test]
1016 fn bar_surface_lift_tracks_the_local_foreground_polarity() {
1017 let light_surface = tab_bar_surface_material(cranpose_ui_graphics::Color::BLACK);
1018 assert_eq!(light_surface.lift, Some(0.35));
1019
1020 let dark_surface = tab_bar_surface_material(cranpose_ui_graphics::Color::WHITE);
1021 assert_eq!(dark_surface.lift, Some(-0.24));
1022 }
1023
1024 #[test]
1025 fn bar_surface_tint_separates_from_same_polarity_backdrops() {
1026 let light_surface = tab_bar_surface_material(cranpose_ui_graphics::Color::BLACK)
1027 .tint
1028 .expect("bar tint");
1029 assert!(light_surface.r() < 0.05);
1030 assert_eq!(light_surface.a(), 0.0);
1031
1032 let dark_surface = tab_bar_surface_material(cranpose_ui_graphics::Color::WHITE)
1033 .tint
1034 .expect("bar tint");
1035 assert!(dark_surface.r() > 0.95);
1036 assert!((0.03..=0.05).contains(&dark_surface.a()));
1037 }
1038
1039 #[test]
1040 fn contact_rises_continuously_and_returns_on_the_measured_settle() {
1041 let cranpose_animation::AnimationType::Spring(rise) = tab_lens_activity_motion(true) else {
1042 panic!("contact rise must use a spring");
1043 };
1044 assert_eq!(rise.stiffness, 1400.0);
1045 let cranpose_animation::AnimationType::Spring(settle) = tab_lens_activity_motion(false)
1046 else {
1047 panic!("arrival contraction must use a spring");
1048 };
1049 assert_eq!(settle.damping_ratio, 1.0);
1050 assert_eq!(settle.stiffness, 900.0);
1051 }
1052}