From 49c1e81e1d92caa8dbb04f812ce3bfdebac66838 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Mon, 23 Nov 2026 12:51:08 -0500 Subject: [PATCH] refactor view-selection once again --- src/app.rs | 26 +++++++++++--------------- src/screens/mod.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index d15d709..4e48fba 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,26 +13,22 @@ pub struct CreddyApp {} impl Render for CreddyApp { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { - let session = cx.global::(); - let screen = match session { - // we can't do anything with an empty session, so that takes precedence over everything else - AppSession::Empty => window + let screen = match Screen::from_context(cx) { + Screen::Welcome => window .use_keyed_state("welcome", cx, |window, cx| WelcomeScreen::new(window, cx)) .into_any_element(), - // If any requests are pending, the approval flow will handle unlocking itself - _ if cx.global::().0.len() > 0 => todo!(), - AppSession::Locked => window + Screen::Unlock => window .use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx)) .into_any_element(), // Otherwise, fall back to "current screen" global - AppSession::Unlocked { .. } => match cx.global::() { - Screen::Home => window - .use_keyed_state("home", cx, |_, _| HomeScreen {}) - .into_any_element(), - Screen::Credentials => window - .use_keyed_state("credentials", cx, |_, cx| CredentialsScreen::new(cx)) - .into_any_element(), - }, + Screen::Home => window + .use_keyed_state("home", cx, |_, _| HomeScreen {}) + .into_any_element(), + Screen::Credentials => window + .use_keyed_state("credentials", cx, |_, cx| CredentialsScreen::new(cx)) + .into_any_element(), + Screen::RequestApproval => todo!(), + Screen::ShowApproval => todo!(), }; let theme = cx.omarchy(); diff --git a/src/screens/mod.rs b/src/screens/mod.rs index a697a82..dbaed25 100644 --- a/src/screens/mod.rs +++ b/src/screens/mod.rs @@ -1,20 +1,43 @@ -use gpui_kit::Global; +use gpui_kit::{Context, Global}; mod credentials; mod home; mod unlock; mod welcome; +use crate::app::CreddyApp; pub use credentials::CredentialsScreen; pub use home::HomeScreen; pub use unlock::UnlockScreen; pub use welcome::WelcomeScreen; +use crate::session::AppSession; +use crate::srv::PendingRequests; -/// Which screen to show while the app is unlocked. +/// It's convenient to be able to reference a screen without actually constructing it +/// (e.g. when navigating), so this exists to map app state to the name of a screen. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Screen { + Welcome, + Unlock, Home, Credentials, + RequestApproval, + ShowApproval, +} + +impl Screen { + pub fn from_context(cx: &Context) -> Screen { + match cx.global::() { + // we can't do anything useful when empty except ask for a passphrase, so this state takes precedence + AppSession::Empty => Screen::Welcome, + // otherwise if we have a pending request, ask for approval, even if locked (approval flow will handle unlock) + _ if cx.global::().0.len() > 0 => Screen::RequestApproval, + // therwise if locked, ask for unlock + AppSession::Locked => Screen::Unlock, + // otherwise, fall back to "current screen" global + AppSession::Unlocked{ .. } => *cx.global::(), + } + } } impl Global for Screen {}