Skip to content

Commit 4f7a038

Browse files
committed
[material_ui] Paint RoundedRectSliderTrackShape segments by role
Draw the inactive segment first and the active segment second, deriving only the horizontal bounds from the text direction. This avoids the local closures in paint and removes the left/right paints and the per-direction top, bottom and radius ternaries.
1 parent d9c3b18 commit 4f7a038

1 file changed

Lines changed: 35 additions & 41 deletions

File tree

‎packages/material_ui/lib/src/slider_parts.dart‎

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ class RectangularSliderTrackShape extends SliderTrackShape with BaseSliderTrackS
396396
}
397397
}
398398

399+
// A horizontal span of the track, and whether it is wide enough to be painted.
400+
typedef _TrackSegment = ({bool draw, double left, double right});
401+
399402
/// The default shape of a [Slider]'s track.
400403
///
401404
/// It paints a solid colored rectangle with rounded edges, vertically centered
@@ -463,10 +466,6 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS
463466
);
464467
final activePaint = Paint()..color = activeTrackColorTween.evaluate(enableAnimation)!;
465468
final inactivePaint = Paint()..color = inactiveTrackColorTween.evaluate(enableAnimation)!;
466-
final (Paint leftTrackPaint, Paint rightTrackPaint) = switch (textDirection) {
467-
TextDirection.ltr => (activePaint, inactivePaint),
468-
TextDirection.rtl => (inactivePaint, activePaint),
469-
};
470469

471470
final Rect trackRect = getPreferredRect(
472471
parentBox: parentBox,
@@ -478,56 +477,51 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS
478477
final trackRadius = Radius.circular(trackRect.height / 2);
479478
final activeTrackRadius = Radius.circular((trackRect.height + additionalActiveTrackHeight) / 2);
480479
final isLTR = textDirection == TextDirection.ltr;
481-
final isRTL = textDirection == TextDirection.rtl;
482480

483-
final bool drawRightTrack = thumbCenter.dx < (trackRect.right - (sliderTheme.trackHeight! / 2));
484-
final bool drawLeftTrack = thumbCenter.dx > (trackRect.left + (sliderTheme.trackHeight! / 2));
481+
// Both segments extend past the thumb center by half of the track height.
482+
final double halfTrackHeight = sliderTheme.trackHeight! / 2;
483+
final _TrackSegment leftTrack = (
484+
draw: thumbCenter.dx > trackRect.left + halfTrackHeight,
485+
left: trackRect.left,
486+
right: thumbCenter.dx + halfTrackHeight,
487+
);
488+
final _TrackSegment rightTrack = (
489+
draw: thumbCenter.dx < trackRect.right - halfTrackHeight,
490+
left: thumbCenter.dx - halfTrackHeight,
491+
right: trackRect.right,
492+
);
493+
final (_TrackSegment inactiveTrack, _TrackSegment activeTrack) = isLTR
494+
? (rightTrack, leftTrack)
495+
: (leftTrack, rightTrack);
485496

486-
void paintRightTrack() {
487-
if (!drawRightTrack) {
488-
return;
489-
}
497+
// The segment painted last has its rounded end drawn on top of the other one,
498+
// so paint the inactive segment first to keep the active one on top in both
499+
// text directions.
500+
if (inactiveTrack.draw) {
490501
context.canvas.drawRRect(
491502
RRect.fromLTRBR(
492-
thumbCenter.dx - (sliderTheme.trackHeight! / 2),
493-
isRTL ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
494-
trackRect.right,
495-
isRTL ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
496-
isLTR ? trackRadius : activeTrackRadius,
503+
inactiveTrack.left,
504+
trackRect.top,
505+
inactiveTrack.right,
506+
trackRect.bottom,
507+
trackRadius,
497508
),
498-
rightTrackPaint,
509+
inactivePaint,
499510
);
500511
}
501-
502-
void paintLeftTrack() {
503-
if (!drawLeftTrack) {
504-
return;
505-
}
512+
if (activeTrack.draw) {
506513
context.canvas.drawRRect(
507514
RRect.fromLTRBR(
508-
trackRect.left,
509-
isLTR ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
510-
thumbCenter.dx + (sliderTheme.trackHeight! / 2),
511-
isLTR ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
512-
isLTR ? activeTrackRadius : trackRadius,
515+
activeTrack.left,
516+
trackRect.top - (additionalActiveTrackHeight / 2),
517+
activeTrack.right,
518+
trackRect.bottom + (additionalActiveTrackHeight / 2),
519+
activeTrackRadius,
513520
),
514-
leftTrackPaint,
521+
activePaint,
515522
);
516523
}
517524

518-
// Both segments extend past the thumb center by half of the track height, so
519-
// the segment painted last has its rounded end drawn on top of the other one.
520-
// Paint the inactive segment first so the active segment is on top in both
521-
// text directions.
522-
switch (textDirection) {
523-
case TextDirection.ltr:
524-
paintRightTrack();
525-
paintLeftTrack();
526-
case TextDirection.rtl:
527-
paintLeftTrack();
528-
paintRightTrack();
529-
}
530-
531525
final bool showSecondaryTrack =
532526
(secondaryOffset != null) &&
533527
(isLTR ? (secondaryOffset.dx > thumbCenter.dx) : (secondaryOffset.dx < thumbCenter.dx));

0 commit comments

Comments
 (0)