From 326164eab56b001d5647959ca1083838ff9c9c37 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 15 Jul 2024 16:36:19 +0200 Subject: [PATCH] Fix some clippy lints --- crates/egui_plot/src/axis.rs | 1 + crates/egui_plot/src/items/mod.rs | 23 ++++++++++++++------- crates/egui_plot/src/items/values.rs | 4 ++-- crates/egui_plot/src/legend.rs | 13 +++++++----- crates/egui_plot/src/lib.rs | 12 ++++++++--- crates/egui_plot/src/plot_ui.rs | 31 ++++++++++++++++------------ crates/egui_plot/src/transform.rs | 6 +++++- 7 files changed, 59 insertions(+), 31 deletions(-) diff --git a/crates/egui_plot/src/axis.rs b/crates/egui_plot/src/axis.rs index 7992511..45da25f 100644 --- a/crates/egui_plot/src/axis.rs +++ b/crates/egui_plot/src/axis.rs @@ -244,6 +244,7 @@ impl<'a> AxisWidget<'a> { } /// Returns the actual thickness of the axis. + #[allow(clippy::too_many_lines)] // TODO(emilk): shorten this function pub fn ui(self, ui: &mut Ui, axis: Axis) -> (Response, f32) { let response = ui.allocate_rect(self.rect, Sense::hover()); diff --git a/crates/egui_plot/src/items/mod.rs b/crates/egui_plot/src/items/mod.rs index 8e31c1f..6d5f7cf 100644 --- a/crates/egui_plot/src/items/mod.rs +++ b/crates/egui_plot/src/items/mod.rs @@ -3,12 +3,17 @@ use std::ops::RangeInclusive; -use epaint::{emath::Rot2, Mesh}; +use egui::{ + emath::Rot2, + epaint::{CircleShape, TextShape}, + pos2, vec2, Align2, Color32, Id, ImageOptions, Mesh, NumExt as _, Pos2, Rect, Rgba, Rounding, + Shape, Stroke, TextStyle, TextureId, Ui, Vec2, WidgetText, +}; -use crate::*; +use emath::Float as _; +use rect_elem::{highlighted_color, RectElement}; use super::{Cursor, LabelFormatter, PlotBounds, PlotTransform}; -use rect_elem::*; pub use bar::Bar; pub use box_elem::{BoxElem, BoxSpread}; @@ -725,7 +730,11 @@ impl PlotItem for Polygon { let shape = Shape::convex_polygon(values_tf.clone(), fill_color, Stroke::NONE); shapes.push(shape); - values_tf.push(*values_tf.first().unwrap()); + + if let Some(first) = values_tf.first() { + values_tf.push(*first); // close the polygon + } + style.style_line(values_tf, *stroke, *highlight, shapes); } @@ -860,7 +869,7 @@ impl PlotItem for Text { let pos = transform.position_from_point(&self.position); let rect = self.anchor.anchor_size(pos, galley.size()); - shapes.push(epaint::TextShape::new(rect.min, galley, color).into()); + shapes.push(TextShape::new(rect.min, galley, color).into()); if self.highlight { shapes.push(Shape::rect_stroke( @@ -1020,6 +1029,7 @@ impl Points { } impl PlotItem for Points { + #[allow(clippy::too_many_lines)] // TODO(emilk): shorten this function fn shapes(&self, _ui: &Ui, transform: &PlotTransform, shapes: &mut Vec) { let sqrt_3 = 3_f32.sqrt(); let frac_sqrt_3_2 = 3_f32.sqrt() / 2.0; @@ -1067,7 +1077,7 @@ impl PlotItem for Points { match shape { MarkerShape::Circle => { - shapes.push(Shape::Circle(epaint::CircleShape { + shapes.push(Shape::Circle(CircleShape { center, radius, fill, @@ -1262,7 +1272,6 @@ impl Arrows { impl PlotItem for Arrows { fn shapes(&self, _ui: &Ui, transform: &PlotTransform, shapes: &mut Vec) { - use crate::emath::*; let Self { origins, tips, diff --git a/crates/egui_plot/src/items/values.rs b/crates/egui_plot/src/items/values.rs index dbbb59b..f080fd6 100644 --- a/crates/egui_plot/src/items/values.rs +++ b/crates/egui_plot/src/items/values.rs @@ -1,6 +1,6 @@ use std::ops::{Bound, RangeBounds, RangeInclusive}; -use egui::{Pos2, Shape, Stroke, Vec2}; +use egui::{lerp, Pos2, Shape, Stroke, Vec2}; use crate::transform::PlotBounds; @@ -405,7 +405,7 @@ impl ExplicitGenerator { const N: u32 = 8; for i in 1..N { let t = i as f64 / (N - 1) as f64; - let x = crate::lerp(min_x..=max_x, t); + let x = lerp(min_x..=max_x, t); add_x(x); } } else { diff --git a/crates/egui_plot/src/legend.rs b/crates/egui_plot/src/legend.rs index e7eb079..32a540d 100644 --- a/crates/egui_plot/src/legend.rs +++ b/crates/egui_plot/src/legend.rs @@ -1,6 +1,9 @@ use std::{collections::BTreeMap, string::String}; -use crate::*; +use egui::{ + epaint::CircleShape, pos2, vec2, Align, Color32, Direction, Frame, Layout, PointerButton, Rect, + Response, Sense, Shadow, Shape, TextStyle, Ui, Widget, WidgetInfo, WidgetType, +}; use super::items::PlotItem; @@ -139,7 +142,7 @@ impl LegendEntry { let painter = ui.painter(); - painter.add(epaint::CircleShape { + painter.add(CircleShape { center: icon_rect.center(), radius: icon_size * 0.5, fill: visuals.bg_fill, @@ -152,7 +155,7 @@ impl LegendEntry { } else { *color }; - painter.add(epaint::Shape::circle_filled( + painter.add(Shape::circle_filled( icon_rect.center(), icon_size * 0.4, fill, @@ -262,7 +265,7 @@ impl Widget for &mut LegendWidget { let background_frame = Frame { inner_margin: vec2(8.0, 4.0).into(), rounding: ui.style().visuals.window_rounding, - shadow: epaint::Shadow::NONE, + shadow: Shadow::NONE, fill: ui.style().visuals.extreme_bg_color, stroke: ui.style().visuals.window_stroke(), ..Default::default() @@ -287,7 +290,7 @@ impl Widget for &mut LegendWidget { response }) .reduce(|r1, r2| r1.union(r2)) - .unwrap(); + .expect("No entries in the legend"); if let Some(focus_on_item) = focus_on_item { handle_focus_on_legend_item(&focus_on_item, entries); diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index aba6793..f6e9123 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -18,9 +18,12 @@ mod transform; use std::{cmp::Ordering, ops::RangeInclusive, sync::Arc}; use ahash::HashMap; -use egui::*; +use egui::{ + epaint, remap_clamp, vec2, Align2, Color32, CursorIcon, Id, Layout, NumExt, PointerButton, + Pos2, Rangef, Rect, Response, Rounding, Sense, Shape, Stroke, TextStyle, Ui, Vec2, Vec2b, + WidgetText, +}; use emath::Float as _; -use epaint::Hsva; pub use crate::{ axis::{Axis, AxisHints, HPlacement, Placement, VPlacement}, @@ -575,6 +578,7 @@ impl<'a> Plot<'a> { /// Add this plot to an axis link group so that this plot will share the bounds with other plots in the /// same group. A plot cannot belong to more than one axis group. + #[allow(clippy::fn_params_excessive_bools)] // TODO(emilk): use a `Vec2` as argument instead #[inline] pub fn link_axis(mut self, group_id: impl Into, link_x: bool, link_y: bool) -> Self { self.linked_axes = Some(( @@ -589,6 +593,7 @@ impl<'a> Plot<'a> { /// Add this plot to a cursor link group so that this plot will share the cursor position with other plots /// in the same group. A plot cannot belong to more than one cursor group. + #[allow(clippy::fn_params_excessive_bools)] // TODO(emilk): use a `Vec2` as argument instead #[inline] pub fn link_cursor(mut self, group_id: impl Into, link_x: bool, link_y: bool) -> Self { self.linked_cursors = Some(( @@ -731,6 +736,7 @@ impl<'a> Plot<'a> { self.show_dyn(ui, Box::new(build_fn)) } + #[allow(clippy::too_many_lines)] // TODO(emilk): shorten this function fn show_dyn( self, ui: &mut Ui, @@ -1729,7 +1735,7 @@ impl<'a> PreparedPlot<'a> { /// assert_eq!(next_power(0.2, 10.0), 1); /// ``` fn next_power(value: f64, base: f64) -> f64 { - debug_assert_ne!(value, 0.0); // can be negative (typical for Y axis) + debug_assert_ne!(value, 0.0, "Bad input"); // can be negative (typical for Y axis) base.powi(value.abs().log(base).ceil() as i32) } diff --git a/crates/egui_plot/src/plot_ui.rs b/crates/egui_plot/src/plot_ui.rs index 83c4c36..8b24650 100644 --- a/crates/egui_plot/src/plot_ui.rs +++ b/crates/egui_plot/src/plot_ui.rs @@ -1,9 +1,14 @@ -use crate::*; +use egui::{epaint::Hsva, Color32, Pos2, Response, Vec2, Vec2b}; + +use crate::{BoundsModification, PlotBounds, PlotItem, PlotPoint, PlotTransform}; + +#[allow(unused_imports)] // for links in docstrings +use crate::Plot; /// Provides methods to interact with a plot while building it. It is the single argument of the closure /// provided to [`Plot::show`]. See [`Plot`] for an example of how to use it. pub struct PlotUi { - pub(crate) ctx: Context, + pub(crate) ctx: egui::Context, pub(crate) items: Vec>, pub(crate) next_auto_color_idx: usize, pub(crate) last_plot_transform: PlotTransform, @@ -21,7 +26,7 @@ impl PlotUi { Hsva::new(h, 0.85, 0.5, 1.0).into() // TODO(emilk): OkLab or some other perspective color space } - pub fn ctx(&self) -> &Context { + pub fn ctx(&self) -> &egui::Context { &self.ctx } @@ -122,7 +127,7 @@ impl PlotUi { } /// Add a data line. - pub fn line(&mut self, mut line: Line) { + pub fn line(&mut self, mut line: crate::Line) { if line.series.is_empty() { return; }; @@ -135,7 +140,7 @@ impl PlotUi { } /// Add a polygon. The polygon has to be convex. - pub fn polygon(&mut self, mut polygon: Polygon) { + pub fn polygon(&mut self, mut polygon: crate::Polygon) { if polygon.series.is_empty() { return; }; @@ -148,7 +153,7 @@ impl PlotUi { } /// Add a text. - pub fn text(&mut self, text: Text) { + pub fn text(&mut self, text: crate::Text) { if text.text.is_empty() { return; }; @@ -157,7 +162,7 @@ impl PlotUi { } /// Add data points. - pub fn points(&mut self, mut points: Points) { + pub fn points(&mut self, mut points: crate::Points) { if points.series.is_empty() { return; }; @@ -170,7 +175,7 @@ impl PlotUi { } /// Add arrows. - pub fn arrows(&mut self, mut arrows: Arrows) { + pub fn arrows(&mut self, mut arrows: crate::Arrows) { if arrows.origins.is_empty() || arrows.tips.is_empty() { return; }; @@ -183,14 +188,14 @@ impl PlotUi { } /// Add an image. - pub fn image(&mut self, image: PlotImage) { + pub fn image(&mut self, image: crate::PlotImage) { self.items.push(Box::new(image)); } /// Add a horizontal line. /// Can be useful e.g. to show min/max bounds or similar. /// Always fills the full width of the plot. - pub fn hline(&mut self, mut hline: HLine) { + pub fn hline(&mut self, mut hline: crate::HLine) { if hline.stroke.color == Color32::TRANSPARENT { hline.stroke.color = self.auto_color(); } @@ -200,7 +205,7 @@ impl PlotUi { /// Add a vertical line. /// Can be useful e.g. to show min/max bounds or similar. /// Always fills the full height of the plot. - pub fn vline(&mut self, mut vline: VLine) { + pub fn vline(&mut self, mut vline: crate::VLine) { if vline.stroke.color == Color32::TRANSPARENT { vline.stroke.color = self.auto_color(); } @@ -208,7 +213,7 @@ impl PlotUi { } /// Add a box plot diagram. - pub fn box_plot(&mut self, mut box_plot: BoxPlot) { + pub fn box_plot(&mut self, mut box_plot: crate::BoxPlot) { if box_plot.boxes.is_empty() { return; } @@ -221,7 +226,7 @@ impl PlotUi { } /// Add a bar chart. - pub fn bar_chart(&mut self, mut chart: BarChart) { + pub fn bar_chart(&mut self, mut chart: crate::BarChart) { if chart.bars.is_empty() { return; } diff --git a/crates/egui_plot/src/transform.rs b/crates/egui_plot/src/transform.rs index 405b6e4..82eccbd 100644 --- a/crates/egui_plot/src/transform.rs +++ b/crates/egui_plot/src/transform.rs @@ -1,7 +1,10 @@ use std::ops::RangeInclusive; +use egui::{pos2, remap, Pos2, Rect, Vec2}; + +use crate::Axis; + use super::PlotPoint; -use crate::*; /// 2D bounding box of f64 precision. /// @@ -279,6 +282,7 @@ pub struct PlotTransform { } impl PlotTransform { + #[allow(clippy::fn_params_excessive_bools)] // TODO(emilk): use a `Vec2` as argument instead pub fn new(frame: Rect, bounds: PlotBounds, x_centered: bool, y_centered: bool) -> Self { debug_assert!( 0.0 <= frame.width() && 0.0 <= frame.height(),