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