BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / toast.rs

toast.rs

toast.rs 20.6 KiB Download Raw
1use std::time::Instant;
2
3use gpui_kit::component::{
4 h_flex,
5 input::{InputEvent, Textarea, TextareaState},
6 v_flex,
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10
11use super::agent_toast::agent_toast_content;
12use super::center::NotificationCenter;
13use super::layout::{
14 ACTION_BUTTON, AVATAR_GAP, AVATAR_SIZE, MENTION_BORDER, PREVIEW_MAX_LINES, PREVIEW_SIZE,
15 SEND_BUTTON, TOAST_PADDING,
16};
17use super::platform;
18use super::rules::ChatKind;
19use super::stack::{ReplyState, ToastModel};
20use super::text::{ToastText, describe, mention_ranges};
21use crate::sidebar_model::{AvatarSpec, Face};
22use crate::theme;
23use crate::views::avatar::{spec_avatar, with_presence};
24use crate::views::composer::submitted_text;
25use crate::views::widgets::symbol;
26
27const CLOSE_SIZE: f32 = 24.;
28const TOAST_RADIUS: f32 = 12.;
29
30pub struct ToastView {
31 center: Entity<NotificationCenter>,
32 id: u64,
33 input: Entity<TextareaState>,
34 focus_handle: FocusHandle,
35 last_title: String,
36 _subscriptions: Vec<Subscription>,
37}
38
39impl ToastView {
40 pub fn new(
41 center: Entity<NotificationCenter>,
42 id: u64,
43 window: &mut Window,
44 cx: &mut Context<Self>,
45 ) -> Self {
46 let input = cx.new(|cx| {
47 TextareaState::new(window, cx)
48 .auto_grow(1, 1)
49 .submit_on_enter(true)
50 .placeholder("Reply ...")
51 });
52 let subscriptions = vec![
53 cx.subscribe_in(
54 &input,
55 window,
56 |this, input, event: &InputEvent, window, cx| {
57 let value = input.read(cx).value();
58 if submitted_text(event, &value).is_some() {
59 this.submit(window, cx);
60 }
61 },
62 ),
63 cx.observe(&center, |_, _, cx| cx.notify()),
64 ];
65 ToastView {
66 center,
67 id,
68 input,
69 focus_handle: cx.focus_handle(),
70 last_title: String::new(),
71 _subscriptions: subscriptions,
72 }
73 }
74
75 fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
76 let text = self.input.read(cx).value().trim().to_owned();
77 if text.is_empty() {
78 return;
79 }
80 self.input
81 .update(cx, |state, cx| state.set_value("", window, cx));
82 let id = self.id;
83 self.center
84 .update(cx, |center, cx| center.submit_reply(id, text, cx));
85 }
86
87 fn open_reply(&mut self, window: &mut Window, cx: &mut Context<Self>) {
88 let id = self.id;
89 let draft = self
90 .center
91 .update(cx, |center, cx| center.open_reply(id, cx));
92 if let Some(native) = platform::native_handle(window) {
93 platform::set_activatable(native, true);
94 }
95 let input = self.input.clone();
96 cx.defer_in(window, move |_, window, cx| {
97 input.update(cx, |state, cx| {
98 if let Some(draft) = draft.filter(|draft| !draft.is_empty()) {
99 state.set_value(&draft, window, cx);
100 }
101 state.focus(window, cx);
102 });
103 });
104 }
105
106 fn close_reply(&mut self, window: &mut Window, cx: &mut Context<Self>) {
107 self.input
108 .update(cx, |state, cx| state.set_value("", window, cx));
109 let id = self.id;
110 self.center
111 .update(cx, |center, cx| center.close_reply(id, cx));
112 window.focus(&self.focus_handle, cx);
113 }
114
115 fn retry(&mut self, cx: &mut Context<Self>) {
116 let id = self.id;
117 self.center.update(cx, |center, cx| {
118 if let Some(text) = center.reply_text(id) {
119 center.submit_reply(id, text, cx);
120 }
121 });
122 }
123}
124
125fn flat_button(
126 label: &'static str,
127 symbol_name: &'static str,
128 accent: bool,
129 mention: bool,
130) -> Stateful<Div> {
131 let color = if accent {
132 theme::accent_text()
133 } else {
134 theme::text_soft()
135 };
136 let hover_fill = if mention {
137 theme::bubble_own()
138 } else {
139 theme::border_strong()
140 };
141 h_flex()
142 .id(label)
143 .h(px(ACTION_BUTTON))
144 .px(px(10.))
145 .gap(px(6.))
146 .items_center()
147 .rounded(px(6.))
148 .cursor_pointer()
149 .text_size(px(12.5))
150 .font_weight(FontWeight::SEMIBOLD)
151 .text_color(color)
152 .hover(move |button| button.bg(hover_fill))
153 .child(symbol(symbol_name, 15., color))
154 .child(label)
155}
156
157fn highlighted_preview(text: &str, mention: bool) -> AnyElement {
158 let mut styled = StyledText::new(text.to_owned());
159 if mention {
160 let style = HighlightStyle {
161 color: Some(theme::accent_soft()),
162 font_weight: Some(FontWeight::SEMIBOLD),
163 ..Default::default()
164 };
165 let highlights: Vec<_> = mention_ranges(text)
166 .into_iter()
167 .map(|range| (range, style))
168 .collect();
169 styled = styled.with_highlights(highlights);
170 }
171 div()
172 .text_size(px(PREVIEW_SIZE))
173 .line_height(px(18.))
174 .text_color(theme::text())
175 .line_clamp(PREVIEW_MAX_LINES)
176 .child(styled)
177 .into_any_element()
178}
179
180fn side_chip(label: String, mention: bool) -> Div {
181 div()
182 .h(px(20.))
183 .px(px(8.))
184 .flex()
185 .items_center()
186 .rounded(px(10.))
187 .bg(if mention {
188 theme::bubble_own()
189 } else {
190 theme::border_strong()
191 })
192 .text_color(if mention {
193 theme::accent_soft()
194 } else {
195 theme::text()
196 })
197 .text_size(px(11.))
198 .line_height(px(20.))
199 .font_weight(FontWeight::SEMIBOLD)
200 .child(label)
201}
202
203impl ToastView {
204 fn avatar(&self, model: &ToastModel, fill: Hsla, cx: &App) -> AnyElement {
205 let center = self.center.read(cx);
206 let directory = center.directory(cx);
207 let sender = Face {
208 user_id: model.sender_id.clone(),
209 name: model.sender_name.clone(),
210 };
211 match &model.kind {
212 ChatKind::Group { .. } => {
213 let chat = Face {
214 user_id: None,
215 name: model.chat_title.clone(),
216 };
217 spec_avatar(
218 directory,
219 &AvatarSpec::Pair(sender, chat),
220 AVATAR_SIZE,
221 fill,
222 )
223 }
224 _ => {
225 let presence = model
226 .sender_id
227 .as_deref()
228 .map(|user_id| directory.presence_of(user_id))
229 .unwrap_or(crate::data::Presence::Live(
230 crate::data::PresenceKind::Unknown,
231 ));
232 let face = spec_avatar(directory, &AvatarSpec::Single(sender), AVATAR_SIZE, fill);
233 with_presence(face, presence, AVATAR_SIZE, fill).into_any_element()
234 }
235 }
236 }
237
238 fn side_slot(&self, model: &ToastModel, hovered: bool) -> AnyElement {
239 let id = self.id;
240 if hovered {
241 let center = self.center.clone();
242 return div()
243 .id("toast-close")
244 .size(px(CLOSE_SIZE))
245 .flex_none()
246 .flex()
247 .items_center()
248 .justify_center()
249 .rounded_full()
250 .bg(theme::border_strong())
251 .cursor_pointer()
252 .hover(|button| button.bg(theme::text_faint()))
253 .child(symbol("close", 16., theme::text()))
254 .on_click(move |_, _, cx| {
255 cx.stop_propagation();
256 center.update(cx, |center, cx| center.dismiss(id, cx));
257 })
258 .into_any_element();
259 }
260 if model.mentions_me {
261 return side_chip("Mention".to_owned(), true).into_any_element();
262 }
263 if model.count > 1 {
264 return side_chip(format!("{} new", model.count), false).into_any_element();
265 }
266 div()
267 .text_size(px(12.))
268 .text_color(theme::text_muted())
269 .child(model.time.clone())
270 .into_any_element()
271 }
272
273 fn body(&self, model: &ToastModel, text: &ToastText, hovered: bool) -> Div {
274 let subtitle_icon = match &model.kind {
275 ChatKind::Group { .. } => Some("group"),
276 ChatKind::Channel { .. } => Some("tag"),
277 ChatKind::Direct => None,
278 };
279 let preview = if text.image_only {
280 h_flex()
281 .gap(px(6.))
282 .items_center()
283 .child(symbol("image", 16., theme::text_muted()))
284 .child(
285 div()
286 .text_size(px(13.))
287 .text_color(theme::text())
288 .child(text.preview.clone()),
289 )
290 .into_any_element()
291 } else {
292 highlighted_preview(&text.preview, model.mentions_me)
293 };
294 v_flex()
295 .flex_1()
296 .min_w_0()
297 .gap(px(2.))
298 .child(
299 h_flex()
300 .w_full()
301 .h(px(20.))
302 .gap(px(8.))
303 .items_center()
304 .justify_between()
305 .child(
306 div()
307 .flex_1()
308 .min_w_0()
309 .truncate()
310 .text_size(px(14.))
311 .line_height(px(20.))
312 .font_weight(FontWeight::SEMIBOLD)
313 .text_color(theme::text())
314 .child(text.title.clone()),
315 )
316 .child(div().flex_none().child(self.side_slot(model, hovered))),
317 )
318 .child(
319 h_flex()
320 .gap(px(4.))
321 .items_center()
322 .h(px(16.))
323 .text_size(px(12.))
324 .text_color(theme::text_muted())
325 .children(subtitle_icon.map(|name| symbol(name, 14., theme::text_muted())))
326 .child(div().truncate().child(text.subtitle.clone())),
327 )
328 .child(preview)
329 }
330
331 fn action_row(&self, mention: bool, cx: &mut Context<Self>) -> Div {
332 let id = self.id;
333 let center = self.center.clone();
334 h_flex()
335 .w_full()
336 .mt(px(4.))
337 .gap(px(4.))
338 .justify_end()
339 .child(
340 flat_button("Mark as read", "done", false, mention).on_click(move |_, _, cx| {
341 cx.stop_propagation();
342 center.update(cx, |center, cx| center.mark_read(id, cx));
343 }),
344 )
345 .child(
346 flat_button("Reply", "reply", true, mention).on_click(cx.listener(
347 |this, _, window, cx| {
348 cx.stop_propagation();
349 this.open_reply(window, cx);
350 },
351 )),
352 )
353 }
354
355 fn reply_field(&self, window: &Window, cx: &mut Context<Self>) -> Div {
356 let focused = self.input.focus_handle(cx).contains_focused(window, cx);
357 let empty = self.input.read(cx).value().trim().is_empty();
358 let send = div()
359 .id("toast-send")
360 .size(px(SEND_BUTTON))
361 .flex_none()
362 .flex()
363 .items_center()
364 .justify_center()
365 .rounded(px(8.))
366 .child(symbol("keyboard_return", 20., white()))
367 .when(empty, |button| button.opacity(0.4))
368 .when(!empty, |button| {
369 button
370 .cursor_pointer()
371 .hover(|button| button.bg(white().opacity(0.1)))
372 .on_click(cx.listener(|this, _, window, cx| {
373 cx.stop_propagation();
374 this.submit(window, cx);
375 }))
376 });
377 let hint = |keys: &'static str, action: &'static str| {
378 h_flex()
379 .gap(px(4.))
380 .child(div().text_color(theme::text_soft()).child(keys))
381 .child(action)
382 };
383 v_flex()
384 .w_full()
385 .mt(px(8.))
386 .gap(px(4.))
387 .child(
388 h_flex()
389 .w_full()
390 .items_end()
391 .gap(px(6.))
392 .py(px(6.))
393 .pl(px(12.))
394 .pr(px(6.))
395 .rounded(px(10.))
396 .bg(theme::surface())
397 .border_1()
398 .border_color(if focused {
399 theme::accent()
400 } else {
401 theme::border_strong()
402 })
403 .child(
404 div()
405 .flex_1()
406 .min_w_0()
407 .child(Textarea::new(&self.input).appearance(false).bordered(false)),
408 )
409 .child(send),
410 )
411 .child(
412 h_flex()
413 .h(px(16.))
414 .gap(px(12.))
415 .justify_end()
416 .text_size(px(11.))
417 .text_color(theme::text_muted())
418 .child(hint("Enter", "send"))
419 .child(hint("Shift+Enter", "new line"))
420 .child(hint("Esc", "close")),
421 )
422 }
423
424 fn confirmation(&self, model: &ToastModel, cx: &mut Context<Self>) -> Div {
425 let sent = model.reply == ReplyState::Sent;
426 let label = if sent {
427 format!("Sent to {}", target_name(model))
428 } else {
429 "Couldn't send".to_owned()
430 };
431 let (icon_name, tone) = if sent {
432 ("check_circle", theme::green())
433 } else {
434 ("error", theme::red_soft())
435 };
436 h_flex()
437 .size_full()
438 .gap(px(10.))
439 .items_center()
440 .child(symbol(icon_name, 22., tone))
441 .child(
442 div()
443 .flex_1()
444 .min_w_0()
445 .truncate()
446 .text_size(px(14.))
447 .font_weight(FontWeight::SEMIBOLD)
448 .text_color(theme::text())
449 .child(label),
450 )
451 .when(sent, |row| {
452 row.child(
453 div()
454 .text_size(px(12.))
455 .text_color(theme::text_muted())
456 .child(model.time.clone()),
457 )
458 })
459 .when(!sent, |row| {
460 row.child(
461 flat_button("Retry", "reply", true, false).on_click(cx.listener(
462 |this, _, _, cx| {
463 cx.stop_propagation();
464 this.retry(cx);
465 },
466 )),
467 )
468 })
469 }
470}
471
472fn target_name(model: &ToastModel) -> String {
473 match &model.kind {
474 ChatKind::Group { .. } => model.chat_title.clone(),
475 _ => model.sender_name.clone(),
476 }
477}
478
479impl Render for ToastView {
480 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
481 let id = self.id;
482 let (model, preview_on) = {
483 let center = self.center.read(cx);
484 (center.model(id).cloned(), center.settings().preview)
485 };
486 let Some(model) = model else {
487 return div()
488 .size_full()
489 .bg(theme::surface_raised())
490 .into_any_element();
491 };
492 let text = describe(&model, preview_on);
493 let narration = model
494 .agent
495 .as_ref()
496 .map_or_else(|| text.narration(), |agent| agent.title.clone());
497 if narration != self.last_title {
498 window.set_window_title(&narration);
499 self.last_title = narration;
500 }
501 let mention = model.mentions_me;
502 let fill = if mention {
503 theme::toast_mention_fill()
504 } else {
505 theme::surface_raised()
506 };
507 let border = if mention {
508 theme::accent()
509 } else {
510 theme::border_strong()
511 };
512 let border_width = if mention { MENTION_BORDER } else { 1. };
513 let hovered = model.hovered;
514 let content = match model.reply {
515 _ if model.agent.is_some() => {
516 let agent = model.agent.as_ref().expect("checked above");
517 agent_toast_content(&self.center, id, agent, hovered)
518 }
519 ReplyState::Sent | ReplyState::Failed => self.confirmation(&model, cx),
520 ReplyState::Open => v_flex()
521 .w_full()
522 .child(
523 h_flex()
524 .w_full()
525 .gap(px(AVATAR_GAP))
526 .items_start()
527 .child(self.avatar(&model, fill, cx))
528 .child(self.body(&model, &text, true)),
529 )
530 .child(self.reply_field(window, cx)),
531 ReplyState::Closed => v_flex()
532 .w_full()
533 .child(
534 h_flex()
535 .w_full()
536 .gap(px(AVATAR_GAP))
537 .items_start()
538 .child(self.avatar(&model, fill, cx))
539 .child(self.body(&model, &text, hovered)),
540 )
541 .child(self.action_row(mention, cx)),
542 };
543 let center = self.center.clone();
544 let hover_center = self.center.clone();
545 div()
546 .id(("toast", id))
547 .track_focus(&self.focus_handle)
548 .relative()
549 .size_full()
550 .opacity(model.opacity(Instant::now()))
551 .rounded(px(TOAST_RADIUS))
552 .bg(fill)
553 .border(px(border_width))
554 .border_color(border)
555 .overflow_hidden()
556 .cursor_pointer()
557 .on_hover(move |is_hovered, _, cx| {
558 hover_center.update(cx, |center, cx| center.set_hover(id, *is_hovered, cx));
559 })
560 .on_click(move |_, _, cx| {
561 center.update(cx, |center, cx| center.activate(id, cx));
562 })
563 .capture_action(cx.listener(
564 |this, _: &gpui_kit::component::input::Escape, window, cx| {
565 this.close_reply(window, cx);
566 cx.stop_propagation();
567 },
568 ))
569 .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| {
570 if event.keystroke.key == "escape" {
571 let id = this.id;
572 this.center.update(cx, |center, cx| center.dismiss(id, cx));
573 }
574 }))
575 .child(div().size_full().p(px(TOAST_PADDING)).child(content))
576 .into_any_element()
577 }
578}
579
580pub struct PillView {
581 center: Entity<NotificationCenter>,
582 _subscription: Subscription,
583}
584
585impl PillView {
586 pub fn new(center: Entity<NotificationCenter>, cx: &mut Context<Self>) -> Self {
587 let subscription = cx.observe(&center, |_, _, cx| cx.notify());
588 PillView {
589 center,
590 _subscription: subscription,
591 }
592 }
593}
594
595impl Render for PillView {
596 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
597 let (queued, opacity) = {
598 let center = self.center.read(cx);
599 (center.queued_count(), center.stack_opacity())
600 };
601 let center = self.center.clone();
602 h_flex()
603 .id("toast-hide-all")
604 .size_full()
605 .gap(px(6.))
606 .items_center()
607 .justify_center()
608 .opacity(opacity)
609 .rounded(px(TOAST_RADIUS))
610 .bg(theme::surface_raised())
611 .border_1()
612 .border_color(theme::border_strong())
613 .cursor_pointer()
614 .hover(|bar| bar.bg(theme::border_strong()))
615 .text_size(px(12.5))
616 .font_weight(FontWeight::SEMIBOLD)
617 .text_color(theme::text_soft())
618 .child("Hide all")
619 .when(queued > 0, |bar| {
620 bar.child(
621 div()
622 .text_color(theme::text_muted())
623 .child(format!("{queued} more waiting")),
624 )
625 })
626 .on_click(move |_, _, cx| {
627 center.update(cx, |center, cx| center.hide_all(cx));
628 })
629 }
630}