BlueGit

ict rusty-teams

Files

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

ring_view.rs

cc834635 History
ring_view.rs 6.2 KiB Download Raw
1use calling::AcceptMode;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{h_flex, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
6
7use super::center::NotificationCenter;
8use super::layout::{AVATAR_GAP, TOAST_PADDING};
9use crate::app_state::AppState;
10use crate::call::RingEntry;
11use crate::theme;
12use crate::views::avatar::person_avatar;
13use crate::views::call_view::pulsing;
14use crate::views::widgets::icon;
15
16pub const RING_AVATAR: f32 = 56.;
17const RING_WIDTH: f32 = 3.;
18const RING_GAP: f32 = 3.;
19pub(super) const TOAST_RADIUS: f32 = 12.;
20const BUTTON_HEIGHT: f32 = 32.;
21pub const ENDS_CURRENT_CALL: &str = "Accept ends current call";
22
23pub struct RingView {
24 app: Entity<AppState>,
25 ring_id: u64,
26 _subscription: Subscription,
27}
28
29impl RingView {
30 pub fn new(center: Entity<NotificationCenter>, ring_id: u64, cx: &mut Context<Self>) -> Self {
31 let app = center.read(cx).app().clone();
32 let subscription = cx.observe(&app, |_, _, cx| cx.notify());
33 RingView {
34 app,
35 ring_id,
36 _subscription: subscription,
37 }
38 }
39}
40
41pub(super) fn answer_button(
42 id: &'static str,
43 label: &str,
44 glyph: IconName,
45 fill: Hsla,
46) -> Stateful<gpui_kit::Div> {
47 h_flex()
48 .id(id)
49 .h(px(BUTTON_HEIGHT))
50 .px(px(10.))
51 .gap(px(5.))
52 .flex_none()
53 .items_center()
54 .justify_center()
55 .rounded_full()
56 .bg(fill)
57 .text_color(theme::white())
58 .text_size(px(13.))
59 .font_weight(FontWeight::SEMIBOLD)
60 .cursor_pointer()
61 .hover(|button| button.opacity(0.85))
62 .child(icon(glyph, 16., theme::white()))
63 .child(label.to_owned())
64}
65
66fn caller_avatar(state: &AppState, entry: &RingEntry) -> AnyElement {
67 let avatar = person_avatar(
68 &state.directory,
69 entry.caller_user_id().as_deref(),
70 entry.caller_name(),
71 RING_AVATAR,
72 );
73 let ringed = div()
74 .p(px(RING_GAP))
75 .rounded_full()
76 .border(px(RING_WIDTH))
77 .border_color(theme::green())
78 .child(avatar);
79 pulsing(ringed, "ring-avatar-pulse")
80}
81
82impl Render for RingView {
83 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
84 let state = self.app.read(cx);
85 let Some(entry) = state.rings.get(self.ring_id).cloned() else {
86 return div().size_full().bg(theme::popup()).into_any_element();
87 };
88 window.set_window_title(&format!("{}: {}", entry.caller_name(), entry.subtitle()));
89 let ends_current = state.call.is_some();
90 let avatar = caller_avatar(state, &entry);
91 let ring_id = self.ring_id;
92 let decline_app = self.app.clone();
93 let accept_app = self.app.clone();
94 let decline = answer_button("ring-decline", "Decline", IconName::PhoneOff, theme::red())
95 .on_click(move |_, _, cx| {
96 cx.stop_propagation();
97 decline_app.update(cx, |state, cx| state.decline_ring(ring_id, cx));
98 });
99 let accept = answer_button(
100 "ring-accept",
101 entry.accept_label(),
102 IconName::Phone,
103 theme::green(),
104 )
105 .on_click(move |_, _, cx| {
106 cx.stop_propagation();
107 accept_app.update(cx, |state, cx| {
108 state.accept_ring(ring_id, AcceptMode::Audio, cx)
109 });
110 });
111 let video_app = self.app.clone();
112 let accept_video = entry.ring.video.then(|| {
113 answer_button(
114 "ring-accept-video",
115 "Video",
116 IconName::Video,
117 theme::green(),
118 )
119 .on_click(move |_, _, cx| {
120 cx.stop_propagation();
121 video_app.update(cx, |state, cx| {
122 state.accept_ring(ring_id, AcceptMode::Video, cx)
123 });
124 })
125 });
126 div()
127 .id(("ring", ring_id))
128 .size_full()
129 .rounded(px(TOAST_RADIUS))
130 .bg(theme::popup())
131 .overflow_hidden()
132 .child(
133 div().size_full().p(px(TOAST_PADDING)).child(
134 h_flex()
135 .size_full()
136 .gap(px(AVATAR_GAP))
137 .items_start()
138 .child(avatar)
139 .child(
140 v_flex()
141 .flex_1()
142 .min_w_0()
143 .gap(px(2.))
144 .child(
145 div()
146 .truncate()
147 .text_size(px(15.))
148 .font_weight(FontWeight::SEMIBOLD)
149 .text_color(theme::text())
150 .child(entry.caller_name().to_owned()),
151 )
152 .child(
153 div()
154 .text_size(px(12.5))
155 .text_color(theme::text_muted())
156 .child(entry.subtitle()),
157 )
158 .when(ends_current, |column| {
159 column.child(
160 div()
161 .text_size(px(12.))
162 .text_color(theme::amber())
163 .child(ENDS_CURRENT_CALL),
164 )
165 })
166 .child(
167 h_flex()
168 .mt(px(8.))
169 .gap(px(6.))
170 .child(decline)
171 .child(accept)
172 .children(accept_video),
173 ),
174 ),
175 ),
176 )
177 .into_any_element()
178 }
179}