voelin-gpui: the password prompt ignores an empty field and Escape closes it
The Slint dialog (ChannelPasswordDialog) turns Join off and ignores Enter while the field is empty, keeping the dialog open, and closes on Escape. The prompt here took itself away before join_with_password refused an empty password, so Join or Enter on an empty field closed it without a join, and Escape did nothing (the field's own Escape passes the key on). The submit now reads the field first and leaves the prompt open when it is empty; Join is disabled while it is (the field's changes redraw the prompt); the prompt handles the Escape the field passes on and cancels. The prompt's test covers Join and Enter on an empty field, Escape (nothing sent, the channel no longer the one to join), and a typed password entered with Enter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c158bdda00
commit
bad5312bfd
2 files changed
+70
-12
No files matched your search
@@ -3,10 +3,12 @@
|
||||
//! `Update::password` when a channel with a password is joined and the one
|
||||
//! given this connection (if any) is not at hand, or was refused. It is a
|
||||
//! card over the window with a scrim; until the dialogs slice makes every
|
||||
//! dialog one, it is this view's own.
|
||||
//! 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.
|
||||
|
||||
use gpui_kit::component::Disableable as _;
|
||||
use gpui_kit::component::button::{Button, ButtonVariants as _};
|
||||
use gpui_kit::component::input::{Input, InputEvent, InputState};
|
||||
use gpui_kit::component::input::{Escape, Input, InputEvent, InputState};
|
||||
use gpui_kit::prelude::FluentBuilder as _;
|
||||
use gpui_kit::{
|
||||
App, AppContext as _, Context, Entity, FontWeight, InteractiveElement as _, IntoElement,
|
||||
@@ -32,10 +34,13 @@ impl ChannelPassword {
|
||||
let input = cx.new(|cx| {
|
||||
InputState::new(window, cx).placeholder(SharedString::from("Password")).masked(true)
|
||||
});
|
||||
cx.subscribe_in(&input, window, |_, _, event: &InputEvent, window, cx| {
|
||||
if matches!(event, InputEvent::PressEnter { .. }) {
|
||||
cx.subscribe_in(&input, window, |_, _, event: &InputEvent, window, cx| match event {
|
||||
InputEvent::PressEnter { .. } => {
|
||||
window.dispatch_action(Box::new(SubmitChannelPassword), cx);
|
||||
}
|
||||
// Join is there while there is something to join with.
|
||||
InputEvent::Change => cx.notify(),
|
||||
_ => {}
|
||||
})
|
||||
.detach();
|
||||
input.update(cx, |input, cx| input.focus(window, cx));
|
||||
@@ -52,7 +57,7 @@ impl ChannelPassword {
|
||||
self.wrong
|
||||
}
|
||||
|
||||
/// What the field holds.
|
||||
/// What the field holds. The prompt joins only with something in it.
|
||||
pub fn text(&self, cx: &App) -> String {
|
||||
self.input.read(cx).value().to_string()
|
||||
}
|
||||
@@ -67,9 +72,16 @@ impl Render for ChannelPassword {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let theme = VoelinTheme::global(cx).clone();
|
||||
let (c, m) = (&theme.colors, &theme.metrics);
|
||||
let empty = self.text(cx).is_empty();
|
||||
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)
|
||||
}))
|
||||
.absolute()
|
||||
.top(px(0.0))
|
||||
.left(px(0.0))
|
||||
@@ -127,11 +139,13 @@ impl Render for ChannelPassword {
|
||||
))
|
||||
.child(tagged(
|
||||
"password-join",
|
||||
Button::new("password-join").primary().label("Join").on_click(
|
||||
|_, window, cx| {
|
||||
Button::new("password-join")
|
||||
.primary()
|
||||
.label("Join")
|
||||
.disabled(empty)
|
||||
.on_click(|_, window, cx| {
|
||||
window.dispatch_action(Box::new(SubmitChannelPassword), cx)
|
||||
},
|
||||
),
|
||||
}),
|
||||
)),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -330,15 +330,21 @@ impl MainView {
|
||||
self.app.update(cx, |app, cx| app.open_member(client, cx));
|
||||
}
|
||||
|
||||
/// The prompt's Join: the password in its field goes with the join.
|
||||
/// The prompt's Join: the password in its field goes with the join. An
|
||||
/// empty field joins nothing and the prompt stays open, as the Slint dialog
|
||||
/// (its Join is off and its Enter ignored while the field is empty).
|
||||
fn submit_channel_password(
|
||||
&mut self,
|
||||
_: &SubmitChannelPassword,
|
||||
_: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let Some(prompt) = self.prompt.take() else { return };
|
||||
let Some(prompt) = self.prompt.as_ref() else { return };
|
||||
let password = prompt.read(cx).text(cx);
|
||||
if password.is_empty() {
|
||||
return;
|
||||
}
|
||||
self.prompt = None;
|
||||
self.app.update(cx, |app, cx| app.join_with_password(password, cx));
|
||||
cx.notify();
|
||||
}
|
||||
@@ -1119,7 +1125,9 @@ mod tests {
|
||||
}
|
||||
|
||||
/// A locked channel asks for its password first; the prompt's Join sends it
|
||||
/// with the move; one the server refuses asks again, in words.
|
||||
/// with the move; one the server refuses asks again, in words. As in the
|
||||
/// Slint dialog an empty field joins nothing and leaves the prompt open
|
||||
/// (Join and Enter), and Escape closes it.
|
||||
#[gpui::test]
|
||||
fn a_locked_channel_asks_for_its_password(cx: &mut TestAppContext) {
|
||||
let (shell, cx) = open(cx, Backend::in_memory().unwrap(), Switches::default());
|
||||
@@ -1136,6 +1144,27 @@ mod tests {
|
||||
("Officers".to_owned(), false)
|
||||
);
|
||||
|
||||
// Nothing in the field: Join and Enter do nothing and the prompt stays.
|
||||
click(cx, "password-join");
|
||||
cx.simulate_keystrokes("enter");
|
||||
frame(cx);
|
||||
assert!(at(cx, "password-prompt").is_some(), "an empty password closes nothing");
|
||||
assert!(shell.app.read_with(cx, |app, _| app.take_sent()).is_empty(), "and joins nothing");
|
||||
assert!(shell.app.read_with(cx, |app, _| app.vm.join_target.is_some()), "still asking");
|
||||
|
||||
// Escape closes it, and the channel is no longer the one to join.
|
||||
cx.simulate_keystrokes("escape");
|
||||
frame(cx);
|
||||
assert!(at(cx, "password-prompt").is_none(), "Escape closes the prompt");
|
||||
assert!(shell.app.read_with(cx, |app, _| app.take_sent()).is_empty());
|
||||
assert!(shell.app.read_with(cx, |app, _| app.vm.join_target.is_none()));
|
||||
|
||||
// Asked again, with something typed, Join sends it.
|
||||
double_click(cx, "tree-channel-7");
|
||||
let prompt = shell
|
||||
.view
|
||||
.read_with(cx, |view, _| view.password_prompt().cloned())
|
||||
.expect("the prompt is open again");
|
||||
cx.update(|window, cx| prompt.update(cx, |p, cx| p.set_text("secret", window, cx)));
|
||||
click(cx, "password-join");
|
||||
let sent = shell.app.read_with(cx, |app, _| app.take_sent());
|
||||
@@ -1166,6 +1195,20 @@ mod tests {
|
||||
assert!(at(cx, "password-prompt").is_none());
|
||||
assert!(shell.app.read_with(cx, |app, _| app.take_sent()).is_empty());
|
||||
assert!(shell.app.read_with(cx, |app, _| app.vm.join_target.is_none()));
|
||||
|
||||
// Typed and entered with the keyboard, the same.
|
||||
double_click(cx, "tree-channel-7");
|
||||
assert!(at(cx, "password-prompt").is_some());
|
||||
cx.simulate_input("again");
|
||||
cx.simulate_keystrokes("enter");
|
||||
frame(cx);
|
||||
let sent = shell.app.read_with(cx, |app, _| app.take_sent());
|
||||
assert!(
|
||||
matches!(sent.as_slice(), [Command::MoveToChannel { channel: 7, password, .. }]
|
||||
if password.as_deref() == Some("again")),
|
||||
"{sent:?}"
|
||||
);
|
||||
assert!(at(cx, "password-prompt").is_none(), "closed");
|
||||
}
|
||||
|
||||
/// The channels with a banner picture are drawn with the veil of the
|
||||
@@ -1192,6 +1235,7 @@ mod tests {
|
||||
assert!(at(cx, &format!("tree-channel-{}", plain.id)).is_some(), "it is on screen");
|
||||
assert!(at(cx, &format!("tree-scrim-{}", plain.id)).is_none(), "and has no veil");
|
||||
}
|
||||
|
||||
/// A person who changes channel makes the rows between the two places
|
||||
/// change and none outside them: the channels' counts, the person's row
|
||||
/// moved; a person who mutes their microphone changes one row.
|
||||
|
||||
Reference in new issue
Block a user