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