rework basically everything
This commit is contained in:
1 parent
2bd81a66a4
commit
a1e5daba59
12 files changed
+450
-157
No files matched your search
@@ -0,0 +1,97 @@
|
||||
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 {
|
||||
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(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 {
|
||||
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")
|
||||
};
|
||||
|
||||
input("passphrase", &self.state, window, cx)
|
||||
.w_full()
|
||||
.suffix(
|
||||
icon_button("toggle-show", icon, label, ButtonVariant::Secondary, cx)
|
||||
.on_click(cx.listener(|this, _, _, cx| this.toggle_show(cx))),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user