Skip to content

Commit

Permalink
Adjustable Slider rail height (#4092)
Browse files Browse the repository at this point in the history
Adjustable Slider rail height 


![explain20240312-2](https://github.com/emilk/egui/assets/127506429/d5edfe10-8191-44ed-8567-d9d2127ce4b8)

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
rustbasic and emilk authored Mar 21, 2024
1 parent 8ca270e commit 861a1b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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: 8.0,
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

0 comments on commit 861a1b6

Please sign in to comment.