From c1186ad55e20f9fa579211e324c95b1dae7f4378 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Fri, 20 Nov 2026 05:21:09 -0500 Subject: [PATCH] use a builder for PassphraseInput --- src/components/passphrase_input.rs | 87 +++++++++++++++++++----------- src/screens/credentials.rs | 3 +- src/screens/unlock.rs | 6 +-- src/screens/welcome.rs | 10 +++- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/src/components/passphrase_input.rs b/src/components/passphrase_input.rs index 7e78be1..dd8c89c 100644 --- a/src/components/passphrase_input.rs +++ b/src/components/passphrase_input.rs @@ -8,9 +8,6 @@ use gpui_omarchy::{ButtonVariant, icon_button, input}; /// A masked passphrase field with a show/hide toggle. -/// -/// The `show` flag is passed in from outside so that it can be shared -/// (e.g. passphrase and confirm-passphrase inputs should share it). pub struct PassphraseInput { id: SharedString, state: Entity, @@ -29,38 +26,68 @@ pub enum PassphraseInputEvent { impl EventEmitter for PassphraseInput {} -impl PassphraseInput { - pub fn new(id: impl Into, show: Entity, window: &mut Window, cx: &mut Context) -> Self { - let masked = !*show.read(cx); - let state = cx.new(|cx| { - InputState::new(window, cx) - .masked(masked) - .placeholder("correct horse battery staple") - }); +pub struct PassphraseInputBuilder { + id: SharedString, + show: Option>, + placeholder: SharedString, +} - // keep this field's masking in sync with the shared flag - let sync_mask = cx.observe_in(&show, window, |this, show, window, cx| { +impl PassphraseInputBuilder { + /// Share the show/hide flag with other inputs (e.g. passphrase and confirm-passphrase). + pub fn show(mut self, show: Entity) -> Self { + self.show = Some(show); + self + } + + pub fn placeholder(mut self, placeholder: impl Into) -> Self { + self.placeholder = placeholder.into(); + self + } + + pub fn build(self, window: &mut Window, cx: &mut App) -> Entity { + let show = self.show.unwrap_or_else(|| cx.new(|_| false)); + cx.new(|cx| { let masked = !*show.read(cx); - this.state.update(cx, |state, cx| state.set_masked(masked, window, cx)); - cx.notify(); - }); + let state = cx.new(|cx| { + InputState::new(window, cx) + .masked(masked) + .placeholder(self.placeholder) + }); - // re-emit the subset of input events that consumers care about - let forward = cx.subscribe(&state, |_, _, event: &InputEvent, cx| { - let event = match event { - InputEvent::PressEnter { .. } => PassphraseInputEvent::Submit, - InputEvent::Change => PassphraseInputEvent::Change, - InputEvent::Blur => PassphraseInputEvent::Blur, - InputEvent::Focus => return, - }; - cx.emit(event); - }); + // keep this field's masking in sync with the shared flag + let sync_mask = cx.observe_in(&show, window, |this: &mut PassphraseInput, show, window, cx| { + let masked = !*show.read(cx); + this.state.update(cx, |state, cx| state.set_masked(masked, window, cx)); + cx.notify(); + }); - Self { + // re-emit the subset of input events that consumers care about + let forward = cx.subscribe(&state, |_, _, event: &InputEvent, cx| { + let event = match event { + InputEvent::PressEnter { .. } => PassphraseInputEvent::Submit, + InputEvent::Change => PassphraseInputEvent::Change, + InputEvent::Blur => PassphraseInputEvent::Blur, + InputEvent::Focus => return, + }; + cx.emit(event); + }); + + PassphraseInput { + id: self.id, + state, + show, + _subscriptions: vec![sync_mask, forward], + } + }) + } +} + +impl PassphraseInput { + pub fn builder(id: impl Into) -> PassphraseInputBuilder { + PassphraseInputBuilder { id: id.into(), - state, - show, - _subscriptions: vec![sync_mask, forward], + show: None, + placeholder: SharedString::default(), } } diff --git a/src/screens/credentials.rs b/src/screens/credentials.rs index b3a9528..edbfc0e 100644 --- a/src/screens/credentials.rs +++ b/src/screens/credentials.rs @@ -54,8 +54,7 @@ impl CredentialsScreen { fn add_aws(&mut self, window: &mut Window, cx: &mut Context) { let name = cx.new(|cx| InputState::new(window, cx)); let access_key_id = cx.new(|cx| InputState::new(window, cx)); - let show = cx.new(|_| false); - let secret_access_key = cx.new(|cx| PassphraseInput::new("aws-secret", show, window, cx)); + let secret_access_key = PassphraseInput::builder("aws-secret").build(window, cx); name.update(cx, |name, cx| name.focus(window, cx)); let mut _subscriptions = Vec::new(); diff --git a/src/screens/unlock.rs b/src/screens/unlock.rs index 1b6c3f3..6b611c2 100644 --- a/src/screens/unlock.rs +++ b/src/screens/unlock.rs @@ -22,9 +22,9 @@ pub struct UnlockScreen { impl UnlockScreen { pub fn new(window: &mut Window, cx: &mut Context) -> Self { - // nothing else shares the show/hide flag, so it's private to this input - let show = cx.new(|_| false); - let passphrase = cx.new(|cx| PassphraseInput::new("passphrase", show, window, cx)); + let passphrase = PassphraseInput::builder("passphrase") + .placeholder("correct horse battery staple") + .build(window, cx); passphrase.update(cx, |input, cx| input.focus(window, cx)); let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| { diff --git a/src/screens/welcome.rs b/src/screens/welcome.rs index 70746e1..e017894 100644 --- a/src/screens/welcome.rs +++ b/src/screens/welcome.rs @@ -26,8 +26,14 @@ pub struct WelcomeScreen { impl WelcomeScreen { pub fn new(window: &mut Window, cx: &mut Context) -> Self { let show = cx.new(|_| false); - let passphrase = cx.new(|cx| PassphraseInput::new("passphrase", show.clone(), window, cx)); - let confirm = cx.new(|cx| PassphraseInput::new("confirm", show, window, cx)); + let passphrase = PassphraseInput::builder("passphrase") + .show(show.clone()) + .placeholder("correct horse battery staple") + .build(window, cx); + let confirm = PassphraseInput::builder("confirm") + .show(show) + .placeholder("correct horse battery staple") + .build(window, cx); passphrase.update(cx, |input, cx| input.focus(window, cx)); let mut _subscriptions = Vec::new();