commit_tooltip.rs

  1use crate::commit_view::CommitView;
  2use editor::hover_markdown_style;
  3use futures::Future;
  4use git::blame::BlameEntry;
  5use git::repository::CommitSummary;
  6use git::{GitRemote, blame::ParsedCommitMessage};
  7use gpui::{
  8    App, Asset, ClipboardItem, Element, Entity, MouseButton, ParentElement, Render, ScrollHandle,
  9    StatefulInteractiveElement, WeakEntity, prelude::*,
 10};
 11use markdown::{Markdown, MarkdownElement};
 12use project::git_store::Repository;
 13use settings::Settings;
 14use std::hash::Hash;
 15use theme::ThemeSettings;
 16use time::{OffsetDateTime, UtcOffset};
 17use time_format::format_local_timestamp;
 18use ui::{Avatar, Divider, IconButtonShape, prelude::*, tooltip_container};
 19use workspace::Workspace;
 20
 21#[derive(Clone, Debug)]
 22pub struct CommitDetails {
 23    pub sha: SharedString,
 24    pub author_name: SharedString,
 25    pub author_email: SharedString,
 26    pub commit_time: OffsetDateTime,
 27    pub message: Option<ParsedCommitMessage>,
 28}
 29
 30struct CommitAvatar<'a> {
 31    commit: &'a CommitDetails,
 32}
 33
 34impl<'a> CommitAvatar<'a> {
 35    fn new(details: &'a CommitDetails) -> Self {
 36        Self { commit: details }
 37    }
 38}
 39
 40impl<'a> CommitAvatar<'a> {
 41    fn render(
 42        &'a self,
 43        window: &mut Window,
 44        cx: &mut Context<CommitTooltip>,
 45    ) -> Option<impl IntoElement + use<>> {
 46        let remote = self
 47            .commit
 48            .message
 49            .as_ref()
 50            .and_then(|details| details.remote.clone())
 51            .filter(|remote| remote.host_supports_avatars())?;
 52
 53        let avatar_url = CommitAvatarAsset::new(remote, self.commit.sha.clone());
 54
 55        let element = match window.use_asset::<CommitAvatarAsset>(&avatar_url, cx) {
 56            // Loading or no avatar found
 57            None | Some(None) => Icon::new(IconName::Person)
 58                .color(Color::Muted)
 59                .into_element()
 60                .into_any(),
 61            // Found
 62            Some(Some(url)) => Avatar::new(url.to_string()).into_element().into_any(),
 63        };
 64        Some(element)
 65    }
 66}
 67
 68#[derive(Clone, Debug)]
 69struct CommitAvatarAsset {
 70    sha: SharedString,
 71    remote: GitRemote,
 72}
 73
 74impl Hash for CommitAvatarAsset {
 75    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 76        self.sha.hash(state);
 77        self.remote.host.name().hash(state);
 78    }
 79}
 80
 81impl CommitAvatarAsset {
 82    fn new(remote: GitRemote, sha: SharedString) -> Self {
 83        Self { remote, sha }
 84    }
 85}
 86
 87impl Asset for CommitAvatarAsset {
 88    type Source = Self;
 89    type Output = Option<SharedString>;
 90
 91    fn load(
 92        source: Self::Source,
 93        cx: &mut App,
 94    ) -> impl Future<Output = Self::Output> + Send + 'static {
 95        let client = cx.http_client();
 96
 97        async move {
 98            source
 99                .remote
100                .avatar_url(source.sha, client)
101                .await
102                .map(|url| SharedString::from(url.to_string()))
103        }
104    }
105}
106
107pub struct CommitTooltip {
108    commit: CommitDetails,
109    scroll_handle: ScrollHandle,
110    markdown: Entity<Markdown>,
111    repository: Entity<Repository>,
112    workspace: WeakEntity<Workspace>,
113}
114
115impl CommitTooltip {
116    pub fn blame_entry(
117        blame: &BlameEntry,
118        details: Option<ParsedCommitMessage>,
119        repository: Entity<Repository>,
120        workspace: WeakEntity<Workspace>,
121        cx: &mut Context<Self>,
122    ) -> Self {
123        let commit_time = blame
124            .committer_time
125            .and_then(|t| OffsetDateTime::from_unix_timestamp(t).ok())
126            .unwrap_or(OffsetDateTime::now_utc());
127
128        Self::new(
129            CommitDetails {
130                sha: blame.sha.to_string().into(),
131                commit_time,
132                author_name: blame
133                    .author
134                    .clone()
135                    .unwrap_or("<no name>".to_string())
136                    .into(),
137                author_email: blame.author_mail.clone().unwrap_or("".to_string()).into(),
138                message: details,
139            },
140            repository,
141            workspace,
142            cx,
143        )
144    }
145
146    pub fn new(
147        commit: CommitDetails,
148        repository: Entity<Repository>,
149        workspace: WeakEntity<Workspace>,
150        cx: &mut Context<Self>,
151    ) -> Self {
152        let markdown = cx.new(|cx| {
153            Markdown::new(
154                commit
155                    .message
156                    .as_ref()
157                    .map(|message| message.message.clone())
158                    .unwrap_or_default(),
159                None,
160                None,
161                cx,
162            )
163        });
164        Self {
165            commit,
166            repository,
167            workspace,
168            scroll_handle: ScrollHandle::new(),
169            markdown,
170        }
171    }
172}
173
174impl Render for CommitTooltip {
175    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
176        let avatar = CommitAvatar::new(&self.commit).render(window, cx);
177
178        let author = self.commit.author_name.clone();
179
180        let author_email = self.commit.author_email.clone();
181
182        let short_commit_id = self
183            .commit
184            .sha
185            .get(0..8)
186            .map(|sha| sha.to_string().into())
187            .unwrap_or_else(|| self.commit.sha.clone());
188        let full_sha = self.commit.sha.to_string().clone();
189        let absolute_timestamp = format_local_timestamp(
190            self.commit.commit_time,
191            OffsetDateTime::now_utc(),
192            time_format::TimestampFormat::MediumAbsolute,
193        );
194        let markdown_style = {
195            let mut style = hover_markdown_style(window, cx);
196            if let Some(code_block) = &style.code_block.text {
197                style.base_text_style.refine(code_block);
198            }
199            style
200        };
201
202        let message = self
203            .commit
204            .message
205            .as_ref()
206            .map(|_| MarkdownElement::new(self.markdown.clone(), markdown_style).into_any())
207            .unwrap_or("<no commit message>".into_any());
208
209        let pull_request = self
210            .commit
211            .message
212            .as_ref()
213            .and_then(|details| details.pull_request.clone());
214
215        let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
216        let message_max_height = window.line_height() * 12 + (ui_font_size / 0.4);
217        let repo = self.repository.clone();
218        let workspace = self.workspace.clone();
219        let commit_summary = CommitSummary {
220            sha: self.commit.sha.clone(),
221            subject: self
222                .commit
223                .message
224                .as_ref()
225                .map_or(Default::default(), |message| {
226                    message
227                        .message
228                        .split('\n')
229                        .next()
230                        .unwrap()
231                        .trim_end()
232                        .to_string()
233                        .into()
234                }),
235            commit_timestamp: self.commit.commit_time.unix_timestamp(),
236            has_parent: false,
237        };
238
239        tooltip_container(window, cx, move |this, _, cx| {
240            this.occlude()
241                .on_mouse_move(|_, _, cx| cx.stop_propagation())
242                .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
243                .child(
244                    v_flex()
245                        .w(gpui::rems(30.))
246                        .gap_4()
247                        .child(
248                            h_flex()
249                                .pb_1p5()
250                                .gap_x_2()
251                                .overflow_x_hidden()
252                                .flex_wrap()
253                                .children(avatar)
254                                .child(author)
255                                .when(!author_email.is_empty(), |this| {
256                                    this.child(
257                                        div()
258                                            .text_color(cx.theme().colors().text_muted)
259                                            .child(author_email),
260                                    )
261                                })
262                                .border_b_1()
263                                .border_color(cx.theme().colors().border_variant),
264                        )
265                        .child(
266                            div()
267                                .id("inline-blame-commit-message")
268                                .child(message)
269                                .max_h(message_max_height)
270                                .overflow_y_scroll()
271                                .track_scroll(&self.scroll_handle),
272                        )
273                        .child(
274                            h_flex()
275                                .text_color(cx.theme().colors().text_muted)
276                                .w_full()
277                                .justify_between()
278                                .pt_1p5()
279                                .border_t_1()
280                                .border_color(cx.theme().colors().border_variant)
281                                .child(absolute_timestamp)
282                                .child(
283                                    h_flex()
284                                        .gap_1p5()
285                                        .when_some(pull_request, |this, pr| {
286                                            this.child(
287                                                Button::new(
288                                                    "pull-request-button",
289                                                    format!("#{}", pr.number),
290                                                )
291                                                .color(Color::Muted)
292                                                .icon(IconName::PullRequest)
293                                                .icon_color(Color::Muted)
294                                                .icon_position(IconPosition::Start)
295                                                .style(ButtonStyle::Subtle)
296                                                .on_click(move |_, _, cx| {
297                                                    cx.stop_propagation();
298                                                    cx.open_url(pr.url.as_str())
299                                                }),
300                                            )
301                                        })
302                                        .child(Divider::vertical())
303                                        .child(
304                                            Button::new(
305                                                "commit-sha-button",
306                                                short_commit_id.clone(),
307                                            )
308                                            .style(ButtonStyle::Subtle)
309                                            .color(Color::Muted)
310                                            .icon(IconName::FileGit)
311                                            .icon_color(Color::Muted)
312                                            .icon_position(IconPosition::Start)
313                                            .on_click(
314                                                move |_, window, cx| {
315                                                    CommitView::open(
316                                                        commit_summary.clone(),
317                                                        repo.downgrade(),
318                                                        workspace.clone(),
319                                                        window,
320                                                        cx,
321                                                    );
322                                                    cx.stop_propagation();
323                                                },
324                                            ),
325                                        )
326                                        .child(
327                                            IconButton::new("copy-sha-button", IconName::Copy)
328                                                .shape(IconButtonShape::Square)
329                                                .icon_size(IconSize::Small)
330                                                .icon_color(Color::Muted)
331                                                .on_click(move |_, _, cx| {
332                                                    cx.stop_propagation();
333                                                    cx.write_to_clipboard(
334                                                        ClipboardItem::new_string(full_sha.clone()),
335                                                    )
336                                                }),
337                                        ),
338                                ),
339                        ),
340                )
341        })
342    }
343}
344
345fn blame_entry_timestamp(blame_entry: &BlameEntry, format: time_format::TimestampFormat) -> String {
346    match blame_entry.author_offset_date_time() {
347        Ok(timestamp) => {
348            let local = chrono::Local::now().offset().local_minus_utc();
349            time_format::format_localized_timestamp(
350                timestamp,
351                time::OffsetDateTime::now_utc(),
352                UtcOffset::from_whole_seconds(local).unwrap(),
353                format,
354            )
355        }
356        Err(_) => "Error parsing date".to_string(),
357    }
358}
359
360pub fn blame_entry_relative_timestamp(blame_entry: &BlameEntry) -> String {
361    blame_entry_timestamp(blame_entry, time_format::TimestampFormat::Relative)
362}