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

Add trailing_fill() toggle to Slider #2660

Merged
merged 4 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
* ⚠️ BREAKING: `egui::Context` now use closures for locking ([#2625](https://github.com/emilk/egui/pull/2625)):
* `ctx.input().key_pressed(Key::A)` -> `ctx.input(|i| i.key_pressed(Key::A))`
* `ui.memory().toggle_popup(popup_id)` -> `ui.memory_mut(|mem| mem.toggle_popup(popup_id))`
* Add `Slider::trailing_fill` for trailing color behind the circle like a `ProgressBar` ([#2660](https://github.com/emilk/egui/pull/2660)).

### Added ⭐
* Add `Response::drag_started_by` and `Response::drag_released_by` for convenience, similar to `dragged` and `dragged_by` ([#2507](https://github.com/emilk/egui/pull/2507)).
Expand Down
9 changes: 9 additions & 0 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ pub struct Visuals {
/// Wether or not Grids and Tables should be striped by default
/// (have alternating rows differently colored).
pub striped: bool,

/// Show trailing color behind the circle of a [`Slider`]. Default is OFF.
///
/// Enabling this will affect ALL sliders, and can be enabled/disabled per slider with [`Slider::trailing_fill`].
pub slider_trailing_fill: bool,
}

impl Visuals {
Expand Down Expand Up @@ -764,6 +769,8 @@ impl Visuals {
indent_has_left_vline: true,

striped: false,

slider_trailing_fill: false,
}
}

Expand Down Expand Up @@ -1324,6 +1331,8 @@ impl Visuals {
indent_has_left_vline,

striped,

slider_trailing_fill: _,
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved
} = self;

ui.collapsing("Background Colors", |ui| {
Expand Down
52 changes: 44 additions & 8 deletions crates/egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct Slider<'a> {
max_decimals: Option<usize>,
custom_formatter: Option<NumFormatter<'a>>,
custom_parser: Option<NumParser<'a>>,
trailing_fill: Option<bool>,
}

impl<'a> Slider<'a> {
Expand Down Expand Up @@ -129,6 +130,7 @@ impl<'a> Slider<'a> {
max_decimals: None,
custom_formatter: None,
custom_parser: None,
trailing_fill: None,
}
}

Expand Down Expand Up @@ -269,6 +271,17 @@ impl<'a> Slider<'a> {
self
}

/// Display trailing color behind the slider's circle. Default is OFF.
///
/// This setting can be enabled globally for all sliders with [`Visuals::slider_trailing_fill`].
/// Toggling it here will override the above setting ONLY for this individual slider.
///
/// The fill color will be taken from `selection.bg_fill` in your [`Visuals`], the same as a [`ProgressBar`].
pub fn trailing_fill(mut self, trailing_fill: bool) -> Self {
self.trailing_fill = Some(trailing_fill);
self
}

/// Set custom formatter defining how numbers are converted into text.
///
/// A custom formatter takes a `f64` for the numeric value and a `RangeInclusive<usize>` representing
Expand Down Expand Up @@ -616,18 +629,41 @@ impl<'a> Slider<'a> {
let rail_radius = ui.painter().round_to_pixel(self.rail_radius_limit(rect));
let rail_rect = self.rail_rect(rect, rail_radius);

let position_1d = self.position_from_value(value, position_range);

let visuals = ui.style().interact(response);
ui.painter().add(epaint::RectShape {
rect: rail_rect,
rounding: ui.visuals().widgets.inactive.rounding,
fill: ui.visuals().widgets.inactive.bg_fill,
stroke: Default::default(),
});
let widget_visuals = &ui.visuals().widgets;

ui.painter().rect_filled(
rail_rect,
widget_visuals.inactive.rounding,
widget_visuals.inactive.bg_fill,
);

let position_1d = self.position_from_value(value, position_range);
let center = self.marker_center(position_1d, &rail_rect);

// Decide if we should add trailing fill.
let trailing_fill = match self.trailing_fill {
Some(override_bool) => override_bool,
None => ui.visuals().slider_trailing_fill,
};
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved

// Paint trailing fill.
if trailing_fill {
let mut trailing_rail_rect = rail_rect;

// The trailing rect has to be drawn differently depending on the orientation.
match self.orientation {
SliderOrientation::Vertical => trailing_rail_rect.min.y = center.y,
SliderOrientation::Horizontal => trailing_rail_rect.max.x = center.x,
};

ui.painter().rect_filled(
trailing_rail_rect,
widget_visuals.inactive.rounding,
ui.visuals().selection.bg_fill,
);
}

ui.painter().add(epaint::CircleShape {
center,
radius: self.handle_radius(rect) + visuals.expansion,
Expand Down