BlueGit

ict rusty-teams

Files

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

pins.rs

pins.rs 5.8 KiB Download Raw
1use std::rc::Rc;
2
3use gpui_kit::*;
4
5use super::ConversationView;
6use crate::app_state::Selection;
7use crate::rows::reply_excerpt;
8use crate::views::message_actions::Action;
9use crate::views::pin_banner::{PinBannerActions, PinBannerData, cycle_pin, render_pin_banner};
10
11pub(super) struct PinPreview {
12 author: Option<String>,
13 text: String,
14}
15
16impl ConversationView {
17 pub(super) fn chat_id(&self) -> Option<String> {
18 match self.current.as_ref()?.selection {
19 Selection::Chat(ref chat_id) => Some(chat_id.clone()),
20 Selection::Channel(_) => None,
21 }
22 }
23
24 pub(super) fn refresh_pin_previews(&mut self, cx: &mut Context<Self>) {
25 self.pin_previews.clear();
26 self.pin_index = 0;
27 let Some(chat_id) = self.chat_id() else {
28 return;
29 };
30 self.update_pin_previews(&chat_id, cx);
31 cx.notify();
32 }
33
34 pub(super) fn on_pins_changed(&mut self, chat_id: &str, cx: &mut Context<Self>) {
35 if self.chat_id().as_deref() != Some(chat_id) {
36 return;
37 }
38 self.pin_index = 0;
39 self.update_pin_previews(chat_id, cx);
40 cx.notify();
41 }
42
43 fn update_pin_previews(&mut self, chat_id: &str, cx: &mut Context<Self>) {
44 let state = self.app.read(cx);
45 let ids: Vec<String> = state
46 .pinned_messages(chat_id)
47 .iter()
48 .map(|pin| pin.message_id.clone())
49 .collect();
50 let records = state
51 .store
52 .messages_by_id(chat_id, &ids)
53 .unwrap_or_default();
54 self.pin_previews = records
55 .into_iter()
56 .map(|(message_id, record)| {
57 let preview = PinPreview {
58 author: record.sender_name.clone(),
59 text: reply_excerpt(&record),
60 };
61 (message_id, preview)
62 })
63 .collect();
64 let count = ids.len();
65 if self.pin_index >= count {
66 self.pin_index = 0;
67 }
68 }
69
70 fn pin_action(
71 &self,
72 view: WeakEntity<Self>,
73 run: fn(&mut Self, &mut Window, &mut Context<Self>),
74 ) -> Action {
75 Rc::new(move |window, cx| {
76 view.update(cx, |this, cx| run(this, window, cx)).ok();
77 })
78 }
79
80 pub(super) fn render_pin_banner(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
81 let chat_id = self.chat_id()?;
82 let pins = self.app.read(cx).pinned_messages(&chat_id);
83 let pin = pins.get(self.pin_index)?;
84 let preview = self.pin_previews.get(&pin.message_id);
85 let data = PinBannerData {
86 author: preview.and_then(|preview| preview.author.clone()),
87 preview: preview.map(|preview| preview.text.clone()),
88 position: self.pin_index,
89 total: pins.len(),
90 };
91 let view = cx.weak_entity();
92 let actions = PinBannerActions {
93 open: self.pin_action(view.clone(), |this, _, cx| this.open_shown_pin(cx)),
94 previous: self.pin_action(view.clone(), |this, _, cx| this.step_pin(-1, cx)),
95 next: self.pin_action(view.clone(), |this, _, cx| this.step_pin(1, cx)),
96 unpin: self.pin_action(view, |this, _, cx| this.unpin_shown_pin(cx)),
97 };
98 Some(render_pin_banner(data, actions))
99 }
100
101 fn shown_pin_id(&self, cx: &App) -> Option<String> {
102 let chat_id = self.chat_id()?;
103 self.app
104 .read(cx)
105 .pinned_messages(&chat_id)
106 .get(self.pin_index)
107 .map(|pin| pin.message_id.clone())
108 }
109
110 fn open_shown_pin(&mut self, cx: &mut Context<Self>) {
111 let (Some(chat_id), Some(message_id)) = (self.chat_id(), self.shown_pin_id(cx)) else {
112 return;
113 };
114 self.jump_to_message(&chat_id, &message_id, cx);
115 }
116
117 fn step_pin(&mut self, delta: isize, cx: &mut Context<Self>) {
118 let Some(chat_id) = self.chat_id() else {
119 return;
120 };
121 let total = self.app.read(cx).pinned_messages(&chat_id).len();
122 self.pin_index = cycle_pin(self.pin_index, total, delta);
123 cx.notify();
124 }
125
126 fn unpin_shown_pin(&mut self, cx: &mut Context<Self>) {
127 let (Some(chat_id), Some(message_id)) = (self.chat_id(), self.shown_pin_id(cx)) else {
128 return;
129 };
130 self.app.update(cx, |state, cx| {
131 state.set_pinned(&chat_id, &message_id, false, cx)
132 });
133 }
134
135 pub(super) fn forward_message(&mut self, message_id: &str, cx: &mut Context<Self>) {
136 if let Some(conversation_id) = self.conversation_id() {
137 self.app.update(cx, |state, cx| {
138 state.request_forward(&conversation_id, message_id, cx)
139 });
140 }
141 }
142
143 pub(super) fn copy_link(&mut self, message_id: &str, cx: &mut Context<Self>) {
144 if let Some(conversation_id) = self.conversation_id() {
145 self.app.update(cx, |state, cx| {
146 state.copy_message_link(&conversation_id, message_id, cx)
147 });
148 }
149 }
150
151 pub(super) fn toggle_saved(&mut self, message_id: &str, cx: &mut Context<Self>) {
152 if let Some(conversation_id) = self.conversation_id() {
153 self.app.update(cx, |state, cx| {
154 state.toggle_saved(&conversation_id, message_id, cx)
155 });
156 }
157 }
158
159 pub(super) fn toggle_pinned(&mut self, message_id: &str, cx: &mut Context<Self>) {
160 if let Some(chat_id) = self.chat_id() {
161 self.app.update(cx, |state, cx| {
162 state.toggle_pinned(&chat_id, message_id, cx)
163 });
164 }
165 }
166
167 pub(super) fn mark_unread_from(&mut self, message_id: &str, cx: &mut Context<Self>) {
168 if let Some(chat_id) = self.chat_id() {
169 self.app.update(cx, |state, cx| {
170 state.mark_unread_from_message(&chat_id, message_id, cx)
171 });
172 }
173 }
174}