1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Bubble: conversational content in a chat bubble.
use std::rc::Rc;
use gpui::{
div, prelude::*, px, AnyElement, App, ElementId, Pixels, SharedString, StyleRefinement, Window,
};
use super::overlay::child_id;
use crate::sx::SxStyled;
use crate::{
hooks::{use_keyed_state, EventHandler},
styles,
styles::ApplyStyleOverrides,
theme::ActiveTheme,
};
/// Fill of a [`Bubble`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BubbleVariant {
/// Muted fill: messages from others.
#[default]
Default,
/// Primary fill: your own messages.
Primary,
/// Bordered, no fill.
Outline,
/// No fill or padding: assistant replies that read like a document.
Ghost,
}
/// Which side the bubble's tail is on.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BubbleAlign {
/// At the reading-direction start (messages from others).
#[default]
Start,
/// At the reading-direction end (your own messages).
End,
}
/// Position within a run of bubbles from the same sender. Corners on the tail
/// side tighten so a run reads as one group.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BubbleGroupPosition {
/// A message on its own: every corner rounded.
#[default]
Single,
/// The first of consecutive messages from one sender.
First,
/// Between the first and last of a group.
Middle,
/// The last of consecutive messages from one sender.
Last,
}
/// A reaction chip under a bubble.
#[derive(Clone)]
pub struct BubbleReaction {
emoji: SharedString,
count: usize,
reacted: bool,
}
impl BubbleReaction {
/// An `emoji` reaction with its `count`.
pub fn new(emoji: impl Into<SharedString>, count: usize) -> Self {
Self {
emoji: emoji.into(),
count,
reacted: false,
}
}
/// Highlight the chip: the current user added this reaction.
#[must_use]
pub fn reacted(mut self, reacted: bool) -> Self {
self.reacted = reacted;
self
}
}
/// ```no_run
/// # use rok_ui::prelude::*;
/// # fn example(window: &mut Window, cx: &mut App) {
/// # let long_text = "A long message.";
/// let question = Bubble::new("m1")
/// .align(BubbleAlign::End)
/// .variant(BubbleVariant::Primary)
/// .group_position(BubbleGroupPosition::First)
/// .child("Are we still on for tomorrow?");
///
/// let reply = Bubble::new("m2")
/// .reaction(BubbleReaction::new("👍", 2))
/// .collapse_after(px(120.))
/// .child(long_text);
/// # }
/// ```
#[derive(IntoElement)]
pub struct Bubble {
id: ElementId,
variant: BubbleVariant,
align: BubbleAlign,
group_position: BubbleGroupPosition,
reactions: Vec<BubbleReaction>,
on_reaction: Option<EventHandler<SharedString>>,
collapse_after: Option<Pixels>,
children: Vec<AnyElement>,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Bubble);
impl Bubble {
/// Create the component. `id` must be unique among its siblings; it keys the component's state.
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
variant: BubbleVariant::Default,
align: BubbleAlign::Start,
group_position: BubbleGroupPosition::Single,
reactions: Vec::new(),
on_reaction: None,
collapse_after: None,
children: Vec::new(),
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
/// The visual variant.
#[must_use]
pub fn variant(mut self, variant: BubbleVariant) -> Self {
self.variant = variant;
self
}
/// Which side of the conversation the bubble sits on.
#[must_use]
pub fn align(mut self, align: BubbleAlign) -> Self {
self.align = align;
self
}
/// Where the bubble sits in a run of messages from one sender.
#[must_use]
pub fn group_position(mut self, position: BubbleGroupPosition) -> Self {
self.group_position = position;
self
}
/// Add a reaction below the bubble.
#[must_use]
pub fn reaction(mut self, reaction: BubbleReaction) -> Self {
self.reactions.push(reaction);
self
}
/// Called with the emoji when a reaction chip is clicked.
#[must_use]
pub fn on_reaction(
mut self,
handler: impl Fn(&SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_reaction = Some(Rc::new(handler));
self
}
/// Clip content taller than `height` behind a "Show more" toggle.
#[must_use]
pub fn collapse_after(mut self, height: impl Into<Pixels>) -> Self {
self.collapse_after = Some(height.into());
self
}
}
impl ParentElement for Bubble {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
styles! {
BUBBLE = {
// Physical sides: end-aligned bubbles sit on the right in LTR and the left in RTL.
column: { display: flex, direction: column, gap: 1 },
column_left: { align: start },
column_right: { align: end },
bubble: {
display: flex,
direction: column,
max_width: 120,
padding_x: 3.5,
padding_y: 2,
border: 1,
text: sm,
},
variant(BubbleVariant): {
Default: { background: muted, color: foreground, border_color: muted },
Primary: { background: primary, color: primary_foreground, border_color: primary },
Outline: { background: transparent, color: foreground, border_color: border },
Ghost: {
padding_x: 0,
padding_y: 0,
background: transparent,
color: foreground,
border_color: transparent,
},
},
content: { position: relative, display: flex, direction: column, gap: 1 },
fade: { position: absolute, bottom: 0, left: 0, width: full, height: 6 },
collapse_toggle: {
padding_top: 1,
text: xs,
font: medium,
opacity: 0.8,
cursor: pointer,
hover: { underline: true },
},
reactions: { display: flex, wrap: true, gap: 1 },
reaction: {
display: flex,
align: center,
gap: 1,
height: 6,
padding_x: 2,
radius: full,
border: 1,
border_color: border,
background: background,
text: xs,
},
reacted: { border_color: ring, background: accent },
reaction_clickable: { cursor: pointer },
}
}
impl RenderOnce for Bubble {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let expanded = use_keyed_state(child_id(&self.id, "expanded"), window, cx, || false);
let is_expanded = expanded.get(cx);
let theme = cx.theme();
let colors = theme.colors.clone();
let large = theme.radius_extra_large().max(px(12.));
let small = px(4.);
// Collapsed content fades out into the bubble fill (or the page behind a
// transparent bubble) instead of cutting a line in half.
let fade_color = match self.variant {
BubbleVariant::Default => colors.muted,
BubbleVariant::Primary => colors.primary,
BubbleVariant::Outline | BubbleVariant::Ghost => colors.background,
};
let on_right = (self.align == BubbleAlign::End) ^ super::direction::is_rtl();
// Tail-side corners: the bottom one is the tail; the top one tightens when
// the bubble continues a run from the same sender.
let tail_top = match self.group_position {
BubbleGroupPosition::Single | BubbleGroupPosition::First => large,
BubbleGroupPosition::Middle | BubbleGroupPosition::Last => small,
};
let tail_bottom = small;
let collapse_toggle = self.collapse_after.map(|_| {
let expanded = expanded.clone();
div()
.id("bubble-collapse-toggle")
.sx(&BUBBLE.collapse_toggle)
.on_click(move |_, _, cx| expanded.update(cx, |expanded| *expanded = !*expanded))
.child(if is_expanded {
"Show less"
} else {
"Show more"
})
});
let content = div().sx(&BUBBLE.content).children(self.children).when_some(
self.collapse_after.filter(|_| !is_expanded),
|content, height| {
content
.max_h(height)
.overflow_hidden()
.child(div().sx(&BUBBLE.fade).bg(gpui::linear_gradient(
180.,
gpui::linear_color_stop(fade_color.opacity(0.), 0.),
gpui::linear_color_stop(fade_color, 1.),
)))
},
);
let bubble = div()
.sx((&BUBBLE.bubble, BUBBLE.variant(self.variant)))
// The tail is on the left for start-aligned bubbles in LTR, the right in RTL.
.map(|bubble| {
if on_right {
bubble
.rounded_tl(large)
.rounded_bl(large)
.rounded_tr(tail_top)
.rounded_br(tail_bottom)
} else {
bubble
.rounded_tr(large)
.rounded_br(large)
.rounded_tl(tail_top)
.rounded_bl(tail_bottom)
}
})
.child(content)
.children(collapse_toggle);
let reactions = (!self.reactions.is_empty()).then(|| {
let on_reaction = self.on_reaction.clone();
div()
.sx(&BUBBLE.reactions)
.children(
self.reactions
.into_iter()
.enumerate()
.map(|(index, reaction)| {
let on_reaction = on_reaction.clone();
let emoji = reaction.emoji.clone();
div()
.id(("bubble-reaction", index))
.sx((
&BUBBLE.reaction,
reaction.reacted.then_some(&BUBBLE.reacted),
on_reaction.is_some().then_some(&BUBBLE.reaction_clickable),
))
.when_some(on_reaction, |chip, handler| {
chip.on_click(move |_, window, cx| handler(&emoji, window, cx))
})
.child(reaction.emoji)
.child(reaction.count.to_string())
}),
)
});
div()
.id(self.id)
.sx((
&BUBBLE.column,
if on_right {
&BUBBLE.column_right
} else {
&BUBBLE.column_left
},
&self.sx,
))
.child(bubble)
.children(reactions)
.apply_style_overrides(&self.style_overrides)
}
}