Skip to content

Commit

Permalink
Add web event handler for egui's cursor change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
white-axe committed Oct 8, 2023
1 parent cb6f6ab commit c9e5116
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/filesystem/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ pub fn setup_main_thread_hooks(mut filesystem_rx: mpsc::UnboundedReceiver<FileSy

loop {
let Some(command) = filesystem_rx.recv().await else {
tracing::warn!(
"FileSystem main thread loop is stopping! This is not supposed to happen."
);
return;
};
tracing::debug!("Main thread received FS command: {:?}", command,);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ use tabs::tab::Tab;
pub struct GlobalState {
pub prefers_color_scheme_dark: Option<bool>,
pub filesystem_tx: mpsc::UnboundedSender<filesystem::web::FileSystemCommand>,
pub output_tx: mpsc::UnboundedSender<egui::PlatformOutput>,
}

#[cfg(target_arch = "wasm32")]
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ pub fn luminol_main_start() {
let (filesystem_tx, filesystem_rx) = mpsc::unbounded_channel();
filesystem::web::setup_main_thread_hooks(filesystem_rx);

let (output_tx, output_rx) = mpsc::unbounded_channel();

if luminol::GLOBAL_STATE
.set(luminol::GlobalState {
prefers_color_scheme_dark,
filesystem_tx,
output_tx,
})
.is_err()
{
Expand Down Expand Up @@ -189,6 +192,7 @@ pub fn luminol_main_start() {
canvas,
state.event_tx.clone(),
state.custom_event_tx.clone(),
output_rx,
);
});
worker.set_onmessage(Some(callback.as_ref().unchecked_ref()));
Expand Down Expand Up @@ -236,6 +240,7 @@ pub async fn luminol_worker_start(canvas: web_sys::OffscreenCanvas) {
state.prefers_color_scheme_dark,
Some(event_rx),
Some(custom_event_rx),
Some(state.output_tx.clone()),
)
.await;
runner.setup_render_hooks();
Expand Down
65 changes: 65 additions & 0 deletions src/web/web_worker_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct WebWorkerRunnerState {

event_rx: Option<mpsc::UnboundedReceiver<egui::Event>>,
custom_event_rx: Option<mpsc::UnboundedReceiver<WebWorkerRunnerEvent>>,
output_tx: Option<mpsc::UnboundedSender<egui::PlatformOutput>>,
}

/// A runner for wgpu egui applications intended to be run in a web worker.
Expand All @@ -82,6 +83,7 @@ impl WebWorkerRunner {
prefers_color_scheme_dark: Option<bool>,
event_rx: Option<mpsc::UnboundedReceiver<egui::Event>>,
custom_event_rx: Option<mpsc::UnboundedReceiver<WebWorkerRunnerEvent>>,
output_tx: Option<mpsc::UnboundedSender<egui::PlatformOutput>>,
) -> Self {
let Some(worker) = bindings::worker() else {
panic!("cannot use `WebWorkerRunner::new()` outside of a web worker");
Expand Down Expand Up @@ -193,6 +195,7 @@ impl WebWorkerRunner {
pixel_ratio: 1.,
event_rx,
custom_event_rx,
output_tx,
})),
context,
time_lock,
Expand Down Expand Up @@ -283,6 +286,9 @@ impl WebWorkerRunner {
let output = self
.context
.run(input, |_| state.app.custom_update(&self.context));
if let Some(output_tx) = &state.output_tx {
let _ = output_tx.send(output.platform_output);
}
let clear_color = state.app.clear_color(&self.context.style().visuals);
let paint_jobs = self.context.tessellate(output.shapes);

Expand Down Expand Up @@ -384,6 +390,7 @@ pub fn setup_main_thread_hooks(
canvas: web_sys::HtmlCanvasElement,
event_tx: mpsc::UnboundedSender<egui::Event>,
custom_event_tx: mpsc::UnboundedSender<WebWorkerRunnerEvent>,
mut output_rx: mpsc::UnboundedReceiver<egui::PlatformOutput>,
) {
let window =
web_sys::window().expect("cannot run `setup_main_thread_hooks()` outside of main thread");
Expand Down Expand Up @@ -748,4 +755,62 @@ pub fn setup_main_thread_hooks(
.expect("failed to register canvas mutation observer");
callback.forget();
}

wasm_bindgen_futures::spawn_local(async move {
let body_style = window.document().unwrap().body().unwrap().style();
loop {
let Some(output) = output_rx.recv().await else {
tracing::warn!(
"WebWorkerRunner main thread loop is stopping! This is not supposed to happen."
);
return;
};

let _ = body_style.set_property(
"cursor",
match output.cursor_icon {
egui::CursorIcon::Default => "default",
egui::CursorIcon::None => "none",

egui::CursorIcon::ContextMenu => "context-menu",
egui::CursorIcon::Help => "help",
egui::CursorIcon::PointingHand => "pointer",
egui::CursorIcon::Progress => "progress",
egui::CursorIcon::Wait => "wait",

egui::CursorIcon::Cell => "cell",
egui::CursorIcon::Crosshair => "crosshair",
egui::CursorIcon::Text => "text",
egui::CursorIcon::VerticalText => "vertical-text",

egui::CursorIcon::Alias => "alias",
egui::CursorIcon::Copy => "copy",
egui::CursorIcon::Move => "move",
egui::CursorIcon::NoDrop => "no-drop",
egui::CursorIcon::NotAllowed => "not-allowed",
egui::CursorIcon::Grab => "grab",
egui::CursorIcon::Grabbing => "grabbing",

egui::CursorIcon::AllScroll => "all-scroll",
egui::CursorIcon::ResizeColumn => "col-resize",
egui::CursorIcon::ResizeRow => "row-resize",
egui::CursorIcon::ResizeNorth => "n-resize",
egui::CursorIcon::ResizeEast => "e-resize",
egui::CursorIcon::ResizeSouth => "s-resize",
egui::CursorIcon::ResizeWest => "w-resize",
egui::CursorIcon::ResizeNorthEast => "ne-resize",
egui::CursorIcon::ResizeNorthWest => "nw-resize",
egui::CursorIcon::ResizeSouthEast => "se-resize",
egui::CursorIcon::ResizeSouthWest => "sw-resize",
egui::CursorIcon::ResizeHorizontal => "ew-resize",
egui::CursorIcon::ResizeVertical => "ns-resize",
egui::CursorIcon::ResizeNwSe => "nwse-resize",
egui::CursorIcon::ResizeNeSw => "nesw-resize",

egui::CursorIcon::ZoomIn => "zoom-in",
egui::CursorIcon::ZoomOut => "zoom-out",
},
);
}
});
}

0 comments on commit c9e5116

Please sign in to comment.