BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / call_health.rs

call_health.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
call_health.rs 7.0 KiB Download Raw
1use calling::NetworkLevel;
2use gpui_kit::component::button::{Button, ButtonVariants as _};
3use gpui_kit::component::popover::Popover;
4use gpui_kit::component::{h_flex, v_flex};
5use gpui_kit::*;
6
7use super::call_organizer::pill;
8use super::call_view::{BANNER_HEIGHT, MENU_WIDTH};
9use super::widgets::symbol;
10use crate::app_state::AppState;
11use crate::call::{CallModel, HealthBanner, level_text, media_text, network_rows, transport_text};
12use crate::theme;
13
14const BAR_WIDTH: f32 = 3.;
15const BAR_HEIGHTS: [f32; 3] = [6., 10., 14.];
16const BAR_GAP: f32 = 2.;
17const INACTIVE_BAR: f32 = 0.3;
18const INDICATOR_HEIGHT: f32 = 28.;
19const POPOVER_PADDING: f32 = 12.;
20const BANNER_TEXT: f32 = 13.;
21const NETWORK_TEXT: &str = "Your network is unstable. Turning off incoming video may help.";
22const CPU_TEXT: &str = "Your computer is overloaded. Turning off video effects may help.";
23
24fn dark_text() -> Hsla {
25 hsla(0., 0., 0.08, 1.)
26}
27
28fn level_color(level: NetworkLevel) -> Hsla {
29 match level {
30 NetworkLevel::Good => theme::text_muted(),
31 NetworkLevel::Poor => theme::amber(),
32 NetworkLevel::Bad => theme::red(),
33 }
34}
35
36fn lit_bars(level: NetworkLevel) -> usize {
37 match level {
38 NetworkLevel::Good => BAR_HEIGHTS.len(),
39 NetworkLevel::Poor => 2,
40 NetworkLevel::Bad => 1,
41 }
42}
43
44fn signal_bars(level: NetworkLevel) -> Div {
45 let lit = lit_bars(level);
46 h_flex()
47 .h(px(BAR_HEIGHTS[2]))
48 .gap(px(BAR_GAP))
49 .items_end()
50 .children(BAR_HEIGHTS.iter().enumerate().map(|(index, height)| {
51 let color = level_color(level);
52 div()
53 .w(px(BAR_WIDTH))
54 .h(px(*height))
55 .rounded(px(1.))
56 .bg(if index < lit {
57 color
58 } else {
59 color.opacity(INACTIVE_BAR)
60 })
61 }))
62}
63
64fn section_title(title: &'static str) -> Div {
65 div()
66 .text_size(px(12.))
67 .font_weight(FontWeight::SEMIBOLD)
68 .text_color(theme::text_muted())
69 .child(title)
70}
71
72fn row(label: &'static str, value: String) -> Div {
73 h_flex()
74 .w_full()
75 .justify_between()
76 .text_size(px(13.))
77 .child(div().text_color(theme::text_muted()).child(label))
78 .child(value)
79}
80
81fn health_popover(model: &CallModel) -> Div {
82 let health = model.health.latest.clone();
83 let level = model.health.level();
84 let network = match &health {
85 Some(health) => v_flex()
86 .gap(px(4.))
87 .child(row("Quality", level_text(level).to_owned()))
88 .children(
89 network_rows(&health.sample)
90 .into_iter()
91 .map(|(label, value)| row(label, value)),
92 ),
93 None => v_flex().child(row("Quality", "Measuring...".to_owned())),
94 };
95 let transport = health.iter().flat_map(|health| {
96 [
97 Some(transport_text(health)),
98 media_text(health, &model.peer_name),
99 ]
100 .into_iter()
101 .flatten()
102 });
103 v_flex()
104 .debug_selector(|| "shell/islands/main/call-health-popover".into())
105 .w(px(MENU_WIDTH))
106 .p(px(POPOVER_PADDING))
107 .gap(px(12.))
108 .child(
109 div()
110 .text_size(px(15.))
111 .font_weight(FontWeight::SEMIBOLD)
112 .child("Call health"),
113 )
114 .child(
115 v_flex()
116 .gap(px(6.))
117 .child(section_title("Network"))
118 .child(network),
119 )
120 .child(
121 v_flex()
122 .gap(px(6.))
123 .child(section_title("Encryption"))
124 .child(
125 div()
126 .text_size(px(13.))
127 .font_weight(FontWeight::SEMIBOLD)
128 .child("Secured by Teams encryption"),
129 )
130 .children(transport.map(|line| {
131 div()
132 .text_size(px(13.))
133 .text_color(theme::text_muted())
134 .child(line)
135 })),
136 )
137}
138
139pub fn health_indicator(model: &CallModel) -> Option<AnyElement> {
140 model.health.latest.as_ref()?;
141 let level = model.health.level();
142 let model = model.clone();
143 Some(
144 Popover::new("call-health-popover")
145 .anchor(Anchor::TopLeft)
146 .offset(px(8.))
147 .trigger(
148 Button::new("call-health")
149 .ghost()
150 .p_0()
151 .h(px(INDICATOR_HEIGHT))
152 .px(px(6.))
153 .child(
154 h_flex()
155 .debug_selector(|| "shell/islands/main/call-health".into())
156 .gap(px(8.))
157 .items_center()
158 .child(signal_bars(level))
159 .child(symbol("shield", 18., theme::text_muted())),
160 ),
161 )
162 .content(move |_, _, _| health_popover(&model))
163 .into_any_element(),
164 )
165}
166
167pub fn health_banner(app: &Entity<AppState>, model: &CallModel) -> Option<AnyElement> {
168 let banner = model.health_banner()?;
169 let (text, action_label) = match banner {
170 HealthBanner::Network => (NETWORK_TEXT, "Turn off incoming video"),
171 HealthBanner::Cpu => (CPU_TEXT, "Turn off effects"),
172 };
173 let (action_app, close_app) = (app.clone(), app.clone());
174 Some(
175 h_flex()
176 .id("call-health-banner")
177 .debug_selector(|| "shell/islands/main/call-health-banner".into())
178 .w_full()
179 .h(px(BANNER_HEIGHT))
180 .flex_none()
181 .px(px(20.))
182 .gap(px(12.))
183 .items_center()
184 .justify_center()
185 .bg(theme::amber())
186 .text_color(dark_text())
187 .text_size(px(BANNER_TEXT))
188 .font_weight(FontWeight::SEMIBOLD)
189 .child(text)
190 .child(
191 pill("call-health-action", action_label, true).on_click(move |_, _, cx| {
192 action_app.update(cx, |state, cx| match banner {
193 HealthBanner::Network => state.set_call_incoming_video(false, cx),
194 HealthBanner::Cpu => state.turn_off_call_effects(cx),
195 })
196 }),
197 )
198 .child(
199 div()
200 .id("call-health-close")
201 .size(px(24.))
202 .flex()
203 .flex_none()
204 .items_center()
205 .justify_center()
206 .rounded_full()
207 .cursor_pointer()
208 .hover(|button| button.bg(dark_text().opacity(0.12)))
209 .child(symbol("close", 16., dark_text()))
210 .on_click(move |_, _, cx| {
211 close_app.update(cx, |state, cx| state.close_call_health_banner(banner, cx))
212 }),
213 )
214 .into_any_element(),
215 )
216}