Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Positioning issues with Slider and other Button. #4937

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions crates/egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
52 changes: 39 additions & 13 deletions crates/egui_demo_app/src/apps/image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
Comment on lines +76 to +87
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 Users should not need to wrap all their sliders in horizontal layouts


// rotation
ui.add_space(2.0);
Expand All @@ -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
Expand Down Expand Up @@ -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(_)) {
Expand All @@ -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 { .. }) {
Expand All @@ -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);
Expand Down
Loading