From 553887f3f71fb303e5cbcd659b7e992c0c7d8309 Mon Sep 17 00:00:00 2001 From: i2cjak Date: Sat, 21 Nov 2026 00:42:19 -0500 Subject: [PATCH 1/4] kicad pane: no press handling while the pane is not in the page (another viewer on show) The hidden pane's frame rectangle is where that viewer's page now is: a click in the Browser tab's page was taken for a press in the editor, and its release handed the keyboard to the hidden KiCad frame (kicad_focus at the release, traced), taking it from the page the person clicked. in_page (false when the thread's viewer holds no editor) gates it; the press state is cleared while out of the page. Co-Authored-By: Claude Opus 5.5 --- native/gpui/src/viewers/kicad_pane.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/native/gpui/src/viewers/kicad_pane.rs b/native/gpui/src/viewers/kicad_pane.rs index e759bf7f35..1c75956703 100644 --- a/native/gpui/src/viewers/kicad_pane.rs +++ b/native/gpui/src/viewers/kicad_pane.rs @@ -1521,8 +1521,11 @@ impl KicadPane { let in_dialog = self.info["mode"] == "fork" && nc::focus_in_pid(self.info["pid"].as_u64().unwrap_or(0) as u32) && !nc::has_focus(c); // a press on another top-level over the pane (a KiCad dialog, another program) is not the editor's: nothing is taken or given let elsewhere = self.want().is_some_and(|w| nc::button_over(w).is_some() && nc::pointer_on_other_window(w)); - if elsewhere { self.press_focus = None; } - if let (Some(w), false, false) = (self.want(), in_dialog, elsewhere) { + // a pane not in the page (another viewer on show: the Browser tab) takes no press: its frame's rectangle is where that viewer's + // page now is, and a click there handed the keyboard to the hidden editor's frame, taking it from the page the person clicked + // (the Browser's Chromium, a click in its page with a KiCad pane in the window: the keys went nowhere they were typed) + if elsewhere || !self.in_page { self.press_focus = None; self.press_in = false; } + if let (Some(w), false, false, true) = (self.want(), in_dialog, elsewhere, self.in_page) { // BACKPLANE_KICAD_FOCUS_ON_PRESS: the old way (focus mid-press), to check a KiCad that must cope with it let fork = self.info["mode"] == "fork" && std::env::var_os("BACKPLANE_KICAD_FOCUS_ON_PRESS").is_none(); match (nc::button_over(w), nc::has_focus(c)) { From f4ea12f539c9d58403ab5c7e006baa64e9baf178 Mon Sep 17 00:00:00 2001 From: i2cjak Date: Sat, 21 Nov 2026 00:54:25 -0500 Subject: [PATCH 2/4] native_child (Linux): the focus proxy only where a KiCad pane's frame is on screen (per top-level, per child); the Browser e2e's page JSON-quoted, its B3 reading the page's own log With another viewer on show (the Browser tab) a pane's frame is unmapped: nothing of KiCad is under the pointer, so the redirect stays off and the focus is where GPUI and the Browser put it, as before #1505. The Browser's own click hand-back (its want_yank) put the focus on the top-level and the redirect then moved it to the proxy: a click into a Browser page with a KiCad pane in the window gave the page no keys. PaneScreens (top-level -> children on screen) is tested; the pane reports (top-level, container) each tick and takes it back when either changes or it is dropped; coming on screen moves a focus left on the top-level to the proxy. Co-Authored-By: Claude Opus 5.5 --- native/gpui/src/viewers/kicad_pane.rs | 11 +++- native/gpui/src/viewers/native_child.rs | 58 +++++++++++++++++-- native/gpui/src/viewers/native_child_mac.rs | 1 + .../embed_pointer_keys_browser_e2e.sh | 10 +++- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/native/gpui/src/viewers/kicad_pane.rs b/native/gpui/src/viewers/kicad_pane.rs index 1c75956703..3ee3582928 100644 --- a/native/gpui/src/viewers/kicad_pane.rs +++ b/native/gpui/src/viewers/kicad_pane.rs @@ -669,6 +669,8 @@ pub struct KicadPane { /// (a tab switch, a frames change, the hub's echo of it), this is not. A re-place at this same rect needs no geometry (Linux: /// `nc::show_local` maps the frame and nothing else), and `kicad_place`'s `changed` / `frame_changed` compare with it. last_place: Option<(u64, nc::R, Option)>, + /// the (top-level, container) last reported on screen to the Linux focus proxy (nc::note_pane_on_screen): taken off when either changes + screen_key: Option<(u64, u64)>, focus: FocusHandle, keys: bool, pub light: bool, _subs: Vec, /// the fork's holder already in our save-set; reserved chords pressed in KiCad, waiting to be pressed in gpui kept: u64, pub press: Vec, @@ -925,7 +927,7 @@ impl KicadPane { last = std::time::Instant::now(); let _ = drained_tx.send(()); } }).detach(); - KicadPane { born: std::time::Instant::now(), edit_key: asked_key(!deferred_open(), trigger_key()), handoff_placed: None, handoff_over: false, host_sent: None, mac_show_held: None, mac_full: false, in_page: true, mac_raise: RaiseState::default(), mac_buried: false, mac_watching: false, mac_hot: None, mac_seen: (false, None), mac_pressed: false, mac_press: None, mac_looked: None, busy_sent: None, press_in: false, press_gave_keys: false, want_gerbers: Vec::new(), bound: String::new(), project, root, project_id, open, last_hub_open: hub_open, hub, container: None, toplevel: 0, attached: false, info: Value::Null, frames: Vec::new(), marker: Arc::default(), end_marker: Arc::default(), mac_covered: false, mac_settle: None, host_sends: Default::default(), host_unsent: false, last_root: (i32::MIN, 0), placed: None, last_place: None, + KicadPane { born: std::time::Instant::now(), edit_key: asked_key(!deferred_open(), trigger_key()), handoff_placed: None, handoff_over: false, host_sent: None, mac_show_held: None, mac_full: false, in_page: true, mac_raise: RaiseState::default(), mac_buried: false, mac_watching: false, mac_hot: None, mac_seen: (false, None), mac_pressed: false, mac_press: None, mac_looked: None, busy_sent: None, press_in: false, press_gave_keys: false, want_gerbers: Vec::new(), bound: String::new(), project, root, project_id, open, last_hub_open: hub_open, hub, container: None, toplevel: 0, attached: false, info: Value::Null, frames: Vec::new(), marker: Arc::default(), end_marker: Arc::default(), mac_covered: false, mac_settle: None, host_sends: Default::default(), host_unsent: false, last_root: (i32::MIN, 0), placed: None, last_place: None, screen_key: None, focus: cx.focus_handle(), keys: false, light: false, _subs: Vec::new(), kept: 0, press: Vec::new(), lent_at: None, poked: None, unmapped_since: None, remounts: None, unshown_since: None, unshown_asks: 0, reattached_for: None, adrift_held_at: None, override_kind: None, pending_show: None, chosen_at: 0, theme_sent: false, home: 0, conflicts: Vec::new(), jobs: Vec::new(), ever_shown: Vec::new(), prewarmed: None, first_placed: None, shown_once: false, at_px: None, dialogs_open: Vec::new(), took: None, await_close: None, press_focus: None, want_keys: opening_wants_keys(why), why, auto_taken_said: false, generation: None, mac_plan_said: String::new(), refocus: None, verify_handin: None, prewarm_guard: None, unexplained_reposted: false, focus_seen: (0, 0), was_dialog: false, starting_since: None, starting_said: 0, opening_timer: Arc::default() } } /// Edit pressed on this pane: KiCad ready but without a frame of the kind the pane wants: it is asked for one (a fresh frame). @@ -1342,6 +1344,11 @@ impl KicadPane { } } nc::note_pane_keys(self.keys); + // the Linux focus proxy only where this pane's frame is on screen (in the page, placed): another viewer on show leaves the focus to it + let key = (self.toplevel, self.container.unwrap_or(0)); + if let Some(old) = self.screen_key.filter(|k| *k != key) { nc::note_pane_on_screen(old.0, old.1, false); } + self.screen_key = Some(key); + nc::note_pane_on_screen(key.0, key.1, self.in_page && self.last_place.is_some_and(|(_, _, s)| s.is_some())); // the shield never re-raises while the F5 guard is armed (pre-warm or a hand-in being verified) let f5_armed = self.prewarm_guard.is_some() || self.verify_handin.is_some(); if let Some(c) = self.container { nc::shield_tick(c, if cfg!(windows) && self.info["mode"] == "fork" && !(f5_armed && self.keys) { self.want() } else { None }); } @@ -2025,7 +2032,7 @@ impl KicadPane { "in_gpui_tree": shown_meta.map_or(false, |(_, _, _, ok)| ok)}) } } -impl Drop for KicadPane { fn drop(&mut self) { self.detach(); } } +impl Drop for KicadPane { fn drop(&mut self) { if let Some((t, c)) = self.screen_key.take() { nc::note_pane_on_screen(t, c, false); } self.detach(); } } /// At quit (the window's close, the app's quit): KiCad's holders out of every container of this window (nc::release_holders), before the /// process ends and the X server destroys its windows with them. diff --git a/native/gpui/src/viewers/native_child.rs b/native/gpui/src/viewers/native_child.rs index 9bc5c7727c..9d181736a6 100644 --- a/native/gpui/src/viewers/native_child.rs +++ b/native/gpui/src/viewers/native_child.rs @@ -108,6 +108,26 @@ pub fn floating_dialog(style: u32, class: &str) -> bool { #[derive(Clone, Debug, PartialEq)] pub struct Frame { pub window: u64, pub title: String, pub kind: &'static str } +/// Which top-levels have a KiCad pane's frame on screen now (the pane in the page, its frame placed): the Linux focus proxy is used only +/// there. Elsewhere nothing of KiCad is under the pointer, the descendant rule cannot give it a key, and the focus stays where GPUI and the +/// Browser tab put it, as before #1505 (a click into the Browser's page with a pane hidden in the window was moved off the page). +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +/// Keyed by the top-level and, within it, by the native child (a pane's container; the Browser's page can register its own): the +/// top-level counts as on screen while any of its children is. +#[derive(Debug, Default)] +pub(crate) struct PaneScreens(std::collections::HashMap>); +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +impl PaneScreens { + /// Child `child` of top-level `top` on screen or not: true when this brings the top-level on screen (none of its children was; the + /// redirect resumes: a focus left on the top-level itself moves to its proxy) + pub(crate) fn set(&mut self, top: u64, child: u64, on: bool) -> bool { + let was = self.on(top); + if on { self.0.entry(top).or_default().insert(child); } else if let Some(s) = self.0.get_mut(&top) { s.remove(&child); if s.is_empty() { self.0.remove(&top); } } + !was && self.on(top) + } + pub(crate) fn on(&self, top: u64) -> bool { self.0.get(&top).is_some_and(|s| !s.is_empty()) } +} + /// The Linux focus proxy's redirect cap (#1519): at most `per` redirects of the focus to the proxy within `span`; one more starts a rest of /// `rest` with the focus left on the top-level, and when the rest has run out one re-check is due (the pane's tick moves a focus still /// left on the top-level to the proxy again, counted as the next burst's first). Pure, so its cycle is tested on every platform. @@ -194,7 +214,7 @@ mod imp { let _ = c.change_window_attributes(top, &ChangeWindowAttributesAux::new().event_mask(EventMask::from(mask | u32::from(EventMask::FOCUS_CHANGE)))); proxies().lock().unwrap().insert(top, id); // the focus already on the top-level (it had the keyboard before the pane came: an automatic attach) has no FocusIn to come: moved now - if !PANE_KEYS.load(std::sync::atomic::Ordering::SeqCst) && c.get_input_focus().ok().and_then(|r| r.reply().ok()).is_some_and(|r| r.focus == top) { + if !PANE_KEYS.load(std::sync::atomic::Ordering::SeqCst) && pane_on_screen(top) && c.get_input_focus().ok().and_then(|r| r.reply().ok()).is_some_and(|r| r.focus == top) { let _ = c.set_input_focus(InputFocus::PARENT, id, x11rb::CURRENT_TIME); } let _ = c.flush(); @@ -213,8 +233,21 @@ mod imp { if tripped { eprintln!("GPUI_LOG {}", serde_json::json!({"kicad_focus_proxy_rest": {"redirects": REDIRECTS_PER, "within_ms": REDIRECT_SPAN.as_millis() as u64, "rest_ms": REDIRECT_REST.as_millis() as u64}})); } ok } - /// The window the keyboard goes to when it is Backplane's in top-level `w`: its focus proxy, else `w` itself - fn home_of(w: u32) -> u32 { proxies().lock().unwrap().get(&w).copied().unwrap_or(w) } + static SCREENS: std::sync::LazyLock> = std::sync::LazyLock::new(Default::default); + fn pane_on_screen(top: u32) -> bool { SCREENS.lock().unwrap_or_else(|e| e.into_inner()).on(top as u64) } + /// The pane's tick: whether its frame (`child`: its container) is on screen in top-level `top` (in the page, placed). Coming on screen with the focus on the + /// top-level itself and Backplane holding the keys, the focus moves to the proxy (the redirect resumes) + pub fn note_pane_on_screen(top: u64, child: u64, on: bool) { + if top == 0 { return } + let came = SCREENS.lock().unwrap_or_else(|e| e.into_inner()).set(top, child, on); + if !came || PANE_KEYS.load(std::sync::atomic::Ordering::SeqCst) { return } + let Some(g) = conn() else { return }; let c = g.as_ref().unwrap(); + let Some(p) = proxies().lock().unwrap().get(&(top as u32)).copied() else { return }; + if c.get_input_focus().ok().and_then(|r| r.reply().ok()).is_some_and(|r| r.focus == top as u32) { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); let _ = c.flush(); } + } + /// The window the keyboard goes to when it is Backplane's in top-level `w`: its focus proxy while a pane's frame is on screen there, + /// else `w` itself + fn home_of(w: u32) -> u32 { if !pane_on_screen(w) { return w } proxies().lock().unwrap().get(&w).copied().unwrap_or(w) } /// Every child of `old` (stock KiCad's frames, or the fork's holder) moved into `new`, unmapped where it was not shown: a pane moved to /// another window (popped out, or back in) takes KiCad with it, its state untouched. pub fn move_frames(old: u64, new: u64) { @@ -683,7 +716,7 @@ mod imp { if !kicad && CAP.lock().unwrap_or_else(|e| e.into_inner()).recheck(std::time::Instant::now()) { let Some(g) = conn() else { return }; let c = g.as_ref().unwrap(); let f = c.get_input_focus().ok().and_then(|r| r.reply().ok()).map(|r| r.focus); - let p = f.and_then(|f| proxies().lock().unwrap().get(&f).copied()); + let p = f.filter(|&f| pane_on_screen(f)).and_then(|f| proxies().lock().unwrap().get(&f).copied()); if let Some(p) = p.filter(|_| redirect_allowed()) { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); let _ = c.flush(); } if std::env::var_os("BACKPLANE_KICAD_DEBUG").is_some() { eprintln!("GPUI_LOG {}", serde_json::json!({"kicad_focus_proxy_recheck": {"focus": f, "moved_to": p}})); } return; @@ -692,7 +725,7 @@ mod imp { if kicad || !was { return } let Some(g) = conn() else { return }; let c = g.as_ref().unwrap(); let Some(f) = c.get_input_focus().ok().and_then(|r| r.reply().ok()).map(|r| r.focus) else { return }; - if let Some(p) = proxies().lock().unwrap().get(&f).copied() { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); let _ = c.flush(); } + if let Some(p) = proxies().lock().unwrap().get(&f).copied().filter(|_| pane_on_screen(f)) { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); let _ = c.flush(); } } /// Windows only (the F3 input shield): Linux has its button grab (grab_click). pub fn shield(_container: u64, _on: bool) {} @@ -860,7 +893,7 @@ mod imp { // 1 of 20 on a real desktop). The pane reads its keys from the X focus (has_focus of its container), and a focus on the // top-level or on its proxy reads the same there; a hand-in that follows focuses KiCad's frame from either. x11rb::protocol::Event::FocusIn(f) => { - let proxy = proxies().lock().unwrap().get(&f.event).copied(); + let proxy = proxies().lock().unwrap().get(&f.event).copied().filter(|_| pane_on_screen(f.event)); if let Some(p) = proxy { if c.get_input_focus().ok().and_then(|r| r.reply().ok()).is_some_and(|r| r.focus == f.event) && redirect_allowed() { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); @@ -1403,6 +1436,7 @@ mod imp { /// The pane's view of who has the keys (true: KiCad's shown editor), noted each tick, for the reclaim's decision. static PANE_KEYS: AtomicBool = AtomicBool::new(false); pub fn note_pane_keys(kicad: bool) { PANE_KEYS.store(kicad, Ordering::SeqCst); } + pub fn note_pane_on_screen(_top: u64, _child: u64, _on: bool) {} /// When a button last went down in Backplane's own window outside the pane (`button_over`): the person taking the /// keys to Backplane, as opposed to focus drifting there on its own (a pre-warmed frame's activation ending). static BACKPLANE_PRESS_AT: AtomicU64 = AtomicU64::new(0); @@ -2597,6 +2631,7 @@ mod imp { pub fn activated_after_close(_main: u64, _popped: u64) -> bool { true } pub fn keep_focus(_top: u64) -> bool { false } pub fn note_pane_keys(_kicad: bool) {} + pub fn note_pane_on_screen(_top: u64, _child: u64, _on: bool) {} pub fn shield(_container: u64, _on: bool) {} pub fn shield_tick(_container: u64, _shown: Option) {} pub fn backplane_press_ms_ago() -> u64 { u64::MAX } @@ -2628,6 +2663,17 @@ pub use imp::*; #[cfg(test)] mod tests { + #[test] fn the_focus_proxy_is_used_only_where_a_panes_frame_is_on_screen() { + let mut s = super::PaneScreens::default(); + assert!(!s.on(7), "no pane on screen: the focus stays where GPUI and the Browser put it"); + assert!(s.set(7, 70, true), "coming on screen: the redirect resumes (a focus on the top-level moves to the proxy)"); + assert!(s.on(7) && !s.on(8), "per top-level: another window's pane is its own"); + assert!(!s.set(7, 70, true), "still on screen: no new transition (the tick does not move the focus again)"); + assert!(!s.set(7, 71, true), "a second child on screen in the same window: already on, no transition"); + assert!(!s.set(7, 70, false) && s.on(7), "one child off, the other still on screen: still on"); + assert!(!s.set(7, 71, false) && !s.on(7), "the last child off (another viewer on show): off, nothing moved"); + assert!(s.set(7, 70, true), "back in the page: on again"); + } #[test] fn the_focus_proxy_cap_trips_rests_rechecks_once_and_counts_a_new_burst() { use std::time::{Duration, Instant}; let ms = Duration::from_millis; diff --git a/native/gpui/src/viewers/native_child_mac.rs b/native/gpui/src/viewers/native_child_mac.rs index dfe9d9a9da..42a927eea9 100644 --- a/native/gpui/src/viewers/native_child_mac.rs +++ b/native/gpui/src/viewers/native_child_mac.rs @@ -296,6 +296,7 @@ pub fn yield_to(w: u64) { } pub fn activated_after_close(_main: u64, _popped: u64) -> bool { true } pub fn note_pane_keys(_kicad: bool) {} +pub fn note_pane_on_screen(_top: u64, _child: u64, _on: bool) {} pub fn shield(_container: u64, _on: bool) {} pub fn shield_tick(_container: u64, _shown: Option) {} pub fn backplane_press_ms_ago() -> u64 { u64::MAX } diff --git a/test/kicad_embed/embed_pointer_keys_browser_e2e.sh b/test/kicad_embed/embed_pointer_keys_browser_e2e.sh index 9c4a7dc918..78cce3cd04 100644 --- a/test/kicad_embed/embed_pointer_keys_browser_e2e.sh +++ b/test/kicad_embed/embed_pointer_keys_browser_e2e.sh @@ -23,11 +23,13 @@ start_xvfb_and_wm export BACKPLANE_KICAD_DEBUG=1 start_hub PAGE='data:text/html,

keys

' +# (the page as a JSON string: it has quotes of its own) +PAGE_JSON=$(python3 -c 'import json,sys; print(json.dumps(sys.argv[1]))' "$PAGE") if [ "$WITH_PANE" = 1 ]; then OPEN='{"select":0},{"wait":1500},{"click":"rail:Board"},{"wait":1500},{"click":"ui:Edit"},{"kicad":{"wait_painted":120000,"kind":"PCB","tag":"up"}},{"wait":3000},' else OPEN='{"select":0},{"wait":2000},'; fi cat >"$OUT/script.json" </dev/null; sleep 1.0; say "a click in th type_word ctl; sleep 0.4 python3 "$XINPUT_PY" move "$GX" "$GY" >/dev/null; sleep 0.8; say "pointer over the page" type_word pg; sleep 0.8 +python3 "$HERE/lib/focus_trace.py" watch 3 >"$OUT/page-click-focus.log" 2>&1 & FT=$!; sleep 0.2 python3 "$XINPUT_PY" click "$GX" "$GY" >/dev/null; sleep 1.0; say "a click in the page" +wait "$FT"; echo "INFO the focus through the page click (ms, window): $(tr '\n' ' ' <"$OUT/page-click-focus.log")" type_word in; sleep 0.8 python3 "$XINPUT_PY" click "$CX" "$CY" >/dev/null; sleep 1.0 python3 "$XINPUT_PY" move "$GX" "$GY" >/dev/null; sleep 0.8; say "back in the composer, pointer over the page" @@ -93,6 +97,8 @@ grep -o '"window_active":[a-z]*' "$OUT/client.log" | uniq -c | tr '\n' ' ' | sed has() { python3 -c 'import json,sys; print(1 if sys.argv[2] in (json.loads(sys.argv[1]) or "") else 0)' "$1" "$2"; } check "B1 control: keys over the composer land in it" "$(has "$TEXT" ctl)" "$TEXT" check "B2 with the pointer over the page (no click) the keys land in the composer" "$(has "$TEXT" ctlpg)" "composer $TEXT, page $TITLE" -check "B3 a click in the page gives it the keyboard (the page logs them)" "$(has "$TITLE" in)" "page $TITLE" +# (the page's log is its title "k:"; a page whose script never ran keeps its URL as the title, which may contain any letters) +logged() { python3 -c 'import json,sys; t=json.loads(sys.argv[1]) or ""; print(1 if t.startswith("k:") and sys.argv[2] in t[2:] else 0)' "$1" "$2"; } +check "B3 a click in the page gives it the keyboard (the page logs them)" "$(logged "$TITLE" in)" "page $TITLE" check "B4 back in the composer, the pointer over the page: keys in the composer" "$(has "$TEXT" bk)" "composer $TEXT, page $TITLE" finish_suite "embed_pointer_keys_browser_e2e (with pane $WITH_PANE)" From 0269f2af6691444b6910260471093d8fd4b5ec57 Mon Sep 17 00:00:00 2001 From: i2cjak Date: Sat, 21 Nov 2026 01:11:48 -0500 Subject: [PATCH 3/4] kicad pane: on screen for the focus proxy at the placement, before the map; the key taken back in detach(); the came move counted against the redirect cap; pane-side tests (#1556 review 2, 3, 4) The tick reported the frame on screen up to 40 ms after it was mapped: a key typed in between, the pointer resting where the pane appeared, went to KiCad. A pane whose container another takes over (take_kept) could remove that pane's entry from its own late tick or Drop: detach() now takes its key back and forgets it. pane_shows and screen_rekey are pure and tested. Co-Authored-By: Claude Opus 5.5 --- native/gpui/src/viewers/kicad_pane.rs | 41 ++++++++++++++++++++++--- native/gpui/src/viewers/native_child.rs | 3 +- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/native/gpui/src/viewers/kicad_pane.rs b/native/gpui/src/viewers/kicad_pane.rs index 3ee3582928..b2fc5c6b9f 100644 --- a/native/gpui/src/viewers/kicad_pane.rs +++ b/native/gpui/src/viewers/kicad_pane.rs @@ -401,6 +401,12 @@ pub fn raise_step(above: bool, on_screen: bool, st: &mut RaiseState, buried: &mu /// or the hub; another thread; Files, Mech, Browser, Gerbers; a Diff over it) hides it (#1199). Shown again only once the hitbox is /// drawn: there is a rect to show it at. `covered`: a screen of Backplane's drawn over the whole pane (Settings, the palette, the /// lightbox, Improvements: `covers_pane`): macOS cannot cut holes in KiCad's frame, which would stay above it, so it is hidden too. +/// Whether a pane's frame is on screen (the Linux focus proxy's condition): the pane in the page, placed somewhere, a frame on show +pub fn pane_shows(in_page: bool, placed: Option<(nc::R, Option)>) -> bool { in_page && placed.is_some_and(|(_, show)| show.is_some()) } + +/// The on-screen key moving to `key`: the old one to take back (when it differs) and the key now held +pub fn screen_rekey(old: Option<(u64, u64)>, key: (u64, u64)) -> (Option<(u64, u64)>, (u64, u64)) { (old.filter(|k| *k != key), key) } + pub fn mac_shown(show: bool, host_visible: bool, hitbox: bool, in_page: bool, covered: bool, dialog: bool, was_visible: bool) -> bool { show && host_visible && (!covered || (dialog && was_visible)) && (hitbox || (was_visible && (in_page || dialog))) } @@ -1322,6 +1328,14 @@ impl KicadPane { eprintln!("GPUI_LOG {}", json!({"kicad_place": at.map(|r| [r.x, r.y, r.w, r.h]), "visible": at.is_some() && show.is_some(), "changed": pr != at, "frame": show, "frame_changed": ps != show, "kind": kind, "container": c, "local": local, "why": why, "noop": noop, "at_ms": crate::screens::kcomment::epoch_ms() as u64})); } + /// This pane's frame on screen or not, for the Linux focus proxy (nc::note_pane_on_screen), under (top-level, `container`): the key + /// last reported is taken back first when it changes (a rehome, a pop-out) + fn report_screen(&mut self, container: u64, on: bool) { + let (off, key) = screen_rekey(self.screen_key, (self.toplevel, container)); + if let Some((t, k)) = off { nc::note_pane_on_screen(t, k, false); } + self.screen_key = Some(key); + nc::note_pane_on_screen(key.0, key.1, on); + } fn want(&self) -> Option { if self.info.is_null() || (cfg!(windows) && self.info["mode"] == "stock") { return None } pick_local(self.wanted_kind(), &self.info, &self.frames, &self.ever_shown) } fn tick(&mut self, cx: &mut Context) { // Windows: Backplane in front with nothing holding the keyboard (a jump away from the editor it was lent from) gets it @@ -1345,10 +1359,8 @@ impl KicadPane { } nc::note_pane_keys(self.keys); // the Linux focus proxy only where this pane's frame is on screen (in the page, placed): another viewer on show leaves the focus to it - let key = (self.toplevel, self.container.unwrap_or(0)); - if let Some(old) = self.screen_key.filter(|k| *k != key) { nc::note_pane_on_screen(old.0, old.1, false); } - self.screen_key = Some(key); - nc::note_pane_on_screen(key.0, key.1, self.in_page && self.last_place.is_some_and(|(_, _, s)| s.is_some())); + let on = pane_shows(self.in_page, self.last_place.map(|(_, r, s)| (r, s))); + self.report_screen(self.container.unwrap_or(0), on); // the shield never re-raises while the F5 guard is armed (pre-warm or a hand-in being verified) let f5_armed = self.prewarm_guard.is_some() || self.verify_handin.is_some(); if let Some(c) = self.container { nc::shield_tick(c, if cfg!(windows) && self.info["mode"] == "fork" && !(f5_armed && self.keys) { self.want() } else { None }); } @@ -1643,7 +1655,7 @@ impl KicadPane { // it was, with no round trip and no frame of the pane's background if let Some(k) = take_kept(top, &self.project) { self.container = Some(k.container); self.ever_shown = k.ever_shown; self.frames = nc::frames(k.container); - if let (Some(r), Some(w)) = (k.rect, self.want()) { nc::place_local(k.container, Some(r), Some(w), &self.ever_shown, &mut self.last_root); self.log_place("kept", k.container, Some(r), Some(w), None, true, false); self.placed = Some((Some(r), Some(w), Vec::new())); self.last_place = Some((k.container, r, Some(w))); } + if let (Some(r), Some(w)) = (k.rect, self.want()) { self.report_screen(k.container, pane_shows(self.in_page, Some((r, Some(w))))); nc::place_local(k.container, Some(r), Some(w), &self.ever_shown, &mut self.last_root); self.log_place("kept", k.container, Some(r), Some(w), None, true, false); self.placed = Some((Some(r), Some(w), Vec::new())); self.last_place = Some((k.container, r, Some(w))); } self.handoff_over = true; // mapped in this very frame: nothing to hand over } else { self.container = nc::container_for(top, &self.project); @@ -1769,6 +1781,9 @@ impl KicadPane { // the switch) only maps the frame on show (raised, the others unmapped), with no configure and no synthetic ConfigureNotify; a // move of the pane on screen is still told by sync_root (rail-round4: three same-rect placements per Board<->Schematic switch) let shown_only = cfg!(target_os = "linux") && local && no_geometry(prev, at, show); + // on screen for the Linux focus proxy before the frame is mapped (the tick's report came up to 40 ms after it: a key typed in + // between, the pointer resting where the pane appeared, went to KiCad by the descendant rule) + self.report_screen(c, pane_shows(self.in_page, at.map(|r| (r, show)))); #[allow(unused_mut)] let mut noop = false; if shown_only { #[cfg(target_os = "linux")] @@ -1984,6 +1999,9 @@ impl KicadPane { nc::cancel_yield(); // the editor leaves the pane: a hand-in still waiting to fall back is void (nc::yield_to) #[cfg(target_os = "macos")] if let Some((x, y, w, h, true)) = self.host_sent.take() { self.host_rpc(json!({"project": self.project, "x": x, "y": y, "width": w, "height": h, "visible": false, "raise": false})); } + // off screen for the Linux focus proxy, and the key forgotten: a pane taking this container over (take_kept) owns it from now, and + // this pane's later tick or Drop must not take its entry away + if let Some((t, k)) = self.screen_key.take() { nc::note_pane_on_screen(t, k, false); } let Some(c) = self.container.take() else { return }; let prev = self.last_place.filter(|(lc, _, _)| *lc == c).map(|(_, r, s)| (Some(r), s)); nc::place(c, None, None, None, &mut self.last_root); self.log_place("leave", c, None, None, prev, false, false); self.last_place = None; nc::grab_escape(self.toplevel, false); @@ -2109,6 +2127,19 @@ impl Render for KicadPane { #[cfg(test)] mod tests { + #[test] fn the_focus_proxy_condition_is_a_frame_shown_in_the_page() { + use super::{pane_shows, screen_rekey}; + let r = super::nc::R { x: 0, y: 0, w: 10, h: 10 }; + assert!(pane_shows(true, Some((r, Some(5)))), "in the page, placed, a frame on show: on screen"); + assert!(!pane_shows(false, Some((r, Some(5)))), "another viewer on show (not in the page): off, whatever was placed last"); + assert!(!pane_shows(true, Some((r, None))), "placed with no frame on show: off"); + assert!(!pane_shows(true, None), "not placed (the frame unmapped): off"); + // the key: (top-level, container); a rehome or a pop-out moves it and the old one is taken back once + assert_eq!(screen_rekey(None, (1, 10)), (None, (1, 10)), "the first report: nothing to take back"); + assert_eq!(screen_rekey(Some((1, 10)), (1, 10)), (None, (1, 10)), "the same key: nothing taken back (no flapping)"); + assert_eq!(screen_rekey(Some((1, 10)), (2, 20)), (Some((1, 10)), (2, 20)), "popped out to another window: the old window's entry is taken back"); + assert_eq!(screen_rekey(Some((1, 10)), (1, 11)), (Some((1, 10)), (1, 11)), "another container in the same window: the old one taken back"); + } #[test] fn an_edits_waiting_hand_in_is_dropped_by_a_click_in_backplane() { assert!(super::hand_in_dropped(true, false, super::TurnedTo::Click)); assert!(!super::hand_in_dropped(true, true, super::TurnedTo::Click), "KiCad has the keys already: nothing waits"); diff --git a/native/gpui/src/viewers/native_child.rs b/native/gpui/src/viewers/native_child.rs index 9d181736a6..0055ded3a8 100644 --- a/native/gpui/src/viewers/native_child.rs +++ b/native/gpui/src/viewers/native_child.rs @@ -243,7 +243,8 @@ mod imp { if !came || PANE_KEYS.load(std::sync::atomic::Ordering::SeqCst) { return } let Some(g) = conn() else { return }; let c = g.as_ref().unwrap(); let Some(p) = proxies().lock().unwrap().get(&(top as u32)).copied() else { return }; - if c.get_input_focus().ok().and_then(|r| r.reply().ok()).is_some_and(|r| r.focus == top as u32) { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); let _ = c.flush(); } + // (counted against the redirect cap like every other move: a pane flapping on and off screen cannot trade the focus without bound) + if c.get_input_focus().ok().and_then(|r| r.reply().ok()).is_some_and(|r| r.focus == top as u32) && redirect_allowed() { let _ = c.set_input_focus(InputFocus::PARENT, p, x11rb::CURRENT_TIME); let _ = c.flush(); } } /// The window the keyboard goes to when it is Backplane's in top-level `w`: its focus proxy while a pane's frame is on screen there, /// else `w` itself From a3791901493ec0752bb899853da45f67d683d1f1 Mon Sep 17 00:00:00 2001 From: i2cjak Date: Sat, 21 Nov 2026 01:17:50 -0500 Subject: [PATCH 4/4] kicad pane (#1556 delta review): rehome takes the on-screen key back before the old container goes (the old window no longer redirects with no frame in it until the next tick); pane_shows and screen_rekey moved above mac_shown's doc comment; note_pane_on_screen's doc names every caller Co-Authored-By: Claude Opus 5.5 --- native/gpui/src/viewers/kicad_pane.rs | 15 +++++++++------ native/gpui/src/viewers/native_child.rs | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/native/gpui/src/viewers/kicad_pane.rs b/native/gpui/src/viewers/kicad_pane.rs index b2fc5c6b9f..f5d63b88c5 100644 --- a/native/gpui/src/viewers/kicad_pane.rs +++ b/native/gpui/src/viewers/kicad_pane.rs @@ -394,6 +394,12 @@ pub fn raise_step(above: bool, on_screen: bool, st: &mut RaiseState, buried: &mu /// `Above`: the frame is above ours with nothing but KiCad's own windows between. `Idle`: the frame or our window is not on screen. /// `OursBelow`: the frame is behind ours with only our own and KiCad's windows between: ours goes just below the frame. #[derive(Debug, PartialEq)] pub enum OrderPlan { Above, Idle, OursBelow, AskKicad } +/// Whether a pane's frame is on screen (the Linux focus proxy's condition): the pane in the page, placed somewhere, a frame on show +pub fn pane_shows(in_page: bool, placed: Option<(nc::R, Option)>) -> bool { in_page && placed.is_some_and(|(_, show)| show.is_some()) } + +/// The on-screen key moving to `key`: the old one to take back (when it differs) and the key now held +pub fn screen_rekey(old: Option<(u64, u64)>, key: (u64, u64)) -> (Option<(u64, u64)>, (u64, u64)) { (old.filter(|k| *k != key), key) } + /// macOS: whether KiCad's frame is to be on show over the pane, from the state alone (never our window's activity or focus): an editor /// wanted (`show`), our window seen (`host_visible`), and the pane's hitbox drawn this frame. A frame with no hitbox (our window inactive /// while KiCad has the keyboard draws such frames, d563d8a5ab) keeps a frame on show while the page still holds the editor (`in_page`) or @@ -401,12 +407,6 @@ pub fn raise_step(above: bool, on_screen: bool, st: &mut RaiseState, buried: &mu /// or the hub; another thread; Files, Mech, Browser, Gerbers; a Diff over it) hides it (#1199). Shown again only once the hitbox is /// drawn: there is a rect to show it at. `covered`: a screen of Backplane's drawn over the whole pane (Settings, the palette, the /// lightbox, Improvements: `covers_pane`): macOS cannot cut holes in KiCad's frame, which would stay above it, so it is hidden too. -/// Whether a pane's frame is on screen (the Linux focus proxy's condition): the pane in the page, placed somewhere, a frame on show -pub fn pane_shows(in_page: bool, placed: Option<(nc::R, Option)>) -> bool { in_page && placed.is_some_and(|(_, show)| show.is_some()) } - -/// The on-screen key moving to `key`: the old one to take back (when it differs) and the key now held -pub fn screen_rekey(old: Option<(u64, u64)>, key: (u64, u64)) -> (Option<(u64, u64)>, (u64, u64)) { (old.filter(|k| *k != key), key) } - pub fn mac_shown(show: bool, host_visible: bool, hitbox: bool, in_page: bool, covered: bool, dialog: bool, was_visible: bool) -> bool { show && host_visible && (!covered || (dialog && was_visible)) && (hitbox || (was_visible && (in_page || dialog))) } @@ -1139,6 +1139,9 @@ impl KicadPane { let Some(old) = self.container else { return }; if t == self.toplevel { return } let Some(new) = nc::container_for(t, &self.project) else { return }; + // off screen for the Linux focus proxy before the old container goes: the old window would else redirect with no frame in it until + // the next tick (as detach() does) + if let Some((tk, k)) = self.screen_key.take() { nc::note_pane_on_screen(tk, k, false); } if new != old { nc::move_frames(old, new); nc::destroy(old); } self.toplevel = t; self.container = Some(new); self.placed = None; self.unmapped_since = None; self.last_root = (i32::MIN, 0); // the editor in another window now (popped out, or back in): its keys are handed in when it is placed there diff --git a/native/gpui/src/viewers/native_child.rs b/native/gpui/src/viewers/native_child.rs index 0055ded3a8..7d819b55c0 100644 --- a/native/gpui/src/viewers/native_child.rs +++ b/native/gpui/src/viewers/native_child.rs @@ -235,8 +235,8 @@ mod imp { } static SCREENS: std::sync::LazyLock> = std::sync::LazyLock::new(Default::default); fn pane_on_screen(top: u32) -> bool { SCREENS.lock().unwrap_or_else(|e| e.into_inner()).on(top as u64) } - /// The pane's tick: whether its frame (`child`: its container) is on screen in top-level `top` (in the page, placed). Coming on screen with the focus on the - /// top-level itself and Backplane holding the keys, the focus moves to the proxy (the redirect resumes) + /// Whether a pane's frame (`child`: its container) is on screen in top-level `top` (in the page, placed), from the pane's placement, + /// tick, rehome, detach() and Drop. Coming on screen with the focus on the top-level itself and Backplane holding the keys, the focus moves to the proxy (the redirect resumes) pub fn note_pane_on_screen(top: u64, child: u64, on: bool) { if top == 0 { return } let came = SCREENS.lock().unwrap_or_else(|e| e.into_inner()).set(top, child, on);