BlueGit

ict rusty-teams

Files

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

call_controls.rs

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