Skip to main content

waft_material/
slider_value_indicator_shape.rs

1//! Flutter counterpart: material_ui's `slider_value_indicator_shape.dart`.
2//!
3//! [`SliderComponentShape`] is an abstract class users subclass, so a trait; a theme holds a
4//! [`SliderComponentShapeRef`]. Its `paint` takes the `App` (the animations, the parent box
5//! and the text painter's fonts live there) and the label painter by `&mut`, which the slider
6//! lends from its render object for the call. The value indicator shapes' `getPreferredSize`
7//! overrides add Dart's optional `labelPainter` and `textScaleFactor`, which a trait method
8//! cannot: each such shape has an inherent `get_preferred_size` that takes them, and its trait
9//! method calls that one without them, failing as Dart's `labelPainter!` does.
10
11use 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
29/// Base class for slider thumb, thumb overlay, and value indicator shapes.
30///
31/// Create a subclass of this if you would like a custom shape.
32///
33/// All shapes are painted to the same canvas and ordering is important.
34/// The overlay is painted first, then the value indicator, then the thumb.
35///
36/// The thumb painting can be skipped by specifying [`no_thumb`](<dyn SliderComponentShape>::no_thumb)
37/// for `SliderThemeData::thumb_shape`.
38///
39/// The overlay painting can be skipped by specifying
40/// [`no_overlay`](<dyn SliderComponentShape>::no_overlay) for `SliderThemeData::overlay_shape`.
41///
42/// Equality is [`eq_shape`](Self::eq_shape): identity, as Dart's default `==`, unless the
43/// shape overrides it.
44///
45/// See also:
46///
47///  * `RoundSliderThumbShape`, which is the default `Slider`'s thumb shape that
48///    paints a solid circle.
49///  * [`RoundSliderOverlayShape`], which is the default `Slider` and
50///    `RangeSlider`'s overlay shape that paints a transparent circle.
51///  * [`PaddleSliderValueIndicatorShape`], which is the default `Slider`'s value
52///    indicator shape that paints a custom path with text in it.
53pub trait SliderComponentShape: Any + Debug {
54    /// Returns the preferred size of the shape, based on the given conditions.
55    fn get_preferred_size(&self, is_enabled: bool, is_discrete: bool) -> Size;
56
57    /// Paints the shape, taking into account the state passed to it.
58    ///
59    /// The `context` argument is the same as the one that includes the `Slider`'s
60    /// render box.
61    ///
62    /// The `center` argument is the offset for where this shape's center should be
63    /// painted. This offset is relative to the origin of the `context` canvas.
64    ///
65    /// The `activation_animation` argument is an animation triggered when the user
66    /// begins to interact with the slider. It reverses when the user stops interacting
67    /// with the slider.
68    ///
69    /// The `enable_animation` argument is an animation triggered when the `Slider`
70    /// is enabled, and it reverses when the slider is disabled. The `Slider` is
71    /// enabled when `Slider::on_changed` is not null. Use this to paint
72    /// intermediate frames for this shape when the slider changes enabled state.
73    ///
74    /// The `is_discrete` argument is true if `Slider::divisions` is non-null. When
75    /// true, the slider will render tick marks on top of the track.
76    ///
77    /// If the `label_painter` argument is non-null, then `TextPainter::paint`
78    /// should be called on the `label_painter` with the location that the label
79    /// should appear. If the `label_painter` argument is null, then no label was
80    /// supplied to the `Slider`.
81    ///
82    /// The `parent_box` argument is the `RenderBox` of the `Slider`. Its attributes,
83    /// such as size, can be used to assist in painting this shape.
84    ///
85    /// the `slider_theme` argument is the theme assigned to the `Slider` that this
86    /// shape belongs to.
87    ///
88    /// The `text_direction` argument can be used to determine how any extra text
89    /// or graphics (besides the text painted by the `label_painter`) should be
90    /// positioned. The `label_painter` already has the `text_direction` set.
91    ///
92    /// The `value` argument is the current parametric value (from 0.0 to 1.0) of
93    /// the slider.
94    ///
95    /// The `text_scale_factor` argument can be used to determine whether the
96    /// component should paint larger or smaller, depending on whether
97    /// `text_scale_factor` is greater than 1 for larger, and between 0 and 1 for
98    /// smaller. It's usually computed from `MediaQueryData::text_scaler`.
99    ///
100    /// The `size_with_overflow` argument can be used to determine the bounds the
101    /// drawing of the components that are outside of the regular slider bounds.
102    /// It's the size of the box, whose center is aligned with the slider's
103    /// bounds, that the value indicators must be drawn within. Typically, it is
104    /// bigger than the slider.
105    #[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    /// Downcast support for Dart's `is` checks.
124    fn as_any(&self) -> &dyn Any;
125
126    /// Dart's `==`: identity unless a shape overrides it.
127    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
128        std::ptr::eq(self.as_any(), other.as_any())
129    }
130}
131
132/// What a field or parameter Dart types as `SliderComponentShape` becomes.
133pub 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    /// Special instance of [`SliderComponentShape`] to skip the thumb drawing.
148    ///
149    /// Each call hands out the same instance, as Dart's `static final` field is one object.
150    ///
151    /// See also:
152    ///
153    ///  * `SliderThemeData::thumb_shape`, which is the shape that the `Slider`
154    ///    uses when painting the thumb.
155    pub fn no_thumb() -> SliderComponentShapeRef {
156        NO_THUMB.with(Rc::clone)
157    }
158
159    /// Special instance of [`SliderComponentShape`] to skip the overlay drawing.
160    ///
161    /// Each call hands out the same instance, as Dart's `static final` field is one object.
162    ///
163    /// See also:
164    ///
165    ///  * `SliderThemeData::overlay_shape`, which is the shape that the `Slider`
166    ///    uses when painting the overlay.
167    pub fn no_overlay() -> SliderComponentShapeRef {
168        NO_OVERLAY.with(Rc::clone)
169    }
170}
171
172/// A special version of [`SliderComponentShape`] that has a zero size and paints
173/// nothing.
174///
175/// This class is used to create a special instance of a [`SliderComponentShape`]
176/// that will not paint any component shape. A static reference is stored in
177/// [`no_thumb`](<dyn SliderComponentShape>::no_thumb) and
178/// [`no_overlay`](<dyn SliderComponentShape>::no_overlay). When this value
179/// is specified for `SliderThemeData::thumb_shape`, the thumb painting is
180/// skipped. When this value is specified for `SliderThemeData::overlay_shape`,
181/// the overlay painting is skipped.
182#[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        // no-op.
207    }
208
209    fn as_any(&self) -> &dyn Any {
210        self
211    }
212}
213
214/// The default shape of a `Slider`'s thumb overlay.
215///
216/// The shape of the overlay is a circle with the same center as the thumb, but
217/// with a larger radius. It animates to full size when the thumb is pressed,
218/// and animates back down to size 0 when it is released. It is painted behind
219/// the thumb, and is expected to extend beyond the bounds of the thumb so that
220/// it is visible.
221///
222/// The overlay color is defined by `SliderThemeData::overlay_color`.
223///
224/// See also:
225///
226///  * `Slider`, which includes an overlay defined by this shape.
227///  * `SliderTheme`, which can be used to configure the overlay shape of all
228///    sliders in a widget subtree.
229#[derive(Clone, Copy, Debug, PartialEq)]
230pub struct RoundSliderOverlayShape {
231    /// The preferred radius of the round thumb shape when enabled.
232    ///
233    /// If it is not provided, then half of the `SliderThemeData::track_height` is
234    /// used.
235    pub overlay_radius: f64,
236}
237
238impl RoundSliderOverlayShape {
239    /// Create a slider thumb overlay that draws a circle.
240    pub const fn new() -> RoundSliderOverlayShape {
241        RoundSliderOverlayShape {
242            overlay_radius: 24.0,
243        }
244    }
245
246    /// Dart `RoundSliderOverlayShape(overlayRadius:)`.
247    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    /// Value equality, as Dart's canonicalized `const` instances are identical.
296    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
297        other.as_any().downcast_ref::<Self>() == Some(self)
298    }
299}
300
301/// The default shape of a `Slider`'s value indicator.
302///
303/// See also:
304///
305///  * `Slider`, which includes a value indicator defined by this shape.
306///  * `SliderTheme`, which can be used to configure the slider value indicator
307///    of all sliders in a widget subtree.
308#[derive(Clone, Copy, Debug, Default, PartialEq)]
309pub struct RectangularSliderValueIndicatorShape;
310
311impl RectangularSliderValueIndicatorShape {
312    const PATH_PAINTER: RectangularSliderValueIndicatorPathPainter =
313        RectangularSliderValueIndicatorPathPainter;
314
315    /// Create a slider value indicator that resembles a rectangular tooltip.
316    pub const fn new() -> RectangularSliderValueIndicatorShape {
317        RectangularSliderValueIndicatorShape
318    }
319
320    /// Dart's `getPreferredSize` with its optional `labelPainter` and `textScaleFactor`,
321    /// which this shape needs.
322    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    /// Value equality, as Dart's canonicalized `const` instances are identical.
387    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
388        other.as_any().downcast_ref::<Self>() == Some(self)
389    }
390}
391
392/// The default shape of a `RangeSlider`'s value indicators.
393///
394/// See also:
395///
396///  * `RangeSlider`, which includes value indicators defined by this shape.
397///  * `SliderTheme`, which can be used to configure the range slider value
398///    indicator of all sliders in a widget subtree.
399#[derive(Clone, Copy, Debug, Default, PartialEq)]
400pub struct RectangularRangeSliderValueIndicatorShape;
401
402impl RectangularRangeSliderValueIndicatorShape {
403    const PATH_PAINTER: RectangularSliderValueIndicatorPathPainter =
404        RectangularSliderValueIndicatorPathPainter;
405
406    /// Create a range slider value indicator that resembles a rectangular tooltip.
407    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    /// Value equality, as Dart's canonicalized `const` instances are identical.
491    fn eq_shape(&self, other: &dyn RangeSliderValueIndicatorShape) -> bool {
492        other.as_any().downcast_ref::<Self>() == Some(self)
493    }
494}
495
496/// Dart's `_RectangularSliderValueIndicatorPathPainter`.
497struct 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        // Value indicator draws on the Overlay and by using the global Offset
532        // we are making sure we use the bounds of the Overlay instead of the Slider.
533        let global_center = parent_box.local_to_global(app, center, None);
534
535        // The rectangle must be shifted towards the center so that it minimizes the
536        // chance of it rendering outside the bounds of the render box. If the shift
537        // is negative, then the lobe is shifted from right to left, and if it is
538        // positive, then the lobe is shifted from left to right.
539        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            // Zero scale essentially means "do not draw anything", so it's safe to just return.
581            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        // Dart's `lineTo` on an empty path starts its contour at the origin.
606        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        // Prepare the canvas for the base of the tooltip, which is relative to the
622        // center of the thumb.
623        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        // The label text is centered within the value indicator.
639        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/// A variant shape of a `Slider`'s value indicator . The value indicator is in
653/// the shape of an upside-down pear.
654///
655/// See also:
656///
657///  * `Slider`, which includes a value indicator defined by this shape.
658///  * `SliderTheme`, which can be used to configure the slider value indicator
659///    of all sliders in a widget subtree.
660#[derive(Clone, Copy, Debug, Default, PartialEq)]
661pub struct PaddleSliderValueIndicatorShape;
662
663impl PaddleSliderValueIndicatorShape {
664    const PATH_PAINTER: PaddleSliderValueIndicatorPathPainter =
665        PaddleSliderValueIndicatorPathPainter;
666
667    /// Create a slider value indicator in the shape of an upside-down pear.
668    pub const fn new() -> PaddleSliderValueIndicatorShape {
669        PaddleSliderValueIndicatorShape
670    }
671
672    /// Dart's `getPreferredSize` with its optional `labelPainter` and `textScaleFactor`,
673    /// which this shape needs.
674    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    /// Value equality, as Dart's canonicalized `const` instances are identical.
747    fn eq_shape(&self, other: &dyn SliderComponentShape) -> bool {
748        other.as_any().downcast_ref::<Self>() == Some(self)
749    }
750}
751
752/// A variant shape of a `RangeSlider`'s value indicators. The value indicator
753/// is in the shape of an upside-down pear.
754///
755/// See also:
756///
757///  * `RangeSlider`, which includes value indicators defined by this shape.
758///  * `SliderTheme`, which can be used to configure the range slider value
759///    indicator of all sliders in a widget subtree.
760#[derive(Clone, Copy, Debug, Default, PartialEq)]
761pub struct PaddleRangeSliderValueIndicatorShape;
762
763impl PaddleRangeSliderValueIndicatorShape {
764    const PATH_PAINTER: PaddleSliderValueIndicatorPathPainter =
765        PaddleSliderValueIndicatorPathPainter;
766
767    /// Create a slider value indicator in the shape of an upside-down pear.
768    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        // Add a stroke of 1dp around the top paddle.
839        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    /// Value equality, as Dart's canonicalized `const` instances are identical.
861    fn eq_shape(&self, other: &dyn RangeSliderValueIndicatorShape) -> bool {
862        other.as_any().downcast_ref::<Self>() == Some(self)
863    }
864}
865
866/// Dart's `_PaddleSliderValueIndicatorPathPainter`.
867struct PaddleSliderValueIndicatorPathPainter;
868
869impl PaddleSliderValueIndicatorPathPainter {
870    // These constants define the shape of the default value indicator.
871    // The value indicator changes shape based on the size of
872    // the label: The top lobe spreads horizontally, and the
873    // top arc on the neck moves down to keep it merging smoothly
874    // with the top lobe as it expands.
875
876    // Radius of the top lobe of the value indicator.
877    const TOP_LOBE_RADIUS: f64 = 16.0;
878    const MIN_LABEL_WIDTH: f64 = 16.0;
879    // Radius of the bottom lobe of the value indicator.
880    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    // The base of the triangle between the top lobe center and the centers of
886    // the two top neck arcs.
887    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    // The length of the hypotenuse of the triangle formed by the center
894    // of the left top lobe arc and the center of the top left neck arc.
895    // Used to calculate the position of the center of the arc.
896    const NECK_TRIANGLE_HYPOTENUSE: f64 = Self::TOP_LOBE_RADIUS + Self::TOP_NECK_RADIUS;
897    // Some convenience values to help readability.
898    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    // Set to true if you want a rectangle to be drawn around the label bubble.
905    // This helps with building tests that check that the label draws in the right
906    // place (because it prints the rect in the failed test output). It should not
907    // be checked in while set to "true".
908    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    // Adds an arc to the path that has the attributes passed in. This is
918    // a convenience to make adding arcs have less boilerplate.
919    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    // Determines the "best" offset to keep the bubble within the slider. The
963    // calling code will bound that with the available movement in the paddle shape.
964    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        // We can just multiply by scale instead of a transform, since we're scaling
979        // around (0, 0).
980        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            // Shifting to the left.
996            shift = shift.max(-half_width_needed);
997        } else {
998            // Shifting to the right.
999            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            // Zero scale essentially means "do not draw anything", so it's safe to just return. Otherwise,
1019            // our math below will attempt to divide by zero and send needless NaNs to the engine.
1020            return;
1021        }
1022        debug_assert!(!size_with_overflow.is_empty());
1023
1024        // The entire value indicator should scale with the size of the label,
1025        // to keep it large enough to encompass the label text.
1026        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        // This is the needed extra width for the label. It is only positive when
1076        // the label exceeds the minimum size contained by the round top lobe.
1077        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        // The parameter that describes how far along the transition from round to
1091        // stretched we are.
1092        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        // The angle between the top neck arc's center and the top lobe's center
1095        // and vertical. The base amount is chosen so that the neck is smooth,
1096        // even when the lobe is shifted due to its size.
1097        let left_theta = (1.0 - left_amount) * Self::THIRTY_DEGREES;
1098        let right_theta = (1.0 - right_amount) * Self::THIRTY_DEGREES;
1099        // The center of the top left neck arc.
1100        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        // The distance between the end of the bottom neck arc and the beginning of
1111        // the top neck arc. We use this to shrink/expand it based on the scale
1112        // factor of the value indicator.
1113        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        // Draw the label.
1181        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    /// Dart's `labelPainter!` throws when a call through the base signature leaves it out.
1245    #[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}