@@ -91,6 +91,8 @@ pub struct Slider<'a> {
9191
9292 rail_color : Option < Color32 > ,
9393 thickness : Option < f32 > ,
94+
95+ symmetric_slider : bool ,
9496}
9597
9698impl < ' a > Slider < ' a > {
@@ -111,6 +113,26 @@ impl<'a> Slider<'a> {
111113 }
112114 }
113115
116+ /// Creates a new horizontal slider with symmetric values.
117+ pub fn new_symmetric < Num : emath:: Numeric > ( value : & ' a mut Num , max_abs_value : Num ) -> Self {
118+ let abs_value = max_abs_value. to_f64 ( ) . abs ( ) ;
119+ let range_f64 = -abs_value..=abs_value;
120+ let mut slf = Self :: from_get_set ( range_f64, move |v : Option < f64 > | {
121+ if let Some ( v) = v {
122+ * value = Num :: from_f64 ( v) ;
123+ }
124+ value. to_f64 ( )
125+ } ) ;
126+
127+ slf. symmetric_slider = true ;
128+
129+ if Num :: INTEGRAL {
130+ slf. integer ( )
131+ } else {
132+ slf
133+ }
134+ }
135+
114136 pub fn from_get_set (
115137 range : RangeInclusive < f64 > ,
116138 get_set_value : impl ' a + FnMut ( Option < f64 > ) -> f64 ,
@@ -141,6 +163,8 @@ impl<'a> Slider<'a> {
141163
142164 rail_color : None ,
143165 thickness : None ,
166+
167+ symmetric_slider : false ,
144168 }
145169 }
146170
@@ -725,10 +749,36 @@ impl<'a> Slider<'a> {
725749 let mut trailing_rail_rect = rail_rect;
726750
727751 // The trailing rect has to be drawn differently depending on the orientation.
728- match self . orientation {
729- SliderOrientation :: Vertical => trailing_rail_rect. min . y = center. y ,
730- SliderOrientation :: Horizontal => trailing_rail_rect. max . x = center. x ,
731- } ;
752+ if self . symmetric_slider {
753+ let position_at_0 = self . position_from_value ( 0.0 , position_range) ;
754+ let center_at_0 = self . marker_center ( position_at_0, & rail_rect) ;
755+
756+ match self . orientation {
757+ SliderOrientation :: Vertical => {
758+ if center. y < center_at_0. y {
759+ trailing_rail_rect. min . y = center. y ;
760+ trailing_rail_rect. max . y = center_at_0. y ;
761+ } else {
762+ trailing_rail_rect. min . y = center_at_0. y ;
763+ trailing_rail_rect. max . y = center. y ;
764+ }
765+ }
766+ SliderOrientation :: Horizontal => {
767+ if center. x < center_at_0. x {
768+ trailing_rail_rect. min . x = center. x ;
769+ trailing_rail_rect. max . x = center_at_0. x ;
770+ } else {
771+ trailing_rail_rect. min . x = center_at_0. x ;
772+ trailing_rail_rect. max . x = center. x ;
773+ }
774+ }
775+ } ;
776+ } else {
777+ match self . orientation {
778+ SliderOrientation :: Vertical => trailing_rail_rect. min . y = center. y ,
779+ SliderOrientation :: Horizontal => trailing_rail_rect. max . x = center. x ,
780+ } ;
781+ }
732782
733783 ui. painter ( ) . rect_filled (
734784 trailing_rail_rect,
0 commit comments