1use std::any::Any;
12use std::f64::consts::PI;
13use std::fmt::Debug;
14use std::rc::Rc;
15
16use waft_animation::AnyAnimation;
17use waft_embedder::{
18 Canvas, Color, FillRule, Offset, Paint, PaintStyle, PathBuilder, RRect, Radius, Rect, Size,
19 Stroke, TextDirection, clamp_double, rrect_radii_elliptical,
20};
21use waft_foundation::App;
22use waft_painting::{ClipContext, PaintingBinding, TextPainter};
23use waft_rendering::{AnyRenderBox, PaintingContext};
24
25use crate::range_slider_parts::RangeSliderValueIndicatorShape;
26use crate::slider_parts::{evaluate_color_tween, evaluate_tween};
27use crate::slider_theme::{SliderThemeData, Thumb};
28
29pub trait SliderComponentShape: Any + Debug {
54 fn get_preferred_size(&self, is_enabled: bool, is_discrete: bool) -> Size;
56
57 #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
106 fn paint(
107 &self,
108 app: &mut App,
109 context: &mut PaintingContext,
110 center: Offset,
111 activation_animation: AnyAnimation<f64>,
112 enable_animation: AnyAnimation<f64>,
113 is_discrete: bool,
114 label_painter: &mut TextPainter,
115 parent_box: AnyRenderBox,
116 slider_theme: &SliderThemeData,
117 text_direction: TextDirection,
118 value: f64,
119 text_scale_factor: f64,
120 size_with_overflow: Size,
121 );
122
123 fn as_any(&self) -> &dyn Any;
125
126 fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
128 std::ptr::eq(self.as_any(), other.as_any())
129 }
130}
131
132pub type SliderComponentShapeRef = Rc<dyn SliderComponentShape>;
134
135impl PartialEq for dyn SliderComponentShape {
136 fn eq(&self, other: &dyn SliderComponentShape) -> bool {
137 self.eq_shape(other)
138 }
139}
140
141thread_local! {
142 static NO_THUMB: SliderComponentShapeRef = Rc::new(EmptySliderComponentShape);
143 static NO_OVERLAY: SliderComponentShapeRef = Rc::new(EmptySliderComponentShape);
144}
145
146impl dyn SliderComponentShape {
147 pub fn no_thumb() -> SliderComponentShapeRef {
156 NO_THUMB.with(Rc::clone)
157 }
158
159 pub fn no_overlay() -> SliderComponentShapeRef {
168 NO_OVERLAY.with(Rc::clone)
169 }
170}
171
172#[derive(Debug)]
183struct EmptySliderComponentShape;
184
185impl SliderComponentShape for EmptySliderComponentShape {
186 fn get_preferred_size(&self, _is_enabled: bool, _is_discrete: bool) -> Size {
187 Size::ZERO
188 }
189
190 fn paint(
191 &self,
192 _app: &mut App,
193 _context: &mut PaintingContext,
194 _center: Offset,
195 _activation_animation: AnyAnimation<f64>,
196 _enable_animation: AnyAnimation<f64>,
197 _is_discrete: bool,
198 _label_painter: &mut TextPainter,
199 _parent_box: AnyRenderBox,
200 _slider_theme: &SliderThemeData,
201 _text_direction: TextDirection,
202 _value: f64,
203 _text_scale_factor: f64,
204 _size_with_overflow: Size,
205 ) {
206 }
208
209 fn as_any(&self) -> &dyn Any {
210 self
211 }
212}
213
214#[derive(Clone, Copy, Debug, PartialEq)]
230pub struct RoundSliderOverlayShape {
231 pub overlay_radius: f64,
236}
237
238impl RoundSliderOverlayShape {
239 pub const fn new() -> RoundSliderOverlayShape {
241 RoundSliderOverlayShape {
242 overlay_radius: 24.0,
243 }
244 }
245
246 pub const fn overlay_radius(mut self, overlay_radius: f64) -> RoundSliderOverlayShape {
248 self.overlay_radius = overlay_radius;
249 self
250 }
251}
252
253impl Default for RoundSliderOverlayShape {
254 fn default() -> RoundSliderOverlayShape {
255 RoundSliderOverlayShape::new()
256 }
257}
258
259impl SliderComponentShape for RoundSliderOverlayShape {
260 fn get_preferred_size(&self, _is_enabled: bool, _is_discrete: bool) -> Size {
261 Size::from_radius(self.overlay_radius)
262 }
263
264 fn paint(
265 &self,
266 app: &mut App,
267 context: &mut PaintingContext,
268 center: Offset,
269 activation_animation: AnyAnimation<f64>,
270 _enable_animation: AnyAnimation<f64>,
271 _is_discrete: bool,
272 _label_painter: &mut TextPainter,
273 _parent_box: AnyRenderBox,
274 slider_theme: &SliderThemeData,
275 _text_direction: TextDirection,
276 _value: f64,
277 _text_scale_factor: f64,
278 _size_with_overflow: Size,
279 ) {
280 let radius = evaluate_tween(app, 0.0, self.overlay_radius, activation_animation);
281 let color = slider_theme
282 .overlay_color
283 .as_ref()
284 .expect("a slider theme with an overlay color")
285 .color();
286 context
287 .canvas()
288 .draw_circle(center, radius as f32, &Paint::from_color(color.into()));
289 }
290
291 fn as_any(&self) -> &dyn Any {
292 self
293 }
294
295 fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
297 other.as_any().downcast_ref::<Self>() == Some(self)
298 }
299}
300
301#[derive(Clone, Copy, Debug, Default, PartialEq)]
309pub struct RectangularSliderValueIndicatorShape;
310
311impl RectangularSliderValueIndicatorShape {
312 const PATH_PAINTER: RectangularSliderValueIndicatorPathPainter =
313 RectangularSliderValueIndicatorPathPainter;
314
315 pub const fn new() -> RectangularSliderValueIndicatorShape {
317 RectangularSliderValueIndicatorShape
318 }
319
320 pub fn get_preferred_size(
323 &self,
324 _is_enabled: bool,
325 _is_discrete: bool,
326 label_painter: Option<&TextPainter>,
327 text_scale_factor: Option<f64>,
328 ) -> Size {
329 debug_assert!(label_painter.is_some());
330 debug_assert!(text_scale_factor.is_some_and(|factor| factor >= 0.0));
331 Self::PATH_PAINTER.get_preferred_size(
332 label_painter.expect("a label painter"),
333 text_scale_factor.expect("a text scale factor"),
334 )
335 }
336}
337
338impl SliderComponentShape for RectangularSliderValueIndicatorShape {
339 fn get_preferred_size(&self, is_enabled: bool, is_discrete: bool) -> Size {
340 RectangularSliderValueIndicatorShape::get_preferred_size(
341 self,
342 is_enabled,
343 is_discrete,
344 None,
345 None,
346 )
347 }
348
349 fn paint(
350 &self,
351 app: &mut App,
352 context: &mut PaintingContext,
353 center: Offset,
354 activation_animation: AnyAnimation<f64>,
355 _enable_animation: AnyAnimation<f64>,
356 _is_discrete: bool,
357 label_painter: &mut TextPainter,
358 parent_box: AnyRenderBox,
359 slider_theme: &SliderThemeData,
360 _text_direction: TextDirection,
361 _value: f64,
362 text_scale_factor: f64,
363 size_with_overflow: Size,
364 ) {
365 let scale = activation_animation.value(app);
366 Self::PATH_PAINTER.paint(
367 app,
368 parent_box,
369 context.canvas(),
370 center,
371 scale,
372 label_painter,
373 text_scale_factor,
374 size_with_overflow,
375 slider_theme
376 .value_indicator_color
377 .expect("a slider theme with a value indicator color"),
378 slider_theme.value_indicator_stroke_color,
379 );
380 }
381
382 fn as_any(&self) -> &dyn Any {
383 self
384 }
385
386 fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
388 other.as_any().downcast_ref::<Self>() == Some(self)
389 }
390}
391
392#[derive(Clone, Copy, Debug, Default, PartialEq)]
400pub struct RectangularRangeSliderValueIndicatorShape;
401
402impl RectangularRangeSliderValueIndicatorShape {
403 const PATH_PAINTER: RectangularSliderValueIndicatorPathPainter =
404 RectangularSliderValueIndicatorPathPainter;
405
406 pub const fn new() -> RectangularRangeSliderValueIndicatorShape {
408 RectangularRangeSliderValueIndicatorShape
409 }
410}
411
412impl RangeSliderValueIndicatorShape for RectangularRangeSliderValueIndicatorShape {
413 fn get_preferred_size(
414 &self,
415 _is_enabled: bool,
416 _is_discrete: bool,
417 label_painter: &TextPainter,
418 text_scale_factor: f64,
419 ) -> Size {
420 debug_assert!(text_scale_factor >= 0.0);
421 Self::PATH_PAINTER.get_preferred_size(label_painter, text_scale_factor)
422 }
423
424 fn get_horizontal_shift(
425 &self,
426 app: &App,
427 parent_box: Option<AnyRenderBox>,
428 center: Option<Offset>,
429 label_painter: Option<&TextPainter>,
430 activation_animation: Option<AnyAnimation<f64>>,
431 text_scale_factor: Option<f64>,
432 size_with_overflow: Option<Size>,
433 ) -> f64 {
434 Self::PATH_PAINTER.get_horizontal_shift(
435 app,
436 parent_box.expect("a parent box"),
437 center.expect("a center"),
438 label_painter.expect("a label painter"),
439 text_scale_factor.expect("a text scale factor"),
440 size_with_overflow.expect("a size with overflow"),
441 activation_animation
442 .expect("an activation animation")
443 .value(app),
444 )
445 }
446
447 fn paint(
448 &self,
449 app: &mut App,
450 context: &mut PaintingContext,
451 center: Offset,
452 activation_animation: AnyAnimation<f64>,
453 _enable_animation: AnyAnimation<f64>,
454 _is_discrete: Option<bool>,
455 is_on_top: Option<bool>,
456 label_painter: &mut TextPainter,
457 text_scale_factor: Option<f64>,
458 size_with_overflow: Option<Size>,
459 parent_box: AnyRenderBox,
460 slider_theme: &SliderThemeData,
461 _text_direction: Option<TextDirection>,
462 _value: Option<f64>,
463 _thumb: Option<Thumb>,
464 ) {
465 let scale = activation_animation.value(app);
466 Self::PATH_PAINTER.paint(
467 app,
468 parent_box,
469 context.canvas(),
470 center,
471 scale,
472 label_painter,
473 text_scale_factor.expect("a text scale factor"),
474 size_with_overflow.expect("a size with overflow"),
475 slider_theme
476 .value_indicator_color
477 .expect("a slider theme with a value indicator color"),
478 if is_on_top.expect("whether this indicator is on top") {
479 slider_theme.overlapping_shape_stroke_color
480 } else {
481 slider_theme.value_indicator_stroke_color
482 },
483 );
484 }
485
486 fn as_any(&self) -> &dyn Any {
487 self
488 }
489
490 fn eq_shape(&self, other: &dyn RangeSliderValueIndicatorShape) -> bool {
492 other.as_any().downcast_ref::<Self>() == Some(self)
493 }
494}
495
496struct RectangularSliderValueIndicatorPathPainter;
498
499impl RectangularSliderValueIndicatorPathPainter {
500 const TRIANGLE_HEIGHT: f64 = 8.0;
501 const LABEL_PADDING: f64 = 16.0;
502 const PREFERRED_HEIGHT: f64 = 32.0;
503 const MIN_LABEL_WIDTH: f64 = 16.0;
504 const BOTTOM_TIP_Y_OFFSET: f64 = 14.0;
505 const PREFERRED_HALF_HEIGHT: f64 = Self::PREFERRED_HEIGHT / 2.0;
506 const UPPER_RECT_RADIUS: f64 = 4.0;
507
508 fn get_preferred_size(&self, label_painter: &TextPainter, text_scale_factor: f64) -> Size {
509 Size::new(
510 self.upper_rectangle_width(label_painter, 1.0, text_scale_factor),
511 label_painter.height() + Self::LABEL_PADDING,
512 )
513 }
514
515 #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
516 fn get_horizontal_shift(
517 &self,
518 app: &App,
519 parent_box: AnyRenderBox,
520 center: Offset,
521 label_painter: &TextPainter,
522 text_scale_factor: f64,
523 size_with_overflow: Size,
524 scale: f64,
525 ) -> f64 {
526 debug_assert!(!size_with_overflow.is_empty());
527
528 const EDGE_PADDING: f64 = 8.0;
529 let rectangle_width = self.upper_rectangle_width(label_painter, scale, text_scale_factor);
530
531 let global_center = parent_box.local_to_global(app, center, None);
534
535 let overflow_left = 0.0_f64.max(rectangle_width / 2.0 - global_center.dx() + EDGE_PADDING);
540 let overflow_right = 0.0_f64.max(
541 rectangle_width / 2.0
542 - (size_with_overflow.width() - global_center.dx() - EDGE_PADDING),
543 );
544
545 if rectangle_width < size_with_overflow.width() {
546 overflow_left - overflow_right
547 } else if overflow_left - overflow_right > 0.0 {
548 overflow_left - (EDGE_PADDING * text_scale_factor)
549 } else {
550 -overflow_right + (EDGE_PADDING * text_scale_factor)
551 }
552 }
553
554 fn upper_rectangle_width(
555 &self,
556 label_painter: &TextPainter,
557 scale: f64,
558 text_scale_factor: f64,
559 ) -> f64 {
560 let unscaled_width = (Self::MIN_LABEL_WIDTH * text_scale_factor).max(label_painter.width())
561 + Self::LABEL_PADDING * 2.0;
562 unscaled_width * scale
563 }
564
565 #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
566 fn paint(
567 &self,
568 app: &mut App,
569 parent_box: AnyRenderBox,
570 canvas: &mut Canvas,
571 center: Offset,
572 scale: f64,
573 label_painter: &mut TextPainter,
574 text_scale_factor: f64,
575 size_with_overflow: Size,
576 background_paint_color: Color,
577 stroke_paint_color: Option<Color>,
578 ) {
579 if scale == 0.0 {
580 return;
582 }
583 debug_assert!(!size_with_overflow.is_empty());
584
585 let rectangle_width = self.upper_rectangle_width(label_painter, scale, text_scale_factor);
586 let horizontal_shift = self.get_horizontal_shift(
587 app,
588 parent_box,
589 center,
590 label_painter,
591 text_scale_factor,
592 size_with_overflow,
593 scale,
594 );
595
596 let rect_height = label_painter.height() + Self::LABEL_PADDING;
597 let upper_rect = Rect::from_ltwh(
598 -rectangle_width / 2.0 + horizontal_shift,
599 -Self::TRIANGLE_HEIGHT - rect_height,
600 rectangle_width,
601 rect_height,
602 );
603
604 let mut triangle_path = PathBuilder::new();
605 triangle_path.move_to((0.0, 0.0));
607 triangle_path
608 .line_to((-Self::TRIANGLE_HEIGHT as f32, -Self::TRIANGLE_HEIGHT as f32))
609 .line_to((Self::TRIANGLE_HEIGHT as f32, -Self::TRIANGLE_HEIGHT as f32))
610 .close();
611 let fill_paint = Paint::from_color(background_paint_color.into());
612 let upper_rrect =
613 RRect::from_rect_and_radius(upper_rect, Radius::circular(Self::UPPER_RECT_RADIUS));
614 triangle_path.rrect_radii_elliptical(
615 upper_rrect.outer_rect(),
616 rrect_radii_elliptical(upper_rrect),
617 );
618 let triangle_path = triangle_path.build();
619
620 canvas.save();
621 canvas.translate(
624 center.dx() as f32,
625 (center.dy() - Self::BOTTOM_TIP_Y_OFFSET) as f32,
626 );
627 canvas.scale(scale as f32, scale as f32);
628 if let Some(stroke_paint_color) = stroke_paint_color {
629 let stroke_paint = Paint {
630 color: stroke_paint_color.into(),
631 style: PaintStyle::Stroke(Stroke::new(1.0)),
632 ..Paint::default()
633 };
634 canvas.draw_path(&triangle_path, FillRule::NonZero, &stroke_paint);
635 }
636 canvas.draw_path(&triangle_path, FillRule::NonZero, &fill_paint);
637
638 let bottom_tip_to_upper_rect_translate_y =
640 -Self::PREFERRED_HALF_HEIGHT / 2.0 - upper_rect.height();
641 canvas.translate(0.0, bottom_tip_to_upper_rect_translate_y as f32);
642 let box_center = Offset::new(horizontal_shift, upper_rect.height() / 2.0);
643 let half_label_painter_offset =
644 Offset::new(label_painter.width() / 2.0, label_painter.height() / 2.0);
645 let label_offset = box_center - half_label_painter_offset;
646 let fonts = PaintingBinding::instance(app).fonts(app);
647 label_painter.paint(app.get_mut(fonts), canvas, label_offset);
648 canvas.restore();
649 }
650}
651
652#[derive(Clone, Copy, Debug, Default, PartialEq)]
661pub struct PaddleSliderValueIndicatorShape;
662
663impl PaddleSliderValueIndicatorShape {
664 const PATH_PAINTER: PaddleSliderValueIndicatorPathPainter =
665 PaddleSliderValueIndicatorPathPainter;
666
667 pub const fn new() -> PaddleSliderValueIndicatorShape {
669 PaddleSliderValueIndicatorShape
670 }
671
672 pub fn get_preferred_size(
675 &self,
676 _is_enabled: bool,
677 _is_discrete: bool,
678 label_painter: Option<&TextPainter>,
679 text_scale_factor: Option<f64>,
680 ) -> Size {
681 debug_assert!(label_painter.is_some());
682 debug_assert!(text_scale_factor.is_some_and(|factor| factor >= 0.0));
683 Self::PATH_PAINTER.get_preferred_size(
684 label_painter.expect("a label painter"),
685 text_scale_factor.expect("a text scale factor"),
686 )
687 }
688}
689
690impl SliderComponentShape for PaddleSliderValueIndicatorShape {
691 fn get_preferred_size(&self, is_enabled: bool, is_discrete: bool) -> Size {
692 PaddleSliderValueIndicatorShape::get_preferred_size(
693 self,
694 is_enabled,
695 is_discrete,
696 None,
697 None,
698 )
699 }
700
701 fn paint(
702 &self,
703 app: &mut App,
704 context: &mut PaintingContext,
705 center: Offset,
706 activation_animation: AnyAnimation<f64>,
707 enable_animation: AnyAnimation<f64>,
708 _is_discrete: bool,
709 label_painter: &mut TextPainter,
710 _parent_box: AnyRenderBox,
711 slider_theme: &SliderThemeData,
712 _text_direction: TextDirection,
713 _value: f64,
714 text_scale_factor: f64,
715 size_with_overflow: Size,
716 ) {
717 debug_assert!(!size_with_overflow.is_empty());
718 let paint = Paint::from_color(
719 evaluate_color_tween(
720 app,
721 slider_theme.disabled_thumb_color,
722 slider_theme.value_indicator_color,
723 enable_animation,
724 )
725 .expect("both value indicator colors are set")
726 .into(),
727 );
728 let scale = activation_animation.value(app);
729 Self::PATH_PAINTER.paint(
730 app,
731 context.canvas(),
732 center,
733 paint,
734 scale,
735 label_painter,
736 text_scale_factor,
737 size_with_overflow,
738 slider_theme.value_indicator_stroke_color,
739 );
740 }
741
742 fn as_any(&self) -> &dyn Any {
743 self
744 }
745
746 fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
748 other.as_any().downcast_ref::<Self>() == Some(self)
749 }
750}
751
752#[derive(Clone, Copy, Debug, Default, PartialEq)]
761pub struct PaddleRangeSliderValueIndicatorShape;
762
763impl PaddleRangeSliderValueIndicatorShape {
764 const PATH_PAINTER: PaddleSliderValueIndicatorPathPainter =
765 PaddleSliderValueIndicatorPathPainter;
766
767 pub const fn new() -> PaddleRangeSliderValueIndicatorShape {
769 PaddleRangeSliderValueIndicatorShape
770 }
771}
772
773impl RangeSliderValueIndicatorShape for PaddleRangeSliderValueIndicatorShape {
774 fn get_preferred_size(
775 &self,
776 _is_enabled: bool,
777 _is_discrete: bool,
778 label_painter: &TextPainter,
779 text_scale_factor: f64,
780 ) -> Size {
781 debug_assert!(text_scale_factor >= 0.0);
782 Self::PATH_PAINTER.get_preferred_size(label_painter, text_scale_factor)
783 }
784
785 fn get_horizontal_shift(
786 &self,
787 app: &App,
788 _parent_box: Option<AnyRenderBox>,
789 center: Option<Offset>,
790 label_painter: Option<&TextPainter>,
791 activation_animation: Option<AnyAnimation<f64>>,
792 text_scale_factor: Option<f64>,
793 size_with_overflow: Option<Size>,
794 ) -> f64 {
795 Self::PATH_PAINTER.get_horizontal_shift(
796 center.expect("a center"),
797 label_painter.expect("a label painter"),
798 activation_animation
799 .expect("an activation animation")
800 .value(app),
801 text_scale_factor.expect("a text scale factor"),
802 size_with_overflow.expect("a size with overflow"),
803 )
804 }
805
806 fn paint(
807 &self,
808 app: &mut App,
809 context: &mut PaintingContext,
810 center: Offset,
811 activation_animation: AnyAnimation<f64>,
812 enable_animation: AnyAnimation<f64>,
813 _is_discrete: Option<bool>,
814 is_on_top: Option<bool>,
815 label_painter: &mut TextPainter,
816 text_scale_factor: Option<f64>,
817 size_with_overflow: Option<Size>,
818 _parent_box: AnyRenderBox,
819 slider_theme: &SliderThemeData,
820 _text_direction: Option<TextDirection>,
821 _value: Option<f64>,
822 _thumb: Option<Thumb>,
823 ) {
824 let is_on_top = is_on_top.unwrap_or(false);
825 let size_with_overflow = size_with_overflow.expect("a size with overflow");
826 debug_assert!(!size_with_overflow.is_empty());
827 let paint = Paint::from_color(
828 evaluate_color_tween(
829 app,
830 slider_theme.disabled_thumb_color,
831 slider_theme.value_indicator_color,
832 enable_animation,
833 )
834 .expect("both value indicator colors are set")
835 .into(),
836 );
837 let scale = activation_animation.value(app);
838 Self::PATH_PAINTER.paint(
840 app,
841 context.canvas(),
842 center,
843 paint,
844 scale,
845 label_painter,
846 text_scale_factor.expect("a text scale factor"),
847 size_with_overflow,
848 if is_on_top {
849 slider_theme.overlapping_shape_stroke_color
850 } else {
851 slider_theme.value_indicator_stroke_color
852 },
853 );
854 }
855
856 fn as_any(&self) -> &dyn Any {
857 self
858 }
859
860 fn eq_shape(&self, other: &dyn RangeSliderValueIndicatorShape) -> bool {
862 other.as_any().downcast_ref::<Self>() == Some(self)
863 }
864}
865
866struct PaddleSliderValueIndicatorPathPainter;
868
869impl PaddleSliderValueIndicatorPathPainter {
870 const TOP_LOBE_RADIUS: f64 = 16.0;
878 const MIN_LABEL_WIDTH: f64 = 16.0;
879 const BOTTOM_LOBE_RADIUS: f64 = 10.0;
881 const LABEL_PADDING: f64 = 8.0;
882 const DISTANCE_BETWEEN_TOP_BOTTOM_CENTERS: f64 = 40.0;
883 const MIDDLE_NECK_WIDTH: f64 = 3.0;
884 const BOTTOM_NECK_RADIUS: f64 = 4.5;
885 const NECK_TRIANGLE_BASE: f64 = Self::TOP_NECK_RADIUS + Self::MIDDLE_NECK_WIDTH / 2.0;
888 const RIGHT_BOTTOM_NECK_CENTER_X: f64 =
889 Self::MIDDLE_NECK_WIDTH / 2.0 + Self::BOTTOM_NECK_RADIUS;
890 const RIGHT_BOTTOM_NECK_ANGLE_START: f64 = PI;
891 const TOP_LOBE_CENTER: Offset = Offset::new(0.0, -Self::DISTANCE_BETWEEN_TOP_BOTTOM_CENTERS);
892 const TOP_NECK_RADIUS: f64 = 13.0;
893 const NECK_TRIANGLE_HYPOTENUSE: f64 = Self::TOP_LOBE_RADIUS + Self::TOP_NECK_RADIUS;
897 const TWO_SEVENTY_DEGREES: f64 = 3.0 * PI / 2.0;
899 const NINETY_DEGREES: f64 = PI / 2.0;
900 const THIRTY_DEGREES: f64 = PI / 6.0;
901 const PREFERRED_HEIGHT: f64 = Self::DISTANCE_BETWEEN_TOP_BOTTOM_CENTERS
902 + Self::TOP_LOBE_RADIUS
903 + Self::BOTTOM_LOBE_RADIUS;
904 const DEBUGGING_LABEL_LOCATION: bool = false;
909
910 fn get_preferred_size(&self, label_painter: &TextPainter, text_scale_factor: f64) -> Size {
911 debug_assert!(text_scale_factor >= 0.0);
912 let width = (Self::MIN_LABEL_WIDTH * text_scale_factor).max(label_painter.width())
913 + Self::LABEL_PADDING * 2.0 * text_scale_factor;
914 Size::new(width, Self::PREFERRED_HEIGHT * text_scale_factor)
915 }
916
917 fn add_arc(
920 path: &mut PathBuilder,
921 center: Offset,
922 radius: f64,
923 start_angle: f64,
924 end_angle: f64,
925 ) {
926 debug_assert!(center.is_finite());
927 path.arc(
928 center,
929 radius as f32,
930 start_angle as f32,
931 (end_angle - start_angle) as f32,
932 );
933 }
934
935 fn get_horizontal_shift(
936 &self,
937 center: Offset,
938 label_painter: &TextPainter,
939 scale: f64,
940 text_scale_factor: f64,
941 size_with_overflow: Size,
942 ) -> f64 {
943 debug_assert!(!size_with_overflow.is_empty());
944 let inverse_text_scale = if text_scale_factor != 0.0 {
945 1.0 / text_scale_factor
946 } else {
947 0.0
948 };
949 let label_half_width = label_painter.width() / 2.0;
950 let half_width_needed = 0.0_f64.max(
951 inverse_text_scale * label_half_width - (Self::TOP_LOBE_RADIUS - Self::LABEL_PADDING),
952 );
953 let shift = self.get_ideal_offset(
954 half_width_needed,
955 text_scale_factor * scale,
956 center,
957 size_with_overflow.width(),
958 );
959 shift * text_scale_factor
960 }
961
962 fn get_ideal_offset(
965 &self,
966 half_width_needed: f64,
967 scale: f64,
968 center: Offset,
969 width_with_overflow: f64,
970 ) -> f64 {
971 const EDGE_MARGIN: f64 = 8.0;
972 let top_lobe_rect = Rect::from_ltwh(
973 -Self::TOP_LOBE_RADIUS - half_width_needed,
974 -Self::TOP_LOBE_RADIUS - Self::DISTANCE_BETWEEN_TOP_BOTTOM_CENTERS,
975 2.0 * (Self::TOP_LOBE_RADIUS + half_width_needed),
976 2.0 * Self::TOP_LOBE_RADIUS,
977 );
978 let top_left = (top_lobe_rect.top_left() * scale) + center;
981 let bottom_right = (top_lobe_rect.bottom_right() * scale) + center;
982 let mut shift = 0.0;
983
984 if top_left.dx() < EDGE_MARGIN {
985 shift = EDGE_MARGIN - top_left.dx();
986 }
987
988 let end_global = width_with_overflow;
989 if bottom_right.dx() > end_global - EDGE_MARGIN {
990 shift = end_global - EDGE_MARGIN - bottom_right.dx();
991 }
992
993 shift = if scale == 0.0 { 0.0 } else { shift / scale };
994 if shift < 0.0 {
995 shift = shift.max(-half_width_needed);
997 } else {
998 shift = shift.min(half_width_needed);
1000 }
1001 shift
1002 }
1003
1004 #[expect(clippy::too_many_arguments, reason = "Dart's parameters, in its order")]
1005 fn paint(
1006 &self,
1007 app: &mut App,
1008 canvas: &mut Canvas,
1009 center: Offset,
1010 paint: Paint,
1011 scale: f64,
1012 label_painter: &mut TextPainter,
1013 text_scale_factor: f64,
1014 size_with_overflow: Size,
1015 stroke_paint_color: Option<Color>,
1016 ) {
1017 if scale == 0.0 {
1018 return;
1021 }
1022 debug_assert!(!size_with_overflow.is_empty());
1023
1024 let overall_scale = scale * text_scale_factor;
1027 let inverse_text_scale = if text_scale_factor != 0.0 {
1028 1.0 / text_scale_factor
1029 } else {
1030 0.0
1031 };
1032 let label_half_width = label_painter.width() / 2.0;
1033
1034 canvas.save();
1035 canvas.translate(center.dx() as f32, center.dy() as f32);
1036 canvas.scale(overall_scale as f32, overall_scale as f32);
1037
1038 let bottom_neck_triangle_hypotenuse =
1039 Self::BOTTOM_NECK_RADIUS + Self::BOTTOM_LOBE_RADIUS / overall_scale;
1040 let right_bottom_neck_center_y = -(bottom_neck_triangle_hypotenuse.powi(2)
1041 - Self::RIGHT_BOTTOM_NECK_CENTER_X.powi(2))
1042 .sqrt();
1043 let right_bottom_neck_angle_end =
1044 PI + (right_bottom_neck_center_y / Self::RIGHT_BOTTOM_NECK_CENTER_X).atan();
1045 let mut path = PathBuilder::new();
1046 path.move_to((
1047 (Self::MIDDLE_NECK_WIDTH / 2.0) as f32,
1048 right_bottom_neck_center_y as f32,
1049 ));
1050 Self::add_arc(
1051 &mut path,
1052 Offset::new(Self::RIGHT_BOTTOM_NECK_CENTER_X, right_bottom_neck_center_y),
1053 Self::BOTTOM_NECK_RADIUS,
1054 Self::RIGHT_BOTTOM_NECK_ANGLE_START,
1055 right_bottom_neck_angle_end,
1056 );
1057 Self::add_arc(
1058 &mut path,
1059 Offset::ZERO,
1060 Self::BOTTOM_LOBE_RADIUS / overall_scale,
1061 right_bottom_neck_angle_end - PI,
1062 2.0 * PI - right_bottom_neck_angle_end,
1063 );
1064 Self::add_arc(
1065 &mut path,
1066 Offset::new(
1067 -Self::RIGHT_BOTTOM_NECK_CENTER_X,
1068 right_bottom_neck_center_y,
1069 ),
1070 Self::BOTTOM_NECK_RADIUS,
1071 PI - right_bottom_neck_angle_end,
1072 0.0,
1073 );
1074
1075 let half_width_needed = 0.0_f64.max(
1078 inverse_text_scale * label_half_width - (Self::TOP_LOBE_RADIUS - Self::LABEL_PADDING),
1079 );
1080
1081 let shift = self.get_ideal_offset(
1082 half_width_needed,
1083 overall_scale,
1084 center,
1085 size_with_overflow.width(),
1086 );
1087 let left_width_needed = half_width_needed - shift;
1088 let right_width_needed = half_width_needed + shift;
1089
1090 let left_amount = 0.0_f64.max(1.0_f64.min(left_width_needed / Self::NECK_TRIANGLE_BASE));
1093 let right_amount = 0.0_f64.max(1.0_f64.min(right_width_needed / Self::NECK_TRIANGLE_BASE));
1094 let left_theta = (1.0 - left_amount) * Self::THIRTY_DEGREES;
1098 let right_theta = (1.0 - right_amount) * Self::THIRTY_DEGREES;
1099 let left_top_neck_center = Offset::new(
1101 -Self::NECK_TRIANGLE_BASE,
1102 Self::TOP_LOBE_CENTER.dy() + left_theta.cos() * Self::NECK_TRIANGLE_HYPOTENUSE,
1103 );
1104 let neck_right_center = Offset::new(
1105 Self::NECK_TRIANGLE_BASE,
1106 Self::TOP_LOBE_CENTER.dy() + right_theta.cos() * Self::NECK_TRIANGLE_HYPOTENUSE,
1107 );
1108 let left_neck_arc_angle = Self::NINETY_DEGREES - left_theta;
1109 let right_neck_arc_angle = PI + Self::NINETY_DEGREES - right_theta;
1110 let neck_stretch_baseline = 0.0_f64.max(
1114 right_bottom_neck_center_y - left_top_neck_center.dy().max(neck_right_center.dy()),
1115 );
1116 let t = inverse_text_scale.powf(3.0);
1117 let stretch = clamp_double(neck_stretch_baseline * t, 0.0, 10.0 * neck_stretch_baseline);
1118 let neck_stretch = Offset::new(0.0, neck_stretch_baseline - stretch);
1119
1120 if cfg!(debug_assertions) && Self::DEBUGGING_LABEL_LOCATION {
1121 let left_center =
1122 Self::TOP_LOBE_CENTER - Offset::new(left_width_needed, 0.0) + neck_stretch;
1123 let right_center =
1124 Self::TOP_LOBE_CENTER + Offset::new(right_width_needed, 0.0) + neck_stretch;
1125 let value_rect = Rect::from_ltrb(
1126 left_center.dx() - Self::TOP_LOBE_RADIUS,
1127 left_center.dy() - Self::TOP_LOBE_RADIUS,
1128 right_center.dx() + Self::TOP_LOBE_RADIUS,
1129 right_center.dy() + Self::TOP_LOBE_RADIUS,
1130 );
1131 let outline_paint = Paint {
1132 color: Color::new(0xffff0000).into(),
1133 style: PaintStyle::Stroke(Stroke::new(1.0)),
1134 ..Paint::default()
1135 };
1136 canvas.draw_rect(value_rect, &outline_paint);
1137 }
1138
1139 Self::add_arc(
1140 &mut path,
1141 left_top_neck_center + neck_stretch,
1142 Self::TOP_NECK_RADIUS,
1143 0.0,
1144 -left_neck_arc_angle,
1145 );
1146 Self::add_arc(
1147 &mut path,
1148 Self::TOP_LOBE_CENTER - Offset::new(left_width_needed, 0.0) + neck_stretch,
1149 Self::TOP_LOBE_RADIUS,
1150 Self::NINETY_DEGREES + left_theta,
1151 Self::TWO_SEVENTY_DEGREES,
1152 );
1153 Self::add_arc(
1154 &mut path,
1155 Self::TOP_LOBE_CENTER + Offset::new(right_width_needed, 0.0) + neck_stretch,
1156 Self::TOP_LOBE_RADIUS,
1157 Self::TWO_SEVENTY_DEGREES,
1158 Self::TWO_SEVENTY_DEGREES + PI - right_theta,
1159 );
1160 Self::add_arc(
1161 &mut path,
1162 neck_right_center + neck_stretch,
1163 Self::TOP_NECK_RADIUS,
1164 right_neck_arc_angle,
1165 PI,
1166 );
1167 let path = path.build();
1168
1169 if let Some(stroke_paint_color) = stroke_paint_color {
1170 let stroke_paint = Paint {
1171 color: stroke_paint_color.into(),
1172 style: PaintStyle::Stroke(Stroke::new(1.0)),
1173 ..Paint::default()
1174 };
1175 canvas.draw_path(&path, FillRule::NonZero, &stroke_paint);
1176 }
1177
1178 canvas.draw_path(&path, FillRule::NonZero, &paint);
1179
1180 canvas.save();
1182 canvas.translate(
1183 shift as f32,
1184 (-Self::DISTANCE_BETWEEN_TOP_BOTTOM_CENTERS + neck_stretch.dy()) as f32,
1185 );
1186 canvas.scale(inverse_text_scale as f32, inverse_text_scale as f32);
1187 let fonts = PaintingBinding::instance(app).fonts(app);
1188 label_painter.paint(
1189 app.get_mut(fonts),
1190 canvas,
1191 Offset::ZERO - Offset::new(label_half_width, label_painter.height() / 2.0),
1192 );
1193 canvas.restore();
1194 canvas.restore();
1195 }
1196}
1197
1198#[cfg(test)]
1199mod tests {
1200 use super::*;
1201
1202 #[test]
1203 fn round_slider_overlay_shape_prefers_its_radius() {
1204 assert_eq!(
1205 RoundSliderOverlayShape::new().get_preferred_size(true, false),
1206 Size::new(48.0, 48.0)
1207 );
1208 assert_eq!(
1209 RoundSliderOverlayShape::new()
1210 .overlay_radius(10.0)
1211 .get_preferred_size(false, true),
1212 Size::new(20.0, 20.0)
1213 );
1214 }
1215
1216 #[test]
1217 fn no_thumb_and_no_overlay_are_empty_distinct_instances() {
1218 let no_thumb = <dyn SliderComponentShape>::no_thumb();
1219 let no_overlay = <dyn SliderComponentShape>::no_overlay();
1220 assert_eq!(no_thumb.get_preferred_size(true, true), Size::ZERO);
1221 assert_eq!(no_overlay.get_preferred_size(true, true), Size::ZERO);
1222 assert!(Rc::ptr_eq(
1223 &no_thumb,
1224 &<dyn SliderComponentShape>::no_thumb()
1225 ));
1226 assert!(*no_thumb != *no_overlay);
1227 }
1228
1229 #[test]
1230 fn built_in_value_indicator_shapes_compare_by_value() {
1231 let paddle: SliderComponentShapeRef = Rc::new(PaddleSliderValueIndicatorShape::new());
1232 let same: SliderComponentShapeRef = Rc::new(PaddleSliderValueIndicatorShape::new());
1233 let rectangular: SliderComponentShapeRef =
1234 Rc::new(RectangularSliderValueIndicatorShape::new());
1235 assert!(*paddle == *same);
1236 assert!(*paddle != *rectangular);
1237 assert!(
1238 rectangular
1239 .as_any()
1240 .is::<RectangularSliderValueIndicatorShape>()
1241 );
1242 }
1243
1244 #[test]
1246 #[should_panic]
1247 fn a_value_indicator_preferred_size_needs_its_label_painter() {
1248 let shape: SliderComponentShapeRef = Rc::new(RectangularSliderValueIndicatorShape::new());
1249 shape.get_preferred_size(true, false);
1250 }
1251}