Skip to repository content475 lines · 17.9 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T03:27:27.191Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
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, commit::ParsedCommitMessage};
7use gpui::{
8 AbsoluteLength, App, Asset, Element, Entity, MouseButton, ParentElement, Pixels, Render,
9 ScrollHandle, StatefulInteractiveElement, WeakEntity, prelude::*,
10};
11use markdown::{Markdown, MarkdownElement};
12use project::git_store::Repository;
13use settings::Settings;
14use std::hash::Hash;
15use theme_settings::ThemeSettings;
16use time::{OffsetDateTime, UtcOffset};
17use ui::{Avatar, Chip, CopyButton, Divider, Tooltip, 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 pub tag_names: Vec<SharedString>,
28}
29
30const MAX_COMMIT_TOOLTIP_TAG_CHIPS: usize = 2;
31
32pub(crate) fn commit_tag_chips(tag_names: &[SharedString]) -> Option<impl IntoElement> {
33 if tag_names.is_empty() {
34 return None;
35 }
36
37 let (visible_tags, hidden_tags) =
38 tag_names.split_at(tag_names.len().min(MAX_COMMIT_TOOLTIP_TAG_CHIPS));
39
40 Some(
41 h_flex().max_w(relative(0.6)).gap_1().child(
42 h_flex()
43 .gap_1()
44 .min_w_0()
45 .children(
46 visible_tags
47 .iter()
48 .map(|tag_name| Chip::new(tag_name.clone()).truncate()),
49 )
50 .when(!hidden_tags.is_empty(), |this| {
51 let hidden_tags = hidden_tags.to_vec();
52 this.child(Chip::new(format!("+{}", hidden_tags.len())).tooltip(
53 Tooltip::element(move |_window, cx| {
54 v_flex()
55 .gap_1()
56 .children(itertools::Itertools::intersperse_with(
57 hidden_tags.iter().map(|tag_name| {
58 Label::new(tag_name.clone())
59 .size(LabelSize::Small)
60 .buffer_font(cx)
61 .into_any_element()
62 }),
63 || Divider::horizontal().into_any_element(),
64 ))
65 .into_any_element()
66 }),
67 ))
68 })
69 .child(Divider::vertical()),
70 ),
71 )
72}
73
74const COMMIT_AVATAR_BORDER_WIDTH: Pixels = px(1.);
75
76pub struct CommitAvatar<'a> {
77 sha: &'a SharedString,
78 author_email: Option<SharedString>,
79 remote: Option<&'a GitRemote>,
80 size: Option<AbsoluteLength>,
81}
82
83impl<'a> CommitAvatar<'a> {
84 pub fn new(
85 sha: &'a SharedString,
86 author_email: Option<SharedString>,
87 remote: Option<&'a GitRemote>,
88 ) -> Self {
89 Self {
90 sha,
91 author_email,
92 remote,
93 size: None,
94 }
95 }
96
97 pub fn from_commit_details(details: &'a CommitDetails) -> Self {
98 Self {
99 sha: &details.sha,
100 author_email: Some(details.author_email.clone()),
101 remote: details
102 .message
103 .as_ref()
104 .and_then(|details| details.remote.as_ref()),
105 size: None,
106 }
107 }
108
109 pub fn size(mut self, size: impl Into<AbsoluteLength>) -> Self {
110 self.size = Some(size.into());
111 self
112 }
113
114 pub fn rendered_size(size: impl Into<AbsoluteLength>, window: &Window) -> Pixels {
115 size.into().to_pixels(window.rem_size()) + COMMIT_AVATAR_BORDER_WIDTH * 2.
116 }
117
118 pub fn render(&'a self, window: &mut Window, cx: &mut App) -> AnyElement {
119 let border_color = cx.theme().colors().border_variant;
120
121 match self.avatar(window, cx) {
122 None => {
123 let container_size = self.size.map(|size| Self::rendered_size(size, window));
124
125 h_flex()
126 .when_some(container_size, |this, size| this.size(size))
127 .justify_center()
128 .rounded_full()
129 .border(COMMIT_AVATAR_BORDER_WIDTH)
130 .border_color(border_color)
131 .bg(cx.theme().colors().element_disabled)
132 .child(
133 Icon::new(IconName::Person)
134 .color(Color::Muted)
135 .size(IconSize::XSmall),
136 )
137 .into_any_element()
138 }
139 Some(avatar) => avatar
140 .when_some(self.size, |this, size| this.size(size))
141 .border_color(border_color)
142 .into_any_element(),
143 }
144 }
145
146 pub fn avatar(&'a self, window: &mut Window, cx: &mut App) -> Option<Avatar> {
147 // Bail early if the email isn't available yet. Without it,
148 // the GitHub provider skips the fast CDN path and falls back
149 // to an unauthenticated per-commit API call that is slow and
150 // rate-limited. Worse, a failed lookup gets permanently
151 // cached under the key (sha, host) — so even when the email
152 // arrives on a later render, the cached None shadows the
153 // fast path forever.
154 self.author_email.as_ref()?;
155
156 let remote = self
157 .remote
158 .filter(|remote| remote.host_supports_avatars())?;
159 let avatar_url =
160 CommitAvatarAsset::new(remote.clone(), self.sha.clone(), self.author_email.clone());
161
162 let url = window.use_asset::<CommitAvatarAsset>(&avatar_url, cx)??;
163 Some(Avatar::new(url.to_string()))
164 }
165}
166
167#[derive(Clone, Debug)]
168struct CommitAvatarAsset {
169 sha: SharedString,
170 author_email: Option<SharedString>,
171 remote: GitRemote,
172}
173
174impl Hash for CommitAvatarAsset {
175 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
176 self.sha.hash(state);
177 self.remote.host.name().hash(state);
178 }
179}
180
181impl CommitAvatarAsset {
182 fn new(remote: GitRemote, sha: SharedString, author_email: Option<SharedString>) -> Self {
183 Self {
184 remote,
185 sha,
186 author_email,
187 }
188 }
189}
190
191impl Asset for CommitAvatarAsset {
192 type Source = Self;
193 type Output = Option<SharedString>;
194
195 fn load(
196 source: Self::Source,
197 cx: &mut App,
198 ) -> impl Future<Output = Self::Output> + Send + 'static {
199 let client = cx.http_client();
200
201 async move {
202 source
203 .remote
204 .avatar_url(source.sha, source.author_email, client)
205 .await
206 .map(|url| SharedString::from(url.to_string()))
207 }
208 }
209}
210
211pub struct CommitTooltip {
212 commit: CommitDetails,
213 scroll_handle: ScrollHandle,
214 markdown: Entity<Markdown>,
215 repository: Entity<Repository>,
216 workspace: WeakEntity<Workspace>,
217}
218
219impl CommitTooltip {
220 pub fn blame_entry(
221 blame: &BlameEntry,
222 details: Option<ParsedCommitMessage>,
223 tag_names: Vec<SharedString>,
224 repository: Entity<Repository>,
225 workspace: WeakEntity<Workspace>,
226 cx: &mut Context<Self>,
227 ) -> Self {
228 let commit_time = blame
229 .committer_time
230 .and_then(|t| OffsetDateTime::from_unix_timestamp(t).ok())
231 .unwrap_or(OffsetDateTime::now_utc());
232
233 Self::new(
234 CommitDetails {
235 sha: blame.sha.to_string().into(),
236 commit_time,
237 author_name: blame
238 .author
239 .clone()
240 .unwrap_or("<no name>".to_string())
241 .into(),
242 author_email: blame.author_mail.clone().unwrap_or("".to_string()).into(),
243 message: details,
244 tag_names,
245 },
246 repository,
247 workspace,
248 cx,
249 )
250 }
251
252 pub fn new(
253 commit: CommitDetails,
254 repository: Entity<Repository>,
255 workspace: WeakEntity<Workspace>,
256 cx: &mut Context<Self>,
257 ) -> Self {
258 let markdown = cx.new(|cx| {
259 Markdown::new(
260 commit
261 .message
262 .as_ref()
263 .map(|message| message.message.clone())
264 .unwrap_or_default(),
265 None,
266 None,
267 cx,
268 )
269 });
270 Self {
271 commit,
272 repository,
273 workspace,
274 scroll_handle: ScrollHandle::new(),
275 markdown,
276 }
277 }
278}
279
280impl Render for CommitTooltip {
281 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
282 let avatar = CommitAvatar::from_commit_details(&self.commit).render(window, cx);
283
284 let author = self.commit.author_name.clone();
285
286 let author_email = self.commit.author_email.clone();
287
288 let short_commit_id = self
289 .commit
290 .sha
291 .get(0..git::SHORT_SHA_LENGTH)
292 .map(|sha| sha.to_string().into())
293 .unwrap_or_else(|| self.commit.sha.clone());
294 let full_sha = self.commit.sha.to_string();
295 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
296 let absolute_timestamp = time_format::format_localized_timestamp(
297 self.commit.commit_time,
298 OffsetDateTime::now_utc(),
299 local_offset,
300 time_format::TimestampFormat::MediumAbsolute,
301 );
302 let markdown_style = {
303 let style = hover_markdown_style(window, cx);
304 style
305 };
306
307 let message = self
308 .commit
309 .message
310 .as_ref()
311 .map(|_| {
312 MarkdownElement::new(self.markdown.clone(), markdown_style)
313 .scroll_handle(self.scroll_handle.clone())
314 .into_any()
315 })
316 .unwrap_or("<no commit message>".into_any());
317
318 let pull_request = self
319 .commit
320 .message
321 .as_ref()
322 .and_then(|details| details.pull_request.clone());
323 let tag_names = self.commit.tag_names.clone();
324
325 let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
326 let message_max_height = window.line_height() * 12 + (ui_font_size / 0.4);
327 let repo = self.repository.clone();
328 let workspace = self.workspace.clone();
329 let commit_summary = CommitSummary {
330 sha: self.commit.sha.clone(),
331 subject: self
332 .commit
333 .message
334 .as_ref()
335 .map_or(Default::default(), |message| {
336 message
337 .message
338 .split('\n')
339 .next()
340 .unwrap()
341 .trim_end()
342 .to_string()
343 .into()
344 }),
345 commit_timestamp: self.commit.commit_time.unix_timestamp(),
346 author_name: self.commit.author_name.clone(),
347 has_parent: false,
348 };
349
350 tooltip_container(cx, move |this, cx| {
351 this.occlude()
352 .on_mouse_move(|_, _, cx| cx.stop_propagation())
353 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
354 .child(
355 v_flex()
356 .w(gpui::rems(30.))
357 .child(
358 h_flex()
359 .pb_1()
360 .gap_2()
361 .overflow_x_hidden()
362 .flex_wrap()
363 .child(avatar)
364 .child(author)
365 .when(!author_email.is_empty(), |this| {
366 this.child(
367 div()
368 .text_color(cx.theme().colors().text_muted)
369 .child(author_email),
370 )
371 })
372 .border_b_1()
373 .border_color(cx.theme().colors().border_variant),
374 )
375 .child(
376 div()
377 .id("inline-blame-commit-message")
378 .track_scroll(&self.scroll_handle)
379 .py_1p5()
380 .max_h(message_max_height)
381 .overflow_y_scroll()
382 .child(message),
383 )
384 .child(
385 h_flex()
386 .text_color(cx.theme().colors().text_muted)
387 .w_full()
388 .justify_between()
389 .pt_1()
390 .gap_1()
391 .flex_wrap()
392 .border_t_1()
393 .border_color(cx.theme().colors().border_variant)
394 .child(absolute_timestamp)
395 .child(
396 h_flex()
397 .gap_1()
398 .min_w_0()
399 .children(commit_tag_chips(&tag_names))
400 .when_some(pull_request, |this, pr| {
401 this.child(
402 Button::new(
403 "pull-request-button",
404 format!("#{}", pr.number),
405 )
406 .color(Color::Muted)
407 .start_icon(
408 Icon::new(IconName::PullRequest)
409 .size(IconSize::Small)
410 .color(Color::Muted),
411 )
412 .on_click(move |_, _, cx| {
413 cx.stop_propagation();
414 cx.open_url(pr.url.as_str())
415 }),
416 )
417 .child(Divider::vertical())
418 })
419 .child(
420 Button::new(
421 "commit-sha-button",
422 short_commit_id.clone(),
423 )
424 .color(Color::Muted)
425 .start_icon(
426 Icon::new(IconName::FileGit)
427 .size(IconSize::Small)
428 .color(Color::Muted),
429 )
430 .on_click(
431 move |_, window, cx| {
432 CommitView::open(
433 commit_summary.sha.to_string(),
434 repo.downgrade(),
435 workspace.clone(),
436 None,
437 None,
438 window,
439 cx,
440 );
441 cx.stop_propagation();
442 },
443 ),
444 )
445 .child(Divider::vertical())
446 .child(
447 CopyButton::new("copy-commit-sha", full_sha)
448 .tooltip_label("Copy SHA"),
449 ),
450 ),
451 ),
452 )
453 })
454 }
455}
456
457fn blame_entry_timestamp(blame_entry: &BlameEntry, format: time_format::TimestampFormat) -> String {
458 match blame_entry.author_offset_date_time() {
459 Ok(timestamp) => {
460 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
461 time_format::format_localized_timestamp(
462 timestamp,
463 time::OffsetDateTime::now_utc(),
464 local_offset,
465 format,
466 )
467 }
468 Err(_) => "Error parsing date".to_string(),
469 }
470}
471
472pub fn blame_entry_relative_timestamp(blame_entry: &BlameEntry) -> String {
473 blame_entry_timestamp(blame_entry, time_format::TimestampFormat::Relative)
474}
475