//! The window: a sidebar of pages, the page shown, and what came of each change as a //! notification. mod dpi; mod logs; mod outbounds; mod overview; mod settings; mod telegram; mod widgets; use gpui_kit::component::notification::Notification; use gpui_kit::component::sidebar::{Sidebar, SidebarGroup, SidebarHeader, SidebarMenu, SidebarMenuItem}; use gpui_kit::assets::IconName; use gpui_kit::component::*; use gpui_kit::*; use crate::model::{Model, Outcome}; #[derive(Clone, Copy, PartialEq, Eq)] pub enum Page { Overview, Dpi, Telegram, Outbounds, Logs, Settings, Components, } impl Page { const ALL: [Page; 7] = [Page::Overview, Page::Dpi, Page::Telegram, Page::Outbounds, Page::Logs, Page::Settings, Page::Components]; fn title(self) -> &'static str { match self { Page::Overview => "Overview", Page::Dpi => "DPI bypass", Page::Telegram => "Telegram proxy", Page::Outbounds => "Outbounds", Page::Logs => "Logs", Page::Settings => "Settings", Page::Components => "Components", } } fn icon(self) -> IconName { match self { Page::Overview => IconName::LayoutDashboard, Page::Dpi => IconName::Shield, Page::Telegram => IconName::Send, Page::Outbounds => IconName::Globe, Page::Logs => IconName::SquareTerminal, Page::Settings => IconName::Settings, Page::Components => IconName::Package, } } } pub struct AppView { model: Entity, page: Page, dpi: Entity, telegram: Entity, outbounds: Entity, /// Made when first shown: following the logs starts then. logs: Option>, /// Made when first shown, as it loads the configuration into its editor then. settings: Option>, _subscriptions: Vec, } impl AppView { pub fn new(model: Entity, window: &mut Window, cx: &mut Context) -> Self { let subscriptions = vec![ cx.observe(&model, |_, _, cx| cx.notify()), cx.subscribe_in(&model, window, |_, _, outcome: &Outcome, window, cx| { let note = match outcome { Outcome::Done(m) => Notification::success(m.clone()), Outcome::Failed(m) => Notification::error(m.clone()).autohide(false), }; window.push_notification(note, cx); }), ]; let dpi = cx.new(|cx| dpi::DpiPage::new(model.clone(), window, cx)); let telegram = cx.new(|cx| telegram::TelegramPage::new(model.clone(), window, cx)); let outbounds = cx.new(|cx| outbounds::OutboundsPage::new(model.clone(), window, cx)); Self { model, page: Page::Overview, dpi, telegram, outbounds, logs: None, settings: None, _subscriptions: subscriptions } } fn sidebar(&self, cx: &mut Context) -> impl IntoElement { let items = Page::ALL.map(|page| { SidebarMenuItem::new(page.title()).icon(page.icon()).active(self.page == page).on_click(cx.listener(move |this, _, _, cx| { this.page = page; cx.notify(); })) }); Sidebar::new("pages").header(SidebarHeader::new().child(widgets::brand(cx))).child(SidebarGroup::new("Proxy Suite").child(SidebarMenu::new().children(items))) } } impl Render for AppView { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let page = match self.page { Page::Overview => overview::render(&self.model, window, cx).into_any_element(), Page::Dpi => self.dpi.clone().into_any_element(), Page::Telegram => self.telegram.clone().into_any_element(), Page::Outbounds => self.outbounds.clone().into_any_element(), Page::Logs => { let model = self.model.clone(); self.logs.get_or_insert_with(|| cx.new(|cx| logs::LogsPage::new(model, cx))).clone().into_any_element() } Page::Settings => { let model = self.model.clone(); self.settings.get_or_insert_with(|| cx.new(|cx| settings::SettingsPage::new(model, window, cx))).clone().into_any_element() } other => widgets::page(other.title(), cx).child(div().text_color(cx.theme().muted_foreground).child("Not here yet.")).into_any_element(), }; div() .h_flex() .size_full() .bg(cx.theme().background) .text_color(cx.theme().foreground) .child(self.sidebar(cx)) .child(div().id("page").flex_1().min_w_0().h_full().overflow_y_scroll().child(page)) } }