From 7168c998a39d8ca10201bad9ab72a0139cb2991e Mon Sep 17 00:00:00 2001 From: JohannesProgrammiert Date: Fri, 26 May 2023 11:37:32 +0200 Subject: [PATCH] plot-axis: Use 'into impl' as axis label formatter --- crates/egui/src/widgets/plot/axis.rs | 33 +++++++--------------------- crates/egui/src/widgets/plot/mod.rs | 8 +++---- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/crates/egui/src/widgets/plot/axis.rs b/crates/egui/src/widgets/plot/axis.rs index 146c64f13..a4de812a5 100644 --- a/crates/egui/src/widgets/plot/axis.rs +++ b/crates/egui/src/widgets/plot/axis.rs @@ -1,7 +1,4 @@ -use std::{ - fmt::{Debug, Formatter}, - ops::RangeInclusive, -}; +use std::{fmt::Debug, ops::RangeInclusive}; use epaint::{ emath::{lerp, remap_clamp, round_to_decimals}, @@ -41,29 +38,15 @@ pub(super) type YAxisWidget = AxisWidget; /// Axis configuration. /// /// Used to configure axis label and ticks. +/// The AXIS argument must be either [`X_AXIS`] or [`Y_AXIS`]. Everything else is disallowed. #[derive(Clone)] pub struct AxisHints { - pub(super) label: String, + pub(super) label: WidgetText, pub(super) formatter: AxisFormatterFn, digits: usize, pub(super) placement: Placement, } -impl Debug for AxisHints { - fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { - let axis_str = match AXIS { - X_AXIS => "x-axis", - Y_AXIS => "y-axis", - _ => unreachable!(), - }; - write!( - fmt, - "Axis ( placement: {:?}, label: {}, formatter: ???, axis: {} )", - self.placement, self.label, axis_str - ) - } -} - // TODO: this just a guess. It might cease to work if a user changes font size. const LINE_HEIGHT: f32 = 12.0; @@ -75,8 +58,8 @@ impl Default for AxisHints { /// maximum `digits` on tick label is 5 fn default() -> Self { let label = match AXIS { - X_AXIS => "x".to_owned(), - Y_AXIS => "y".to_owned(), + X_AXIS => "x".into(), + Y_AXIS => "y".into(), _ => unreachable!(), }; Self { @@ -114,8 +97,8 @@ impl AxisHints { /// Specify axis label. /// /// The default is 'x' for x-axes and 'y' for y-axes. - pub fn label(mut self, label: String) -> Self { - self.label = label; + pub fn label(mut self, label: impl Into) -> Self { + self.label = label.into(); self } @@ -183,7 +166,7 @@ impl Widget for AxisWidget { let response = ui.allocate_rect(self.rect, Sense::click_and_drag()); if ui.is_rect_visible(response.rect) { let visuals = ui.style().visuals.clone(); - let text: WidgetText = self.hints.label.into(); + let text = self.hints.label; let galley = text.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Body); let text_color = visuals .override_text_color diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index 729da46de..76eed0acd 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -566,16 +566,16 @@ impl Plot { } /// Set the x axis label of the bottom x-axis - pub fn x_axis_label(mut self, label: String) -> Self { + pub fn x_axis_label(mut self, label: impl Into) -> Self { if !self.x_axes.is_empty() { - self.x_axes[0].label = label; + self.x_axes[0].label = label.into(); } self } /// Set the y axis label of the left y-axis - pub fn y_axis_label(mut self, label: String) -> Self { + pub fn y_axis_label(mut self, label: impl Into) -> Self { if !self.y_axes.is_empty() { - self.y_axes[0].label = label; + self.y_axes[0].label = label.into(); } self }