Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui_web: Preserve iOS keyboard edits and browser window activity (#64882)
Gaauwe Romboutse773a56d6cf0
diff --git a/crates/gpui_web/src/events.rs b/crates/gpui_web/src/events.rsindex bc854cca7d2..9908b423470 100644--- a/crates/gpui_web/src/events.rs+++ b/crates/gpui_web/src/events.rs@@ -468,10 +468,13 @@ impl WebWindowInner {/// Cancels touch default handling separately because iOS does not consistently/// transfer pointer-event cancellation to the corresponding touch event.+ /// During native scrolling, touchend may be non-cancelable.fn register_touch_end(self: &Rc<Self>) -> EventListenerHandle {self.listen_non_passive("touchend", move |event: JsValue| {let event: web_sys::Event = event.unchecked_into();- event.prevent_default();+ if event.cancelable() {+ event.prevent_default();+ }})}@@ -524,11 +527,7 @@ impl WebWindowInner {let callback = wasm_bindgen::closure::Closure::once_into_js({let this = Rc::clone(self);move || {- this.state.borrow_mut().is_active = true;- this.with_callback(- |callbacks| &mut callbacks.active_status_change,- |callback| callback(true),- );+ this.refresh_active_status();}});if let Err(error) = self@@ -724,6 +723,20 @@ impl WebWindowInner {let is_held = event.repeat();let key_char = compute_key_char(&event, &key, &modifiers);+ // The software keyboard must edit the mirror itself: cancelling+ // keydown prevents iOS from updating its autocorrect context and+ // from delivering the corresponding beforeinput/input events.+ if this.touch_input+ && this.ime_mirror.virtual_keyboard_enabled()+ && this.state.borrow().input_handler.is_some()+ && !modifiers.platform+ && !modifiers.control+ && !modifiers.alt+ && (key_char.is_some() || matches!(key.as_str(), "backspace" | "delete" | "enter"))+ {+ return;+ }+let keystroke = Keystroke {modifiers,key,@@ -1161,36 +1174,30 @@ impl WebWindowInner {fn register_focus(self: &Rc<Self>) -> EventListenerHandle {let this = Rc::clone(self);- self.listen_input("focus", move |_event: JsValue| {- if this.suppress_focus_status_events.get() {- return;- }- {- let mut state = this.state.borrow_mut();- state.is_active = true;- }- this.with_callback(- |callbacks| &mut callbacks.active_status_change,- |callback| callback(true),- );- })+ EventListenerHandle::add(+ self.browser_window.as_ref(),+ "focus",+ move |_event: JsValue| {+ if this.suppress_focus_status_events.get() {+ return;+ }+ this.refresh_active_status();+ },+ )}fn register_blur(self: &Rc<Self>) -> EventListenerHandle {let this = Rc::clone(self);- self.listen_input("blur", move |_event: JsValue| {- if this.suppress_focus_status_events.get() {- return;- }- {- let mut state = this.state.borrow_mut();- state.is_active = false;- }- this.with_callback(- |callbacks| &mut callbacks.active_status_change,- |callback| callback(false),- );- })+ EventListenerHandle::add(+ self.browser_window.as_ref(),+ "blur",+ move |_event: JsValue| {+ if this.suppress_focus_status_events.get() {+ return;+ }+ this.refresh_active_status();+ },+ )}fn register_pointer_enter(self: &Rc<Self>) -> EventListenerHandle {diff --git a/crates/gpui_web/src/window.rs b/crates/gpui_web/src/window.rsindex d503148fed5..66c1177ebf5 100644--- a/crates/gpui_web/src/window.rs+++ b/crates/gpui_web/src/window.rs@@ -195,7 +195,7 @@ impl WebWindow {title: String::new(),input_handler: None,is_fullscreen: false,- is_active: true,+ is_active: document_is_active(&browser_window),visibility: document_visibility(&browser_window),is_hovered: false,mouse_position: Point::default(),@@ -417,6 +417,16 @@ impl WebWindowInner {);}+ pub(crate) fn refresh_active_status(&self) {+ let active = document_is_active(&self.browser_window);+ if std::mem::replace(&mut self.state.borrow_mut().is_active, active) != active {+ self.with_callback(+ |callbacks| &mut callbacks.active_status_change,+ |callback| callback(active),+ );+ }+ }+/// Invokes a registered callback with take/call/restore semantics.////// The callback is removed from the slot for the duration of the call, so@@ -518,17 +528,11 @@ impl WebWindowInner {"visibilitychange",move |_event: JsValue| {let visibility = document_visibility(&this.browser_window);- let is_visible = visibility.is_visible();-let visibility_changed = {let mut state = this.state.borrow_mut();- state.is_active = is_visible;std::mem::replace(&mut state.visibility, visibility) != visibility};- this.with_callback(- |callbacks| &mut callbacks.active_status_change,- |callback| callback(is_visible),- );+ this.refresh_active_status();if visibility_changed {this.with_callback(|callbacks| &mut callbacks.visibility_change,@@ -655,6 +659,14 @@ fn document_visibility(browser_window: &web_sys::Window) -> WindowVisibility {}}+fn document_is_active(browser_window: &web_sys::Window) -> bool {+ document_visibility(browser_window).is_visible()+ && browser_window+ .document()+ .and_then(|document| document.has_focus().ok())+ .unwrap_or(true)+}+struct MqlHandle {mql: web_sys::MediaQueryList,_closure: Closure<dyn FnMut(JsValue)>,