add unlock screen

This commit is contained in:
jfmonty2 committed 2026-11-12 14:47:10 -05:00
1 parent a1e5daba59
commit 2371486f39
5 files changed
+138 -4

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
use gpui_kit::{
AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
Styled, Subscription, Task, Window, div, rems,
};
use gpui_omarchy::{ButtonVariant, Status, alert, button, focus_scope};
use crate::components::{PassphraseInput, PassphraseInputEvent};
use crate::config::AppConfig;
use crate::db::Db;
use crate::error::CreddyError;
use crate::session::AppSession;
/// Shown at startup when a database exists: enter the passphrase to open it.
pub struct UnlockScreen {
passphrase: Entity<PassphraseInput>,
error: Option<SharedString>,
/// Set while the KDF is running; dropping it cancels the task.
pending: Option<Task<()>>,
_subscriptions: Vec<Subscription>,
}
impl UnlockScreen {
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 show = cx.new(|_| false);
let passphrase = cx.new(|cx| PassphraseInput::new(show, window, cx));
passphrase.update(cx, |input, cx| input.focus(window, cx));
let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| {
if let PassphraseInputEvent::Submit = event {
this.submit(window, cx);
}
});
Self {
passphrase,
error: None,
pending: None,
_subscriptions: vec![submit],
}
}
fn submit(&mut self, _: &mut Window, cx: &mut Context<Self>) {
if self.pending.is_some() {
return;
}
let passphrase = self.passphrase.read(cx).value(cx);
if passphrase.is_empty() {
self.error = Some("Passphrase is empty.".into());
cx.notify();
return;
}
let config = cx.global::<AppConfig>().clone();
self.error = None;
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 { Db::connect(&config, passphrase.as_str()) })
.await;
let _ = this.update(cx, |this, cx| {
this.pending = None;
match result {
Ok(db) => cx.set_global(AppSession::Unlocked(db)),
Err(e) => this.error = Some(error_message(&e)),
}
cx.notify();
});
}));
cx.notify();
}
}
/// Turn a failed unlock into something fit to show the user.
fn error_message(e: &CreddyError) -> SharedString {
// todo: have Db::connect return a dedicated error variant for this instead
if let CreddyError::Db(duckdb::Error::DuckDBFailure(_, Some(desc))) = e
&& desc.contains("Wrong encryption key")
{
return "Incorrect passphrase.".into();
}
format!("Failed to unlock: {e}").into()
}
impl Render for UnlockScreen {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
focus_scope("unlock")
.size_full()
.flex()
.flex_col()
.items_center()
.p(rems(0.5))
.child(
div()
.text_size(rems(1.875))
.font_weight(FontWeight::BOLD)
.child("Creddy is locked"),
)
.child(
// fill the rest of the window so the form sits in the middle of it
div()
.flex_1()
.w_full()
.max_w(rems(24.))
.flex()
.flex_col()
.justify_center()
.gap(rems(1.))
.child(
div()
.text_center()
.text_size(rems(1.25))
.font_weight(FontWeight::BOLD)
.child("Please enter your passphrase"),
)
.children(self.error.clone().map(|e| alert(e, Status::Error, cx)))
.child(self.passphrase.clone())
.child(
button("submit", "Submit", ButtonVariant::Primary, cx)
.disabled(self.pending.is_some())
.on_click(cx.listener(|this, _, window, cx| this.submit(window, cx))),
),
)
}
}