diff --git a/crates/egui/src/widgets/slider.rs b/crates/egui/src/widgets/slider.rs index 84e47aeef3e..9d21a8a0ab8 100644 --- a/crates/egui/src/widgets/slider.rs +++ b/crates/egui/src/widgets/slider.rs @@ -550,6 +550,13 @@ impl<'a> Slider<'a> { self.range.clone() } + fn button_padding(&self, ui: &Ui) -> f32 { + match self.orientation { + SliderOrientation::Horizontal => ui.spacing().button_padding.y, + SliderOrientation::Vertical => ui.spacing().button_padding.x, + } + } + /// For instance, `position` is the mouse position and `position_range` is the physical location of the slider on the screen. fn value_from_position(&self, position: f32, position_range: Rangef) -> f64 { let normalized = remap_clamp(position, position_range, 0.0..=1.0) as f64; @@ -787,7 +794,7 @@ impl<'a> Slider<'a> { SliderOrientation::Horizontal => rect.height(), SliderOrientation::Vertical => rect.width(), }; - limit / 2.5 + limit / 2.8 } fn value_ui(&mut self, ui: &mut Ui, position_range: Rangef) -> Response { @@ -845,9 +852,12 @@ impl<'a> Slider<'a> { fn add_contents(&mut self, ui: &mut Ui) -> Response { let old_value = self.get_value(); + let button_padding = self.button_padding(ui); + let thickness = ui - .text_style_height(&TextStyle::Body) - .at_least(ui.spacing().interact_size.y); + .text_style_height(&ui.style().drag_value_text_style) + .at_least(ui.spacing().interact_size.y) + + (button_padding * 2.0); let mut response = self.allocate_slider_space(ui, thickness); self.slider_ui(ui, &response); @@ -922,8 +932,8 @@ impl<'a> Slider<'a> { impl<'a> Widget for Slider<'a> { fn ui(mut self, ui: &mut Ui) -> Response { let inner_response = match self.orientation { - SliderOrientation::Horizontal => ui.horizontal(|ui| self.add_contents(ui)), - SliderOrientation::Vertical => ui.vertical(|ui| self.add_contents(ui)), + SliderOrientation::Horizontal => ui.horizontal_centered(|ui| self.add_contents(ui)), + SliderOrientation::Vertical => ui.vertical_centered(|ui| self.add_contents(ui)), }; inner_response.inner | inner_response.response diff --git a/crates/egui_demo_app/src/apps/image_viewer.rs b/crates/egui_demo_app/src/apps/image_viewer.rs index ad7a8448640..4ee9b42d360 100644 --- a/crates/egui_demo_app/src/apps/image_viewer.rs +++ b/crates/egui_demo_app/src/apps/image_viewer.rs @@ -73,10 +73,18 @@ impl eframe::App for ImageViewer { egui::SidePanel::new(Side::Left, "controls").show(ctx, |ui| { // uv ui.label("UV"); - ui.add(Slider::new(&mut self.image_options.uv.min.x, 0.0..=1.0).text("min x")); - ui.add(Slider::new(&mut self.image_options.uv.min.y, 0.0..=1.0).text("min y")); - ui.add(Slider::new(&mut self.image_options.uv.max.x, 0.0..=1.0).text("max x")); - ui.add(Slider::new(&mut self.image_options.uv.max.y, 0.0..=1.0).text("max y")); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut self.image_options.uv.min.x, 0.0..=1.0).text("min x")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut self.image_options.uv.min.y, 0.0..=1.0).text("min y")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut self.image_options.uv.max.x, 0.0..=1.0).text("max x")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut self.image_options.uv.max.y, 0.0..=1.0).text("max y")); + }); // rotation ui.add_space(2.0); @@ -99,8 +107,12 @@ impl eframe::App for ImageViewer { ui.drag_angle(&mut angle); *rot = Rot2::from_angle(angle); - ui.add(Slider::new(&mut origin.x, 0.0..=1.0).text("origin x")); - ui.add(Slider::new(&mut origin.y, 0.0..=1.0).text("origin y")); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut origin.x, 0.0..=1.0).text("origin x")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut origin.y, 0.0..=1.0).text("origin y")); + }); } // bg_fill @@ -150,8 +162,12 @@ impl eframe::App for ImageViewer { let ImageFit::Exact(size) = &mut self.fit else { unreachable!() }; - ui.add(Slider::new(&mut size.x, 0.0..=2048.0).text("width")); - ui.add(Slider::new(&mut size.y, 0.0..=2048.0).text("height")); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut size.x, 0.0..=2048.0).text("width")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut size.y, 0.0..=2048.0).text("height")); + }); } ChosenFit::Fraction => { if !matches!(self.fit, ImageFit::Fraction(_)) { @@ -160,8 +176,12 @@ impl eframe::App for ImageViewer { let ImageFit::Fraction(fract) = &mut self.fit else { unreachable!() }; - ui.add(Slider::new(&mut fract.x, 0.0..=1.0).text("width")); - ui.add(Slider::new(&mut fract.y, 0.0..=1.0).text("height")); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut fract.x, 0.0..=1.0).text("width")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut fract.y, 0.0..=1.0).text("height")); + }); } ChosenFit::OriginalSize => { if !matches!(self.fit, ImageFit::Original { .. }) { @@ -170,15 +190,21 @@ impl eframe::App for ImageViewer { let ImageFit::Original { scale } = &mut self.fit else { unreachable!() }; - ui.add(Slider::new(scale, 0.1..=4.0).text("scale")); + ui.horizontal(|ui| { + ui.add(Slider::new(scale, 0.1..=4.0).text("scale")); + }); } } // max size ui.add_space(5.0); ui.label("The calculated size will not exceed the maximum size"); - ui.add(Slider::new(&mut self.max_size.x, 0.0..=2048.0).text("width")); - ui.add(Slider::new(&mut self.max_size.y, 0.0..=2048.0).text("height")); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut self.max_size.x, 0.0..=2048.0).text("width")); + }); + ui.horizontal(|ui| { + ui.add(Slider::new(&mut self.max_size.y, 0.0..=2048.0).text("height")); + }); // aspect ratio ui.add_space(5.0);