1use crate::{item::ItemHandle, ItemDeserializers, Member, Pane, PaneAxis, Workspace, WorkspaceId};
2use anyhow::{Context, Result};
3use async_recursion::async_recursion;
4use db::sqlez::{
5 bindable::{Bind, Column, StaticColumnCount},
6 statement::Statement,
7};
8use gpui::{
9 platform::WindowBounds, AsyncAppContext, Axis, ModelHandle, Task, ViewHandle, WeakViewHandle,
10};
11use project::Project;
12use std::{
13 path::{Path, PathBuf},
14 sync::Arc,
15};
16use util::ResultExt;
17use uuid::Uuid;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct WorkspaceLocation(Arc<Vec<PathBuf>>);
21
22impl WorkspaceLocation {
23 pub fn paths(&self) -> Arc<Vec<PathBuf>> {
24 self.0.clone()
25 }
26}
27
28impl<P: AsRef<Path>, T: IntoIterator<Item = P>> From<T> for WorkspaceLocation {
29 fn from(iterator: T) -> Self {
30 let mut roots = iterator
31 .into_iter()
32 .map(|p| p.as_ref().to_path_buf())
33 .collect::<Vec<_>>();
34 roots.sort();
35 Self(Arc::new(roots))
36 }
37}
38
39impl StaticColumnCount for WorkspaceLocation {}
40impl Bind for &WorkspaceLocation {
41 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
42 bincode::serialize(&self.0)
43 .expect("Bincode serialization of paths should not fail")
44 .bind(statement, start_index)
45 }
46}
47
48impl Column for WorkspaceLocation {
49 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
50 let blob = statement.column_blob(start_index)?;
51 Ok((
52 WorkspaceLocation(bincode::deserialize(blob).context("Bincode failed")?),
53 start_index + 1,
54 ))
55 }
56}
57
58#[derive(Debug, PartialEq, Clone)]
59pub struct SerializedWorkspace {
60 pub id: WorkspaceId,
61 pub location: WorkspaceLocation,
62 pub center_group: SerializedPaneGroup,
63 pub bounds: Option<WindowBounds>,
64 pub display: Option<Uuid>,
65 pub docks: DockStructure,
66}
67
68#[derive(Debug, PartialEq, Clone, Default)]
69pub struct DockStructure {
70 pub(crate) left: DockData,
71 pub(crate) right: DockData,
72 pub(crate) bottom: DockData,
73}
74
75impl Column for DockStructure {
76 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
77 let (left, next_index) = DockData::column(statement, start_index)?;
78 let (right, next_index) = DockData::column(statement, next_index)?;
79 let (bottom, next_index) = DockData::column(statement, next_index)?;
80 Ok((
81 DockStructure {
82 left,
83 right,
84 bottom,
85 },
86 next_index,
87 ))
88 }
89}
90
91impl Bind for DockStructure {
92 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
93 let next_index = statement.bind(&self.left, start_index)?;
94 let next_index = statement.bind(&self.right, next_index)?;
95 statement.bind(&self.bottom, next_index)
96 }
97}
98
99#[derive(Debug, PartialEq, Clone, Default)]
100pub struct DockData {
101 pub(crate) visible: bool,
102 pub(crate) active_panel: Option<String>,
103 pub(crate) zoom: bool,
104}
105
106impl Column for DockData {
107 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
108 let (visible, next_index) = Option::<bool>::column(statement, start_index)?;
109 let (active_panel, next_index) = Option::<String>::column(statement, next_index)?;
110 let (zoom, next_index) = Option::<bool>::column(statement, next_index)?;
111 Ok((
112 DockData {
113 visible: visible.unwrap_or(false),
114 active_panel,
115 zoom: zoom.unwrap_or(false),
116 },
117 next_index,
118 ))
119 }
120}
121
122impl Bind for DockData {
123 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
124 let next_index = statement.bind(&self.visible, start_index)?;
125 let next_index = statement.bind(&self.active_panel, next_index)?;
126 statement.bind(&self.zoom, next_index)
127 }
128}
129
130#[derive(Debug, PartialEq, Eq, Clone)]
131pub enum SerializedPaneGroup {
132 Group {
133 axis: Axis,
134 children: Vec<SerializedPaneGroup>,
135 },
136 Pane(SerializedPane),
137}
138
139#[cfg(test)]
140impl Default for SerializedPaneGroup {
141 fn default() -> Self {
142 Self::Pane(SerializedPane {
143 children: vec![SerializedItem::default()],
144 active: false,
145 })
146 }
147}
148
149impl SerializedPaneGroup {
150 #[async_recursion(?Send)]
151 pub(crate) async fn deserialize(
152 &self,
153 project: &ModelHandle<Project>,
154 workspace_id: WorkspaceId,
155 workspace: &WeakViewHandle<Workspace>,
156 cx: &mut AsyncAppContext,
157 ) -> Option<(
158 Member,
159 Option<ViewHandle<Pane>>,
160 Vec<Option<Box<dyn ItemHandle>>>,
161 )> {
162 match self {
163 SerializedPaneGroup::Group { axis, children } => {
164 let mut current_active_pane = None;
165 let mut members = Vec::new();
166 let mut items = Vec::new();
167 for child in children {
168 if let Some((new_member, active_pane, new_items)) = child
169 .deserialize(project, workspace_id, workspace, cx)
170 .await
171 {
172 members.push(new_member);
173 items.extend(new_items);
174 current_active_pane = current_active_pane.or(active_pane);
175 }
176 }
177
178 if members.is_empty() {
179 return None;
180 }
181
182 if members.len() == 1 {
183 return Some((members.remove(0), current_active_pane, items));
184 }
185
186 Some((
187 Member::Axis(PaneAxis {
188 axis: *axis,
189 members,
190 }),
191 current_active_pane,
192 items,
193 ))
194 }
195 SerializedPaneGroup::Pane(serialized_pane) => {
196 let pane = workspace
197 .update(cx, |workspace, cx| workspace.add_pane(cx).downgrade())
198 .log_err()?;
199 let active = serialized_pane.active;
200 let new_items = serialized_pane
201 .deserialize_to(project, &pane, workspace_id, workspace, cx)
202 .await
203 .log_err()?;
204
205 if pane
206 .read_with(cx, |pane, _| pane.items_len() != 0)
207 .log_err()?
208 {
209 let pane = pane.upgrade(cx)?;
210 Some((Member::Pane(pane.clone()), active.then(|| pane), new_items))
211 } else {
212 let pane = pane.upgrade(cx)?;
213 workspace
214 .update(cx, |workspace, cx| workspace.force_remove_pane(&pane, cx))
215 .log_err()?;
216 None
217 }
218 }
219 }
220 }
221}
222
223#[derive(Debug, PartialEq, Eq, Default, Clone)]
224pub struct SerializedPane {
225 pub(crate) active: bool,
226 pub(crate) children: Vec<SerializedItem>,
227}
228
229impl SerializedPane {
230 pub fn new(children: Vec<SerializedItem>, active: bool) -> Self {
231 SerializedPane { children, active }
232 }
233
234 pub async fn deserialize_to(
235 &self,
236 project: &ModelHandle<Project>,
237 pane: &WeakViewHandle<Pane>,
238 workspace_id: WorkspaceId,
239 workspace: &WeakViewHandle<Workspace>,
240 cx: &mut AsyncAppContext,
241 ) -> Result<Vec<Option<Box<dyn ItemHandle>>>> {
242 let mut items = Vec::new();
243 let mut active_item_index = None;
244 for (index, item) in self.children.iter().enumerate() {
245 let project = project.clone();
246 let item_handle = pane
247 .update(cx, |_, cx| {
248 if let Some(deserializer) = cx.global::<ItemDeserializers>().get(&item.kind) {
249 deserializer(project, workspace.clone(), workspace_id, item.item_id, cx)
250 } else {
251 Task::ready(Err(anyhow::anyhow!(
252 "Deserializer does not exist for item kind: {}",
253 item.kind
254 )))
255 }
256 })?
257 .await
258 .log_err();
259
260 items.push(item_handle.clone());
261
262 if let Some(item_handle) = item_handle {
263 pane.update(cx, |pane, cx| {
264 pane.add_item(item_handle.clone(), true, true, None, cx);
265 })?;
266 }
267
268 if item.active {
269 active_item_index = Some(index);
270 }
271 }
272
273 if let Some(active_item_index) = active_item_index {
274 pane.update(cx, |pane, cx| {
275 pane.activate_item(active_item_index, false, false, cx);
276 })?;
277 }
278
279 anyhow::Ok(items)
280 }
281}
282
283pub type GroupId = i64;
284pub type PaneId = i64;
285pub type ItemId = usize;
286
287#[derive(Debug, PartialEq, Eq, Clone)]
288pub struct SerializedItem {
289 pub kind: Arc<str>,
290 pub item_id: ItemId,
291 pub active: bool,
292}
293
294impl SerializedItem {
295 pub fn new(kind: impl AsRef<str>, item_id: ItemId, active: bool) -> Self {
296 Self {
297 kind: Arc::from(kind.as_ref()),
298 item_id,
299 active,
300 }
301 }
302}
303
304#[cfg(test)]
305impl Default for SerializedItem {
306 fn default() -> Self {
307 SerializedItem {
308 kind: Arc::from("Terminal"),
309 item_id: 100000,
310 active: false,
311 }
312 }
313}
314
315impl StaticColumnCount for SerializedItem {
316 fn column_count() -> usize {
317 3
318 }
319}
320impl Bind for &SerializedItem {
321 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
322 let next_index = statement.bind(&self.kind, start_index)?;
323 let next_index = statement.bind(&self.item_id, next_index)?;
324 statement.bind(&self.active, next_index)
325 }
326}
327
328impl Column for SerializedItem {
329 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
330 let (kind, next_index) = Arc::<str>::column(statement, start_index)?;
331 let (item_id, next_index) = ItemId::column(statement, next_index)?;
332 let (active, next_index) = bool::column(statement, next_index)?;
333 Ok((
334 SerializedItem {
335 kind,
336 item_id,
337 active,
338 },
339 next_index,
340 ))
341 }
342}