refactoring

This commit is contained in:
Simon Gardling
2022-04-01 02:18:17 -04:00
parent 6d953381aa
commit 06d5a32d09
4 changed files with 145 additions and 147 deletions

View File

@@ -1,8 +1,8 @@
#![allow(clippy::too_many_arguments)] // Clippy, shut
use crate::egui_app::AppSettings;
use crate::math_app::AppSettings;
use crate::misc::*;
use crate::parsing::BackingFunction;
use crate::parsing::{process_func_str, BackingFunction};
use crate::suggestions::{generate_hint, HintEnum};
use eframe::{egui, epaint};
use egui::{
@@ -65,6 +65,116 @@ impl AutoComplete {
self.changed = false;
}
}
/// Creates and manages text box and autocompletion of function input
/// Returns whether or not the function text box is in focus
fn ui(&mut self, ui: &mut egui::Ui, string: &mut String) -> bool {
// Put here so these key presses don't interact with other elements
let enter_pressed = ui
.input_mut()
.consume_key(egui::Modifiers::NONE, Key::Enter);
let tab_pressed = ui.input_mut().consume_key(egui::Modifiers::NONE, Key::Tab);
let te_id = ui.make_persistent_id("text_edit_ac".to_string());
// update self.autocomplete
self.changed(string.clone());
let mut func_edit = egui::TextEdit::singleline(string)
.hint_forward(true)
.lock_focus(true);
if self.hint.is_none() {
func_edit.id(te_id).ui(ui);
return false;
}
if let Some(single_hint) = self.hint.get_single() {
let func_edit_2 = func_edit;
func_edit = func_edit_2.hint_text(&single_hint);
}
let re = func_edit.id(te_id).ui(ui);
let func_edit_focus = re.has_focus();
// If in focus and right arrow key was pressed, apply hint
if func_edit_focus {
let mut push_cursor: bool = false;
let apply_key = ui.input().key_pressed(Key::ArrowRight) | enter_pressed | tab_pressed;
if apply_key && let Some(single_hint) = self.hint.get_single() {
push_cursor = true;
*string = string.clone() + &single_hint;
} else if self.hint.is_multi() {
let selections = self.hint.ensure_many();
let max_i = selections.len() as i16 - 1;
let mut i = self.i as i16;
if ui.input().key_pressed(Key::ArrowDown) {
i += 1;
if i > max_i {
i = 0;
}
} else if ui.input().key_pressed(Key::ArrowUp) {
i -= 1;
if 0 > i {
i = max_i
}
}
self.i = i as usize;
let popup_id = ui.make_persistent_id("autocomplete_popup");
let mut clicked = false;
egui::popup_below_widget(ui, popup_id, &re, |ui| {
for (i, candidate) in selections.iter().enumerate() {
if ui
.selectable_label(i == self.i, *candidate)
.clicked()
{
clicked = true;
self.i = i;
}
}
});
if clicked | apply_key {
*string += selections[self.i];
push_cursor = true;
// don't need this here as it simply won't be display next frame in `math_app.rs`
// ui.memory().close_popup();
} else {
ui.memory().open_popup(popup_id);
}
}
// Push cursor to end if needed
if push_cursor {
let mut state = TextEdit::load_state(ui.ctx(), te_id).unwrap();
state.set_cursor_range(Some(CursorRange::one(Cursor {
ccursor: CCursor {
index: 0,
prefer_next_row: false,
},
rcursor: RCursor { row: 0, column: 0 },
pcursor: PCursor {
paragraph: 0,
offset: 10000,
prefer_next_row: false,
},
})));
TextEdit::store_state(ui.ctx(), te_id, state);
}
}
func_edit_focus
}
}
/// `FunctionEntry` is a function that can calculate values, integrals,
@@ -77,7 +187,7 @@ pub struct FunctionEntry {
/// Stores a function string (that hasn't been processed via
/// `process_func_str`) to display to the user
func_str: String,
raw_func_str: String,
/// Minimum and Maximum values of what do display
min_x: f64,
@@ -104,7 +214,7 @@ impl Default for FunctionEntry {
fn default() -> FunctionEntry {
FunctionEntry {
function: BackingFunction::new(""),
func_str: String::new(),
raw_func_str: String::new(),
min_x: -1.0,
max_x: 1.0,
integral: false,
@@ -120,17 +230,30 @@ impl Default for FunctionEntry {
}
impl FunctionEntry {
pub fn get_func_raw(&self) -> String { self.raw_func_str.to_string() }
pub fn auto_complete(&mut self, ui: &mut egui::Ui, string: &mut String) -> bool {
self.autocomplete.ui(ui, string)
}
/// Update function settings
pub fn update(&mut self, func_str: &str, integral: bool, derivative: bool) {
// If the function string changes, just wipe and restart from scratch
if func_str != self.func_str {
self.func_str = func_str.to_string();
self.function = BackingFunction::new(func_str);
pub fn update(
&mut self, raw_func_str: &str, integral: bool, derivative: bool,
) -> Option<String> {
if raw_func_str != self.get_func_raw() {
let processed_func = process_func_str(raw_func_str);
let output = crate::parsing::test_func(&processed_func);
self.raw_func_str = raw_func_str.to_string();
if output.is_some() {
return output;
}
self.function = BackingFunction::new(&processed_func);
self.invalidate_whole();
}
self.derivative = derivative;
self.integral = integral;
return None;
}
fn get_sum_func(&self, sum: Riemann) -> FunctionHelper {
@@ -147,111 +270,6 @@ impl FunctionEntry {
}
}
/// Creates and manages text box and autocompletion of function input
/// Returns whether or not the function text box is in focus
pub fn auto_complete(&mut self, ui: &mut egui::Ui, string: &mut String) -> bool {
// Put here so these key presses don't interact with other elements
let enter_pressed = ui
.input_mut()
.consume_key(egui::Modifiers::NONE, Key::Enter);
let tab_pressed = ui.input_mut().consume_key(egui::Modifiers::NONE, Key::Tab);
let te_id = ui.make_persistent_id("text_edit_ac".to_string());
// update self.autocomplete
self.autocomplete.changed(string.clone());
let mut func_edit = egui::TextEdit::singleline(string)
.hint_forward(true)
.lock_focus(true);
if self.autocomplete.hint.is_none() {
func_edit.id(te_id).ui(ui);
return false;
}
if let Some(single_hint) = self.autocomplete.hint.get_single() {
let func_edit_2 = func_edit;
func_edit = func_edit_2.hint_text(&single_hint);
}
let re = func_edit.id(te_id).ui(ui);
let func_edit_focus = re.has_focus();
// If in focus and right arrow key was pressed, apply hint
if func_edit_focus {
let mut push_cursor: bool = false;
let apply_key = ui.input().key_pressed(Key::ArrowRight) | enter_pressed | tab_pressed;
if apply_key && let Some(single_hint) = self.autocomplete.hint.get_single() {
push_cursor = true;
*string = string.clone() + &single_hint;
} else if self.autocomplete.hint.is_multi() {
let selections = self.autocomplete.hint.ensure_many();
let max_i = selections.len() as i16 - 1;
let mut i = self.autocomplete.i as i16;
if ui.input().key_pressed(Key::ArrowDown) {
i += 1;
if i > max_i {
i = 0;
}
} else if ui.input().key_pressed(Key::ArrowUp) {
i -= 1;
if 0 > i {
i = max_i
}
}
self.autocomplete.i = i as usize;
let popup_id = ui.make_persistent_id("autocomplete_popup");
let mut clicked = false;
egui::popup_below_widget(ui, popup_id, &re, |ui| {
for (i, candidate) in selections.iter().enumerate() {
if ui
.selectable_label(i == self.autocomplete.i, *candidate)
.clicked()
{
clicked = true;
self.autocomplete.i = i;
}
}
});
if apply_key | clicked {
*string = string.clone() + selections[self.autocomplete.i];
push_cursor = true;
} else {
ui.memory().open_popup(popup_id);
}
}
// Push cursor to end if needed
if push_cursor {
let mut state = TextEdit::load_state(ui.ctx(), te_id).unwrap();
state.set_cursor_range(Some(CursorRange::one(Cursor {
ccursor: CCursor {
index: 0,
prefer_next_row: false,
},
rcursor: RCursor { row: 0, column: 0 },
pcursor: PCursor {
paragraph: 0,
offset: 10000,
prefer_next_row: false,
},
})));
TextEdit::store_state(ui.ctx(), te_id, state);
}
}
func_edit_focus
}
/// Creates and does the math for creating all the rectangles under the
/// graph
fn integral_rectangles(
@@ -289,9 +307,6 @@ impl FunctionEntry {
(data2, area)
}
/// Returns `self.func_str`
pub fn get_func_str(&self) -> &str { &self.func_str }
/// Helps with processing newton's method depending on level of derivative
fn newtons_method_helper(
&self, threshold: &f64, derivative_level: usize,
@@ -444,7 +459,6 @@ impl FunctionEntry {
/// Displays the function's output on PlotUI `plot_ui` with settings
/// `settings`. Returns an `Option<f64>` of the calculated integral
pub fn display(&self, plot_ui: &mut PlotUi, settings: &AppSettings) -> Option<f64> {
let func_str = self.get_func_str();
let derivative_str = self.function.get_derivative_str();
let step = (settings.integral_min_x - settings.integral_max_x).abs()
/ (settings.integral_num as f64);
@@ -455,7 +469,7 @@ impl FunctionEntry {
.clone()
.to_line()
.color(Color32::RED)
.name(func_str),
.name(self.get_func_raw()),
);
}
@@ -595,7 +609,7 @@ mod tests {
sum: Riemann, integral_min_x: f64, integral_max_x: f64, pixel_width: usize,
integral_num: usize,
) -> AppSettings {
crate::egui_app::AppSettings {
crate::math_app::AppSettings {
riemann_sum: sum,
integral_min_x,
integral_max_x,