Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

#1298: browser OS for the web keymap

aiongga8ca532c4eeb
diff --git a/crates/ui-egui/src/keys.rs b/crates/ui-egui/src/keys.rs
index 8600dbae5..784ee5dbc 100644
--- a/crates/ui-egui/src/keys.rs
+++ b/crates/ui-egui/src/keys.rs
@@ -4,6 +4,7 @@
use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;
+use std::sync::atomic::{AtomicU8, Ordering};
use craft_keymap::{Bindable, Binding, Chord, CommandRef, Key, Keymap, Kind, Mods, Overrides, Platform, Policy, Scope, Scopes};
use serde_json::{Value, json};
@@ -88,9 +89,53 @@ pub fn command_ref(id: &str, params: &Value) -> CommandRef {
}
}
-/// The keymap with the user's overrides.
+/// The browser's operating system as [`note_os`] recorded it, by [`platform_code`]; 0 before the
+/// first frame.
+static WEB_PLATFORM: AtomicU8 = AtomicU8::new(0);
+
+fn platform_code(p: Platform) -> u8 {
+ match p {
+ Platform::Mac => 1,
+ Platform::Windows => 2,
+ Platform::Linux => 3,
+ }
+}
+
+/// The platform shortcuts follow: the build's, and on the web the browser's operating system, so a
+/// browser on a Mac reads ⌘ as Cmd and leaves Control as Control.
+pub fn platform() -> Platform {
+ if !cfg!(target_arch = "wasm32") {
+ return Platform::current();
+ }
+ match WEB_PLATFORM.load(Ordering::Relaxed) {
+ 1 => Platform::Mac,
+ 2 => Platform::Windows,
+ 3 => Platform::Linux,
+ _ => Platform::current(),
+ }
+}
+
+/// The platform for an operating system egui reports (on the web, guessed from the browser's user
+/// agent). An iPad with a keyboard has ⌘ as on a Mac.
+pub fn platform_of(os: egui::os::OperatingSystem) -> Platform {
+ use egui::os::OperatingSystem as Os;
+ match os {
+ Os::Mac | Os::IOS => Platform::Mac,
+ Os::Windows => Platform::Windows,
+ Os::Nix | Os::Android | Os::Unknown => Platform::Linux,
+ }
+}
+
+/// Records the operating system egui reports, which the web build reads from the browser.
+pub fn note_os(os: egui::os::OperatingSystem) {
+ if cfg!(target_arch = "wasm32") {
+ WEB_PLATFORM.store(platform_code(platform_of(os)), Ordering::Relaxed);
+ }
+}
+
+/// The keymap with the user's overrides, for [`platform`].
pub fn new_keymap(overrides: Overrides) -> Keymap {
- Keymap::new(catalogue().to_vec(), Scopes::new().with_context(TEXT_CONTEXT), Platform::current()).with_overrides(overrides)
+ Keymap::new(catalogue().to_vec(), Scopes::new().with_context(TEXT_CONTEXT), platform()).with_overrides(overrides)
}
/// Every bindable command, in dispatch order: where two share keys the earlier one runs. UI
@@ -205,7 +250,7 @@ pub fn shortcut_display(keymap: &Keymap, cmd: &CommandRef) -> Option<String> {
/// A key press as shortcut text ("Cmd+Shift+K"); `None` for a key the keymap has no name for.
pub fn chord_text(m: egui::Modifiers, key: egui::Key) -> Option<String> {
let key = Key::from_egui(key)?;
- Some(Chord::new(Mods::from_egui(&m, Platform::current()), key).to_string())
+ Some(Chord::new(Mods::from_egui(&m, platform()), key).to_string())
}
/// Copies the keymap's overrides to the saved UI state.
@@ -259,3 +304,31 @@ pub fn conflicts(keymap: &Keymap, cmd: &CommandRef) -> Vec<String> {
pub fn label(keymap: &Keymap, key: &str) -> String {
CommandRef::parse_key(key).and_then(|c| keymap.bindable(&c).map(|b| b.label.clone())).unwrap_or_else(|| key.to_string())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// The web build follows the operating system in the browser's user agent.
+ #[test]
+ fn browser_user_agent_picks_the_platform() {
+ use egui::os::OperatingSystem as Os;
+ let of = |ua: &str| platform_of(Os::from_user_agent(ua));
+ let mac = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15";
+ let ipad = "Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1";
+ let windows = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
+ let linux = "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0";
+ let android = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36";
+ assert_eq!(of(mac), Platform::Mac);
+ assert_eq!(of(ipad), Platform::Mac);
+ assert_eq!(of(windows), Platform::Windows);
+ assert_eq!(of(linux), Platform::Linux);
+ assert_eq!(of(android), Platform::Linux);
+ assert_eq!(platform_of(Os::Unknown), Platform::Linux);
+ // Native builds keep their own platform whatever egui reports.
+ if !cfg!(target_arch = "wasm32") {
+ note_os(Os::Mac);
+ assert_eq!(platform(), Platform::current());
+ }
+ }
+}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 59c25b4c0..744e7ce8c 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1127,9 +1127,13 @@ impl DesignApp {
}
}
- /// Applies `ui.shortcuts` to the keymap when they differ, e.g. after preferences were loaded.
+ /// Applies `ui.shortcuts` to the keymap when they differ, e.g. after preferences were loaded,
+ /// and rebuilds it when the shortcut platform changed (the web build learns the browser's
+ /// operating system on the first frame).
pub fn sync_keymap(&mut self) {
- if *self.keymap.overrides() != self.ui.shortcuts {
+ if self.keymap.platform() != keys::platform() {
+ self.keymap = keys::new_keymap(self.ui.shortcuts.clone());
+ } else if *self.keymap.overrides() != self.ui.shortcuts {
self.keymap.set_overrides(self.ui.shortcuts.clone());
}
}
@@ -1144,6 +1148,7 @@ impl DesignApp {
if self.egui_ctx.is_none() {
self.egui_ctx = Some(ctx.clone());
}
+ keys::note_os(ctx.os());
self.sync_keymap();
self.window_maximized = ctx.input(|i| i.viewport().maximized.unwrap_or(false));
if !self.color_applied {
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index ab3835f74..190a4cdff 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -2179,7 +2179,7 @@ pub fn shortcut_string(m: egui::Modifiers, key: egui::Key) -> String {
/// A shortcut for display: ⌃⌥⇧⌘K on macOS, Ctrl+Alt+Shift+K elsewhere.
pub fn shortcut_text(sc: &str) -> String {
match craft_keymap::Binding::parse(sc) {
- Ok(b) => b.trigger.display(craft_keymap::Platform::current()),
+ Ok(b) => b.trigger.display(crate::keys::platform()),
Err(_) => sc.to_string(),
}
}