1use gpui::{
2 AnyElement, App, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window, div,
3 prelude::FluentBuilder as _, relative, rems,
4};
5
6use crate::{ActiveTheme as _, Colorize as _, button::Button, message::MessageAlignment, v_flex};
7
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10pub enum BubbleVariant {
11 #[default]
13 Filled,
14 Secondary,
16 Muted,
18 Tinted,
20 Outline,
22 Ghost,
24 Destructive,
26}
27
28#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
30pub enum BubbleReactionSide {
31 Top,
33 #[default]
35 Bottom,
36}
37
38#[derive(IntoElement)]
43pub struct Bubble {
44 style: StyleRefinement,
45 alignment: Option<MessageAlignment>,
46 variant: BubbleVariant,
47 content: BubbleContent,
48 reactions: Option<BubbleReactions>,
49}
50
51impl Bubble {
52 pub fn new() -> Self {
54 Self {
55 style: StyleRefinement::default(),
56 alignment: None,
57 variant: BubbleVariant::Filled,
58 content: BubbleContent::new(),
59 reactions: None,
60 }
61 }
62
63 pub fn alignment(mut self, alignment: MessageAlignment) -> Self {
65 self.alignment = Some(alignment);
66 self
67 }
68
69 pub fn with_variant(mut self, variant: BubbleVariant) -> Self {
71 self.variant = variant;
72 self
73 }
74
75 pub(crate) fn is_ghost(&self) -> bool {
76 self.variant == BubbleVariant::Ghost
77 }
78
79 pub fn content(mut self, mut content: BubbleContent) -> Self {
85 let mut children = std::mem::take(&mut self.content.children);
86 children.append(&mut content.children);
87 content.children = children;
88 self.content = content;
89 self
90 }
91
92 pub fn reactions(mut self, reactions: BubbleReactions) -> Self {
94 self.reactions = Some(reactions);
95 self
96 }
97}
98
99impl Default for Bubble {
100 fn default() -> Self {
101 Self::new()
102 }
103}
104
105impl ParentElement for Bubble {
106 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
107 self.content.children.extend(elements);
108 }
109}
110
111impl Styled for Bubble {
112 fn style(&mut self) -> &mut StyleRefinement {
113 &mut self.style
114 }
115}
116
117impl RenderOnce for Bubble {
118 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
119 let variant = self.variant;
120 let mut content = self.content;
121 content.variant = variant;
122 content.alignment = self.alignment;
123
124 div()
125 .relative()
126 .flex()
127 .min_w_0()
128 .flex_col()
129 .flex_none()
130 .gap_1()
131 .max_w(relative(0.8))
132 .when(variant == BubbleVariant::Ghost, |this| {
133 this.w_full().max_w_full()
134 })
135 .when_some(self.alignment, |this, alignment| match alignment {
136 MessageAlignment::Start => this.self_start().mr_auto(),
137 MessageAlignment::End => this.self_end().ml_auto(),
138 })
139 .refine_style(&self.style)
140 .child(content)
141 .when_some(self.reactions, |this, reactions| this.child(reactions))
142 }
143}
144
145#[derive(IntoElement)]
150pub struct BubbleContent {
151 style: StyleRefinement,
152 variant: BubbleVariant,
153 alignment: Option<MessageAlignment>,
154 children: Vec<AnyElement>,
155}
156
157impl BubbleContent {
158 pub fn new() -> Self {
160 Self {
161 style: StyleRefinement::default(),
162 variant: BubbleVariant::default(),
163 alignment: None,
164 children: Vec::new(),
165 }
166 }
167}
168
169impl Default for BubbleContent {
170 fn default() -> Self {
171 Self::new()
172 }
173}
174
175impl ParentElement for BubbleContent {
176 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
177 self.children.extend(elements);
178 }
179}
180
181impl Styled for BubbleContent {
182 fn style(&mut self) -> &mut StyleRefinement {
183 &mut self.style
184 }
185}
186
187impl RenderOnce for BubbleContent {
188 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
189 let tokens = cx.theme().semantic_tokens();
190
191 div()
192 .min_w_0()
193 .max_w_full()
194 .when(self.variant != BubbleVariant::Ghost, |this| {
197 this.overflow_hidden()
198 })
199 .rounded(cx.theme().radius_2xl())
200 .border_1()
201 .border_color(cx.theme().transparent)
202 .px_3()
203 .py_2()
204 .text_sm()
205 .line_height(relative(1.625))
206 .when_some(self.alignment, |this, alignment| match alignment {
207 MessageAlignment::Start => this.self_start(),
208 MessageAlignment::End => this.self_end(),
209 })
210 .map(|this| match self.variant {
211 BubbleVariant::Filled => this
212 .bg(tokens.colors.primary)
213 .text_color(tokens.colors.primary_foreground),
214 BubbleVariant::Secondary => this
219 .bg(tokens.colors.muted)
220 .text_color(tokens.colors.secondary_foreground),
221 BubbleVariant::Muted => this
222 .bg(tokens.colors.muted)
223 .text_color(tokens.colors.foreground),
224 BubbleVariant::Tinted => this
225 .bg(tokens.colors.primary.mix_oklab(
226 tokens.colors.background,
227 if cx.theme().is_dark() { 0.24 } else { 0.12 },
228 ))
229 .text_color(tokens.colors.foreground),
230 BubbleVariant::Outline => this
231 .border_color(tokens.colors.border)
232 .bg(tokens.colors.background)
233 .text_color(tokens.colors.foreground),
234 BubbleVariant::Ghost => this
235 .rounded(tokens.radius.none)
236 .border_0()
237 .bg(cx.theme().transparent)
238 .text_color(tokens.colors.foreground)
239 .p_0(),
240 BubbleVariant::Destructive => this
241 .bg(tokens.colors.destructive.opacity(if cx.theme().is_dark() {
242 0.2
243 } else {
244 0.1
245 }))
246 .text_color(tokens.colors.destructive),
247 })
248 .refine_style(&self.style)
249 .children(self.children)
250 }
251}
252
253#[derive(IntoElement)]
255pub struct BubbleGroup {
256 style: StyleRefinement,
257 children: Vec<AnyElement>,
258}
259
260impl BubbleGroup {
261 pub fn new() -> Self {
263 Self {
264 style: StyleRefinement::default(),
265 children: Vec::new(),
266 }
267 }
268}
269
270impl Default for BubbleGroup {
271 fn default() -> Self {
272 Self::new()
273 }
274}
275
276impl ParentElement for BubbleGroup {
277 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
278 self.children.extend(elements);
279 }
280}
281
282impl Styled for BubbleGroup {
283 fn style(&mut self) -> &mut StyleRefinement {
284 &mut self.style
285 }
286}
287
288impl RenderOnce for BubbleGroup {
289 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
290 v_flex()
291 .min_w_0()
292 .gap_2()
293 .refine_style(&self.style)
294 .children(self.children)
295 }
296}
297
298enum BubbleReactionChild {
299 Action(Box<Button>),
300 Element(AnyElement),
301}
302
303#[derive(IntoElement)]
308pub struct BubbleReactions {
309 style: StyleRefinement,
310 side: BubbleReactionSide,
311 alignment: MessageAlignment,
312 children: Vec<BubbleReactionChild>,
313}
314
315impl BubbleReactions {
316 pub fn new() -> Self {
318 Self {
319 style: StyleRefinement::default(),
320 side: BubbleReactionSide::Bottom,
321 alignment: MessageAlignment::End,
322 children: Vec::new(),
323 }
324 }
325
326 pub fn side(mut self, side: BubbleReactionSide) -> Self {
328 self.side = side;
329 self
330 }
331
332 pub fn alignment(mut self, alignment: MessageAlignment) -> Self {
334 self.alignment = alignment;
335 self
336 }
337
338 pub fn action(mut self, action: Button) -> Self {
346 self.children
347 .push(BubbleReactionChild::Action(Box::new(action)));
348 self
349 }
350}
351
352impl Default for BubbleReactions {
353 fn default() -> Self {
354 Self::new()
355 }
356}
357
358impl ParentElement for BubbleReactions {
359 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
360 self.children
361 .extend(elements.into_iter().map(BubbleReactionChild::Element));
362 }
363}
364
365impl Styled for BubbleReactions {
366 fn style(&mut self) -> &mut StyleRefinement {
367 &mut self.style
368 }
369}
370
371impl RenderOnce for BubbleReactions {
372 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
373 let tokens = cx.theme().semantic_tokens();
374 let has_action = self
375 .children
376 .iter()
377 .any(|child| matches!(child, BubbleReactionChild::Action(_)));
378 let action_radius = cx.theme().radius_full();
379 let children = self.children.into_iter().map(move |child| match child {
380 BubbleReactionChild::Action(action) => {
381 (*action).rounded(action_radius).into_any_element()
382 }
383 BubbleReactionChild::Element(element) => element,
384 });
385
386 div()
387 .absolute()
388 .flex()
389 .flex_none()
390 .items_center()
391 .justify_center()
392 .gap_1()
393 .rounded(cx.theme().radius_full())
394 .border_3()
395 .border_color(tokens.colors.background)
396 .bg(tokens.colors.muted)
397 .text_color(tokens.colors.foreground)
398 .when(!has_action, |this| this.px_1p5().py_0p5())
399 .text_sm()
400 .when(self.side == BubbleReactionSide::Top, |this| {
404 this.top(-rems(1.25))
405 })
406 .when(self.side == BubbleReactionSide::Bottom, |this| {
407 this.bottom(-rems(1.25))
408 })
409 .when(self.alignment == MessageAlignment::Start, |this| {
410 this.left_3()
411 })
412 .when(self.alignment == MessageAlignment::End, |this| {
413 this.right_3()
414 })
415 .refine_style(&self.style)
416 .children(children)
417 }
418}
419
420#[cfg(test)]
421mod tests {
422 use super::*;
423
424 #[test]
425 fn test_bubble_builder() {
426 let bubble = Bubble::new()
427 .alignment(MessageAlignment::End)
428 .with_variant(BubbleVariant::Outline)
429 .content(BubbleContent::new().child("Hello"))
430 .reactions(BubbleReactions::new().child("👍"));
431
432 assert_eq!(bubble.alignment, Some(MessageAlignment::End));
433 assert_eq!(bubble.variant, BubbleVariant::Outline);
434 assert_eq!(bubble.content.children.len(), 1);
435 assert!(bubble.reactions.is_some());
436
437 let reordered = Bubble::new()
440 .child("Existing")
441 .content(BubbleContent::new().child("Configured"));
442 assert_eq!(reordered.content.children.len(), 2);
443
444 let group = BubbleGroup::new().child("First").child("Second");
445 assert_eq!(group.children.len(), 2);
446
447 let reactions = BubbleReactions::new()
448 .side(BubbleReactionSide::Top)
449 .alignment(MessageAlignment::Start)
450 .child("👍 2");
451
452 assert_eq!(reactions.side, BubbleReactionSide::Top);
453 assert_eq!(reactions.alignment, MessageAlignment::Start);
454 assert_eq!(reactions.children.len(), 1);
455
456 let reactions = BubbleReactions::new()
457 .action(Button::new("reaction-action"))
458 .child("👍");
459 assert_eq!(reactions.children.len(), 2);
460 assert!(matches!(
461 reactions.children.first(),
462 Some(BubbleReactionChild::Action(_))
463 ));
464 assert!(matches!(
465 reactions.children.get(1),
466 Some(BubbleReactionChild::Element(_))
467 ));
468 }
469}