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

Conversation

rustbasic
Copy link
Contributor

@rustbasic rustbasic commented Aug 8, 2024

Fix Issue: Positioning issues with Slider and other Button.
Fix Issue: When ui.slider() is inside ui.horizontal(), the DragValue button position moves down slightly.
Improvement: .text_style_height(&ui.style().drag_value_text_style)
Improvement: button_padding applied

If there are multiple SliderOrientation::Verticals, things can get a little tricky to handle.

Before :
https://github.com/user-attachments/assets/3f8120aa-cf92-4383-a038-c3af4b768ce0

After :
https://github.com/user-attachments/assets/f0f037ff-5fd7-42bf-b9b6-277951232edc

Test Example :

use eframe::egui::*;

fn main() -> eframe::Result {
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
        ..Default::default()
    };
    eframe::run_native(
        "My egui App",
        options,
        Box::new(|_cc| Ok(Box::<MyApp>::default())),
    )
}

#[derive(Default)]
struct MyApp {
    temp_value: usize,
    _text: String,
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.separator();

            ui.horizontal(|ui| {
                ui.add(Slider::new(&mut self.temp_value, 0..=255));

                ui.add(DragValue::new(&mut self.temp_value));

                if ui.button("⟲").clicked() {
                    self.temp_value = 255;
                }

                ui.add(Slider::new(&mut self.temp_value, 0..=255));

                ui.add(DragValue::new(&mut self.temp_value));

                if ui.button("⟲").clicked() {
                    self.temp_value = 255;
                }
            });

            ui.separator();

            ui.horizontal(|ui| {
                ui.set_max_width(100.0);

                ui.vertical_centered(|ui| {
                    ui.add(Slider::new(&mut self.temp_value, 0..=255).vertical());

                    ui.add(DragValue::new(&mut self.temp_value));

                    if ui.button("⟲").clicked() {
                        self.temp_value = 255;
                    }
                });
                ui.vertical_centered(|ui| {
                    ui.add(Slider::new(&mut self.temp_value, 0..=255).vertical());

                    ui.add(DragValue::new(&mut self.temp_value));

                    if ui.button("⟲").clicked() {
                        self.temp_value = 255;
                    }
                });
            });

            ui.separator();

            ctx.settings_ui(ui);
        });
    }
}

@rustbasic rustbasic closed this Aug 10, 2024
@rustbasic rustbasic reopened this Aug 10, 2024
@rustbasic rustbasic changed the title Fix: When ui.slider() is inside ui.horizontal(), the DragValue button position moves down slightly. Fix: Positioning issues with Slider and other Button. Aug 10, 2024
@rustbasic rustbasic closed this Aug 21, 2024
@rustbasic rustbasic reopened this Aug 22, 2024
Comment on lines +76 to +87
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"));
});
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow sliders to be centred
3 participants