Refactor out git status into FileName component

Integrate file name component into the editor's tab content
This commit is contained in:
Mikayla Maki 2023-05-13 02:26:45 -07:00
parent e1c1100c7b
commit 41bef2e444
No known key found for this signature in database
6 changed files with 98 additions and 26 deletions

1
Cargo.lock generated
View File

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

View File

@ -14,7 +14,7 @@ use language::{
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point, proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point,
SelectionGoal, SelectionGoal,
}; };
use project::{FormatTrigger, Item as _, Project, ProjectPath}; use project::{repository::GitFileStatus, FormatTrigger, Item as _, Project, ProjectPath};
use rpc::proto::{self, update_view}; use rpc::proto::{self, update_view};
use settings::Settings; use settings::Settings;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -27,6 +27,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use text::Selection; use text::Selection;
use theme::ui::FileName;
use util::{ResultExt, TryFutureExt}; use util::{ResultExt, TryFutureExt};
use workspace::item::{BreadcrumbText, FollowableItemHandle}; use workspace::item::{BreadcrumbText, FollowableItemHandle};
use workspace::{ use workspace::{
@ -565,8 +566,25 @@ impl Item for Editor {
style: &theme::Tab, style: &theme::Tab,
cx: &AppContext, cx: &AppContext,
) -> AnyElement<T> { ) -> 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() 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| { .with_children(detail.and_then(|detail| {
let path = path_for_buffer(&self.buffer, detail, false, cx)?; let path = path_for_buffer(&self.buffer, detail, false, cx)?;
let description = path.to_string_lossy(); let description = path.to_string_lossy();
@ -580,6 +598,7 @@ impl Item for Editor {
.aligned(), .aligned(),
) )
})) }))
.align_children_center()
.into_any() .into_any()
} }

View File

@ -578,6 +578,15 @@ pub struct ComponentHost<V: View, C: Component<V>> {
view_type: PhantomData<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> { impl<V: View, C: Component<V>> Deref for ComponentHost<V, C> {
type Target = C; type Target = C;

View File

@ -6,7 +6,7 @@ use gpui::{
actions, actions,
anyhow::{anyhow, Result}, anyhow::{anyhow, Result},
elements::{ elements::{
AnchorCorner, ChildView, ContainerStyle, Empty, Flex, Label, MouseEventHandler, AnchorCorner, ChildView, ComponentHost, ContainerStyle, Empty, Flex, MouseEventHandler,
ParentElement, ScrollTarget, Stack, Svg, UniformList, UniformListState, ParentElement, ScrollTarget, Stack, Svg, UniformList, UniformListState,
}, },
geometry::vector::Vector2F, geometry::vector::Vector2F,
@ -29,7 +29,7 @@ use std::{
path::Path, path::Path,
sync::Arc, sync::Arc,
}; };
use theme::ProjectPanelEntry; use theme::{ui::FileName, ProjectPanelEntry};
use unicase::UniCase; use unicase::UniCase;
use workspace::Workspace; use workspace::Workspace;
@ -1083,19 +1083,6 @@ impl ProjectPanel {
let kind = details.kind; let kind = details.kind;
let show_editor = details.is_editing && !details.is_processing; 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() Flex::row()
.with_child( .with_child(
if kind == EntryKind::Dir { if kind == EntryKind::Dir {
@ -1123,12 +1110,16 @@ impl ProjectPanel {
.flex(1.0, true) .flex(1.0, true)
.into_any() .into_any()
} else { } else {
Label::new(details.filename.clone(), filename_text_style) ComponentHost::new(FileName::new(
.contained() details.filename.clone(),
.with_margin_left(style.icon_spacing) details.git_status,
.aligned() FileName::style(style.text.clone(), &cx.global::<Settings>().theme),
.left() ))
.into_any() .contained()
.with_margin_left(style.icon_spacing)
.aligned()
.left()
.into_any()
}) })
.constrained() .constrained()
.with_height(style.height) .with_height(style.height)

View File

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

View File

@ -1,9 +1,10 @@
use std::borrow::Cow; use std::borrow::Cow;
use fs::repository::GitFileStatus;
use gpui::{ use gpui::{
color::Color, color::Color,
elements::{ elements::{
ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label, ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label, LabelStyle,
MouseEventHandler, ParentElement, Stack, Svg, MouseEventHandler, ParentElement, Stack, Svg,
}, },
fonts::TextStyle, fonts::TextStyle,
@ -11,11 +12,11 @@ use gpui::{
platform, platform,
platform::MouseButton, platform::MouseButton,
scene::MouseClick, scene::MouseClick,
Action, Element, EventContext, MouseState, View, ViewContext, Action, AnyElement, Element, EventContext, MouseState, View, ViewContext,
}; };
use serde::Deserialize; use serde::Deserialize;
use crate::{ContainedText, Interactive}; use crate::{ContainedText, Interactive, Theme};
#[derive(Clone, Deserialize, Default)] #[derive(Clone, Deserialize, Default)]
pub struct CheckboxStyle { pub struct CheckboxStyle {
@ -252,3 +253,53 @@ where
.constrained() .constrained()
.with_height(style.dimensions().y()) .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()
}
}