Skip to content

Commit

Permalink
Fix some clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jul 15, 2024
1 parent 2adefc8 commit 326164e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 31 deletions.
1 change: 1 addition & 0 deletions crates/egui_plot/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
23 changes: 16 additions & 7 deletions crates/egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<Shape>) {
let sqrt_3 = 3_f32.sqrt();
let frac_sqrt_3_2 = 3_f32.sqrt() / 2.0;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1262,7 +1272,6 @@ impl Arrows {

impl PlotItem for Arrows {
fn shapes(&self, _ui: &Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
use crate::emath::*;
let Self {
origins,
tips,
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_plot/src/items/values.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions crates/egui_plot/src/legend.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Id>, link_x: bool, link_y: bool) -> Self {
self.linked_axes = Some((
Expand All @@ -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<Id>, link_x: bool, link_y: bool) -> Self {
self.linked_cursors = Some((
Expand Down Expand Up @@ -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<R>(
self,
ui: &mut Ui,
Expand Down Expand Up @@ -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)
}

Expand Down
31 changes: 18 additions & 13 deletions crates/egui_plot/src/plot_ui.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn PlotItem>>,
pub(crate) next_auto_color_idx: usize,
pub(crate) last_plot_transform: PlotTransform,
Expand All @@ -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
}

Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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();
}
Expand All @@ -200,15 +205,15 @@ 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();
}
self.items.push(Box::new(vline));
}

/// 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;
}
Expand All @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion crates/egui_plot/src/transform.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 326164e

Please sign in to comment.