diff --git a/crates/voelin-gpui/src/lib.rs b/crates/voelin-gpui/src/lib.rs index 3fe8a43..438db8d 100644 --- a/crates/voelin-gpui/src/lib.rs +++ b/crates/voelin-gpui/src/lib.rs @@ -217,6 +217,7 @@ pub fn run() -> Result<()> { let closed_sessions = closed_sessions.clone(); move |cx| { gpui_kit::init(cx); + shell::bind_keys(cx); fonts::load(cx); // The mode and the text size are the settings', or a development // switch's. diff --git a/crates/voelin-gpui/src/shell/mod.rs b/crates/voelin-gpui/src/shell/mod.rs index 1fb78c7..56778ef 100644 --- a/crates/voelin-gpui/src/shell/mod.rs +++ b/crates/voelin-gpui/src/shell/mod.rs @@ -26,6 +26,12 @@ pub use form::ServerForm; pub use main_panel::{MainPanel, MainState}; pub use members::{MemberCardView, MembersPane}; pub use password::ChannelPassword; + +/// Bind the shell's keys (Escape in the password prompt): once, after the +/// widget library's own are (`gpui_kit::init`). +pub fn bind_keys(cx: &mut gpui_kit::App) { + password::bind_keys(cx); +} pub use rail::{RailView, ServerRail}; pub use sidebar::{Sidebar, SidebarView}; pub use toast::{TOAST_TIME, Toast}; diff --git a/crates/voelin-gpui/src/shell/password.rs b/crates/voelin-gpui/src/shell/password.rs index 7d44081..9642b13 100644 --- a/crates/voelin-gpui/src/shell/password.rs +++ b/crates/voelin-gpui/src/shell/password.rs @@ -5,15 +5,24 @@ //! card over the window with a scrim; until the dialogs slice makes every //! dialog one, it is this view's own. As in the Slint dialog, Join (and Enter) //! do nothing while the field is empty, and Escape closes the prompt. +//! +//! Escape is a key binding of the prompt's key context ([`bind_keys`]) that +//! dispatches [`CancelChannelPassword`], and the prompt is a focus target of +//! its own (its scrim, which the card is on): a click on the card, off the +//! field, takes the focus from the field, and a click outside any focus target +//! would hand it to the window's root, which the prompt is not under, so +//! without a target the context would not be on the dispatch path and Escape +//! would do nothing. With the field focused its own Escape (which has nothing +//! to undo in a password) passes the key on, and the binding closes the prompt. use gpui_kit::component::Disableable as _; use gpui_kit::component::button::{Button, ButtonVariants as _}; -use gpui_kit::component::input::{Escape, Input, InputEvent, InputState}; +use gpui_kit::component::input::{Input, InputEvent, InputState}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::{ - App, AppContext as _, Context, Entity, FontWeight, InteractiveElement as _, IntoElement, - ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled as _, Window, - div, px, + App, AppContext as _, Context, Entity, FocusHandle, FontWeight, InteractiveElement as _, + IntoElement, KeyBinding, ParentElement as _, Render, SharedString, + StatefulInteractiveElement as _, Styled as _, Window, div, px, }; use voelin_ui_vm::host::PasswordPrompt; @@ -21,11 +30,23 @@ use super::actions::{CancelChannelPassword, SubmitChannelPassword}; use super::widgets::tagged; use crate::theme::{VoelinTheme, hsla}; +/// The key context of the prompt. +const CONTEXT: &str = "ChannelPassword"; + +/// The keys of the prompt: Escape closes it. Bound once, at start, with the +/// widget library's own. +pub fn bind_keys(cx: &mut App) { + cx.bind_keys([KeyBinding::new("escape", CancelChannelPassword, Some(CONTEXT))]); +} + /// The prompt for the password of one channel. pub struct ChannelPassword { channel: String, wrong: bool, input: Entity, + /// What the prompt's scrim holds the focus with, so that Escape reaches the + /// prompt wherever in it the last click was. + focus: FocusHandle, } impl ChannelPassword { @@ -44,7 +65,7 @@ impl ChannelPassword { }) .detach(); input.update(cx, |input, cx| input.focus(window, cx)); - Self { channel: ask.name.clone(), wrong: ask.wrong, input } + Self { channel: ask.name.clone(), wrong: ask.wrong, input, focus: cx.focus_handle() } } /// The channel it asks for. @@ -76,12 +97,8 @@ impl Render for ChannelPassword { div() .id("password-prompt") .debug_selector(|| "password-prompt".into()) - .key_context("ChannelPassword") - // The field has the keys, and its Escape has nothing to undo and - // passes the key on: it closes the prompt. - .on_action(cx.listener(|_, _: &Escape, window, cx| { - window.dispatch_action(Box::new(CancelChannelPassword), cx) - })) + .key_context(CONTEXT) + .track_focus(&self.focus) .absolute() .top(px(0.0)) .left(px(0.0)) @@ -95,6 +112,7 @@ impl Render for ChannelPassword { .child( div() .id("password-card") + .debug_selector(|| "password-card".into()) .w(px(380.0)) .flex() .flex_col() diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index 166a083..efd1888 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -586,6 +586,7 @@ mod tests { ) -> (Shell, &mut VisualTestContext) { cx.update(|cx| { gpui_kit::init(cx); + crate::shell::bind_keys(cx); VoelinTheme::init(cx, Choice::Dark, WindowAppearance::Dark, 1.0); }); let (events, rx) = mpsc::unbounded_channel(); @@ -1214,6 +1215,39 @@ mod tests { assert!(at(cx, "password-prompt").is_none(), "closed"); } + /// Escape closes the password prompt wherever the focus is in it, not only + /// while its field has it: a click on the card, off the field, moves the + /// focus away from the field (to the card), and the key must still reach + /// the prompt. + #[gpui::test] + fn escape_closes_the_password_prompt_after_a_click_on_its_card(cx: &mut TestAppContext) { + let (shell, cx) = open(cx, Backend::in_memory().unwrap(), Switches::default()); + sample_server(&shell, cx); + shell.app.read_with(cx, |app, _| app.take_sent()); + let prompt_open = |shell: &Shell, cx: &mut VisualTestContext| { + shell.view.read_with(cx, |view, _| view.password_prompt().is_some()) + }; + for aim in ["the card's corner", "the card's disabled Join"] { + double_click(cx, "tree-channel-7"); + assert!(prompt_open(&shell, cx), "{aim}: asked for the password"); + if aim == "the card's corner" { + let card = at(cx, "password-card").expect("the card is on screen"); + let corner = card.origin + point(px(6.0), px(6.0)); + cx.simulate_click(corner, Default::default()); + frame(cx); + } else { + click(cx, "password-join"); + } + assert!(prompt_open(&shell, cx), "{aim}: a click inside the card closes nothing"); + cx.simulate_keystrokes("escape"); + frame(cx); + assert!(!prompt_open(&shell, cx), "{aim}: Escape closes the prompt"); + assert!(at(cx, "password-prompt").is_none()); + assert!(shell.app.read_with(cx, |app, _| app.vm.join_target.is_none())); + } + assert!(shell.app.read_with(cx, |app, _| app.take_sent()).is_empty(), "nothing joined"); + } + /// The channels with a banner picture are drawn with the veil of the /// surface colour over it (so that their text reads over any picture); the /// others have none. The picture covers the row's own background, so a diff --git a/docs/ui.md b/docs/ui.md index 0ff4614..5b7f361 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -1481,7 +1481,8 @@ What is on screen: password was not given this connection asks for it (a card over the window, Enter joins); a password the server refuses asks again, in words. As in the Slint dialog, Join is off and Enter does nothing while the field is empty - (the prompt stays), and Escape closes it without joining. + (the prompt stays), and Escape closes it without joining, wherever in the + prompt the last click was (below). #### Per-area signals and cached panes @@ -1863,7 +1864,19 @@ the slices that meet it; none of it is a defect of what exists today. start and takes the focus back when the element that had it is gone (`on_focus_lost`: the form closed, a button replaced by another). A later window (the Stream Studio's) does the same, and a new page's buttons dispatch - actions the root handles rather than listeners of their own. + actions the root handles rather than listeners of their own. A key that + belongs to an overlay needs the overlay on that path. The root's handle takes + the focus on every mouse down inside the window (GPUI transfers it to the + element under the pointer that has a handle, the root being under all of + them), and the overlay is a child of the root, not an ancestor of the root's + handle: a click on the overlay that no handle inside it takes leaves the + focus on the root, and the overlay's key context is off the path. The + password prompt therefore holds a focus handle of its own on its scrim (the + card is on it), and Escape is a key binding of its key context + (`shell::bind_keys`, called beside `gpui_kit::init`), not a listener on the + field's `Escape`: with the field focused its Escape passes the key on and the + binding closes the prompt, with anything else in the prompt focused the + binding is the only one there. - **The hotkey portal calls `tokio::spawn`** (`voelin-platform`, `hotkey/portal.rs`, `PortalBackend::new`): it panics where no tokio runtime is current, and a GPUI foreground task has none. The GPUI front end starts