BlueGit

ict rusty-teams

Files

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

call_controls.rs

call_controls.rs 6.4 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_CALL_TAG: &str = "header-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(call_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 call_split_button(app: &Entity<AppState>, meet_now: bool, compact: bool) -> AnyElement {
97 let primary_app = app.clone();
98 let menu_app = app.clone();
99 let labelled = meet_now && !compact;
100 let (icon_name, tooltip_key) = match meet_now {
101 true => (IconName::Video, "calls.chrome.meet_now"),
102 false => (IconName::Phone, "calls.chrome.audio_call"),
103 };
104 h_flex()
105 .flex_none()
106 .items_center()
107 .child(
108 Button::new(HEADER_CALL_TAG)
109 .ghost()
110 .h(px(BUTTON_SIZE))
111 .map(|button| {
112 if labelled {
113 button.px(px(8.))
114 } else {
115 button.p_0().w(px(BUTTON_SIZE))
116 }
117 })
118 .tooltip(tr(tooltip_key))
119 .debug_selector(|| HEADER_CALL_TAG.into())
120 .child(
121 h_flex()
122 .gap(px(6.))
123 .items_center()
124 .child(icon(icon_name, 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(&primary_app, meet_now, 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 = menu_app.clone();
152 let audio = menu_app.clone();
153 let audio_item = PopupMenuItem::new(tr("calls.chrome.audio_call"))
154 .icon(menu_icon(IconName::Phone))
155 .action(Box::new(StartAudioCall))
156 .on_click(move |_, _, cx| start_call(&audio, false, cx));
157 if meet_now {
158 popup.item(audio_item)
159 } else {
160 popup
161 .item(
162 PopupMenuItem::new(tr("calls.chrome.video_call"))
163 .icon(menu_icon(IconName::Video))
164 .action(Box::new(StartVideoCall))
165 .on_click(move |_, _, cx| start_call(&video, true, cx)),
166 )
167 .item(audio_item)
168 }
169 }),
170 )
171 .into_any_element()
172}