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