BlueGit

ict rusty-teams

Files

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

call_control_fold.rs

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