diff --git a/build.rs b/build.rs index 7c15ffc..442ab08 100644 --- a/build.rs +++ b/build.rs @@ -8,8 +8,8 @@ use std::{ }; use epaint::{ - text::{FontData, FontDefinitions, FontTweak}, FontFamily, + text::{FontData, FontDefinitions, FontTweak}, }; use run_script::ScriptOptions; diff --git a/src/function_entry.rs b/src/function_entry.rs index f825bdc..e563c2c 100644 --- a/src/function_entry.rs +++ b/src/function_entry.rs @@ -449,7 +449,7 @@ impl FunctionEntry { Some(integral_data) => { if integral_step > step { plot_ui.bar_chart( - BarChart::new(integral_data.0.clone()) + BarChart::new("integral bar chart", integral_data.0.clone()) .color(Color32::BLUE) .width(integral_step), ); diff --git a/src/function_manager.rs b/src/function_manager.rs index 9981c88..d3b1e7d 100644 --- a/src/function_manager.rs +++ b/src/function_manager.rs @@ -1,7 +1,7 @@ use crate::{ consts::COLORS, function_entry::FunctionEntry, misc::random_u64, widgets::widgets_ontop, }; -use egui::{Button, Id, Key, Modifiers, TextEdit, WidgetText}; +use egui::{Button, Id, Key, Modifiers, PopupCloseBehavior, TextEdit, WidgetText}; use emath::vec2; use parsing::Movement; use serde::ser::SerializeStruct; @@ -147,17 +147,23 @@ impl FunctionManager { let autocomplete_popup_id = Id::new("autocomplete popup"); - egui::popup_below_widget(ui, autocomplete_popup_id, &re, |ui| { - hints.iter().enumerate().for_each(|(i, candidate)| { - if ui - .selectable_label(i == function.autocomplete.i, *candidate) - .clicked() - { - clicked = true; - function.autocomplete.i = i; - } - }); - }); + egui::popup_below_widget( + ui, + autocomplete_popup_id, + &re, + PopupCloseBehavior::CloseOnClickOutside, + |ui| { + hints.iter().enumerate().for_each(|(i, candidate)| { + if ui + .selectable_label(i == function.autocomplete.i, *candidate) + .clicked() + { + clicked = true; + function.autocomplete.i = i; + } + }); + }, + ); if clicked { function @@ -172,10 +178,14 @@ impl FunctionManager { // Push cursor to end if needed if movement == Movement::Complete { + // TODO! proper error handling let mut state = unsafe { TextEdit::load_state(ui.ctx(), te_id).unwrap_unchecked() }; let ccursor = egui::text::CCursor::new(function.autocomplete.string.len()); - state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor))); + state + .cursor + .set_char_range(Some(egui::text::CCursorRange::one(ccursor))); + TextEdit::store_state(ui.ctx(), te_id, state); } } diff --git a/src/lib.rs b/src/lib.rs index f2c1d2f..f4df882 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ pub use crate::{ cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] { use wasm_bindgen::prelude::*; + use web_sys::HtmlCanvasElement; use lol_alloc::{FreeListAllocator, LockedAllocator}; #[global_allocator] @@ -49,26 +50,28 @@ cfg_if::cfg_if! { } } - /// Call this once from JavaScript to start your app. - #[wasm_bindgen] - pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> { - self.runner - .start( - canvas_id, - eframe::WebOptions::default(), - Box::new(|cc| Box::new(math_app::MathApp::new(cc))), - ) - .await - } + /// Call this once from JavaScript to start your app. + #[wasm_bindgen] + pub async fn start(&self, canvas_id: HtmlCanvasElement) -> Result<(), wasm_bindgen::JsValue> { + self.runner + .start( + canvas_id, + eframe::WebOptions::default(), + Box::new(|cc| Ok(Box::new(math_app::MathApp::new(cc)))), + ) + .await + } } #[wasm_bindgen(start)] pub async fn start() { tracing::info!("Starting..."); + let document = web_sys::window().unwrap().document().unwrap(); + let canvas = document.get_element_by_id("canvas").unwrap().dyn_into::().unwrap(); let web_handle = WebHandle::new(); - web_handle.start("canvas").await.unwrap() + web_handle.start(canvas).await.unwrap() } } } diff --git a/src/math_app.rs b/src/math_app.rs index 123afa5..accfaee 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -6,13 +6,13 @@ use crate::{ }; use eframe::App; use egui::{ - Button, CentralPanel, Color32, ComboBox, Context, DragValue, Frame, Key, Layout, SidePanel, - TopBottomPanel, Ui, Vec2, Window, style::Margin, + Button, CentralPanel, Color32, ComboBox, Context, CornerRadius, DragValue, Frame, Key, Layout, + SidePanel, TopBottomPanel, Ui, Vec2, Window, }; use egui_plot::Plot; use emath::{Align, Align2}; -use epaint::Rounding; +use epaint::{Margin, Rounding}; use instant::Instant; use itertools::Itertools; use std::{io::Read, ops::BitXorAssign}; @@ -535,11 +535,11 @@ impl App for MathApp { // Central panel which contains the central plot (or an error created when parsing) CentralPanel::default() .frame(Frame { - inner_margin: Margin::symmetric(0.0, 0.0), - rounding: Rounding::ZERO, + inner_margin: Margin::ZERO, + corner_radius: CornerRadius::ZERO, // fill: crate::style::STYLE.window_fill(), fill: Color32::from_gray(27), - ..Frame::none() + ..Frame::NONE }) .show(ctx, |ui| { // Display an error if it exists diff --git a/src/misc.rs b/src/misc.rs index 9ed1e93..e39c3d7 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -1,4 +1,4 @@ -use base64::{engine::general_purpose, Engine as _}; +use base64::{Engine as _, engine::general_purpose}; use egui_plot::{Line, PlotPoint, PlotPoints, Points}; use emath::Pos2; use getrandom::getrandom;