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"
dependencies = [
"anyhow",
"fs",
"gpui",
"indexmap",
"parking_lot 0.11.2",

View File

@ -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()
}

View File

@ -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;

View File

@ -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)

View File

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

View File

@ -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()
}
}