BlueGit

ict rusty-teams

Files

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

call_control_fold.rs

call_control_fold.rs 7.2 KiB Download Raw
1use calling::Reaction;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::Icon;
4use gpui_kit::component::menu::{PopupMenu, PopupMenuItem};
5use gpui_kit::*;
6
7use super::call_organizer::CAMERA_MENU_WIDTH;
8use super::call_view::{CONTROL_SIZE, reaction_glyph};
9use super::call_view_menu::view_items;
10use crate::app_state::AppState;
11use crate::call::CallModel;
12
13pub(super) const CONTROL_GAP: f32 = 12.;
14pub(super) const CONTROLS_PADDING: f32 = 16.;
15const LEAVE_WIDTH: f32 = 116.;
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum Foldable {
19 View,
20 Reactions,
21 RaiseHand,
22 People,
23 Chat,
24}
25
26#[derive(Clone, Debug, Default)]
27pub struct FoldedControls {
28 pub items: Vec<Foldable>,
29 pub fullscreen: bool,
30 pub people_open: bool,
31 pub chat_open: bool,
32 pub chat_unread: u32,
33}
34
35impl FoldedControls {
36 pub fn folds(&self, control: Foldable) -> bool {
37 self.items.contains(&control)
38 }
39}
40
41pub fn fixed_controls_width(has_camera_menu: bool, has_more: bool, has_leave_menu: bool) -> f32 {
42 let mut widths = vec![
43 CONTROL_SIZE + CAMERA_MENU_WIDTH,
44 CONTROL_SIZE
45 + if has_camera_menu {
46 CAMERA_MENU_WIDTH
47 } else {
48 0.
49 },
50 CONTROL_SIZE,
51 LEAVE_WIDTH,
52 ];
53 if has_more {
54 widths.push(CONTROL_SIZE);
55 }
56 if has_leave_menu {
57 widths.push(CONTROL_SIZE);
58 }
59 widths.iter().sum::<f32>() + CONTROL_GAP * (widths.len() - 1) as f32
60}
61
62pub fn folded_for_width(available: f32, fixed: f32, present: &[Foldable]) -> Vec<Foldable> {
63 let room = available - 2. * CONTROLS_PADDING - fixed;
64 let fitting = if room <= 0. {
65 0
66 } else {
67 (room / (CONTROL_SIZE + CONTROL_GAP)).floor() as usize
68 };
69 let folded_count = present.len().saturating_sub(fitting);
70 present[..folded_count].to_vec()
71}
72
73pub fn folded_items(
74 mut popup: PopupMenu,
75 window: &mut Window,
76 cx: &mut Context<PopupMenu>,
77 app: &Entity<AppState>,
78 model: &CallModel,
79 folded: &FoldedControls,
80) -> PopupMenu {
81 if folded.items.is_empty() {
82 return popup;
83 }
84 for item in &folded.items {
85 popup = match item {
86 Foldable::View => view_submenu(popup, window, cx, app, model, folded.fullscreen),
87 Foldable::Reactions => reactions_submenu(popup, window, cx, app, model),
88 Foldable::RaiseHand => hand_item(popup, app, model),
89 Foldable::People => people_item(popup, app, folded.people_open),
90 Foldable::Chat => chat_item(popup, app, folded),
91 };
92 }
93 popup.separator()
94}
95
96fn view_submenu(
97 popup: PopupMenu,
98 window: &mut Window,
99 cx: &mut Context<PopupMenu>,
100 app: &Entity<AppState>,
101 model: &CallModel,
102 fullscreen: bool,
103) -> PopupMenu {
104 let app = app.clone();
105 let model = model.clone();
106 popup.submenu_with_icon(
107 Some(Icon::new(IconName::LayoutGrid)),
108 "View",
109 window,
110 cx,
111 move |menu, _, _| view_items(menu, &app, &model, fullscreen),
112 )
113}
114
115fn reactions_submenu(
116 popup: PopupMenu,
117 window: &mut Window,
118 cx: &mut Context<PopupMenu>,
119 app: &Entity<AppState>,
120 model: &CallModel,
121) -> PopupMenu {
122 let app = app.clone();
123 let can_react = model.can_react();
124 popup.submenu_with_icon(
125 Some(Icon::new(IconName::FaceSlightlySmiling)),
126 "Reactions",
127 window,
128 cx,
129 move |mut menu, _, _| {
130 for reaction in Reaction::ALL {
131 let app = app.clone();
132 menu = menu.item(
133 PopupMenuItem::new(format!(
134 "{} {}",
135 reaction_glyph(&reaction),
136 reaction.name()
137 ))
138 .disabled(!can_react)
139 .on_click(move |_, _, cx| {
140 app.update(cx, |state, cx| {
141 state.send_call_reaction(reaction.clone(), cx)
142 })
143 }),
144 );
145 }
146 menu
147 },
148 )
149}
150
151fn hand_item(popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
152 let app = app.clone();
153 let raised = model.own_hand.is_some();
154 popup.item(
155 PopupMenuItem::new(if raised { "Lower hand" } else { "Raise hand" })
156 .icon(IconName::Hand)
157 .disabled(!model.can_react())
158 .on_click(move |_, _, cx| app.update(cx, |state, cx| state.toggle_call_hand(cx))),
159 )
160}
161
162fn people_item(popup: PopupMenu, app: &Entity<AppState>, open: bool) -> PopupMenu {
163 let app = app.clone();
164 popup.item(
165 PopupMenuItem::new(if open {
166 "Close the people list"
167 } else {
168 "Show people"
169 })
170 .icon(IconName::Users)
171 .on_click(move |_, _, cx| app.update(cx, |state, cx| state.toggle_call_people(cx))),
172 )
173}
174
175fn chat_item(popup: PopupMenu, app: &Entity<AppState>, folded: &FoldedControls) -> PopupMenu {
176 let app = app.clone();
177 let label = match (folded.chat_open, folded.chat_unread) {
178 (true, _) => "Close the chat".to_owned(),
179 (false, 0) => "Open the chat".to_owned(),
180 (false, unread) => format!("Open the chat ({unread})"),
181 };
182 popup.item(
183 PopupMenuItem::new(label)
184 .icon(IconName::MessageCircle)
185 .on_click(move |_, _, cx| app.update(cx, |state, cx| state.toggle_call_chat(cx))),
186 )
187}
188
189#[cfg(test)]
190mod tests {
191 use super::{CONTROLS_PADDING, Foldable, fixed_controls_width, folded_for_width};
192
193 const ALL: [Foldable; 5] = [
194 Foldable::View,
195 Foldable::Reactions,
196 Foldable::RaiseHand,
197 Foldable::People,
198 Foldable::Chat,
199 ];
200 const SLOT: f32 = 44. + 12.;
201 const FIXED: f32 = 400.;
202
203 fn width_for(fitting: f32) -> f32 {
204 FIXED + 2. * CONTROLS_PADDING + fitting * SLOT
205 }
206
207 #[test]
208 fn nothing_folds_when_everything_fits() {
209 assert!(folded_for_width(width_for(5.), FIXED, &ALL).is_empty());
210 assert!(folded_for_width(width_for(9.), FIXED, &ALL).is_empty());
211 }
212
213 #[test]
214 fn folds_view_first_then_reactions() {
215 assert_eq!(
216 folded_for_width(width_for(4.), FIXED, &ALL),
217 [Foldable::View]
218 );
219 assert_eq!(
220 folded_for_width(width_for(3.), FIXED, &ALL),
221 [Foldable::View, Foldable::Reactions]
222 );
223 }
224
225 #[test]
226 fn everything_folds_when_only_the_fixed_controls_fit() {
227 assert_eq!(folded_for_width(width_for(0.), FIXED, &ALL), ALL);
228 assert_eq!(folded_for_width(100., FIXED, &ALL), ALL);
229 }
230
231 #[test]
232 fn only_present_controls_are_counted() {
233 let present = [Foldable::View, Foldable::People];
234 assert!(folded_for_width(width_for(2.), FIXED, &present).is_empty());
235 assert_eq!(
236 folded_for_width(width_for(1.), FIXED, &present),
237 [Foldable::View]
238 );
239 }
240
241 #[test]
242 fn fixed_width_counts_optional_controls() {
243 let base = fixed_controls_width(false, false, false);
244 assert_eq!(fixed_controls_width(true, false, false), base + 28.);
245 assert_eq!(fixed_controls_width(false, true, false), base + 44. + 12.);
246 assert_eq!(fixed_controls_width(false, false, true), base + 44. + 12.);
247 }
248}