tessera-components 0.0.0

Basic components for tessera-ui, using md3e design principles.
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
//! Material progress indicators.
//!
//! ## Usage
//!
//! Use to indicate the completion of a task or a specific value in a range.
use std::time::Instant;

use derive_setters::Setters;
use tessera_ui::{
    Color, ComputedData, Constraint, DimensionValue, Dp, MeasurementError, Modifier,
    ParentConstraint, Px, PxPosition,
    accesskit::Role,
    layout::{LayoutInput, LayoutOutput, LayoutSpec, RenderInput},
    remember, tessera, use_context,
};

use crate::{
    modifier::{ModifierExt as _, SemanticsArgs},
    pipelines::progress_arc::command::{ProgressArcCap, ProgressArcCommand},
    shape_def::Shape,
    surface::{SurfaceArgs, surface},
    theme::MaterialTheme,
};

/// Stroke cap for progress indicators.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ProgressStrokeCap {
    /// Rounded stroke ends.
    #[default]
    Round,
    /// Flat stroke ends.
    Butt,
}

impl ProgressStrokeCap {
    fn effective_is_butt(self, component_width: Px, component_height: Px) -> bool {
        self == ProgressStrokeCap::Butt || component_height.0 > component_width.0
    }
}

#[derive(Clone, PartialEq)]
struct LinearProgressLayout {
    progress: Option<f32>,
    stroke_cap: ProgressStrokeCap,
    gap_size: Dp,
    draw_stop_indicator: bool,
    animation_cycle: Option<f32>,
}

impl LayoutSpec for LinearProgressLayout {
    fn measure(
        &self,
        input: &LayoutInput<'_>,
        output: &mut LayoutOutput<'_>,
    ) -> Result<ComputedData, MeasurementError> {
        let (self_width, self_height) = resolve_linear_size(input.parent_constraint());
        let is_butt = self.stroke_cap.effective_is_butt(self_width, self_height);
        let gap_fraction =
            adjusted_linear_gap_fraction(self_width, self_height, self.gap_size, is_butt);
        let child_height = self_height;

        let children_ids = input.children_ids();

        if let Some(progress) = self.progress {
            let progress = if progress.is_nan() {
                0.0
            } else {
                progress.clamp(0.0, 1.0)
            };
            let track_start = progress + progress.min(gap_fraction);

            let track_id = children_ids[0];
            let indicator_id = children_ids[1];
            let stop_id = if self.draw_stop_indicator {
                children_ids.get(2).copied()
            } else {
                None
            };

            if let Some((x, w)) =
                linear_segment_bounds(0.0, progress, self_width, self_height, is_butt)
            {
                let constraint = Constraint::new(
                    DimensionValue::Fixed(w),
                    DimensionValue::Fixed(child_height),
                );
                input.measure_child(indicator_id, &constraint)?;
                output.place_child(indicator_id, PxPosition::new(x, Px(0)));
            } else {
                let constraint = Constraint::new(
                    DimensionValue::Fixed(Px(0)),
                    DimensionValue::Fixed(child_height),
                );
                input.measure_child(indicator_id, &constraint)?;
                output.place_child(indicator_id, PxPosition::new(Px(0), Px(0)));
            }

            if track_start <= 1.0
                && let Some((x, w)) =
                    linear_segment_bounds(track_start, 1.0, self_width, self_height, is_butt)
            {
                let constraint = Constraint::new(
                    DimensionValue::Fixed(w),
                    DimensionValue::Fixed(child_height),
                );
                input.measure_child(track_id, &constraint)?;
                output.place_child(track_id, PxPosition::new(x, Px(0)));
            } else {
                let constraint = Constraint::new(
                    DimensionValue::Fixed(Px(0)),
                    DimensionValue::Fixed(child_height),
                );
                input.measure_child(track_id, &constraint)?;
                output.place_child(track_id, PxPosition::new(Px(0), Px(0)));
            }

            if let Some(stop_id) = stop_id {
                let (pos, stop_size) = stop_indicator_bounds(self_width, self_height);
                let constraint = Constraint::new(
                    DimensionValue::Fixed(stop_size),
                    DimensionValue::Fixed(stop_size),
                );
                input.measure_child(stop_id, &constraint)?;
                output.place_child(stop_id, pos);
            }
        } else {
            let cycle = self.animation_cycle.unwrap_or(0.0);
            let first_head = keyframe_0_to_1(cycle, 0, 1000, 1750, emphasized_accelerate);
            let first_tail = keyframe_0_to_1(cycle, 250, 1000, 1750, emphasized_accelerate);
            let second_head = keyframe_0_to_1(cycle, 650, 850, 1750, emphasized_accelerate);
            let second_tail = keyframe_0_to_1(cycle, 900, 850, 1750, emphasized_accelerate);

            let track_before_id = children_ids[0];
            let line1_id = children_ids[1];
            let track_between_id = children_ids[2];
            let line2_id = children_ids[3];
            let track_after_id = children_ids[4];

            let mut set_segment = |node_id, start: f32, end: f32| -> Result<(), MeasurementError> {
                if let Some((x, w)) =
                    linear_segment_bounds(start, end, self_width, self_height, is_butt)
                {
                    let constraint = Constraint::new(
                        DimensionValue::Fixed(w),
                        DimensionValue::Fixed(child_height),
                    );
                    input.measure_child(node_id, &constraint)?;
                    output.place_child(node_id, PxPosition::new(x, Px(0)));
                } else {
                    let constraint = Constraint::new(
                        DimensionValue::Fixed(Px(0)),
                        DimensionValue::Fixed(child_height),
                    );
                    input.measure_child(node_id, &constraint)?;
                    output.place_child(node_id, PxPosition::new(Px(0), Px(0)));
                }
                Ok(())
            };

            if first_head < 1.0 - gap_fraction {
                let start = if first_head > 0.0 {
                    first_head + gap_fraction
                } else {
                    0.0
                };
                set_segment(track_before_id, start, 1.0)?;
            } else {
                set_segment(track_before_id, 0.0, 0.0)?;
            }

            if first_head - first_tail > 0.0 {
                set_segment(line1_id, first_head, first_tail)?;
            } else {
                set_segment(line1_id, 0.0, 0.0)?;
            }

            if first_tail > gap_fraction {
                let start = if second_head > 0.0 {
                    second_head + gap_fraction
                } else {
                    0.0
                };
                let end = if first_tail < 1.0 {
                    first_tail - gap_fraction
                } else {
                    1.0
                };
                set_segment(track_between_id, start, end)?;
            } else {
                set_segment(track_between_id, 0.0, 0.0)?;
            }

            if second_head - second_tail > 0.0 {
                set_segment(line2_id, second_head, second_tail)?;
            } else {
                set_segment(line2_id, 0.0, 0.0)?;
            }

            if second_tail > gap_fraction {
                let end = if second_tail < 1.0 {
                    second_tail - gap_fraction
                } else {
                    1.0
                };
                set_segment(track_after_id, 0.0, end)?;
            } else {
                set_segment(track_after_id, 0.0, 0.0)?;
            }
        }

        Ok(ComputedData {
            width: self_width,
            height: self_height,
        })
    }
}

#[derive(Clone, PartialEq)]
struct CircularProgressLayout {
    progress: Option<f32>,
    diameter: Dp,
    stroke_width: Dp,
    color: Color,
    track_color: Color,
    stroke_cap: ProgressStrokeCap,
    gap_size: Dp,
    animation_start: Instant,
}

impl LayoutSpec for CircularProgressLayout {
    fn measure(
        &self,
        _input: &LayoutInput<'_>,
        _output: &mut LayoutOutput<'_>,
    ) -> Result<ComputedData, MeasurementError> {
        let diameter_px = self.diameter.to_px();
        Ok(ComputedData {
            width: diameter_px,
            height: diameter_px,
        })
    }

    fn record(&self, input: &RenderInput<'_>) {
        let diameter_px = self.diameter.to_px();
        let stroke_px = self.stroke_width.to_px();

        let is_butt = self.stroke_cap.effective_is_butt(diameter_px, diameter_px);
        let cap = if is_butt {
            ProgressArcCap::Butt
        } else {
            ProgressArcCap::Round
        };

        let start_base = 270.0;
        let gap_sweep =
            circular_gap_sweep_degrees(self.diameter, self.stroke_width, self.gap_size, is_butt);

        let mut metadata = input.metadata_mut();

        if let Some(progress) = self.progress {
            let progress = if progress.is_nan() {
                0.0
            } else {
                progress.clamp(0.0, 1.0)
            };
            let sweep = progress * 360.0;
            let gap = sweep.min(gap_sweep);
            let track_start = start_base + sweep + gap;
            let track_sweep = 360.0 - sweep - gap * 2.0;

            if self.track_color.a > 0.0 && track_sweep > 0.0 {
                metadata.push_draw_command(ProgressArcCommand {
                    color: self.track_color,
                    stroke_width_px: stroke_px.to_f32(),
                    start_angle_degrees: track_start,
                    sweep_angle_degrees: track_sweep,
                    cap,
                });
            }
            if self.color.a > 0.0 && sweep > 0.0 {
                metadata.push_draw_command(ProgressArcCommand {
                    color: self.color,
                    stroke_width_px: stroke_px.to_f32(),
                    start_angle_degrees: start_base,
                    sweep_angle_degrees: sweep,
                    cap,
                });
            }
        } else {
            let elapsed_ms = Instant::now()
                .saturating_duration_since(self.animation_start)
                .as_millis() as f32;
            let cycle_ms = elapsed_ms % 6000.0;

            let global_rotation = (cycle_ms / 6000.0) * 1080.0;
            let additional_rotation = circular_additional_rotation_degrees(cycle_ms);
            let rotation = global_rotation + additional_rotation;

            let progress = circular_indeterminate_progress(cycle_ms);
            let sweep = progress * 360.0;
            let gap = sweep.min(gap_sweep);

            let track_start = rotation + sweep + gap;
            let track_sweep = 360.0 - sweep - gap * 2.0;

            if self.track_color.a > 0.0 && track_sweep > 0.0 {
                metadata.push_draw_command(ProgressArcCommand {
                    color: self.track_color,
                    stroke_width_px: stroke_px.to_f32(),
                    start_angle_degrees: track_start,
                    sweep_angle_degrees: track_sweep,
                    cap,
                });
            }
            if self.color.a > 0.0 && sweep > 0.0 {
                metadata.push_draw_command(ProgressArcCommand {
                    color: self.color,
                    stroke_width_px: stroke_px.to_f32(),
                    start_angle_degrees: rotation,
                    sweep_angle_degrees: sweep,
                    cap,
                });
            }
        }
    }
}

/// Material Design 3 defaults for progress indicators.
pub struct ProgressIndicatorDefaults;

impl ProgressIndicatorDefaults {
    /// Default width for linear progress indicators.
    pub const LINEAR_INDICATOR_WIDTH: Dp = Dp(240.0);
    /// Default height for linear progress indicators.
    pub const LINEAR_INDICATOR_HEIGHT: Dp = Dp(4.0);
    /// Default stop indicator size for linear progress indicators.
    pub const LINEAR_TRACK_STOP_INDICATOR_SIZE: Dp = Dp(4.0);
    /// Default gap between the indicator and the track for linear indicators.
    pub const LINEAR_INDICATOR_TRACK_GAP_SIZE: Dp = Dp(4.0);
    /// Maximum trailing inset for the stop indicator.
    pub const STOP_INDICATOR_TRAILING_SPACE: Dp = Dp(6.0);

    /// Default diameter for circular progress indicators.
    pub const CIRCULAR_INDICATOR_DIAMETER: Dp = Dp(40.0);
    /// Default stroke width for circular progress indicators.
    pub const CIRCULAR_STROKE_WIDTH: Dp = Dp(4.0);
    /// Default gap between the indicator and the track for circular indicators.
    pub const CIRCULAR_INDICATOR_TRACK_GAP_SIZE: Dp = Dp(4.0);
}

fn cubic_bezier(t: f32, a: f32, b: f32, c: f32, d: f32) -> f32 {
    let u = 1.0 - t;
    (u * u * u * a) + (3.0 * u * u * t * b) + (3.0 * u * t * t * c) + (t * t * t * d)
}

fn cubic_bezier_easing(progress: f32, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
    let x = progress.clamp(0.0, 1.0);
    let mut lo = 0.0;
    let mut hi = 1.0;
    let mut t = x;

    for _ in 0..16 {
        let mid = (lo + hi) * 0.5;
        let mid_x = cubic_bezier(mid, 0.0, x1, x2, 1.0);
        if mid_x < x {
            lo = mid;
        } else {
            hi = mid;
        }
        t = mid;
    }

    cubic_bezier(t, 0.0, y1, y2, 1.0).clamp(0.0, 1.0)
}

fn lerp(a: f32, b: f32, t: f32) -> f32 {
    a + (b - a) * t
}

fn linear_cycle_progress(start: Instant, duration_ms: u32) -> f32 {
    let elapsed_ms = Instant::now().saturating_duration_since(start).as_millis() as u64;
    let duration_ms = duration_ms.max(1) as u64;
    (elapsed_ms % duration_ms) as f32 / duration_ms as f32
}

fn keyframe_0_to_1(
    cycle_progress: f32,
    delay_ms: u32,
    duration_ms: u32,
    total_ms: u32,
    easing: fn(f32) -> f32,
) -> f32 {
    let t_ms = cycle_progress.clamp(0.0, 1.0) * total_ms as f32;
    let delay = delay_ms as f32;
    let duration = duration_ms.max(1) as f32;
    if t_ms <= delay {
        0.0
    } else if t_ms >= delay + duration {
        1.0
    } else {
        easing((t_ms - delay) / duration)
    }
}

fn emphasized_accelerate(progress: f32) -> f32 {
    cubic_bezier_easing(progress, 0.3, 0.0, 0.8, 0.15)
}

fn emphasized_decelerate(progress: f32) -> f32 {
    cubic_bezier_easing(progress, 0.05, 0.7, 0.1, 1.0)
}

fn standard_easing(progress: f32) -> f32 {
    cubic_bezier_easing(progress, 0.2, 0.0, 0.0, 1.0)
}

fn resolve_dimension(dimension: DimensionValue, fallback: Px) -> Px {
    match dimension {
        DimensionValue::Fixed(px) => px,
        DimensionValue::Fill { max, .. } | DimensionValue::Wrap { max, .. } => {
            max.unwrap_or(fallback)
        }
    }
}

fn resolve_linear_size(parent: ParentConstraint<'_>) -> (Px, Px) {
    let fallback_width = ProgressIndicatorDefaults::LINEAR_INDICATOR_WIDTH.to_px();
    let fallback_height = ProgressIndicatorDefaults::LINEAR_INDICATOR_HEIGHT.to_px();
    let merged = Constraint::new(parent.width(), parent.height()).merge(parent);
    let width = resolve_dimension(merged.width, fallback_width);
    let height = resolve_dimension(merged.height, fallback_height);
    (width, height)
}

fn linear_segment_bounds(
    start_fraction: f32,
    end_fraction: f32,
    width: Px,
    height: Px,
    is_butt: bool,
) -> Option<(Px, Px)> {
    if width.0 <= 0 || height.0 <= 0 {
        return None;
    }

    let start = start_fraction;
    let end = end_fraction;
    let min_frac = start.min(end);
    let max_frac = start.max(end);
    if (max_frac - min_frac) <= 0.0 {
        return None;
    }

    let w = width.to_f32();
    let bar_start = (min_frac * w).clamp(0.0, w);
    let bar_end = (max_frac * w).clamp(0.0, w);

    if is_butt {
        let seg_w = bar_end - bar_start;
        if seg_w <= 0.0 {
            None
        } else {
            Some((
                Px::saturating_from_f32(bar_start),
                Px::saturating_from_f32(seg_w),
            ))
        }
    } else {
        let stroke_cap_offset = height.to_f32() * 0.5;
        let adjusted_start = bar_start.clamp(stroke_cap_offset, w - stroke_cap_offset);
        let adjusted_end = bar_end.clamp(stroke_cap_offset, w - stroke_cap_offset);
        let seg_w = (adjusted_end - adjusted_start) + stroke_cap_offset * 2.0;
        if seg_w <= 0.0 {
            None
        } else {
            Some((
                Px::saturating_from_f32(adjusted_start - stroke_cap_offset),
                Px::saturating_from_f32(seg_w),
            ))
        }
    }
}

fn adjusted_linear_gap_fraction(width: Px, height: Px, gap_size: Dp, is_butt: bool) -> f32 {
    if width.0 <= 0 {
        return 0.0;
    }
    let adjusted_gap = if is_butt {
        gap_size
    } else {
        Dp(gap_size.0 + Dp::from_pixels_f32(height.to_f32()).0)
    };
    adjusted_gap.to_pixels_f32() / width.to_f32()
}

fn stop_indicator_bounds(width: Px, height: Px) -> (PxPosition, Px) {
    let stop_size_px = Px::saturating_from_f32(
        ProgressIndicatorDefaults::LINEAR_TRACK_STOP_INDICATOR_SIZE
            .to_pixels_f32()
            .min(height.to_f32()),
    );
    let max_stop_offset = ProgressIndicatorDefaults::STOP_INDICATOR_TRAILING_SPACE.to_px();
    let stop_offset =
        ((height.to_f32() - stop_size_px.to_f32()) / 2.0).min(max_stop_offset.to_f32());
    let x = width.to_f32() - stop_size_px.to_f32() - stop_offset;
    let y = (height.to_f32() - stop_size_px.to_f32()) / 2.0;
    (
        PxPosition::new(Px::saturating_from_f32(x), Px::saturating_from_f32(y)),
        stop_size_px,
    )
}

/// Arguments for configuring a Material Design linear progress indicator.
#[derive(Clone, Debug, Setters)]
pub struct LinearProgressIndicatorArgs {
    /// Current progress in the range 0.0..=1.0.
    ///
    /// When omitted, the indicator renders in an indeterminate mode.
    #[setters(strip_option)]
    pub progress: Option<f32>,

    /// Modifier chain applied to the indicator subtree.
    pub modifier: Modifier,

    /// Color of the active indicator.
    pub color: Color,

    /// Color of the inactive track.
    pub track_color: Color,

    /// Stroke cap used for the indicator ends.
    pub stroke_cap: ProgressStrokeCap,

    /// Size of the gap between the active indicator and the track.
    pub gap_size: Dp,

    /// Whether to draw a stop indicator at the end of the track.
    pub draw_stop_indicator: bool,

    /// Optional accessibility label read by assistive technologies.
    #[setters(strip_option, into)]
    pub accessibility_label: Option<String>,

    /// Optional accessibility description.
    #[setters(strip_option, into)]
    pub accessibility_description: Option<String>,
}

impl Default for LinearProgressIndicatorArgs {
    fn default() -> Self {
        let scheme = use_context::<MaterialTheme>()
            .expect("MaterialTheme must be provided")
            .get()
            .color_scheme;
        Self {
            progress: None,
            modifier: Modifier::new().size(
                ProgressIndicatorDefaults::LINEAR_INDICATOR_WIDTH,
                ProgressIndicatorDefaults::LINEAR_INDICATOR_HEIGHT,
            ),
            color: scheme.primary,
            track_color: scheme.secondary_container,
            stroke_cap: ProgressStrokeCap::default(),
            gap_size: ProgressIndicatorDefaults::LINEAR_INDICATOR_TRACK_GAP_SIZE,
            draw_stop_indicator: true,
            accessibility_label: None,
            accessibility_description: None,
        }
    }
}

/// # linear_progress_indicator
///
/// Renders a Material Design progress indicator in a horizontal, linear form.
///
/// ## Usage
///
/// Display completion state for determinate work, or show ongoing activity when
/// the remaining duration is unknown.
///
/// ## Parameters
///
/// - `args` — configures the indicator; see [`LinearProgressIndicatorArgs`].
///
/// ## Examples
///
/// ```
/// # use tessera_ui::tessera;
/// # #[tessera]
/// # fn component() {
/// use tessera_components::progress::{LinearProgressIndicatorArgs, linear_progress_indicator};
/// # use tessera_components::theme::{MaterialTheme, material_theme};
///
/// # material_theme(
/// #     || MaterialTheme::default(),
/// #     || {
/// linear_progress_indicator(LinearProgressIndicatorArgs::default().progress(0.75));
/// #     },
/// # );
/// # }
/// # component();
/// ```
#[tessera]
pub fn linear_progress_indicator(args: impl Into<LinearProgressIndicatorArgs>) {
    let args: LinearProgressIndicatorArgs = args.into();
    let modifier = args.modifier;
    modifier.run(move || linear_progress_indicator_inner(args));
}

#[tessera]
fn linear_progress_indicator_inner(args: LinearProgressIndicatorArgs) {
    let args_for_accessibility = args.clone();
    let animation_start = remember(Instant::now);

    let segment_shape = if args.stroke_cap == ProgressStrokeCap::Butt {
        Shape::RECTANGLE
    } else {
        Shape::capsule()
    };

    let mut semantics = SemanticsArgs::new().role(Role::ProgressIndicator);
    if let Some(label) = args_for_accessibility.accessibility_label.clone() {
        semantics = semantics.label(label);
    }
    if let Some(description) = args_for_accessibility.accessibility_description.clone() {
        semantics = semantics.description(description);
    }
    if let Some(progress) = args_for_accessibility.progress {
        let progress = if progress.is_nan() {
            0.0
        } else {
            progress.clamp(0.0, 1.0)
        };
        semantics = semantics
            .numeric_range(0.0, 1.0)
            .numeric_value(progress as f64);
    }

    let args_for_children = args.clone();
    let args_for_measure = args;
    let animation_start_for_measure = animation_start;

    Modifier::new().semantics(semantics).run(move || {
        if args_for_children.progress.is_some() {
            surface(
                SurfaceArgs::default()
                    .style(args_for_children.track_color.into())
                    .shape(segment_shape)
                    .modifier(Modifier::new().fill_max_size()),
                || {},
            );
            surface(
                SurfaceArgs::default()
                    .style(args_for_children.color.into())
                    .shape(segment_shape)
                    .modifier(Modifier::new().fill_max_size()),
                || {},
            );
            if args_for_children.draw_stop_indicator {
                let stop_shape = if args_for_children.stroke_cap == ProgressStrokeCap::Butt {
                    Shape::RECTANGLE
                } else {
                    Shape::Ellipse
                };
                surface(
                    SurfaceArgs::default()
                        .style(args_for_children.color.into())
                        .shape(stop_shape)
                        .modifier(Modifier::new().fill_max_size()),
                    || {},
                );
            }
        } else {
            for (color, shape) in [
                (args_for_children.track_color, segment_shape),
                (args_for_children.color, segment_shape),
                (args_for_children.track_color, segment_shape),
                (args_for_children.color, segment_shape),
                (args_for_children.track_color, segment_shape),
            ] {
                surface(
                    SurfaceArgs::default()
                        .style(color.into())
                        .shape(shape)
                        .modifier(Modifier::new().fill_max_size()),
                    || {},
                );
            }
        }

        let animation_cycle = if args_for_measure.progress.is_some() {
            None
        } else {
            Some(linear_cycle_progress(
                animation_start_for_measure.get(),
                1750,
            ))
        };
        layout(LinearProgressLayout {
            progress: args_for_measure.progress,
            stroke_cap: args_for_measure.stroke_cap,
            gap_size: args_for_measure.gap_size,
            draw_stop_indicator: args_for_measure.draw_stop_indicator,
            animation_cycle,
        });
    });
}

/// Arguments for configuring a Material Design circular progress indicator.
#[derive(Clone, Debug, Setters)]
pub struct CircularProgressIndicatorArgs {
    /// Current progress in the range 0.0..=1.0.
    ///
    /// When omitted, the indicator renders in an indeterminate mode.
    #[setters(strip_option)]
    pub progress: Option<f32>,

    /// Diameter of the indicator.
    pub diameter: Dp,

    /// Stroke width of the indicator.
    pub stroke_width: Dp,

    /// Color of the active indicator.
    pub color: Color,

    /// Color of the track behind the indicator.
    pub track_color: Color,

    /// Stroke cap used for the arc ends.
    pub stroke_cap: ProgressStrokeCap,

    /// Gap between the indicator and the track.
    pub gap_size: Dp,

    /// Optional accessibility label read by assistive technologies.
    #[setters(strip_option, into)]
    pub accessibility_label: Option<String>,

    /// Optional accessibility description.
    #[setters(strip_option, into)]
    pub accessibility_description: Option<String>,
}

impl Default for CircularProgressIndicatorArgs {
    fn default() -> Self {
        let scheme = use_context::<MaterialTheme>()
            .expect("MaterialTheme must be provided")
            .get()
            .color_scheme;
        Self {
            progress: None,
            diameter: ProgressIndicatorDefaults::CIRCULAR_INDICATOR_DIAMETER,
            stroke_width: ProgressIndicatorDefaults::CIRCULAR_STROKE_WIDTH,
            color: scheme.primary,
            track_color: scheme.secondary_container,
            stroke_cap: ProgressStrokeCap::default(),
            gap_size: ProgressIndicatorDefaults::CIRCULAR_INDICATOR_TRACK_GAP_SIZE,
            accessibility_label: None,
            accessibility_description: None,
        }
    }
}

fn circular_gap_sweep_degrees(diameter: Dp, stroke_width: Dp, gap_size: Dp, is_butt: bool) -> f32 {
    let adjusted_gap = if is_butt {
        gap_size
    } else {
        Dp(gap_size.0 + stroke_width.0)
    };
    let diameter_dp = diameter.0 as f32;
    let gap_dp = adjusted_gap.0 as f32;
    let circumference = std::f32::consts::PI * diameter_dp.max(0.0001);
    (gap_dp / circumference) * 360.0
}

fn circular_additional_rotation_degrees(cycle_ms: f32) -> f32 {
    fn segment(t: f32, start_ms: f32, end_ms: f32, from: f32, to: f32) -> Option<f32> {
        if t < start_ms {
            None
        } else if t >= end_ms {
            Some(to)
        } else {
            let local = (t - start_ms) / (end_ms - start_ms);
            Some(lerp(from, to, emphasized_decelerate(local)))
        }
    }

    let t = cycle_ms;
    if let Some(v) = segment(t, 0.0, 300.0, 0.0, 90.0) {
        return v;
    }
    if t < 1500.0 {
        return 90.0;
    }
    if let Some(v) = segment(t, 1500.0, 1800.0, 90.0, 180.0) {
        return v;
    }
    if t < 3000.0 {
        return 180.0;
    }
    if let Some(v) = segment(t, 3000.0, 3300.0, 180.0, 270.0) {
        return v;
    }
    if t < 4500.0 {
        return 270.0;
    }
    if let Some(v) = segment(t, 4500.0, 4800.0, 270.0, 360.0) {
        return v;
    }
    360.0
}

fn circular_indeterminate_progress(cycle_ms: f32) -> f32 {
    const MIN_PROGRESS: f32 = 0.1;
    const MAX_PROGRESS: f32 = 0.87;
    if cycle_ms <= 3000.0 {
        let t = (cycle_ms / 3000.0).clamp(0.0, 1.0);
        lerp(MIN_PROGRESS, MAX_PROGRESS, standard_easing(t))
    } else {
        let t = ((cycle_ms - 3000.0) / 3000.0).clamp(0.0, 1.0);
        lerp(MAX_PROGRESS, MIN_PROGRESS, t)
    }
}

/// # circular_progress_indicator
///
/// Renders a Material Design progress indicator in a circular form.
///
/// ## Usage
///
/// Show ongoing activity or a completion fraction for tasks such as loading,
/// syncing, or background work.
///
/// ## Parameters
///
/// - `args` — configures the indicator; see [`CircularProgressIndicatorArgs`].
///
/// ## Examples
///
/// ```
/// # use tessera_ui::tessera;
/// # #[tessera]
/// # fn component() {
/// use tessera_components::progress::{
///     CircularProgressIndicatorArgs, circular_progress_indicator,
/// };
/// # use tessera_components::theme::{MaterialTheme, material_theme};
///
/// # material_theme(
/// #     || MaterialTheme::default(),
/// #     || {
/// circular_progress_indicator(CircularProgressIndicatorArgs::default().progress(0.6));
/// #     },
/// # );
/// # }
/// # component();
/// ```
#[tessera]
pub fn circular_progress_indicator(args: impl Into<CircularProgressIndicatorArgs>) {
    let args: CircularProgressIndicatorArgs = args.into();
    let args_for_accessibility = args.clone();
    let animation_start = remember(Instant::now);

    let mut semantics = SemanticsArgs::new().role(Role::ProgressIndicator);
    if let Some(label) = args_for_accessibility.accessibility_label.clone() {
        semantics = semantics.label(label);
    }
    if let Some(description) = args_for_accessibility.accessibility_description.clone() {
        semantics = semantics.description(description);
    }
    if let Some(progress) = args_for_accessibility.progress {
        let progress = if progress.is_nan() {
            0.0
        } else {
            progress.clamp(0.0, 1.0)
        };
        semantics = semantics
            .numeric_range(0.0, 1.0)
            .numeric_value(progress as f64);
    }

    Modifier::new().semantics(semantics).run(move || {
        let animation_start = animation_start.get();
        layout(CircularProgressLayout {
            progress: args.progress,
            diameter: args.diameter,
            stroke_width: args.stroke_width,
            color: args.color,
            track_color: args.track_color,
            stroke_cap: args.stroke_cap,
            gap_size: args.gap_size,
            animation_start,
        });
    });
}

/// Arguments for the `progress` component.
#[derive(Clone, Debug, Setters)]
pub struct ProgressArgs {
    /// The current value of the progress bar, ranging from 0.0 to 1.0.
    pub value: f32,

    /// Modifier chain applied to the progress bar subtree.
    pub modifier: Modifier,

    /// The color of the active part of the track.
    pub progress_color: Color,

    /// The color of the inactive part of the track.
    pub track_color: Color,
}

impl Default for ProgressArgs {
    fn default() -> Self {
        let scheme = use_context::<MaterialTheme>()
            .expect("MaterialTheme must be provided")
            .get()
            .color_scheme;
        Self {
            value: 0.0,
            modifier: Modifier::new().size(
                ProgressIndicatorDefaults::LINEAR_INDICATOR_WIDTH,
                ProgressIndicatorDefaults::LINEAR_INDICATOR_HEIGHT,
            ),
            progress_color: scheme.primary,
            track_color: scheme.surface_variant,
        }
    }
}

/// # progress
///
/// Renders a linear progress indicator that visualizes a value from 0.0 to 1.0.
///
/// ## Usage
///
/// Display the status of an ongoing operation, such as a download or a setup
/// process.
///
/// ## Parameters
///
/// - `args` — configures the progress bar's value and appearance; see
///   [`ProgressArgs`].
///
/// ## Examples
///
/// ```
/// # use tessera_ui::tessera;
/// # #[tessera]
/// # fn component() {
/// use tessera_components::progress::{ProgressArgs, progress};
/// # use tessera_components::theme::{MaterialTheme, material_theme};
///
/// // Creates a progress bar that is 75% complete.
/// # material_theme(
/// #     || MaterialTheme::default(),
/// #     || {
/// progress(ProgressArgs::default().value(0.75));
/// #     },
/// # );
/// # }
/// # component();
/// ```
#[tessera]
pub fn progress(args: impl Into<ProgressArgs>) {
    let args: ProgressArgs = args.into();

    linear_progress_indicator(
        LinearProgressIndicatorArgs::default()
            .progress(args.value)
            .modifier(args.modifier)
            .color(args.progress_color)
            .track_color(args.track_color),
    );
}