1use std::marker::PhantomData;
2
3use crate::prelude::*;
4use crate::{theme, Icon, IconColor, IconElement, Label, LabelColor};
5
6#[derive(Element, Clone)]
7pub struct Tab<S: 'static + Send + Sync + Clone> {
8 state_type: PhantomData<S>,
9 title: String,
10 icon: Option<Icon>,
11 current: bool,
12 dirty: bool,
13 fs_status: FileSystemStatus,
14 git_status: GitStatus,
15 diagnostic_status: DiagnosticStatus,
16 close_side: IconSide,
17}
18
19impl<S: 'static + Send + Sync + Clone> Tab<S> {
20 pub fn new() -> Self {
21 Self {
22 state_type: PhantomData,
23 title: "untitled".to_string(),
24 icon: None,
25 current: false,
26 dirty: false,
27 fs_status: FileSystemStatus::None,
28 git_status: GitStatus::None,
29 diagnostic_status: DiagnosticStatus::None,
30 close_side: IconSide::Right,
31 }
32 }
33
34 pub fn current(mut self, current: bool) -> Self {
35 self.current = current;
36 self
37 }
38
39 pub fn title(mut self, title: String) -> Self {
40 self.title = title;
41 self
42 }
43
44 pub fn icon<I>(mut self, icon: I) -> Self
45 where
46 I: Into<Option<Icon>>,
47 {
48 self.icon = icon.into();
49 self
50 }
51
52 pub fn dirty(mut self, dirty: bool) -> Self {
53 self.dirty = dirty;
54 self
55 }
56
57 pub fn fs_status(mut self, fs_status: FileSystemStatus) -> Self {
58 self.fs_status = fs_status;
59 self
60 }
61
62 pub fn git_status(mut self, git_status: GitStatus) -> Self {
63 self.git_status = git_status;
64 self
65 }
66
67 pub fn diagnostic_status(mut self, diagnostic_status: DiagnosticStatus) -> Self {
68 self.diagnostic_status = diagnostic_status;
69 self
70 }
71
72 pub fn close_side(mut self, close_side: IconSide) -> Self {
73 self.close_side = close_side;
74 self
75 }
76
77 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
78 let theme = theme(cx);
79 let has_fs_conflict = self.fs_status == FileSystemStatus::Conflict;
80 let is_deleted = self.fs_status == FileSystemStatus::Deleted;
81
82 let label = match (self.git_status, is_deleted) {
83 (_, true) | (GitStatus::Deleted, false) => Label::new(self.title.clone())
84 .color(LabelColor::Hidden)
85 .set_strikethrough(true),
86 (GitStatus::None, false) => Label::new(self.title.clone()),
87 (GitStatus::Created, false) => {
88 Label::new(self.title.clone()).color(LabelColor::Created)
89 }
90 (GitStatus::Modified, false) => {
91 Label::new(self.title.clone()).color(LabelColor::Modified)
92 }
93 (GitStatus::Renamed, false) => Label::new(self.title.clone()).color(LabelColor::Accent),
94 (GitStatus::Conflict, false) => Label::new(self.title.clone()),
95 };
96
97 let close_icon = IconElement::new(Icon::Close).color(IconColor::Muted);
98
99 div()
100 .px_2()
101 .py_0p5()
102 .flex()
103 .items_center()
104 .justify_center()
105 .fill(if self.current {
106 theme.highest.base.default.background
107 } else {
108 theme.middle.base.default.background
109 })
110 .child(
111 div()
112 .px_1()
113 .flex()
114 .items_center()
115 .gap_1()
116 .children(has_fs_conflict.then(|| {
117 IconElement::new(Icon::ExclamationTriangle)
118 .size(crate::IconSize::Small)
119 .color(IconColor::Warning)
120 }))
121 .children(self.icon.map(IconElement::new))
122 .children(if self.close_side == IconSide::Left {
123 Some(close_icon.clone())
124 } else {
125 None
126 })
127 .child(label)
128 .children(if self.close_side == IconSide::Right {
129 Some(close_icon)
130 } else {
131 None
132 }),
133 )
134 }
135}
136
137#[cfg(feature = "stories")]
138pub use stories::*;
139
140#[cfg(feature = "stories")]
141mod stories {
142 use strum::IntoEnumIterator;
143
144 use crate::{h_stack, v_stack, Icon, Story};
145
146 use super::*;
147
148 #[derive(Element)]
149 pub struct TabStory<S: 'static + Send + Sync + Clone> {
150 state_type: PhantomData<S>,
151 }
152
153 impl<S: 'static + Send + Sync + Clone> TabStory<S> {
154 pub fn new() -> Self {
155 Self {
156 state_type: PhantomData,
157 }
158 }
159
160 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
161 let git_statuses = GitStatus::iter();
162 let fs_statuses = FileSystemStatus::iter();
163
164 Story::container(cx)
165 .child(Story::title_for::<_, Tab<S>>(cx))
166 .child(
167 h_stack().child(
168 v_stack()
169 .gap_2()
170 .child(Story::label(cx, "Default"))
171 .child(Tab::new()),
172 ),
173 )
174 .child(
175 h_stack().child(
176 v_stack().gap_2().child(Story::label(cx, "Current")).child(
177 h_stack()
178 .gap_4()
179 .child(Tab::new().title("Current".to_string()).current(true))
180 .child(Tab::new().title("Not Current".to_string()).current(false)),
181 ),
182 ),
183 )
184 .child(
185 h_stack().child(
186 v_stack()
187 .gap_2()
188 .child(Story::label(cx, "Titled"))
189 .child(Tab::new().title("label".to_string())),
190 ),
191 )
192 .child(
193 h_stack().child(
194 v_stack()
195 .gap_2()
196 .child(Story::label(cx, "With Icon"))
197 .child(
198 Tab::new()
199 .title("label".to_string())
200 .icon(Some(Icon::Envelope)),
201 ),
202 ),
203 )
204 .child(
205 h_stack().child(
206 v_stack()
207 .gap_2()
208 .child(Story::label(cx, "Close Side"))
209 .child(
210 h_stack()
211 .gap_4()
212 .child(
213 Tab::new()
214 .title("Left".to_string())
215 .close_side(IconSide::Left),
216 )
217 .child(Tab::new().title("Right".to_string())),
218 ),
219 ),
220 )
221 .child(
222 v_stack()
223 .gap_2()
224 .child(Story::label(cx, "Git Status"))
225 .child(h_stack().gap_4().children(git_statuses.map(|git_status| {
226 Tab::new()
227 .title(git_status.to_string())
228 .git_status(git_status)
229 }))),
230 )
231 .child(
232 v_stack()
233 .gap_2()
234 .child(Story::label(cx, "File System Status"))
235 .child(h_stack().gap_4().children(fs_statuses.map(|fs_status| {
236 Tab::new().title(fs_status.to_string()).fs_status(fs_status)
237 }))),
238 )
239 }
240 }
241}