Skip to content

Commit

Permalink
res IsMac now a local, init via Once; removed comments, use user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed May 31, 2023
1 parent 070f108 commit 8768a9c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bevy = { version = "0.10", default-features = false, features = [

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = "0.28"
web-sys = { version = "*", features = [
web-sys = { version = "0.3.63", features = [
"Clipboard",
"ClipboardEvent",
"DataTransfer",
Expand Down
19 changes: 0 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ impl Default for EguiSettings {
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
pub struct EguiInput(pub egui::RawInput);

/// A resource to check if we're on a mac, correctly detects on web too.
#[derive(Resource)]
pub struct IsMac(pub bool);

/// A resource for accessing clipboard.
///
/// The resource is available only if `manage_clipboard` feature is enabled.
Expand Down Expand Up @@ -557,21 +553,6 @@ impl Plugin for EguiPlugin {
world.init_resource::<EguiClipboard>();
world.init_resource::<EguiUserTextures>();
world.init_resource::<EguiMousePosition>();
if !world.contains_resource::<IsMac>() {
let mut is_mac = cfg!(target_os = "macos");
#[cfg(target_arch = "wasm32")]
{
let window = web_sys::window().expect("window");

let nav = window.navigator();
let platform = nav.platform();
if let Ok(platform) = platform {
is_mac = platform.starts_with("Mac");
}
}
log::info!(is_mac);
world.insert_resource(IsMac(is_mac));
}

#[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))]
app.add_startup_system(web_clipboard::startup_setup_web_events);
Expand Down
27 changes: 23 additions & 4 deletions src/systems.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
EguiContext, EguiContextQuery, EguiInput, EguiMousePosition, EguiSettings, IsMac, WindowSize,
EguiContext, EguiContextQuery, EguiInput, EguiMousePosition, EguiSettings, WindowSize,
};
#[cfg(feature = "open_url")]
use bevy::log;
Expand Down Expand Up @@ -71,14 +71,33 @@ pub struct ContextSystemParams<'w, 's> {

/// Processes Bevy input and feeds it to Egui.
pub fn process_input_system(
mut is_mac: Res<IsMac>,
mut input_events: InputEvents,
mut input_resources: InputResources,
mut context_params: ContextSystemParams,
egui_settings: Res<EguiSettings>,
mut egui_mouse_position: ResMut<EguiMousePosition>,
time: Res<Time>,
mut is_mac: Local<bool>,
) {
use std::sync::Once;
static START: Once = Once::new();

START.call_once(|| {
// run initialization here
*is_mac = cfg!(target_os = "macos");
#[cfg(target_arch = "wasm32")]
{
let window = web_sys::window().expect("window");

let nav = window.navigator();
let user_agent = nav.user_agent();
if let Ok(user_agent) = user_agent {
log::debug!("{:?}", user_agent);
*is_mac = user_agent.contains("Macintosh;");
}
}
});

// This is a workaround for Windows. For some reason, `WindowFocused` event isn't fired
// when a window is created.
if let Some(event) = input_events.ev_window_created.iter().last() {
Expand All @@ -102,8 +121,8 @@ pub fn process_input_system(
let win = input_resources.keyboard_input.pressed(KeyCode::LWin)
|| input_resources.keyboard_input.pressed(KeyCode::RWin);

let mac_cmd = if is_mac.0 { win } else { false };
let command = if is_mac.0 { win } else { ctrl };
let mac_cmd = if *is_mac { win } else { false };
let command = if *is_mac { win } else { ctrl };

let modifiers = egui::Modifiers {
alt,
Expand Down
21 changes: 6 additions & 15 deletions src/web_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,14 @@ fn setup_clipboard_copy(clipboard_channel: &mut WebChannel<WebEventCopy>) {
let (tx, rx): (Sender<WebEventCopy>, Receiver<WebEventCopy>) = crossbeam_channel::bounded(1);

let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::ClipboardEvent| {
// TODO: maybe we should check if current canvas is selected ? not sure it's possible,
// but reacting to event at the document level will lead to problems if multiple games are on the same page.
tx.try_send(WebEventCopy);
});

// TODO: a lot of unwraps ; it's using documents because paste event set on a canvas do not trigger (also tested on firefox in vanilla javascript)
let listener = closure.as_ref().unchecked_ref();
web_sys::window()
.unwrap()
.expect("Could not retrieve web_sys::window()")
.document()
.unwrap()
.expect("Could not retrieve web_sys window's document")
.add_event_listener_with_callback("copy", listener)
.expect("Could not add copy event listener.");
closure.forget();
Expand All @@ -70,17 +67,14 @@ fn setup_clipboard_cut(clipboard_channel: &mut WebChannel<WebEventCut>) {
let (tx, rx): (Sender<WebEventCut>, Receiver<WebEventCut>) = crossbeam_channel::bounded(1);

let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::ClipboardEvent| {
// TODO: maybe we should check if current canvas is selected ? not sure it's possible,
// but reacting to event at the document level will lead to problems if multiple games are on the same page.
tx.try_send(WebEventCut);
});

// TODO: a lot of unwraps ; it's using documents because paste event set on a canvas do not trigger (also tested on firefox in vanilla javascript)
let listener = closure.as_ref().unchecked_ref();
web_sys::window()
.unwrap()
.expect("Could not retrieve web_sys::window()")
.document()
.unwrap()
.expect("Could not retrieve web_sys window's document")
.add_event_listener_with_callback("cut", listener)
.expect("Could not add cut event listener.");
closure.forget();
Expand All @@ -91,8 +85,6 @@ fn setup_clipboard_paste(clipboard_channel: &mut WebChannel<WebEventPaste>) {
let (tx, rx): (Sender<WebEventPaste>, Receiver<WebEventPaste>) = crossbeam_channel::bounded(1);

let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::ClipboardEvent| {
// TODO: maybe we should check if current canvas is selected ? not sure it's possible,
// but reacting to event at the document level will lead to problems if multiple games are on the same page.
match event
.clipboard_data()
.expect("could not get clipboard data.")
Expand All @@ -108,12 +100,11 @@ fn setup_clipboard_paste(clipboard_channel: &mut WebChannel<WebEventPaste>) {
info!("{:?}", event.clipboard_data())
});

// TODO: a lot of unwraps ; it's using documents because paste event set on a canvas do not trigger (also tested on firefox in vanilla javascript)
let listener = closure.as_ref().unchecked_ref();
web_sys::window()
.unwrap()
.expect("Could not retrieve web_sys::window()")
.document()
.unwrap()
.expect("Could not retrieve web_sys window's document")
.add_event_listener_with_callback("paste", listener)
.expect("Could not add paste event listener.");
closure.forget();
Expand Down

0 comments on commit 8768a9c

Please sign in to comment.