Focus ring flips the dock tab so the focused surface is always mounted
Browse files at this commit49bd1d044546ae58a9e9477511379f5b43ec7d7eParent: 01c40d5- fix
- focus ring flips the dock tab so the focused surface is always mounted
4 changed files +57 -5
- crates/app/src/player/actions.rs +1 -1
- crates/app/src/tests/view.rs +18 -0
- crates/app/src/ui/dock_stance.rs +1 -2
- crates/app/src/ui/stance.rs +37 -2
crates/app/src/player/actions.rsmodified+1 -1
| @@ -173,7 +173,7 @@ impl Player { | |||
| 173 | // starts. Lands on the cycle's first stop (`stance::SURFACE_CYCLE` | 173 | // starts. Lands on the cycle's first stop (`stance::SURFACE_CYCLE` |
| 174 | // -- the dock); a second Tab from there walks it same as ever. | 174 | // -- the dock); a second Tab from there walks it same as ever. |
| 175 | ActionId::FocusPanels => { | 175 | ActionId::FocusPanels => { |
| 176 | - window.focus(&self.focus_dock); | 176 | + self.focus_surface(crate::ui::stance::Surface::Dock, window, cx); |
| 177 | } | 177 | } |
| 178 | // Nothing to cancel while nothing is exporting; the export guard in | 178 | // Nothing to cancel while nothing is exporting; the export guard in |
| 179 | // the key handler is what answers this one while there is. | 179 | // the key handler is what answers this one while there is. |
crates/app/src/tests/view.rsmodified+18 -0
| @@ -912,6 +912,24 @@ fn tab_cycles_dock_bench_inspector_and_wraps() { | |||
| 912 | assert_eq!(next_surface(Surface::Bench, true), Surface::Dock); | 912 | assert_eq!(next_surface(Surface::Bench, true), Surface::Dock); |
| 913 | } | 913 | } |
| 914 | 914 | ||
| 915 | +/// The defect this closes: the dock mounts only one of its two tabs at a | ||
| 916 | +/// time (`dock_stance::render`'s `match src_active`), so focusing | ||
| 917 | +/// `focus_dock`/`focus_inspector` while the other tab is showing lands on a | ||
| 918 | +/// handle with no tree node -- gpui then dispatches no key listener at all, | ||
| 919 | +/// keyboard input is dead until a mouse click. `Player::focus_surface` | ||
| 920 | +/// (`ui::stance.rs`) flips the tab first via this pure decision so the | ||
| 921 | +/// surface being entered is always the one mounted. | ||
| 922 | +#[test] | ||
| 923 | +fn surface_wants_src_active_mounts_the_tab_before_focus_lands_on_it() { | ||
| 924 | + use crate::ui::stance::{Surface, surface_wants_src_active}; | ||
| 925 | + | ||
| 926 | + assert_eq!(surface_wants_src_active(Surface::Dock), Some(true)); | ||
| 927 | + assert_eq!(surface_wants_src_active(Surface::Inspector), Some(false)); | ||
| 928 | + // Bench has no dock tab of its own -- entering it must not disturb | ||
| 929 | + // whichever tab the dock was last left on. | ||
| 930 | + assert_eq!(surface_wants_src_active(Surface::Bench), None); | ||
| 931 | +} | ||
| 932 | + | ||
| 915 | /// The root fallback guarantee (main.rs:1054's "every keybind hangs off the | 933 | /// The root fallback guarantee (main.rs:1054's "every keybind hangs off the |
| 916 | /// root FocusHandle"): a surface's own `on_key_down` answers Tab/Shift-Tab | 934 | /// root FocusHandle"): a surface's own `on_key_down` answers Tab/Shift-Tab |
| 917 | /// and nothing else -- `cx.stop_propagation()` is only ever reached from | 935 | /// and nothing else -- `cx.stop_propagation()` is only ever reached from |
crates/app/src/ui/dock_stance.rsmodified+1 -2
| @@ -1316,9 +1316,8 @@ fn cycle_on_key_down( | |||
| 1316 | let key = event.keystroke.key.as_str(); | 1316 | let key = event.keystroke.key.as_str(); |
| 1317 | if is_focus_cycle_key(key) { | 1317 | if is_focus_cycle_key(key) { |
| 1318 | let next = next_surface(surface, event.keystroke.modifiers.shift); | 1318 | let next = next_surface(surface, event.keystroke.modifiers.shift); |
| 1319 | - window.focus(this.focus_handle(next)); | 1319 | + this.focus_surface(next, window, cx); |
| 1320 | cx.stop_propagation(); | 1320 | cx.stop_propagation(); |
| 1321 | - cx.notify(); | ||
| 1322 | } else if is_focus_exit_key(key) { | 1321 | } else if is_focus_exit_key(key) { |
| 1323 | // Same exit as `stance.rs`'s bench handler: leaves the ring for | 1322 | // Same exit as `stance.rs`'s bench handler: leaves the ring for |
| 1324 | // the root handle rather than letting escape bubble to whatever | 1323 | // the root handle rather than letting escape bubble to whatever |
crates/app/src/ui/stance.rsmodified+37 -2
| @@ -65,6 +65,22 @@ pub(crate) fn is_focus_exit_key(key: &str) -> bool { | |||
| 65 | key == "escape" | 65 | key == "escape" |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | +/// The dock mounts only one of its two tabs at a time | ||
| 69 | +/// (`dock_stance::render`'s `match src_active`), so `focus_dock` only has a | ||
| 70 | +/// tree node under Sources and `focus_inspector` only under Clip -- focusing | ||
| 71 | +/// the unmounted one leaves gpui with a focused handle no node answers to, | ||
| 72 | +/// and every key dies until the next mouse click. Answers "does entering | ||
| 73 | +/// `surface` require flipping the dock tab first", pure so it's testable | ||
| 74 | +/// without a `Window`: `Some(true)`/`Some(false)` is the `dock_src_active` | ||
| 75 | +/// the surface needs mounted, `None` (bench) means leave the tab alone. | ||
| 76 | +pub(crate) fn surface_wants_src_active(surface: Surface) -> Option<bool> { | ||
| 77 | + match surface { | ||
| 78 | + Surface::Dock => Some(true), | ||
| 79 | + Surface::Inspector => Some(false), | ||
| 80 | + Surface::Bench => None, | ||
| 81 | + } | ||
| 82 | +} | ||
| 83 | + | ||
| 68 | impl Player { | 84 | impl Player { |
| 69 | /// The `FocusHandle` a [`Surface`] paints its ring on and Tab/Shift-Tab | 85 | /// The `FocusHandle` a [`Surface`] paints its ring on and Tab/Shift-Tab |
| 70 | /// moves to next -- one door, so `next_surface`'s answer and the handle | 86 | /// moves to next -- one door, so `next_surface`'s answer and the handle |
| @@ -76,6 +92,26 @@ impl Player { | |||
| 76 | Surface::Inspector => &self.focus_inspector, | 92 | Surface::Inspector => &self.focus_inspector, |
| 77 | } | 93 | } |
| 78 | } | 94 | } |
| 95 | + | ||
| 96 | + /// One door onto every ring move: flips the dock tab first | ||
| 97 | + /// ([`surface_wants_src_active`]) so `surface`'s handle is always | ||
| 98 | + /// mounted before `window.focus` lands on it, then persists the flip | ||
| 99 | + /// exactly as the tab's own click does (`dock_stance::dock_tab`'s | ||
| 100 | + /// `on_click`), so the ring and a mouse click can't disagree about | ||
| 101 | + /// which tab was last chosen. | ||
| 102 | + pub(crate) fn focus_surface( | ||
| 103 | + &mut self, | ||
| 104 | + surface: Surface, | ||
| 105 | + window: &mut Window, | ||
| 106 | + cx: &mut Context<Self>, | ||
| 107 | + ) { | ||
| 108 | + if let Some(src_active) = surface_wants_src_active(surface) { | ||
| 109 | + self.dock_src_active = src_active; | ||
| 110 | + crate::ui::dock_stance::save(src_active); | ||
| 111 | + } | ||
| 112 | + window.focus(self.focus_handle(surface)); | ||
| 113 | + cx.notify(); | ||
| 114 | + } | ||
| 79 | } | 115 | } |
| 80 | 116 | ||
| 81 | /// Left rail, full height (DESIGN §5). | 117 | /// Left rail, full height (DESIGN §5). |
| @@ -608,9 +644,8 @@ fn bench( | |||
| 608 | let key = event.keystroke.key.as_str(); | 644 | let key = event.keystroke.key.as_str(); |
| 609 | if is_focus_cycle_key(key) { | 645 | if is_focus_cycle_key(key) { |
| 610 | let next = next_surface(Surface::Bench, event.keystroke.modifiers.shift); | 646 | let next = next_surface(Surface::Bench, event.keystroke.modifiers.shift); |
| 611 | - window.focus(this.focus_handle(next)); | 647 | + this.focus_surface(next, window, cx); |
| 612 | cx.stop_propagation(); | 648 | cx.stop_propagation(); |
| 613 | - cx.notify(); | ||
| 614 | } else if is_focus_exit_key(key) { | 649 | } else if is_focus_exit_key(key) { |
| 615 | // Leaves the ring rather than deleting it: the root handle | 650 | // Leaves the ring rather than deleting it: the root handle |
| 616 | // gets focus back, and a second escape there is what does | 651 | // gets focus back, and a second escape there is what does |