//! The members panel (`MembersPanel` and `MemberCardPopup` of //! `screens/members.slint`): everyone on the server in sections (those //! streaming first, then each server group), a search, and the card of one //! person over the panel. //! //! [`MembersPane`] is a cached pane over a virtual list that follows //! [`TreeModel`] as the tree does ([`TreeEvent::People`]: the row operations //! the list applies). It is the pane a talker redraws: a person's row has the //! speaking ring, "Speaking" and the bars, and nothing else of the window is //! told. [`MemberCardView`] is the card, a card over the window with a scrim //! that a click outside closes; its buttons (the volume, a message, a poke, //! friends, blocking) come with the slices that have those dialogs. use std::sync::Arc; use gpui_kit::component::input::{Input, InputEvent, InputState}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::{ AnyElement, AppContext as _, Context, Entity, FontWeight, InteractiveElement as _, IntoElement, ListAlignment, ListState, ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled as _, Subscription, Window, div, list, px, }; use voelin_ui_vm::bridge::{MemberCard, MemberItem}; use voelin_ui_vm::list::Changes; use super::actions::{OpenMember, SearchMembers}; use super::tree_model::{Emptiness, TreeEvent, TreeModel}; use super::widgets::{ Avatar, IconButton, Presence, chip, icon, live_badge, small_picture, speaking_bars, }; use crate::meter::Counters; use crate::theme::tokens::Rgba; use crate::theme::{VoelinTheme, hsla}; /// The colour of the shield of a moderator's role. const ROLE: Rgba = Rgba(0xc06c_ffff); /// The members panel. pub struct MembersPane { rows: Arc>, total: i32, emptiness: Emptiness, /// The person whose card is open. open: Option, list: ListState, search: Entity, searching: bool, /// The text size factor the list measured its rows at. measured_at: f32, counters: Counters, _subscriptions: Vec, } impl MembersPane { /// A panel that follows `model`. pub fn new( model: &Entity, counters: Counters, window: &mut Window, cx: &mut Context, ) -> Self { let (rows, total, emptiness, open) = { let model = model.read(cx); (model.people().clone(), model.total(), model.emptiness(), model.card().map(|c| c.id)) }; let search = cx.new(|cx| { InputState::new(window, cx).placeholder(SharedString::from("Search members…")) }); let subscriptions = vec![ cx.subscribe(model, |this: &mut Self, model, event: &TreeEvent, cx| { match event { TreeEvent::People(changes) => { let new = model.read(cx).people().clone(); this.apply(changes, new); } TreeEvent::Lists => { let model = model.read(cx); let now = (model.total(), model.emptiness()); if now == (this.total, this.emptiness) { return; } (this.total, this.emptiness) = now; } TreeEvent::Card => this.open = model.read(cx).card().map(|card| card.id), TreeEvent::Rows(_) | TreeEvent::Selection => return, } cx.notify(); }), cx.subscribe_in(&search, window, |_, input, event: &InputEvent, window, cx| { if matches!(event, InputEvent::Change) { let text = input.read(cx).value().to_string(); window.dispatch_action(Box::new(SearchMembers { text }), cx); } }), ]; Self { list: ListState::new(rows.len(), ListAlignment::Top, px(240.0)), rows, total, emptiness, open, search, searching: false, measured_at: 0.0, counters, _subscriptions: subscriptions, } } /// The rows it shows. pub fn rows(&self) -> &[MemberItem] { &self.rows } /// The person whose card is open, as the panel marks them. pub fn open(&self) -> Option { self.open } /// Whether the search box is shown. pub fn searching(&self) -> bool { self.searching } /// Take the new rows in, telling the list which rows came, went or changed /// their height (the first row of a section has the heading above it). fn apply(&mut self, changes: &Changes, new: Arc>) { use voelin_ui_vm::list::Change; let mut headed: Vec = self.rows.iter().map(|row| row.first_in_group).collect(); for change in &changes.0 { match *change { Change::Update(i) => { if headed[i] != new[i].first_in_group { self.list.splice(i..i + 1, 1); } headed[i] = new[i].first_in_group; } Change::Remove(i) => { headed.remove(i); self.list.splice(i..i + 1, 0); } Change::Insert(i) => { headed.insert(i, new[i].first_in_group); self.list.splice(i..i, 1); } } } self.rows = new; } /// The search box was opened or closed: closing it shows everyone again. fn toggle_search(&mut self, window: &mut Window, cx: &mut Context) { self.searching = !self.searching; if self.searching { self.search.update(cx, |input, cx| input.focus(window, cx)); } else { self.search.update(cx, |input, cx| input.set_value("", window, cx)); window.dispatch_action(Box::new(SearchMembers { text: String::new() }), cx); } cx.notify(); } /// The entry at `ix` for the list: the heading of its section if it opens /// one, then the person. fn entry(&mut self, ix: usize, cx: &mut Context) -> AnyElement { let theme = VoelinTheme::global(cx).clone(); let Some(item) = self.rows.get(ix) else { return div().into_any_element() }; let m = &theme.metrics; div() .w_full() .px(px(m.space_3)) .flex() .flex_col() .when(item.first_in_group, |entry| entry.child(group_header(&theme, item))) .child(member_row(&theme, item, self.open == Some(item.id))) .into_any_element() } } /// The words of a panel with nobody in it. fn empty_words(emptiness: Emptiness) -> &'static str { match emptiness { Emptiness::Loading => "Loading who is here…", Emptiness::NoServer | Emptiness::Connect => "Connect to see who is here.", } } /// The heading above the first person of a section: "Moderator — 3". fn group_header(theme: &VoelinTheme, item: &MemberItem) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); let words = if item.group_count > 0 { format!("{} — {}", item.group, item.group_count) } else { item.group.to_string() }; div() .flex() .flex_row() .items_center() .gap(px(m.space_2)) .pt(px(m.space_5)) .pb(px(m.space_2)) .pl(px(m.space_2)) .children(small_picture(&item.group_icon, 16.0)) .child( div() .flex_1() .min_w_0() .truncate() .text_size(px(m.font_md)) .font_weight(FontWeight::MEDIUM) .text_color(hsla(c.text_secondary)) .child(words), ) } /// One person: avatar, name with their badges and role, what they are doing. fn member_row(theme: &VoelinTheme, item: &MemberItem, open: bool) -> AnyElement { let (c, m) = (&theme.colors, &theme.metrics); let id = item.id; let hover = hsla(c.surface_3); let presence = if item.away { Presence::Idle } else if item.sound_off { Presence::Dnd } else { Presence::Online }; let name_color = if item.myself { c.accent_text } else if !item.role.is_empty() { c.name_text } else { c.text }; let crowns = div() .flex() .flex_row() .items_center() .gap(px(3.0)) .when(item.admin, |row| row.child(icon("crown", 14.0, c.gold))) .when(item.priority, |row| row.child(icon("star", 14.0, c.accent_text))) .when(item.commander, |row| row.child(icon("flag", 14.0, c.info))) .when(item.recording, |row| row.child(icon("circle-dot", 14.0, c.danger))); let badges = div().flex().flex_row().items_center().gap(px(2.0)).children( [&item.badge, &item.badge_2, &item.badge_3] .into_iter() .filter_map(|b| small_picture(b, 14.0)), ); div() .id(("member", id as u64)) .debug_selector(move || format!("member-{id}")) .w_full() .h(px(52.0 * m.font_scale)) .flex_none() .rounded(px(m.radius_md)) .cursor_pointer() .when(open, |row| row.bg(hsla(c.accent_soft))) .when(!open, |row| row.hover(move |style| style.bg(hover))) .when(item.away, |row| row.opacity(0.6)) .flex() .flex_row() .items_center() .px(px(m.space_2)) .gap(px(m.space_3)) .child( Avatar::new(("member-avatar", id as u64), 40.0, &item.initials, item.tint) .image(item.avatar.clone()) .presence(presence) .speaking(item.talking), ) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .gap(px(1.0)) .child( div() .flex() .flex_row() .items_center() .gap(px(m.space_2)) .child( div() .min_w_0() .truncate() .text_size(px(m.font_md)) .font_weight(FontWeight::MEDIUM) .text_color(hsla(name_color)) .child(item.name.to_string()), ) .child(crowns) .child(badges), ) .child( div() .flex() .flex_row() .items_center() .gap(px(m.space_2)) .child( div() .min_w_0() .truncate() .text_size(px(m.font_sm)) .text_color(hsla(if item.talking { c.success } else { c.text_muted })) .child(item.status.to_string()), ) .when(item.talking, |row| row.child(speaking_bars(theme, 12.0))) .when(item.streaming, |row| { row.child(icon("monitor", 14.0, c.text_secondary)) }), ), ) .when(!item.role.is_empty(), |row| { row.child(icon("shield-check", 18.0, ROLE)).child(chip( theme, item.role.to_string(), None, None, )) }) .when(item.streaming, |row| row.child(live_badge(theme))) .when(item.muted, |row| row.child(icon("mic-off", 16.0, c.danger))) .when(item.sound_off, |row| row.child(icon("headphone-off", 16.0, c.danger))) // A hand, as TeamSpeak asks for talk power; the card has the number. .when(item.cannot_talk, |row| { row.child( div() .id(("member-hand", id as u64)) .tooltip(|window, cx| { gpui_kit::component::tooltip::Tooltip::new("Needs talk power to speak") .build(window, cx) }) .child(icon("hand", 16.0, c.text_muted)), ) }) .on_click(move |_, window, cx| { window.dispatch_action(Box::new(OpenMember { client: id }), cx) }) .into_any_element() } impl Render for MembersPane { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { self.counters.panes.members.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); if (self.measured_at - m.font_scale).abs() > f32::EPSILON { if self.measured_at != 0.0 { self.list.remeasure(); } self.measured_at = m.font_scale; } let searching = self.searching; let this = cx.entity(); let search_button = IconButton::new("members-search", "search", "Search members") .size(32.0) .checked(searching) .on_click(move |_, window, cx| { this.update(cx, |pane, cx| pane.toggle_search(window, cx)); }); div() .id("members") .debug_selector(|| "members".into()) .size_full() .flex() .flex_col() .gap(px(m.space_2)) .pt(px(m.space_4)) .pb(px(m.space_2)) .rounded(px(m.radius_xl)) .border_1() .border_color(hsla(c.border)) .bg(hsla(c.surface.with_alpha(235))) .overflow_hidden() .child( div() .flex() .flex_row() .items_center() .gap(px(m.space_2)) .pl(px(m.space_5)) .pr(px(m.space_3)) .child( div() .id("members-title") .flex_1() .min_w_0() .truncate() .text_size(px(m.font_lg)) .font_weight(FontWeight::MEDIUM) .text_color(hsla(c.text)) .child(format!("Members — {}", self.total)), ) .child(search_button), ) .when(searching, |pane| { pane.child( div() .id("members-search-box") .debug_selector(|| "members-search-box".into()) .px(px(m.space_4)) .child(Input::new(&self.search)), ) }) .map(|pane| { if self.rows.is_empty() { pane.child( div() .id("members-empty") .debug_selector(|| "members-empty".into()) .px(px(m.space_5)) .text_size(px(m.font_sm)) .text_color(hsla(c.text_muted)) .child(empty_words(self.emptiness)), ) } else { pane.child( list( self.list.clone(), cx.processor(|this, ix, _window, cx| this.entry(ix, cx)), ) .flex_1() .w_full() .min_h_0(), ) } }) } } /// The card over a member row, over the window: a scrim that a click outside /// the card closes, and the card with what is known of the person. pub struct MemberCardView { card: Option, /// How wide the members panel is: the card stands beside it. panel_width: f32, _subscription: Subscription, } impl MemberCardView { /// A card that follows `model`, beside a panel `panel_width` pixels wide. pub fn new(model: &Entity, panel_width: f32, cx: &mut Context) -> Self { let card = model.read(cx).card().cloned(); let subscription = cx.subscribe(model, |this: &mut Self, model, event: &TreeEvent, cx| { if matches!(event, TreeEvent::Card) { this.card = model.read(cx).card().cloned(); cx.notify(); } }); Self { card, panel_width, _subscription: subscription } } /// The card that is open. pub fn card(&self) -> Option<&MemberCard> { self.card.as_ref() } } /// The heading of a part of the card: "Groups", "Badges". fn caption(theme: &VoelinTheme, text: &'static str) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); div() .text_size(px(m.font_xs)) .font_weight(FontWeight::SEMIBOLD) .text_color(hsla(c.text_muted)) .child(text) } impl Render for MemberCardView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let Some(person) = self.card.clone() else { return div().into_any_element() }; let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); let facts = div() .flex() .flex_row() .flex_wrap() .gap(px(m.space_2)) .when(person.talk_power > 0, |row| { row.child(chip( &theme, format!("talk power {}", person.talk_power), Some("mic"), None, )) }) .when(!person.country.is_empty(), |row| { row.child(chip(&theme, person.country.to_string(), Some("globe"), None)) }); let body = div() .id("member-card") .debug_selector(|| "member-card".into()) .w(px(300.0)) .flex() .flex_col() .gap(px(m.space_3)) .p(px(m.space_4)) .rounded(px(m.radius_lg)) .border_1() .border_color(hsla(c.border_popup)) .bg(hsla(c.surface_3)) // Clicks inside stay inside. .on_click(|_, _, cx| cx.stop_propagation()) .child( div() .flex() .flex_row() .items_center() .gap(px(m.space_3)) .child( Avatar::new("member-card-avatar", 52.0, &person.initials, person.tint) .image(person.avatar.clone()), ) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .gap(px(2.0)) .child( div() .truncate() .text_size(px(m.font_lg)) .font_weight(FontWeight::SEMIBOLD) .text_color(hsla(c.text)) .child(person.name.to_string()), ) .child( div() .truncate() .text_size(px(m.font_sm)) .text_color(hsla(c.text_muted)) .child(person.status.to_string()), ), ) .child(IconButton::new("member-card-close", "x", "Close").size(26.0).on_click( |_, window, cx| { window.dispatch_action(Box::new(OpenMember { client: -1 }), cx) }, )), ) .when(!person.description.is_empty(), |card| { card.child( div() .text_size(px(m.font_sm)) .text_color(hsla(c.text_secondary)) .child(person.description.to_string()), ) }) .when(!person.groups.is_empty(), |card| { card.child( div() .flex() .flex_col() .gap(px(m.space_2)) .child(caption(&theme, "Groups")) .children(person.groups.iter().map(|group| { div() .truncate() .text_size(px(m.font_sm)) .text_color(hsla(c.text_secondary)) .child(group.to_string()) })), ) }) .when(!person.badges.is_empty(), |card| { card.child( div() .flex() .flex_col() .gap(px(m.space_2)) .child(caption(&theme, "Badges")) .child(div().flex().flex_row().flex_wrap().gap(px(m.space_2)).children( person.badges.iter().enumerate().map(|(i, badge)| { let description = badge.description.clone(); div() .id(("member-badge", i as u64)) .tooltip(move |window, cx| { gpui_kit::component::tooltip::Tooltip::new( description.to_string(), ) .build(window, cx) }) .child(chip( &theme, badge.name.to_string(), None, small_picture(&badge.icon, 14.0), )) }), )), ) }) .child(facts); // The card stands beside the panel, near the top. div() .id("member-card-scrim") .debug_selector(|| "member-card-scrim".into()) .absolute() .top(px(0.0)) .left(px(0.0)) .size_full() .bg(hsla(c.scrim_light)) .on_click(|_, window, cx| { window.dispatch_action(Box::new(OpenMember { client: -1 }), cx) }) .flex() .flex_row() .justify_end() .items_start() .pt(px(120.0)) .pr(px(self.panel_width + 24.0)) .child(body) .into_any_element() } }