Use total_cmp for clamping drag value in order to avoid floating point ambiguities (#2213)
* Use total_cmp for clamping DragValue * Added test for clamping * Increase MSRV in all crates * Increased rust version for github actions and lib.rs * Inversed ranges are now working properply with clamp_to_range * Added more tests
This commit is contained in:
parent
34e6e12f00
commit
4d1e858a52
29 changed files with 75 additions and 36 deletions
8
.github/workflows/rust.yml
vendored
8
.github/workflows/rust.yml
vendored
|
@ -19,7 +19,7 @@ jobs:
|
|||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: default
|
||||
toolchain: 1.61.0
|
||||
toolchain: 1.62.0
|
||||
override: true
|
||||
- name: Install packages (Linux)
|
||||
if: runner.os == 'Linux'
|
||||
|
@ -84,7 +84,7 @@ jobs:
|
|||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.61.0
|
||||
toolchain: 1.62.0
|
||||
target: wasm32-unknown-unknown
|
||||
override: true
|
||||
|
||||
|
@ -136,7 +136,7 @@ jobs:
|
|||
- uses: actions/checkout@v2
|
||||
- uses: EmbarkStudios/cargo-deny-action@v1
|
||||
with:
|
||||
rust-version: "1.61.0"
|
||||
rust-version: "1.62.0"
|
||||
|
||||
android:
|
||||
name: android
|
||||
|
@ -146,7 +146,7 @@ jobs:
|
|||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.61.0
|
||||
toolchain: 1.62.0
|
||||
target: aarch64-linux-android
|
||||
override: true
|
||||
- name: Set up cargo cache
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "egui framework - write GUI apps that compiles to web and/or natively"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/eframe"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -8,7 +8,7 @@ authors = [
|
|||
"Emil Ernerfeldt <emil.ernerfeldt@gmail.com>",
|
||||
]
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/egui-wgpu"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "Bindings for using egui with winit"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/egui-winit"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "An easy-to-use immediate mode GUI that runs on both web and native"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "../../README.md"
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
//! Try the live web demo: <https://www.egui.rs/#demo>. Read more about egui at <https://github.com/emilk/egui>.
|
||||
//!
|
||||
//! `egui` is in heavy development, with each new version having breaking changes.
|
||||
//! You need to have rust 1.61.0 or later to use `egui`.
|
||||
//! You need to have rust 1.62.0 or later to use `egui`.
|
||||
//!
|
||||
//! To quickly get started with egui, you can take a look at [`eframe_template`](https://github.com/emilk/eframe_template)
|
||||
//! which uses [`eframe`](https://docs.rs/eframe).
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#![allow(clippy::needless_pass_by_value)] // False positives with `impl ToString`
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
use std::{cmp::Ordering, ops::RangeInclusive};
|
||||
|
||||
use crate::*;
|
||||
|
||||
|
@ -508,8 +508,47 @@ impl<'a> Widget for DragValue<'a> {
|
|||
}
|
||||
|
||||
fn clamp_to_range(x: f64, range: RangeInclusive<f64>) -> f64 {
|
||||
x.clamp(
|
||||
range.start().min(*range.end()),
|
||||
range.start().max(*range.end()),
|
||||
)
|
||||
let (mut min, mut max) = (*range.start(), *range.end());
|
||||
|
||||
if min.total_cmp(&max) == Ordering::Greater {
|
||||
(min, max) = (max, min);
|
||||
}
|
||||
|
||||
match x.total_cmp(&min) {
|
||||
Ordering::Less | Ordering::Equal => min,
|
||||
Ordering::Greater => match x.total_cmp(&max) {
|
||||
Ordering::Greater | Ordering::Equal => max,
|
||||
Ordering::Less => x,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::clamp_to_range;
|
||||
|
||||
macro_rules! total_assert_eq {
|
||||
($a:expr, $b:expr) => {
|
||||
assert!(
|
||||
matches!($a.total_cmp(&$b), std::cmp::Ordering::Equal),
|
||||
"{} != {}",
|
||||
$a,
|
||||
$b
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_total_cmp_clamp_to_range() {
|
||||
total_assert_eq!(0.0_f64, clamp_to_range(-0.0, 0.0..=f64::MAX));
|
||||
total_assert_eq!(-0.0_f64, clamp_to_range(0.0, -1.0..=-0.0));
|
||||
total_assert_eq!(-1.0_f64, clamp_to_range(-25.0, -1.0..=1.0));
|
||||
total_assert_eq!(5.0_f64, clamp_to_range(5.0, -1.0..=10.0));
|
||||
total_assert_eq!(15.0_f64, clamp_to_range(25.0, -1.0..=15.0));
|
||||
total_assert_eq!(1.0_f64, clamp_to_range(1.0, 1.0..=10.0));
|
||||
total_assert_eq!(10.0_f64, clamp_to_range(10.0, 1.0..=10.0));
|
||||
total_assert_eq!(5.0_f64, clamp_to_range(5.0, 10.0..=1.0));
|
||||
total_assert_eq!(5.0_f64, clamp_to_range(15.0, 5.0..=1.0));
|
||||
total_assert_eq!(1.0_f64, clamp_to_range(-5.0, 5.0..=1.0));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
default-run = "egui_demo_app"
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "Example library for egui"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/egui_demo_lib"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -8,7 +8,7 @@ authors = [
|
|||
]
|
||||
description = "Extra functionality and widgets for the egui GUI library"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "Bindings for using egui natively using the glium library"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/egui_glium"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "Bindings for using egui natively using the glow library"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/egui_glow"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "Minimal 2D math library for GUI work"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/emath"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.19.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
description = "Minimal 2D graphics library for GUI work"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
homepage = "https://github.com/emilk/egui/tree/master/crates/epaint"
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["tami5 <kkharji@proton.me>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["René Rössler <rene@freshx.de>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
rust-version = "1.62"
|
||||
publish = false
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,6 @@
|
|||
# to the user in the error, instead of "error: invalid channel name '[toolchain]'".
|
||||
|
||||
[toolchain]
|
||||
channel = "1.61.0"
|
||||
channel = "1.62.0"
|
||||
components = [ "rustfmt", "clippy" ]
|
||||
targets = [ "wasm32-unknown-unknown" ]
|
||||
|
|
Loading…
Reference in a new issue