From b0735775f39fce38262a35011d6e879e3cd25350 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 15 Aug 2023 19:46:04 +0200 Subject: [PATCH] Fix plot formatter not taking closures (#3260) and fix their their comments --- crates/egui/src/widgets/plot/axis.rs | 13 ++++++++----- crates/egui/src/widgets/plot/mod.rs | 28 ++++++++++++++++++---------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/crates/egui/src/widgets/plot/axis.rs b/crates/egui/src/widgets/plot/axis.rs index 6430378bd6a..0ed2eabd995 100644 --- a/crates/egui/src/widgets/plot/axis.rs +++ b/crates/egui/src/widgets/plot/axis.rs @@ -9,7 +9,7 @@ use crate::{Response, Sense, TextStyle, Ui, WidgetText}; use super::{transform::PlotTransform, GridMark}; -pub(super) type AxisFormatterFn = fn(f64, usize, &RangeInclusive) -> String; +pub(super) type AxisFormatterFn = dyn Fn(f64, usize, &RangeInclusive) -> String; /// X or Y axis. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -81,7 +81,7 @@ impl From for Placement { #[derive(Clone)] pub struct AxisHints { pub(super) label: WidgetText, - pub(super) formatter: AxisFormatterFn, + pub(super) formatter: Arc, pub(super) digits: usize, pub(super) placement: Placement, } @@ -98,7 +98,7 @@ impl Default for AxisHints { fn default() -> Self { Self { label: Default::default(), - formatter: Self::default_formatter, + formatter: Arc::new(Self::default_formatter), digits: 5, placement: Placement::LeftBottom, } @@ -111,8 +111,11 @@ impl AxisHints { /// The first parameter of `formatter` is the raw tick value as `f64`. /// The second parameter is the maximum number of characters that fit into y-labels. /// The second parameter of `formatter` is the currently shown range on this axis. - pub fn formatter(mut self, fmt: fn(f64, usize, &RangeInclusive) -> String) -> Self { - self.formatter = fmt; + pub fn formatter( + mut self, + fmt: impl Fn(f64, usize, &RangeInclusive) -> String + 'static, + ) -> Self { + self.formatter = Arc::new(fmt); self } diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index 4bd95337ece..e1f3ffc0b76 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -613,24 +613,32 @@ impl Plot { /// Specify custom formatter for ticks on the main X-axis. /// - /// The first parameter of `fmt` is the raw tick value as `f64`. - /// The second parameter is the maximum requested number of characters per tick label. - /// The second parameter of `fmt` is the currently shown range on this axis. - pub fn x_axis_formatter(mut self, fmt: fn(f64, usize, &RangeInclusive) -> String) -> Self { + /// Arguments of `fmt`: + /// * raw tick value as `f64`. + /// * maximum requested number of characters per tick label. + /// * currently shown range on this axis. + pub fn x_axis_formatter( + mut self, + fmt: impl Fn(f64, usize, &RangeInclusive) -> String + 'static, + ) -> Self { if let Some(main) = self.x_axes.first_mut() { - main.formatter = fmt; + main.formatter = Arc::new(fmt); } self } /// Specify custom formatter for ticks on the main Y-axis. /// - /// The first parameter of `formatter` is the raw tick value as `f64`. - /// The second parameter is the maximum requested number of characters per tick label. - /// The second parameter of `formatter` is the currently shown range on this axis. - pub fn y_axis_formatter(mut self, fmt: fn(f64, usize, &RangeInclusive) -> String) -> Self { + /// Arguments of `fmt`: + /// * raw tick value as `f64`. + /// * maximum requested number of characters per tick label. + /// * currently shown range on this axis. + pub fn y_axis_formatter( + mut self, + fmt: impl Fn(f64, usize, &RangeInclusive) -> String + 'static, + ) -> Self { if let Some(main) = self.y_axes.first_mut() { - main.formatter = fmt; + main.formatter = Arc::new(fmt); } self }