diff --git a/egui/src/demos/app.rs b/egui/src/demos/app.rs index 72e25bd4..1d1f7f63 100644 --- a/egui/src/demos/app.rs +++ b/egui/src/demos/app.rs @@ -404,13 +404,13 @@ impl Widgets { }); ui.add(Slider::f32(&mut self.slider_value, -10.0..=10.0).text("value")); - if ui.add(Button::new("Double it")).clicked { - self.slider_value *= 2.0; - } ui.horizontal(|ui| { ui.label("drag this number:"); ui.add(DragValue::f32(&mut self.slider_value).speed(0.01)); }); + if ui.add(Button::new("Assign PI")).clicked { + self.slider_value = std::f32::consts::PI; + } ui.horizontal(|ui| { ui.add(label!("Single line text input:")); diff --git a/egui/src/math.rs b/egui/src/math.rs index 4a48d681..df008231 100644 --- a/egui/src/math.rs +++ b/egui/src/math.rs @@ -83,3 +83,16 @@ pub fn round_to_precision(value: f32, decimal_places: usize) -> f32 { .parse() .unwrap_or_else(|_| value) } + +pub fn format_with_minimum_precision(value: f32, precision: usize) -> String { + let text = format!("{:.*}", precision, value); + if (text.parse::().unwrap() - value).abs() <= std::f32::EPSILON { + // Enough precision to show the value accurately - good! + text + } else { + // The value has more precision than we expected. + // Probably the value was set not by the slider, but from outside. + // In any case: show the full value + value.to_string() + } +} diff --git a/egui/src/widgets.rs b/egui/src/widgets.rs index 6b9cec11..d9671e0f 100644 --- a/egui/src/widgets.rs +++ b/egui/src/widgets.rs @@ -96,7 +96,7 @@ impl Label { // TODO: this should return a LabelLayout which has a paint method. // We can then split Widget::Ui in two: layout + allocating space, and painting. - // this allows us to assemble lables, THEN detect interaction, THEN chose color style based on that. + // this allows us to assemble labels, THEN detect interaction, THEN chose color style based on that. // pub fn layout(self, ui: &mut ui) -> LabelLayout { } // TODO: a paint method for painting anywhere in a ui. @@ -570,7 +570,7 @@ impl<'a> Widget for DragValue<'a> { .log10() .ceil() .max(0.0) as usize; - let button = Button::new(format!("{:.*}", precision, *value)) + let button = Button::new(format_with_minimum_precision(*value, precision)) .sense(Sense::drag()) .text_style(TextStyle::Monospace); let interact = ui.add(button); diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 28b7d69c..25818df5 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -184,7 +184,7 @@ impl<'a> Slider<'a> { fn text_ui(&mut self, ui: &mut Ui, x_range: RangeInclusive) { let aim_radius = ui.input().aim_radius(); let value_text = self.format_value(aim_radius, x_range); - let label_text = self.text.as_ref().map(String::as_str).unwrap_or_default(); + let label_text = self.text.as_deref().unwrap_or_default(); let text = format!("{}: {}", label_text, value_text); let text_color = self.text_color.unwrap_or_else(|| ui.style().text_color); ui.add(Label::new(text).multiline(false).text_color(text_color)); @@ -203,16 +203,7 @@ impl<'a> Slider<'a> { (-range.log10()).ceil().max(0.0) as usize }); - let text = format!("{:.*}", precision, value); - if (text.parse::().unwrap() - value).abs() <= std::f32::EPSILON { - // Enough precision to show the value accurately - good! - text - } else { - // The value has more precision than we expected. - // Probably the value was set not by the slider, but from outside. - // In any case: show the full value - value.to_string() - } + format_with_minimum_precision(value, precision) } }