initial commit - just the welcome screen and db connection, at present
This commit is contained in:
1 parent
d67871f491
commit
2bd81a66a4
10 files changed
+9377
No files matched your search
@@ -0,0 +1,130 @@
|
||||
use gpui_kit::base::input::{InputEvent, InputState};
|
||||
use gpui_kit::{
|
||||
AppContext, Context, Entity, IntoElement, ParentElement, Render, SharedString, Styled,
|
||||
Subscription, Task, Window, div, rems,
|
||||
};
|
||||
use gpui_omarchy::{ActiveTheme, ButtonVariant, Status, alert, button, focus_scope, input, panel};
|
||||
|
||||
use crate::session::AppSession;
|
||||
|
||||
|
||||
/// First-run screen: choose a passphrase, derive the key, and unlock the session.
|
||||
pub struct WelcomeScreen {
|
||||
session: Entity<AppSession>,
|
||||
passphrase: Entity<InputState>,
|
||||
confirm: Entity<InputState>,
|
||||
error: Option<SharedString>,
|
||||
/// Set while the KDF is running; dropping it cancels the task.
|
||||
pending: Option<Task<()>>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
impl WelcomeScreen {
|
||||
pub fn new(session: Entity<AppSession>, window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let passphrase = cx.new(|cx| {
|
||||
InputState::new(window, cx)
|
||||
.masked(true)
|
||||
.placeholder("Passphrase")
|
||||
});
|
||||
let confirm = cx.new(|cx| {
|
||||
InputState::new(window, cx)
|
||||
.masked(true)
|
||||
.placeholder("Confirm passphrase")
|
||||
});
|
||||
passphrase.update(cx, |state, cx| state.focus(window, cx));
|
||||
|
||||
// Enter in either field submits
|
||||
let _subscriptions = [&passphrase, &confirm]
|
||||
.into_iter()
|
||||
.map(|field| {
|
||||
cx.subscribe_in(field, window, |this, _, event, window, cx| {
|
||||
if let InputEvent::PressEnter { .. } = event {
|
||||
this.submit(window, cx);
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
session,
|
||||
passphrase,
|
||||
confirm,
|
||||
error: None,
|
||||
pending: None,
|
||||
_subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
fn submit(&mut self, _: &mut Window, cx: &mut Context<Self>) {
|
||||
if self.pending.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
let passphrase = self.passphrase.read(cx).value();
|
||||
let confirm = self.confirm.read(cx).value();
|
||||
if passphrase.is_empty() {
|
||||
self.error = Some("Passphrase cannot be empty.".into());
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
if passphrase != confirm {
|
||||
self.error = Some("Passphrases do not match.".into());
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
|
||||
self.error = None;
|
||||
// todo: persist the salt (and KDF params) so the database can be unlocked again later
|
||||
self.pending = Some(cx.spawn(async move |this, cx| {
|
||||
// argon2 is deliberately slow, so keep it off the UI thread
|
||||
let result = cx
|
||||
.background_spawn(async move {
|
||||
let salt = AppSession::salt().map_err(|e| e.to_string())?;
|
||||
AppSession::new(&passphrase, &salt).map_err(|e| e.to_string())
|
||||
})
|
||||
.await;
|
||||
|
||||
let _ = this.update(cx, |this, cx| {
|
||||
this.pending = None;
|
||||
match result {
|
||||
Ok(unlocked) => {
|
||||
this.session.update(cx, |session, cx| {
|
||||
*session = unlocked;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
Err(e) => this.error = Some(format!("Failed to derive key: {e}").into()),
|
||||
}
|
||||
cx.notify();
|
||||
});
|
||||
}));
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for WelcomeScreen {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let busy = self.pending.is_some();
|
||||
let label = if busy { "Creating…" } else { "Get started" };
|
||||
|
||||
focus_scope("welcome")
|
||||
.size_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.bg(cx.omarchy().background)
|
||||
.child(
|
||||
panel("Welcome to Creddy", cx)
|
||||
.w(rems(24.))
|
||||
.child(div().child("Choose a passphrase to encrypt your credentials."))
|
||||
.child(input("passphrase", &self.passphrase, window, cx))
|
||||
.child(input("confirm", &self.confirm, window, cx))
|
||||
.children(self.error.clone().map(|e| alert(e, Status::Error, cx)))
|
||||
.child(
|
||||
button("get-started", label, ButtonVariant::Primary, cx)
|
||||
.disabled(busy)
|
||||
.on_click(cx.listener(|this, _, window, cx| this.submit(window, cx))),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user