From 64ace24dc70fac0f73dc403f71bc2ac25b259f5a Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 18 Apr 2022 10:47:44 -0400 Subject: [PATCH] refactor function removal --- src/function.rs | 18 +++++++++--------- src/math_app.rs | 11 +++++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/function.rs b/src/function.rs index 22a872f..b0f07b9 100644 --- a/src/function.rs +++ b/src/function.rs @@ -103,9 +103,9 @@ impl Default for FunctionEntry { } impl FunctionEntry { - pub fn function_entry( - &mut self, ui: &mut egui::Ui, remove_i: &mut Option, can_remove: bool, i: usize, - ) { + /// Creates edit box for [`FunctionEntry`] to edit function settings and string. + /// Returns whether or not this function was marked for removal. + pub fn function_entry(&mut self, ui: &mut egui::Ui, can_remove: bool, i: usize) -> bool { let output_string = self.autocomplete.string.clone(); self.update_string(&output_string); @@ -144,7 +144,7 @@ impl FunctionEntry { ); if ui.ctx().animate_bool(te_id, re.has_focus()) < 1.0 { - return; + return false; } self.autocomplete.update_string(&new_string); @@ -217,16 +217,15 @@ impl FunctionEntry { .fixed_pos(pos2(re.rect.min.x, re.rect.min.y + (row_height * 1.32))) .order(egui::Order::Foreground); + let mut should_remove: bool = false; + buttons_area.show(ui.ctx(), |ui| { ui.horizontal(|ui| { // There's more than 1 function! Functions can now be deleted - if ui + should_remove = ui .add_enabled(can_remove, Button::new("✖").frame(false)) .on_hover_text("Delete Function") - .clicked() - { - *remove_i = Some(i); - } + .clicked(); // Toggle integral being enabled or not self.integral.bitxor_assign( @@ -258,6 +257,7 @@ impl FunctionEntry { ); }); }); + return should_remove; } pub fn settings_window(&mut self, ctx: &Context) { diff --git a/src/math_app.rs b/src/math_app.rs index ab37aa4..a362b6e 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -452,18 +452,21 @@ impl MathApp { self.settings.integral_changed = max_x_changed | min_x_changed | integral_num_changed | riemann_changed; - let functions_len = self.functions.len(); - let mut remove_i: Option = None; + let can_remove = self.functions.len() > 1; ui.label("Functions:"); + + let mut remove_i: Option = None; for (i, function) in self.functions.iter_mut().enumerate() { // Entry for a function - function.function_entry(ui, &mut remove_i, functions_len > 1, i); + if function.function_entry(ui, can_remove, i) { + remove_i = Some(i); + } function.settings_window(ctx); } // Remove function if the user requests it - if let Some(remove_i_unwrap) = remove_i { + if can_remove && let Some(remove_i_unwrap) = remove_i { self.functions.remove(remove_i_unwrap); }