use a builder for PassphraseInput
This commit is contained in:
1 parent
39b3f74068
commit
c1186ad55e
4 files changed
+69
-37
No files matched your search
@@ -8,9 +8,6 @@ use gpui_omarchy::{ButtonVariant, icon_button, input};
|
|||||||
|
|
||||||
|
|
||||||
/// A masked passphrase field with a show/hide toggle.
|
/// 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 {
|
pub struct PassphraseInput {
|
||||||
id: SharedString,
|
id: SharedString,
|
||||||
state: Entity<InputState>,
|
state: Entity<InputState>,
|
||||||
@@ -29,38 +26,68 @@ pub enum PassphraseInputEvent {
|
|||||||
|
|
||||||
impl EventEmitter<PassphraseInputEvent> for PassphraseInput {}
|
impl EventEmitter<PassphraseInputEvent> for PassphraseInput {}
|
||||||
|
|
||||||
impl PassphraseInput {
|
pub struct PassphraseInputBuilder {
|
||||||
pub fn new(id: impl Into<SharedString>, show: Entity<bool>, window: &mut Window, cx: &mut Context<Self>) -> Self {
|
id: SharedString,
|
||||||
let masked = !*show.read(cx);
|
show: Option<Entity<bool>>,
|
||||||
let state = cx.new(|cx| {
|
placeholder: SharedString,
|
||||||
InputState::new(window, cx)
|
}
|
||||||
.masked(masked)
|
|
||||||
.placeholder("correct horse battery staple")
|
|
||||||
});
|
|
||||||
|
|
||||||
// keep this field's masking in sync with the shared flag
|
impl PassphraseInputBuilder {
|
||||||
let sync_mask = cx.observe_in(&show, window, |this, show, window, cx| {
|
/// Share the show/hide flag with other inputs (e.g. passphrase and confirm-passphrase).
|
||||||
|
pub fn show(mut self, show: Entity<bool>) -> Self {
|
||||||
|
self.show = Some(show);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
|
||||||
|
self.placeholder = placeholder.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self, window: &mut Window, cx: &mut App) -> Entity<PassphraseInput> {
|
||||||
|
let show = self.show.unwrap_or_else(|| cx.new(|_| false));
|
||||||
|
cx.new(|cx| {
|
||||||
let masked = !*show.read(cx);
|
let masked = !*show.read(cx);
|
||||||
this.state.update(cx, |state, cx| state.set_masked(masked, window, cx));
|
let state = cx.new(|cx| {
|
||||||
cx.notify();
|
InputState::new(window, cx)
|
||||||
});
|
.masked(masked)
|
||||||
|
.placeholder(self.placeholder)
|
||||||
|
});
|
||||||
|
|
||||||
// re-emit the subset of input events that consumers care about
|
// keep this field's masking in sync with the shared flag
|
||||||
let forward = cx.subscribe(&state, |_, _, event: &InputEvent, cx| {
|
let sync_mask = cx.observe_in(&show, window, |this: &mut PassphraseInput, show, window, cx| {
|
||||||
let event = match event {
|
let masked = !*show.read(cx);
|
||||||
InputEvent::PressEnter { .. } => PassphraseInputEvent::Submit,
|
this.state.update(cx, |state, cx| state.set_masked(masked, window, cx));
|
||||||
InputEvent::Change => PassphraseInputEvent::Change,
|
cx.notify();
|
||||||
InputEvent::Blur => PassphraseInputEvent::Blur,
|
});
|
||||||
InputEvent::Focus => return,
|
|
||||||
};
|
|
||||||
cx.emit(event);
|
|
||||||
});
|
|
||||||
|
|
||||||
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<SharedString>) -> PassphraseInputBuilder {
|
||||||
|
PassphraseInputBuilder {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
state,
|
show: None,
|
||||||
show,
|
placeholder: SharedString::default(),
|
||||||
_subscriptions: vec![sync_mask, forward],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,8 +54,7 @@ impl CredentialsScreen {
|
|||||||
fn add_aws(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn add_aws(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let name = cx.new(|cx| InputState::new(window, cx));
|
let name = cx.new(|cx| InputState::new(window, cx));
|
||||||
let access_key_id = 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 = PassphraseInput::builder("aws-secret").build(window, cx);
|
||||||
let secret_access_key = cx.new(|cx| PassphraseInput::new("aws-secret", show, window, cx));
|
|
||||||
name.update(cx, |name, cx| name.focus(window, cx));
|
name.update(cx, |name, cx| name.focus(window, cx));
|
||||||
|
|
||||||
let mut _subscriptions = Vec::new();
|
let mut _subscriptions = Vec::new();
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ pub struct UnlockScreen {
|
|||||||
|
|
||||||
impl UnlockScreen {
|
impl UnlockScreen {
|
||||||
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||||
// nothing else shares the show/hide flag, so it's private to this input
|
let passphrase = PassphraseInput::builder("passphrase")
|
||||||
let show = cx.new(|_| false);
|
.placeholder("correct horse battery staple")
|
||||||
let passphrase = cx.new(|cx| PassphraseInput::new("passphrase", show, window, cx));
|
.build(window, cx);
|
||||||
passphrase.update(cx, |input, cx| input.focus(window, cx));
|
passphrase.update(cx, |input, cx| input.focus(window, cx));
|
||||||
|
|
||||||
let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| {
|
let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| {
|
||||||
|
|||||||
@@ -26,8 +26,14 @@ pub struct WelcomeScreen {
|
|||||||
impl WelcomeScreen {
|
impl WelcomeScreen {
|
||||||
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||||
let show = cx.new(|_| false);
|
let show = cx.new(|_| false);
|
||||||
let passphrase = cx.new(|cx| PassphraseInput::new("passphrase", show.clone(), window, cx));
|
let passphrase = PassphraseInput::builder("passphrase")
|
||||||
let confirm = cx.new(|cx| PassphraseInput::new("confirm", show, window, cx));
|
.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));
|
passphrase.update(cx, |input, cx| input.focus(window, cx));
|
||||||
|
|
||||||
let mut _subscriptions = Vec::new();
|
let mut _subscriptions = Vec::new();
|
||||||
|
|||||||
Reference in new issue
Block a user