Skip to content

Commit

Permalink
retrieve user agent for better platform detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Feb 7, 2024
1 parent f095c38 commit f1fdc76
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ name = "ui"
required-features = ["render"]

[dependencies]
bevy = { version = "0.12", default-features = false, features = [
"bevy_asset",
] }
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] }
egui = { version = "0.24.0", default-features = false, features = ["bytemuck"] }
webbrowser = { version = "0.8.2", optional = true }

[target.'cfg(not(any(target_arch = "wasm32", target_os = "android")))'.dependencies]
arboard = { version = "3.2.0", optional = true }
thread_local = { version = "1.1.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.63", features = ["Navigator"] }

[dev-dependencies]
once_cell = "1.16.0"
version-sync = "0.9.4"
Expand Down
8 changes: 7 additions & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ fn main() {
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.insert_resource(Msaa::Sample4)
.init_resource::<UiState>()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
prevent_default_event_handling: false,
..default()
}),
..default()
}))
.add_plugins(EguiPlugin)
.add_systems(Startup, configure_visuals_system)
.add_systems(Startup, configure_ui_state_system)
Expand Down
51 changes: 48 additions & 3 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ impl<'w, 's> InputEvents<'w, 's> {
#[derive(Resource, Default)]
pub struct TouchId(pub Option<u64>);

/// Initialized once at runtime in `process_input_system`,
/// to know interally which native platform we're running on. Leverages user-agent on web.
#[derive(Default, Debug, Clone, PartialEq)]
pub enum RuntimeHostPlatform {
/// Unknown platform, not relevant to our logic.
#[default]
Other,
/// Apple desktop
Macintosh,
/// Windows
Windows,
}

#[allow(missing_docs)]
#[derive(SystemParam)]
pub struct InputResources<'w, 's> {
Expand Down Expand Up @@ -86,7 +99,35 @@ pub fn process_input_system(
egui_settings: Res<EguiSettings>,
mut egui_mouse_position: ResMut<EguiMousePosition>,
time: Res<Time<Real>>,
mut runtime_host_platform: Local<RuntimeHostPlatform>,
) {
use std::sync::Once;
static START: Once = Once::new();

START.call_once(|| {
// run initialization here
if cfg!(target_os = "macos") {
*runtime_host_platform = RuntimeHostPlatform::Macintosh;
} else if cfg!(target_os = "windows") {
*runtime_host_platform = RuntimeHostPlatform::Windows
}

#[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);
if user_agent.contains("(Windows") {
*runtime_host_platform = RuntimeHostPlatform::Windows;
} else if user_agent.contains("Macintosh;") {
*runtime_host_platform = RuntimeHostPlatform::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.read().last() {
Expand All @@ -112,12 +153,16 @@ pub fn process_input_system(
let win = input_resources.keyboard_input.pressed(KeyCode::SuperLeft)
|| input_resources.keyboard_input.pressed(KeyCode::SuperRight);

let mac_cmd = if cfg!(target_os = "macos") {
let mac_cmd = if *runtime_host_platform == RuntimeHostPlatform::Macintosh {
win
} else {
false
};
let command = if cfg!(target_os = "macos") { win } else { ctrl };
let command = if *runtime_host_platform == RuntimeHostPlatform::Windows {
win
} else {
ctrl
};

let modifiers = egui::Modifiers {
alt,
Expand Down Expand Up @@ -220,7 +265,7 @@ pub fn process_input_system(
}
}

if !command || cfg!(target_os = "windows") && ctrl && alt {
if !command || *runtime_host_platform == RuntimeHostPlatform::Windows && ctrl && alt {
for event in input_events.ev_received_character.read() {
if !event.char.is_control() {
let mut context = context_params.contexts.get_mut(event.window).unwrap();
Expand Down

0 comments on commit f1fdc76

Please sign in to comment.