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 .bg(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(
161 &mut self,
162 _view: &mut S,
163 cx: &mut ViewContext<S>,
164 ) -> impl Element<ViewState = S> {
165 let git_statuses = GitStatus::iter();
166 let fs_statuses = FileSystemStatus::iter();
167
168 Story::container(cx)
169 .child(Story::title_for::<_, Tab<S>>(cx))
170 .child(
171 h_stack().child(
172 v_stack()
173 .gap_2()
174 .child(Story::label(cx, "Default"))
175 .child(Tab::new()),
176 ),
177 )
178 .child(
179 h_stack().child(
180 v_stack().gap_2().child(Story::label(cx, "Current")).child(
181 h_stack()
182 .gap_4()
183 .child(Tab::new().title("Current".to_string()).current(true))
184 .child(Tab::new().title("Not Current".to_string()).current(false)),
185 ),
186 ),
187 )
188 .child(
189 h_stack().child(
190 v_stack()
191 .gap_2()
192 .child(Story::label(cx, "Titled"))
193 .child(Tab::new().title("label".to_string())),
194 ),
195 )
196 .child(
197 h_stack().child(
198 v_stack()
199 .gap_2()
200 .child(Story::label(cx, "With Icon"))
201 .child(
202 Tab::new()
203 .title("label".to_string())
204 .icon(Some(Icon::Envelope)),
205 ),
206 ),
207 )
208 .child(
209 h_stack().child(
210 v_stack()
211 .gap_2()
212 .child(Story::label(cx, "Close Side"))
213 .child(
214 h_stack()
215 .gap_4()
216 .child(
217 Tab::new()
218 .title("Left".to_string())
219 .close_side(IconSide::Left),
220 )
221 .child(Tab::new().title("Right".to_string())),
222 ),
223 ),
224 )
225 .child(
226 v_stack()
227 .gap_2()
228 .child(Story::label(cx, "Git Status"))
229 .child(h_stack().gap_4().children(git_statuses.map(|git_status| {
230 Tab::new()
231 .title(git_status.to_string())
232 .git_status(git_status)
233 }))),
234 )
235 .child(
236 v_stack()
237 .gap_2()
238 .child(Story::label(cx, "File System Status"))
239 .child(h_stack().gap_4().children(fs_statuses.map(|fs_status| {
240 Tab::new().title(fs_status.to_string()).fs_status(fs_status)
241 }))),
242 )
243 }
244 }
245}