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 1 commit
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_color` for trailing color behind the circle like a `ProgressBar`
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved

### 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
37 changes: 33 additions & 4 deletions crates/egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct Slider<'a> {
clamp_to_range: bool,
smart_aim: bool,
show_value: bool,
trailing_color: bool,
orientation: SliderOrientation,
prefix: String,
suffix: String,
Expand Down Expand Up @@ -119,6 +120,7 @@ impl<'a> Slider<'a> {
clamp_to_range: true,
smart_aim: true,
show_value: true,
trailing_color: false,
orientation: SliderOrientation::Horizontal,
prefix: Default::default(),
suffix: Default::default(),
Expand Down Expand Up @@ -213,6 +215,14 @@ impl<'a> Slider<'a> {
self
}

/// Display trailing color behind the slider's circle. Default is OFF.
///
/// The color will be taken from `selection.bg_fill` in your [`style::Visuals`], the same as a [`widgets::ProgressBar`].
pub fn trailing_color(mut self, trailing_color: bool) -> Self {
self.trailing_color = trailing_color;
self
}

/// Sets the minimal change of the value.
///
/// Value `0.0` effectively disables the feature. If the new value is out of range
Expand Down Expand Up @@ -616,18 +626,37 @@ 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);
let widget_visuals = &ui.visuals().widgets;

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

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

// Paint trailing color if enabled.
if self.trailing_color {
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().add(epaint::RectShape {
rect: trailing_rail_rect,
rounding: widget_visuals.inactive.rounding,
fill: ui.visuals().selection.bg_fill,
stroke: Default::default(),
});
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved
}

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