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

Slider now warns if max < min and swaps the values #1882

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ You can find its changes [documented below](#070---2021-01-01).
- x11: Implement primary_clipboard ([#1867] by [@psychon])
- x11: Set WM_CLASS property ([#1868] by [@psychon])
- Expose `RawWindowHandle` for `WindowHandle` under the `raw-win-handle` feature ([#1828] by [@djeedai])
- `Slider` widget now warns if max < min and swaps the values ([#1882] by [@Maan2003])

### Changed

Expand Down Expand Up @@ -765,6 +766,7 @@ Last release without a changelog :(
[#1868]: https://github.com/linebender/druid/pull/1868
[#1873]: https://github.com/linebender/druid/pull/1873
[#1876]: https://github.com/linebender/druid/pull/1876
[#1882]: https://github.com/linebender/druid/pull/1882

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
20 changes: 17 additions & 3 deletions druid/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use crate::kurbo::{Circle, Shape};
use crate::widget::prelude::*;
use crate::{theme, LinearGradient, Point, Rect, UnitPoint};
use tracing::{instrument, trace};
use tracing::{instrument, trace, warn};

const TRACK_THICKNESS: f64 = 4.0;
const BORDER_WIDTH: f64 = 2.0;
Expand Down Expand Up @@ -56,6 +56,17 @@ impl Slider {
self.max = max;
self
}

/// check self.min <= self.max, if not swaps the values.
fn check_range(&mut self) {
if self.max < self.min {
warn!(
"min({}) should be less than max({}), swaping the values",
self.min, self.max
);
std::mem::swap(&mut self.max, &mut self.min);
}
}
}

impl Slider {
Expand Down Expand Up @@ -125,8 +136,11 @@ impl Widget<f64> for Slider {

#[instrument(name = "Slider", level = "trace", skip(self, ctx, event, _data, _env))]
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _data: &f64, _env: &Env) {
if let LifeCycle::DisabledChanged(_) = event {
ctx.request_paint();
match event {
// checked in LifeCycle::WidgetAdded because logging may not be setup in with_range
LifeCycle::WidgetAdded => self.check_range(),
LifeCycle::DisabledChanged(_) => ctx.request_paint(),
_ => (),
}
}

Expand Down