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 mut style = hover_markdown_style(window, cx);
201            if let Some(code_block) = &style.code_block.text {
202                style.base_text_style.refine(code_block);
203            }
204            style
205        };
206
207        let message = self
208            .commit
209            .message
210            .as_ref()
211            .map(|_| MarkdownElement::new(self.markdown.clone(), markdown_style).into_any())
212            .unwrap_or("<no commit message>".into_any());
213
214        let pull_request = self
215            .commit
216            .message
217            .as_ref()
218            .and_then(|details| details.pull_request.clone());
219
220        let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
221        let message_max_height = window.line_height() * 12 + (ui_font_size / 0.4);
222        let repo = self.repository.clone();
223        let workspace = self.workspace.clone();
224        let commit_summary = CommitSummary {
225            sha: self.commit.sha.clone(),
226            subject: self
227                .commit
228                .message
229                .as_ref()
230                .map_or(Default::default(), |message| {
231                    message
232                        .message
233                        .split('\n')
234                        .next()
235                        .unwrap()
236                        .trim_end()
237                        .to_string()
238                        .into()
239                }),
240            commit_timestamp: self.commit.commit_time.unix_timestamp(),
241            author_name: self.commit.author_name.clone(),
242            has_parent: false,
243        };
244
245        tooltip_container(cx, move |this, cx| {
246            this.occlude()
247                .on_mouse_move(|_, _, cx| cx.stop_propagation())
248                .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
249                .child(
250                    v_flex()
251                        .w(gpui::rems(30.))
252                        .gap_4()
253                        .child(
254                            h_flex()
255                                .pb_1p5()
256                                .gap_x_2()
257                                .overflow_x_hidden()
258                                .flex_wrap()
259                                .children(avatar)
260                                .child(author)
261                                .when(!author_email.is_empty(), |this| {
262                                    this.child(
263                                        div()
264                                            .text_color(cx.theme().colors().text_muted)
265                                            .child(author_email),
266                                    )
267                                })
268                                .border_b_1()
269                                .border_color(cx.theme().colors().border_variant),
270                        )
271                        .child(
272                            div()
273                                .id("inline-blame-commit-message")
274                                .child(message)
275                                .max_h(message_max_height)
276                                .overflow_y_scroll()
277                                .track_scroll(&self.scroll_handle),
278                        )
279                        .child(
280                            h_flex()
281                                .text_color(cx.theme().colors().text_muted)
282                                .w_full()
283                                .justify_between()
284                                .pt_1p5()
285                                .border_t_1()
286                                .border_color(cx.theme().colors().border_variant)
287                                .child(absolute_timestamp)
288                                .child(
289                                    h_flex()
290                                        .gap_1p5()
291                                        .when_some(pull_request, |this, pr| {
292                                            this.child(
293                                                Button::new(
294                                                    "pull-request-button",
295                                                    format!("#{}", pr.number),
296                                                )
297                                                .color(Color::Muted)
298                                                .icon(IconName::PullRequest)
299                                                .icon_color(Color::Muted)
300                                                .icon_position(IconPosition::Start)
301                                                .style(ButtonStyle::Subtle)
302                                                .on_click(move |_, _, cx| {
303                                                    cx.stop_propagation();
304                                                    cx.open_url(pr.url.as_str())
305                                                }),
306                                            )
307                                        })
308                                        .child(Divider::vertical())
309                                        .child(
310                                            Button::new(
311                                                "commit-sha-button",
312                                                short_commit_id.clone(),
313                                            )
314                                            .style(ButtonStyle::Subtle)
315                                            .color(Color::Muted)
316                                            .icon(IconName::FileGit)
317                                            .icon_color(Color::Muted)
318                                            .icon_position(IconPosition::Start)
319                                            .on_click(
320                                                move |_, window, cx| {
321                                                    CommitView::open(
322                                                        commit_summary.sha.to_string(),
323                                                        repo.downgrade(),
324                                                        workspace.clone(),
325                                                        None,
326                                                        window,
327                                                        cx,
328                                                    );
329                                                    cx.stop_propagation();
330                                                },
331                                            ),
332                                        )
333                                        .child(
334                                            IconButton::new("copy-sha-button", IconName::Copy)
335                                                .shape(IconButtonShape::Square)
336                                                .icon_size(IconSize::Small)
337                                                .icon_color(Color::Muted)
338                                                .on_click(move |_, _, cx| {
339                                                    cx.stop_propagation();
340                                                    cx.write_to_clipboard(
341                                                        ClipboardItem::new_string(full_sha.clone()),
342                                                    )
343                                                }),
344                                        ),
345                                ),
346                        ),
347                )
348        })
349    }
350}
351
352fn blame_entry_timestamp(blame_entry: &BlameEntry, format: time_format::TimestampFormat) -> String {
353    match blame_entry.author_offset_date_time() {
354        Ok(timestamp) => {
355            let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
356            time_format::format_localized_timestamp(
357                timestamp,
358                time::OffsetDateTime::now_utc(),
359                local_offset,
360                format,
361            )
362        }
363        Err(_) => "Error parsing date".to_string(),
364    }
365}
366
367pub fn blame_entry_relative_timestamp(blame_entry: &BlameEntry) -> String {
368    blame_entry_timestamp(blame_entry, time_format::TimestampFormat::Relative)
369}