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;
 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        window: &mut Window,
122        cx: &mut Context<Self>,
123    ) -> Self {
124        let commit_time = blame
125            .committer_time
126            .and_then(|t| OffsetDateTime::from_unix_timestamp(t).ok())
127            .unwrap_or(OffsetDateTime::now_utc());
128
129        Self::new(
130            CommitDetails {
131                sha: blame.sha.to_string().into(),
132                commit_time,
133                author_name: blame
134                    .author
135                    .clone()
136                    .unwrap_or("<no name>".to_string())
137                    .into(),
138                author_email: blame.author_mail.clone().unwrap_or("".to_string()).into(),
139                message: details,
140            },
141            repository,
142            workspace,
143            window,
144            cx,
145        )
146    }
147
148    pub fn new(
149        commit: CommitDetails,
150        repository: Entity<Repository>,
151        workspace: WeakEntity<Workspace>,
152        window: &mut Window,
153        cx: &mut Context<Self>,
154    ) -> Self {
155        let mut style = hover_markdown_style(window, cx);
156        if let Some(code_block) = &style.code_block.text {
157            style.base_text_style.refine(code_block);
158        }
159        let markdown = cx.new(|cx| {
160            Markdown::new(
161                commit
162                    .message
163                    .as_ref()
164                    .map(|message| message.message.clone())
165                    .unwrap_or_default(),
166                style,
167                None,
168                None,
169                cx,
170            )
171        });
172        Self {
173            commit,
174            repository,
175            workspace,
176            scroll_handle: ScrollHandle::new(),
177            markdown,
178        }
179    }
180}
181
182impl Render for CommitTooltip {
183    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
184        let avatar = CommitAvatar::new(&self.commit).render(window, cx);
185
186        let author = self.commit.author_name.clone();
187
188        let author_email = self.commit.author_email.clone();
189
190        let short_commit_id = self
191            .commit
192            .sha
193            .get(0..8)
194            .map(|sha| sha.to_string().into())
195            .unwrap_or_else(|| self.commit.sha.clone());
196        let full_sha = self.commit.sha.to_string().clone();
197        let absolute_timestamp = format_local_timestamp(
198            self.commit.commit_time,
199            OffsetDateTime::now_utc(),
200            time_format::TimestampFormat::MediumAbsolute,
201        );
202
203        let message = self
204            .commit
205            .message
206            .as_ref()
207            .map(|_| self.markdown.clone().into_any_element())
208            .unwrap_or("<no commit message>".into_any());
209
210        let pull_request = self
211            .commit
212            .message
213            .as_ref()
214            .and_then(|details| details.pull_request.clone());
215
216        let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
217        let message_max_height = window.line_height() * 12 + (ui_font_size / 0.4);
218        let repo = self.repository.clone();
219        let workspace = self.workspace.clone();
220        let commit_summary = CommitSummary {
221            sha: self.commit.sha.clone(),
222            subject: self
223                .commit
224                .message
225                .as_ref()
226                .map_or(Default::default(), |message| {
227                    message
228                        .message
229                        .split('\n')
230                        .next()
231                        .unwrap()
232                        .trim_end()
233                        .to_string()
234                        .into()
235                }),
236            commit_timestamp: self.commit.commit_time.unix_timestamp(),
237            has_parent: false,
238        };
239
240        tooltip_container(window, cx, move |this, _, cx| {
241            this.occlude()
242                .on_mouse_move(|_, _, cx| cx.stop_propagation())
243                .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
244                .child(
245                    v_flex()
246                        .w(gpui::rems(30.))
247                        .gap_4()
248                        .child(
249                            h_flex()
250                                .pb_1p5()
251                                .gap_x_2()
252                                .overflow_x_hidden()
253                                .flex_wrap()
254                                .children(avatar)
255                                .child(author)
256                                .when(!author_email.is_empty(), |this| {
257                                    this.child(
258                                        div()
259                                            .text_color(cx.theme().colors().text_muted)
260                                            .child(author_email),
261                                    )
262                                })
263                                .border_b_1()
264                                .border_color(cx.theme().colors().border_variant),
265                        )
266                        .child(
267                            div()
268                                .id("inline-blame-commit-message")
269                                .child(message)
270                                .max_h(message_max_height)
271                                .overflow_y_scroll()
272                                .track_scroll(&self.scroll_handle),
273                        )
274                        .child(
275                            h_flex()
276                                .text_color(cx.theme().colors().text_muted)
277                                .w_full()
278                                .justify_between()
279                                .pt_1p5()
280                                .border_t_1()
281                                .border_color(cx.theme().colors().border_variant)
282                                .child(absolute_timestamp)
283                                .child(
284                                    h_flex()
285                                        .gap_1p5()
286                                        .when_some(pull_request, |this, pr| {
287                                            this.child(
288                                                Button::new(
289                                                    "pull-request-button",
290                                                    format!("#{}", pr.number),
291                                                )
292                                                .color(Color::Muted)
293                                                .icon(IconName::PullRequest)
294                                                .icon_color(Color::Muted)
295                                                .icon_position(IconPosition::Start)
296                                                .style(ButtonStyle::Subtle)
297                                                .on_click(move |_, _, cx| {
298                                                    cx.stop_propagation();
299                                                    cx.open_url(pr.url.as_str())
300                                                }),
301                                            )
302                                        })
303                                        .child(Divider::vertical())
304                                        .child(
305                                            Button::new(
306                                                "commit-sha-button",
307                                                short_commit_id.clone(),
308                                            )
309                                            .style(ButtonStyle::Subtle)
310                                            .color(Color::Muted)
311                                            .icon(IconName::FileGit)
312                                            .icon_color(Color::Muted)
313                                            .icon_position(IconPosition::Start)
314                                            .on_click(
315                                                move |_, window, cx| {
316                                                    CommitView::open(
317                                                        commit_summary.clone(),
318                                                        repo.downgrade(),
319                                                        workspace.clone(),
320                                                        window,
321                                                        cx,
322                                                    );
323                                                    cx.stop_propagation();
324                                                },
325                                            ),
326                                        )
327                                        .child(
328                                            IconButton::new("copy-sha-button", IconName::Copy)
329                                                .shape(IconButtonShape::Square)
330                                                .icon_size(IconSize::Small)
331                                                .icon_color(Color::Muted)
332                                                .on_click(move |_, _, cx| {
333                                                    cx.stop_propagation();
334                                                    cx.write_to_clipboard(
335                                                        ClipboardItem::new_string(full_sha.clone()),
336                                                    )
337                                                }),
338                                        ),
339                                ),
340                        ),
341                )
342        })
343    }
344}
345
346fn blame_entry_timestamp(blame_entry: &BlameEntry, format: time_format::TimestampFormat) -> String {
347    match blame_entry.author_offset_date_time() {
348        Ok(timestamp) => {
349            let local = chrono::Local::now().offset().local_minus_utc();
350            time_format::format_localized_timestamp(
351                timestamp,
352                time::OffsetDateTime::now_utc(),
353                UtcOffset::from_whole_seconds(local).unwrap(),
354                format,
355            )
356        }
357        Err(_) => "Error parsing date".to_string(),
358    }
359}
360
361pub fn blame_entry_relative_timestamp(blame_entry: &BlameEntry) -> String {
362    blame_entry_timestamp(blame_entry, time_format::TimestampFormat::Relative)
363}