1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Use Arc to avoid some expensive clones

This commit is contained in:
Emil Ernerfeldt
2023-08-14 13:36:52 +02:00
parent 29eb119714
commit 75617d5a15
2 changed files with 12 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
use std::{fmt::Debug, ops::RangeInclusive};
use std::{fmt::Debug, ops::RangeInclusive, sync::Arc};
use epaint::{
emath::{lerp, remap_clamp, round_to_decimals},
@@ -143,7 +143,7 @@ pub(super) struct AxisWidget<const AXIS: usize> {
pub(super) hints: AxisHints<AXIS>,
pub(super) rect: Rect,
pub(super) transform: Option<PlotTransform>,
pub(super) steps: Vec<GridMark>,
pub(super) steps: Arc<Vec<GridMark>>,
}
impl<const AXIS: usize> AxisWidget<AXIS> {
@@ -154,7 +154,7 @@ impl<const AXIS: usize> AxisWidget<AXIS> {
hints,
rect,
transform: None,
steps: Vec::new(),
steps: Default::default(),
}
}
}
@@ -228,7 +228,7 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
None => return response,
};
for step in self.steps {
for step in self.steps.iter() {
let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range);
if !text.is_empty() {
const MIN_TEXT_SPACING: f32 = 20.0;

View File

@@ -1,9 +1,8 @@
//! Simple plotting library.
use ahash::HashMap;
use std::ops::RangeInclusive;
use std::{ops::RangeInclusive, sync::Arc};
use crate::*;
use ahash::HashMap;
use epaint::util::FloatOrd;
use epaint::Hsva;
@@ -11,6 +10,8 @@ use axis::{XAxisWidget, YAxisWidget, X_AXIS, Y_AXIS};
use items::PlotItem;
use legend::LegendWidget;
use crate::*;
pub use items::{
Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, HLine, Line, LineStyle, MarkerShape,
Orientation, PlotImage, PlotPoint, PlotPoints, Points, Polygon, Text, VLine,
@@ -1178,21 +1179,21 @@ impl Plot {
// Add legend widgets to plot
let bounds = transform.bounds();
let x_axis_range = bounds.range_x();
let x_steps = {
let x_steps = Arc::new({
let input = GridInput {
bounds: (bounds.min[X_AXIS], bounds.max[X_AXIS]),
base_step_size: transform.dvalue_dpos()[X_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0,
};
(grid_spacers[X_AXIS])(input)
};
});
let y_axis_range = bounds.range_y();
let y_steps = {
let y_steps = Arc::new({
let input = GridInput {
bounds: (bounds.min[Y_AXIS], bounds.max[Y_AXIS]),
base_step_size: transform.dvalue_dpos()[Y_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0,
};
(grid_spacers[Y_AXIS])(input)
};
});
for mut widget in x_axis_widgets {
widget.range = x_axis_range.clone();
widget.transform = Some(transform);