//! The main area. Until the voice page, the chat and the rest come, it is the //! empty page: the "no server selected" state with the way to add one, and for //! a server on screen its name, address and state with the buttons that //! connect, disconnect and edit it. An entity of its own, drawn as a cached //! pane: it listens to the servers' signal and tells the window only when //! what it shows changed. use gpui_kit::component::button::{Button, ButtonVariants as _}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::{ Context, Entity, FontWeight, InteractiveElement as _, IntoElement, ParentElement as _, Render, Styled as _, Subscription, Window, div, px, }; use voelin_ui_vm::bridge::ServerItem; use voelin_ui_vm::servers::logic; use super::actions::{AddServer, ConnectVoice, DisconnectVoice, EditServer, ShowVoiceView}; use super::widgets::{Avatar, Presence, icon, panel, tagged}; use crate::app::AppState; use crate::meter::Counters; use crate::signals::Area; use crate::theme::{VoelinTheme, hsla}; /// What the main area shows. #[derive(Clone, Debug, PartialEq)] pub enum MainState { /// No server is on screen. NoServer, /// A server is. Server(ServerItem), } impl MainState { /// The main area for the state. pub fn of(app: &AppState) -> Self { let current = app.vm.current; logic::rail(&app.vm, |bookmark| app.server_icon(bookmark)) .into_iter() .find(|item| Some(i64::from(item.id)) == current) .map_or(Self::NoServer, Self::Server) } } /// The words for a server's state. fn state_words(status: &str) -> &'static str { match status { "connected" => "Connected with voice", "connecting" => "Connecting…", "observing" => "Observed without joining", _ => "Not connected", } } /// The main area. pub struct MainPanel { state: MainState, counters: Counters, _subscriptions: Vec, } impl MainPanel { /// A main area that follows the server on screen in `app`. pub fn new(app: &Entity, cx: &mut Context) -> Self { let (state, counters, signals) = { let shown = app.read(cx); (MainState::of(shown), shown.counters().clone(), shown.signals().clone()) }; let subscriptions = signals.watch(app, &[Area::Servers], cx, |this: &mut Self, app, cx| { let state = MainState::of(app.read(cx)); if state != this.state { this.state = state; cx.notify(); } }); Self { state, counters, _subscriptions: subscriptions } } /// What it shows now. pub fn state(&self) -> &MainState { &self.state } } impl Render for MainPanel { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { self.counters.panes.main.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); let body = match &self.state { MainState::NoServer => div() .id("main-empty") .debug_selector(|| "main-empty".into()) .flex() .flex_col() .items_center() .gap(px(m.space_4)) .child( div() .size(px(72.0)) .rounded(px(m.radius_xl)) .bg(hsla(c.surface_2)) .border_1() .border_color(hsla(c.border)) .flex() .items_center() .justify_center() .child(icon("server", 32.0, c.text_muted)), ) .child( div() .text_size(px(m.font_xl)) .font_weight(FontWeight::SEMIBOLD) .text_color(hsla(c.text)) .child("No server selected"), ) .child( div() .max_w(px(360.0)) .text_center() .text_size(px(m.font_body)) .text_color(hsla(c.text_muted)) .child("Pick a server in the list, or add one to start talking."), ) .child(tagged( "main-add", Button::new("main-add") .primary() .label("Add a server") .on_click(|_, window, cx| window.dispatch_action(Box::new(AddServer), cx)), )), MainState::Server(item) => { let id = i64::from(item.id); let connected = item.status.as_ref() == "connected"; let connecting = item.status.as_ref() == "connecting"; div() .id("main-server") .debug_selector(|| "main-server".into()) .flex() .flex_col() .items_center() .gap(px(m.space_4)) .child( Avatar::new("main-server-avatar", 72.0, &item.initials, item.tint) .image(item.icon.clone()) .square() .presence(Presence::of_server(&item.status)), ) .child( div() .max_w(px(480.0)) .truncate() .text_size(px(m.font_2xl)) .font_weight(FontWeight::SEMIBOLD) .text_color(hsla(c.text)) .child(item.name.to_string()), ) .child( div() .text_size(px(m.font_body)) .text_color(hsla(c.text_secondary)) .child(item.address.to_string()), ) .child( div() .id("main-server-state") .text_size(px(m.font_sm)) .text_color(hsla(c.text_muted)) .child(state_words(&item.status)), ) .child( div() .max_w(px(360.0)) .text_center() .text_size(px(m.font_body)) .text_color(hsla(c.text_muted)) .child("Its chat comes with a later slice."), ) .child( div() .flex() .flex_row() .gap(px(m.space_3)) .when(connected || connecting, |row| { row.child(tagged( "main-disconnect", Button::new("main-disconnect") .danger() .label("Disconnect") .on_click(|_, window, cx| { window.dispatch_action(Box::new(DisconnectVoice), cx); }), )) }) .when(!connected && !connecting, |row| { row.child(tagged( "main-connect", Button::new("main-connect") .primary() .label("Connect") .on_click(|_, window, cx| { window.dispatch_action(Box::new(ConnectVoice), cx); }), )) }) .when(connected, |row| { row.child(tagged( "main-voice", Button::new("main-voice") .primary() .label("Voice channel") .on_click(|_, window, cx| { window.dispatch_action( Box::new(ShowVoiceView { on: true }), cx, ); }), )) }) .child(tagged( "main-edit", Button::new("main-edit").label("Edit").on_click( move |_, window, cx| { window.dispatch_action(Box::new(EditServer { id }), cx); }, ), )), ) } }; // The root of a cached pane takes the size its place gives it: laid out // on its own, `flex_1` would be as wide as its contents. panel(&theme).id("main").size_full().items_center().justify_center().child(body) } }