BlueGit

ict rusty-teams

Files

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

call_controls.rs

call_controls.rs 6.3 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{
3 button::{Button, ButtonVariants as _},
4 h_flex,
5 menu::{DropdownMenu as _, PopupMenuItem},
6};
7use gpui_kit::prelude::FluentBuilder as _;
8use gpui_kit::*;
9
10use super::shortcuts::{StartAudioCall, StartVideoCall};
11use super::widgets::{icon, menu_icon};
12use crate::app_state::{AppState, Selection};
13use crate::call::{is_echo_chat, plan_for_chat};
14use crate::i18n::tr;
15use crate::theme;
16
17const BUTTON_SIZE: f32 = 30.;
18const GLYPH_SIZE: f32 = 16.;
19const JOIN_HEIGHT: f32 = 30.;
20const CHEVRON_WIDTH: f32 = 18.;
21const CHEVRON_GLYPH: f32 = 12.;
22pub const HEADER_VIDEO_CALL_TAG: &str = "header-video-call";
23pub const HEADER_CALL_OPTIONS_TAG: &str = "header-call-options";
24const GROUP_KIND: &str = "group";
25const MEETING_KIND: &str = "meeting";
26
27pub fn header_call_controls(
28 app: &Entity<AppState>,
29 state: &AppState,
30 selection: &Selection,
31 compact: bool,
32) -> Vec<AnyElement> {
33 let conversation_id = selection.conversation_id().to_owned();
34 let mut controls = Vec::new();
35 let chat = match selection {
36 Selection::Chat(chat_id) => state.sidebar.chats.iter().find(|chat| &chat.id == chat_id),
37 Selection::Channel(_) => None,
38 };
39 let running = state.running_meeting(&conversation_id).is_some();
40 let meeting_chat = chat.is_some_and(|chat| chat.kind.eq_ignore_ascii_case(MEETING_KIND));
41 if running || meeting_chat {
42 let join_app = app.clone();
43 let join_id = conversation_id.clone();
44 let (background, foreground) = if running {
45 (theme::green(), theme::white())
46 } else {
47 (theme::accent(), theme::on_accent())
48 };
49 controls.push(
50 h_flex()
51 .id("header-join")
52 .h(px(JOIN_HEIGHT))
53 .px(px(14.))
54 .gap(px(6.))
55 .flex_none()
56 .items_center()
57 .rounded_full()
58 .bg(background)
59 .text_color(foreground)
60 .text_size(px(13.))
61 .font_weight(FontWeight::SEMIBOLD)
62 .cursor_pointer()
63 .hover(|button| button.opacity(0.85))
64 .when(running, |button| {
65 button.child(icon(IconName::Phone, 14., foreground))
66 })
67 .child(tr("common.join"))
68 .on_click(move |_, _, cx| {
69 join_app.update(cx, |state, cx| state.join_chat_meeting(&join_id, cx));
70 })
71 .into_any_element(),
72 );
73 }
74 let callable = chat.is_some_and(|chat| {
75 is_echo_chat(chat) || plan_for_chat(chat, state.directory.me.as_ref()).is_some()
76 });
77 if let Some(chat) = chat.filter(|_| callable) {
78 let meet_now = chat.kind == GROUP_KIND && !is_echo_chat(chat);
79 controls.push(video_split_button(app, meet_now, compact));
80 }
81 controls
82}
83
84fn start_call(app: &Entity<AppState>, with_video: bool, cx: &mut App) {
85 app.update(cx, |state, cx| {
86 if let Some(Selection::Chat(chat_id)) = state.selection.clone() {
87 if with_video {
88 state.start_chat_video_call(&chat_id, cx);
89 } else {
90 state.start_chat_call(&chat_id, cx);
91 }
92 }
93 });
94}
95
96fn video_split_button(app: &Entity<AppState>, meet_now: bool, compact: bool) -> AnyElement {
97 let video_app = app.clone();
98 let menu_app = app.clone();
99 let labelled = meet_now && !compact;
100 h_flex()
101 .flex_none()
102 .items_center()
103 .child(
104 Button::new(HEADER_VIDEO_CALL_TAG)
105 .ghost()
106 .h(px(BUTTON_SIZE))
107 .map(|button| {
108 if labelled {
109 button.px(px(8.))
110 } else {
111 button.p_0().w(px(BUTTON_SIZE))
112 }
113 })
114 .tooltip(if meet_now {
115 tr("calls.chrome.meet_now")
116 } else {
117 tr("calls.chrome.video_call")
118 })
119 .debug_selector(|| HEADER_VIDEO_CALL_TAG.into())
120 .child(
121 h_flex()
122 .gap(px(6.))
123 .items_center()
124 .child(icon(IconName::Video, GLYPH_SIZE, theme::text_soft()))
125 .when(labelled, |row| {
126 row.child(
127 div()
128 .text_size(px(14.))
129 .font_weight(FontWeight::SEMIBOLD)
130 .text_color(theme::text())
131 .child(tr("calls.chrome.meet_now")),
132 )
133 }),
134 )
135 .on_click(move |_, _, cx| start_call(&video_app, true, cx)),
136 )
137 .child(
138 Button::new(HEADER_CALL_OPTIONS_TAG)
139 .ghost()
140 .p_0()
141 .w(px(CHEVRON_WIDTH))
142 .h(px(BUTTON_SIZE))
143 .tooltip(tr("calls.chrome.call_options"))
144 .debug_selector(|| HEADER_CALL_OPTIONS_TAG.into())
145 .child(icon(
146 IconName::ChevronDown,
147 CHEVRON_GLYPH,
148 theme::text_soft(),
149 ))
150 .dropdown_menu(move |popup, _, _| {
151 let (video, audio) = (menu_app.clone(), menu_app.clone());
152 let popup = if meet_now {
153 popup
154 } else {
155 popup.item(
156 PopupMenuItem::new(tr("calls.chrome.video_call"))
157 .icon(menu_icon(IconName::Video))
158 .action(Box::new(StartVideoCall))
159 .on_click(move |_, _, cx| start_call(&video, true, cx)),
160 )
161 };
162 popup.item(
163 PopupMenuItem::new(tr("calls.chrome.audio_call"))
164 .icon(menu_icon(IconName::Phone))
165 .action(Box::new(StartAudioCall))
166 .on_click(move |_, _, cx| start_call(&audio, false, cx)),
167 )
168 }),
169 )
170 .into_any_element()
171}