BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / conversation / chat_header_actions.rs

chat_header_actions.rs

chat_header_actions.rs 7.6 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::button::{Button, ButtonVariants as _};
3use gpui_kit::component::h_flex;
4use gpui_kit::component::menu::{DropdownMenu as _, PopupMenu, PopupMenuItem};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7
8use teams_core::is_notes_chat;
9
10use super::ConversationView;
11use crate::app_state::Selection;
12use crate::data::Directory;
13use crate::find_in_chat::FIND_IN_CHAT;
14use crate::theme;
15use crate::views::chat_menu::{ChatMenuTarget, MenuContext, chat_menu};
16use crate::views::sidebar::channel_menu;
17use crate::views::composer::plus_menu::chat_invitees;
18use crate::views::new_meeting_dialog::open_new_meeting_with;
19use crate::views::avatar::status_glyph;
20use crate::views::widgets::{icon, menu_icon, symbol};
21
22pub const CHAT_HEADER_MORE_TAG: &str = "chat-header-more";
23pub const CHANNEL_HEADER_MORE_TAG: &str = "channel-header-more";
24const SCHEDULE_MEETING_LABEL: &str = "Schedule meeting";
25const BUTTON_SIZE: f32 = 32.;
26const HEADER_COMPACT_WIDTH: f32 = 480.;
27const COUNT_GLYPH: f32 = 16.;
28
29impl ConversationView {
30 fn header_chat_id(&self) -> Option<&str> {
31 match &self.current.as_ref()?.selection {
32 Selection::Chat(chat_id) if !self.draft_active => Some(chat_id),
33 _ => None,
34 }
35 }
36
37 pub(super) fn chat_find_button(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
38 self.header_chat_id().filter(|_| !self.pinned)?;
39 Some(
40 Button::new("chat-find")
41 .icon(IconName::Search)
42 .ghost()
43 .size(px(BUTTON_SIZE))
44 .rounded(px(8.))
45 .tooltip(FIND_IN_CHAT)
46 .on_click(cx.listener(|this, _, window, cx| this.open_find(window, cx)))
47 .into_any_element(),
48 )
49 }
50
51 pub(super) fn channel_more_button(&self, cx: &App) -> Option<AnyElement> {
52 let Selection::Channel(channel_id) = &self.current.as_ref()?.selection else {
53 return None;
54 };
55 if self.pinned || self.popped_out {
56 return None;
57 }
58 let notifications = self
59 .app
60 .read(cx)
61 .sidebar
62 .teams
63 .iter()
64 .find(|entry| entry.channels.iter().any(|channel| &channel.id == channel_id))?
65 .channel_notifications(channel_id);
66 let (state, channel_id) = (self.app.clone(), channel_id.clone());
67 Some(
68 Button::new(CHANNEL_HEADER_MORE_TAG)
69 .icon(IconName::Ellipsis)
70 .ghost()
71 .size(px(BUTTON_SIZE))
72 .rounded(px(8.))
73 .tooltip("More channel options")
74 .debug_selector(|| CHANNEL_HEADER_MORE_TAG.into())
75 .dropdown_menu(move |popup, window, cx| {
76 channel_menu(popup, window, cx, state.clone(), &channel_id, notifications)
77 })
78 .into_any_element(),
79 )
80 }
81
82 pub(super) fn header_overflows(&self) -> bool {
83 let width = self.header_width.get();
84 width > 0.
85 && width < HEADER_COMPACT_WIDTH
86 && !self.popped_out
87 && !self.pinned
88 && self.header_chat_id().is_some()
89 }
90
91 pub(super) fn chat_more_button(&self, overflow: bool, cx: &Context<Self>) -> Option<AnyElement> {
92 let chat_id = self
93 .header_chat_id()
94 .filter(|_| !self.popped_out && !self.pinned)?;
95 let target = ChatMenuTarget::for_chat(self.app.read(cx), chat_id)?;
96 let context = MenuContext::new(&self.app, cx);
97 let view = cx.entity().downgrade();
98 let (open_details, close_details) = self.details_labels();
99 let details_label = if self.channel_details_open {
100 close_details
101 } else {
102 open_details
103 };
104 let copilot_label = self.copilot_label();
105 Some(
106 Button::new(CHAT_HEADER_MORE_TAG)
107 .icon(IconName::Ellipsis)
108 .ghost()
109 .size(px(BUTTON_SIZE))
110 .rounded(px(8.))
111 .tooltip("More chat options")
112 .debug_selector(|| CHAT_HEADER_MORE_TAG.into())
113 .dropdown_menu(move |popup, window, cx| {
114 let popup = if overflow {
115 overflow_items(popup, view.clone(), copilot_label, details_label)
116 } else {
117 popup
118 };
119 let popup = if is_notes_chat(&target.id) {
120 popup
121 } else {
122 schedule_meeting_item(popup, &context, &target.id)
123 };
124 chat_menu(popup, window, cx, &context, &target)
125 })
126 .into_any_element(),
127 )
128 }
129}
130
131fn overflow_items(
132 popup: PopupMenu,
133 view: WeakEntity<ConversationView>,
134 copilot_label: &'static str,
135 details_label: &'static str,
136) -> PopupMenu {
137 let (find_view, details_view) = (view.clone(), view.clone());
138 popup
139 .item(
140 PopupMenuItem::new(FIND_IN_CHAT)
141 .icon(menu_icon(IconName::Search))
142 .on_click(move |_, window, cx| {
143 find_view
144 .update(cx, |this, cx| this.open_find(window, cx))
145 .ok();
146 }),
147 )
148 .item(
149 PopupMenuItem::new(copilot_label)
150 .icon(menu_icon(IconName::Bot))
151 .on_click(move |_, window, cx| {
152 view.update(cx, |this, cx| this.toggle_copilot(window, cx))
153 .ok();
154 }),
155 )
156 .item(
157 PopupMenuItem::new(details_label)
158 .icon(menu_icon(IconName::PanelRight))
159 .on_click(move |_, _, cx| {
160 details_view.update(cx, |this, cx| this.toggle_details(cx)).ok();
161 }),
162 )
163 .separator()
164}
165
166fn schedule_meeting_item(popup: PopupMenu, context: &MenuContext, chat_id: &str) -> PopupMenu {
167 let (state, chat_id) = (context.state.clone(), chat_id.to_owned());
168 popup
169 .item(
170 PopupMenuItem::new(SCHEDULE_MEETING_LABEL)
171 .icon(menu_icon(IconName::Calendar))
172 .on_click(move |_, window, cx| {
173 let invitees = chat_invitees(state.read(cx), &chat_id);
174 open_new_meeting_with(state.clone(), invitees, window, cx);
175 }),
176 )
177 .separator()
178}
179
180pub fn member_count(count: usize) -> Div {
181 h_flex()
182 .gap(px(4.))
183 .items_center()
184 .text_size(px(13.))
185 .text_color(theme::text_soft())
186 .child(icon(IconName::Users, COUNT_GLYPH, theme::text_soft()))
187 .child(count.to_string())
188}
189
190const TITLE_NAMES: usize = 3;
191const NAME_DOT: f32 = 10.;
192
193pub fn group_title(directory: &Directory, faces: &[(Option<String>, String)]) -> Div {
194 let hidden = faces.len().saturating_sub(TITLE_NAMES);
195 h_flex()
196 .min_w_0()
197 .gap(px(6.))
198 .items_center()
199 .children(faces.iter().take(TITLE_NAMES).map(|(user_id, name)| {
200 let first = name.split_whitespace().next().unwrap_or(name).to_owned();
201 let dot = user_id
202 .as_deref()
203 .and_then(|id| status_glyph(directory.presence_of(id).kind()))
204 .map(|(glyph, color)| symbol(glyph, NAME_DOT, color));
205 h_flex()
206 .flex_none()
207 .gap(px(3.))
208 .items_center()
209 .child(first)
210 .children(dot)
211 }))
212 .when(hidden > 0, |row| row.child(format!("+{hidden}")))
213}