BlueGit

ict rusty-teams

Files

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

call_window.rs

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