BlueGit

ict rusty-teams

Files

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

ring_view.rs

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