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