Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix Git blame gutter width with avatars (#61287)

Xin Zhao1f99101eb0b9
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 276c083dd15..635521b4602 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -11598,15 +11598,12 @@ impl EditorSnapshot {
let renderer = cx.global::<GlobalBlameRenderer>().0.clone();
const MAX_RELATIVE_TIMESTAMP: &str = "2 years, 11 months ago";
- /// The number of characters to dedicate to gaps and margins.
- const SPACING_WIDTH: usize = 4;
-
let max_char_count = max_author_length.min(renderer.max_author_length())
+ ::git::SHORT_SHA_LENGTH
- + MAX_RELATIVE_TIMESTAMP.len()
- + SPACING_WIDTH;
+ + MAX_RELATIVE_TIMESTAMP.len();
ch_advance * max_char_count
+ + renderer.blame_entry_non_text_width(window, cx)
});
let is_singleton = self.buffer_snapshot().is_singleton();
diff --git a/crates/editor/src/git/blame.rs b/crates/editor/src/git/blame.rs
index 6423182d504..347a1e8dafc 100644
--- a/crates/editor/src/git/blame.rs
+++ b/crates/editor/src/git/blame.rs
@@ -8,7 +8,7 @@ use git::{
commit::ParsedCommitMessage,
};
use gpui::{
- AnyElement, App, AppContext as _, Context, Entity, Hsla, ScrollHandle, SharedString,
+ AnyElement, App, AppContext as _, Context, Entity, Hsla, Pixels, ScrollHandle, SharedString,
Subscription, Task, TextStyle, WeakEntity, Window,
};
use itertools::Itertools;
@@ -87,6 +87,10 @@ pub struct GitBlame {
pub trait BlameRenderer {
fn max_author_length(&self) -> usize;
+ fn blame_entry_non_text_width(&self, _: &Window, _: &App) -> Pixels {
+ Pixels::ZERO
+ }
+
fn render_blame_entry(
&self,
_: &TextStyle,
diff --git a/crates/git_ui/src/blame_ui.rs b/crates/git_ui/src/blame_ui.rs
index ec1386e54dc..07097e21092 100644
--- a/crates/git_ui/src/blame_ui.rs
+++ b/crates/git_ui/src/blame_ui.rs
@@ -5,7 +5,7 @@ use crate::{
use editor::{BlameRenderer, Editor, hover_markdown_style};
use git::{blame::BlameEntry, commit::ParsedCommitMessage, repository::CommitSummary};
use gpui::{
- ClipboardItem, Entity, Hsla, MouseButton, ScrollHandle, Subscription, TextStyle,
+ ClipboardItem, Entity, Hsla, MouseButton, Pixels, Rems, ScrollHandle, Subscription, TextStyle,
TextStyleRefinement, UnderlineStyle, WeakEntity, prelude::*,
};
use markdown::{Markdown, MarkdownElement};
@@ -20,6 +20,9 @@ use ui::{ContextMenu, CopyButton, Divider, prelude::*, tooltip_container};
use workspace::Workspace;
const GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED: usize = 20;
+const GIT_BLAME_GUTTER_MARGIN: Rems = rems(0.5);
+const GIT_BLAME_GUTTER_GAP: Rems = rems(0.5);
+const GIT_BLAME_AVATAR_SIZE: Rems = rems(1.);
pub struct GitBlameRenderer;
@@ -126,6 +129,19 @@ impl BlameRenderer for GitBlameRenderer {
GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED
}
+ fn blame_entry_non_text_width(&self, window: &Window, cx: &App) -> Pixels {
+ let show_avatar = ProjectSettings::get_global(cx).git.blame.show_avatar;
+ let gap_count = if show_avatar { 3. } else { 2. };
+ let width = GIT_BLAME_GUTTER_MARGIN.to_pixels(window.rem_size())
+ + GIT_BLAME_GUTTER_GAP.to_pixels(window.rem_size()) * gap_count;
+
+ if show_avatar {
+ width + CommitAvatar::rendered_size(GIT_BLAME_AVATAR_SIZE, window)
+ } else {
+ width
+ }
+ }
+
fn render_blame_entry(
&self,
style: &TextStyle,
@@ -160,6 +176,7 @@ impl BlameRenderer for GitBlameRenderer {
author_email,
details.as_ref().and_then(|it| it.remote.as_ref()),
)
+ .size(GIT_BLAME_AVATAR_SIZE)
.render(window, cx),
)
} else {
diff --git a/crates/git_ui/src/commit_tooltip.rs b/crates/git_ui/src/commit_tooltip.rs
index 2c0e81d0e36..14a24f76351 100644
--- a/crates/git_ui/src/commit_tooltip.rs
+++ b/crates/git_ui/src/commit_tooltip.rs
@@ -5,8 +5,8 @@ use git::blame::BlameEntry;
use git::repository::CommitSummary;
use git::{GitRemote, commit::ParsedCommitMessage};
use gpui::{
- AbsoluteLength, App, Asset, Element, Entity, MouseButton, ParentElement, Render, ScrollHandle,
- StatefulInteractiveElement, WeakEntity, prelude::*,
+ AbsoluteLength, App, Asset, Element, Entity, MouseButton, ParentElement, Pixels, Render,
+ ScrollHandle, StatefulInteractiveElement, WeakEntity, prelude::*,
};
use markdown::{Markdown, MarkdownElement};
use project::git_store::Repository;
@@ -71,6 +71,8 @@ pub(crate) fn commit_tag_chips(tag_names: &[SharedString]) -> Option<impl IntoEl
)
}
+const COMMIT_AVATAR_BORDER_WIDTH: Pixels = px(1.);
+
pub struct CommitAvatar<'a> {
sha: &'a SharedString,
author_email: Option<SharedString>,
@@ -109,21 +111,22 @@ impl<'a> CommitAvatar<'a> {
self
}
+ pub fn rendered_size(size: impl Into<AbsoluteLength>, window: &Window) -> Pixels {
+ size.into().to_pixels(window.rem_size()) + COMMIT_AVATAR_BORDER_WIDTH * 2.
+ }
+
pub fn render(&'a self, window: &mut Window, cx: &mut App) -> AnyElement {
let border_color = cx.theme().colors().border_variant;
- let border_width = px(1.);
match self.avatar(window, cx) {
None => {
- let container_size = self
- .size
- .map(|s| s.to_pixels(window.rem_size()) + border_width * 2.);
+ let container_size = self.size.map(|size| Self::rendered_size(size, window));
h_flex()
.when_some(container_size, |this, size| this.size(size))
.justify_center()
.rounded_full()
- .border(border_width)
+ .border(COMMIT_AVATAR_BORDER_WIDTH)
.border_color(border_color)
.bg(cx.theme().colors().element_disabled)
.child(