diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b0671941a..769b79667e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/druid/src/widget/slider.rs b/druid/src/widget/slider.rs index acd5bf4810..071c72f14c 100644 --- a/druid/src/widget/slider.rs +++ b/druid/src/widget/slider.rs @@ -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; @@ -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 { @@ -125,8 +136,11 @@ impl Widget 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(), + _ => (), } }