Skip to content

Commit ff15ee3

Browse files
rustbasicemilk
authored andcommitted
Adjustable Slider rail height (emilk#4092)
Adjustable Slider rail height ![explain20240312-2](https://github.com/emilk/egui/assets/127506429/d5edfe10-8191-44ed-8567-d9d2127ce4b8) --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
1 parent cd0ca1d commit ff15ee3

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

crates/egui/src/style.rs

+9
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ pub struct Spacing {
281281
/// Default width of a [`Slider`].
282282
pub slider_width: f32,
283283

284+
/// Default rail height of a [`Slider`].
285+
pub slider_rail_height: f32,
286+
284287
/// Default (minimum) width of a [`ComboBox`](crate::ComboBox).
285288
pub combo_width: f32,
286289

@@ -1224,6 +1227,7 @@ impl Default for Spacing {
12241227
indent: 18.0, // match checkbox/radio-button with `button_padding.x + icon_width + icon_spacing`
12251228
interact_size: vec2(40.0, 18.0),
12261229
slider_width: 100.0,
1230+
slider_rail_height: 8.0,
12271231
combo_width: 100.0,
12281232
text_edit_width: 280.0,
12291233
icon_width: 14.0,
@@ -1573,6 +1577,7 @@ impl Spacing {
15731577
indent,
15741578
interact_size,
15751579
slider_width,
1580+
slider_rail_height,
15761581
combo_width,
15771582
text_edit_width,
15781583
icon_width,
@@ -1601,6 +1606,10 @@ impl Spacing {
16011606
ui.add(DragValue::new(slider_width).clamp_range(0.0..=1000.0));
16021607
ui.label("Slider width");
16031608
});
1609+
ui.horizontal(|ui| {
1610+
ui.add(DragValue::new(slider_rail_height).clamp_range(0.0..=50.0));
1611+
ui.label("Slider rail height");
1612+
});
16041613
ui.horizontal(|ui| {
16051614
ui.add(DragValue::new(combo_width).clamp_range(0.0..=1000.0));
16061615
ui.label("ComboBox width");

crates/egui/src/widgets/slider.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,12 @@ impl<'a> Slider<'a> {
680680
if ui.is_rect_visible(response.rect) {
681681
let value = self.get_value();
682682

683-
let rail_radius = ui.painter().round_to_pixel(self.rail_radius_limit(rect));
684-
let rail_rect = self.rail_rect(rect, rail_radius);
685-
686683
let visuals = ui.style().interact(response);
687684
let widget_visuals = &ui.visuals().widgets;
685+
let spacing = &ui.style().spacing;
686+
687+
let rail_radius = (spacing.slider_rail_height / 2.0).at_least(0.0);
688+
let rail_rect = self.rail_rect(rect, rail_radius);
688689

689690
ui.painter().rect_filled(
690691
rail_rect,
@@ -800,13 +801,6 @@ impl<'a> Slider<'a> {
800801
limit / 2.5
801802
}
802803

803-
fn rail_radius_limit(&self, rect: &Rect) -> f32 {
804-
match self.orientation {
805-
SliderOrientation::Horizontal => (rect.height() / 4.0).at_least(2.0),
806-
SliderOrientation::Vertical => (rect.width() / 4.0).at_least(2.0),
807-
}
808-
}
809-
810804
fn value_ui(&mut self, ui: &mut Ui, position_range: Rangef) -> Response {
811805
// If [`DragValue`] is controlled from the keyboard and `step` is defined, set speed to `step`
812806
let change = ui.input(|input| {

0 commit comments

Comments
 (0)