From 9afa4d86bcd649a17ca5c1c135c089ff435c6480 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 20 Apr 2022 11:20:43 -0400 Subject: [PATCH] create widgets_ontop --- src/function.rs | 88 +++++++++++++++++++++++++------------------------ src/math_app.rs | 5 +-- src/widgets.rs | 11 +++++++ 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/src/function.rs b/src/function.rs index f115bb0..dbc170c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -4,7 +4,7 @@ use crate::math_app::AppSettings; use crate::misc::*; use crate::parsing::{process_func_str, BackingFunction}; use crate::suggestions::Hint; -use crate::widgets::{AutoComplete, Movement}; +use crate::widgets::{widgets_ontop, AutoComplete, Movement}; use eframe::{egui, emath, epaint}; use egui::{ plot::{BarChart, PlotUi, Value}, @@ -207,13 +207,6 @@ impl FunctionEntry { } } - /// the y offset multiplier of the `buttons_area` area - const BUTTONS_Y_OFFSET: f32 = 1.32; - - let buttons_area = egui::Area::new(format!("buttons_area_{}", i)) - .fixed_pos(re.rect.min.offset_y(row_height * BUTTONS_Y_OFFSET)) - .order(egui::Order::Foreground); // put it in the foreground so it's above the text edit box - /// Function that creates button that's used with the `button_area` fn button_area_button(text: impl Into) -> Button { Button::new(text.into()).frame(false) @@ -222,44 +215,53 @@ impl FunctionEntry { // returned at the end of this function to indicate whether or not this function should be removed from where it's stored 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 - should_remove = ui - .add_enabled(can_remove, button_area_button("✖")) - .on_hover_text("Delete Function") - .clicked(); + /// the y offset multiplier of the `buttons_area` area + const BUTTONS_Y_OFFSET: f32 = 1.32; - // Toggle integral being enabled or not - self.integral.bitxor_assign( - ui.add(button_area_button("∫")) - .on_hover_text(match self.integral { - true => "Don't integrate", - false => "Integrate", - }) - .clicked(), - ); + widgets_ontop( + ui, + format!("buttons_area_{}", i), + &re, + row_height * BUTTONS_Y_OFFSET, + |ui| { + ui.horizontal(|ui| { + // There's more than 1 function! Functions can now be deleted + should_remove = ui + .add_enabled(can_remove, button_area_button("✖")) + .on_hover_text("Delete Function") + .clicked(); - // Toggle showing the derivative (even though it's already calculated this option just toggles if it's displayed or not) - self.derivative.bitxor_assign( - ui.add(button_area_button("d/dx")) - .on_hover_text(match self.derivative { - true => "Don't Differentiate", - false => "Differentiate", - }) - .clicked(), - ); + // Toggle integral being enabled or not + self.integral.bitxor_assign( + ui.add(button_area_button("∫")) + .on_hover_text(match self.integral { + true => "Don't integrate", + false => "Integrate", + }) + .clicked(), + ); - self.settings_opened.bitxor_assign( - ui.add(button_area_button("⚙")) - .on_hover_text(match self.settings_opened { - true => "Close Settings", - false => "Open Settings", - }) - .clicked(), - ); - }); - }); + // Toggle showing the derivative (even though it's already calculated this option just toggles if it's displayed or not) + self.derivative.bitxor_assign( + ui.add(button_area_button("d/dx")) + .on_hover_text(match self.derivative { + true => "Don't Differentiate", + false => "Differentiate", + }) + .clicked(), + ); + + self.settings_opened.bitxor_assign( + ui.add(button_area_button("⚙")) + .on_hover_text(match self.settings_opened { + true => "Close Settings", + false => "Open Settings", + }) + .clicked(), + ); + }); + }, + ); should_remove } diff --git a/src/math_app.rs b/src/math_app.rs index 7ada411..6596265 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -490,7 +490,7 @@ impl MathApp { } impl epi::App for MathApp { - /// Called each time the UI needs repainting, which may be many times per second. + /// Called each time the UI needs repainting. fn update(&mut self, ctx: &Context, _frame: &mut epi::Frame) { // start timer let start = instant::Instant::now(); @@ -501,8 +501,9 @@ impl epi::App for MathApp { false => Visuals::light(), }); - // if keyboard input isn't being grabed, check for key combos + // If keyboard input isn't being grabbed, check for key combos if !ctx.wants_keyboard_input() { + // If `H` key is pressed, toggle Side Panel self.opened .side_panel .bitxor_assign(ctx.input_mut().consume_key(egui::Modifiers::NONE, Key::H)); diff --git a/src/widgets.rs b/src/widgets.rs index 4a0859c..bc2f5d6 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -100,6 +100,17 @@ pub fn move_cursor_to_end(ctx: &egui::Context, te_id: egui::Id) { TextEdit::store_state(ctx, te_id, state); } +pub fn widgets_ontop( + ui: &egui::Ui, id: String, re: &egui::Response, y_offset: f32, + add_contents: impl FnOnce(&mut egui::Ui) -> R, +) -> R { + let area = egui::Area::new(id) + .fixed_pos(re.rect.min.offset_y(y_offset)) + .order(egui::Order::Foreground); + + area.show(ui.ctx(), |ui| add_contents(ui)).inner +} + #[cfg(test)] mod autocomplete_tests { use super::*;