egui: make the WASM port actually work

This commit is contained in:
Mikolaj Wielgus 2024-03-02 16:09:01 +00:00
parent 95d3b8291e
commit e50fb26bdd
5 changed files with 176 additions and 21 deletions

BIN
assets/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

26
assets/sw.js Normal file
View File

@ -0,0 +1,26 @@
var cacheName = 'topola-egui';
var filesToCache = [
'./',
'./index.html',
// I'm not sure if these two work, as teir filenames in dist/ have an alphanumeric string appended.
'./topola-egui.js',
'./topola-egui_bg.wasm',
];
// Start the service worker and cache all of the app's content.
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(cacheName).then(function (cache) {
return cache.addAll(filesToCache);
})
);
});
// Serve cached content when offline.
self.addEventListener('fetch', function (e) {
e.respondWith(
caches.match(e.request).then(function (response) {
return response || fetch(e.request);
})
);
});

122
index.html Normal file
View File

@ -0,0 +1,122 @@
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Disable zooming: -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<head>
<title>Topola</title>
<link data-trunk rel="rust" data-bin="topola-egui" data-cargo-features="egui" data-wasm-opt="2" />
<base data-trunk-public-url />
<link data-trunk rel="icon" href="assets/favicon.ico">
<!-- TODO: More icons -->
<meta name="theme-color" media="(prefers-color-scheme: light)" content="white">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#404040">
<style>
html {
/* Remove touch delay: */
touch-action: manipulation;
}
body {
/* Light mode background color for what is not covered by the egui canvas,
or where the egui canvas is translucent. */
background: #909090;
}
@media (prefers-color-scheme: dark) {
body {
/* Dark mode background color for what is not covered by the egui canvas,
or where the egui canvas is translucent. */
background: #404040;
}
}
/* Allow canvas to fill entire web page: */
html,
body {
overflow: hidden;
margin: 0 !important;
padding: 0 !important;
height: 100%;
width: 100%;
}
/* Position canvas in center-top: */
canvas {
margin-right: auto;
margin-left: auto;
display: block;
position: absolute;
top: 0%;
left: 50%;
transform: translate(-50%, 0%);
}
.centered {
margin-right: auto;
margin-left: auto;
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #f0f0f0;
font-size: 24px;
font-family: Ubuntu-Light, Helvetica, sans-serif;
text-align: center;
}
/* ---------------------------------------------- */
/* Loading animation from https://loading.io/css/ */
.lds-dual-ring {
display: inline-block;
width: 24px;
height: 24px;
}
.lds-dual-ring:after {
content: " ";
display: block;
width: 24px;
height: 24px;
margin: 0px;
border-radius: 50%;
border: 3px solid #fff;
border-color: #fff transparent #fff transparent;
animation: lds-dual-ring 1.2s linear infinite;
}
@keyframes lds-dual-ring {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<canvas id="topola-egui"></canvas>
<!--Register a service worker to cache the WASM and JS scripts for offline use. -->
<script>
// Disable caching during development so that we don't end up seeing an outdated version.
if ('serviceWorker' in navigator && window.location.hash !== "#dev") {
window.addEventListener('load', function () {
navigator.serviceWorker.register('sw.js');
});
}
</script>
</body>
</html>

View File

@ -19,8 +19,9 @@ pub struct App {
// Example stuff:
label: String,
//#[serde(skip)] // Don't serialize this field.
//text_channel: (Sender<String>, Receiver<String>),
#[serde(skip)] // Don't serialize this field.
file_handle_channel: (Sender<rfd::FileHandle>, Receiver<rfd::FileHandle>),
#[serde(skip)]
design: Option<DsnDesign>,
}
@ -30,7 +31,7 @@ impl Default for App {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
//text_channel: channel(),
file_handle_channel: channel(),
design: None,
}
}
@ -59,32 +60,38 @@ impl eframe::App for App {
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if let Ok(file_handle) = self.file_handle_channel.1.try_recv() {
dbg!(file_handle);
// TODO: actually load the file from the handle.
}
if ui.button("Open").clicked() {
// `Context` is cheap to clone as it's wrapped in an `Arc`.
let ctx = ui.ctx().clone();
// NOTE: This requires Zenity to be installed on your system.
// NOTE: On Linux, this requires Zenity to be installed on your system.
let sender = self.file_handle_channel.0.clone();
let task = rfd::AsyncFileDialog::new().pick_file();
// Doing this synchronously may not work on WASM. I haven't tested this
// yet, so I'm leaving a commented-out asynchronous version further below.
let maybe_path = rfd::FileDialog::new().pick_file();
execute(async move {
let maybe_file_handle = task.await;
if let Some(path) = maybe_path {
self.design = DsnDesign::load_from_file(path.to_str().unwrap()).ok();
}
//let task = rfd::AsyncFileDialog::new().pick_file();
/*execute(async move {
let file = task.await;
if let Some(file) = file {
if let Some(file_handle) = maybe_file_handle {
let _ = sender.send(file_handle);
ctx.request_repaint();
}
/*if let Some(file) = file {
let text = file.read().await;
let _ = sender.send(String::from_utf8_lossy(&text).to_string());
ctx.request_repaint();
}
});*/
}*/
});
}
if ui.button("Quit").clicked() {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
// "Quit" button wouldn't work on a Web page.
if !cfg!(target_arch = "wasm32") {
if ui.button("Quit").clicked() {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}
}
});
ui.add_space(16.0);

View File

@ -33,9 +33,9 @@ fn main() {
wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new()
.start(
"the_canvas_id",
"topola-egui",
web_options,
Box::new(|cc| Box::new(TemplateApp::new(cc))),
Box::new(|cc| Box::new(App::new(cc))),
)
.await
.expect("failed to start eframe");