BlueGit

ict rusty-teams

Files

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

call_window.rs

call_window.rs 11.0 KiB Download Raw
1use app_ui_kit::layout::ISLAND_GAP;
2use gpui_kit::component::{TitleBar, elevation, h_flex, v_flex};
3use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::*;
5
6use super::actions::ToggleCallMute;
7use super::call_end_view::render_call_end;
8use super::call_pop_out::zoom_listener;
9use super::call_stage::render_stage_overlay;
10use super::call_view::render_call_view;
11use super::conversation::ConversationView;
12use super::prejoin_view::PrejoinView;
13use super::shortcuts::{
14 HangUpCall, OpenLobbyList, PushToTalk, ToggleCallCamera, ToggleCallFullscreen, ToggleCallHand,
15 ToggleCallShare, ZoomShareIn, ZoomShareOut, ZoomShareReset,
16};
17use super::title_bar::{TITLE_BAR_HEIGHT, app_icon_image};
18use crate::app_state::{AppState, Selection};
19use crate::call::ZoomStep;
20use crate::theme;
21
22pub const CALL_WINDOW_TAG: &str = "call-window";
23pub const CALL_WINDOW_SIZE: (f32, f32) = (1180., 760.);
24pub const CALL_WINDOW_MIN_SIZE: (f32, f32) = (560., 420.);
25const FALLBACK_TITLE: &str = "Call";
26const KEY_CONTEXT: &str = "CallWindow";
27const APP_ICON_SIZE: f32 = 18.;
28
29pub fn call_window_title(state: &AppState) -> String {
30 state
31 .call
32 .as_ref()
33 .map(|call| call.model.title.clone())
34 .or_else(|| {
35 state
36 .prejoin
37 .as_ref()
38 .map(|pending| pending.join.title.clone())
39 })
40 .filter(|title| !title.is_empty())
41 .unwrap_or_else(|| FALLBACK_TITLE.to_owned())
42}
43
44/// The meeting window: like Teams, a call or meeting runs in its own window and the main window stays free for chats.
45pub struct CallWindowView {
46 app: Entity<AppState>,
47 focus: FocusHandle,
48 call_chat: Option<(String, Entity<ConversationView>)>,
49 prejoin: Option<Entity<PrejoinView>>,
50 fullscreen: bool,
51 was_fullscreen: bool,
52 _app_subscription: Subscription,
53 _media_subscription: Option<Subscription>,
54 _activation_subscription: Subscription,
55}
56
57impl CallWindowView {
58 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
59 let app_subscription = cx.observe(&app, |_, _, cx| cx.notify());
60 let media_subscription = app
61 .read(cx)
62 .call_media
63 .clone()
64 .map(|media| cx.observe(&media, |_, _, cx| cx.notify()));
65 let activation_subscription = cx.observe_window_activation(window, |this, window, cx| {
66 if !window.is_window_active() {
67 this.app
68 .update(cx, |state, cx| state.release_push_to_talk(cx));
69 }
70 });
71 let closing_app = app.clone();
72 window.on_window_should_close(cx, move |_, cx| {
73 leave_or_dismiss(&closing_app, cx);
74 true
75 });
76 let focus = cx.focus_handle();
77 window.focus(&focus, cx);
78 CallWindowView {
79 app,
80 focus,
81 call_chat: None,
82 prejoin: None,
83 fullscreen: false,
84 was_fullscreen: false,
85 _app_subscription: app_subscription,
86 _media_subscription: media_subscription,
87 _activation_subscription: activation_subscription,
88 }
89 }
90
91 fn sync_call_chat(
92 &mut self,
93 thread: Option<String>,
94 window: &mut Window,
95 cx: &mut Context<Self>,
96 ) {
97 match (thread, &self.call_chat) {
98 (Some(wanted), Some((open, _))) if &wanted == open => {}
99 (Some(wanted), _) => {
100 let selection = Selection::Chat(wanted.clone());
101 let app = self.app.clone();
102 let view = cx.new(|cx| ConversationView::pinned_to(app, selection, window, cx));
103 self.call_chat = Some((wanted, view));
104 }
105 (None, _) => self.call_chat = None,
106 }
107 }
108
109 fn sync_prejoin(&mut self, window: &mut Window, cx: &mut Context<Self>) {
110 let wanted = self.app.read(cx).prejoin.clone();
111 let open = self.prejoin.as_ref().map(|view| view.read(cx).pending_id());
112 match (wanted, open) {
113 (Some(pending), Some(id)) if pending.id == id => {}
114 (Some(pending), _) => {
115 let app = self.app.clone();
116 self.prejoin = Some(cx.new(|cx| PrejoinView::new(app, &pending, window, cx)));
117 }
118 (None, _) => self.prejoin = None,
119 }
120 }
121
122 fn sync_fullscreen(&mut self, wanted: bool, window: &mut Window) {
123 if wanted == self.fullscreen {
124 return;
125 }
126 self.fullscreen = wanted;
127 if wanted {
128 self.was_fullscreen = window.is_fullscreen();
129 }
130 if wanted != window.is_fullscreen() && (wanted || !self.was_fullscreen) {
131 window.toggle_fullscreen();
132 }
133 }
134
135 fn title_bar(&self, title: String, cx: &App) -> impl IntoElement {
136 let app_icon = crate::active_appearance::current(cx).app_icon;
137 let app = self.app.clone();
138 TitleBar::new()
139 .h(px(TITLE_BAR_HEIGHT))
140 .border_b_0()
141 .bg(theme::background())
142 .on_close_window(move |_, window, cx| {
143 leave_or_dismiss(&app, cx);
144 window.remove_window();
145 })
146 .child(
147 h_flex()
148 .debug_selector(|| "call-window/title-bar".into())
149 .flex_1()
150 .min_w_0()
151 .h_full()
152 .gap(px(12.))
153 .items_center()
154 .child(
155 img(app_icon_image(app_icon))
156 .size(px(APP_ICON_SIZE))
157 .flex_none(),
158 )
159 .child(
160 div()
161 .min_w_0()
162 .truncate()
163 .text_size(px(13.))
164 .font_weight(FontWeight::SEMIBOLD)
165 .text_color(theme::text())
166 .child(title),
167 ),
168 )
169 }
170}
171
172fn call_action<A: Action>(
173 app: &Entity<AppState>,
174 action: fn(&mut AppState, &mut Context<AppState>),
175) -> impl Fn(&A, &mut Window, &mut App) + 'static {
176 let app = app.clone();
177 move |_, _, cx| app.update(cx, |state, cx| action(state, cx))
178}
179
180fn leave_or_dismiss(app: &Entity<AppState>, cx: &mut App) {
181 app.update(cx, |state, cx| {
182 if state.call.is_some() {
183 state.leave_call(cx);
184 } else if state.prejoin.is_some() {
185 state.cancel_prejoin(cx);
186 } else {
187 state.dismiss_call_end(cx);
188 }
189 });
190}
191
192impl Focusable for CallWindowView {
193 fn focus_handle(&self, _: &App) -> FocusHandle {
194 self.focus.clone()
195 }
196}
197
198impl Render for CallWindowView {
199 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
200 let chat_thread = self.app.read(cx).call_chat_thread();
201 self.sync_call_chat(chat_thread, window, cx);
202 self.sync_prejoin(window, cx);
203 let state = self.app.read(cx);
204 let fullscreen = state.call_fullscreen();
205 let open_dialogs = [
206 state.transfer_picker.is_some(),
207 state
208 .call
209 .as_ref()
210 .is_some_and(|call| call.options_dialog.is_some()),
211 state
212 .call
213 .as_ref()
214 .is_some_and(|call| call.model.consent_required),
215 ]
216 .into_iter()
217 .filter(|open| *open)
218 .count();
219 let title = call_window_title(state);
220 window.set_window_title(&title);
221 let window_width = f32::from(window.viewport_size().width);
222 let stage_width = window_width - 2. * ISLAND_GAP;
223 let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone().into());
224 let stage = render_call_view(&self.app, state, chat_panel, fullscreen, stage_width)
225 .or_else(|| render_call_end(&self.app, state))
226 .or_else(|| {
227 self.prejoin
228 .clone()
229 .map(|prejoin| prejoin.into_any_element())
230 });
231 let stage_overlay = render_stage_overlay(&self.app, state);
232 elevation::set_app_modal_layers(window, open_dialogs, cx);
233 self.sync_fullscreen(fullscreen, window);
234 let title_bar = (!fullscreen).then(|| self.title_bar(title, cx));
235 let release_app = self.app.clone();
236 let modifier_app = self.app.clone();
237 let escape_app = self.app.clone();
238 v_flex()
239 .id(CALL_WINDOW_TAG)
240 .debug_selector(|| CALL_WINDOW_TAG.into())
241 .key_context(KEY_CONTEXT)
242 .track_focus(&self.focus)
243 .size_full()
244 .bg(theme::background())
245 .text_color(theme::text())
246 .text_size(px(14.))
247 .on_action(call_action::<ToggleCallMute>(
248 &self.app,
249 AppState::toggle_call_mute,
250 ))
251 .on_action(call_action::<PushToTalk>(
252 &self.app,
253 AppState::press_push_to_talk,
254 ))
255 .on_action(call_action::<HangUpCall>(&self.app, AppState::leave_call))
256 .on_action(call_action::<ToggleCallCamera>(
257 &self.app,
258 AppState::toggle_call_camera,
259 ))
260 .on_action(call_action::<ToggleCallShare>(
261 &self.app,
262 AppState::toggle_call_share,
263 ))
264 .on_action(call_action::<ToggleCallHand>(
265 &self.app,
266 AppState::toggle_call_hand,
267 ))
268 .on_action(call_action::<OpenLobbyList>(
269 &self.app,
270 AppState::open_call_lobby_list,
271 ))
272 .on_action(call_action::<ToggleCallFullscreen>(
273 &self.app,
274 AppState::toggle_call_fullscreen,
275 ))
276 .on_action(zoom_listener::<ZoomShareIn>(&self.app, ZoomStep::In))
277 .on_action(zoom_listener::<ZoomShareOut>(&self.app, ZoomStep::Out))
278 .on_action(zoom_listener::<ZoomShareReset>(&self.app, ZoomStep::Reset))
279 .on_key_down(move |event: &KeyDownEvent, _, cx| {
280 if event.keystroke.key == "escape" {
281 escape_app.update(cx, |state, cx| state.escape_call_fullscreen(cx));
282 }
283 })
284 .on_key_up(move |event: &KeyUpEvent, _, cx| {
285 if event.keystroke.key == "space" {
286 release_app.update(cx, |state, cx| state.release_push_to_talk(cx));
287 }
288 })
289 .on_modifiers_changed(move |event: &ModifiersChangedEvent, _, cx| {
290 if !event.modifiers.control {
291 modifier_app.update(cx, |state, cx| state.release_push_to_talk(cx));
292 }
293 })
294 .children(title_bar)
295 .child(
296 h_flex()
297 .flex_1()
298 .min_h_0()
299 .w_full()
300 .when(!fullscreen, |row| row.px(px(ISLAND_GAP)).pb(px(ISLAND_GAP)))
301 .children(stage),
302 )
303 .children(stage_overlay)
304 }
305}