138 lines
4.9 KiB
Rust
138 lines
4.9 KiB
Rust
use gpui_kit::{
|
|
AppContext, BorrowAppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
|
|
Styled, Subscription, Task, Window, div, rems,
|
|
};
|
|
use gpui_omarchy::{ButtonVariant, button, focus_scope};
|
|
|
|
use crate::components::{PassphraseInput, PassphraseInputEvent, error_alert};
|
|
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("passphrase", 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 {
|
|
let db = Db::connect(&config, passphrase.as_str())?;
|
|
// Some of AppConfig comes from the db, but we couldn't see it until now
|
|
let private_config = db.load_private_config()?;
|
|
Ok((db, private_config))
|
|
})
|
|
.await;
|
|
|
|
// db connection has been established or not, so now we handle the result
|
|
let _ = this.update(cx, |this, cx| {
|
|
this.pending = None;
|
|
match result {
|
|
Ok((db, private_config)) => {
|
|
cx.update_global::<AppConfig, _>(|config, _cx| config.merge_private(private_config));
|
|
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| error_alert(e, 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))),
|
|
),
|
|
)
|
|
}
|
|
}
|