BlueGit

ict rusty-teams

Files

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

saved_panel.rs

saved_panel.rs 9.9 KiB Download Raw
1use chrono::{DateTime, Local, Offset};
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4use gpui_kit::*;
5use teams_core::SavedMessage;
6
7use super::avatar::person_avatar;
8use super::title_bar::TITLE_BAR_HEIGHT;
9use super::widgets::icon;
10use crate::app_state::{AppState, selection_title};
11use crate::format;
12use crate::notify::selection_for;
13use crate::theme;
14
15const PANEL_WIDTH: f32 = 420.;
16const PANEL_MAX_HEIGHT_RATIO: f32 = 0.7;
17const PANEL_MARGIN: f32 = 8.;
18const AVATAR_SIZE: f32 = 32.;
19const BUTTON_SIZE: f32 = 24.;
20const PREVIEW_LINES: usize = 2;
21const EMPTY_TEXT: &str = "No saved messages. Use Save message in the ... menu of a message.";
22const UNKNOWN_AUTHOR: &str = "Unknown";
23
24pub enum SavedPanelEvent {
25 Close,
26 Open {
27 conversation_id: String,
28 message_id: String,
29 },
30}
31
32pub struct SavedPanel {
33 app: Entity<AppState>,
34 focus_handle: FocusHandle,
35 scroll: ScrollHandle,
36 _observation: Subscription,
37}
38
39impl EventEmitter<SavedPanelEvent> for SavedPanel {}
40
41pub fn count_label(count: usize) -> String {
42 match count {
43 0 => String::new(),
44 count => count.to_string(),
45 }
46}
47
48impl SavedPanel {
49 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
50 let focus_handle = cx.focus_handle();
51 window.focus(&focus_handle, cx);
52 let observation = cx.observe(&app, |_, _, cx| cx.notify());
53 app.update(cx, |state, cx| state.refresh_saved(cx));
54 SavedPanel {
55 app,
56 focus_handle,
57 scroll: ScrollHandle::new(),
58 _observation: observation,
59 }
60 }
61
62 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
63 if event.keystroke.key == "escape" {
64 cx.emit(SavedPanelEvent::Close);
65 }
66 }
67
68 fn header(&self, count: usize) -> Div {
69 h_flex()
70 .px(px(12.))
71 .pt(px(10.))
72 .pb(px(8.))
73 .gap(px(8.))
74 .items_center()
75 .border_b_1()
76 .border_color(theme::border())
77 .child(
78 div()
79 .text_size(px(15.))
80 .font_weight(FontWeight::SEMIBOLD)
81 .text_color(theme::text())
82 .child("Saved"),
83 )
84 .child(
85 div()
86 .text_size(px(12.))
87 .text_color(theme::text_muted())
88 .child(count_label(count)),
89 )
90 }
91
92 fn row(
93 &self,
94 item: &SavedMessage,
95 index: usize,
96 title: String,
97 panel: Entity<SavedPanel>,
98 cx: &App,
99 ) -> AnyElement {
100 let app = self.app.clone();
101 let state = self.app.read(cx);
102 let now = Local::now();
103 let author = item.author_name.as_deref().unwrap_or(UNKNOWN_AUTHOR);
104 let group = SharedString::from(format!("saved-row-group-{index}"));
105 let (conversation_id, message_id) = (item.conversation_id.clone(), item.message_id.clone());
106 let unsave_item = item.clone();
107 h_flex()
108 .id(ElementId::Name(format!("saved-row-{index}").into()))
109 .group(group.clone())
110 .mx(px(6.))
111 .px(px(6.))
112 .py(px(8.))
113 .gap(px(10.))
114 .items_start()
115 .rounded(px(6.))
116 .cursor_pointer()
117 .hover(|row| row.bg(theme::row_hover()))
118 .child(div().flex_none().size(px(AVATAR_SIZE)).child(person_avatar(
119 &state.directory,
120 item.author_id.as_deref(),
121 author,
122 AVATAR_SIZE,
123 )))
124 .child(
125 v_flex()
126 .flex_1()
127 .min_w_0()
128 .gap(px(2.))
129 .child(
130 h_flex()
131 .gap(px(6.))
132 .items_baseline()
133 .child(
134 div()
135 .flex_none()
136 .text_size(px(13.))
137 .font_weight(FontWeight::SEMIBOLD)
138 .text_color(theme::text())
139 .child(author.to_owned()),
140 )
141 .child(
142 div()
143 .min_w_0()
144 .truncate()
145 .text_size(px(12.))
146 .text_color(theme::text_muted())
147 .child(title),
148 ),
149 )
150 .child(
151 div()
152 .line_clamp(PREVIEW_LINES)
153 .text_size(px(12.5))
154 .text_color(theme::text_soft())
155 .child(item.preview.clone()),
156 ),
157 )
158 .child(
159 v_flex()
160 .flex_none()
161 .items_end()
162 .gap(px(2.))
163 .child(
164 div()
165 .text_size(px(11.))
166 .text_color(theme::text_muted())
167 .child(saved_time(item.saved_at, now)),
168 )
169 .child(
170 div()
171 .id(ElementId::Name(format!("saved-unsave-{index}").into()))
172 .size(px(BUTTON_SIZE))
173 .flex()
174 .flex_none()
175 .items_center()
176 .justify_center()
177 .rounded(px(6.))
178 .cursor_pointer()
179 .opacity(0.)
180 .group_hover(group, |button| button.opacity(1.))
181 .hover(|button| button.bg(theme::border_strong()))
182 .child(icon(IconName::BookmarkOff, 14., theme::text_muted()))
183 .tooltip(|window, cx| Tooltip::new("Unsave").build(window, cx))
184 .on_click(move |_, _, cx| {
185 cx.stop_propagation();
186 let entry = unsave_item.clone();
187 app.update(cx, |state, cx| state.set_saved(entry, false, cx));
188 }),
189 ),
190 )
191 .on_click(move |_, _, cx| {
192 let event = SavedPanelEvent::Open {
193 conversation_id: conversation_id.clone(),
194 message_id: message_id.clone(),
195 };
196 panel.update(cx, |_, cx| cx.emit(event));
197 })
198 .into_any_element()
199 }
200}
201
202fn saved_time(saved_at: DateTime<chrono::Utc>, now: DateTime<Local>) -> String {
203 format::list_time_label(saved_at, now.date_naive(), now.offset().fix())
204}
205
206impl Render for SavedPanel {
207 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
208 let panel = cx.entity();
209 let state = self.app.read(cx);
210 let items = state.saved.items().to_vec();
211 let mut rows: Vec<AnyElement> = items
212 .iter()
213 .enumerate()
214 .map(|(index, item)| {
215 let title = selection_for(&state.sidebar, &item.conversation_id)
216 .map(|selection| selection_title(&state.sidebar, &selection))
217 .or_else(|| item.topic.clone())
218 .unwrap_or_default();
219 self.row(item, index, title, panel.clone(), cx)
220 })
221 .collect();
222 if rows.is_empty() {
223 rows.push(
224 div()
225 .py(px(32.))
226 .px(px(24.))
227 .flex()
228 .justify_center()
229 .text_center()
230 .text_size(px(13.))
231 .text_color(theme::text_muted())
232 .child(EMPTY_TEXT)
233 .into_any_element(),
234 );
235 }
236 let max_height = f32::from(window.viewport_size().height) * PANEL_MAX_HEIGHT_RATIO;
237 div()
238 .id("saved-layer")
239 .absolute()
240 .top(px(TITLE_BAR_HEIGHT))
241 .left_0()
242 .right_0()
243 .bottom_0()
244 .occlude()
245 .flex()
246 .justify_end()
247 .items_start()
248 .p(px(PANEL_MARGIN))
249 .on_click(cx.listener(|_, _, _, cx| cx.emit(SavedPanelEvent::Close)))
250 .child(
251 v_flex()
252 .id("saved-card")
253 .track_focus(&self.focus_handle)
254 .w(px(PANEL_WIDTH))
255 .max_h(px(max_height))
256 .rounded(px(10.))
257 .border_1()
258 .border_color(theme::border_strong())
259 .bg(theme::surface())
260 .shadow_lg()
261 .overflow_hidden()
262 .occlude()
263 .on_click(|_, _, cx| cx.stop_propagation())
264 .on_key_down(cx.listener(Self::on_key_down))
265 .child(self.header(items.len()))
266 .child(
267 v_flex()
268 .id("saved-list")
269 .flex_1()
270 .min_h_0()
271 .py(px(4.))
272 .overflow_y_scroll()
273 .track_scroll(&self.scroll)
274 .children(rows),
275 ),
276 )
277 }
278}
279
280#[cfg(test)]
281mod tests {
282 use super::count_label;
283
284 #[test]
285 fn the_header_count_is_hidden_when_nothing_is_saved() {
286 assert_eq!(count_label(0), "");
287 assert_eq!(count_label(2), "2");
288 }
289}