Files
VoelinClient/crates/voelin-gpui/src/shell/channels.rs
T
faumarayandClaude Sonnet 5.5 f28c02a18c voelin-gpui: a banner row shows its selection and its hover over the picture
A channel row paints its selected and hover backgrounds on the row, and the
banner picture, drawn from the indent on, covers them: a banner row showed a
strip of hover left of the picture and no selection at all. channels.slint
tints the hover over the picture with a translucent child (surface-3 at 50 %),
as the own channel's tint is drawn here; the row now does the same, with a
child above the veil for the selected row (surface 3 at 75 %, a state Slint
has none for) and one for the hover (50 %, through the row's group, so the
pointer anywhere on the row tints it). Our own channel keeps its accent tint.

A test selects two banner rows of the sample server in turn and finds the
tint on the selected one only; docs/ui/gpui-tree-banner-selected-dark.png
shows Raid Night selected and the pointer on Music (release build, lavapipe on
Xvfb :233 with openbox).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-28 07:48:58 +03:00

495 lines
15 KiB
Rust

//! The channel tree of the sidebar (`ChannelTree` of `screens/channels.slint`):
//! the channels of the server on screen with the people in them, nested, with
//! their spacers, pictures, passwords and counts, and the person's marks.
//!
//! A cached pane ([`gpui_kit::Entity::cached`], which needs the size the
//! sidebar gives it) over a virtual list ([`gpui_kit::list`]: only the rows in
//! view are built, the rows differ in height) that follows [`TreeModel`]: the
//! model says which rows changed ([`TreeEvent::Rows`]) and the pane tells its
//! list, so a person who joins is a row inserted and the scroll stays where it
//! was. The pane reads nothing but its own fields while it renders, so
//! nothing but its own notify draws it again. A click selects a channel, a
//! double-click joins it, the chevron folds it, a right-click offers the
//! same; a click on a person is logged (their volume dialog is a slice of
//! its own).
use std::sync::Arc;
use gpui_kit::component::menu::ContextMenuExt as _;
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, Context, Entity, FontWeight, InteractiveElement as _, IntoElement, ListAlignment,
ListState, ObjectFit, ParentElement as _, Render, SharedString,
StatefulInteractiveElement as _, Styled as _, StyledImage as _, Subscription, Window, div,
list, px,
};
use voelin_ui_vm::bridge::TreeItem;
use voelin_ui_vm::tree::ChannelId;
use super::actions::{
EditChannel, JoinChannel, MoveClient, OpenClient, OpenMember, SelectChannel, ToggleChannel,
};
use super::tree_model::{Emptiness, TreeEvent, TreeModel, debug_check};
use super::widgets::{Avatar, banner_scrim, icon, live_badge, small_picture, speaking_bars};
use crate::meter::Counters;
use crate::pictures::{PictureKey, picture};
use crate::theme::{VoelinTheme, hsla};
/// How far each level of the tree is indented.
const INDENT: f32 = 14.0;
/// How opaque the hover and selection tints of a row with a banner are over
/// the banner and its veil: the hover is `channels.slint`'s (`Theme.surface-3.
/// transparentize(0.5)`), the selection, which Slint has no state for, is a
/// stronger one of the same colour.
const BANNER_HOVER: f32 = 0.5;
const BANNER_SELECTED: f32 = 0.75;
/// The channel tree.
pub struct ChannelTree {
rows: Arc<Vec<TreeItem>>,
selected: Option<ChannelId>,
emptiness: Emptiness,
list: ListState,
/// The text size factor the list measured its rows at.
measured_at: f32,
counters: Counters,
_subscription: Subscription,
}
impl ChannelTree {
/// A tree that follows `model`.
pub fn new(model: &Entity<TreeModel>, counters: Counters, cx: &mut Context<Self>) -> Self {
let (rows, selected, emptiness) = {
let model = model.read(cx);
(model.rows().clone(), model.selected(), model.emptiness())
};
let subscription = cx.subscribe(model, |this: &mut Self, model, event: &TreeEvent, cx| {
match event {
TreeEvent::Rows(changes, rows) => this.apply(changes, rows.clone()),
TreeEvent::Selection => this.selected = model.read(cx).selected(),
// The count of people is the members panel's.
TreeEvent::Lists => {
let emptiness = model.read(cx).emptiness();
if emptiness == this.emptiness {
return;
}
this.emptiness = emptiness;
}
TreeEvent::People(..) | TreeEvent::Card => return,
}
cx.notify();
});
Self {
list: ListState::new(rows.len(), ListAlignment::Top, px(240.0)),
rows,
selected,
emptiness,
measured_at: 0.0,
counters,
_subscription: subscription,
}
}
/// The rows it shows.
pub fn rows(&self) -> &[TreeItem] {
&self.rows
}
/// The channel it shows selected.
pub fn selected(&self) -> Option<ChannelId> {
self.selected
}
/// Take the new rows in (the rows `changes` lead to, carried by the event),
/// telling the list which rows came, went or changed their height (a
/// channel row and a person's differ).
fn apply(&mut self, changes: &voelin_ui_vm::list::Changes, new: Arc<Vec<TreeItem>>) {
use voelin_ui_vm::list::Change;
debug_check(self.rows.len(), changes, new.len());
let mut channels: Vec<bool> = self.rows.iter().map(|row| row.is_channel).collect();
for change in &changes.0 {
match *change {
Change::Update(i) => {
if channels[i] != new[i].is_channel {
self.list.splice(i..i + 1, 1);
}
channels[i] = new[i].is_channel;
}
Change::Remove(i) => {
channels.remove(i);
self.list.splice(i..i + 1, 0);
}
Change::Insert(i) => {
channels.insert(i, new[i].is_channel);
self.list.splice(i..i, 1);
}
}
}
self.rows = new;
}
/// The row at `ix`, for the list.
fn row(&mut self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
let theme = VoelinTheme::global(cx).clone();
match self.rows.get(ix) {
Some(item) if item.is_channel => {
channel_row(&theme, item, self.selected == Some(item.id as ChannelId))
}
Some(item) => client_row(&theme, item),
None => div().into_any_element(),
}
}
}
/// The words of a tree with no rows.
fn empty_words(emptiness: Emptiness) -> &'static str {
match emptiness {
Emptiness::NoServer => "No server yet.",
Emptiness::Loading => "Loading the channels…",
Emptiness::Connect => "Connect to see the channels.",
}
}
/// The chevron of a channel, which folds it.
fn chevron(item: &TreeItem, covered: bool, theme: &VoelinTheme) -> impl IntoElement {
let c = &theme.colors;
let id = item.id as ChannelId;
div()
.id(("tree-toggle", id))
.debug_selector(move || format!("tree-toggle-{id}"))
.w(px(18.0))
.h(px(18.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.on_click(move |_, window, cx| {
cx.stop_propagation();
window.dispatch_action(Box::new(ToggleChannel { channel: id }), cx);
})
.child(icon(
if item.collapsed { "chevron-right" } else { "chevron-down" },
14.0,
if covered { c.text } else { c.text_muted },
))
}
/// One channel: its icon (or a speaker, or a lock), its name as the server
/// set it out, how many are in it, and the chevron.
fn channel_row(theme: &VoelinTheme, item: &TreeItem, selected: bool) -> AnyElement {
let (c, m) = (&theme.colors, &theme.metrics);
let id = item.id as ChannelId;
let indent = item.depth as f32 * INDENT;
let banner = PictureKey::of(&item.banner);
let covered = banner.is_some();
let strong = item.own || covered;
let (hover, accent_soft, accent, selected_fill) =
(hsla(c.surface_3), hsla(c.accent_soft), hsla(c.accent), hsla(c.surface_3));
let own = item.own;
let has_icon = PictureKey::of(&item.icon).is_some();
let name_color = if strong { c.text } else { c.text_secondary };
let weight = if strong { FontWeight::SEMIBOLD } else { FontWeight::MEDIUM };
let name = div()
.flex_1()
.min_w_0()
.text_size(px(m.font_body))
.font_weight(weight)
.text_color(hsla(name_color))
.when(item.spacer == 2, |name| name.text_center())
.when(item.spacer == 3, |name| name.text_right())
// A line is cut where the row ends; it does not widen the list.
.when(item.spacer == 4, |name| name.overflow_hidden().whitespace_nowrap())
.when(item.spacer != 4, |name| name.truncate())
.child(item.name.to_string());
let counted = item.spacer == 0 && (item.members > 0 || item.max_members >= 0);
let count = if item.max_members >= 0 {
format!("{}/{}", item.members, item.max_members)
} else {
item.members.to_string()
};
let group = SharedString::from(format!("tree-row-{id}"));
let row = div()
.id(("tree-channel", id))
.debug_selector(move || format!("tree-channel-{id}"))
.group(group.clone())
.relative()
.w_full()
.h(px(34.0 * m.font_scale))
.flex_none()
.overflow_hidden()
.rounded(px(m.radius_md))
.cursor_pointer()
.when(own, |row| row.bg(accent_soft).border_1().border_color(accent))
.when(!own && selected, |row| row.bg(selected_fill))
.when(!own && !selected, |row| row.hover(move |style| style.bg(hover)))
// Artwork behind the same compact row as its title, from the indent on,
// under a veil of the surface colour that keeps the text readable over
// any picture, and under the tints of the row: our own channel's, the
// selected one and the one the pointer is over. The artwork covers the
// row's own background (which only the indent shows), so the tints
// are translucent children above the veil, as `channels.slint` does
// for the hover.
.when_some(banner, |row, key| {
let tint = || {
div()
.absolute()
.top(px(0.0))
.bottom(px(0.0))
.left(px(indent))
.right(px(0.0))
.rounded(px(m.radius_md))
};
row.child(
div()
.absolute()
.top(px(0.0))
.bottom(px(0.0))
.left(px(indent))
.right(px(0.0))
.child(picture(key).size_full().object_fit(ObjectFit::Cover)),
)
.child(
div()
.id(("tree-scrim", id))
.debug_selector(move || format!("tree-scrim-{id}"))
.absolute()
.top(px(0.0))
.bottom(px(0.0))
.left(px(indent))
.right(px(0.0))
.bg(banner_scrim(theme)),
)
.when(own, |row| {
row.child(
div()
.absolute()
.top(px(0.0))
.bottom(px(0.0))
.left(px(indent))
.right(px(0.0))
.rounded(px(m.radius_md))
.bg(accent_soft.opacity(0.65)),
)
})
.when(!own && selected, |row| {
row.child(
tint()
.debug_selector(move || format!("tree-selected-{id}"))
.bg(selected_fill.opacity(BANNER_SELECTED)),
)
})
.when(!own && !selected, |row| {
row.child(
tint().group_hover(group, move |style| style.bg(hover.opacity(BANNER_HOVER))),
)
})
})
.flex()
.flex_row()
.items_center()
.pl(px(m.space_2 + indent))
.pr(px(m.space_3))
.gap(px(m.space_2))
// A centred spacer is balanced against the chevron.
.when(item.spacer == 2, |row| row.child(div().w(px(18.0)).flex_none()))
.when(item.spacer == 0, |row| match small_picture(&item.icon, 16.0) {
Some(picture) => row.child(picture),
None => row.child(icon(
if item.locked { "lock" } else { "volume-2" },
18.0,
if strong { c.text } else { c.icon },
)),
})
.child(name)
.when(item.locked && (has_icon || item.spacer != 0), |row| {
row.child(icon("lock", 14.0, if covered { c.text } else { c.icon }))
})
.when(counted, |row| {
row.child(
div()
.flex_none()
.flex()
.flex_row()
.items_center()
.gap(px(3.0))
.when(own, |count| count.child(icon("users", 14.0, c.text_secondary)))
.child(
div()
.text_size(px(m.font_sm))
.text_color(hsla(if strong { c.text } else { c.text_muted }))
.child(count),
),
)
})
.child(chevron(item, covered, theme))
.on_click(move |event, window, cx| {
if event.click_count() >= 2 {
window.dispatch_action(Box::new(JoinChannel { channel: id }), cx);
} else {
window.dispatch_action(Box::new(SelectChannel { channel: id }), cx);
}
})
.context_menu(move |menu, _, _| {
menu.menu("Join channel", Box::new(JoinChannel { channel: id }))
.menu("Fold or unfold", Box::new(ToggleChannel { channel: id }))
.separator()
.menu("Edit channel…", Box::new(EditChannel { channel: id }))
});
row.into_any_element()
}
/// One person: their avatar, name and badges, and the marks of what they do.
fn client_row(theme: &VoelinTheme, item: &TreeItem) -> AnyElement {
let (c, m) = (&theme.colors, &theme.metrics);
let id = item.id as u64;
let indent = item.depth as f32 * INDENT;
let (hover, myself) = (hsla(c.surface_3), item.myself);
let name_color = if item.away {
c.text_muted
} else if myself {
c.accent_text
} else {
c.text_secondary
};
let mark = |name: &'static str, color| icon(name, 14.0, color);
div()
.id(("tree-client", id))
.debug_selector(move || format!("tree-client-{id}"))
.w_full()
.h(px(30.0 * m.font_scale))
.flex_none()
.rounded(px(m.radius_md))
.cursor_pointer()
.hover(move |style| style.bg(hover))
.flex()
.flex_row()
.items_center()
.pl(px(m.space_2 + indent + 12.0))
.pr(px(m.space_3))
.gap(px(m.space_3))
.child(
Avatar::new(("tree-avatar", id), 22.0, &item.initials, item.tint)
.image(item.avatar.clone())
.speaking(item.talking),
)
.child(
div()
.min_w_0()
.truncate()
.text_size(px(m.font_body))
.font_weight(if myself { FontWeight::SEMIBOLD } else { FontWeight::NORMAL })
.text_color(hsla(name_color))
.child(item.name.to_string()),
)
// Badges after the name, then the free room.
.child(
div().flex_1().min_w_0().flex().flex_row().items_center().gap(px(2.0)).children(
[&item.badge, &item.badge_2, &item.badge_3]
.into_iter()
.filter_map(|badge| small_picture(badge, 14.0)),
),
)
// The Slint bars move; these stand still (nothing here draws the window
// anew with the clock).
.when(item.talking, |row| {
row.child(
div()
.debug_selector(move || format!("tree-speaking-{id}"))
.flex_none()
.child(speaking_bars(theme, 14.0)),
)
})
.when(item.streaming, |row| row.child(live_badge(theme)))
.when(item.local_muted, |row| row.child(mark("volume-x", c.warning)))
.when(!item.local_muted && item.local_volume != 100, |row| {
row.child(
div()
.text_size(px(m.font_xs))
.text_color(hsla(c.text_muted))
.child(format!("{}%", item.local_volume)),
)
})
.when(item.muted, |row| row.child(mark("mic-off", c.danger)))
.when(item.sound_off, |row| row.child(mark("headphone-off", c.danger)))
.when(item.away, |row| {
row.child(div().text_size(px(m.font_xs)).text_color(hsla(c.text_muted)).child("away"))
})
.children(
[&item.icon, &item.icon_2, &item.icon_3]
.into_iter()
.filter_map(|i| small_picture(i, 16.0)),
)
.on_click(move |_, window, cx| {
if !myself {
window.dispatch_action(Box::new(OpenClient { client: id as u16 }), cx);
}
})
.context_menu(move |menu, _, _| {
menu.menu("Show the card", Box::new(OpenMember { client: id as i32 }))
.menu("Volume…", Box::new(OpenClient { client: id as u16 }))
.separator()
.menu("Move to my channel", Box::new(MoveClient { client: id as u16 }))
})
.into_any_element()
}
impl Render for ChannelTree {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.counters.panes.tree.incr();
let theme = VoelinTheme::global(cx).clone();
let (c, m) = (&theme.colors, &theme.metrics);
// A new text size makes the rows another height.
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;
}
div()
.id("sidebar-channels")
.debug_selector(|| "sidebar-channels".into())
.size_full()
.flex()
.flex_col()
.gap(px(m.space_2))
.px(px(m.space_2))
.child(
div()
.min_h(px(24.0))
.flex()
.flex_row()
.items_center()
.gap(px(m.space_2))
.text_size(px(m.font_sm))
.font_weight(FontWeight::SEMIBOLD)
.text_color(hsla(c.text_secondary))
.child(icon("volume-2", m.icon_sm, c.text_secondary))
.child("Channels"),
)
.map(|pane| {
if self.rows.is_empty() {
pane.child(
div()
.id("tree-empty")
.debug_selector(|| "tree-empty".into())
.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.row(ix, cx)),
)
.flex_1()
.w_full()
.min_h_0(),
)
}
})
}
}