Skip to content

Commit

Permalink
refactor axislabel drawing (#42)
Browse files Browse the repository at this point in the history
I fixed an emilk TODO to reduce a functions size
 
* [x] I have followed the instructions in the PR template
  • Loading branch information
bircni authored Sep 30, 2024
1 parent ff10790 commit 21c768f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions egui_plot/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,18 @@ 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());

if !ui.is_rect_visible(response.rect) {
return (response, 0.0);
}

let Some(transform) = self.transform else {
return (response, 0.0);
};
let thickness = self.add_tick_labels(ui, transform, axis);

let visuals = ui.style().visuals.clone();

{
Expand Down Expand Up @@ -309,15 +313,14 @@ impl<'a> AxisWidget<'a> {
.add(TextShape::new(text_pos, galley, text_color).with_angle(angle));
}

let font_id = TextStyle::Body.resolve(ui.style());
let Some(transform) = self.transform else {
return (response, 0.0);
};
(response, thickness)
}

/// Add tick labels to the axis. Returns the thickness of the axis.
fn add_tick_labels(&self, ui: &Ui, transform: PlotTransform, axis: Axis) -> f32 {
let font_id = TextStyle::Body.resolve(ui.style());
let label_spacing = self.hints.label_spacing;

let mut thickness: f32 = 0.0;

// Add tick labels:
for step in self.steps.iter() {
let text = (self.hints.formatter)(*step, &self.range);
Expand Down Expand Up @@ -393,7 +396,6 @@ impl<'a> AxisWidget<'a> {
};
}
}

(response, thickness)
thickness
}
}

0 comments on commit 21c768f

Please sign in to comment.