rework basically everything

This commit is contained in:
jfmonty2 committed 2026-11-12 11:28:18 -05:00
1 parent 2bd81a66a4
commit a1e5daba59
12 files changed
+450 -157

No files matched your search

+106 -58
View File
@@ -1,18 +1,22 @@
use gpui_kit::base::input::{InputEvent, InputState};
use gpui_kit::{
AppContext, Context, Entity, IntoElement, ParentElement, Render, SharedString, Styled,
Subscription, Task, Window, div, rems,
AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
Styled, Subscription, Task,Window, div, rems,
};
use gpui_omarchy::{ActiveTheme, ButtonVariant, Status, alert, button, focus_scope, input, panel};
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 {
session: Entity<AppSession>,
passphrase: Entity<InputState>,
confirm: Entity<InputState>,
passphrase: Entity<PassphraseInput>,
confirm: Entity<PassphraseInput>,
error: Option<SharedString>,
/// Set while the KDF is running; dropping it cancels the task.
pending: Option<Task<()>>,
@@ -20,33 +24,37 @@ pub struct WelcomeScreen {
}
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));
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));
// 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);
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();
}
})
})
.collect();
_ => (),
}
}));
}
// 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 {
session,
passphrase,
confirm,
error: None,
@@ -55,44 +63,42 @@ impl WelcomeScreen {
}
}
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();
let confirm = self.confirm.read(cx).value();
if passphrase.is_empty() {
self.error = Some("Passphrase cannot be empty.".into());
let passphrase = self.passphrase.read(cx).value(cx);
if !self.passphrases_match(cx) {
self.error = Some(MISMATCH.into());
cx.notify();
return;
}
if passphrase != confirm {
self.error = Some("Passphrases do not match.".into());
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 {
let salt = AppSession::salt().map_err(|e| e.to_string())?;
AppSession::new(&passphrase, &salt).map_err(|e| e.to_string())
Db::connect(&config, passphrase.as_str())
})
.await;
let _ = this.update(cx, |this, cx| {
this.pending = None;
match result {
Ok(unlocked) => {
this.session.update(cx, |session, cx| {
*session = unlocked;
cx.notify();
});
}
Ok(db) => cx.set_global(AppSession::Unlocked(db)),
Err(e) => this.error = Some(format!("Failed to derive key: {e}").into()),
}
cx.notify();
@@ -103,27 +109,69 @@ impl WelcomeScreen {
}
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" };
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()
.items_center()
.justify_center()
.bg(cx.omarchy().background)
.items_center()
.bg(t.background)
.text_color(t.foreground)
.font_family(t.font.clone())
.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)))
div()
.w_full()
.max_w(rems(32.))
.flex()
.flex_col()
.gap(rems(2.))
.child(
button("get-started", label, ButtonVariant::Primary, cx)
.disabled(busy)
.on_click(cx.listener(|this, _, window, cx| this.submit(window, cx))),
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)
})),
),
),
)
}