BlueGit

ict rusty-teams

Files

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

call_window.rs

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