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