Files
Ely-GPUI-Components/src/messaging/thread.rs
T
ZacharyZhang-NYandClaude Opus 5.5 5779daef82 Capture · Roots: roots drop w_full; hosts give the width
145 component roots drop w_full, the early returns included. Three
field rows keep it, Input, InputGroup and TagInput: stretched without
a width, taffy 0.9 laid the field's flex_1 share at no width in the
template picker at 280px, and the placeholder drew nothing. Container
keeps it, since mx_auto needs a width to fill.

Hosts that relied on a root's width now give it: MessageList boxes
each row at the list's width, the splash gives its bar the column's,
and test stages that mount a component as the window's root sit in a
size_full box. Tests hold a narrow row across the list and the splash
bar across its column; each goes red without its fix.

The gallery gains --narrow <px>, which lays each page in a padded
card at that width and shoots it without its script. The page
header wraps: the title takes the room and the theme switcher drops
below, where a long summary used to push it past the edge. A file
panel step waits for the panel to become key instead of a fixed
pause, which lost a race in a capture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 13:07:22 -04:00

255 lines
7.9 KiB
Rust

use std::rc::Rc;
use gpui::{
AnyElement, App, ElementId, FontWeight, IntoElement, ParentElement, RenderOnce, SharedString,
Styled, Window, div, prelude::*,
};
use jiff::Timestamp;
use smallvec::SmallVec;
use crate::{
buttons::{ButtonVariant, IconButton},
data_display::{Avatar, AvatarGroup},
forms::Run,
layout::on_axis,
primitives::{Divider, FocusRing, IconName},
theme::{ActiveTheme, AvatarSize, ControlSize, Radius, TextSize},
typography::{Ellipsis, format},
};
/// A thread's mark under its first message: who replied, how many replies, and when the last came. With a handler, a press opens the thread.
#[derive(IntoElement)]
pub struct MessageThread {
id: ElementId,
replies: usize,
last: Timestamp,
repliers: Vec<Avatar>,
on_open: Option<Run>,
}
impl MessageThread {
pub fn new(id: impl Into<ElementId>, replies: usize, last: Timestamp) -> Self {
assert!(replies > 0, "a thread holds a reply");
Self {
id: id.into(),
replies,
last,
repliers: Vec::new(),
on_open: None,
}
}
/// Who replied; the first three show.
pub fn repliers(mut self, repliers: impl IntoIterator<Item = Avatar>) -> Self {
self.repliers = repliers.into_iter().collect();
assert!(
self.repliers.len() <= self.replies,
"more repliers than replies"
);
self
}
pub fn on_open(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_open = Some(Rc::new(handler));
self
}
}
impl RenderOnce for MessageThread {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let colors = &theme.colors;
let count = format::plural(self.replies as u64, "reply", "replies");
let when = format!(
"Last reply {}",
format::relative(self.last, Timestamp::now())
);
let id = self.id.clone();
div()
.id(self.id)
.debug_selector(|| "message-thread".into())
.flex()
.items_center()
.gap_2()
.px_1p5()
.py_1()
.rounded(theme.radius(Radius::Md))
.border_1()
.border_color(gpui::transparent_black())
.text_size(theme.text_size(TextSize::Sm))
.when_some(self.on_open, |row, open| {
row.cursor_pointer()
.tab_index(0)
.focus_ring(cx)
.hover(|style| style.bg(colors.surface))
.on_click(move |_, window, cx| {
log::info!("message thread {id}: open");
open(window, cx)
})
})
.when(!self.repliers.is_empty(), |row| {
row.child(
div()
.flex_none()
.child(AvatarGroup::new(self.repliers).max(3).size(AvatarSize::Xs)),
)
})
.child(
div()
.flex_none()
.font_weight(FontWeight::SEMIBOLD)
.text_color(colors.link)
.child(count),
)
.child(
div()
.flex_1()
.min_w_0()
.text_color(colors.fg_subtle)
.child(Ellipsis::new(when)),
)
}
}
/// A thread beside its chat: a head with the chat's name and a close, the first message, a rule counting the replies, the replies, and a composer held at the bottom. The body scrolls; the host gives the panel its size.
#[derive(IntoElement)]
pub struct ThreadPanel {
id: ElementId,
root: AnyElement,
chat: Option<SharedString>,
replies: SmallVec<[AnyElement; 4]>,
composer: Option<AnyElement>,
on_close: Option<Run>,
}
impl ThreadPanel {
pub fn new(id: impl Into<ElementId>, root: impl IntoElement) -> Self {
Self {
id: id.into(),
root: root.into_any_element(),
chat: None,
replies: SmallVec::new(),
composer: None,
on_close: None,
}
}
/// The chat the thread is in, after the title.
pub fn chat(mut self, name: impl Into<SharedString>) -> Self {
self.chat = Some(name.into());
self
}
pub fn composer(mut self, composer: impl IntoElement) -> Self {
self.composer = Some(composer.into_any_element());
self
}
pub fn on_close(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_close = Some(Rc::new(handler));
self
}
}
impl ParentElement for ThreadPanel {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.replies.extend(elements);
}
}
impl RenderOnce for ThreadPanel {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let colors = &theme.colors;
let count = self.replies.len();
let close = self.on_close.map(|close| {
let id = self.id.clone();
IconButton::new((self.id.clone(), "close"), IconName::X)
.variant(ButtonVariant::Ghost)
.size(ControlSize::Sm)
.tooltip("Close thread")
.on_click(move |_, window, cx| {
log::info!("thread panel {id}: close");
close(window, cx)
})
});
let head = div()
.flex_none()
.flex()
.items_center()
.justify_between()
.gap_2()
.px_4()
.py_2()
.border_b_1()
.border_color(colors.border)
.child(
div()
.flex_1()
.min_w_0()
.flex()
.items_baseline()
.gap_2()
.child(
div()
.flex_none()
.font_weight(FontWeight::SEMIBOLD)
.text_color(colors.fg)
.child("Thread"),
)
.children(self.chat.map(|chat| {
div()
.flex_1()
.min_w_0()
.text_size(theme.text_size(TextSize::Sm))
.text_color(colors.fg_muted)
.child(Ellipsis::new(chat))
})),
)
.children(close.map(|close| div().flex_none().child(close)));
let rule = (count > 0).then(|| {
div()
.debug_selector(|| "thread-replies".into())
.px_3()
.py_1()
.child(Divider::horizontal().label(format::plural(
count as u64,
"reply",
"replies",
)))
});
let body = on_axis(
div()
.id((self.id.clone(), "body"))
.flex_1()
.min_h_0()
.overflow_y_scroll(),
)
.child(
div()
.flex()
.flex_col()
.gap_1()
.py_2()
.child(self.root)
.children(rule)
.children(self.replies),
);
div()
.debug_selector(|| "thread-panel".into())
.size_full()
.flex()
.flex_col()
.child(head)
.child(body)
.children(self.composer.map(|composer| {
div()
.flex_none()
.p_3()
.border_t_1()
.border_color(colors.border)
.child(composer)
}))
}
}