From 3c06f7e53680ee820d94bac390746607638d676c Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Wed, 18 Nov 2026 20:12:22 -0500 Subject: [PATCH] add unlock/exit buttons to home screen --- src/assets.rs | 28 +++++++++++++++++++ src/main.rs | 22 ++++++++++++++- src/screens/home.rs | 68 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 src/assets.rs diff --git a/src/assets.rs b/src/assets.rs new file mode 100644 index 0000000..b9d1b60 --- /dev/null +++ b/src/assets.rs @@ -0,0 +1,28 @@ +use std::borrow::Cow; + +use gpui_kit::assets::{Assets, icon_assets}; +use gpui_kit::{AssetSource, Result, SharedString}; + + +// gpui-kit only embeds the icons its own components use, so anything else has to be listed here +icon_assets!(CreddyIcons, [LogOut, ShieldCheck]); + +/// Our extra icons, falling back to gpui-kit's defaults. +pub struct CreddyAssets; + +impl AssetSource for CreddyAssets { + fn load(&self, path: &str) -> Result>> { + match CreddyIcons.load(path)? { + Some(bytes) => Ok(Some(bytes)), + None => Assets.load(path), + } + } + + fn list(&self, path: &str) -> Result> { + let mut paths = Assets.list(path)?; + paths.extend(CreddyIcons.list(path)?); + paths.sort(); + paths.dedup(); + Ok(paths) + } +} diff --git a/src/main.rs b/src/main.rs index d5d7da4..7fa2de1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use gpui_kit::{ }; use gpui_omarchy::ActiveTheme; +mod assets; mod components; mod config; mod db; @@ -68,7 +69,7 @@ fn main() { // Icons are read through the asset source, so register the bundle. gpui_kit::application() - .with_assets(gpui_kit::assets::Assets) + .with_assets(assets::CreddyAssets) .run(|app| { launch(app, app_config, app_session); app.activate(true); @@ -204,4 +205,23 @@ mod tests { assert!(is_unlocked(cx)); assert_eq!(cx.read(|cx| cx.global::().rehide_ms), 5000); } + + #[gpui_kit::test] + fn test_lock(cx: &mut TestAppContext) { + let env = TestEnv::new().unwrap(); + env.db().unwrap(); + let window = open_app(&env, cx); + fill(window, "passphrase", TEST_PASSPHRASE, cx); + click(window, "submit", cx); + assert!(is_unlocked(cx)); + + click(window, "lock", cx); + + assert!(cx.read(|cx| matches!(cx.global::(), AppSession::Locked))); + // The unlock screen should be a new one, not the old one with the passphrase still in it. + // The masked input doesn't expose its value, so check by submitting it as-is. + click(window, "submit", cx); + assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty.")); + assert!(!is_unlocked(cx)); + } } diff --git a/src/screens/home.rs b/src/screens/home.rs index de62f55..b1c2ead 100644 --- a/src/screens/home.rs +++ b/src/screens/home.rs @@ -1,13 +1,71 @@ -use gpui_kit::{div, Render, Window, Context, IntoElement, Styled, ParentElement}; +use gpui_kit::base::Button; +use gpui_kit::{ + App, Context, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, Render, + StatefulInteractiveElement, Styled, Window, div, rems, +}; +use gpui_omarchy::{ActiveTheme, IconName, focus_scope, icon}; + +use crate::session::AppSession; + pub struct HomeScreen {} impl Render for HomeScreen { - fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { - div() + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let t = cx.omarchy(); + let lock = nav_tile("lock", IconName::ShieldCheck, "Lock", "Lock Creddy.", t.warning, cx) + .on_click(|_, _, cx| cx.set_global(AppSession::Locked)); + let exit = nav_tile("exit", IconName::LogOut, "Exit", "Close Creddy.", t.accent, cx) + .on_click(|_, _, cx| cx.quit()); + + focus_scope("home") + .size_full() .flex() - .justify_center() + .flex_col() .items_center() - .child("Creddy is unlocked") + .p(rems(0.5)) + .child( + div() + .text_size(rems(1.875)) + .font_weight(FontWeight::BOLD) + .child("Creddy"), + ) + .child( + // fill the rest of the window so the tiles sit in the middle of it + div() + .flex_1() + .flex() + .items_center() + .gap(rems(1.5)) + .child(lock) + .child(exit), + ) } } + +/// A big square-ish button with an icon, a title, and a short description. +fn nav_tile( + id: &'static str, + icon_name: IconName, + title: &'static str, + description: &'static str, + color: Hsla, + cx: &App, +) -> Button { + let t = cx.omarchy(); + Button::new(id) + .accessibility_label(title) + .flex_col() + .justify_start() + .gap(rems(1.)) + .w(rems(14.)) + .p(rems(1.)) + .border_1() + .border_color(color) + .hover(|s| s.bg(t.hover_fill())) + .focus_visible(|s| s.bg(t.hover_fill())) + .active(|s| s.bg(t.pressed_fill())) + .child(icon(icon_name).size(rems(3.)).text_color(color)) + .child(div().text_size(rems(1.125)).font_weight(FontWeight::BOLD).child(title)) + .child(div().text_size(rems(0.875)).text_center().child(description)) +}