101 lines
3.2 KiB
Rust
101 lines
3.2 KiB
Rust
use gpui_kit::assets::IconName;
|
|
use gpui_kit::base::input::{InputEvent, InputState};
|
|
use gpui_kit::{
|
|
App, AppContext, Context, Entity, EventEmitter, IntoElement, Render, SharedString, Styled,
|
|
Subscription, Window,
|
|
};
|
|
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<InputState>,
|
|
show: Entity<bool>,
|
|
_subscriptions: Vec<Subscription>,
|
|
}
|
|
|
|
pub enum PassphraseInputEvent {
|
|
/// The user pressed Enter in the field.
|
|
Submit,
|
|
/// The field's text changed.
|
|
Change,
|
|
/// The field lost focus.
|
|
Blur,
|
|
}
|
|
|
|
impl EventEmitter<PassphraseInputEvent> for PassphraseInput {}
|
|
|
|
impl PassphraseInput {
|
|
pub fn new(id: impl Into<SharedString>, show: Entity<bool>, window: &mut Window, cx: &mut Context<Self>) -> Self {
|
|
let masked = !*show.read(cx);
|
|
let state = cx.new(|cx| {
|
|
InputState::new(window, cx)
|
|
.masked(masked)
|
|
.placeholder("correct horse battery staple")
|
|
});
|
|
|
|
// keep this field's masking in sync with the shared flag
|
|
let sync_mask = cx.observe_in(&show, window, |this, show, window, cx| {
|
|
let masked = !*show.read(cx);
|
|
this.state.update(cx, |state, cx| state.set_masked(masked, window, cx));
|
|
cx.notify();
|
|
});
|
|
|
|
// 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);
|
|
});
|
|
|
|
Self {
|
|
id: id.into(),
|
|
state,
|
|
show,
|
|
_subscriptions: vec![sync_mask, forward],
|
|
}
|
|
}
|
|
|
|
pub fn value(&self, cx: &App) -> SharedString {
|
|
self.state.read(cx).value()
|
|
}
|
|
|
|
pub fn focus(&self, window: &mut Window, cx: &mut App) {
|
|
self.state.update(cx, |state, cx| state.focus(window, cx));
|
|
}
|
|
|
|
fn toggle_show(&mut self, cx: &mut Context<Self>) {
|
|
self.show.update(cx, |show, cx| {
|
|
*show = !*show;
|
|
cx.notify();
|
|
});
|
|
}
|
|
}
|
|
|
|
impl Render for PassphraseInput {
|
|
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let (icon, label) = if *self.show.read(cx) {
|
|
(IconName::EyeOff, "Hide passphrase")
|
|
}
|
|
else {
|
|
(IconName::Eye, "Show passphrase")
|
|
};
|
|
|
|
let toggle_id = SharedString::from(format!("{}-toggle-show", self.id));
|
|
input(self.id.clone(), &self.state, window, cx)
|
|
.w_full()
|
|
.suffix(
|
|
icon_button(toggle_id, icon, label, ButtonVariant::Secondary, cx)
|
|
.on_click(cx.listener(|this, _, _, cx| this.toggle_show(cx))),
|
|
)
|
|
}
|
|
}
|