rpack/crates/rpack_egui/src/main.rs

74 lines
2.5 KiB
Rust

#![warn(clippy::all, rust_2018_idioms)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result<()> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let file_arg: Option<String> = if std::env::args().len() > 1 {
std::env::args().skip(1).next()
} else {
None
};
let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([400.0, 300.0])
.with_icon(
eframe::icon_data::from_png_bytes(include_bytes!("../static/base_icon.png"))
.unwrap_or_default(),
)
.with_min_inner_size([400.0, 300.0]),
..Default::default()
};
eframe::run_native(
"rPack",
native_options,
Box::new(|cc| Ok(Box::new(rpack_egui::Application::new(cc, file_arg)))),
)
}
// When compiling to web using trunk:
#[cfg(target_arch = "wasm32")]
fn main() {
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
let web_options = eframe::WebOptions::default();
wasm_bindgen_futures::spawn_local(async {
use web_sys::wasm_bindgen::JsCast;
use web_sys::{HtmlCanvasElement, window};
let canvas = window()
.and_then(|w| w.document())
.and_then(|d| d.get_element_by_id("the_canvas_id"))
.expect("No Canvas found")
.dyn_into::<HtmlCanvasElement>()
.expect("Could not cast to Canvas");
let start_result = eframe::WebRunner::new()
.start(
canvas,
web_options,
Box::new(|cc| Ok(Box::new(rpack_egui::Application::new(cc, None)))),
)
.await;
// Remove the loading text and spinner:
let loading_text = window()
.and_then(|w| w.document())
.and_then(|d| d.get_element_by_id("loading_text"));
if let Some(loading_text) = loading_text {
match start_result {
Ok(_) => {
loading_text.remove();
}
Err(e) => {
loading_text.set_inner_html(
"<p> The app has crashed. See the developer console for details. </p>",
);
panic!("Failed to start eframe: {e:?}");
}
}
}
});
}