Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions native/gpui/src/viewers/kicad_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>)>) -> 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
Expand Down Expand Up @@ -669,6 +675,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<u64>)>,
/// 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<Subscription>,
/// 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<String>,
Expand Down Expand Up @@ -925,7 +933,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).
Expand Down Expand Up @@ -1131,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
Expand Down Expand Up @@ -1320,6 +1331,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<u64> { 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<Self>) {
// Windows: Backplane in front with nothing holding the keyboard (a jump away from the editor it was lent from) gets it
Expand All @@ -1342,6 +1361,9 @@ 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 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 }); }
Expand Down Expand Up @@ -1521,8 +1543,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)) {
Expand Down Expand Up @@ -1633,7 +1658,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);
Expand Down Expand Up @@ -1759,6 +1784,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")]
Expand Down Expand Up @@ -1974,6 +2002,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);
Expand Down Expand Up @@ -2022,7 +2053,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.
Expand Down Expand Up @@ -2099,6 +2130,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");
Expand Down
Loading
Loading