179 lines
6.5 KiB
Rust
179 lines
6.5 KiB
Rust
use gpui_kit::{
|
|
AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
|
|
Styled, Subscription, Task,Window, div, rems,
|
|
};
|
|
use gpui_omarchy::{ActiveTheme, ButtonVariant, Status, alert, button, focus_scope};
|
|
|
|
use crate::components::{PassphraseInput, PassphraseInputEvent};
|
|
use crate::config::AppConfig;
|
|
use crate::db::Db;
|
|
use crate::session::AppSession;
|
|
|
|
|
|
const MISMATCH: &str = "Passphrases do not match.";
|
|
|
|
|
|
/// First-run screen: choose a passphrase, derive the key, and unlock the session.
|
|
pub struct WelcomeScreen {
|
|
passphrase: Entity<PassphraseInput>,
|
|
confirm: Entity<PassphraseInput>,
|
|
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(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
|
let show = cx.new(|_| false);
|
|
let passphrase = cx.new(|cx| PassphraseInput::new(show.clone(), window, cx));
|
|
let confirm = cx.new(|cx| PassphraseInput::new(show, window, cx));
|
|
passphrase.update(cx, |input, cx| input.focus(window, cx));
|
|
|
|
let mut _subscriptions = Vec::new();
|
|
for input in [&passphrase, &confirm] {
|
|
_subscriptions.push(cx.subscribe_in(input, window, |this, _, event, window, cx| {
|
|
match event {
|
|
PassphraseInputEvent::Submit => this.submit(window, cx),
|
|
// typing only dismisses the mismatch error, never raises it
|
|
PassphraseInputEvent::Change if this.passphrases_match(cx) => {
|
|
this.error = None;
|
|
cx.notify();
|
|
}
|
|
_ => (),
|
|
}
|
|
}));
|
|
}
|
|
// leaving the confirm field is what raises the mismatch error
|
|
_subscriptions.push(cx.subscribe_in(&confirm, window, |this, _, event, _, cx| {
|
|
if let PassphraseInputEvent::Blur = event
|
|
&& !this.passphrases_match(cx)
|
|
{
|
|
this.error = Some(MISMATCH.into());
|
|
cx.notify();
|
|
}
|
|
}));
|
|
|
|
Self {
|
|
passphrase,
|
|
confirm,
|
|
error: None,
|
|
pending: None,
|
|
_subscriptions,
|
|
}
|
|
}
|
|
|
|
fn passphrases_match(&self, cx: &Context<Self>) -> bool {
|
|
self.passphrase.read(cx).value(cx) == self.confirm.read(cx).value(cx)
|
|
}
|
|
|
|
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 !self.passphrases_match(cx) {
|
|
self.error = Some(MISMATCH.into());
|
|
cx.notify();
|
|
return;
|
|
}
|
|
if passphrase.is_empty() {
|
|
self.error = Some("Passphrase is empty.".into());
|
|
cx.notify();
|
|
return;
|
|
}
|
|
|
|
let config = cx.global::<AppConfig>().clone();
|
|
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 {
|
|
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(format!("Failed to derive key: {e}").into()),
|
|
}
|
|
cx.notify();
|
|
});
|
|
}));
|
|
cx.notify();
|
|
}
|
|
}
|
|
|
|
impl Render for WelcomeScreen {
|
|
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let t = cx.omarchy();
|
|
let field = |label: &'static str, input: &Entity<PassphraseInput>| {
|
|
div()
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(0.25))
|
|
.child(div().text_size(rems(0.875)).child(label))
|
|
.child(input.clone())
|
|
};
|
|
|
|
focus_scope("welcome")
|
|
.size_full()
|
|
.flex()
|
|
.justify_center()
|
|
.items_center()
|
|
.bg(t.background)
|
|
.text_color(t.foreground)
|
|
.font_family(t.font.clone())
|
|
.child(
|
|
div()
|
|
.w_full()
|
|
.max_w(rems(32.))
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(2.))
|
|
.child(
|
|
div()
|
|
.text_center()
|
|
.text_size(rems(1.5))
|
|
.font_weight(FontWeight::BOLD)
|
|
.child("Welcome to Creddy!"),
|
|
)
|
|
.child(
|
|
div()
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(1.))
|
|
.child("Create a passphrase to get started.")
|
|
.child(
|
|
"Please note that if you forget your passphrase, there is no way \
|
|
to recover your stored credentials. You will have to start over \
|
|
with a new passphrase.",
|
|
),
|
|
)
|
|
.child(
|
|
div()
|
|
.w_full()
|
|
.max_w(rems(24.))
|
|
.mx_auto()
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(1.))
|
|
.children(self.error.clone().map(|e| alert(e, Status::Error, cx)))
|
|
.child(field("Passphrase", &self.passphrase))
|
|
.child(field("Re-enter passphrase", &self.confirm))
|
|
.child(
|
|
button("submit", "Submit", ButtonVariant::Primary, cx)
|
|
.disabled(self.pending.is_some())
|
|
.on_click(cx.listener(|this, _, window, cx| {
|
|
this.submit(window, cx)
|
|
})),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
}
|