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