//! The server rail: the saved servers as avatars with their state, the //! selection, the add-server button and the settings button (`ServerRail` of //! `ui/shells/desktop.slint`). An entity of its own, drawn as a cached pane: //! it listens to the servers' signal ([`crate::signals`]) and tells the window //! only when the servers it lists changed. use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::{ Context, Entity, FontWeight, InteractiveElement as _, IntoElement, ParentElement as _, Render, StatefulInteractiveElement as _, Styled as _, Subscription, Window, div, px, }; use voelin_ui_vm::bridge::ServerItem; use voelin_ui_vm::servers::logic; use super::actions::{AddServer, EditServer, OpenSettings, SelectServer}; use super::widgets::{Avatar, IconButton, Presence}; use crate::app::AppState; use crate::meter::Counters; use crate::signals::Area; use crate::theme::{VoelinTheme, hsla}; /// What the rail shows: the servers in order, and which is on screen. #[derive(Clone, Debug, Default, PartialEq)] pub struct RailView { /// The saved servers, as `voelin_ui_vm::servers::logic::rail` has them. pub servers: Vec, /// The bookmark on screen. pub current: Option, } impl RailView { /// The rail for the state. pub fn of(app: &AppState) -> Self { Self { servers: logic::rail(&app.vm, |bookmark| app.server_icon(bookmark)), current: app.vm.current, } } } /// The rail. pub struct ServerRail { view: RailView, counters: Counters, _subscriptions: Vec, } impl ServerRail { /// A rail that follows the servers of `app`. pub fn new(app: &Entity, cx: &mut Context) -> Self { let (view, counters, signals) = { let state = app.read(cx); (RailView::of(state), state.counters().clone(), state.signals().clone()) }; let subscriptions = signals.watch(app, &[Area::Servers], cx, |this: &mut Self, app, cx| { let view = RailView::of(app.read(cx)); if view != this.view { this.view = view; cx.notify(); } }); Self { view, counters, _subscriptions: subscriptions } } /// What it shows now. pub fn view(&self) -> &RailView { &self.view } } /// One server of the rail: the selection pill, the ring of the one on screen, /// the avatar with its state, the unread count and the live mark. fn server(theme: &VoelinTheme, item: &ServerItem, selected: bool) -> impl IntoElement + use<> { let (c, m) = (&theme.colors, &theme.metrics); let id = i64::from(item.id); let group = format!("rail-server-{id}"); let name = item.name.to_string(); let tooltip = format!("{name}\n{}", item.address); let pill = div() .absolute() .left(px(0.0)) .w(px(4.0)) .h(px(if selected { 36.0 } else { 0.0 })) .rounded(px(2.0)) .bg(hsla(c.text)) .when(!selected, |pill| { let group = group.clone(); pill.group_hover(group, |style| style.h(px(18.0))) }); div() .id(("rail-server", id as u64)) .group(group) .relative() .w_full() .h(px(56.0)) .flex_none() .flex() .items_center() .justify_center() .cursor_pointer() .debug_selector(|| format!("rail-server-{id}")) .tooltip(move |window, cx| { gpui_kit::component::tooltip::Tooltip::new(tooltip.clone()).build(window, cx) }) .on_click(move |event, window, cx| { // A second click on the server is the Slint rail's double click. if event.click_count() >= 2 { window.dispatch_action(Box::new(EditServer { id }), cx); } else { window.dispatch_action(Box::new(SelectServer { id }), cx); } }) .child(pill) .when(selected, |row| { row.child( div() .absolute() .size(px(52.0)) .rounded(px(m.radius_lg + 2.0)) .border_2() .border_color(hsla(c.accent)) .bg(hsla(c.glow.with_alpha(0x66))), ) }) .child( Avatar::new(("rail-avatar", id as u64), 46.0, &item.initials, item.tint) .image(item.icon.clone()) .square() .presence(Presence::of_server(&item.status)), ) .when(item.unread > 0, |row| { row.child( div() .absolute() .top(px(0.0)) .left(px(50.0)) .min_w(px(18.0)) .h(px(18.0)) .px(px(5.0)) .rounded(px(9.0)) .bg(hsla(c.live)) .border_2() .border_color(hsla(c.bg_rail)) .flex() .items_center() .justify_center() .text_size(px(m.font_xs)) .font_weight(FontWeight::BOLD) .text_color(hsla(c.on_accent)) .child(item.unread.to_string()), ) }) .when(item.live && item.unread == 0, |row| { row.child( div() .absolute() .top(px(4.0)) .left(px(52.0)) .size(px(12.0)) .rounded(px(6.0)) .bg(hsla(c.live)) .border_2() .border_color(hsla(c.bg_rail)), ) }) } impl Render for ServerRail { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { self.counters.panes.rail.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); let current = self.view.current; let servers: Vec<_> = self .view .servers .iter() .map(|item| server(&theme, item, current == Some(i64::from(item.id)))) .collect(); div() .id("rail") .w(px(m.rail_width)) .h_full() .flex_none() .flex() .flex_col() .items_center() .py(px(m.space_3)) .gap(px(m.space_2)) .bg(hsla(c.bg_rail.with_alpha(230))) .border_r_1() .border_color(hsla(c.border)) // The app's mark. (Home, which the Slint rail opens from it, comes // with its slice.) .child( div() .size(px(48.0)) .flex_none() .rounded(px(m.radius_lg)) .border_1() .border_color(hsla(c.border)) .bg(hsla(c.surface_2)) .flex() .items_center() .justify_center() .child(gpui_kit::img("logo.svg").size(px(30.0))), ) .child( div() .w(px(32.0)) .h(px(2.0)) .my(px(m.space_2)) .rounded(px(1.0)) .bg(hsla(c.border_strong)), ) .child( div() .id("rail-servers") .flex_1() .min_h_0() .w_full() .overflow_y_scroll() .flex() .flex_col() .items_center() .gap(px(m.space_2)) .children(servers) .child( div().pt(px(m.space_2)).child( IconButton::new("rail-add", "plus", "Add a server") .size(46.0) .round() .filled() .tint(c.success) .on_click(|_, window, cx| { window.dispatch_action(Box::new(AddServer), cx); }), ), ), ) .child( IconButton::new("rail-settings", "settings", "Settings") .size(46.0) .round() .on_click(|_, window, cx| { window.dispatch_action(Box::new(OpenSettings), cx); }), ) } }