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