From 22d1be59f5ad7c2386f98989b583a076baeeeb86 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 22 Apr 2022 14:18:24 -0400 Subject: [PATCH] start working on better mobile support --- src/function_entry.rs | 5 ++++- src/lib.rs | 1 + src/main.rs | 1 + src/math_app.rs | 27 +++++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/function_entry.rs b/src/function_entry.rs index 999b644..0502097 100644 --- a/src/function_entry.rs +++ b/src/function_entry.rs @@ -101,7 +101,9 @@ impl Default for FunctionEntry { impl FunctionEntry { /// 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 { + pub fn function_entry( + &mut self, ui: &mut egui::Ui, can_remove: bool, i: usize, mobile: bool, + ) -> bool { let output_string = self.autocomplete.string.clone(); self.update_string(&output_string); @@ -727,6 +729,7 @@ mod tests { do_extrema: false, do_roots: false, plot_width: pixel_width, + is_mobile: false, } } diff --git a/src/lib.rs b/src/lib.rs index d20ef31..f27ec82 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![feature(const_mut_refs)] #![feature(let_chains)] +#![feature(stmt_expr_attributes)] #[macro_use] extern crate static_assertions; diff --git a/src/main.rs b/src/main.rs index 3909338..659a70b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(const_mut_refs)] #![feature(let_chains)] +#![feature(stmt_expr_attributes)] #[macro_use] extern crate static_assertions; diff --git a/src/math_app.rs b/src/math_app.rs index af43bee..596fa25 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -30,6 +30,14 @@ cfg_if::cfg_if! { // Remove the element loading_element.remove(); } + + fn is_mobile() -> Option { + const MOBILE_DEVICE: [&str; 6] = ["Android", "iPhone", "iPad", "iPod", "webOS", "BlackBerry"]; + + let user_agent = web_sys::window()?.navigator().user_agent().ok()?; + Some(MOBILE_DEVICE.iter().any(|&name| user_agent.contains(name))) + } + } } @@ -60,6 +68,8 @@ pub struct AppSettings { /// Stores current plot pixel width pub plot_width: usize, + + pub is_mobile: bool, } impl Default for AppSettings { @@ -75,6 +85,7 @@ impl Default for AppSettings { do_extrema: true, do_roots: true, plot_width: 0, + is_mobile: false, } } } @@ -124,10 +135,19 @@ impl MathApp { pub fn new(cc: &eframe::CreationContext<'_>) -> Self { let start = instant::Instant::now(); + #[allow(unused_mut)] + #[allow(unused_assignments)] + let mut mobile = false; + // Remove loading indicator on wasm #[cfg(target_arch = "wasm32")] stop_loading(); + #[cfg(target_arch = "wasm32")] + { + mobile = is_mobile().unwrap_or_default(); + } + #[cfg(threading)] tracing::info!("Threading: Enabled"); @@ -256,7 +276,10 @@ impl MathApp { dark_mode: true, text: text_data.expect("text.json failed to load"), opened: Opened::default(), - settings: AppSettings::default(), + settings: AppSettings { + is_mobile: mobile, + ..AppSettings::default() + }, } } @@ -362,7 +385,7 @@ impl MathApp { let mut remove_i: Option = None; for (i, function) in self.functions.iter_mut().enumerate() { // Entry for a function - if function.function_entry(ui, can_remove, i) { + if function.function_entry(ui, can_remove, i, self.settings.is_mobile) { remove_i = Some(i); }