2022-03-21 20:48:35 +00:00
|
|
|
//! Demo app for egui
|
2022-10-30 19:55:07 +00:00
|
|
|
#![allow(clippy::missing_errors_doc)]
|
2022-03-21 20:48:35 +00:00
|
|
|
|
2022-04-28 09:23:34 +00:00
|
|
|
mod apps;
|
|
|
|
mod backend_panel;
|
|
|
|
pub(crate) mod frame_history;
|
|
|
|
mod wrap_app;
|
|
|
|
|
2022-08-02 15:42:55 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2022-08-02 15:37:12 +00:00
|
|
|
use eframe::web::AppRunnerRef;
|
2022-08-02 15:42:55 +00:00
|
|
|
|
2022-04-28 09:23:34 +00:00
|
|
|
pub use wrap_app::WrapApp;
|
|
|
|
|
|
|
|
/// Time of day as seconds since midnight. Used for clock in demo app.
|
2022-07-11 21:08:48 +00:00
|
|
|
pub(crate) fn seconds_since_midnight() -> f64 {
|
2022-04-28 09:23:34 +00:00
|
|
|
use chrono::Timelike;
|
|
|
|
let time = chrono::Local::now().time();
|
2022-07-11 21:08:48 +00:00
|
|
|
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64)
|
2022-04-28 09:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-12-19 20:30:51 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-12-29 14:57:13 +00:00
|
|
|
use eframe::wasm_bindgen::{self, prelude::*};
|
2020-04-12 10:07:51 +00:00
|
|
|
|
2020-12-19 20:30:51 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-07-18 16:35:17 +00:00
|
|
|
#[wasm_bindgen]
|
2022-08-02 15:42:55 +00:00
|
|
|
pub struct WebHandle {
|
2022-08-02 15:37:12 +00:00
|
|
|
handle: AppRunnerRef,
|
2022-08-02 15:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl WebHandle {
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn stop_web(&self) -> Result<(), wasm_bindgen::JsValue> {
|
|
|
|
let mut app = self.handle.lock();
|
2022-10-30 19:55:07 +00:00
|
|
|
app.destroy()
|
2022-08-02 15:42:55 +00:00
|
|
|
}
|
2022-09-09 06:22:46 +00:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn set_some_content_from_javasript(&mut self, _some_data: &str) {
|
|
|
|
let _app = self.handle.lock().app_mut::<WrapApp>();
|
|
|
|
// _app.data = some_data;
|
|
|
|
}
|
2022-08-02 15:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn init_wasm_hooks() {
|
2022-02-10 14:44:41 +00:00
|
|
|
// Make sure panics are logged using `console.error`.
|
|
|
|
console_error_panic_hook::set_once();
|
|
|
|
|
|
|
|
// Redirect tracing to console.log and friends:
|
|
|
|
tracing_wasm::set_as_global_default();
|
2022-08-02 15:42:55 +00:00
|
|
|
}
|
2022-02-10 14:44:41 +00:00
|
|
|
|
2022-08-02 15:42:55 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[wasm_bindgen]
|
2022-10-05 18:14:18 +00:00
|
|
|
pub async fn start_separate(canvas_id: &str) -> Result<WebHandle, wasm_bindgen::JsValue> {
|
2022-06-09 15:41:59 +00:00
|
|
|
let web_options = eframe::WebOptions::default();
|
2022-11-16 18:08:03 +00:00
|
|
|
eframe::start_web(
|
2022-06-09 15:41:59 +00:00
|
|
|
canvas_id,
|
|
|
|
web_options,
|
|
|
|
Box::new(|cc| Box::new(WrapApp::new(cc))),
|
|
|
|
)
|
2022-10-05 18:14:18 +00:00
|
|
|
.await
|
2022-11-16 18:08:03 +00:00
|
|
|
.map(|handle| WebHandle { handle })
|
2022-08-02 15:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is the entry-point for all the web-assembly.
|
|
|
|
/// This is called once from the HTML.
|
|
|
|
/// It loads the app, installs some callbacks, then returns.
|
|
|
|
/// You can add more callbacks like this if you want to call in to your code.
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[wasm_bindgen]
|
2022-10-05 18:14:18 +00:00
|
|
|
pub async fn start(canvas_id: &str) -> Result<WebHandle, wasm_bindgen::JsValue> {
|
2022-08-02 15:42:55 +00:00
|
|
|
init_wasm_hooks();
|
2022-10-05 18:14:18 +00:00
|
|
|
start_separate(canvas_id).await
|
2020-07-18 16:00:05 +00:00
|
|
|
}
|