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

Adjustable Slider rail height #4092

Merged
merged 18 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
9 changes: 9 additions & 0 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ pub struct Spacing {
/// Default width of a [`Slider`].
pub slider_width: f32,

/// Default rail height of a [`Slider`].
pub slider_rail_height: f32,

/// Default (minimum) width of a [`ComboBox`](crate::ComboBox).
pub combo_width: f32,

Expand Down Expand Up @@ -1224,6 +1227,7 @@ impl Default for Spacing {
indent: 18.0, // match checkbox/radio-button with `button_padding.x + icon_width + icon_spacing`
interact_size: vec2(40.0, 18.0),
slider_width: 100.0,
slider_rail_height: 9.6,
emilk marked this conversation as resolved.
Show resolved Hide resolved
combo_width: 100.0,
text_edit_width: 280.0,
icon_width: 14.0,
Expand Down Expand Up @@ -1573,6 +1577,7 @@ impl Spacing {
indent,
interact_size,
slider_width,
slider_rail_height,
combo_width,
text_edit_width,
icon_width,
Expand Down Expand Up @@ -1601,6 +1606,10 @@ impl Spacing {
ui.add(DragValue::new(slider_width).clamp_range(0.0..=1000.0));
ui.label("Slider width");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(slider_rail_height).clamp_range(0.0..=50.0));
ui.label("Slider rail height");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(combo_width).clamp_range(0.0..=1000.0));
ui.label("ComboBox width");
Expand Down
14 changes: 4 additions & 10 deletions crates/egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,12 @@ impl<'a> Slider<'a> {
if ui.is_rect_visible(response.rect) {
let value = self.get_value();

let rail_radius = ui.painter().round_to_pixel(self.rail_radius_limit(rect));
let rail_rect = self.rail_rect(rect, rail_radius);

let visuals = ui.style().interact(response);
let widget_visuals = &ui.visuals().widgets;
let spacing = &ui.style().spacing;

let rail_radius = (spacing.slider_rail_height / 2.0).at_least(0.0);
let rail_rect = self.rail_rect(rect, rail_radius);

ui.painter().rect_filled(
rail_rect,
Expand Down Expand Up @@ -800,13 +801,6 @@ impl<'a> Slider<'a> {
limit / 2.5
}

fn rail_radius_limit(&self, rect: &Rect) -> f32 {
match self.orientation {
SliderOrientation::Horizontal => (rect.height() / 4.0).at_least(2.0),
SliderOrientation::Vertical => (rect.width() / 4.0).at_least(2.0),
}
}

fn value_ui(&mut self, ui: &mut Ui, position_range: Rangef) -> Response {
// If [`DragValue`] is controlled from the keyboard and `step` is defined, set speed to `step`
let change = ui.input(|input| {
Expand Down
Loading