From d6b66480796b6d7f360921aa07bfff3e5b6a5ff4 Mon Sep 17 00:00:00 2001 From: i2cjak Date: Sun, 22 Nov 2026 13:13:33 -0500 Subject: [PATCH] Fix embedded browser click focus handoff across Linux, macOS, and Windows Clicking a text field in the embedded CEF browser could leave keyboard focus with Backplane because: - on_got_focus never consulted button_down() when pressed_in() was unwatched (Windows and macOS), and single-consume press records dropped paired on_set_focus/on_got_focus callbacks across intervening pump() ticks; - CefPage's independent want_focus/want_yank booleans allowed unclicked on_got_focus, background tabs, or cross-tab switches to yank or mis-blur focus; - macOS focus_child unconditionally reset firstResponder on the outer wrapper view even when an inner Chromium view held focus, and lacked pre-dispatch click recording. Add a pure cross-platform FocusHandoff/PressTracker state machine (viewers/cef_focus.rs) with sequence-ordered press/cutoff tracking and unit tests, hook Windows (WH_MOUSE + WH_CALLWNDPROC WM_MOUSEACTIVATE deduplication) and macOS (GPUIApplication -sendEvent: + AppKit hitTest: + arch-exact ObjCBool) mouse-down events, preserve inner NSView firstResponder on macOS, handle window deactivation and inactive handback cleanly, and wire CefPage/Tab through RenderFocusPlan. Fixes #1595 --- native/gpui/src/viewers/cef.rs | 82 +- native/gpui/src/viewers/cef_focus.rs | 2303 +++++++++++++++++ native/gpui/src/viewers/cef_native.rs | 959 ++++++- native/gpui/src/viewers/cef_page.rs | 165 +- native/gpui/src/viewers/mod.rs | 1 + native/gpui/vendor/gpui/PATCH.md | 2 +- native/gpui/vendor/gpui/src/platform.rs | 7 + .../vendor/gpui/src/platform/mac/platform.rs | 18 + 8 files changed, 3421 insertions(+), 116 deletions(-) create mode 100644 native/gpui/src/viewers/cef_focus.rs diff --git a/native/gpui/src/viewers/cef.rs b/native/gpui/src/viewers/cef.rs index fba7c757ff..8b73a4d395 100644 --- a/native/gpui/src/viewers/cef.rs +++ b/native/gpui/src/viewers/cef.rs @@ -13,7 +13,7 @@ use cef::{rc::Rc as _, *}; pub use cef::{CefStringList, ImplFileDialogCallback, KeyEvent}; use super::downloads::{bring_to_downloads, safe_name, unique_path}; -use std::{path::{Path, PathBuf}, sync::{atomic::{AtomicBool, AtomicI32, AtomicU32, Ordering}, Arc, Mutex, OnceLock}}; +use std::{path::{Path, PathBuf}, sync::{atomic::{AtomicBool, AtomicI32, AtomicU32, AtomicU64, Ordering}, Arc, Mutex, OnceLock}}; /// The CEF version the `cef` crate in Cargo.toml was generated for; the runtime directory is `/backplane/cef-runtime/`. pub const VERSION: &str = "154.0.34"; @@ -23,7 +23,7 @@ pub enum Cur { Default, Hand, IBeam, Cross, Wait, Help, Move, ResizeH, ResizeV, pub struct Frame { pub w: u32, pub h: u32, pub bgra: Vec } #[derive(Default)] -pub struct State { pub frame: Option, pub fresh: bool, pub got_focus: bool, pub person: bool, pub keys: Vec<(String, KeyEvent)>, pub took_focus: bool, pub url: String, pub title: String, pub loading: bool, pub back: bool, pub forward: bool, pub popups: Vec, pub popup_tabs: Vec>, pub dialogs: Vec, pub download: String, pub closed: bool, pub load_error: String } +pub struct State { pub frame: Option, pub fresh: bool, pub got_focus_seq: u64, pub click_seq: u64, pub key_seq: u64, pub keys: Vec<(String, KeyEvent)>, pub took_focus_seq: u64, pub url: String, pub title: String, pub loading: bool, pub back: bool, pub forward: bool, pub popups: Vec, pub popup_tabs: Vec>, pub dialogs: Vec, pub download: String, pub closed: bool, pub load_error: String } /// A file dialog CEF asked for (OSR has none of its own): the page shows gpui's picker and answers through `cb`. pub struct Dialog { pub save: bool, pub multiple: bool, pub folder: bool, pub title: String, pub cb: FileDialogCallback } unsafe impl Send for Dialog {} @@ -39,6 +39,7 @@ pub struct Tab { w: AtomicU32, h: AtomicU32, scale_milli: AtomicU32, wake: async_channel::Sender<()>, hidden: AtomicBool, + pub focus_id: u64, pub id: AtomicI32, /// how many frames CEF has painted (the e2e's frame rate) pub paints: std::sync::atomic::AtomicU64, @@ -125,16 +126,18 @@ wrap_life_span_handler! { false as _ } fn on_after_created(&self, b: Option<&mut Browser>) { if let Some(b) = b { eprintln!("GPUI_LOG {{\"cef_created\":{},\"windowed\":{}}}", b.identifier(), self.tab.host.is_some()); if let Some(h) = b.host() { - if self.tab.host.is_some() { let w = handle_u64(h.window_handle()); eprintln!("GPUI_LOG {{\"cef_native\":{w}}}"); self.tab.native.store(w, Ordering::Relaxed); } else { h.was_hidden(false as _); h.was_resized(); h.invalidate(PaintElementType::default()); } } self.tab.id.store(b.identifier(), Ordering::Relaxed); *self.tab.browser.lock().unwrap() = Some(B(b.clone())); let _ = self.tab.wake.try_send(()); } } + if self.tab.host.is_some() { let w = handle_u64(h.window_handle()); eprintln!("GPUI_LOG {{\"cef_native\":{w}}}"); if w != 0 { if self.tab.frame == 0 { native::register_press_wake(w, self.tab.wake.clone()); } native::watch_native(w); } self.tab.native.store(w, Ordering::Relaxed); } else { h.was_hidden(false as _); h.was_resized(); h.invalidate(PaintElementType::default()); } } self.tab.id.store(b.identifier(), Ordering::Relaxed); *self.tab.browser.lock().unwrap() = Some(B(b.clone())); let _ = self.tab.wake.try_send(()); } } /// windowed: true, so only the browser's own window goes (false sends the OS close to its top-level window, which is the client's window) /// (and it closes by its native window going: then CEF destroys the browser and calls on_before_close) fn do_close(&self, _b: Option<&mut Browser>) -> ::std::os::raw::c_int { // X11: CEF closes its own window (WM_DELETE_WINDOW to itself); Windows and macOS: CEF would close the top-level window (WM_CLOSE, // performClose:), which is the client's, so the browser's own window or view goes instead (CEF then destroys the browser) if self.tab.host.is_none() || cfg!(target_os = "linux") { return false as _ } - native::close(self.tab.native()); true as _ + let w = self.tab.native(); + native::unwatch_native(w); + native::close(w); true as _ } - fn on_before_close(&self, _b: Option<&mut Browser>) { *self.tab.browser.lock().unwrap() = None; if self.tab.frame != 0 { native::destroy(self.tab.frame); } self.tab.st.lock().unwrap().closed = true; let _ = self.tab.wake.try_send(()); } + fn on_before_close(&self, _b: Option<&mut Browser>) { *self.tab.browser.lock().unwrap() = None; let w = self.tab.native(); if w != 0 { native::unwatch_native(w); } if self.tab.frame != 0 { native::destroy(self.tab.frame); } self.tab.st.lock().unwrap().closed = true; let _ = self.tab.wake.try_send(()); } } } wrap_dialog_handler! { @@ -185,20 +188,41 @@ wrap_focus_handler! { /// counts the browser focused already, as after a yank that moved only the X focus away: with a KiCad pane, a click in the page then /// came here alone and the keyboard was taken back) fn on_got_focus(&self, _b: Option<&mut Browser>) { if self.tab.host.is_some() { - let clicked = native::pressed_in(self.tab.outer()) == Some(true); - if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_got_focus": {"clicked": clicked}})); } - let mut st = self.tab.st.lock().unwrap(); st.got_focus = true; if clicked { st.person = true; } drop(st); let _ = self.tab.wake.try_send(()); } } + let outer = self.tab.outer(); + let event_seq = super::cef_focus::next_focus_seq(); + let click_seq = super::cef_focus::callback_click_seq( + true, + super::cef_focus::FocusCallback::GotFocus, + super::cef_focus::FocusSourceKind::System, + event_seq, + || native::pressed_in(outer, super::cef_focus::FocusCallback::GotFocus), + || native::button_down(outer), + ); + if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_got_focus": {"clicked": click_seq.is_some()}})); } + let mut st = self.tab.st.lock().unwrap(); + st.got_focus_seq = st.got_focus_seq.max(event_seq); + if let Some(pseq) = click_seq { st.click_seq = st.click_seq.max(pseq); } + drop(st); + let _ = self.tab.wake.try_send(()); } } /// Tab past the page's last element: the keyboard goes back to the client /// a click into the page (the system moving the keyboard there) is the person's, unlike our own SetFocus calls fn on_set_focus(&self, _b: Option<&mut Browser>, source: FocusSource) -> ::std::os::raw::c_int { - // The native page records and replays the delivered X press before CEF sees it. - // A SYSTEM focus with no such record cannot claim a page with an active button grab. - let clicked = self.tab.host.is_some() && source == FocusSource::SYSTEM && native::pressed_in(self.tab.outer()).unwrap_or_else(|| native::button_down(self.tab.outer())); - if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_set_focus": {"system": source == FocusSource::SYSTEM, "clicked": clicked}})); } - if clicked { self.tab.st.lock().unwrap().person = true; let _ = self.tab.wake.try_send(()); } + let outer = self.tab.outer(); + let src = if source == FocusSource::SYSTEM { super::cef_focus::FocusSourceKind::System } else { super::cef_focus::FocusSourceKind::Navigation }; + let event_seq = super::cef_focus::next_focus_seq(); + let click_seq = super::cef_focus::callback_click_seq( + self.tab.host.is_some(), + super::cef_focus::FocusCallback::SetFocus, + src, + event_seq, + || native::pressed_in(outer, super::cef_focus::FocusCallback::SetFocus), + || native::button_down(outer), + ); + if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_set_focus": {"system": source == FocusSource::SYSTEM, "clicked": click_seq.is_some()}})); } + if let Some(pseq) = click_seq { let mut st = self.tab.st.lock().unwrap(); st.click_seq = st.click_seq.max(pseq); drop(st); let _ = self.tab.wake.try_send(()); } false as _ } - fn on_take_focus(&self, _b: Option<&mut Browser>, _next: ::std::os::raw::c_int) { if self.tab.host.is_some() { self.tab.st.lock().unwrap().took_focus = true; let _ = self.tab.wake.try_send(()); } } + fn on_take_focus(&self, _b: Option<&mut Browser>, _next: ::std::os::raw::c_int) { if self.tab.host.is_some() { let seq = super::cef_focus::next_focus_seq(); let mut st = self.tab.st.lock().unwrap(); st.took_focus_seq = st.took_focus_seq.max(seq); drop(st); let _ = self.tab.wake.try_send(()); } } } } #[cfg(target_os = "linux")] type OsKey<'a> = Option<&'a mut sys::XEvent>; @@ -213,7 +237,7 @@ wrap_keyboard_handler! { let (Some(e), true) = (e, self.tab.host.is_some()) else { return false as _ }; if e.type_ != KeyEventType::RAWKEYDOWN { return false as _ } if self.tab.reinject.load(Ordering::Relaxed) > 0 { self.tab.reinject.fetch_sub(1, Ordering::Relaxed); return false as _ } - self.tab.st.lock().unwrap().person = true; let _ = self.tab.wake.try_send(()); // a key the person pressed in the page + { let seq = super::cef_focus::next_focus_seq(); let mut st = self.tab.st.lock().unwrap(); st.key_seq = st.key_seq.max(seq); } let _ = self.tab.wake.try_send(()); // a key the person pressed in the page let Some(ks) = client_key(e.windows_key_code, e.modifiers) else { return false as _ }; self.tab.st.lock().unwrap().keys.push((ks, e.clone())); let _ = self.tab.wake.try_send(()); true as _ } @@ -453,7 +477,10 @@ impl Tab { } /// A tab whose browser CEF makes itself (a popup): the state and handlers only. pub fn detached(url: &str, w: u32, h: u32, scale: f32, wake: async_channel::Sender<()>, host: Option) -> Arc { - Arc::new(Tab { st: Mutex::new(State { url: url.into(), ..Default::default() }), browser: Mutex::new(None), cursor: Mutex::new(Cur::Default), w: w.max(1).into(), h: h.max(1).into(), scale_milli: ((scale * 1000.0) as u32).into(), wake, hidden: AtomicBool::new(false), id: AtomicI32::new(0), paints: Default::default(), resize_at: Mutex::new(None), resize_done: Arc::new(Mutex::new(-1.0)), placed_size: Mutex::new((0, 0)), frame: host.and_then(native::container).map_or(0, |c| match c { native::Host::X11(id) => id as u64, _ => 0 }), host, native: Default::default(), reinject: AtomicU32::new(0) }) + static NEXT_TAB_ID: AtomicU64 = AtomicU64::new(1); + let frame = host.and_then(native::container).map_or(0, |c| match c { native::Host::X11(id) => id as u64, _ => 0 }); + if frame != 0 { native::register_press_wake(frame, wake.clone()); } + Arc::new(Tab { st: Mutex::new(State { url: url.into(), ..Default::default() }), browser: Mutex::new(None), cursor: Mutex::new(Cur::Default), w: w.max(1).into(), h: h.max(1).into(), scale_milli: ((scale * 1000.0) as u32).into(), wake, hidden: AtomicBool::new(false), focus_id: NEXT_TAB_ID.fetch_add(1, Ordering::Relaxed), id: AtomicI32::new(0), paints: Default::default(), resize_at: Mutex::new(None), resize_done: Arc::new(Mutex::new(-1.0)), placed_size: Mutex::new((0, 0)), frame, host, native: Default::default(), reinject: AtomicU32::new(0) }) } /// The browser, taken out of its lock before CEF is called: on macOS CEF runs callbacks on this thread inside the call (`on_before_close` takes /// the same lock), so holding it across the call deadlocked the window on closing a tab (test/browser/browser_e2e.py, macos-latest). @@ -541,8 +568,27 @@ impl Tab { } /// windowed: the keyboard to the page (`on`) or back to the client's window pub fn give_keyboard(&self, on: bool) { - self.focus(on); - if on { native::focus_child(self.native()) } else if let Some(h) = self.host { native::focus_host(h, self.frame) } + if on { + self.focus(true); + native::focus_child(self.native()); + } else { + let target = self.outer(); + native::clear_press(target); + if target != 0 && let Some(h) = self.host { native::focus_host(h, target); } + self.focus(false); + } + } + /// windowed: returns keyboard focus from this tab to the host while preserving press records strictly newer than `max_clear_seq` + pub fn yank_keyboard_up_to(&self, max_clear_seq: u64) { + let target = self.outer(); + native::clear_press_up_to(target, max_clear_seq); + if target != 0 && let Some(h) = self.host { native::focus_host(h, target); } + self.focus(false); + } + /// windowed: clears CEF/press focus up to `max_clear_seq` for a non-active tab without changing GPUI focus or focusing the host window + pub fn release_cef_focus_up_to(&self, max_clear_seq: u64) { + self.focus(false); + native::clear_press_up_to(self.outer(), max_clear_seq); } /// windowed: a key the client did not want goes to the page after all (on_pre_key_event lets it through this time) pub fn reinject(&self, e: &KeyEvent) { diff --git a/native/gpui/src/viewers/cef_focus.rs b/native/gpui/src/viewers/cef_focus.rs new file mode 100644 index 0000000000..0260358d6f --- /dev/null +++ b/native/gpui/src/viewers/cef_focus.rs @@ -0,0 +1,2303 @@ +//! Pure focus-handoff state machine and click-tracking logic for embedded windowed CEF (`cef.rs`, `cef_page.rs`, `cef_native.rs`). +//! Compiled unconditionally so unit tests run with or without `--features cef` and across all targets. +#![cfg_attr(not(feature = "cef"), allow(dead_code))] + +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::{Duration, Instant}; + +/// How long a recorded mouse press in a browser view can explain a subsequent CEF focus callback. +pub const PRESS_SPAN: Duration = Duration::from_millis(400); + +static FOCUS_SEQ: AtomicU64 = AtomicU64::new(1); + +/// Returns a strictly increasing sequence number for ordering CEF callbacks against host focus changes. +pub fn next_focus_seq() -> u64 { + FOCUS_SEQ.fetch_add(1, Ordering::SeqCst) +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FocusCallback { + SetFocus, + GotFocus, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FocusSourceKind { + System, + Navigation, +} + +/// Outcome of a Win32 `WM_MOUSEACTIVATE` dispatch reported by `WH_CALLWNDPROCRET`. +#[cfg_attr(not(windows), allow(dead_code))] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum MouseActivateOutcome { + /// `MA_ACTIVATE` / `MA_NOACTIVATE`: button-down will follow via `WH_MOUSE`. + DeliverDown, + /// `MA_ACTIVATEANDEAT`: activates the window and eats the button-down. + ActivateAndEat, + /// `MA_NOACTIVATEANDEAT`: neither activates nor delivers the button-down. + NoActivateAndEat, +} + +/// Result of querying a platform press watcher for a recent click in a watched native browser window. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PressProbe { + /// Watched window received a qualifying press (carries the press-time sequence number). + Pressed(u64), + /// Window is watched (or temporarily gated after blur until button release), with no eligible press. + NotPressed, + /// No press watcher covers this target; callers may fall back to querying live pointer state. + Unwatched, +} + +#[derive(Clone, Copy, Debug)] +struct PressRecord { + target: u64, + msg: u32, + at: Instant, + seq: u64, + used_set: bool, + used_got: bool, + confirmed: bool, +} + +#[derive(Clone, Copy, Debug)] +struct ActivateFrame { + target: u64, + msg: u32, + seq: u64, + down_paired: bool, +} + +#[derive(Clone, Copy, Debug)] +struct PendingActivateDown { + target: u64, + msg: u32, + seq: u64, +} + +/// Tracks mouse presses delivered to embedded browser windows so paired +/// `on_set_focus` and `on_got_focus` callbacks for the same click both recognize it once, +/// carrying the press-time sequence number, decoupling Win32 `WM_MOUSEACTIVATE` + button-down pairing +/// from `PRESS_SPAN` via a LIFO activation stack and per-`(target, msg)` `WH_CALLWNDPROCRET` records, +/// and gating unwatched targets across blur until the held mouse button releases. +#[derive(Debug, Default)] +pub struct PressTracker { + presses: Vec, + activate_stack: Vec, + pending_activate_downs: Vec, + unwatched_blocked: Vec, + unwatched_block_gen: u64, +} + +impl PressTracker { + pub const fn new() -> Self { + Self { + presses: Vec::new(), + activate_stack: Vec::new(), + pending_activate_downs: Vec::new(), + unwatched_blocked: Vec::new(), + unwatched_block_gen: 0, + } + } + + fn upsert_press(&mut self, target: u64, msg: u32, at: Instant, seq: u64, confirmed: bool) { + self.presses.retain(|rec| { + !(rec.target == target && rec.msg == msg) + && (!rec.confirmed || at.saturating_duration_since(rec.at) <= PRESS_SPAN) + && (self.activate_stack.iter().any(|f| f.target == rec.target && f.msg == rec.msg) + || self.pending_activate_downs.iter().any(|p| p.target == rec.target && p.msg == rec.msg)) + }); + self.presses.push(PressRecord { + target, + msg, + at, + seq, + used_set: false, + used_got: false, + confirmed, + }); + } + + fn bump_press(&mut self, target: u64, msg: u32) { + if let Some(pos) = self.presses.iter().position(|rec| rec.target == target && rec.msg == msg) { + let rec = self.presses.remove(pos); + self.presses.push(rec); + } + } + + /// Records a mouse press on `target` at `at`, returning its press-time sequence number (`0` if `target == 0`). + #[cfg_attr(windows, allow(dead_code))] + pub fn record(&mut self, target: u64, at: Instant) -> u64 { + if target == 0 { + return 0; + } + let seq = next_focus_seq(); + self.record_with_seq(target, at, seq); + seq + } + + /// Deterministic variant of [`Self::record`] for tests. + #[cfg_attr(windows, allow(dead_code))] + pub fn record_with_seq(&mut self, target: u64, at: Instant, seq: u64) { + if target == 0 { + return; + } + self.upsert_press(target, 0, at, seq, true); + } + + /// Records entry into a Win32 `WM_MOUSEACTIVATE` dispatch for `target` and button-down message `msg`. + /// Reentrant dispatches push another LIFO frame onto `activate_stack` (reusing an active `(target, msg)` sequence + /// without resetting existing press state) and keep nested clicks unconfirmed until `complete_activate` returns. + #[cfg_attr(not(windows), allow(dead_code))] + pub fn record_activate(&mut self, target: u64, msg: u32, at: Instant) -> u64 { + if target == 0 { + return 0; + } + let seq = self + .activate_stack + .iter() + .rev() + .find(|f| f.target == target && f.msg == msg) + .map_or_else(next_focus_seq, |f| f.seq); + self.record_activate_with_seq(target, msg, at, seq) + } + + /// Deterministic variant of [`Self::record_activate`] for tests. + #[cfg_attr(not(windows), allow(dead_code))] + pub fn record_activate_with_seq(&mut self, target: u64, msg: u32, at: Instant, seq: u64) -> u64 { + if target == 0 { + return 0; + } + if let Some(existing) = self.activate_stack.iter().rev().find(|f| f.target == target && f.msg == msg) { + let existing_seq = existing.seq; + let down_paired = self + .activate_stack + .iter() + .any(|f| f.target == target && f.msg == msg && f.down_paired); + self.activate_stack.push(ActivateFrame { + target, + msg, + seq: existing_seq, + down_paired, + }); + return existing_seq; + } + self.pending_activate_downs.retain(|p| !(p.target == target && p.msg == msg)); + self.activate_stack.push(ActivateFrame { + target, + msg, + seq, + down_paired: false, + }); + self.upsert_press(target, msg, at, seq, false); + seq + } + + /// Handles return from a Win32 `WM_MOUSEACTIVATE` dispatch (`WH_CALLWNDPROCRET`). + /// Pops only the top matching `(target, msg)` LIFO frame from `activate_stack`. + /// When the last frame for `(target, msg)` unwinds: `NoActivateAndEat` discards an unpaired press, + /// nested `ActivateAndEat` discards an unpaired press while an outer frame on `target` stays active, + /// and surviving outcomes confirm `(target, msg)` at confirmation time (upgrading unconfirmed presses + /// above any mid-dispatch unprompted `on_got_focus` cutoff) and queue `pending_activate_downs` on + /// `DeliverDown` unless already paired. + #[cfg_attr(not(windows), allow(dead_code))] + pub fn complete_activate(&mut self, target: u64, msg: u32, outcome: MouseActivateOutcome, at: Instant) -> bool { + self.complete_activate_inner(target, msg, outcome, at, next_focus_seq) + } + + /// Deterministic variant of [`Self::complete_activate`] for tests. + #[cfg(test)] + pub fn complete_activate_with_seq( + &mut self, + target: u64, + msg: u32, + outcome: MouseActivateOutcome, + at: Instant, + confirm_seq: u64, + ) -> bool { + self.complete_activate_inner(target, msg, outcome, at, || confirm_seq) + } + + #[cfg_attr(not(windows), allow(dead_code))] + fn complete_activate_inner( + &mut self, + target: u64, + msg: u32, + outcome: MouseActivateOutcome, + at: Instant, + confirm_seq: impl FnOnce() -> u64, + ) -> bool { + let Some(idx) = self.activate_stack.iter().rposition(|f| f.target == target && f.msg == msg) else { + return false; + }; + let done = self.activate_stack.remove(idx); + if self.activate_stack.iter().any(|f| f.target == done.target && f.msg == done.msg) { + return false; + } + match outcome { + MouseActivateOutcome::NoActivateAndEat if !done.down_paired => { + self.presses.retain(|rec| !(rec.target == done.target && rec.msg == done.msg)); + return false; + } + MouseActivateOutcome::ActivateAndEat + if !done.down_paired && self.activate_stack.iter().any(|f| f.target == done.target) => + { + self.presses.retain(|rec| !(rec.target == done.target && rec.msg == done.msg)); + return false; + } + _ => {} + } + let mut confirmed_seq = None; + let mut confirm_seq = Some(confirm_seq); + for rec in &mut self.presses { + if rec.target == done.target && rec.msg == done.msg { + if !rec.confirmed { + let seq = *confirmed_seq.get_or_insert_with(|| (confirm_seq.take().unwrap())()); + rec.confirmed = true; + rec.seq = seq; + rec.at = at; + } else if confirmed_seq.is_none() { + confirmed_seq = Some(rec.seq); + } + } + } + if outcome == MouseActivateOutcome::DeliverDown && !done.down_paired { + self.pending_activate_downs.retain(|p| !(p.target == done.target && p.msg == done.msg)); + self.pending_activate_downs.push(PendingActivateDown { + target: done.target, + msg: done.msg, + seq: confirmed_seq.unwrap_or(done.seq), + }); + } + confirmed_seq.is_some() + } + + /// Records a Win32 `WH_MOUSE` button-down `msg` on `target`. + /// If it pairs with a matching `(target, msg)` activation record, consumes only that pairing + /// regardless of elapsed time and returns the activation sequence WITHOUT recreating or resetting its press record. + #[cfg_attr(not(windows), allow(dead_code))] + pub fn record_button_down(&mut self, target: u64, msg: u32, at: Instant) -> u64 { + self.record_button_down_inner(target, msg, at, next_focus_seq) + } + + /// Deterministic variant of [`Self::record_button_down`] for tests. + #[cfg(test)] + pub fn record_button_down_with_seq(&mut self, target: u64, msg: u32, at: Instant, seq: u64) -> u64 { + self.record_button_down_inner(target, msg, at, || seq) + } + + #[cfg_attr(not(windows), allow(dead_code))] + fn record_button_down_inner( + &mut self, + target: u64, + msg: u32, + at: Instant, + alloc_seq: impl FnOnce() -> u64, + ) -> u64 { + if target == 0 { + return 0; + } + if let Some(pos) = self.pending_activate_downs.iter().position(|p| p.target == target && p.msg == msg) { + let seq = self.pending_activate_downs.swap_remove(pos).seq; + self.bump_press(target, msg); + return seq; + } + if let Some(top_seq) = self + .activate_stack + .iter() + .rev() + .find(|f| f.target == target && f.msg == msg && !f.down_paired) + .map(|f| f.seq) + { + let mut paired_seq = top_seq; + if let Some(pos) = self.presses.iter().position(|rec| rec.target == target && rec.msg == msg) { + let mut rec = self.presses.remove(pos); + if !rec.confirmed { + paired_seq = alloc_seq(); + rec.confirmed = true; + rec.seq = paired_seq; + rec.at = at; + } else { + paired_seq = rec.seq; + } + self.presses.push(rec); + } + for f in &mut self.activate_stack { + if f.target == target && f.msg == msg { + f.down_paired = true; + f.seq = paired_seq; + } + } + return paired_seq; + } + let seq = alloc_seq(); + self.upsert_press(target, msg, at, seq, true); + seq + } + + #[cfg(test)] + pub fn has_pending_activate(&self) -> bool { + !self.activate_stack.is_empty() || !self.pending_activate_downs.is_empty() + } + + #[cfg(test)] + pub fn has_pending_activate_for(&self, target: u64, msg: u32) -> bool { + self.activate_stack.iter().any(|f| f.target == target && f.msg == msg) + || self.pending_activate_downs.iter().any(|p| p.target == target && p.msg == msg) + } + + /// Blocks `target` from using the unwatched held-button fallback until the mouse button is released. + /// Returns `Some(generation)` only when transitioning from empty to non-empty so callers spawn one watcher. + #[cfg_attr(target_os = "macos", allow(dead_code))] + pub fn block_unwatched_until_release(&mut self, target: u64) -> Option { + if target == 0 { + return None; + } + let was_empty = self.unwatched_blocked.is_empty(); + if !self.unwatched_blocked.contains(&target) { + self.unwatched_blocked.push(target); + } + self.unwatched_block_gen = self.unwatched_block_gen.wrapping_add(1); + was_empty.then_some(self.unwatched_block_gen) + } + + /// Returns whether `target` is currently blocked from using the unwatched held-button fallback. + pub fn is_unwatched_blocked(&self, target: u64) -> bool { + self.unwatched_blocked.contains(&target) + } + + /// Current generation of the unwatched held-button block set. + #[cfg_attr(target_os = "macos", allow(dead_code))] + pub fn unwatched_block_gen(&self) -> u64 { + self.unwatched_block_gen + } + + /// Clears the unwatched held-button block set if no newer block arrived since `block_gen` was observed. + /// Returns `true` when the block set is now empty (either already empty or cleared on matching generation). + #[cfg_attr(target_os = "macos", allow(dead_code))] + pub fn clear_unwatched_release_block_if(&mut self, block_gen: u64) -> bool { + if self.unwatched_blocked.is_empty() { + return true; + } + if self.unwatched_block_gen == block_gen { + self.unwatched_blocked.clear(); + true + } else { + false + } + } + + /// Returns the newest confirmed press-time sequence number if `target` has a fresh (`<= PRESS_SPAN`) press recorded strictly after `after_seq`. + /// Unconfirmed in-flight Win32 `WM_MOUSEACTIVATE` records are excluded until `complete_activate` or `WH_MOUSE` button-down confirms them. + pub fn fresh_press_seq_after(&self, target: u64, after_seq: u64, now: Instant) -> Option { + if target == 0 { + return None; + } + self.presses + .iter() + .filter(|rec| { + rec.confirmed + && rec.target == target + && rec.seq > after_seq + && now.saturating_duration_since(rec.at) <= PRESS_SPAN + }) + .map(|rec| rec.seq) + .max() + } + + /// Consumes a confirmed recorded press on `target` for `cb` if it is fresh (`<= PRESS_SPAN`) and unused for `cb`. + /// Prefers an in-flight `activate_stack` frame's `(target, msg)` click over nested or older buttons on `target`, + /// returning `None` while that active frame remains unconfirmed so pending activations cannot leak or fall back. + /// `SetFocus` leaves `GotFocus` available for the same click; `GotFocus` spends both. + /// Never deletes activation pairings, so a delayed paired button-down still deduplicates cleanly. + pub fn consume(&mut self, target: u64, cb: FocusCallback, now: Instant) -> Option { + if target == 0 { + return None; + } + self.presses.retain(|rec| { + rec.target != target || !rec.confirmed || now.saturating_duration_since(rec.at) <= PRESS_SPAN + }); + let eligible = |rec: &PressRecord| -> bool { + rec.confirmed + && rec.target == target + && match cb { + FocusCallback::SetFocus => !rec.used_set, + FocusCallback::GotFocus => !rec.used_got, + } + }; + let pos = if let Some(frame) = self.activate_stack.iter().rev().find(|f| f.target == target) { + self.presses + .iter() + .rposition(|rec| rec.msg == frame.msg && eligible(rec))? + } else { + self.presses.iter().rposition(eligible)? + }; + let rec = &mut self.presses[pos]; + match cb { + FocusCallback::SetFocus => { + rec.used_set = true; + Some(rec.seq) + } + FocusCallback::GotFocus => { + rec.used_got = true; + rec.used_set = true; + Some(rec.seq) + } + } + } + + /// Clears any recorded press belonging to `target` (e.g. when returning keyboard focus or deactivating). + /// Leaves activation pairings intact so an arriving paired `WM_*BUTTONDOWN` from this cancelled click is still absorbed. + pub fn clear_target(&mut self, target: u64) { + self.presses.retain(|rec| rec.target != target); + } + + /// Clears a confirmed recorded press belonging to `target` only if its sequence is `<= max_seq`, + /// preserving genuinely newer post-cutoff presses and unconfirmed in-flight Win32 `WM_MOUSEACTIVATE` + /// records whose confirmation sequence has not yet been assigned. + pub fn clear_target_up_to(&mut self, target: u64, max_seq: u64) { + self.presses + .retain(|rec| !(rec.target == target && rec.confirmed && rec.seq <= max_seq)); + } + + /// Drops any press, pending activation pairings, and unwatched release block belonging to `target` (used on destroy/unwatch). + #[allow(dead_code)] + pub fn drop_target(&mut self, target: u64) { + self.presses.retain(|rec| rec.target != target); + self.activate_stack.retain(|f| f.target != target); + self.pending_activate_downs.retain(|p| p.target != target); + self.unwatched_blocked.retain(|t| *t != target); + } + + /// Clears any recorded press regardless of target (e.g. on macOS when a click hits outside watched CEF views). + #[cfg_attr(not(target_os = "macos"), allow(dead_code))] + pub fn clear_all(&mut self) { + self.presses.clear(); + } +} + +/// Coordinates X11 watcher barrier timeouts so pre-barrier `ButtonPress` events arriving after a +/// 100 ms caller timeout are suppressed until the watcher drains the matching barrier `ClientMessage`, +/// while handling watcher-before-caller early completion without leaking state. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +#[derive(Debug, Default)] +pub struct X11BarrierFences { + active: Vec<(u64, Vec)>, + completed_early: Vec<(u64, u64)>, + watcher_dead: bool, +} + +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +impl X11BarrierFences { + pub const fn new() -> Self { + Self { + active: Vec::new(), + completed_early: Vec::new(), + watcher_dead: false, + } + } + + pub fn is_fenced(&self, target: u64) -> bool { + target != 0 && self.active.iter().any(|(_, targets)| targets.contains(&target)) + } + + /// Finalizes a timed-out barrier on the caller (`clear_presses`) under the press mutex. + /// Returns `Some(cutoff)` if the watcher already cleared `watched` and allocated `cutoff` + /// before timing out the rendezvous, or installs an active fence until `finish_timeout_watcher`. + pub fn finish_timeout_caller(&mut self, barrier_id: u64, watched: &[u64]) -> Option { + if let Some(pos) = self.completed_early.iter().position(|(id, _)| *id == barrier_id) { + return Some(self.completed_early.swap_remove(pos).1); + } + if !self.watcher_dead { + let live: Vec = watched.iter().copied().filter(|&t| t != 0).collect(); + self.active.push((barrier_id, live)); + } + None + } + + /// Finalizes a timed-out barrier on the watcher (`complete_barrier`) when its `ClientMessage` arrives. + /// Retires any active fence installed by the caller, or runs `on_early` to clear targets and allocate a + /// cutoff before returning to wait for post-barrier events. + pub fn finish_timeout_watcher(&mut self, barrier_id: u64, on_early: impl FnOnce() -> u64) { + if let Some(pos) = self.active.iter().position(|(id, _)| *id == barrier_id) { + self.active.swap_remove(pos); + } else if !self.watcher_dead { + let cutoff = on_early(); + self.completed_early.push((barrier_id, cutoff)); + } + } + + /// Unfences `target` on container destroy while keeping the `barrier_id` slot intact so the + /// watcher's matching `ClientMessage` still retires the barrier cleanly without leaking state. + pub fn drop_target(&mut self, target: u64) { + for (_, targets) in &mut self.active { + targets.retain(|&t| t != target); + } + } + + /// Clears active fences after the watcher thread exits so future waits never fence, while + /// keeping already-completed early cutoffs available for their callers to collect and remove. + pub fn mark_watcher_dead(&mut self) { + self.watcher_dead = true; + self.active.clear(); + } +} + +/// Decides whether a CEF focus callback was triggered by a person's click in the page and returns the click sequence. +/// Consults `probe` first and falls back to `button_down_in_page` only when `probe` returns `PressProbe::Unwatched` +/// (`SetFocus` with a non-system source never triggers or consumes a press). +pub fn callback_click_seq( + windowed: bool, + cb: FocusCallback, + source: FocusSourceKind, + fallback_seq: u64, + probe: impl FnOnce() -> PressProbe, + button_down_in_page: impl FnOnce() -> bool, +) -> Option { + if !windowed || (cb == FocusCallback::SetFocus && source != FocusSourceKind::System) { + return None; + } + match probe() { + PressProbe::Pressed(seq) => Some(seq), + PressProbe::NotPressed => None, + PressProbe::Unwatched => button_down_in_page().then_some(fallback_seq), + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct TabFocusEvents { + pub click_seq: u64, + pub key_seq: u64, + pub got_focus_seq: u64, + pub took_focus_seq: u64, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum FocusIntent { + /// Click or typing inside a specific tab grants GPUI focus to that tab. + FocusPage { tab: u64, seq: u64 }, + /// Tabbing past a specific tab's last/first focusable element blurs back to the host only if that tab is still active. + TabOut { tab: u64, seq: u64 }, + /// User/scripted "Hand back" is intentionally page-wide (matches `CefPage.taken = false` / `{"op": "handback"}`), + /// always returning keyboard control from the entire browser viewer (including any newly opened popup, + /// `origin`, and any stray `unprompted_got` tabs) back to the host unless superseded by a genuine post-handback click. + HandBack { origin: Option, seq: u64 }, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum GpuiFocusChange { + #[default] + None, + FocusPage, + BlurPage, +} + +/// One tab targeted by a focus yank/release together with that tab's own cutoff sequence, +/// so another tab's stray callback can never inflate this tab's press-clear cutoff. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct TabYank { + pub tab: u64, + pub cutoff_seq: u64, +} + +/// Plan of GPUI and native focus adjustments emitted for a single `CefPage::render`. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct RenderFocusPlan { + pub gpui: GpuiFocusChange, + /// Non-active tabs whose CEF/native focus must be cleared without changing GPUI focus or focusing the host. + pub release_cef_only: Vec, + /// Tabs whose keyboard focus should be yanked back to the host window. + pub yank_to_host: Vec, + /// Active tab whose native/CEF focus should be restored after releasing background tabs. + pub restore_active: Option, + /// Cutoff sequence for GPUI's host blur (`BlurPage`), set only from `TabOut`/`HandBack`/`click_cutoff_seq` + /// and never inflated by another tab's stray `got_focus` (`0` unless `gpui == GpuiFocusChange::BlurPage`). + pub blur_cutoff_seq: u64, +} + +impl RenderFocusPlan { + /// Returns the cutoff associated with blurring or yanking `active_tab` during this render, if any, + /// allowing post-yank reconciliation to reclaim focus when a click arrives after precheck. + pub fn active_blur_or_yank_cutoff(&self, active_tab: Option) -> Option { + let yank = active_tab.and_then(|tab| self.yank_to_host.iter().find(|y| y.tab == tab).map(|y| y.cutoff_seq)); + let blur = (self.gpui == GpuiFocusChange::BlurPage).then_some(self.blur_cutoff_seq); + match (blur, yank) { + (Some(b), Some(y)) => Some(b.max(y)), + (Some(b), None) => Some(b), + (None, Some(y)) => Some(y), + (None, None) => None, + } + } + + /// Returns the active-tab click recovery cutoff whenever this render blurred GPUI or yanked any tab to host + /// (including background-only yanks, without inflating the active tab's cutoff to a background tab's sequence). + pub fn recovery_cutoff_for_active(&self, active_tab: Option, floor_cutoff: u64) -> Option { + if let Some(cutoff) = self.active_blur_or_yank_cutoff(active_tab) { + Some(cutoff.max(floor_cutoff)) + } else if !self.yank_to_host.is_empty() && active_tab.is_some() { + Some(floor_cutoff) + } else { + None + } + } +} + +fn push_tab_yank(list: &mut Vec, tab: u64, cutoff_seq: u64) { + if let Some(existing) = list.iter_mut().find(|y| y.tab == tab) { + existing.cutoff_seq = existing.cutoff_seq.max(cutoff_seq); + } else { + list.push(TabYank { tab, cutoff_seq }); + } +} + +/// Coordinates tab-bound, sequence-ordered focus handoff between GPUI and embedded CEF windows across `pump()` and `render()` ticks. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct FocusHandoff { + intent: Option, + unprompted_got: Vec<(u64, u64)>, + click_cutoff_seq: u64, + deactivate_cutoff_seq: u64, + handed_back: bool, +} + +impl FocusHandoff { + pub const fn new() -> Self { + Self { + intent: None, + unprompted_got: Vec::new(), + click_cutoff_seq: 0, + deactivate_cutoff_seq: 0, + handed_back: false, + } + } + + pub const fn recovery_floor_seq(&self) -> u64 { + if self.click_cutoff_seq >= self.deactivate_cutoff_seq { + self.click_cutoff_seq + } else { + self.deactivate_cutoff_seq + } + } + + const fn intent_seq(&self) -> u64 { + match self.intent { + None => 0, + Some(FocusIntent::FocusPage { seq, .. }) + | Some(FocusIntent::TabOut { seq, .. }) + | Some(FocusIntent::HandBack { seq, .. }) => seq, + } + } + + fn record_unprompted(&mut self, tab: u64, seq: u64) { + if let Some((_, existing)) = self.unprompted_got.iter_mut().find(|(t, _)| *t == tab) { + *existing = (*existing).max(seq); + } else { + self.unprompted_got.push((tab, seq)); + } + } + + fn clear_unprompted(&mut self, tab: u64) { + self.unprompted_got.retain(|(t, _)| *t != tab); + } + + /// Applies one tab's drained focus events during `CefPage::pump` or pre-render reconciliation. + /// Returns `true` when qualifying user interaction in the active tab requires taking the page over. + /// While `handed_back` is active, only a genuine post-cutoff click (`click_seq > click_cutoff_seq`) + /// or explicit GPUI host focus (`on_host_focus`) restores `key_seq` takeover or replaces `HandBack`. + pub fn on_pump_tab(&mut self, tab: u64, ev: TabFocusEvents, is_active: bool) -> bool { + let click = if ev.click_seq > self.click_cutoff_seq { ev.click_seq } else { 0 }; + let key = if !self.handed_back && ev.key_seq > self.click_cutoff_seq { ev.key_seq } else { 0 }; + let person = click.max(key); + let got = if ev.got_focus_seq > self.deactivate_cutoff_seq { ev.got_focus_seq } else { 0 }; + let took = if ev.took_focus_seq > self.click_cutoff_seq { ev.took_focus_seq } else { 0 }; + + if !is_active { + if got > 0 { + self.record_unprompted(tab, got); + } + return false; + } + + if took > person && took > 0 { + if matches!(self.intent, Some(FocusIntent::HandBack { .. })) { + self.record_unprompted(tab, took); + } else if took >= self.intent_seq() { + self.intent = Some(FocusIntent::TabOut { tab, seq: took }); + } + if got > took { + self.record_unprompted(tab, got); + } + return person > 0; + } + if person > 0 { + if person >= self.intent_seq() { + if click > 0 { + self.handed_back = false; + } + self.intent = Some(FocusIntent::FocusPage { tab, seq: person }); + self.clear_unprompted(tab); + } else if got > self.intent_seq() { + self.record_unprompted(tab, got); + } + return true; + } + if got > 0 + && !matches!(self.intent, Some(FocusIntent::FocusPage { tab: ptab, .. }) if ptab == tab) + { + self.record_unprompted(tab, got); + } + false + } + + /// Returns the cutoff sequence if `render()` should check for an already-recorded post-cutoff click on + /// `active_tab` (`!page_focused`, or `HandBack`/`TabOut` pending on `active_tab`), so a native click that + /// arrives while GPUI focus is elsewhere still recovers even when CEF emits no focus callback. + pub fn pending_press_cutoff_for_active(&self, active_tab: Option, page_focused: bool) -> Option { + let active = active_tab?; + match self.intent { + Some(FocusIntent::HandBack { seq, .. }) => Some(self.click_cutoff_seq.max(seq)), + Some(FocusIntent::TabOut { tab, seq }) if tab == active => Some(self.click_cutoff_seq.max(seq)), + Some(FocusIntent::FocusPage { tab, .. }) if tab == active => None, + _ if page_focused => None, + _ => { + if let Some((_, seq)) = self + .unprompted_got + .iter() + .find(|(t, seq)| *t == active && *seq > self.deactivate_cutoff_seq) + { + Some(self.click_cutoff_seq.max(*seq)) + } else { + Some(self.recovery_floor_seq()) + } + } + } + } + + /// Promotes a fresh, unconsumed post-cutoff native press on `tab` to `FocusPage` during render + /// reconciliation so `render()` does not erase a click that occurred just before `pump()` ran. + pub fn on_pending_click(&mut self, tab: u64, press_seq: u64) -> bool { + if press_seq <= self.click_cutoff_seq || press_seq <= self.deactivate_cutoff_seq { + return false; + } + match self.intent { + Some(FocusIntent::HandBack { seq, .. }) if press_seq < seq => return false, + Some(FocusIntent::TabOut { tab: itab, seq }) if itab == tab && press_seq < seq => return false, + Some(FocusIntent::FocusPage { tab: itab, seq }) if itab == tab && press_seq < seq => return false, + Some(FocusIntent::TabOut { tab: itab, seq }) if itab != tab => { + self.record_unprompted(itab, seq); + } + _ => {} + } + self.handed_back = false; + self.intent = Some(FocusIntent::FocusPage { tab, seq: press_seq }); + self.clear_unprompted(tab); + true + } + + /// Recovers focus after a post-cutoff native press on `tab` arrived after precheck + /// (during `yank_to_host` or between `window.blur()` in `render()` and `on_blur` at `finish_frame`), + /// lifting `handed_back` and queueing `FocusPage` so subsequent `got_focus` events do not trigger stray yanks. + pub fn recover_click_after_yank(&mut self, tab: u64, press_seq: u64) -> bool { + if press_seq <= self.click_cutoff_seq + || press_seq <= self.deactivate_cutoff_seq + || press_seq < self.intent_seq() + { + return false; + } + self.handed_back = false; + self.intent = Some(FocusIntent::FocusPage { tab, seq: press_seq }); + self.clear_unprompted(tab); + true + } + + /// Notes that GPUI itself gave focus to the browser pane (`cx.on_focus`), lifting `handed_back` + /// so subsequent keyboard interaction inside the page behaves normally. + pub fn on_host_focus(&mut self) { + self.handed_back = false; + } + + /// Requests GPUI to blur the browser view (e.g. on "Hand back") on the next render, invalidating older clicks + /// and blocking queued key takeovers while preserving stray `unprompted_got` tabs so all native containers yield. + pub fn request_blur(&mut self, origin: Option, seq: u64) { + self.click_cutoff_seq = self.click_cutoff_seq.max(seq); + self.handed_back = true; + self.intent = Some(FocusIntent::HandBack { origin, seq }); + } + + /// Invalidates older clicks when GPUI focus leaves the browser view while the host window stays active, + /// while preserving already-recorded stray `unprompted_got` tabs so background native focus is still yanked. + pub fn on_host_blur(&mut self, seq: u64) { + self.click_cutoff_seq = self.click_cutoff_seq.max(seq); + if matches!(self.intent, Some(FocusIntent::FocusPage { seq: pseq, .. }) if pseq <= seq) { + self.intent = None; + } + } + + /// Invalidates pre-deactivation clicks and unprompted yanks when the host window loses OS activation. + /// Pending explicit `HandBack` blur remains intact so GPUI internal focus still drops cleanly. + pub fn on_window_deactivate(&mut self, seq: u64) { + self.click_cutoff_seq = self.click_cutoff_seq.max(seq); + self.deactivate_cutoff_seq = self.deactivate_cutoff_seq.max(seq); + self.unprompted_got.clear(); + if !matches!(self.intent, Some(FocusIntent::HandBack { .. })) { + self.intent = None; + } + } + + /// Resolves and consumes the pending focus plan during `CefPage::render`. + pub fn take_render_plan(&mut self, active_tab: Option, page_focused: bool, window_active: bool) -> RenderFocusPlan { + let intent = self.intent.take(); + let mut unprompted = std::mem::take(&mut self.unprompted_got); + let mut plan = RenderFocusPlan::default(); + let mut final_page_focused = page_focused; + let mut explicit_yanks: Vec = Vec::new(); + let mut blur_cutoff = 0u64; + + match intent { + None => {} + Some(FocusIntent::FocusPage { tab, .. }) => { + if window_active && active_tab == Some(tab) && !page_focused { + plan.gpui = GpuiFocusChange::FocusPage; + final_page_focused = true; + } + } + Some(FocusIntent::TabOut { tab, seq }) => { + let tab_cutoff = self.click_cutoff_seq.max(seq); + if active_tab == Some(tab) || active_tab.is_none() { + if page_focused { + plan.gpui = GpuiFocusChange::BlurPage; + blur_cutoff = tab_cutoff; + final_page_focused = false; + } + if window_active { + push_tab_yank(&mut explicit_yanks, tab, tab_cutoff); + } + } else if window_active { + if page_focused { + push_tab_yank(&mut plan.release_cef_only, tab, tab_cutoff); + plan.restore_active = active_tab; + } else { + push_tab_yank(&mut explicit_yanks, tab, tab_cutoff); + } + } + } + Some(FocusIntent::HandBack { origin, seq }) => { + let hb_cutoff = self.click_cutoff_seq.max(seq); + if page_focused { + plan.gpui = GpuiFocusChange::BlurPage; + blur_cutoff = hb_cutoff; + final_page_focused = false; + } + if window_active { + if let Some(tab) = origin { + push_tab_yank(&mut explicit_yanks, tab, hb_cutoff); + } + if let Some(tab) = active_tab { + push_tab_yank(&mut explicit_yanks, tab, hb_cutoff); + } + } + } + } + + if !window_active { + if plan.gpui == GpuiFocusChange::BlurPage { + plan.blur_cutoff_seq = blur_cutoff; + } + return plan; + } + + unprompted.sort_by_key(|(tab, _)| *tab); + let mut stray: Vec<(u64, u64)> = Vec::with_capacity(unprompted.len()); + for (tab, seq) in unprompted { + if seq > self.deactivate_cutoff_seq { + if let Some((_, max_seq)) = stray.iter_mut().find(|(t, _)| *t == tab) { + *max_seq = (*max_seq).max(seq); + } else { + stray.push((tab, seq)); + } + } + } + + if final_page_focused { + let mut background_stole = false; + for (tab, seq) in stray { + if active_tab != Some(tab) { + push_tab_yank(&mut plan.release_cef_only, tab, self.click_cutoff_seq.max(seq)); + background_stole = true; + } + } + if background_stole { + if let Some(active) = active_tab { + plan.restore_active = Some(active); + } else { + plan.yank_to_host.append(&mut plan.release_cef_only); + } + } + } else { + plan.yank_to_host = explicit_yanks; + for (tab, seq) in stray { + push_tab_yank(&mut plan.yank_to_host, tab, self.click_cutoff_seq.max(seq)); + } + } + + if plan.gpui == GpuiFocusChange::BlurPage { + plan.blur_cutoff_seq = blur_cutoff; + } + + plan + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn got_focus_alone_after_press_release_hands_focus_to_page_without_yank() { + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + let press_seq = next_focus_seq(); + tracker.record_with_seq(42, t0, press_seq); + + let fallback_seq = next_focus_seq(); + let click_seq = callback_click_seq( + true, + FocusCallback::GotFocus, + FocusSourceKind::System, + fallback_seq, + || { + tracker + .consume(42, FocusCallback::GotFocus, t0 + Duration::from_millis(12)) + .map_or(PressProbe::NotPressed, PressProbe::Pressed) + }, + || false, + ); + assert_eq!(click_seq, Some(press_seq), "released quick click delivering only on_got_focus still counts as a user click with press-time seq"); + + let mut handoff = FocusHandoff::new(); + let got_seq = next_focus_seq(); + let took_over = handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: press_seq, + key_seq: 0, + got_focus_seq: got_seq, + took_focus_seq: 0, + }, + true, + ); + assert!(took_over); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + } + + #[test] + fn fallback_consults_button_down_for_got_focus_and_system_set_focus_only() { + let mut called = false; + assert_eq!( + callback_click_seq( + true, + FocusCallback::GotFocus, + FocusSourceKind::System, + 77, + || PressProbe::Unwatched, + || { + called = true; + true + }, + ), + Some(77) + ); + assert!(called, "GotFocus falls back to button_down when watcher is unavailable"); + + assert_eq!( + callback_click_seq( + true, + FocusCallback::SetFocus, + FocusSourceKind::System, + 88, + || PressProbe::Unwatched, + || true, + ), + Some(88) + ); + + assert_eq!( + callback_click_seq( + true, + FocusCallback::SetFocus, + FocusSourceKind::Navigation, + 99, + || unreachable!("Navigation must not consume press"), + || unreachable!("Navigation must not poll button_down"), + ), + None + ); + assert_eq!( + callback_click_seq( + false, + FocusCallback::GotFocus, + FocusSourceKind::System, + 99, + || unreachable!("non-windowed must not consume press"), + || unreachable!("non-windowed must not poll button_down"), + ), + None + ); + assert_eq!( + callback_click_seq( + true, + FocusCallback::GotFocus, + FocusSourceKind::System, + 99, + || PressProbe::NotPressed, + || unreachable!("watched target returning NotPressed must not fall back to button_down"), + ), + None + ); + } + + #[test] + fn press_tracker_pairs_set_and_got_focus_expires_and_isolates_targets() { + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + + assert_eq!(tracker.record(0, t0), 0); + assert_eq!(tracker.consume(0, FocusCallback::SetFocus, t0), None); + + let s1 = tracker.record(10, t0); + assert_ne!(s1, 0); + assert_eq!(tracker.consume(20, FocusCallback::SetFocus, t0 + Duration::from_millis(5)), None, "wrong target never consumes"); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(5)), Some(s1)); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(6)), None, "SetFocus consumed once"); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(10)), Some(s1), "paired GotFocus still succeeds once"); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(12)), None, "third callback rejected"); + + let s2 = tracker.record(10, t0); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(5)), Some(s2)); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(6)), None, "GotFocus spends both"); + + tracker.record(10, t0); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + PRESS_SPAN + Duration::from_millis(1)), None, "expires after PRESS_SPAN"); + + tracker.record(10, t0); + tracker.clear_target(10); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(10)), None, "unused press cleared when focus leaves"); + + tracker.record(10, t0); + tracker.clear_all(); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(5)), None, "clear_all drops recorded press"); + } + + #[test] + fn press_tracker_deduplicates_win32_mouseactivate_bubble_and_paired_button_down() { + const WM_LBUTTONDOWN: u32 = 0x0201; + const WM_XBUTTONDOWN: u32 = 0x020B; + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + + // Nested bubbling WM_MOUSEACTIVATE (child -> parent) pushes another frame; returning eaten + // on the child leaves the parent's activation alive until paired WH_MOUSE down arrives. + let s1 = tracker.record_activate_with_seq(10, WM_XBUTTONDOWN, t0, 100); + assert_eq!(s1, 100); + assert_eq!(tracker.record_activate_with_seq(10, WM_XBUTTONDOWN, t0 + Duration::from_millis(1), 101), 100); + assert!(tracker.has_pending_activate()); + assert_eq!(tracker.fresh_press_seq_after(10, 0, t0 + Duration::from_millis(1)), None, "in-flight activation is unconfirmed"); + // Inner child returning ActivateAndEat while parent still active leaves parent frame pending; + // parent returning DeliverDown confirms it and waits for paired button-down. + assert!(!tracker.complete_activate_with_seq(10, WM_XBUTTONDOWN, MouseActivateOutcome::ActivateAndEat, t0 + Duration::from_millis(1), 100)); + assert!(tracker.has_pending_activate(), "inner eaten must not drop outer bubbling activation"); + assert!(tracker.complete_activate_with_seq(10, WM_XBUTTONDOWN, MouseActivateOutcome::DeliverDown, t0 + Duration::from_millis(2), 100)); + assert_eq!(tracker.fresh_press_seq_after(10, 0, t0 + Duration::from_millis(2)), Some(100)); + assert!(tracker.has_pending_activate(), "non-eaten activation waits for paired button-down"); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(2)), Some(100)); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(3)), Some(100)); + assert_eq!(tracker.record_button_down_with_seq(10, WM_XBUTTONDOWN, t0 + Duration::from_millis(4), 102), 100); + assert!(!tracker.has_pending_activate()); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(5)), None, "paired button-down must not re-enable spent activation click"); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(5)), None); + + // Outermost ActivateAndEat clears pairing immediately while keeping confirmed press so a subsequent click records fresh. + assert_eq!(tracker.record_activate_with_seq(10, WM_XBUTTONDOWN, t0 + Duration::from_millis(15), 150), 150); + assert!(tracker.complete_activate_with_seq(10, WM_XBUTTONDOWN, MouseActivateOutcome::ActivateAndEat, t0 + Duration::from_millis(15), 150)); + assert!(!tracker.has_pending_activate(), "outermost eaten activation expects no WH_MOUSE down"); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(16)), Some(150)); + + let s2 = tracker.record_button_down_with_seq(10, WM_XBUTTONDOWN, t0 + Duration::from_millis(20), 200); + assert_eq!(s2, 200); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(22)), Some(200)); + + // Activation cancelled via clear_target absorbs a stalled (> PRESS_SPAN) paired button-down without re-arming. + assert_eq!(tracker.record_activate_with_seq(10, WM_LBUTTONDOWN, t0 + Duration::from_millis(40), 300), 300); + assert!(tracker.complete_activate_with_seq(10, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t0 + Duration::from_millis(40), 300)); + tracker.clear_target(10); + assert!(tracker.has_pending_activate()); + let stalled = t0 + Duration::from_millis(40) + PRESS_SPAN + Duration::from_millis(250); + assert_eq!(tracker.record_button_down_with_seq(10, WM_LBUTTONDOWN, stalled, 301), 300); + assert!(!tracker.has_pending_activate()); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, stalled + Duration::from_millis(1)), None, "stalled cancelled activation click stays spent when button-down arrives"); + + // Expired consume (> PRESS_SPAN) does NOT erase pending_activate_downs, so a delayed paired button-down still deduplicates. + let t1 = stalled + Duration::from_millis(50); + assert_eq!(tracker.record_activate_with_seq(10, WM_LBUTTONDOWN, t1, 400), 400); + assert!(tracker.complete_activate_with_seq(10, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t1, 400)); + let after_span = t1 + PRESS_SPAN + Duration::from_millis(10); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, after_span), None, "expired activate record rejected"); + assert!(tracker.has_pending_activate(), "expired consume must keep pending pairing until paired button-down arrives"); + assert_eq!(tracker.record_button_down_with_seq(10, WM_LBUTTONDOWN, after_span + Duration::from_millis(5), 401), 400); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, after_span + Duration::from_millis(6)), None, "delayed paired down after expiry must not open fresh press"); + } + + #[test] + fn press_tracker_preserves_outer_win32_activate_across_reentrant_nested_dispatch() { + const WM_LBUTTONDOWN: u32 = 0x0201; + const WM_RBUTTONDOWN: u32 = 0x0204; + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + + // Outer WM_MOUSEACTIVATE on (10, WM_LBUTTONDOWN) stays unconfirmed for both callbacks and cross-thread + // recovery while in-flight, and preserves its click across nested reentrant activations on other keys. + assert_eq!(tracker.record_activate_with_seq(10, WM_LBUTTONDOWN, t0, 100), 100); + assert_eq!(tracker.fresh_press_seq_after(10, 50, t0), None, "in-flight activation cannot wake render recovery before return"); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0), None, "in-flight activation cannot publish before outcome"); + assert_eq!(tracker.record_activate_with_seq(20, WM_RBUTTONDOWN, t0 + Duration::from_millis(1), 200), 200); + assert_eq!(tracker.fresh_press_seq_after(20, 50, t0 + Duration::from_millis(1)), None); + assert!(tracker.complete_activate_with_seq(20, WM_RBUTTONDOWN, MouseActivateOutcome::ActivateAndEat, t0 + Duration::from_millis(1), 200)); + assert!(!tracker.has_pending_activate_for(20, WM_RBUTTONDOWN)); + assert!(tracker.has_pending_activate_for(10, WM_LBUTTONDOWN)); + assert_eq!(tracker.fresh_press_seq_after(10, 50, t0 + Duration::from_millis(2)), None); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(2)), None); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(2)), None); + + assert_eq!(tracker.record_activate_with_seq(10, WM_RBUTTONDOWN, t0 + Duration::from_millis(2), 201), 201); + assert!(tracker.complete_activate_with_seq(10, WM_RBUTTONDOWN, MouseActivateOutcome::DeliverDown, t0 + Duration::from_millis(2), 201)); + // Outer (10, WM_LBUTTONDOWN) stays unconfirmed while still on activate_stack, then confirms on DeliverDown. + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(2)), None); + assert!(tracker.complete_activate_with_seq(10, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t0 + Duration::from_millis(2), 100)); + assert!(tracker.has_pending_activate_for(10, WM_LBUTTONDOWN)); + assert!(tracker.has_pending_activate_for(10, WM_RBUTTONDOWN)); + + // Unrelated unpaired button-down on (30, WM_LBUTTONDOWN) leaves pending pairings intact, + // and each pending activation pairs independently by `(target, msg)` returning its own seq. + assert_eq!(tracker.record_button_down_with_seq(30, WM_LBUTTONDOWN, t0 + Duration::from_millis(3), 260), 260); + assert_eq!(tracker.record_button_down_with_seq(10, WM_LBUTTONDOWN, t0 + Duration::from_millis(4), 261), 100); + assert!(!tracker.has_pending_activate_for(10, WM_LBUTTONDOWN)); + assert!(tracker.has_pending_activate_for(10, WM_RBUTTONDOWN)); + assert_eq!(tracker.fresh_press_seq_after(10, 50, t0 + Duration::from_millis(4)), Some(201)); + assert_eq!(tracker.consume(10, FocusCallback::SetFocus, t0 + Duration::from_millis(4)), Some(100)); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(4)), Some(100)); + assert_eq!(tracker.record_button_down_with_seq(10, WM_RBUTTONDOWN, t0 + Duration::from_millis(5), 262), 201); + assert!(!tracker.has_pending_activate()); + + // drop_target removes only that target's frames and pending pairings. + assert_eq!(tracker.record_activate_with_seq(40, WM_LBUTTONDOWN, t0 + Duration::from_millis(10), 300), 300); + assert_eq!(tracker.record_activate_with_seq(50, WM_LBUTTONDOWN, t0 + Duration::from_millis(11), 301), 301); + assert!(tracker.complete_activate_with_seq(50, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t0 + Duration::from_millis(11), 301)); + tracker.drop_target(40); + assert!(!tracker.has_pending_activate_for(40, WM_LBUTTONDOWN)); + assert!(tracker.has_pending_activate_for(50, WM_LBUTTONDOWN)); + assert_eq!(tracker.record_button_down_with_seq(50, WM_LBUTTONDOWN, t0 + Duration::from_millis(12), 302), 301); + + // When paired button-down arrives AFTER nested non-eaten dispatch and BEFORE CEF focus callbacks, + // consume() still succeeds once with the outer activation seq. + let t1 = t0 + Duration::from_millis(20); + assert_eq!(tracker.record_activate_with_seq(60, WM_LBUTTONDOWN, t1, 400), 400); + assert_eq!(tracker.record_activate_with_seq(70, WM_RBUTTONDOWN, t1 + Duration::from_millis(1), 401), 401); + assert!(tracker.complete_activate_with_seq(70, WM_RBUTTONDOWN, MouseActivateOutcome::DeliverDown, t1 + Duration::from_millis(1), 401)); + assert!(tracker.complete_activate_with_seq(60, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t1 + Duration::from_millis(1), 400)); + assert_eq!(tracker.record_button_down_with_seq(60, WM_LBUTTONDOWN, t1 + Duration::from_millis(2), 402), 400); + assert_eq!(tracker.consume(60, FocusCallback::GotFocus, t1 + Duration::from_millis(3)), Some(400)); + assert_eq!(tracker.consume(60, FocusCallback::GotFocus, t1 + Duration::from_millis(4)), None); + + // When CEF callbacks already spent the outer click across nested dispatch, a later paired button-down + // returns the outer seq without re-arming consume(). + let t2 = t0 + Duration::from_millis(30); + assert_eq!(tracker.record_activate_with_seq(80, WM_LBUTTONDOWN, t2, 500), 500); + assert_eq!(tracker.record_activate_with_seq(90, WM_RBUTTONDOWN, t2 + Duration::from_millis(1), 501), 501); + assert!(tracker.complete_activate_with_seq(90, WM_RBUTTONDOWN, MouseActivateOutcome::ActivateAndEat, t2 + Duration::from_millis(1), 501)); + assert!(tracker.complete_activate_with_seq(80, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t2 + Duration::from_millis(1), 500)); + assert_eq!(tracker.consume(80, FocusCallback::SetFocus, t2 + Duration::from_millis(2)), Some(500)); + assert_eq!(tracker.consume(80, FocusCallback::GotFocus, t2 + Duration::from_millis(2)), Some(500)); + assert_eq!(tracker.record_button_down_with_seq(80, WM_LBUTTONDOWN, t2 + Duration::from_millis(3), 502), 500); + assert_eq!(tracker.consume(80, FocusCallback::GotFocus, t2 + Duration::from_millis(4)), None); + + // Same-target nested two-button activation preserves outer click record and keeps both unconfirmed while active: + // 1) Neither (100, R) nor outer (100, L) can consume while in-flight; outer (100, L) stays unconfirmed until + // outer DeliverDown, then its paired button-down bumps 600 to the front of consumption. + let t3 = t0 + Duration::from_millis(40); + assert_eq!(tracker.record_activate_with_seq(100, WM_LBUTTONDOWN, t3, 600), 600); + assert_eq!(tracker.record_activate_with_seq(100, WM_RBUTTONDOWN, t3 + Duration::from_millis(1), 601), 601); + assert_eq!(tracker.consume(100, FocusCallback::SetFocus, t3 + Duration::from_millis(1)), None); + assert_eq!(tracker.fresh_press_seq_after(100, 0, t3 + Duration::from_millis(1)), None); + assert!(tracker.complete_activate_with_seq(100, WM_RBUTTONDOWN, MouseActivateOutcome::DeliverDown, t3 + Duration::from_millis(1), 601)); + assert_eq!(tracker.consume(100, FocusCallback::SetFocus, t3 + Duration::from_millis(2)), None); + assert_eq!(tracker.fresh_press_seq_after(100, 601, t3 + Duration::from_millis(2)), None); + assert!(tracker.complete_activate_with_seq(100, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t3 + Duration::from_millis(2), 600)); + assert_eq!(tracker.record_button_down_with_seq(100, WM_LBUTTONDOWN, t3 + Duration::from_millis(3), 602), 600); + assert_eq!(tracker.consume(100, FocusCallback::SetFocus, t3 + Duration::from_millis(3)), Some(600)); + assert_eq!(tracker.consume(100, FocusCallback::GotFocus, t3 + Duration::from_millis(3)), Some(600)); + assert_eq!(tracker.consume(100, FocusCallback::GotFocus, t3 + Duration::from_millis(4)), Some(601)); + assert_eq!(tracker.consume(100, FocusCallback::GotFocus, t3 + Duration::from_millis(4)), None); + assert_eq!(tracker.record_button_down_with_seq(100, WM_RBUTTONDOWN, t3 + Duration::from_millis(5), 603), 601); + assert_eq!(tracker.consume(100, FocusCallback::GotFocus, t3 + Duration::from_millis(6)), None); + + // 2) Same-target nested eaten activation (110, WM_RBUTTONDOWN) drops its unused press record without + // ever exposing an in-flight recovery press, leaving outer (110, WM_LBUTTONDOWN) clean on stack. + let t4 = t0 + Duration::from_millis(50); + assert_eq!(tracker.record_activate_with_seq(110, WM_LBUTTONDOWN, t4, 700), 700); + assert_eq!(tracker.record_activate_with_seq(110, WM_RBUTTONDOWN, t4 + Duration::from_millis(1), 701), 701); + assert_eq!(tracker.fresh_press_seq_after(110, 0, t4 + Duration::from_millis(1)), None); + assert!(!tracker.complete_activate_with_seq(110, WM_RBUTTONDOWN, MouseActivateOutcome::ActivateAndEat, t4 + Duration::from_millis(1), 701)); + assert_eq!(tracker.fresh_press_seq_after(110, 0, t4 + Duration::from_millis(2)), None); + assert!(tracker.complete_activate_with_seq(110, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t4 + Duration::from_millis(2), 700)); + assert_eq!(tracker.consume(110, FocusCallback::SetFocus, t4 + Duration::from_millis(2)), Some(700)); + assert_eq!(tracker.consume(110, FocusCallback::GotFocus, t4 + Duration::from_millis(2)), Some(700)); + assert_eq!(tracker.record_button_down_with_seq(110, WM_LBUTTONDOWN, t4 + Duration::from_millis(3), 702), 700); + assert_eq!(tracker.consume(110, FocusCallback::GotFocus, t4 + Duration::from_millis(4)), None); + + // 3) Same-target nested non-eaten where outer paired button-down (120, WM_LBUTTONDOWN) precedes CEF focus callbacks + // bumps outer's press record to the back so consume() picks outer 800 first. + let t5 = t0 + Duration::from_millis(60); + assert_eq!(tracker.record_activate_with_seq(120, WM_LBUTTONDOWN, t5, 800), 800); + assert_eq!(tracker.record_activate_with_seq(120, WM_RBUTTONDOWN, t5 + Duration::from_millis(1), 801), 801); + assert!(tracker.complete_activate_with_seq(120, WM_RBUTTONDOWN, MouseActivateOutcome::DeliverDown, t5 + Duration::from_millis(1), 801)); + assert!(tracker.complete_activate_with_seq(120, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t5 + Duration::from_millis(1), 800)); + assert_eq!(tracker.record_button_down_with_seq(120, WM_LBUTTONDOWN, t5 + Duration::from_millis(2), 802), 800); + assert_eq!(tracker.consume(120, FocusCallback::GotFocus, t5 + Duration::from_millis(3)), Some(800)); + + // 4) NoActivateAndEat never exposes a press before or after complete_activate when no button-down arrived. + let t6 = t0 + Duration::from_millis(70); + assert_eq!(tracker.record_activate_with_seq(125, WM_LBUTTONDOWN, t6, 850), 850); + assert_eq!(tracker.fresh_press_seq_after(125, 0, t6), None); + assert_eq!(tracker.consume(125, FocusCallback::GotFocus, t6), None); + assert!(!tracker.complete_activate_with_seq(125, WM_LBUTTONDOWN, MouseActivateOutcome::NoActivateAndEat, t6, 850)); + assert_eq!(tracker.fresh_press_seq_after(125, 0, t6 + Duration::from_millis(1)), None); + assert_eq!(tracker.consume(125, FocusCallback::GotFocus, t6 + Duration::from_millis(1)), None); + + // 5) Reentrant `left -> right -> left`: mid-dispatch `record_button_down` confirms and upgrades (130, L) + // to confirmation-time 903 before `right` unwinds, and outer `left` returning `NoActivateAndEat` + // preserves 903 without overwriting it with 906. + let t7 = t0 + Duration::from_millis(80); + assert_eq!(tracker.record_activate_with_seq(130, WM_LBUTTONDOWN, t7, 900), 900); + assert_eq!(tracker.record_activate_with_seq(130, WM_RBUTTONDOWN, t7 + Duration::from_millis(1), 901), 901); + assert_eq!(tracker.record_activate_with_seq(130, WM_LBUTTONDOWN, t7 + Duration::from_millis(2), 902), 900); + assert_eq!(tracker.fresh_press_seq_after(130, 0, t7 + Duration::from_millis(2)), None); + assert_eq!(tracker.consume(130, FocusCallback::SetFocus, t7 + Duration::from_millis(2)), None); + assert_eq!(tracker.record_button_down_with_seq(130, WM_LBUTTONDOWN, t7 + Duration::from_millis(2), 903), 903); + assert_eq!(tracker.fresh_press_seq_after(130, 0, t7 + Duration::from_millis(2)), Some(903)); + assert_eq!(tracker.consume(130, FocusCallback::SetFocus, t7 + Duration::from_millis(2)), Some(903)); + assert!(!tracker.complete_activate_with_seq(130, WM_LBUTTONDOWN, MouseActivateOutcome::DeliverDown, t7 + Duration::from_millis(2), 904)); + assert_eq!(tracker.consume(130, FocusCallback::SetFocus, t7 + Duration::from_millis(3)), None); + assert!(!tracker.complete_activate_with_seq(130, WM_RBUTTONDOWN, MouseActivateOutcome::ActivateAndEat, t7 + Duration::from_millis(3), 905)); + assert!(tracker.complete_activate_with_seq(130, WM_LBUTTONDOWN, MouseActivateOutcome::NoActivateAndEat, t7 + Duration::from_millis(4), 906)); + assert!(!tracker.has_pending_activate_for(130, WM_LBUTTONDOWN)); + assert_eq!(tracker.fresh_press_seq_after(130, 0, t7 + Duration::from_millis(4)), Some(903)); + assert_eq!(tracker.consume(130, FocusCallback::GotFocus, t7 + Duration::from_millis(4)), Some(903)); + assert_eq!(tracker.consume(130, FocusCallback::GotFocus, t7 + Duration::from_millis(5)), None); + } + + #[test] + fn press_tracker_unwatched_release_gate_blocks_until_matching_generation_clears() { + let mut tracker = PressTracker::new(); + assert_eq!(tracker.block_unwatched_until_release(0), None); + assert!(!tracker.is_unwatched_blocked(7)); + + let g1 = tracker.block_unwatched_until_release(7); + assert_eq!(g1, Some(1)); + assert!(tracker.is_unwatched_blocked(7)); + + // Second block while already non-empty bumps generation and asks caller not to spawn another poller. + assert_eq!(tracker.block_unwatched_until_release(8), None); + let g2 = tracker.unwatched_block_gen(); + assert_eq!(g2, 2); + assert!(tracker.is_unwatched_blocked(7)); + assert!(tracker.is_unwatched_blocked(8)); + + // Stale generation cannot clear the newer block. + assert!(!tracker.clear_unwatched_release_block_if(g1.unwrap())); + assert!(tracker.is_unwatched_blocked(7)); + assert!(tracker.is_unwatched_blocked(8)); + + // drop_target removes only the closed target. + tracker.drop_target(7); + assert!(!tracker.is_unwatched_blocked(7)); + assert!(tracker.is_unwatched_blocked(8)); + + // Matching generation clears remaining entries. + assert!(tracker.clear_unwatched_release_block_if(g2)); + assert!(!tracker.is_unwatched_blocked(8)); + } + + #[test] + fn render_reconciles_unconsumed_post_cutoff_press_and_clear_target_up_to_preserves_newer_click() { + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + + // Older press at seq 50 cleared by cutoff 50. + tracker.record_with_seq(10, t0, 50); + assert_eq!(tracker.fresh_press_seq_after(10, 50, t0 + Duration::from_millis(1)), None); + tracker.clear_target_up_to(10, 50); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(2)), None); + + // Newer press at seq 75 survives yank_to_host cutoff 60, even if CEF already consumed GotFocus + // before writing back st.click_seq. + tracker.record_with_seq(10, t0 + Duration::from_millis(5), 75); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(6)), Some(75)); + assert_eq!(tracker.fresh_press_seq_after(10, 60, t0 + Duration::from_millis(7)), Some(75)); + tracker.clear_target_up_to(10, 60); + assert_eq!(tracker.fresh_press_seq_after(10, 60, t0 + Duration::from_millis(8)), Some(75)); + + // Render reconciliation promotes the post-cutoff native press into FocusPage, cancelling HandBack. + let mut handoff = FocusHandoff::new(); + handoff.request_blur(Some(1), 60); + assert_eq!(handoff.pending_press_cutoff_for_active(Some(1), false), Some(60)); + assert!(handoff.on_pending_click(1, 75)); + assert_eq!(handoff.pending_press_cutoff_for_active(Some(1), false), None); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + + // Similarly, active-tab unprompted got_focus is overridden by a newer native press before render yanks it. + let mut stray = FocusHandoff::new(); + let got_seq = next_focus_seq(); + let press_seq = next_focus_seq(); + stray.on_pump_tab(1, TabFocusEvents { got_focus_seq: got_seq, ..Default::default() }, true); + assert_eq!(stray.pending_press_cutoff_for_active(Some(1), false), Some(got_seq)); + assert!(stray.on_pending_click(1, press_seq)); + assert_eq!( + stray.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + } + + #[test] + fn queued_key_cannot_cancel_hand_back_before_or_after_render_until_click_or_host_focus() { + let mut handoff = FocusHandoff::new(); + let hb_seq = next_focus_seq(); + handoff.request_blur(Some(1), hb_seq); + + // Queued key repeat arriving AFTER HandBack cutoff but BEFORE render must NOT cancel HandBack or take_over. + let queued_key_before_render = next_focus_seq(); + assert!(!handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: 0, + key_seq: queued_key_before_render, + got_focus_seq: 0, + took_focus_seq: 0, + }, + true, + )); + let plan = handoff.take_render_plan(Some(1), true, true); + assert_eq!( + plan, + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![TabYank { tab: 1, cutoff_seq: hb_seq }], + restore_active: None, + blur_cutoff_seq: hb_seq, + } + ); + + // Even after render consumed HandBack, another queued key repeat must NOT trigger FocusPage. + let queued_key_after_render = next_focus_seq(); + assert!(!handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: 0, + key_seq: queued_key_after_render, + got_focus_seq: 0, + took_focus_seq: 0, + }, + true, + )); + assert_eq!(handoff.take_render_plan(Some(1), false, true), RenderFocusPlan::default()); + + // A genuine user click DOES cancel handed_back and restores FocusPage. + let click_seq = next_focus_seq(); + assert!(handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq, + key_seq: 0, + got_focus_seq: click_seq, + took_focus_seq: 0, + }, + true, + )); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + + // After another HandBack, explicit GPUI host focus (e.g. Tab-in) lifts handed_back so typing works again. + let hb2 = next_focus_seq(); + handoff.request_blur(Some(1), hb2); + let _ = handoff.take_render_plan(Some(1), true, true); + handoff.on_host_focus(); + let next_key = next_focus_seq(); + assert!(handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: 0, + key_seq: next_key, + got_focus_seq: 0, + took_focus_seq: 0, + }, + true, + )); + } + + #[test] + fn split_pumps_keep_focus_page_across_subsequent_unclicked_got_focus() { + let mut handoff = FocusHandoff::new(); + let s1 = next_focus_seq(); + assert!(handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: s1, + key_seq: 0, + got_focus_seq: 0, + took_focus_seq: 0, + }, + true, + )); + let s2 = next_focus_seq(); + assert!(!handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: 0, + key_seq: 0, + got_focus_seq: s2, + took_focus_seq: 0, + }, + true, + )); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + assert_eq!(handoff.take_render_plan(Some(1), true, true), RenderFocusPlan::default()); + } + + #[test] + fn unprompted_got_focus_yanks_originating_tab_only_when_eligible_and_window_active() { + let mut handoff = FocusHandoff::new(); + let s1 = next_focus_seq(); + assert!(!handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: 0, + key_seq: 0, + got_focus_seq: s1, + took_focus_seq: 0, + }, + true, + )); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + yank_to_host: vec![TabYank { tab: 1, cutoff_seq: s1 }], + ..Default::default() + } + ); + + let s2 = next_focus_seq(); + handoff.on_pump_tab(1, TabFocusEvents { got_focus_seq: s2, ..Default::default() }, true); + assert_eq!(handoff.take_render_plan(Some(1), true, true), RenderFocusPlan::default(), "already page focused"); + + let s3 = next_focus_seq(); + handoff.on_pump_tab(1, TabFocusEvents { got_focus_seq: s3, ..Default::default() }, true); + assert_eq!(handoff.take_render_plan(Some(1), false, false), RenderFocusPlan::default(), "inactive window never yanks"); + + // Inactive window never takes focus on delayed click either. + let s4 = next_focus_seq(); + assert!(handoff.on_pump_tab(1, TabFocusEvents { click_seq: s4, ..Default::default() }, true)); + assert_eq!(handoff.take_render_plan(Some(1), false, false), RenderFocusPlan::default(), "inactive window never steals focus"); + + // User click before render overrides a stale YankTab for the active tab. + let s5 = next_focus_seq(); + let s6 = next_focus_seq(); + handoff.on_pump_tab(1, TabFocusEvents { got_focus_seq: s5, ..Default::default() }, true); + assert!(handoff.on_pump_tab(1, TabFocusEvents { click_seq: s6, ..Default::default() }, true)); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + } + + #[test] + fn background_tab_never_steals_focus_from_active_tab_and_order_is_independent() { + // 1. Background tab 1 unclicked got_focus while tab 2 is focused in GPUI + // must release tab 1 inside CEF and restore tab 2 without focusing the host. + let mut handoff = FocusHandoff::new(); + let s1 = next_focus_seq(); + assert!(!handoff.on_pump_tab(1, TabFocusEvents { got_focus_seq: s1, ..Default::default() }, false)); + assert_eq!( + handoff.take_render_plan(Some(2), true, true), + RenderFocusPlan { + gpui: GpuiFocusChange::None, + release_cef_only: vec![TabYank { tab: 1, cutoff_seq: s1 }], + yank_to_host: vec![], + restore_active: Some(2), + blur_cutoff_seq: 0, + } + ); + + // 2. Background tab with a HIGHER got_focus_seq than active tab's click click_seq + // must produce the exact same FocusPage + background release regardless of tab order. + let s_click = next_focus_seq(); + let s_bg_later = next_focus_seq(); + + let mut forward = FocusHandoff::new(); + assert!(!forward.on_pump_tab(1, TabFocusEvents { click_seq: s_bg_later, key_seq: s_bg_later, got_focus_seq: s_bg_later, took_focus_seq: s_bg_later }, false)); + assert!(forward.on_pump_tab(2, TabFocusEvents { click_seq: s_click, ..Default::default() }, true)); + let plan_forward = forward.take_render_plan(Some(2), false, true); + + let mut reverse = FocusHandoff::new(); + assert!(reverse.on_pump_tab(2, TabFocusEvents { click_seq: s_click, ..Default::default() }, true)); + assert!(!reverse.on_pump_tab(1, TabFocusEvents { click_seq: s_bg_later, key_seq: s_bg_later, got_focus_seq: s_bg_later, took_focus_seq: s_bg_later }, false)); + let plan_reverse = reverse.take_render_plan(Some(2), false, true); + + let expected = RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + release_cef_only: vec![TabYank { tab: 1, cutoff_seq: s_bg_later }], + yank_to_host: vec![], + restore_active: Some(2), + blur_cutoff_seq: 0, + }; + assert_eq!(plan_forward, expected); + assert_eq!(plan_reverse, expected); + + // 3. Delayed click on tab 1 must not focus tab 2 if active switched before render. + let mut switched = FocusHandoff::new(); + assert!(switched.on_pump_tab(1, TabFocusEvents { click_seq: next_focus_seq(), ..Default::default() }, true)); + assert_eq!(switched.take_render_plan(Some(2), false, true), RenderFocusPlan::default()); + } + + #[test] + fn tab_out_crossing_tab_switch_does_not_blur_new_tab_and_blur_preserves_stray_yanks() { + // 1. Tab 1 emits TabOut, then a popup/tab switch makes Tab 2 active before render + // while GPUI is focused: must NOT blur GPUI away from Tab 2, only release Tab 1 & restore Tab 2. + let mut handoff = FocusHandoff::new(); + let k1 = next_focus_seq(); + let took1 = next_focus_seq(); + assert!(handoff.on_pump_tab(1, TabFocusEvents { key_seq: k1, took_focus_seq: took1, ..Default::default() }, true)); + assert_eq!( + handoff.take_render_plan(Some(2), true, true), + RenderFocusPlan { + gpui: GpuiFocusChange::None, + release_cef_only: vec![TabYank { tab: 1, cutoff_seq: took1 }], + yank_to_host: vec![], + restore_active: Some(2), + blur_cutoff_seq: 0, + } + ); + + // 2. Tab 1 emits TabOut while Tab 2 also emits unprompted got_focus on the same pump; + // GPUI blurs back to host AND both Tab 1 and Tab 2 are yanked with their own cutoffs. + let mut combined = FocusHandoff::new(); + let k2 = next_focus_seq(); + let took2 = next_focus_seq(); + let bg2 = next_focus_seq(); + assert!(combined.on_pump_tab(1, TabFocusEvents { key_seq: k2, took_focus_seq: took2, ..Default::default() }, true)); + assert!(!combined.on_pump_tab(2, TabFocusEvents { got_focus_seq: bg2, ..Default::default() }, false)); + assert_eq!( + combined.take_render_plan(Some(1), true, true), + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![ + TabYank { tab: 1, cutoff_seq: took2 }, + TabYank { tab: 2, cutoff_seq: bg2 }, + ], + restore_active: None, + blur_cutoff_seq: took2, + } + ); + + // 3. HandBack requested while Tab 1 was active, then Tab 2 becomes active before render + // with Tab 3 unprompted got_focus: page-wide HandBack blurs GPUI at hb_seq and yanks each tab with its own cutoff. + let mut hb = FocusHandoff::new(); + let hb_seq = next_focus_seq(); + hb.request_blur(Some(1), hb_seq); + let stray3 = next_focus_seq(); + assert!(!hb.on_pump_tab(3, TabFocusEvents { got_focus_seq: stray3, ..Default::default() }, false)); + assert_eq!( + hb.take_render_plan(Some(2), true, true), + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![ + TabYank { tab: 1, cutoff_seq: hb_seq }, + TabYank { tab: 2, cutoff_seq: hb_seq }, + TabYank { tab: 3, cutoff_seq: stray3 }, + ], + restore_active: None, + blur_cutoff_seq: hb_seq, + } + ); + } + + #[test] + fn hand_back_is_not_replaced_by_newer_tab_out_when_popup_switches_active_tab() { + let mut handoff = FocusHandoff::new(); + let hb_seq = next_focus_seq(); + let took_after = next_focus_seq(); + handoff.request_blur(Some(1), hb_seq); + assert!(!handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: 0, + key_seq: 0, + got_focus_seq: 0, + took_focus_seq: took_after, + }, + true, + )); + assert_eq!( + handoff.take_render_plan(Some(2), true, true), + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![ + TabYank { tab: 1, cutoff_seq: took_after }, + TabYank { tab: 2, cutoff_seq: hb_seq }, + ], + restore_active: None, + blur_cutoff_seq: hb_seq, + } + ); + } + + #[test] + fn hand_back_and_host_blur_preserve_stray_yanks_including_pre_cutoff_got_focus() { + // Background tab 2 got_focus pumped BEFORE request_blur stays in yank_to_host. + let mut before_hb = FocusHandoff::new(); + let stray_before = next_focus_seq(); + before_hb.on_pump_tab(2, TabFocusEvents { got_focus_seq: stray_before, ..Default::default() }, false); + let hb_seq1 = next_focus_seq(); + before_hb.request_blur(Some(1), hb_seq1); + assert_eq!( + before_hb.take_render_plan(Some(1), true, true), + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![ + TabYank { tab: 1, cutoff_seq: hb_seq1 }, + TabYank { tab: 2, cutoff_seq: hb_seq1 }, + ], + restore_active: None, + blur_cutoff_seq: hb_seq1, + } + ); + + // Background tab 2 got_focus with seq < handback_seq pumped AFTER request_blur still yanks. + let mut delayed_hb = FocusHandoff::new(); + let stray_pre_hb = next_focus_seq(); + let hb_seq = next_focus_seq(); + delayed_hb.request_blur(Some(1), hb_seq); + assert!(!delayed_hb.on_pump_tab(2, TabFocusEvents { got_focus_seq: stray_pre_hb, ..Default::default() }, false)); + assert_eq!( + delayed_hb.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::None, + release_cef_only: vec![], + yank_to_host: vec![ + TabYank { tab: 1, cutoff_seq: hb_seq }, + TabYank { tab: 2, cutoff_seq: hb_seq }, + ], + restore_active: None, + blur_cutoff_seq: 0, + } + ); + + // Background tab 2 pumped before on_host_blur stays in yank_to_host. + let mut host_blur = FocusHandoff::new(); + let stray_before_blur = next_focus_seq(); + host_blur.on_pump_tab(2, TabFocusEvents { got_focus_seq: stray_before_blur, ..Default::default() }, false); + let blur_seq = next_focus_seq(); + host_blur.on_host_blur(blur_seq); + assert_eq!( + host_blur.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::None, + release_cef_only: vec![], + yank_to_host: vec![TabYank { tab: 2, cutoff_seq: blur_seq }], + restore_active: None, + blur_cutoff_seq: 0, + } + ); + } + + #[test] + fn hand_back_deactivation_and_threaded_cutoff_race() { + let mut handoff = FocusHandoff::new(); + + // 1. HandBack while window is inactive must blur GPUI handle only (zero native focus calls). + let hb_inactive_seq = next_focus_seq(); + handoff.request_blur(Some(1), hb_inactive_seq); + assert_eq!( + handoff.take_render_plan(Some(1), true, false), + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![], + restore_active: None, + blur_cutoff_seq: hb_inactive_seq, + } + ); + + // 2. Threaded cutoff race: press happens at press_seq, then HandBack cutoff happens at handback_seq > press_seq, + // then delayed CEF callback publishes click_seq = press_seq alongside a later got_focus_seq = got_after. + // Because click_seq <= click_cutoff_seq, it must NOT take focus back; instead unprompted got_focus yanks! + let press_seq = next_focus_seq(); + let handback_seq = next_focus_seq(); + let got_after = next_focus_seq(); + handoff.request_blur(Some(1), handback_seq); + assert!(!handoff.on_pump_tab( + 1, + TabFocusEvents { + click_seq: press_seq, + key_seq: 0, + got_focus_seq: got_after, + took_focus_seq: 0, + }, + true, + )); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::None, + release_cef_only: vec![], + yank_to_host: vec![TabYank { tab: 1, cutoff_seq: got_after }], + restore_active: None, + blur_cutoff_seq: 0, + } + ); + + // 3. Window deactivation invalidates pre-deactivation clicks and stray yanks, preserves pending HandBack, + // and accepts post-deactivation clicks upon reactivation. + let mut deact = FocusHandoff::new(); + let pre_click = next_focus_seq(); + let pre_stray = next_focus_seq(); + deact.on_pump_tab(1, TabFocusEvents { click_seq: pre_click, ..Default::default() }, true); + deact.on_pump_tab(2, TabFocusEvents { got_focus_seq: pre_stray, ..Default::default() }, false); + deact.on_window_deactivate(next_focus_seq()); + assert_eq!(deact.take_render_plan(Some(1), false, true), RenderFocusPlan::default()); + + let post_click = next_focus_seq(); + assert!(deact.on_pump_tab(1, TabFocusEvents { click_seq: post_click, got_focus_seq: post_click, ..Default::default() }, true)); + assert_eq!( + deact.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + + // Pre-blur click discarded by on_host_blur, and newer post-took click in same batch still wins. + let mut blur_case = FocusHandoff::new(); + blur_case.on_pump_tab(1, TabFocusEvents { click_seq: next_focus_seq(), ..Default::default() }, true); + blur_case.on_host_blur(next_focus_seq()); + assert_eq!(blur_case.take_render_plan(Some(1), false, true), RenderFocusPlan::default()); + let old_took = next_focus_seq(); + let newer_click = next_focus_seq(); + assert!(blur_case.on_pump_tab(1, TabFocusEvents { click_seq: newer_click, took_focus_seq: old_took, ..Default::default() }, true)); + assert_eq!( + blur_case.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + } + + #[test] + fn background_stray_got_focus_never_inflates_active_tab_or_blur_cutoff() { + let mut tracker = PressTracker::new(); + let mut handoff = FocusHandoff::new(); + let t0 = Instant::now(); + + let hb_seq = next_focus_seq(); + let press_seq = next_focus_seq(); + let bg_stale_press = next_focus_seq(); + let bg_stray_got = next_focus_seq(); + + handoff.request_blur(Some(1), hb_seq); + assert!(!handoff.on_pump_tab(2, TabFocusEvents { got_focus_seq: bg_stray_got, ..Default::default() }, false)); + + let plan = handoff.take_render_plan(Some(1), true, true); + assert_eq!( + plan, + RenderFocusPlan { + gpui: GpuiFocusChange::BlurPage, + release_cef_only: vec![], + yank_to_host: vec![ + TabYank { tab: 1, cutoff_seq: hb_seq }, + TabYank { tab: 2, cutoff_seq: bg_stray_got }, + ], + restore_active: None, + blur_cutoff_seq: hb_seq, + } + ); + assert_eq!(plan.active_blur_or_yank_cutoff(Some(1)), Some(hb_seq)); + + // Genuine active-tab press at `press_seq` (`hb_seq < press_seq < bg_stray_got`) survives + // active tab's per-tab yank cutoff, while background tab's stale press is cleared by `bg_stray_got`. + tracker.record_with_seq(10, t0, press_seq); + for y in &plan.yank_to_host { + let native_hwnd = if y.tab == 1 { 10 } else { 20 }; + tracker.clear_target_up_to(native_hwnd, y.cutoff_seq); + } + assert_eq!(tracker.fresh_press_seq_after(10, hb_seq, t0 + Duration::from_millis(5)), Some(press_seq)); + assert!(handoff.recover_click_after_yank(1, press_seq)); + + // Background stale press <= bg_stray_got is cleared under tab 2's cutoff. + tracker.record_with_seq(20, t0, bg_stale_press); + tracker.clear_target_up_to(20, bg_stray_got); + assert_eq!(tracker.fresh_press_seq_after(20, 0, t0 + Duration::from_millis(5)), None); + } + + #[test] + fn post_yank_recovery_reclaims_focus_when_click_lands_after_precheck() { + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + + // Path A: click lands after precheck, right before/during yank_to_host when page was not GPUI-focused. + let mut handoff = FocusHandoff::new(); + let hb_seq = next_focus_seq(); + let post_precheck_press = next_focus_seq(); + let post_precheck_got = next_focus_seq(); + + handoff.request_blur(Some(1), hb_seq); + assert_eq!(handoff.pending_press_cutoff_for_active(Some(1), false), Some(hb_seq)); + let plan = handoff.take_render_plan(Some(1), false, true); + let cutoff = plan.recovery_cutoff_for_active(Some(1), handoff.recovery_floor_seq()).unwrap(); + assert_eq!(cutoff, hb_seq); + + tracker.record_with_seq(10, t0, post_precheck_press); + for y in &plan.yank_to_host { + if y.tab == 1 { + tracker.clear_target_up_to(10, y.cutoff_seq); + } + } + let recovered_seq = tracker.fresh_press_seq_after(10, cutoff, t0 + Duration::from_millis(5)).unwrap(); + assert_eq!(recovered_seq, post_precheck_press); + assert!(handoff.recover_click_after_yank(1, recovered_seq)); + + // Subsequent got_focus from give_keyboard(true) must NOT trigger unprompted yank, + // and unsettled page focus receives FocusPage on the next render. + assert!(!handoff.on_pump_tab(1, TabFocusEvents { got_focus_seq: post_precheck_got, ..Default::default() }, true)); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + + // Path B: queued BlurPage in on_blur rejects stale press <= cutoff, accepts newer click, + // lifts handed_back for subsequent keystrokes, and stays gated by newer deactivation. + let mut blur_handoff = FocusHandoff::new(); + let stale_press = next_focus_seq(); + let hb2 = next_focus_seq(); + let late_press = next_focus_seq(); + let key_after = next_focus_seq(); + + blur_handoff.request_blur(Some(1), hb2); + let blur_plan = blur_handoff.take_render_plan(Some(1), true, true); + let blur_cutoff = blur_plan.blur_cutoff_seq; + assert_eq!(blur_cutoff, hb2); + + tracker.record_with_seq(10, t0, stale_press); + assert_eq!(tracker.fresh_press_seq_after(10, blur_cutoff, t0 + Duration::from_millis(5)), None); + assert!(!blur_handoff.recover_click_after_yank(1, stale_press)); + + tracker.record_with_seq(10, t0, late_press); + assert_eq!(tracker.fresh_press_seq_after(10, blur_cutoff, t0 + Duration::from_millis(5)), Some(late_press)); + assert!(blur_handoff.recover_click_after_yank(1, late_press)); + assert!(blur_handoff.on_pump_tab(1, TabFocusEvents { key_seq: key_after, ..Default::default() }, true)); + + let deact = next_focus_seq(); + blur_handoff.on_window_deactivate(deact); + assert!(!blur_handoff.recover_click_after_yank(1, late_press)); + } + + #[test] + fn background_only_yank_recovers_active_tab_click_without_inflating_cutoff() { + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + + // 1. Pre-check with background-only stray got_focus promotes active tab click whose press_seq + // is strictly above click_cutoff_seq even when below background tab's higher got_focus_seq. + let mut pre = FocusHandoff::new(); + let stale_press = next_focus_seq(); + let cutoff = next_focus_seq(); + let active_press = next_focus_seq(); + let bg_got = next_focus_seq(); + + pre.on_host_blur(cutoff); + pre.on_pump_tab(2, TabFocusEvents { got_focus_seq: bg_got, ..Default::default() }, false); + assert_eq!(pre.pending_press_cutoff_for_active(Some(1), true), None); + assert_eq!(pre.pending_press_cutoff_for_active(Some(1), false), Some(cutoff)); + + tracker.record_with_seq(10, t0, stale_press); + assert_eq!(tracker.fresh_press_seq_after(10, cutoff, t0 + Duration::from_millis(1)), None); + assert!(!pre.on_pending_click(1, stale_press)); + + tracker.record_with_seq(10, t0 + Duration::from_millis(2), active_press); + let pre_seq = tracker.fresh_press_seq_after(10, cutoff, t0 + Duration::from_millis(3)).unwrap(); + assert_eq!(pre_seq, active_press); + assert!(pre.on_pending_click(1, pre_seq)); + assert_eq!( + pre.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + release_cef_only: vec![TabYank { tab: 2, cutoff_seq: bg_got }], + yank_to_host: vec![], + restore_active: Some(1), + blur_cutoff_seq: 0, + } + ); + + // 2. Post-yank recovery when click lands after pre-check and a background-only yank executes: + // recovery_cutoff_for_active returns the host floor cutoff (never bg_got), rejecting stale presses + // <= floor and reclaiming active tab 1 clicks in `(floor, bg_got)`. + let mut post = FocusHandoff::new(); + let stale2 = next_focus_seq(); + let cutoff2 = next_focus_seq(); + let late_active_press = next_focus_seq(); + let bg_got2 = next_focus_seq(); + + post.on_host_blur(cutoff2); + post.on_pump_tab(2, TabFocusEvents { got_focus_seq: bg_got2, ..Default::default() }, false); + let floor = post.recovery_floor_seq(); + assert_eq!(floor, cutoff2); + let plan = post.take_render_plan(Some(1), false, true); + assert_eq!( + plan, + RenderFocusPlan { + gpui: GpuiFocusChange::None, + release_cef_only: vec![], + yank_to_host: vec![TabYank { tab: 2, cutoff_seq: bg_got2 }], + restore_active: None, + blur_cutoff_seq: 0, + } + ); + let recovery_cutoff = plan.recovery_cutoff_for_active(Some(1), floor).unwrap(); + assert_eq!(recovery_cutoff, cutoff2); + + tracker.record_with_seq(10, t0, stale2); + assert_eq!(tracker.fresh_press_seq_after(10, recovery_cutoff, t0 + Duration::from_millis(4)), None); + assert!(!post.recover_click_after_yank(1, stale2)); + + tracker.record_with_seq(10, t0 + Duration::from_millis(5), late_active_press); + let got_late = tracker.fresh_press_seq_after(10, recovery_cutoff, t0 + Duration::from_millis(6)).unwrap(); + assert_eq!(got_late, late_active_press); + assert!(post.recover_click_after_yank(1, got_late)); + assert_eq!( + post.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + + // 3. Even after a background-only yank has already been consumed (`intent == None`, `unprompted_got` empty), + // a subsequent active-tab click while `!page_focused` (even producing NO CEF focus callback) + // is picked up by pending_press_cutoff_for_active(Some(1), false) on the next woken render. + let mut after_bg = FocusHandoff::new(); + let floor3 = next_focus_seq(); + let bg_got3 = next_focus_seq(); + let silent_click = next_focus_seq(); + + after_bg.on_host_blur(floor3); + after_bg.on_pump_tab(2, TabFocusEvents { got_focus_seq: bg_got3, ..Default::default() }, false); + let _consumed_bg = after_bg.take_render_plan(Some(1), false, true); + + assert_eq!(after_bg.pending_press_cutoff_for_active(Some(1), false), Some(floor3)); + tracker.record_with_seq(10, t0 + Duration::from_millis(8), silent_click); + let silent_seq = tracker.fresh_press_seq_after(10, floor3, t0 + Duration::from_millis(9)).unwrap(); + assert_eq!(silent_seq, silent_click); + assert!(after_bg.on_pending_click(1, silent_seq)); + assert_eq!( + after_bg.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + } + + #[test] + fn win32_mid_activate_got_focus_before_return_recovers_on_confirm_and_rejects_eaten() { + const WM_LBUTTONDOWN: u32 = 0x0201; + let t0 = Instant::now(); + + // A) Mid-dispatch on_got_focus(101) arrives before WM_MOUSEACTIVATE returns, so GPUI yanks + // with cutoff 101 (`clear_target_up_to(10, 101)` leaves unconfirmed 100 intact); confirming on + // DeliverDown assigns 102 (> 101) so render recovery lifts the yank and button-down pairs 102. + let mut tracker = PressTracker::new(); + let mut handoff = FocusHandoff::new(); + assert_eq!(tracker.record_activate_with_seq(10, WM_LBUTTONDOWN, t0, 100), 100); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0), None); + handoff.on_pump_tab(1, TabFocusEvents { got_focus_seq: 101, ..Default::default() }, false); + let plan_a = handoff.take_render_plan(Some(1), false, true); + assert_eq!(plan_a.yank_to_host, vec![TabYank { tab: 1, cutoff_seq: 101 }]); + tracker.clear_target_up_to(10, 101); + assert_eq!(tracker.fresh_press_seq_after(10, 101, t0 + Duration::from_millis(1)), None); + assert!(tracker.complete_activate_with_seq( + 10, + WM_LBUTTONDOWN, + MouseActivateOutcome::DeliverDown, + t0 + Duration::from_millis(1), + 102, + )); + let rec_a = tracker.fresh_press_seq_after(10, 101, t0 + Duration::from_millis(1)); + assert_eq!(rec_a, Some(102)); + assert!(handoff.recover_click_after_yank(1, 102)); + assert_eq!( + handoff.take_render_plan(Some(1), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + assert_eq!(tracker.record_button_down_with_seq(10, WM_LBUTTONDOWN, t0 + Duration::from_millis(2), 103), 102); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t0 + Duration::from_millis(2)), Some(102)); + + // B) Mid-dispatch on_got_focus(201) -> DeliverDown confirms 202 before render -> zero yank. + let mut h_b = FocusHandoff::new(); + assert_eq!(tracker.record_activate_with_seq(20, WM_LBUTTONDOWN, t0, 200), 200); + assert_eq!(tracker.consume(20, FocusCallback::GotFocus, t0), None); + h_b.on_pump_tab(2, TabFocusEvents { got_focus_seq: 201, ..Default::default() }, false); + assert!(tracker.complete_activate_with_seq( + 20, + WM_LBUTTONDOWN, + MouseActivateOutcome::DeliverDown, + t0 + Duration::from_millis(1), + 202, + )); + let cutoff_b = h_b.pending_press_cutoff_for_active(Some(2), false).unwrap(); + assert_eq!(cutoff_b, 201); + let seq_b = tracker.fresh_press_seq_after(20, cutoff_b, t0 + Duration::from_millis(1)); + assert_eq!(seq_b, Some(202)); + assert!(h_b.on_pending_click(2, seq_b.unwrap())); + assert_eq!( + h_b.take_render_plan(Some(2), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + + // C) Mid-dispatch on_got_focus(301) -> mid-dispatch record_button_down(302) confirms 302 -> recovers; + // later NoActivateAndEat(305) preserves 302. + let mut h_c = FocusHandoff::new(); + assert_eq!(tracker.record_activate_with_seq(30, WM_LBUTTONDOWN, t0, 300), 300); + h_c.on_pump_tab(3, TabFocusEvents { got_focus_seq: 301, ..Default::default() }, false); + assert_eq!(tracker.record_button_down_with_seq(30, WM_LBUTTONDOWN, t0 + Duration::from_millis(1), 302), 302); + let cutoff_c = h_c.pending_press_cutoff_for_active(Some(3), false).unwrap(); + assert_eq!(cutoff_c, 301); + assert_eq!(tracker.fresh_press_seq_after(30, cutoff_c, t0 + Duration::from_millis(1)), Some(302)); + assert!(h_c.on_pending_click(3, 302)); + assert_eq!( + h_c.take_render_plan(Some(3), false, true), + RenderFocusPlan { + gpui: GpuiFocusChange::FocusPage, + ..Default::default() + } + ); + assert!(tracker.complete_activate_with_seq( + 30, + WM_LBUTTONDOWN, + MouseActivateOutcome::NoActivateAndEat, + t0 + Duration::from_millis(2), + 305, + )); + assert_eq!(tracker.fresh_press_seq_after(30, 301, t0 + Duration::from_millis(2)), Some(302)); + assert_eq!(tracker.consume(30, FocusCallback::GotFocus, t0 + Duration::from_millis(2)), Some(302)); + + // D) Mid-dispatch on_got_focus(401) -> NoActivateAndEat(402) drops unconfirmed click -> yank proceeds. + let mut h_d = FocusHandoff::new(); + assert_eq!(tracker.record_activate_with_seq(40, WM_LBUTTONDOWN, t0, 400), 400); + h_d.on_pump_tab(4, TabFocusEvents { got_focus_seq: 401, ..Default::default() }, false); + assert!(!tracker.complete_activate_with_seq( + 40, + WM_LBUTTONDOWN, + MouseActivateOutcome::NoActivateAndEat, + t0 + Duration::from_millis(1), + 402, + )); + let cutoff_d = h_d.pending_press_cutoff_for_active(Some(4), false).unwrap(); + assert_eq!(cutoff_d, 401); + assert_eq!(tracker.fresh_press_seq_after(40, cutoff_d, t0 + Duration::from_millis(1)), None); + let plan_d = h_d.take_render_plan(Some(4), false, true); + assert_eq!(plan_d.yank_to_host, vec![TabYank { tab: 4, cutoff_seq: 401 }]); + let rec_cutoff_d = plan_d + .recovery_cutoff_for_active(Some(4), h_d.recovery_floor_seq()) + .unwrap(); + assert_eq!(tracker.fresh_press_seq_after(40, rec_cutoff_d, t0 + Duration::from_millis(1)), None); + + // E) Explicit clear_target(50) while in-flight -> DeliverDown returns false without resurrecting, + // while paired WH_MOUSE down still returns original seq 500 to avoid re-arming. + assert_eq!(tracker.record_activate_with_seq(50, WM_LBUTTONDOWN, t0, 500), 500); + tracker.clear_target(50); + assert!(!tracker.complete_activate_with_seq( + 50, + WM_LBUTTONDOWN, + MouseActivateOutcome::DeliverDown, + t0 + Duration::from_millis(1), + 505, + )); + assert_eq!(tracker.fresh_press_seq_after(50, 0, t0 + Duration::from_millis(1)), None); + assert_eq!(tracker.consume(50, FocusCallback::GotFocus, t0 + Duration::from_millis(1)), None); + assert_eq!(tracker.record_button_down_with_seq(50, WM_LBUTTONDOWN, t0 + Duration::from_millis(2), 506), 500); + assert_eq!(tracker.fresh_press_seq_after(50, 0, t0 + Duration::from_millis(2)), None); + assert_eq!(tracker.consume(50, FocusCallback::GotFocus, t0 + Duration::from_millis(2)), None); + + // F) Long reentrant WM_MOUSEACTIVATE (> PRESS_SPAN) is never age-pruned by upsert_press or consume + // while unconfirmed; confirmation resets its clock at confirmation time. + let confirm_at = t0 + PRESS_SPAN + Duration::from_millis(250); + assert_eq!(tracker.record_activate_with_seq(60, WM_LBUTTONDOWN, t0, 600), 600); + assert_eq!(tracker.record_button_down_with_seq(61, WM_LBUTTONDOWN, confirm_at, 601), 601); + assert_eq!(tracker.consume(60, FocusCallback::GotFocus, confirm_at + Duration::from_millis(1)), None); + tracker.clear_target_up_to(60, 602); + assert!(tracker.complete_activate_with_seq( + 60, + WM_LBUTTONDOWN, + MouseActivateOutcome::DeliverDown, + confirm_at + Duration::from_millis(2), + 603, + )); + assert_eq!(tracker.fresh_press_seq_after(60, 602, confirm_at + Duration::from_millis(3)), Some(603)); + assert_eq!(tracker.consume(60, FocusCallback::GotFocus, confirm_at + Duration::from_millis(3)), Some(603)); + assert_eq!(tracker.record_button_down_with_seq(60, WM_LBUTTONDOWN, confirm_at + Duration::from_millis(4), 604), 603); + let expired = confirm_at + Duration::from_millis(2) + PRESS_SPAN + Duration::from_millis(1); + assert_eq!(tracker.fresh_press_seq_after(60, 602, expired), None); + assert_eq!(tracker.consume(60, FocusCallback::GotFocus, expired), None); + + // Also confirm an unconsumed long activation survives past confirm_at and then expires strictly after confirm_at + PRESS_SPAN. + assert_eq!(tracker.record_activate_with_seq(62, WM_LBUTTONDOWN, t0, 610), 610); + assert!(tracker.complete_activate_with_seq( + 62, + WM_LBUTTONDOWN, + MouseActivateOutcome::ActivateAndEat, + confirm_at + Duration::from_millis(2), + 611, + )); + assert_eq!(tracker.consume(62, FocusCallback::GotFocus, expired), None); + } + + #[test] + fn x11_barrier_fences_suppress_pre_barrier_presses_across_timeout_and_rendezvous() { + let mut fences = X11BarrierFences::new(); + + // 1) Normal timeout: caller installs active fence for watched target 10; target 20 stays unfenced; + // watcher retires barrier 1 with no leak and without calling on_early. + assert_eq!(fences.finish_timeout_caller(1, &[10]), None); + assert!(fences.is_fenced(10)); + assert!(!fences.is_fenced(20)); + assert!(!fences.is_fenced(0)); + let mut early_ran = false; + fences.finish_timeout_watcher(1, || { + early_ran = true; + 999 + }); + assert!(!early_ran); + assert!(!fences.is_fenced(10)); + assert!(fences.active.is_empty()); + assert!(fences.completed_early.is_empty()); + + // 2) End-to-end watcher-wins-timeout race: watcher clears pre-barrier press 49, allocates cutoff 50, + // and records post-barrier press 51 before caller runs; caller consumes Some(50) and preserves 51. + let mut tracker = PressTracker::new(); + let t0 = Instant::now(); + let t1 = t0 + Duration::from_millis(5); + tracker.record_with_seq(10, t0, 49); + fences.finish_timeout_watcher(2, || { + tracker.clear_target(10); + 50 + }); + assert_eq!(tracker.fresh_press_seq_after(10, 0, t0), None); + tracker.record_with_seq(10, t1, 51); + let caller_cutoff = fences.finish_timeout_caller(2, &[10]); + assert_eq!(caller_cutoff, Some(50)); + assert_eq!(tracker.fresh_press_seq_after(10, 50, t1), Some(51)); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t1), Some(51)); + assert!(!fences.is_fenced(10)); + assert!(fences.active.is_empty()); + assert!(fences.completed_early.is_empty()); + + // 3) Concurrent out-of-order timed-out barriers keep target 10 fenced until both retire. + assert_eq!(fences.finish_timeout_caller(4, &[10]), None); + assert_eq!(fences.finish_timeout_caller(5, &[10]), None); + assert!(fences.is_fenced(10)); + fences.finish_timeout_watcher(5, || panic!("active barrier should not invoke on_early")); + assert!(fences.is_fenced(10)); + fences.finish_timeout_watcher(4, || panic!("active barrier should not invoke on_early")); + assert!(!fences.is_fenced(10)); + assert!(fences.active.is_empty()); + assert!(fences.completed_early.is_empty()); + + // 4) drop_target unfences destroyed target while preserving barrier id slot so watcher retires cleanly. + assert_eq!(fences.finish_timeout_caller(6, &[10, 11]), None); + fences.drop_target(10); + assert!(!fences.is_fenced(10)); + assert!(fences.is_fenced(11)); + fences.finish_timeout_watcher(6, || panic!("dropped target barrier slot should still retire active fence")); + assert!(!fences.is_fenced(11)); + assert!(fences.active.is_empty()); + assert!(fences.completed_early.is_empty()); + + // 5) mark_watcher_dead clears active fences while keeping completed_early cutoffs for live callers: + // even if the watcher exits between recording post-barrier press 89 and caller collecting cutoff 88, + // finish_timeout_caller(8) still returns Some(88) so post-barrier click 89 survives. + assert_eq!(fences.finish_timeout_caller(7, &[10]), None); + let t2 = t1 + Duration::from_millis(5); + tracker.record_with_seq(10, t2, 87); + fences.finish_timeout_watcher(8, || { + tracker.clear_target(10); + 88 + }); + let t3 = t2 + Duration::from_millis(5); + tracker.record_with_seq(10, t3, 89); + fences.mark_watcher_dead(); + assert!(fences.watcher_dead); + assert!(!fences.is_fenced(10)); + assert!(fences.active.is_empty()); + assert_eq!(fences.finish_timeout_caller(8, &[10]), Some(88)); + assert_eq!(tracker.fresh_press_seq_after(10, 88, t3), Some(89)); + assert_eq!(tracker.consume(10, FocusCallback::GotFocus, t3), Some(89)); + assert!(fences.completed_early.is_empty()); + assert_eq!(fences.finish_timeout_caller(9, &[10]), None); + fences.finish_timeout_watcher(10, || panic!("dead watcher must not invoke on_early")); + assert!(!fences.is_fenced(10)); + assert!(fences.active.is_empty()); + assert!(fences.completed_early.is_empty()); + } +} diff --git a/native/gpui/src/viewers/cef_native.rs b/native/gpui/src/viewers/cef_native.rs index 6cee11275b..7ae9b734ee 100644 --- a/native/gpui/src/viewers/cef_native.rs +++ b/native/gpui/src/viewers/cef_native.rs @@ -32,6 +32,29 @@ impl Host { #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub struct R { pub x: i32, pub y: i32, pub w: i32, pub h: i32 } +static PRESS_WAKERS: std::sync::LazyLock>>> = + std::sync::LazyLock::new(Default::default); + +/// Registers `wake` so recorded native presses on `target` wake `CefPage` even when CEF emits no focus callback. +pub fn register_press_wake(target: u64, wake: async_channel::Sender<()>) { + if target != 0 { + PRESS_WAKERS.lock().unwrap().insert(target, wake); + } +} + +fn unregister_press_wake(target: u64) { + if target != 0 { + PRESS_WAKERS.lock().unwrap().remove(&target); + } +} + +fn notify_press_wake(target: u64) { + let tx = PRESS_WAKERS.lock().unwrap().get(&target).cloned(); + if let Some(tx) = tx { + let _ = tx.try_send(()); + } +} + #[cfg(target_os = "linux")] mod imp { use super::{Host, R}; @@ -64,49 +87,248 @@ mod imp { /// (each with the client's top-level it is in) fn containers() -> &'static Mutex> { static C: OnceLock>> = OnceLock::new(); C.get_or_init(Default::default) } fn top_of(frame: u64) -> Option { containers().lock().unwrap().get(&(frame as u32)).map(|t| *t as u64) } + use crate::viewers::cef_focus::{next_focus_seq, FocusCallback, PressProbe, PressTracker, X11BarrierFences}; use crate::viewers::native_child as nc; + struct X11PressState { + tracker: PressTracker, + fences: X11BarrierFences, + } + impl X11PressState { + const fn new() -> Self { + Self { + tracker: PressTracker::new(), + fences: X11BarrierFences::new(), + } + } + } /// The latest press delivered to one of our containers. Record it before replaying /// the original X event, so even a released click can explain one CEF focus callback. - static PRESS: Mutex> = Mutex::new(None); + static PRESS: Mutex = Mutex::new(X11PressState::new()); static PRESS_WINDOWS: std::sync::LazyLock>> = std::sync::LazyLock::new(Default::default); - const PRESS_SPAN: std::time::Duration = std::time::Duration::from_millis(400); + struct PressWatcher { + conn: std::sync::Weak, + barrier_win: u32, + barrier_atom: u32, + } + static WATCHER: OnceLock> = OnceLock::new(); + + struct BarrierRequest { + id: u64, + targets: Vec, + cutoff: Option, + timed_out: bool, + } + struct BarrierState { + next_id: u64, + dead: bool, + in_flight: Vec, + } + enum FenceResult { + Fenced(u64), + TimedOut(u64), + Unfenced, + } + static BARRIERS: std::sync::LazyLock<(Mutex, std::sync::Condvar)> = + std::sync::LazyLock::new(|| { + ( + Mutex::new(BarrierState { + next_id: 0, + dead: false, + in_flight: Vec::new(), + }), + std::sync::Condvar::new(), + ) + }); + + fn complete_barrier(id: u64) { + enum Claimed { + Clear(Vec), + TimedOut(Vec), + } + let claimed = { + let mut st = BARRIERS.0.lock().unwrap(); + let Some(pos) = st.in_flight.iter().position(|r| r.id == id) else { + return; + }; + if st.in_flight[pos].timed_out { + Claimed::TimedOut(st.in_flight.swap_remove(pos).targets) + } else { + Claimed::Clear(std::mem::take(&mut st.in_flight[pos].targets)) + } + }; + let targets = match claimed { + Claimed::TimedOut(targets) => { + let mut press = PRESS.lock().unwrap(); + let X11PressState { tracker, fences } = &mut *press; + fences.finish_timeout_watcher(id, || { + for f in targets { + tracker.clear_target(f); + } + next_focus_seq() + }); + return; + } + Claimed::Clear(targets) => targets, + }; + // Lock ordering: BARRIERS.0 and PRESS are never held together. Because this runs on the + // sole X11 press-watcher thread, clearing watched targets and allocating `cutoff` right + // before looping back to `wait_for_event` guarantees pre-barrier clicks cannot overtake + // `cutoff` while genuinely post-barrier clicks stay intact with `seq > cutoff`. + let cutoff = { + let mut press = PRESS.lock().unwrap(); + for f in targets { + press.tracker.clear_target(f); + } + next_focus_seq() + }; + let timed_out = { + let mut st = BARRIERS.0.lock().unwrap(); + let Some(pos) = st.in_flight.iter().position(|r| r.id == id) else { + return; + }; + if st.in_flight[pos].timed_out { + st.in_flight.swap_remove(pos); + true + } else { + st.in_flight[pos].cutoff = Some(cutoff); + BARRIERS.1.notify_all(); + false + } + }; + if timed_out { + PRESS.lock().unwrap().fences.finish_timeout_watcher(id, || cutoff); + } + } + + fn mark_barriers_dead() { + let mut st = BARRIERS.0.lock().unwrap(); + st.dead = true; + st.in_flight.retain(|r| r.cutoff.is_some()); + BARRIERS.1.notify_all(); + } + + fn fence_x11_watcher(watched: &[u64]) -> FenceResult { + let Some((w, conn)) = WATCHER + .get() + .and_then(Option::as_ref) + .and_then(|w| w.conn.upgrade().map(|conn| (w, conn))) + else { + return FenceResult::Unfenced; + }; + let id = { + let mut st = BARRIERS.0.lock().unwrap(); + if st.dead { + return FenceResult::Unfenced; + } + st.next_id = st.next_id.wrapping_add(1).max(1); + let id = st.next_id; + st.in_flight.push(BarrierRequest { + id, + targets: watched.to_vec(), + cutoff: None, + timed_out: false, + }); + id + }; + let ev = ClientMessageEvent::new( + 32, + w.barrier_win, + w.barrier_atom, + [id as u32, (id >> 32) as u32, 0, 0, 0], + ); + if conn + .send_event(false, w.barrier_win, EventMask::NO_EVENT, ev) + .ok() + .and_then(|cookie| cookie.check().ok()) + .is_none() + { + let mut st = BARRIERS.0.lock().unwrap(); + st.in_flight.retain(|r| r.id != id); + return FenceResult::Unfenced; + } + let guard = BARRIERS.0.lock().unwrap(); + let mut guard = BARRIERS + .1 + .wait_timeout_while(guard, std::time::Duration::from_millis(100), |st| { + !st.dead && st.in_flight.iter().any(|r| r.id == id && r.cutoff.is_none()) + }) + .unwrap() + .0; + let Some(pos) = guard.in_flight.iter().position(|r| r.id == id) else { + return FenceResult::Unfenced; + }; + if let Some(cutoff) = guard.in_flight[pos].cutoff { + guard.in_flight.swap_remove(pos); + FenceResult::Fenced(cutoff) + } else if guard.dead { + guard.in_flight.swap_remove(pos); + FenceResult::Unfenced + } else { + guard.in_flight[pos].timed_out = true; + FenceResult::TimedOut(id) + } + } fn watch_presses(frame: u32) { use std::sync::{Arc, Weak}; - static WATCHER: OnceLock>> = OnceLock::new(); let watcher = WATCHER.get_or_init(|| { - let start = || -> Result, Box> { - let connection = Arc::new(RustConnection::connect(None)?.0); + let start = || -> Result> { + let (connection, screen_num) = RustConnection::connect(None)?; + let connection = Arc::new(connection); + let root = connection.setup().roots.get(screen_num).ok_or("no_screen")?.root; + let barrier_win = connection.generate_id()?; + connection.create_window(0, barrier_win, root, 0, 0, 1, 1, 0, WindowClass::INPUT_ONLY, x11rb::COPY_FROM_PARENT, &CreateWindowAux::new())?.check()?; + let barrier_atom = connection.intern_atom(false, b"_BACKPLANE_CEF_PRESS_BARRIER")?.reply()?.atom; let thread = connection.clone(); std::thread::Builder::new().name("cef-presses".into()).spawn(move || { let error = loop { match thread.wait_for_event() { Ok(x11rb::protocol::Event::ButtonPress(press)) => { - *PRESS.lock().unwrap() = Some((std::time::Instant::now(), press.event)); + let target = press.event as u64; + let seq = { + let mut press = PRESS.lock().unwrap(); + if press.fences.is_fenced(target) { + 0 + } else { + press.tracker.record(target, std::time::Instant::now()) + } + }; + if seq != 0 { + super::notify_press_wake(target); + } // Replay releases the synchronous grab and delivers the same press to Chromium. if let Err(error) = thread.allow_events(Allow::REPLAY_POINTER, press.time).and_then(|_| thread.flush()) { break error; } } + Ok(x11rb::protocol::Event::ClientMessage(msg)) + if msg.window == barrier_win && msg.type_ == barrier_atom && msg.format == 32 => + { + let d = msg.data.as_data32(); + let id = (d[0] as u64) | ((d[1] as u64) << 32); + complete_barrier(id); + } Ok(_) => {} Err(error) => break error, } }; PRESS_WINDOWS.lock().unwrap().clear(); + PRESS.lock().unwrap().fences.mark_watcher_dead(); + mark_barriers_dead(); eprintln!("GPUI_LOG {}", serde_json::json!({"cef_press_watcher": error.to_string()})); })?; - Ok(Arc::downgrade(&connection)) + Ok(PressWatcher { conn: Arc::downgrade(&connection), barrier_win, barrier_atom }) }; match start() { - Ok(connection) => Some(connection), + Ok(watcher) => Some(watcher), Err(error) => { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_press_watcher": error.to_string()})); None } } }); - let Some(connection) = watcher.as_ref().and_then(Weak::upgrade) else { return }; + let Some(connection) = watcher.as_ref().and_then(|w| Weak::upgrade(&w.conn)) else { return }; // Only buttons that count as a click are grabbed. The server delivers wheel buttons // normally, and delivers touch taps that emulate a button through the same grab. // A container grab observes the actual delivered click, including entry from native Wayland. @@ -135,27 +357,156 @@ mod imp { None } - /// A recent press is consumed once for the page that received it. - pub fn pressed_in(frame: u64) -> Option { - let mut record = PRESS.lock().unwrap(); - if record.is_some_and(|(at, window)| window as u64 == frame && at.elapsed() <= PRESS_SPAN) { - *record = None; - return Some(true); + /// Linux watches the container window created in `container()`, so per-native-handle setup is a no-op here. + pub fn watch_native(_w: u64) {} + pub fn unwatch_native(_w: u64) {} + + /// A recent press is consumed once per callback kind for the page that received it. + pub fn pressed_in(frame: u64, cb: FocusCallback) -> PressProbe { + let mut press = PRESS.lock().unwrap(); + if let Some(seq) = press.tracker.consume(frame, cb, std::time::Instant::now()) { + return PressProbe::Pressed(seq); + } + let blocked = press.tracker.is_unwatched_blocked(frame); + drop(press); + if blocked || PRESS_WINDOWS.lock().unwrap().contains(&(frame as u32)) { + PressProbe::NotPressed + } else { + PressProbe::Unwatched } - PRESS_WINDOWS.lock().unwrap().contains(&(frame as u32)).then_some(false) + } + + fn any_button_held() -> bool { + let Some(g) = conn() else { return false }; + let c = g.as_ref().unwrap(); + let Some(root) = c.setup().roots.get(SCREEN.load(std::sync::atomic::Ordering::Relaxed)).map(|s| s.root) else { return false }; + c.query_pointer(root).ok().and_then(|r| r.reply().ok()).is_some_and(|p| + u16::from(p.mask) & u16::from(KeyButMask::BUTTON1 | KeyButMask::BUTTON2 | KeyButMask::BUTTON3) != 0) + } + + fn apply_unwatched_clear(press: &mut PressTracker, unwatched: &[u64], held: bool) -> Option { + let mut any_blocked = false; + for &f in unwatched { + press.clear_target(f); + if held && press.block_unwatched_until_release(f).is_some() { + any_blocked = true; + } + } + any_blocked.then(|| press.unwatched_block_gen()) + } + + fn spawn_unwatched_release_poller(spawn_gen: Option) { + let Some(mut block_gen) = spawn_gen else { return }; + let _ = std::thread::Builder::new().name("cef-unwatched-rel".into()).spawn(move || { + loop { + std::thread::sleep(std::time::Duration::from_millis(5)); + if !any_button_held() { + let mut press = PRESS.lock().unwrap(); + if press.tracker.clear_unwatched_release_block_if(block_gen) { + break; + } + block_gen = press.tracker.unwatched_block_gen(); + } + } + }); + } + + /// Clears recorded presses on all `frames` and allocates a cutoff sequence while still holding `PRESS`, + /// synchronously fencing queued X11 `ButtonPress` events behind a barrier `ClientMessage` on the watcher + /// connection (suppressing stalled pre-barrier clicks via `X11BarrierFences` across any timeout until + /// the watcher processes the barrier, and gating unwatched targets behind button release) so pre-barrier + /// queued presses cannot overtake the cutoff while genuinely post-cutoff clicks stay intact. + pub fn clear_presses(frames: &[u64]) -> u64 { + let mut unique: Vec = Vec::with_capacity(frames.len()); + for &f in frames { + if f != 0 && !unique.contains(&f) { + unique.push(f); + } + } + if unique.is_empty() { + return next_focus_seq(); + } + let (watched, unwatched): (Vec, Vec) = { + let pw = PRESS_WINDOWS.lock().unwrap(); + unique.into_iter().partition(|f| pw.contains(&(*f as u32))) + }; + let held = !unwatched.is_empty() && any_button_held(); + let fence_result = if watched.is_empty() { + FenceResult::Unfenced + } else { + fence_x11_watcher(&watched) + }; + if let FenceResult::Fenced(cutoff) = fence_result { + if !unwatched.is_empty() { + let spawn_gen = { + let mut press = PRESS.lock().unwrap(); + apply_unwatched_clear(&mut press.tracker, &unwatched, held) + }; + spawn_unwatched_release_poller(spawn_gen); + } + return cutoff; + } + let timed_out_id = match fence_result { + FenceResult::TimedOut(id) => Some(id), + _ => None, + }; + let (cutoff, spawn_gen) = { + let mut press = PRESS.lock().unwrap(); + if let Some(watcher_cutoff) = + timed_out_id.and_then(|id| press.fences.finish_timeout_caller(id, &watched)) + { + let spawn_gen = apply_unwatched_clear(&mut press.tracker, &unwatched, held); + (watcher_cutoff, spawn_gen) + } else { + for &f in &watched { + press.tracker.clear_target(f); + } + let spawn_gen = apply_unwatched_clear(&mut press.tracker, &unwatched, held); + (next_focus_seq(), spawn_gen) + } + }; + spawn_unwatched_release_poller(spawn_gen); + cutoff + } + + /// Clears any recorded press on `frame` when keyboard focus returns to the host. + pub fn clear_press(frame: u64) { + if frame != 0 { + let _ = clear_presses(&[frame]); + } + } + + pub fn fresh_press_seq_after(frame: u64, after_seq: u64) -> Option { + PRESS.lock().unwrap().tracker.fresh_press_seq_after(frame, after_seq, std::time::Instant::now()) + } + + pub fn clear_press_up_to(frame: u64, max_seq: u64) { + PRESS.lock().unwrap().tracker.clear_target_up_to(frame, max_seq); } /// Fallback for SYSTEM focus if the page button grab could not be installed. Require the held /// button and the pointer inside the page that reported focus. pub fn button_down(frame: u64) -> bool { + if frame == 0 || PRESS.lock().unwrap().tracker.is_unwatched_blocked(frame) || !any_button_held() { return false } let Some(g) = conn() else { return false }; let c = g.as_ref().unwrap(); let Some(root) = c.setup().roots.get(SCREEN.load(std::sync::atomic::Ordering::Relaxed)).map(|s| s.root) else { return false }; - let held = c.query_pointer(root).ok().and_then(|r| r.reply().ok()).is_some_and(|p| - u16::from(p.mask) & u16::from(KeyButMask::BUTTON1 | KeyButMask::BUTTON2 | KeyButMask::BUTTON3) != 0); - held && pointer_target(c, root, |w| w as u64 == frame).is_some() + let inside = pointer_target(c, root, |w| w as u64 == frame).is_some(); + drop(g); + inside && !PRESS.lock().unwrap().tracker.is_unwatched_blocked(frame) } /// The container gone (its browser closed). - pub fn destroy(frame: u64) { PRESS_WINDOWS.lock().unwrap().remove(&(frame as u32)); if let Some(t) = containers().lock().unwrap().remove(&(frame as u32)) { nc::release_child(t as u64, frame); } let Some(g) = conn() else { return }; let c = g.as_ref().unwrap(); let _ = c.destroy_window(frame as u32); let _ = c.flush(); } + pub fn destroy(frame: u64) { + PRESS_WINDOWS.lock().unwrap().remove(&(frame as u32)); + { + let mut press = PRESS.lock().unwrap(); + press.tracker.drop_target(frame); + press.fences.drop_target(frame); + } + super::unregister_press_wake(frame); + if let Some(t) = containers().lock().unwrap().remove(&(frame as u32)) { nc::release_child(t as u64, frame); } + let Some(g) = conn() else { return }; let c = g.as_ref().unwrap(); + let _ = c.destroy_window(frame as u32); let _ = c.flush(); + } /// The container (and Chromium's window filling it) over the pane at `r`, mapped; or unmapped. pub fn place_in(frame: u64, w: u64, r: Option) { if let Some(t) = top_of(frame) { nc::note_pane_on_screen(t, frame, r.is_some()); } @@ -225,35 +576,306 @@ mod imp { #[cfg(windows)] mod imp { use super::{Host, R}; + use crate::viewers::cef_focus::{next_focus_seq, FocusCallback, MouseActivateOutcome, PressProbe, PressTracker}; + use std::collections::{HashMap, HashSet}; + use std::sync::{LazyLock, Mutex}; + use windows_sys::Win32::{ + Foundation::{HWND, LPARAM, LRESULT, POINT, RECT, WPARAM}, + Graphics::Gdi::{GetDC, GetPixel, ReleaseDC, ScreenToClient}, + UI::{ + Input::KeyboardAndMouse::{GetAsyncKeyState, GetFocus, SetFocus}, + WindowsAndMessaging::*, + }, + }; + + static PRESS: Mutex = Mutex::new(PressTracker::new()); + static PRESS_WINDOWS: LazyLock>> = LazyLock::new(Default::default); + static HOOKED_THREADS: LazyLock)>>> = + LazyLock::new(Default::default); + pub fn pixel(_w: u64, _x: i32, _y: i32) -> Option<[u8; 3]> { None } pub fn inner_size(_w: u64) -> Option<(i32, i32)> { None } pub fn container(_h: Host) -> Option { None } pub fn place_in(_frame: u64, _w: u64, _r: Option) {} pub fn destroy(_frame: u64) {} - /// X11 only (its press watcher): here on_set_focus asks `button_down`. - pub fn pressed_in(_frame: u64) -> Option { None } - #[cfg(windows)] pub fn close(w: u64) { unsafe { windows_sys::Win32::UI::WindowsAndMessaging::DestroyWindow(w as isize as _); } } - #[cfg(target_os = "macos")] pub fn close(w: u64) { unsafe { send0(v(w), c"removeFromSuperview"); } } + + fn any_button_held() -> bool { + unsafe { [0x01, 0x02, 0x04, 0x05, 0x06].iter().any(|k| GetAsyncKeyState(*k) as u16 & 0x8000 != 0) } + } + + fn is_button_down_msg(msg: u32) -> bool { + matches!( + msg, + WM_LBUTTONDOWN + | WM_LBUTTONDBLCLK + | WM_RBUTTONDOWN + | WM_RBUTTONDBLCLK + | WM_MBUTTONDOWN + | WM_MBUTTONDBLCLK + | WM_XBUTTONDOWN + | WM_XBUTTONDBLCLK + | WM_NCLBUTTONDOWN + | WM_NCRBUTTONDOWN + | WM_NCMBUTTONDOWN + | WM_NCXBUTTONDOWN + ) + } + + fn watched_root(target: HWND) -> Option { + if target.is_null() { + return None; + } + let watched: Vec = PRESS_WINDOWS.lock().unwrap().iter().copied().collect(); + watched.into_iter().find(|&w| { + w != 0 && { + let h = hwnd(w); + h == target || unsafe { IsChild(h, target) } != 0 + } + }) + } + + unsafe extern "system" fn mouse_hook(code: i32, wparam: WPARAM, lparam: LPARAM) -> LRESULT { + if code == HC_ACTION as i32 && lparam != 0 { + let msg = wparam as u32; + if is_button_down_msg(msg) { + let target = unsafe { (*(lparam as *const MOUSEHOOKSTRUCT)).hwnd }; + if let Some(w) = watched_root(target) { + let seq = PRESS.lock().unwrap().record_button_down(w, msg, std::time::Instant::now()); + if seq != 0 { + super::notify_press_wake(w); + } + } + } + } + unsafe { CallNextHookEx(std::ptr::null_mut(), code, wparam, lparam) } + } + + unsafe extern "system" fn callwnd_hook(code: i32, wparam: WPARAM, lparam: LPARAM) -> LRESULT { + if code == HC_ACTION as i32 && lparam != 0 { + let cwp = unsafe { &*(lparam as *const CWPSTRUCT) }; + if cwp.message == WM_MOUSEACTIVATE { + let mouse_msg = ((cwp.lParam as usize >> 16) & 0xffff) as u32; + if is_button_down_msg(mouse_msg) + && let Some(w) = watched_root(cwp.hwnd) + { + PRESS.lock().unwrap().record_activate(w, mouse_msg, std::time::Instant::now()); + } + } + } + unsafe { CallNextHookEx(std::ptr::null_mut(), code, wparam, lparam) } + } + + unsafe extern "system" fn callwndret_hook(code: i32, wparam: WPARAM, lparam: LPARAM) -> LRESULT { + if code == HC_ACTION as i32 && lparam != 0 { + let cwpr = unsafe { &*(lparam as *const CWPRETSTRUCT) }; + if cwpr.message == WM_MOUSEACTIVATE { + let mouse_msg = ((cwpr.lParam as usize >> 16) & 0xffff) as u32; + if is_button_down_msg(mouse_msg) + && let Some(w) = watched_root(cwpr.hwnd) + { + let outcome = match cwpr.lResult { + x if x == MA_NOACTIVATEANDEAT as LRESULT => MouseActivateOutcome::NoActivateAndEat, + x if x == MA_ACTIVATEANDEAT as LRESULT => MouseActivateOutcome::ActivateAndEat, + _ => MouseActivateOutcome::DeliverDown, + }; + let confirmed = PRESS.lock().unwrap().complete_activate(w, mouse_msg, outcome, std::time::Instant::now()); + if confirmed { + super::notify_press_wake(w); + } + } + } + } + unsafe { CallNextHookEx(std::ptr::null_mut(), code, wparam, lparam) } + } + + /// Hooks CEF's UI thread (`WH_MOUSE` + `WH_CALLWNDPROC` + `WH_CALLWNDPROCRET`) so clicks on any descendant HWND + /// (including `WM_MOUSEACTIVATE` before button-down delivery) are recorded before CEF focus callbacks run. + pub fn watch_native(w: u64) { + if w == 0 { return } + let tid = unsafe { GetWindowThreadProcessId(hwnd(w), std::ptr::null_mut()) }; + if tid == 0 { return } + let mut hooked = HOOKED_THREADS.lock().unwrap(); + let ok = if let Some((_, _, _, set)) = hooked.get_mut(&tid) { + set.insert(w); + true + } else { + let mh = unsafe { SetWindowsHookExW(WH_MOUSE, Some(mouse_hook), std::ptr::null_mut(), tid) }; + if mh.is_null() { + eprintln!("GPUI_LOG {}", serde_json::json!({"cef_press_watcher": "could not hook mouse events", "tid": tid})); + false + } else { + let ch = unsafe { SetWindowsHookExW(WH_CALLWNDPROC, Some(callwnd_hook), std::ptr::null_mut(), tid) }; + if ch.is_null() { + unsafe { UnhookWindowsHookEx(mh); } + eprintln!("GPUI_LOG {}", serde_json::json!({"cef_press_watcher": "could not hook activate events", "tid": tid})); + false + } else { + let rh = unsafe { SetWindowsHookExW(WH_CALLWNDPROCRET, Some(callwndret_hook), std::ptr::null_mut(), tid) }; + if rh.is_null() { + unsafe { + UnhookWindowsHookEx(ch); + UnhookWindowsHookEx(mh); + } + eprintln!("GPUI_LOG {}", serde_json::json!({"cef_press_watcher": "could not hook activate return events", "tid": tid})); + false + } else { + let mut set = HashSet::new(); + set.insert(w); + hooked.insert(tid, (mh as usize, ch as usize, rh as usize, set)); + true + } + } + } + }; + drop(hooked); + if ok { + PRESS_WINDOWS.lock().unwrap().insert(w); + } + } + + pub fn unwatch_native(w: u64) { + if w == 0 { return } + PRESS_WINDOWS.lock().unwrap().remove(&w); + PRESS.lock().unwrap().drop_target(w); + super::unregister_press_wake(w); + let to_unhook: Vec<(usize, usize, usize)> = { + let mut hooked = HOOKED_THREADS.lock().unwrap(); + let mut drop_tids = Vec::new(); + let mut hooks = Vec::new(); + for (&tid, (mh, ch, rh, set)) in hooked.iter_mut() { + if set.remove(&w) && set.is_empty() { + drop_tids.push(tid); + hooks.push((*mh, *ch, *rh)); + } + } + for tid in drop_tids { + hooked.remove(&tid); + } + hooks + }; + for (mh, ch, rh) in to_unhook { + unsafe { + UnhookWindowsHookEx(mh as HHOOK); + UnhookWindowsHookEx(ch as HHOOK); + UnhookWindowsHookEx(rh as HHOOK); + } + } + } + + /// A recent press is consumed once per callback kind for the native child that received it. + pub fn pressed_in(w: u64, cb: FocusCallback) -> PressProbe { + let mut press = PRESS.lock().unwrap(); + if let Some(seq) = press.consume(w, cb, std::time::Instant::now()) { + return PressProbe::Pressed(seq); + } + let blocked = press.is_unwatched_blocked(w); + drop(press); + if blocked || PRESS_WINDOWS.lock().unwrap().contains(&w) { + PressProbe::NotPressed + } else { + PressProbe::Unwatched + } + } + + pub fn clear_presses(frames: &[u64]) -> u64 { + let mut unique: Vec = Vec::with_capacity(frames.len()); + for &w in frames { + if w != 0 && !unique.contains(&w) { + unique.push(w); + } + } + if unique.is_empty() { + return next_focus_seq(); + } + let unwatched: Vec = { + let pw = PRESS_WINDOWS.lock().unwrap(); + unique.iter().copied().filter(|w| !pw.contains(w)).collect() + }; + let held = !unwatched.is_empty() && any_button_held(); + let (cutoff, spawn_gen) = { + let mut press = PRESS.lock().unwrap(); + for &w in &unique { + press.clear_target(w); + } + let mut any_blocked = false; + if held { + for &w in &unwatched { + if press.block_unwatched_until_release(w).is_some() { + any_blocked = true; + } + } + } + (next_focus_seq(), any_blocked.then(|| press.unwatched_block_gen())) + }; + if let Some(mut block_gen) = spawn_gen { + let _ = std::thread::Builder::new().name("cef-unwatched-rel".into()).spawn(move || { + loop { + std::thread::sleep(std::time::Duration::from_millis(5)); + if !any_button_held() { + let mut press = PRESS.lock().unwrap(); + if press.clear_unwatched_release_block_if(block_gen) { + break; + } + block_gen = press.unwatched_block_gen(); + } + } + }); + } + cutoff + } + + pub fn clear_press(w: u64) { + if w != 0 { + let _ = clear_presses(&[w]); + } + } + + pub fn fresh_press_seq_after(w: u64, after_seq: u64) -> Option { + PRESS.lock().unwrap().fresh_press_seq_after(w, after_seq, std::time::Instant::now()) + } + + pub fn clear_press_up_to(w: u64, max_seq: u64) { + PRESS.lock().unwrap().clear_target_up_to(w, max_seq); + } + + pub fn close(w: u64) { + unwatch_native(w); + unsafe { DestroyWindow(hwnd(w)); } + } + /// Every window under `h` (class, rect in the screen, visible, style), for the report. - #[cfg(windows)] pub fn tree(h: Host) -> Vec { - use windows_sys::Win32::{Foundation::*, UI::WindowsAndMessaging::*}; + pub fn tree(h: Host) -> Vec { let Host::Win(p) = h else { return vec![] }; unsafe extern "system" fn cb(w: HWND, l: LPARAM) -> i32 { - let out = &mut *(l as *mut Vec); let mut cls = [0u16; 64]; let n = GetClassNameW(w, cls.as_mut_ptr(), 64); - let mut r = std::mem::zeroed::(); GetWindowRect(w, &mut r); + let out = unsafe { &mut *(l as *mut Vec) }; + let mut cls = [0u16; 64]; + let n = unsafe { GetClassNameW(w, cls.as_mut_ptr(), 64) }; + let mut r = unsafe { std::mem::zeroed::() }; + unsafe { GetWindowRect(w, &mut r); } out.push(format!("{:x} {} [{},{} {}x{}] vis={} style={:x} ex={:x} parent={:x}", w as usize, String::from_utf16_lossy(&cls[..n.max(0) as usize]), r.left, r.top, r.right - r.left, r.bottom - r.top, // i18n-ok: a diagnostic in the e2e report - IsWindowVisible(w), GetWindowLongPtrW(w, GWL_STYLE), GetWindowLongPtrW(w, GWL_EXSTYLE), GetParent(w) as usize)); 1 } + unsafe { IsWindowVisible(w) }, unsafe { GetWindowLongPtrW(w, GWL_STYLE) }, unsafe { GetWindowLongPtrW(w, GWL_EXSTYLE) }, unsafe { GetParent(w) as usize })); + 1 + } let mut out: Vec = Vec::new(); - unsafe { let mut r = std::mem::zeroed::(); GetWindowRect(p as HWND, &mut r); out.push(format!("client {:x} [{},{} {}x{}] ex={:x}", p, r.left, r.top, r.right - r.left, r.bottom - r.top, GetWindowLongPtrW(p as HWND, GWL_EXSTYLE))); // i18n-ok: a diagnostic in the e2e report - EnumChildWindows(p as HWND, Some(cb), &mut out as *mut _ as LPARAM); } + unsafe { + let mut r = std::mem::zeroed::(); + GetWindowRect(p as HWND, &mut r); + out.push(format!("client {:x} [{},{} {}x{}] ex={:x}", p, r.left, r.top, r.right - r.left, r.bottom - r.top, GetWindowLongPtrW(p as HWND, GWL_EXSTYLE))); // i18n-ok: a diagnostic in the e2e report + EnumChildWindows(p as HWND, Some(cb), &mut out as *mut _ as LPARAM); + } out } - #[cfg(windows)] pub fn button_down(_frame: u64) -> bool { use windows_sys::Win32::UI::Input::KeyboardAndMouse::GetAsyncKeyState; unsafe { [0x01, 0x02, 0x04].iter().any(|k| GetAsyncKeyState(*k) as u16 & 0x8000 != 0) } } - #[cfg(target_os = "macos")] pub fn button_down(_frame: u64) -> bool { - #[link(name = "objc")] unsafe extern "C" { fn objc_getClass(n: *const std::ffi::c_char) -> *mut c_void; } - unsafe { let cls = objc_getClass(c"NSEvent".as_ptr()); !cls.is_null() && !send0(cls, c"pressedMouseButtons").is_null() } // i18n-ok: an Objective-C class name + + /// Fallback when `watch_native` is unavailable: requires both a held mouse button and the pointer inside `w`. + pub fn button_down(w: u64) -> bool { + if w == 0 || PRESS.lock().unwrap().is_unwatched_blocked(w) || !any_button_held() { return false } + let mut pt = POINT { x: 0, y: 0 }; + if unsafe { GetCursorPos(&mut pt) } == 0 { return false } + let at = unsafe { WindowFromPoint(pt) }; + let inside = !at.is_null() && (at == hwnd(w) || unsafe { IsChild(hwnd(w), at) } != 0); + inside && !PRESS.lock().unwrap().is_unwatched_blocked(w) } - use windows_sys::Win32::{Foundation::HWND, Graphics::Gdi::{GetDC, GetPixel, ReleaseDC}, UI::{Input::KeyboardAndMouse::SetFocus, WindowsAndMessaging::*}}; + fn hwnd(w: u64) -> HWND { w as isize as HWND } /// Asynchronous: the child belongs to CEF's UI thread, and a synchronous call from here would wait on it. pub fn place(w: u64, r: R) { unsafe { SetWindowPos(hwnd(w), std::ptr::null_mut(), r.x, r.y, r.w.max(1), r.h.max(1), SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS); } } @@ -261,13 +883,28 @@ mod imp { /// CEF's SetFocus does it on Windows (its UI thread owns the child); nothing more here. pub fn focus_child(_w: u64) {} /// The client's window is gpui's, on this (its) thread. - pub fn focus_host(h: Host, _frame: u64) { if let Host::Win(p) = h { unsafe { SetFocus(p as HWND); } } } + /// When `target != 0`, only returns Win32 focus to the host if `target` (or a child of `target`) + /// currently holds foreground keyboard focus, so a background tab yank never steals from an active tab. + pub fn focus_host(h: Host, target: u64) { + if let Host::Win(p) = h { + if target != 0 { + let t = hwnd(target); + let mut gti = GUITHREADINFO { cbSize: std::mem::size_of::() as u32, ..Default::default() }; + let focused = if unsafe { GetGUIThreadInfo(0, &mut gti) } != 0 { gti.hwndFocus } else { unsafe { GetFocus() } }; + if focused.is_null() || (focused != t && unsafe { IsChild(t, focused) } == 0) { + return; + } + } + unsafe { SetFocus(p as HWND); } + } + } pub fn geometry(w: u64) -> Option<(R, bool)> { unsafe { - let mut rc = std::mem::zeroed::(); + let mut rc = std::mem::zeroed::(); if GetWindowRect(hwnd(w), &mut rc) == 0 { return None } - let parent = GetParent(hwnd(w)); let mut pt = [windows_sys::Win32::Foundation::POINT { x: rc.left, y: rc.top }]; - windows_sys::Win32::Graphics::Gdi::ScreenToClient(parent, pt.as_mut_ptr()); + let parent = GetParent(hwnd(w)); + let mut pt = [POINT { x: rc.left, y: rc.top }]; + ScreenToClient(parent, pt.as_mut_ptr()); Some((R { x: pt[0].x, y: pt[0].y, w: rc.right - rc.left, h: rc.bottom - rc.top }, IsWindowVisible(hwnd(w)) != 0)) } } @@ -285,65 +922,229 @@ mod imp { #[cfg(target_os = "macos")] mod imp { use super::{Host, R}; + use crate::viewers::cef_focus::{next_focus_seq, FocusCallback, PressProbe, PressTracker}; + use std::collections::HashSet; + use std::ffi::{c_void, CStr}; + use std::sync::{LazyLock, Mutex, OnceLock}; + + static PRESS: Mutex = Mutex::new(PressTracker::new()); + static PRESS_WINDOWS: LazyLock>> = LazyLock::new(Default::default); + static WATCHER: OnceLock<()> = OnceLock::new(); + pub fn pixel(_w: u64, _x: i32, _y: i32) -> Option<[u8; 3]> { None } pub fn inner_size(_w: u64) -> Option<(i32, i32)> { None } pub fn container(_h: Host) -> Option { None } pub fn place_in(_frame: u64, _w: u64, _r: Option) {} pub fn destroy(_frame: u64) {} - /// X11 only (its press watcher): here on_set_focus asks `button_down`. - pub fn pressed_in(_frame: u64) -> Option { None } - #[cfg(windows)] pub fn close(w: u64) { unsafe { windows_sys::Win32::UI::WindowsAndMessaging::DestroyWindow(w as isize as _); } } - #[cfg(target_os = "macos")] pub fn close(w: u64) { unsafe { send0(v(w), c"removeFromSuperview"); } } - /// Every window under `h` (class, rect in the screen, visible, style), for the report. - #[cfg(windows)] pub fn tree(h: Host) -> Vec { - use windows_sys::Win32::{Foundation::*, UI::WindowsAndMessaging::*}; - let Host::Win(p) = h else { return vec![] }; - unsafe extern "system" fn cb(w: HWND, l: LPARAM) -> i32 { - let out = &mut *(l as *mut Vec); let mut cls = [0u16; 64]; let n = GetClassNameW(w, cls.as_mut_ptr(), 64); - let mut r = std::mem::zeroed::(); GetWindowRect(w, &mut r); - out.push(format!("{:x} {} [{},{} {}x{}] vis={} style={:x} ex={:x} parent={:x}", w as usize, String::from_utf16_lossy(&cls[..n.max(0) as usize]), r.left, r.top, r.right - r.left, r.bottom - r.top, // i18n-ok: a diagnostic in the e2e report - IsWindowVisible(w), GetWindowLongPtrW(w, GWL_STYLE), GetWindowLongPtrW(w, GWL_EXSTYLE), GetParent(w) as usize)); 1 } - let mut out: Vec = Vec::new(); - unsafe { let mut r = std::mem::zeroed::(); GetWindowRect(p as HWND, &mut r); out.push(format!("client {:x} [{},{} {}x{}] ex={:x}", p, r.left, r.top, r.right - r.left, r.bottom - r.top, GetWindowLongPtrW(p as HWND, GWL_EXSTYLE))); // i18n-ok: a diagnostic in the e2e report - EnumChildWindows(p as HWND, Some(cb), &mut out as *mut _ as LPARAM); } - out - } - #[cfg(windows)] pub fn button_down(_frame: u64) -> bool { use windows_sys::Win32::UI::Input::KeyboardAndMouse::GetAsyncKeyState; unsafe { [0x01, 0x02, 0x04].iter().any(|k| GetAsyncKeyState(*k) as u16 & 0x8000 != 0) } } - #[cfg(target_os = "macos")] pub fn button_down(_frame: u64) -> bool { - #[link(name = "objc")] unsafe extern "C" { fn objc_getClass(n: *const std::ffi::c_char) -> *mut c_void; } - unsafe { let cls = objc_getClass(c"NSEvent".as_ptr()); !cls.is_null() && !send0(cls, c"pressedMouseButtons").is_null() } // i18n-ok: an Objective-C class name - } - use std::ffi::{c_void, CStr}; + #[repr(C)] #[derive(Clone, Copy)] struct Rect { x: f64, y: f64, w: f64, h: f64 } - #[link(name = "objc")] unsafe extern "C" { fn sel_registerName(n: *const std::ffi::c_char) -> *mut c_void; fn objc_msgSend(); } + #[repr(C)] #[derive(Clone, Copy)] struct Pt { x: f64, y: f64 } + #[link(name = "objc")] unsafe extern "C" { + fn sel_registerName(n: *const std::ffi::c_char) -> *mut c_void; + fn objc_getClass(n: *const std::ffi::c_char) -> *mut c_void; + fn objc_msgSend(); + } fn sel(n: &CStr) -> *mut c_void { unsafe { sel_registerName(n.as_ptr()) } } - unsafe fn send0(o: *mut c_void, s: &CStr) -> *mut c_void { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } - unsafe fn send_bool(o: *mut c_void, s: &CStr) -> bool { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> bool = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } - unsafe fn send_obj(o: *mut c_void, s: &CStr, a: *mut c_void) -> *mut c_void { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) -> *mut c_void = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), a) } - unsafe fn send_flag(o: *mut c_void, s: &CStr, a: bool) { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, bool) = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), a) } - unsafe fn send_rect(o: *mut c_void, s: &CStr, r: Rect) { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, Rect) = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), r) } + unsafe fn send0(o: *mut c_void, s: &CStr) -> *mut c_void { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } } + unsafe fn send_void(o: *mut c_void, s: &CStr) { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } } + unsafe fn send_usize(o: *mut c_void, s: &CStr) -> usize { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> usize = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } } + #[cfg(target_arch = "aarch64")] + type ObjCBool = bool; + #[cfg(not(target_arch = "aarch64"))] + type ObjCBool = i8; + #[cfg(target_arch = "aarch64")] + #[inline] + fn to_objc_bool(v: bool) -> ObjCBool { v } + #[cfg(not(target_arch = "aarch64"))] + #[inline] + fn to_objc_bool(v: bool) -> ObjCBool { v as i8 } + #[cfg(target_arch = "aarch64")] + #[inline] + fn from_objc_bool(v: ObjCBool) -> bool { v } + #[cfg(not(target_arch = "aarch64"))] + #[inline] + fn from_objc_bool(v: ObjCBool) -> bool { v != 0 } + unsafe fn send_bool(o: *mut c_void, s: &CStr) -> bool { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> ObjCBool = std::mem::transmute(objc_msgSend as *const ()); from_objc_bool(f(o, sel(s))) } } + unsafe fn send_obj_bool(o: *mut c_void, s: &CStr, a: *mut c_void) -> bool { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) -> ObjCBool = std::mem::transmute(objc_msgSend as *const ()); from_objc_bool(f(o, sel(s), a)) } } + unsafe fn send_flag(o: *mut c_void, s: &CStr, a: bool) { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, ObjCBool) = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), to_objc_bool(a)) } } + unsafe fn send_rect(o: *mut c_void, s: &CStr, r: Rect) { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, Rect) = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), r) } } + unsafe fn point_of(o: *mut c_void, s: &CStr) -> Pt { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> Pt = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } } + unsafe fn convert_point(o: *mut c_void, s: &CStr, pt: Pt, from: *mut c_void) -> Pt { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, Pt, *mut c_void) -> Pt = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), pt, from) } } + unsafe fn convert_point_from_screen(win: *mut c_void, pt: Pt) -> Pt { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, Pt) -> Pt = std::mem::transmute(objc_msgSend as *const ()); f(win, sel(c"convertPointFromScreen:"), pt) } } // i18n-ok: an Objective-C selector + unsafe fn send_point_obj(o: *mut c_void, s: &CStr, pt: Pt) -> *mut c_void { unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void, Pt) -> *mut c_void = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s), pt) } } #[cfg(target_arch = "x86_64")] #[link(name = "objc")] unsafe extern "C" { fn objc_msgSend_stret(); } unsafe fn rect_of(o: *mut c_void, s: &CStr) -> Rect { - #[cfg(target_arch = "aarch64")] { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> Rect = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } - #[cfg(target_arch = "x86_64")] { let mut r = Rect { x: 0., y: 0., w: 0., h: 0. }; let f: unsafe extern "C" fn(*mut Rect, *mut c_void, *mut c_void) = std::mem::transmute(objc_msgSend_stret as *const ()); f(&mut r, o, sel(s)); r } + #[cfg(target_arch = "aarch64")] unsafe { let f: unsafe extern "C" fn(*mut c_void, *mut c_void) -> Rect = std::mem::transmute(objc_msgSend as *const ()); f(o, sel(s)) } + #[cfg(target_arch = "x86_64")] unsafe { let mut r = Rect { x: 0., y: 0., w: 0., h: 0. }; let f: unsafe extern "C" fn(*mut Rect, *mut c_void, *mut c_void) = std::mem::transmute(objc_msgSend_stret as *const ()); f(&mut r, o, sel(s)); r } } fn v(w: u64) -> *mut c_void { w as usize as *mut c_void } + + /// Returns the actual NSView AppKit hit-tests at window-base point `pt`, honoring overlapping views and visibility. + unsafe fn hit_view(win: *mut c_void, pt: Pt) -> *mut c_void { + unsafe { + let content = send0(win, c"contentView"); // i18n-ok: an Objective-C selector + if content.is_null() { return std::ptr::null_mut() } + let superview = send0(content, c"superview"); + let hit_pt = if superview.is_null() { pt } else { convert_point(superview, c"convertPoint:fromView:", pt, std::ptr::null_mut()) }; // i18n-ok: an Objective-C selector + send_point_obj(content, c"hitTest:", hit_pt) // i18n-ok: an Objective-C selector + } + } + + fn on_mac_mouse_down(event: *mut c_void) { + if event.is_null() { return } + let watched: Vec = PRESS_WINDOWS.lock().unwrap().iter().copied().collect(); + if watched.is_empty() { return } + unsafe { + let ev_win = send0(event, c"window"); + if ev_win.is_null() { + PRESS.lock().unwrap().clear_all(); + return; + } + let pt = point_of(event, c"locationInWindow"); // i18n-ok: an Objective-C selector + let hit = hit_view(ev_win, pt); + if !hit.is_null() { + for w in watched { + if w == 0 { continue } + let view = v(w); + if send0(view, c"window") == ev_win + && !send_bool(view, c"isHiddenOrHasHiddenAncestor") // i18n-ok: an Objective-C selector + && send_obj_bool(hit, c"isDescendantOf:", view) // i18n-ok: an Objective-C selector + { + let seq = PRESS.lock().unwrap().record(w, std::time::Instant::now()); + if seq != 0 { + super::notify_press_wake(w); + } + return; + } + } + } + PRESS.lock().unwrap().clear_all(); + } + } + + pub fn watch_native(w: u64) { + if w == 0 { return } + WATCHER.get_or_init(|| gpui::set_mac_mouse_down_observer(Some(on_mac_mouse_down))); + PRESS_WINDOWS.lock().unwrap().insert(w); + } + + pub fn unwatch_native(w: u64) { + if w == 0 { return } + PRESS_WINDOWS.lock().unwrap().remove(&w); + PRESS.lock().unwrap().drop_target(w); + super::unregister_press_wake(w); + } + + pub fn pressed_in(w: u64, cb: FocusCallback) -> PressProbe { + let mut press = PRESS.lock().unwrap(); + if let Some(seq) = press.consume(w, cb, std::time::Instant::now()) { + return PressProbe::Pressed(seq); + } + let blocked = press.is_unwatched_blocked(w); + drop(press); + if blocked || PRESS_WINDOWS.lock().unwrap().contains(&w) { + PressProbe::NotPressed + } else { + PressProbe::Unwatched + } + } + + pub fn clear_presses(frames: &[u64]) -> u64 { + let mut press = PRESS.lock().unwrap(); + for &w in frames { + if w != 0 { + press.clear_target(w); + } + } + next_focus_seq() + } + + pub fn clear_press(w: u64) { + if w != 0 { + let _ = clear_presses(&[w]); + } + } + + pub fn fresh_press_seq_after(w: u64, after_seq: u64) -> Option { + PRESS.lock().unwrap().fresh_press_seq_after(w, after_seq, std::time::Instant::now()) + } + + pub fn clear_press_up_to(w: u64, max_seq: u64) { + PRESS.lock().unwrap().clear_target_up_to(w, max_seq); + } + + pub fn close(w: u64) { + unwatch_native(w); + unsafe { send_void(v(w), c"removeFromSuperview"); } // i18n-ok: an Objective-C selector + } + + /// Fallback when `watch_native` is unavailable: requires both a pressed mouse button and hit-testing inside `w`. + pub fn button_down(w: u64) -> bool { + if w == 0 { return false } + unsafe { + let cls = objc_getClass(c"NSEvent".as_ptr()); // i18n-ok: an Objective-C class name + if cls.is_null() || send_usize(cls, c"pressedMouseButtons") == 0 { return false } // i18n-ok: an Objective-C selector + let view = v(w); + let win = send0(view, c"window"); + if win.is_null() || send_bool(view, c"isHiddenOrHasHiddenAncestor") { return false } // i18n-ok: an Objective-C selector + let screen_pt = point_of(cls, c"mouseLocation"); // i18n-ok: an Objective-C selector + let win_pt = convert_point_from_screen(win, screen_pt); + let hit = hit_view(win, win_pt); + !hit.is_null() && send_obj_bool(hit, c"isDescendantOf:", view) // i18n-ok: an Objective-C selector + } + } + /// `r` is in points from the parent's top-left; an unflipped parent (gpui's view) counts y from its bottom. pub fn place(w: u64, r: R) { unsafe { let parent = send0(v(w), c"superview"); if parent.is_null() { return } - let y = if send_bool(parent, c"isFlipped") { r.y as f64 } else { rect_of(parent, c"bounds").h - (r.y + r.h) as f64 }; - send_rect(v(w), c"setFrame:", Rect { x: r.x as f64, y, w: r.w.max(1) as f64, h: r.h.max(1) as f64 }); + let y = if send_bool(parent, c"isFlipped") { r.y as f64 } else { rect_of(parent, c"bounds").h - (r.y + r.h) as f64 }; // i18n-ok: an Objective-C selector + send_rect(v(w), c"setFrame:", Rect { x: r.x as f64, y, w: r.w.max(1) as f64, h: r.h.max(1) as f64 }); // i18n-ok: an Objective-C selector + } + } + pub fn show(w: u64, on: bool) { unsafe { send_flag(v(w), c"setHidden:", !on); } } // i18n-ok: an Objective-C selector + /// Moves firstResponder onto CEF's view unless an inner Chromium subview (e.g. `RenderWidgetHostViewCocoa`) already holds it. + pub fn focus_child(w: u64) { + if w == 0 { return } + unsafe { + let view = v(w); + let win = send0(view, c"window"); + if win.is_null() { return } + let fr = send0(win, c"firstResponder"); // i18n-ok: an Objective-C selector + let ns_view = objc_getClass(c"NSView".as_ptr()); // i18n-ok: an Objective-C class name + if !fr.is_null() && !ns_view.is_null() + && send_obj_bool(fr, c"isKindOfClass:", ns_view) // i18n-ok: an Objective-C selector + && send_obj_bool(fr, c"isDescendantOf:", view) // i18n-ok: an Objective-C selector + { + return; + } + let _ = send_obj_bool(win, c"makeFirstResponder:", view); // i18n-ok: an Objective-C selector + } + } + /// Moves firstResponder back to gpui's host view. When `target != 0`, only does so if `target` + /// (or an NSView descendant of `target`) currently holds firstResponder, so a background tab yank never steals from an active tab. + pub fn focus_host(h: Host, target: u64) { + if let Host::Mac(p) = h { + unsafe { + let win = send0(p as *mut c_void, c"window"); // i18n-ok: an Objective-C selector + if win.is_null() { return } + if target != 0 { + let fr = send0(win, c"firstResponder"); // i18n-ok: an Objective-C selector + let ns_view = objc_getClass(c"NSView".as_ptr()); // i18n-ok: an Objective-C class name + let owns = !fr.is_null() && !ns_view.is_null() + && send_obj_bool(fr, c"isKindOfClass:", ns_view) // i18n-ok: an Objective-C selector + && send_obj_bool(fr, c"isDescendantOf:", v(target)); // i18n-ok: an Objective-C selector + if !owns { return } + } + let _ = send_obj_bool(win, c"makeFirstResponder:", p as *mut c_void); // i18n-ok: an Objective-C selector + } } } - pub fn show(w: u64, on: bool) { unsafe { send_flag(v(w), c"setHidden:", !on); } } - pub fn focus_child(w: u64) { unsafe { let win = send0(v(w), c"window"); if !win.is_null() { send_obj(win, c"makeFirstResponder:", v(w)); } } } - pub fn focus_host(h: Host, _frame: u64) { if let Host::Mac(p) = h { unsafe { let win = send0(p as *mut c_void, c"window"); if !win.is_null() { send_obj(win, c"makeFirstResponder:", p as *mut c_void); } } } } pub fn geometry(w: u64) -> Option<(R, bool)> { unsafe { let parent = send0(v(w), c"superview"); if parent.is_null() { return None } - let f = rect_of(v(w), c"frame"); let y = if send_bool(parent, c"isFlipped") { f.y } else { rect_of(parent, c"bounds").h - f.y - f.h }; - Some((R { x: f.x as i32, y: y as i32, w: f.w as i32, h: f.h as i32 }, !send_bool(v(w), c"isHidden"))) + let f = rect_of(v(w), c"frame"); let y = if send_bool(parent, c"isFlipped") { f.y } else { rect_of(parent, c"bounds").h - f.y - f.h }; // i18n-ok: an Objective-C selector + Some((R { x: f.x as i32, y: y as i32, w: f.w as i32, h: f.h as i32 }, !send_bool(v(w), c"isHidden"))) // i18n-ok: an Objective-C selector } } /// Not read on macOS (the e2e reads the page over CDP there): the child is counted painted once it is shown. diff --git a/native/gpui/src/viewers/cef_page.rs b/native/gpui/src/viewers/cef_page.rs index 5abe5a6131..f558da523f 100644 --- a/native/gpui/src/viewers/cef_page.rs +++ b/native/gpui/src/viewers/cef_page.rs @@ -19,7 +19,7 @@ pub struct CefPage { /// windowed: the client window (None until the first render decides; Some(None): off screen), the pane's marker hitbox in the last frame, /// where each tab's child was put (by tab), whether something drawn later covers the pane, gpui keys from the page waiting for a window host: Option>, marker: Arc>>, placed: std::collections::HashMap, u64)>, covered: bool, - from_page: Vec<(String, cef::KeyEvent)>, lat_native: Arc>, want_focus: bool, want_yank: bool, dispatching: Arc, subs: Vec, + from_page: Vec<(String, cef::KeyEvent)>, lat_native: Arc>, focus_handoff: super::cef_focus::FocusHandoff, pending_self_blur_seq: Option, dispatching: Arc, subs: Vec, } impl CefPage { @@ -33,7 +33,7 @@ impl CefPage { } }).detach(); let mut me = CefPage { header_owner: None, tabs: Vec::new(), active: 0, focus: cx.focus_handle(), bar_focus: cx.focus_handle(), bar_text: String::new(), bar_edit: false, bounds: Arc::default(), image: None, retired: Vec::new(), wake, hub, owner, port, taken: false, light: false, start: "about:blank".into(), fresh: false, sample: [0; 3], lat_probe: None, lat_ms: -1.0, buttons: 0, visible: false, composing: false, attached: Vec::new(), status: String::new(), stats: (0, 0, 0), last_mouse: (0, 0), - host: None, marker: Arc::default(), placed: Default::default(), covered: false, from_page: Vec::new(), lat_native: Arc::new(Mutex::new(-1.0)), want_focus: false, want_yank: false, dispatching: Arc::default(), subs: Vec::new() }; + host: None, marker: Arc::default(), placed: Default::default(), covered: false, from_page: Vec::new(), lat_native: Arc::new(Mutex::new(-1.0)), focus_handoff: super::cef_focus::FocusHandoff::new(), pending_self_blur_seq: None, dispatching: Arc::default(), subs: Vec::new() }; me } pub(super) fn settled(&self) -> Option<&'static str> { @@ -61,10 +61,63 @@ impl CefPage { window.on_frame_drawn(move |window, cx| weak.update(cx, |this, cx| this.after_frame(window, cx)).is_ok()); // the keyboard follows gpui's focus: onto the page, Chromium's window has it; off the page (only while this window is the active one, // so another program's never is taken), the client's window has it again - self.subs.push(cx.on_focus(&self.focus, window, |this, _, _| { if let Some(t) = this.tab() { t.give_keyboard(true); } })); - self.subs.push(cx.on_blur(&self.focus, window, |this, window, _| { if window.is_window_active() { if let Some(t) = this.tab() { t.give_keyboard(false); } } })); + self.subs.push(cx.on_focus(&self.focus, window, |this, _, _| { + this.pending_self_blur_seq = None; + this.focus_handoff.on_host_focus(); + if let Some(t) = this.tab() { t.give_keyboard(true); } + })); + self.subs.push(cx.on_blur(&self.focus, window, |this, window, cx| { + let self_blur = this.pending_self_blur_seq.take(); + if window.is_window_active() { + if let Some(plan_seq) = self_blur { + if let Some(t) = this.tab().cloned() + && let Some(press_seq) = native::fresh_press_seq_after(t.outer(), plan_seq) + && this.focus_handoff.recover_click_after_yank(t.focus_id, press_seq) + { + window.focus(&this.focus); + this.bar_edit = false; + t.give_keyboard(true); + this.take_over(); + let weak = cx.entity().downgrade(); + window.defer(cx, move |window, cx| { + window.refresh(); + let _ = weak.update(cx, |_, cx| cx.notify()); + }); + return; + } + this.focus_handoff.on_host_blur(plan_seq); + for t in &this.tabs { native::clear_press_up_to(t.outer(), plan_seq); } + } else { + let outers: Vec = this.tabs.iter().map(|t| t.outer()).collect(); + let seq = native::clear_presses(&outers); + this.focus_handoff.on_host_blur(seq); + if let Some(t) = this.tab().cloned() { + t.yank_keyboard_up_to(seq); + if let Some(press_seq) = native::fresh_press_seq_after(t.outer(), seq) + && this.focus_handoff.recover_click_after_yank(t.focus_id, press_seq) + { + window.focus(&this.focus); + this.bar_edit = false; + t.give_keyboard(true); + this.take_over(); + let weak = cx.entity().downgrade(); + window.defer(cx, move |window, cx| { + window.refresh(); + let _ = weak.update(cx, |_, cx| cx.notify()); + }); + } + } + } + } + })); + self.subs.push(cx.observe_window_activation(window, |this, window, _| { if !window.is_window_active() { this.on_window_deactivate(); } })); } } + fn on_window_deactivate(&mut self) { + self.pending_self_blur_seq = None; + let outers: Vec = self.tabs.iter().map(|t| t.outer()).collect(); + self.focus_handoff.on_window_deactivate(native::clear_presses(&outers)); + } /// windowed, after every frame gpui drew: each tab's child over the pane (the active one, when the pane was drawn and nothing drawn later covers /// it) or hidden (the rest). A sliver under 12 px (a splitter's grip at the pane's edge) does not count as covering. fn after_frame(&mut self, window: &mut Window, cx: &mut Context) { @@ -117,16 +170,28 @@ impl CefPage { match paths { Some(p) if !p.is_empty() => { let mut l = cef::CefStringList::new(); for x in p { l.append(&x.to_string_lossy()); } cb.cont(Some(&mut l)); } _ => cb.cancel() } }).detach(); } + fn drain_focus_events(&mut self) -> bool { + let mut person = false; + let active_id = self.tab().map(|t| t.focus_id); + for t in &self.tabs { + let mut st = t.st.lock().unwrap(); + let ev = super::cef_focus::TabFocusEvents { + click_seq: std::mem::take(&mut st.click_seq), + key_seq: std::mem::take(&mut st.key_seq), + got_focus_seq: std::mem::take(&mut st.got_focus_seq), + took_focus_seq: std::mem::take(&mut st.took_focus_seq), + }; + if self.focus_handoff.on_pump_tab(t.focus_id, ev, Some(t.focus_id) == active_id) { + person = true; + } + } + person + } /// Frames, popups, closed tabs, address changes: everything CEF's callbacks left for the main thread. fn pump(&mut self, cx: &mut Context) { - let (mut popups, mut dialogs, mut person) = (Vec::new(), Vec::new(), false); - for t in &self.tabs { let mut st = t.st.lock().unwrap(); popups.append(&mut st.popup_tabs); dialogs.append(&mut st.dialogs); if !st.download.is_empty() { self.status = st.download.clone(); } - // Chromium took the keyboard: on the person's click the page takes gpui's focus too; by itself (a page shown, a script's focus()) - // it gives the keyboard back unless the page already has gpui's focus - let got = std::mem::take(&mut st.got_focus); - if std::mem::take(&mut st.person) { person = true; self.want_focus = true; } else if got { self.want_yank = true; } - self.from_page.append(&mut st.keys); - if std::mem::take(&mut st.took_focus) { self.want_focus = false; } } + let person = self.drain_focus_events(); + let (mut popups, mut dialogs) = (Vec::new(), Vec::new()); + for t in &self.tabs { let mut st = t.st.lock().unwrap(); popups.append(&mut st.popup_tabs); dialogs.append(&mut st.dialogs); if !st.download.is_empty() { self.status = st.download.clone(); } self.from_page.append(&mut st.keys); } for t in popups { self.tabs.push(t); self.active = self.tabs.len() - 1; self.image = None; self.bar_edit = false; } for d in dialogs { self.file_dialog(d, cx); } if person { self.take_over(); } @@ -158,8 +223,19 @@ impl CefPage { } fn mods(m: &Modifiers, buttons: u32) -> u32 { cef::flags(m.shift, m.control, m.alt, false, false, false) | if m.platform { cef::COMMAND } else { 0 } | buttons } fn take_over(&mut self) { if !self.taken { self.taken = true; self.rpc(serde_json::json!({"bot": self.owner, "op": "takeover"})); } } - /// windowed: the keyboard goes back to the client too, so the person's next click into the page is seen as theirs (on_set_focus) - fn hand_back(&mut self, cx: &mut Context) { if self.taken { self.taken = false; self.rpc(serde_json::json!({"bot": self.owner, "op": "handback"})); if self.windowed() { if let Some(t) = self.tab() { t.give_keyboard(false); } } cx.notify(); } } + /// windowed: the keyboard goes back to the client via render (gated by window activity), so an inactive window never steals OS focus + fn hand_back(&mut self, cx: &mut Context) { + if self.taken { + self.taken = false; + self.rpc(serde_json::json!({"bot": self.owner, "op": "handback"})); + if self.windowed() { + let origin = self.tab().map(|t| t.focus_id); + let outers: Vec = self.tabs.iter().map(|t| t.outer()).collect(); + self.focus_handoff.request_blur(origin, native::clear_presses(&outers)); + } + cx.notify(); + } + } fn go(&mut self, cx: &mut Context) { let raw = self.bar_text.trim().to_string(); if raw.is_empty() { return } let url = if raw.contains("://") || raw.starts_with("about:") || raw.starts_with("file:") { raw } else if raw.contains(' ') || !raw.contains('.') && !raw.starts_with("localhost") { format!("https://www.google.com/search?q={}", raw.replace(' ', "+")) } else if raw.starts_with("localhost") || raw.starts_with("127.") { format!("http://{raw}") } else { format!("https://{raw}") }; @@ -231,10 +307,63 @@ impl Render for CefPage { for old in self.retired.drain(..) { let _ = window.drop_image(old); } if self.host.is_none() { self.decide(window, cx); } if self.tabs.is_empty() { self.new_tab(&self.start.clone(), cx); } - if std::mem::take(&mut self.want_focus) && !self.focus.is_focused(window) { window.focus(&self.focus); self.bar_edit = false; } - if std::mem::take(&mut self.want_yank) && !self.focus.is_focused(window) && window.is_window_active() { if let Some(t) = self.tab() { - if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_yank": true})); } - t.give_keyboard(false); } } + if self.drain_focus_events() { self.take_over(); } + let active_id = self.tab().map(|t| t.focus_id); + let page_focused = self.focus.is_focused(window); + let window_active = window.is_window_active(); + if window_active + && let Some(cutoff) = self.focus_handoff.pending_press_cutoff_for_active(active_id, page_focused) + && let Some(t) = self.tab().cloned() + && let Some(press_seq) = native::fresh_press_seq_after(t.outer(), cutoff) + && self.focus_handoff.on_pending_click(t.focus_id, press_seq) + { + self.take_over(); + } + let plan = self.focus_handoff.take_render_plan(active_id, page_focused, window_active); + match plan.gpui { + super::cef_focus::GpuiFocusChange::FocusPage => { self.pending_self_blur_seq = None; window.focus(&self.focus); self.bar_edit = false; } + super::cef_focus::GpuiFocusChange::BlurPage => { self.pending_self_blur_seq = window.is_window_active().then_some(plan.blur_cutoff_seq); window.blur(); } + super::cef_focus::GpuiFocusChange::None => {} + } + for y in &plan.release_cef_only { + if let Some(t) = self.tabs.iter().find(|t| t.focus_id == y.tab) { + if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_release_bg": true})); } + t.release_cef_focus_up_to(y.cutoff_seq); + } + } + if !plan.yank_to_host.is_empty() { + let mut yanked_any = false; + for y in &plan.yank_to_host { + if let Some(t) = self.tabs.iter().find(|t| t.focus_id == y.tab) { + if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"cef_yank": true})); } + t.yank_keyboard_up_to(y.cutoff_seq); + yanked_any = true; + } + } + if !yanked_any + && (plan.active_blur_or_yank_cutoff(active_id).is_some() || active_id.is_none()) + && let Some(Some(host)) = self.host + { + native::focus_host(host, 0); + } + } + if let Some(tab_id) = plan.restore_active + && let Some(t) = self.tabs.iter().find(|t| t.focus_id == tab_id) + { + t.give_keyboard(true); + } + if window.is_window_active() + && let Some(cutoff) = plan.recovery_cutoff_for_active(active_id, self.focus_handoff.recovery_floor_seq()) + && let Some(t) = self.tab().cloned() + && let Some(press_seq) = native::fresh_press_seq_after(t.outer(), cutoff) + && self.focus_handoff.recover_click_after_yank(t.focus_id, press_seq) + { + self.pending_self_blur_seq = None; + window.focus(&self.focus); + self.bar_edit = false; + t.give_keyboard(true); + self.take_over(); + } if !self.from_page.is_empty() { let keys = std::mem::take(&mut self.from_page); let flag = self.dispatching.clone(); let tab = self.tab().cloned(); window.defer(cx, move |window, cx| for (ks, e) in keys { diff --git a/native/gpui/src/viewers/mod.rs b/native/gpui/src/viewers/mod.rs index 14afd787b0..14fa9c7b92 100644 --- a/native/gpui/src/viewers/mod.rs +++ b/native/gpui/src/viewers/mod.rs @@ -4,6 +4,7 @@ pub mod boards; pub mod browser; pub mod chrome; +pub mod cef_focus; #[cfg(feature = "cef")] pub mod cef; #[cfg(feature = "cef")] diff --git a/native/gpui/vendor/gpui/PATCH.md b/native/gpui/vendor/gpui/PATCH.md index 8117072e31..ea1d621c95 100644 --- a/native/gpui/vendor/gpui/PATCH.md +++ b/native/gpui/vendor/gpui/PATCH.md @@ -2,7 +2,7 @@ gpui 0.2.2 (Apache-2.0) vendored without `examples/` (and its `[[example]]` targ - `build.rs`: gpui's Windows step (`mod windows`) is behind the build host's cfg, so a Windows client built on Linux had no shader bytes to include and no manifest. `mod windows_cross` runs instead when the host is not Windows: for a target without debug assertions it copies `windows-shaders/shaders_bytes.rs` into `OUT_DIR`, refusing it unless its first line is the FNV-1a 64 of every `src/platform/windows/*.hlsl`; with `windows-manifest` it embeds the manifest through embed-resource (llvm-rc for -msvc, windres for -gnu*). - `Cargo.toml`: `embed-resource` is a build dependency on every host, not only Windows. -- `src/platform/mac/platform.rs`: `GPUIApplication` answers Chromium's `isHandlingSendEvent` / `setHandlingSendEvent:` (CrAppControlProtocol) and keeps that flag set inside `-sendEvent:`, as CEF's own applications do, so the embedded Chromium of the Browser tab (feature `cef`) runs inside gpui's NSApplication. +- `src/platform/mac/platform.rs`: `GPUIApplication` answers Chromium's `isHandlingSendEvent` / `setHandlingSendEvent:` (CrAppControlProtocol) and keeps that flag set inside `-sendEvent:`, as CEF's own applications do, so the embedded Chromium of the Browser tab (feature `cef`) runs inside gpui's NSApplication; also exposes `set_mac_mouse_down_observer` (`src/platform.rs`) so `-sendEvent:` notifies embedded CEF on mouse-down events before AppKit routes them to child views. - Linux clipboard writes every form an item has (`src/platform/linux/clipboard_offer.rs`, used by `x11/clipboard.rs` `set_item` and `wayland/clipboard.rs`): upstream offered every item as text only, so a copied picture was an empty string (the client's Copy image gave nothing to paste). A picture goes as `image/png` (transcoded from JPEG, WebP, GIF, BMP, TIFF or SVG) and in its own format, its strings as text. No `text/uri-list` of a picture's file: snap Firefox took it over the image and diff --git a/native/gpui/vendor/gpui/src/platform.rs b/native/gpui/vendor/gpui/src/platform.rs index 3c64fb88e5..1c3290d7c7 100644 --- a/native/gpui/vendor/gpui/src/platform.rs +++ b/native/gpui/vendor/gpui/src/platform.rs @@ -90,6 +90,13 @@ pub use keystroke::*; pub(crate) use linux::*; #[cfg(target_os = "macos")] pub(crate) use mac::*; + +/// Registers a callback invoked on macOS whenever `GPUIApplication -sendEvent:` dispatches a mouse-down `NSEvent`. +/// Backplane patch (PATCH.md), used by embedded CEF to identify user clicks before AppKit routes the event to a child NSView. +#[cfg(target_os = "macos")] +pub fn set_mac_mouse_down_observer(observer: Option) { + mac::set_mouse_down_observer(observer); +} pub use semantic_version::SemanticVersion; #[cfg(any(test, feature = "test-support"))] pub(crate) use test::*; diff --git a/native/gpui/vendor/gpui/src/platform/mac/platform.rs b/native/gpui/vendor/gpui/src/platform/mac/platform.rs index faa03e6a70..0e5aeaa648 100644 --- a/native/gpui/vendor/gpui/src/platform/mac/platform.rs +++ b/native/gpui/vendor/gpui/src/platform/mac/platform.rs @@ -70,6 +70,14 @@ static mut APP_CLASS: *const Class = ptr::null(); static mut APP_DELEGATE_CLASS: *const Class = ptr::null(); const HANDLING_SEND_EVENT_IVAR: &str = "handlingSendEvent"; +static MOUSE_DOWN_OBSERVER: std::sync::atomic::AtomicPtr = + std::sync::atomic::AtomicPtr::new(ptr::null_mut()); + +pub(crate) fn set_mouse_down_observer(observer: Option) { + let ptr = observer.map_or(ptr::null_mut(), |f| f as *mut c_void); + MOUSE_DOWN_OBSERVER.store(ptr, std::sync::atomic::Ordering::Release); +} + extern "C" fn is_handling_send_event(this: &Object, _: Sel) -> BOOL { if unsafe { *this.get_ivar::(HANDLING_SEND_EVENT_IVAR) } { YES } else { NO } } @@ -78,6 +86,16 @@ extern "C" fn set_handling_send_event(this: &mut Object, _: Sel, on: BOOL) { } extern "C" fn app_send_event(this: &mut Object, _: Sel, event: id) { unsafe { + if event != nil { + let ty: NSUInteger = msg_send![event, type]; + if matches!(ty, 1 | 3 | 25) { + let observer = MOUSE_DOWN_OBSERVER.load(std::sync::atomic::Ordering::Acquire); + if !observer.is_null() { + let f: fn(*mut c_void) = std::mem::transmute(observer); + f(event as *mut c_void); + } + } + } let was = *this.get_ivar::(HANDLING_SEND_EVENT_IVAR); this.set_ivar::(HANDLING_SEND_EVENT_IVAR, true); let _: () = msg_send![super(this, class!(NSApplication)), sendEvent: event];