Merge pull request #2468 from zed-industries/touch-up-status

Mikayla Maki created

Improve status integration

Change summary

Cargo.lock                                |  1 
crates/editor/src/items.rs                | 23 +++++++++
crates/gpui/src/elements.rs               |  9 +++
crates/project/src/worktree.rs            | 14 +++--
crates/project_panel/src/project_panel.rs | 33 +++++---------
crates/theme/Cargo.toml                   |  1 
crates/theme/src/ui.rs                    | 57 +++++++++++++++++++++++-
7 files changed, 107 insertions(+), 31 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -6688,6 +6688,7 @@ name = "theme"
 version = "0.1.0"
 dependencies = [
  "anyhow",
+ "fs",
  "gpui",
  "indexmap",
  "parking_lot 0.11.2",

crates/editor/src/items.rs 🔗

@@ -14,7 +14,7 @@ use language::{
     proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point,
     SelectionGoal,
 };
-use project::{FormatTrigger, Item as _, Project, ProjectPath};
+use project::{repository::GitFileStatus, FormatTrigger, Item as _, Project, ProjectPath};
 use rpc::proto::{self, update_view};
 use settings::Settings;
 use smallvec::SmallVec;
@@ -27,6 +27,7 @@ use std::{
     path::{Path, PathBuf},
 };
 use text::Selection;
+use theme::ui::FileName;
 use util::{ResultExt, TryFutureExt};
 use workspace::item::{BreadcrumbText, FollowableItemHandle};
 use workspace::{
@@ -565,8 +566,25 @@ impl Item for Editor {
         style: &theme::Tab,
         cx: &AppContext,
     ) -> AnyElement<T> {
+        fn git_file_status(this: &Editor, cx: &AppContext) -> Option<GitFileStatus> {
+            let project_entry_id = this
+                .buffer()
+                .read(cx)
+                .as_singleton()?
+                .read(cx)
+                .entry_id(cx)?;
+            let project = this.project.as_ref()?.read(cx);
+            let path = project.path_for_entry(project_entry_id, cx)?.path;
+            let worktree = project.worktree_for_entry(project_entry_id, cx)?.read(cx);
+            worktree.repo_for(&path)?.status_for_path(&worktree, &path)
+        }
+
         Flex::row()
-            .with_child(Label::new(self.title(cx).to_string(), style.label.clone()).aligned())
+            .with_child(ComponentHost::new(FileName::new(
+                self.title(cx).to_string(),
+                git_file_status(self, cx),
+                FileName::style(style.label.clone(), &cx.global::<Settings>().theme),
+            )))
             .with_children(detail.and_then(|detail| {
                 let path = path_for_buffer(&self.buffer, detail, false, cx)?;
                 let description = path.to_string_lossy();
@@ -580,6 +598,7 @@ impl Item for Editor {
                     .aligned(),
                 )
             }))
+            .align_children_center()
             .into_any()
     }
 

crates/gpui/src/elements.rs 🔗

@@ -578,6 +578,15 @@ pub struct ComponentHost<V: View, C: Component<V>> {
     view_type: PhantomData<V>,
 }
 
+impl<V: View, C: Component<V>> ComponentHost<V, C> {
+    pub fn new(c: C) -> Self {
+        Self {
+            component: c,
+            view_type: PhantomData,
+        }
+    }
+}
+
 impl<V: View, C: Component<V>> Deref for ComponentHost<V, C> {
     type Target = C;
 

crates/project/src/worktree.rs 🔗

@@ -195,10 +195,10 @@ impl RepositoryEntry {
                             (GitFileStatus::Conflict, _) | (_, GitFileStatus::Conflict) => {
                                 &GitFileStatus::Conflict
                             }
-                            (GitFileStatus::Added, _) | (_, GitFileStatus::Added) => {
-                                &GitFileStatus::Added
+                            (GitFileStatus::Modified, _) | (_, GitFileStatus::Modified) => {
+                                &GitFileStatus::Modified
                             }
-                            _ => &GitFileStatus::Modified,
+                            _ => &GitFileStatus::Added,
                         },
                     )
                     .copied()
@@ -3086,7 +3086,7 @@ impl BackgroundScanner {
                 }
 
                 let git_ptr = local_repo.repo_ptr.lock();
-                git_ptr.worktree_status(&repo_path)?
+                git_ptr.worktree_status(&repo_path)
             };
 
             let work_dir = repo.work_directory(snapshot)?;
@@ -3097,7 +3097,11 @@ impl BackgroundScanner {
                 .update(&work_dir_id, |entry| entry.scan_id = scan_id);
 
             snapshot.repository_entries.update(&work_dir, |entry| {
-                entry.worktree_statuses.insert(repo_path, status)
+                if let Some(status) = status {
+                    entry.worktree_statuses.insert(repo_path, status);
+                } else {
+                    entry.worktree_statuses.remove(&repo_path);
+                }
             });
         }
 

crates/project_panel/src/project_panel.rs 🔗

@@ -6,7 +6,7 @@ use gpui::{
     actions,
     anyhow::{anyhow, Result},
     elements::{
-        AnchorCorner, ChildView, ContainerStyle, Empty, Flex, Label, MouseEventHandler,
+        AnchorCorner, ChildView, ComponentHost, ContainerStyle, Empty, Flex, MouseEventHandler,
         ParentElement, ScrollTarget, Stack, Svg, UniformList, UniformListState,
     },
     geometry::vector::Vector2F,
@@ -29,7 +29,7 @@ use std::{
     path::Path,
     sync::Arc,
 };
-use theme::ProjectPanelEntry;
+use theme::{ui::FileName, ProjectPanelEntry};
 use unicase::UniCase;
 use workspace::Workspace;
 
@@ -1083,19 +1083,6 @@ impl ProjectPanel {
         let kind = details.kind;
         let show_editor = details.is_editing && !details.is_processing;
 
-        // Prepare colors for git statuses
-        let editor_theme = &cx.global::<Settings>().theme.editor;
-        let mut filename_text_style = style.text.clone();
-        filename_text_style.color = details
-            .git_status
-            .as_ref()
-            .map(|status| match status {
-                GitFileStatus::Added => editor_theme.diff.inserted,
-                GitFileStatus::Modified => editor_theme.diff.modified,
-                GitFileStatus::Conflict => editor_theme.diff.deleted,
-            })
-            .unwrap_or(style.text.color);
-
         Flex::row()
             .with_child(
                 if kind == EntryKind::Dir {
@@ -1123,12 +1110,16 @@ impl ProjectPanel {
                     .flex(1.0, true)
                     .into_any()
             } else {
-                Label::new(details.filename.clone(), filename_text_style)
-                    .contained()
-                    .with_margin_left(style.icon_spacing)
-                    .aligned()
-                    .left()
-                    .into_any()
+                ComponentHost::new(FileName::new(
+                    details.filename.clone(),
+                    details.git_status,
+                    FileName::style(style.text.clone(), &cx.global::<Settings>().theme),
+                ))
+                .contained()
+                .with_margin_left(style.icon_spacing)
+                .aligned()
+                .left()
+                .into_any()
             })
             .constrained()
             .with_height(style.height)

crates/theme/Cargo.toml 🔗

@@ -10,6 +10,7 @@ doctest = false
 
 [dependencies]
 gpui = { path = "../gpui" }
+fs = { path = "../fs" }
 anyhow.workspace = true
 indexmap = "1.6.2"
 parking_lot.workspace = true

crates/theme/src/ui.rs 🔗

@@ -1,9 +1,10 @@
 use std::borrow::Cow;
 
+use fs::repository::GitFileStatus;
 use gpui::{
     color::Color,
     elements::{
-        ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label,
+        ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label, LabelStyle,
         MouseEventHandler, ParentElement, Stack, Svg,
     },
     fonts::TextStyle,
@@ -11,11 +12,11 @@ use gpui::{
     platform,
     platform::MouseButton,
     scene::MouseClick,
-    Action, Element, EventContext, MouseState, View, ViewContext,
+    Action, AnyElement, Element, EventContext, MouseState, View, ViewContext,
 };
 use serde::Deserialize;
 
-use crate::{ContainedText, Interactive};
+use crate::{ContainedText, Interactive, Theme};
 
 #[derive(Clone, Deserialize, Default)]
 pub struct CheckboxStyle {
@@ -252,3 +253,53 @@ where
         .constrained()
         .with_height(style.dimensions().y())
 }
+
+pub struct FileName {
+    filename: String,
+    git_status: Option<GitFileStatus>,
+    style: FileNameStyle,
+}
+
+pub struct FileNameStyle {
+    template_style: LabelStyle,
+    git_inserted: Color,
+    git_modified: Color,
+    git_deleted: Color,
+}
+
+impl FileName {
+    pub fn new(filename: String, git_status: Option<GitFileStatus>, style: FileNameStyle) -> Self {
+        FileName {
+            filename,
+            git_status,
+            style,
+        }
+    }
+
+    pub fn style<I: Into<LabelStyle>>(style: I, theme: &Theme) -> FileNameStyle {
+        FileNameStyle {
+            template_style: style.into(),
+            git_inserted: theme.editor.diff.inserted,
+            git_modified: theme.editor.diff.modified,
+            git_deleted: theme.editor.diff.deleted,
+        }
+    }
+}
+
+impl<V: View> gpui::elements::Component<V> for FileName {
+    fn render(&self, _: &mut V, _: &mut ViewContext<V>) -> AnyElement<V> {
+        // Prepare colors for git statuses
+        let mut filename_text_style = self.style.template_style.text.clone();
+        filename_text_style.color = self
+            .git_status
+            .as_ref()
+            .map(|status| match status {
+                GitFileStatus::Added => self.style.git_inserted,
+                GitFileStatus::Modified => self.style.git_modified,
+                GitFileStatus::Conflict => self.style.git_deleted,
+            })
+            .unwrap_or(self.style.template_style.text.color);
+
+        Label::new(self.filename.clone(), filename_text_style).into_any()
+    }
+}