Skip to content

Commit

Permalink
Slider: warn if max < min and swap the values (#1882)
Browse files Browse the repository at this point in the history
  • Loading branch information
maan2003 authored Jul 26, 2021
1 parent a51e19e commit 85c71ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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

0 comments on commit 85c71ca

Please sign in to comment.