1use anyhow::Result;
2use collections::vecset::VecSet;
3use gpui::PathPromptOptions;
4use gpui::{
5 AnyView, App, Context, DragMoveEvent, Entity, EntityId, EventEmitter, FocusHandle, Focusable,
6 ManagedView, MouseButton, Pixels, Render, Subscription, Task, Tiling, Window, WindowId,
7 actions, deferred, px,
8};
9use project::{DirectoryLister, DisableAiSettings, Project, ProjectGroupKey};
10use remote::RemoteConnectionOptions;
11use settings::Settings;
12pub use settings::SidebarSide;
13use std::collections::{HashMap, HashSet};
14use std::future::Future;
15use std::path::Path;
16use std::path::PathBuf;
17use ui::prelude::*;
18use util::ResultExt;
19use util::path_list::PathList;
20use zed_actions::agents_sidebar::ToggleThreadSwitcher;
21
22use agent_settings::AgentSettings;
23use settings::SidebarDockPosition;
24use ui::{ContextMenu, right_click_menu};
25
26const SIDEBAR_RESIZE_HANDLE_SIZE: Pixels = px(6.0);
27
28use crate::open_remote_project_with_existing_connection;
29use crate::{
30 CloseIntent, CloseWindow, DockPosition, Event as WorkspaceEvent, Item, ModalView, OpenMode,
31 Panel, Workspace, WorkspaceId, client_side_decorations,
32 persistence::model::MultiWorkspaceState,
33};
34
35actions!(
36 multi_workspace,
37 [
38 /// Toggles the workspace switcher sidebar.
39 ToggleWorkspaceSidebar,
40 /// Closes the workspace sidebar.
41 CloseWorkspaceSidebar,
42 /// Moves focus to or from the workspace sidebar without closing it.
43 FocusWorkspaceSidebar,
44 /// Activates the next project in the sidebar.
45 NextProject,
46 /// Activates the previous project in the sidebar.
47 PreviousProject,
48 /// Activates the next thread in sidebar order.
49 NextThread,
50 /// Activates the previous thread in sidebar order.
51 PreviousThread,
52 /// Expands the thread list for the current project to show more threads.
53 ShowMoreThreads,
54 /// Collapses the thread list for the current project to show fewer threads.
55 ShowFewerThreads,
56 /// Creates a new thread in the current workspace.
57 NewThread,
58 ]
59);
60
61#[derive(Default)]
62pub struct SidebarRenderState {
63 pub open: bool,
64 pub side: SidebarSide,
65}
66
67pub fn sidebar_side_context_menu(
68 id: impl Into<ElementId>,
69 cx: &App,
70) -> ui::RightClickMenu<ContextMenu> {
71 let current_position = AgentSettings::get_global(cx).sidebar_side;
72 right_click_menu(id).menu(move |window, cx| {
73 let fs = <dyn fs::Fs>::global(cx);
74 ContextMenu::build(window, cx, move |mut menu, _, _cx| {
75 let positions: [(SidebarDockPosition, &str); 2] = [
76 (SidebarDockPosition::Left, "Left"),
77 (SidebarDockPosition::Right, "Right"),
78 ];
79 for (position, label) in positions {
80 let fs = fs.clone();
81 menu = menu.toggleable_entry(
82 label,
83 position == current_position,
84 IconPosition::Start,
85 None,
86 move |_window, cx| {
87 settings::update_settings_file(fs.clone(), cx, move |settings, _cx| {
88 settings
89 .agent
90 .get_or_insert_default()
91 .set_sidebar_side(position);
92 });
93 },
94 );
95 }
96 menu
97 })
98 })
99}
100
101pub enum MultiWorkspaceEvent {
102 ActiveWorkspaceChanged,
103 WorkspaceAdded(Entity<Workspace>),
104 WorkspaceRemoved(EntityId),
105 WorktreePathAdded {
106 old_main_paths: PathList,
107 added_path: PathBuf,
108 },
109 WorktreePathRemoved {
110 old_main_paths: PathList,
111 removed_path: PathBuf,
112 },
113}
114
115pub enum SidebarEvent {
116 SerializeNeeded,
117}
118
119pub trait Sidebar: Focusable + Render + EventEmitter<SidebarEvent> + Sized {
120 fn width(&self, cx: &App) -> Pixels;
121 fn set_width(&mut self, width: Option<Pixels>, cx: &mut Context<Self>);
122 fn has_notifications(&self, cx: &App) -> bool;
123 fn side(&self, _cx: &App) -> SidebarSide;
124
125 fn is_threads_list_view_active(&self) -> bool {
126 true
127 }
128 /// Makes focus reset back to the search editor upon toggling the sidebar from outside
129 fn prepare_for_focus(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}
130 /// Opens or cycles the thread switcher popup.
131 fn toggle_thread_switcher(
132 &mut self,
133 _select_last: bool,
134 _window: &mut Window,
135 _cx: &mut Context<Self>,
136 ) {
137 }
138
139 /// Activates the next or previous project.
140 fn cycle_project(&mut self, _forward: bool, _window: &mut Window, _cx: &mut Context<Self>) {}
141
142 /// Activates the next or previous thread in sidebar order.
143 fn cycle_thread(&mut self, _forward: bool, _window: &mut Window, _cx: &mut Context<Self>) {}
144
145 /// Return an opaque JSON blob of sidebar-specific state to persist.
146 fn serialized_state(&self, _cx: &App) -> Option<String> {
147 None
148 }
149
150 /// Restore sidebar state from a previously-serialized blob.
151 fn restore_serialized_state(
152 &mut self,
153 _state: &str,
154 _window: &mut Window,
155 _cx: &mut Context<Self>,
156 ) {
157 }
158}
159
160pub trait SidebarHandle: 'static + Send + Sync {
161 fn width(&self, cx: &App) -> Pixels;
162 fn set_width(&self, width: Option<Pixels>, cx: &mut App);
163 fn focus_handle(&self, cx: &App) -> FocusHandle;
164 fn focus(&self, window: &mut Window, cx: &mut App);
165 fn prepare_for_focus(&self, window: &mut Window, cx: &mut App);
166 fn has_notifications(&self, cx: &App) -> bool;
167 fn to_any(&self) -> AnyView;
168 fn entity_id(&self) -> EntityId;
169 fn toggle_thread_switcher(&self, select_last: bool, window: &mut Window, cx: &mut App);
170 fn cycle_project(&self, forward: bool, window: &mut Window, cx: &mut App);
171 fn cycle_thread(&self, forward: bool, window: &mut Window, cx: &mut App);
172
173 fn is_threads_list_view_active(&self, cx: &App) -> bool;
174
175 fn side(&self, cx: &App) -> SidebarSide;
176 fn serialized_state(&self, cx: &App) -> Option<String>;
177 fn restore_serialized_state(&self, state: &str, window: &mut Window, cx: &mut App);
178}
179
180#[derive(Clone)]
181pub struct DraggedSidebar;
182
183impl Render for DraggedSidebar {
184 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
185 gpui::Empty
186 }
187}
188
189impl<T: Sidebar> SidebarHandle for Entity<T> {
190 fn width(&self, cx: &App) -> Pixels {
191 self.read(cx).width(cx)
192 }
193
194 fn set_width(&self, width: Option<Pixels>, cx: &mut App) {
195 self.update(cx, |this, cx| this.set_width(width, cx))
196 }
197
198 fn focus_handle(&self, cx: &App) -> FocusHandle {
199 self.read(cx).focus_handle(cx)
200 }
201
202 fn focus(&self, window: &mut Window, cx: &mut App) {
203 let handle = self.read(cx).focus_handle(cx);
204 window.focus(&handle, cx);
205 }
206
207 fn prepare_for_focus(&self, window: &mut Window, cx: &mut App) {
208 self.update(cx, |this, cx| this.prepare_for_focus(window, cx));
209 }
210
211 fn has_notifications(&self, cx: &App) -> bool {
212 self.read(cx).has_notifications(cx)
213 }
214
215 fn to_any(&self) -> AnyView {
216 self.clone().into()
217 }
218
219 fn entity_id(&self) -> EntityId {
220 Entity::entity_id(self)
221 }
222
223 fn toggle_thread_switcher(&self, select_last: bool, window: &mut Window, cx: &mut App) {
224 let entity = self.clone();
225 window.defer(cx, move |window, cx| {
226 entity.update(cx, |this, cx| {
227 this.toggle_thread_switcher(select_last, window, cx);
228 });
229 });
230 }
231
232 fn cycle_project(&self, forward: bool, window: &mut Window, cx: &mut App) {
233 let entity = self.clone();
234 window.defer(cx, move |window, cx| {
235 entity.update(cx, |this, cx| {
236 this.cycle_project(forward, window, cx);
237 });
238 });
239 }
240
241 fn cycle_thread(&self, forward: bool, window: &mut Window, cx: &mut App) {
242 let entity = self.clone();
243 window.defer(cx, move |window, cx| {
244 entity.update(cx, |this, cx| {
245 this.cycle_thread(forward, window, cx);
246 });
247 });
248 }
249
250 fn is_threads_list_view_active(&self, cx: &App) -> bool {
251 self.read(cx).is_threads_list_view_active()
252 }
253
254 fn side(&self, cx: &App) -> SidebarSide {
255 self.read(cx).side(cx)
256 }
257
258 fn serialized_state(&self, cx: &App) -> Option<String> {
259 self.read(cx).serialized_state(cx)
260 }
261
262 fn restore_serialized_state(&self, state: &str, window: &mut Window, cx: &mut App) {
263 self.update(cx, |this, cx| {
264 this.restore_serialized_state(state, window, cx)
265 })
266 }
267}
268
269/// Tracks which workspace the user is currently looking at.
270///
271/// `Persistent` workspaces live in the `workspaces` vec and are shown in the
272/// sidebar. `Transient` workspaces exist outside the vec and are discarded
273/// when the user switches away.
274enum ActiveWorkspace {
275 /// A workspace in the `workspaces` set that will persist across switches.
276 Persistent(Entity<Workspace>),
277 /// A workspace not in the `workspaces` set that will be discarded on
278 /// switch or promoted to persistent when the sidebar is opened.
279 Transient(Entity<Workspace>),
280}
281
282impl ActiveWorkspace {
283 fn workspace(&self) -> &Entity<Workspace> {
284 match self {
285 Self::Persistent(ws) | Self::Transient(ws) => ws,
286 }
287 }
288
289 fn transient_workspace(&self) -> Option<&Entity<Workspace>> {
290 match self {
291 Self::Transient(workspace) => Some(workspace),
292 Self::Persistent(_) => None,
293 }
294 }
295}
296
297pub struct MultiWorkspace {
298 window_id: WindowId,
299 workspaces: VecSet<Entity<Workspace>>,
300 active_workspace: ActiveWorkspace,
301 project_group_keys: Vec<ProjectGroupKey>,
302 workspace_group_keys: HashMap<EntityId, ProjectGroupKey>,
303 sidebar: Option<Box<dyn SidebarHandle>>,
304 sidebar_open: bool,
305 sidebar_overlay: Option<AnyView>,
306 pending_removal_tasks: Vec<Task<()>>,
307 _serialize_task: Option<Task<()>>,
308 _subscriptions: Vec<Subscription>,
309 previous_focus_handle: Option<FocusHandle>,
310}
311
312impl EventEmitter<MultiWorkspaceEvent> for MultiWorkspace {}
313
314impl MultiWorkspace {
315 pub fn sidebar_side(&self, cx: &App) -> SidebarSide {
316 self.sidebar
317 .as_ref()
318 .map_or(SidebarSide::Left, |s| s.side(cx))
319 }
320
321 pub fn sidebar_render_state(&self, cx: &App) -> SidebarRenderState {
322 SidebarRenderState {
323 open: self.sidebar_open() && self.multi_workspace_enabled(cx),
324 side: self.sidebar_side(cx),
325 }
326 }
327
328 pub fn new(workspace: Entity<Workspace>, window: &mut Window, cx: &mut Context<Self>) -> Self {
329 let release_subscription = cx.on_release(|this: &mut MultiWorkspace, _cx| {
330 if let Some(task) = this._serialize_task.take() {
331 task.detach();
332 }
333 for task in std::mem::take(&mut this.pending_removal_tasks) {
334 task.detach();
335 }
336 });
337 let quit_subscription = cx.on_app_quit(Self::app_will_quit);
338 let settings_subscription = cx.observe_global_in::<settings::SettingsStore>(window, {
339 let mut previous_disable_ai = DisableAiSettings::get_global(cx).disable_ai;
340 move |this, window, cx| {
341 if DisableAiSettings::get_global(cx).disable_ai != previous_disable_ai {
342 this.collapse_to_single_workspace(window, cx);
343 previous_disable_ai = DisableAiSettings::get_global(cx).disable_ai;
344 }
345 }
346 });
347 Self::subscribe_to_workspace(&workspace, window, cx);
348 let weak_self = cx.weak_entity();
349 workspace.update(cx, |workspace, cx| {
350 workspace.set_multi_workspace(weak_self, cx);
351 });
352 Self {
353 window_id: window.window_handle().window_id(),
354 project_group_keys: Vec::new(),
355 workspace_group_keys: HashMap::default(),
356 workspaces: VecSet::new(),
357 active_workspace: ActiveWorkspace::Transient(workspace),
358 sidebar: None,
359 sidebar_open: false,
360 sidebar_overlay: None,
361 pending_removal_tasks: Vec::new(),
362 _serialize_task: None,
363 _subscriptions: vec![
364 release_subscription,
365 quit_subscription,
366 settings_subscription,
367 ],
368 previous_focus_handle: None,
369 }
370 }
371
372 pub fn register_sidebar<T: Sidebar>(&mut self, sidebar: Entity<T>, cx: &mut Context<Self>) {
373 self._subscriptions
374 .push(cx.observe(&sidebar, |_this, _, cx| {
375 cx.notify();
376 }));
377 self._subscriptions
378 .push(cx.subscribe(&sidebar, |this, _, event, cx| match event {
379 SidebarEvent::SerializeNeeded => {
380 this.serialize(cx);
381 }
382 }));
383 self.sidebar = Some(Box::new(sidebar));
384 }
385
386 pub fn sidebar(&self) -> Option<&dyn SidebarHandle> {
387 self.sidebar.as_deref()
388 }
389
390 pub fn set_sidebar_overlay(&mut self, overlay: Option<AnyView>, cx: &mut Context<Self>) {
391 self.sidebar_overlay = overlay;
392 cx.notify();
393 }
394
395 pub fn sidebar_open(&self) -> bool {
396 self.sidebar_open
397 }
398
399 pub fn sidebar_has_notifications(&self, cx: &App) -> bool {
400 self.sidebar
401 .as_ref()
402 .map_or(false, |s| s.has_notifications(cx))
403 }
404
405 pub fn is_threads_list_view_active(&self, cx: &App) -> bool {
406 self.sidebar
407 .as_ref()
408 .map_or(false, |s| s.is_threads_list_view_active(cx))
409 }
410
411 pub fn multi_workspace_enabled(&self, cx: &App) -> bool {
412 !DisableAiSettings::get_global(cx).disable_ai
413 }
414
415 pub fn toggle_sidebar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
416 if !self.multi_workspace_enabled(cx) {
417 return;
418 }
419
420 if self.sidebar_open() {
421 self.close_sidebar(window, cx);
422 } else {
423 self.previous_focus_handle = window.focused(cx);
424 self.open_sidebar(cx);
425 if let Some(sidebar) = &self.sidebar {
426 sidebar.prepare_for_focus(window, cx);
427 sidebar.focus(window, cx);
428 }
429 }
430 }
431
432 pub fn close_sidebar_action(&mut self, window: &mut Window, cx: &mut Context<Self>) {
433 if !self.multi_workspace_enabled(cx) {
434 return;
435 }
436
437 if self.sidebar_open() {
438 self.close_sidebar(window, cx);
439 }
440 }
441
442 pub fn focus_sidebar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
443 if !self.multi_workspace_enabled(cx) {
444 return;
445 }
446
447 if self.sidebar_open() {
448 let sidebar_is_focused = self
449 .sidebar
450 .as_ref()
451 .is_some_and(|s| s.focus_handle(cx).contains_focused(window, cx));
452
453 if sidebar_is_focused {
454 self.restore_previous_focus(false, window, cx);
455 } else {
456 self.previous_focus_handle = window.focused(cx);
457 if let Some(sidebar) = &self.sidebar {
458 sidebar.prepare_for_focus(window, cx);
459 sidebar.focus(window, cx);
460 }
461 }
462 } else {
463 self.previous_focus_handle = window.focused(cx);
464 self.open_sidebar(cx);
465 if let Some(sidebar) = &self.sidebar {
466 sidebar.prepare_for_focus(window, cx);
467 sidebar.focus(window, cx);
468 }
469 }
470 }
471
472 pub fn open_sidebar(&mut self, cx: &mut Context<Self>) {
473 self.sidebar_open = true;
474 if let ActiveWorkspace::Transient(workspace) = &self.active_workspace {
475 let workspace = workspace.clone();
476 self.promote_transient(workspace.clone(), cx);
477 self.active_workspace = ActiveWorkspace::Persistent(workspace);
478 }
479 let sidebar_focus_handle = self.sidebar.as_ref().map(|s| s.focus_handle(cx));
480 for workspace in self.workspaces.iter() {
481 workspace.update(cx, |workspace, _cx| {
482 workspace.set_sidebar_focus_handle(sidebar_focus_handle.clone());
483 });
484 }
485 self.serialize(cx);
486 cx.notify();
487 }
488
489 pub fn close_sidebar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
490 self.sidebar_open = false;
491 for workspace in self.workspaces.iter() {
492 workspace.update(cx, |workspace, _cx| {
493 workspace.set_sidebar_focus_handle(None);
494 });
495 }
496 let sidebar_has_focus = self
497 .sidebar
498 .as_ref()
499 .is_some_and(|s| s.focus_handle(cx).contains_focused(window, cx));
500 if sidebar_has_focus {
501 self.restore_previous_focus(true, window, cx);
502 } else {
503 self.previous_focus_handle.take();
504 }
505 self.serialize(cx);
506 cx.notify();
507 }
508
509 fn restore_previous_focus(&mut self, clear: bool, window: &mut Window, cx: &mut Context<Self>) {
510 let focus_handle = if clear {
511 self.previous_focus_handle.take()
512 } else {
513 self.previous_focus_handle.clone()
514 };
515
516 if let Some(previous_focus) = focus_handle {
517 previous_focus.focus(window, cx);
518 } else {
519 let pane = self.workspace().read(cx).active_pane().clone();
520 window.focus(&pane.read(cx).focus_handle(cx), cx);
521 }
522 }
523
524 pub fn close_window(&mut self, _: &CloseWindow, window: &mut Window, cx: &mut Context<Self>) {
525 cx.spawn_in(window, async move |this, cx| {
526 let workspaces = this.update(cx, |multi_workspace, _cx| {
527 multi_workspace.workspaces().cloned().collect::<Vec<_>>()
528 })?;
529
530 for workspace in workspaces {
531 let should_continue = workspace
532 .update_in(cx, |workspace, window, cx| {
533 workspace.prepare_to_close(CloseIntent::CloseWindow, window, cx)
534 })?
535 .await?;
536 if !should_continue {
537 return anyhow::Ok(());
538 }
539 }
540
541 cx.update(|window, _cx| {
542 window.remove_window();
543 })?;
544
545 anyhow::Ok(())
546 })
547 .detach_and_log_err(cx);
548 }
549
550 fn subscribe_to_workspace(
551 workspace: &Entity<Workspace>,
552 window: &Window,
553 cx: &mut Context<Self>,
554 ) {
555 let project = workspace.read(cx).project().clone();
556 cx.subscribe_in(&project, window, {
557 let workspace = workspace.downgrade();
558 move |this, _project, event, _window, cx| match event {
559 project::Event::WorktreeAdded(_)
560 | project::Event::WorktreeRemoved(_)
561 | project::Event::WorktreeUpdatedRootRepoCommonDir(_) => {
562 if let Some(workspace) = workspace.upgrade() {
563 this.handle_workspace_key_change(&workspace, cx);
564 }
565 }
566 _ => {}
567 }
568 })
569 .detach();
570
571 cx.subscribe_in(workspace, window, |this, workspace, event, window, cx| {
572 if let WorkspaceEvent::Activate = event {
573 this.activate(workspace.clone(), window, cx);
574 }
575 })
576 .detach();
577 }
578
579 fn handle_workspace_key_change(
580 &mut self,
581 workspace: &Entity<Workspace>,
582 cx: &mut Context<Self>,
583 ) {
584 let workspace_id = workspace.entity_id();
585 let old_key = self.project_group_key_for_workspace(workspace, cx);
586 let new_key = workspace.read(cx).project_group_key(cx);
587
588 if new_key.path_list().paths().is_empty() || old_key == new_key {
589 return;
590 }
591
592 let active_workspace = self.workspace().clone();
593
594 self.set_workspace_group_key(workspace, new_key.clone());
595
596 let changed_root_paths = workspace.read(cx).root_paths(cx);
597 let old_paths = old_key.path_list().paths();
598 let new_paths = new_key.path_list().paths();
599
600 // Remove workspaces that already had the new key and have the same
601 // root paths (true duplicates that this workspace is replacing).
602 //
603 // NOTE: These are dropped without prompting for unsaved changes because
604 // the user explicitly added a folder that makes this workspace
605 // identical to the duplicate — they are intentionally overwriting it.
606 let duplicate_workspaces: Vec<Entity<Workspace>> = self
607 .workspaces
608 .iter()
609 .filter(|ws| {
610 ws.entity_id() != workspace_id
611 && self.project_group_key_for_workspace(ws, cx) == new_key
612 && ws.read(cx).root_paths(cx) == changed_root_paths
613 })
614 .cloned()
615 .collect();
616
617 if duplicate_workspaces.contains(&active_workspace) {
618 // The active workspace is among the duplicates — drop the
619 // incoming workspace instead so the user stays where they are.
620 self.detach_workspace(workspace, cx);
621 self.workspaces.remove(workspace);
622 } else {
623 for ws in &duplicate_workspaces {
624 self.detach_workspace(ws, cx);
625 self.workspaces.remove(ws);
626 }
627 }
628
629 // Propagate folder adds/removes to linked worktree siblings
630 // (different root paths, same old key) so they stay in the group.
631 let group_workspaces: Vec<Entity<Workspace>> = self
632 .workspaces
633 .iter()
634 .filter(|ws| {
635 ws.entity_id() != workspace_id
636 && self.project_group_key_for_workspace(ws, cx) == old_key
637 })
638 .cloned()
639 .collect();
640
641 for workspace in &group_workspaces {
642 // Pre-set this to stop later WorktreeAdded events from triggering
643 self.set_workspace_group_key(&workspace, new_key.clone());
644
645 let project = workspace.read(cx).project().clone();
646
647 for added_path in new_paths.iter().filter(|p| !old_paths.contains(p)) {
648 project
649 .update(cx, |project, cx| {
650 project.find_or_create_worktree(added_path, true, cx)
651 })
652 .detach_and_log_err(cx);
653 }
654
655 for removed_path in old_paths.iter().filter(|p| !new_paths.contains(p)) {
656 project.update(cx, |project, cx| {
657 project.remove_worktree_for_main_worktree_path(removed_path, cx);
658 });
659 }
660 }
661
662 if !self.workspaces.contains(self.active_workspace.workspace())
663 && self.active_workspace.transient_workspace().is_none()
664 {
665 self.active_workspace = ActiveWorkspace::Persistent(workspace.clone());
666 }
667
668 self.remove_stale_project_group_keys(cx);
669
670 let old_main_paths = old_key.path_list().clone();
671 for added_path in new_paths.iter().filter(|p| !old_paths.contains(p)) {
672 cx.emit(MultiWorkspaceEvent::WorktreePathAdded {
673 old_main_paths: old_main_paths.clone(),
674 added_path: added_path.clone(),
675 });
676 }
677 for removed_path in old_paths.iter().filter(|p| !new_paths.contains(p)) {
678 cx.emit(MultiWorkspaceEvent::WorktreePathRemoved {
679 old_main_paths: old_main_paths.clone(),
680 removed_path: removed_path.clone(),
681 });
682 }
683
684 self.serialize(cx);
685 cx.notify();
686 }
687
688 fn add_project_group_key(&mut self, project_group_key: ProjectGroupKey) {
689 if project_group_key.path_list().paths().is_empty() {
690 return;
691 }
692 if self.project_group_keys.contains(&project_group_key) {
693 return;
694 }
695 // Store newest first so the vec is in "most recently added"
696 self.project_group_keys.insert(0, project_group_key);
697 }
698
699 pub(crate) fn set_workspace_group_key(
700 &mut self,
701 workspace: &Entity<Workspace>,
702 project_group_key: ProjectGroupKey,
703 ) {
704 self.workspace_group_keys
705 .insert(workspace.entity_id(), project_group_key.clone());
706 self.add_project_group_key(project_group_key);
707 }
708
709 pub fn project_group_key_for_workspace(
710 &self,
711 workspace: &Entity<Workspace>,
712 cx: &App,
713 ) -> ProjectGroupKey {
714 self.workspace_group_keys
715 .get(&workspace.entity_id())
716 .cloned()
717 .unwrap_or_else(|| workspace.read(cx).project_group_key(cx))
718 }
719
720 fn remove_stale_project_group_keys(&mut self, cx: &App) {
721 let workspace_keys: HashSet<ProjectGroupKey> = self
722 .workspaces
723 .iter()
724 .map(|workspace| self.project_group_key_for_workspace(workspace, cx))
725 .collect();
726 self.project_group_keys
727 .retain(|key| workspace_keys.contains(key));
728 }
729
730 pub fn restore_project_group_keys(&mut self, keys: Vec<ProjectGroupKey>) {
731 let mut restored: Vec<ProjectGroupKey> = Vec::with_capacity(keys.len());
732 for key in keys {
733 if key.path_list().paths().is_empty() {
734 continue;
735 }
736 if !restored.contains(&key) {
737 restored.push(key);
738 }
739 }
740 for existing_key in &self.project_group_keys {
741 if !restored.contains(existing_key) {
742 restored.push(existing_key.clone());
743 }
744 }
745 self.project_group_keys = restored;
746 }
747
748 pub fn project_group_keys(&self) -> impl Iterator<Item = &ProjectGroupKey> {
749 self.project_group_keys.iter()
750 }
751
752 /// Returns the project groups, ordered by most recently added.
753 pub fn project_groups(
754 &self,
755 cx: &App,
756 ) -> impl Iterator<Item = (ProjectGroupKey, Vec<Entity<Workspace>>)> {
757 let mut groups = self
758 .project_group_keys
759 .iter()
760 .map(|key| (key.clone(), Vec::new()))
761 .collect::<Vec<_>>();
762 for workspace in &self.workspaces {
763 let key = self.project_group_key_for_workspace(workspace, cx);
764 if let Some((_, workspaces)) = groups.iter_mut().find(|(k, _)| k == &key) {
765 workspaces.push(workspace.clone());
766 }
767 }
768 groups.into_iter()
769 }
770
771 pub fn workspaces_for_project_group(
772 &self,
773 project_group_key: &ProjectGroupKey,
774 cx: &App,
775 ) -> impl Iterator<Item = &Entity<Workspace>> {
776 self.workspaces.iter().filter(move |workspace| {
777 self.project_group_key_for_workspace(workspace, cx) == *project_group_key
778 })
779 }
780
781 pub fn remove_folder_from_project_group(
782 &mut self,
783 project_group_key: &ProjectGroupKey,
784 path: &Path,
785 cx: &mut Context<Self>,
786 ) {
787 let new_path_list = project_group_key.path_list().without_path(path);
788 if new_path_list.is_empty() {
789 return;
790 }
791
792 let new_key = ProjectGroupKey::new(project_group_key.host(), new_path_list);
793
794 let workspaces: Vec<_> = self
795 .workspaces_for_project_group(project_group_key, cx)
796 .cloned()
797 .collect();
798
799 self.add_project_group_key(new_key);
800
801 for workspace in workspaces {
802 let project = workspace.read(cx).project().clone();
803 project.update(cx, |project, cx| {
804 project.remove_worktree_for_main_worktree_path(path, cx);
805 });
806 }
807
808 self.serialize(cx);
809 cx.notify();
810 }
811
812 pub fn prompt_to_add_folders_to_project_group(
813 &mut self,
814 key: &ProjectGroupKey,
815 window: &mut Window,
816 cx: &mut Context<Self>,
817 ) {
818 let paths = self.workspace().update(cx, |workspace, cx| {
819 workspace.prompt_for_open_path(
820 PathPromptOptions {
821 files: false,
822 directories: true,
823 multiple: true,
824 prompt: None,
825 },
826 DirectoryLister::Project(workspace.project().clone()),
827 window,
828 cx,
829 )
830 });
831
832 let key = key.clone();
833 cx.spawn_in(window, async move |this, cx| {
834 if let Some(new_paths) = paths.await.ok().flatten() {
835 if !new_paths.is_empty() {
836 this.update(cx, |multi_workspace, cx| {
837 multi_workspace.add_folders_to_project_group(&key, new_paths, cx);
838 })?;
839 }
840 }
841 anyhow::Ok(())
842 })
843 .detach_and_log_err(cx);
844 }
845
846 pub fn add_folders_to_project_group(
847 &mut self,
848 project_group_key: &ProjectGroupKey,
849 new_paths: Vec<PathBuf>,
850 cx: &mut Context<Self>,
851 ) {
852 let mut all_paths: Vec<PathBuf> = project_group_key.path_list().paths().to_vec();
853 all_paths.extend(new_paths.iter().cloned());
854 let new_path_list = PathList::new(&all_paths);
855 let new_key = ProjectGroupKey::new(project_group_key.host(), new_path_list);
856
857 let workspaces: Vec<_> = self
858 .workspaces_for_project_group(project_group_key, cx)
859 .cloned()
860 .collect();
861
862 self.add_project_group_key(new_key);
863
864 for workspace in workspaces {
865 let project = workspace.read(cx).project().clone();
866 for path in &new_paths {
867 project
868 .update(cx, |project, cx| {
869 project.find_or_create_worktree(path, true, cx)
870 })
871 .detach_and_log_err(cx);
872 }
873 }
874
875 self.serialize(cx);
876 cx.notify();
877 }
878
879 pub fn remove_project_group(
880 &mut self,
881 key: &ProjectGroupKey,
882 window: &mut Window,
883 cx: &mut Context<Self>,
884 ) -> Task<Result<bool>> {
885 let workspaces: Vec<_> = self
886 .workspaces_for_project_group(key, cx)
887 .cloned()
888 .collect();
889
890 // Compute the neighbor while the key is still in the list.
891 let neighbor_key = {
892 let pos = self.project_group_keys.iter().position(|k| k == key);
893 pos.and_then(|pos| {
894 // Keys are in display order, so pos+1 is below
895 // and pos-1 is above. Try below first.
896 self.project_group_keys.get(pos + 1).or_else(|| {
897 pos.checked_sub(1)
898 .and_then(|i| self.project_group_keys.get(i))
899 })
900 })
901 .cloned()
902 };
903
904 // Now remove the key.
905 self.project_group_keys.retain(|k| k != key);
906
907 self.remove(
908 workspaces,
909 move |this, window, cx| {
910 if let Some(neighbor_key) = neighbor_key {
911 return this.find_or_create_local_workspace(
912 neighbor_key.path_list().clone(),
913 window,
914 cx,
915 );
916 }
917
918 // No other project groups remain — create an empty workspace.
919 let app_state = this.workspace().read(cx).app_state().clone();
920 let project = Project::local(
921 app_state.client.clone(),
922 app_state.node_runtime.clone(),
923 app_state.user_store.clone(),
924 app_state.languages.clone(),
925 app_state.fs.clone(),
926 None,
927 project::LocalProjectFlags::default(),
928 cx,
929 );
930 let new_workspace =
931 cx.new(|cx| Workspace::new(None, project, app_state, window, cx));
932 Task::ready(Ok(new_workspace))
933 },
934 window,
935 cx,
936 )
937 }
938
939 /// Finds an existing workspace whose root paths and host exactly match.
940 pub fn workspace_for_paths(
941 &self,
942 path_list: &PathList,
943 host: Option<&RemoteConnectionOptions>,
944 cx: &App,
945 ) -> Option<Entity<Workspace>> {
946 self.workspaces
947 .iter()
948 .find(|ws| {
949 let key = ws.read(cx).project_group_key(cx);
950 key.host().as_ref() == host
951 && PathList::new(&ws.read(cx).root_paths(cx)) == *path_list
952 })
953 .cloned()
954 }
955
956 /// Finds an existing workspace whose paths match, or creates a new one.
957 ///
958 /// For local projects (`host` is `None`), this delegates to
959 /// [`Self::find_or_create_local_workspace`]. For remote projects, it
960 /// tries an exact path match and, if no existing workspace is found,
961 /// calls `connect_remote` to establish a connection and creates a new
962 /// remote workspace.
963 ///
964 /// The `connect_remote` closure is responsible for any user-facing
965 /// connection UI (e.g. password prompts). It receives the connection
966 /// options and should return a [`Task`] that resolves to the
967 /// [`RemoteClient`] session, or `None` if the connection was
968 /// cancelled.
969 pub fn find_or_create_workspace(
970 &mut self,
971 paths: PathList,
972 host: Option<RemoteConnectionOptions>,
973 provisional_project_group_key: Option<ProjectGroupKey>,
974 connect_remote: impl FnOnce(
975 RemoteConnectionOptions,
976 &mut Window,
977 &mut Context<Self>,
978 ) -> Task<Result<Option<Entity<remote::RemoteClient>>>>
979 + 'static,
980 window: &mut Window,
981 cx: &mut Context<Self>,
982 ) -> Task<Result<Entity<Workspace>>> {
983 if let Some(workspace) = self.workspace_for_paths(&paths, host.as_ref(), cx) {
984 self.activate(workspace.clone(), window, cx);
985 return Task::ready(Ok(workspace));
986 }
987
988 let Some(connection_options) = host else {
989 return self.find_or_create_local_workspace(paths, window, cx);
990 };
991
992 let app_state = self.workspace().read(cx).app_state().clone();
993 let window_handle = window.window_handle().downcast::<MultiWorkspace>();
994 let connect_task = connect_remote(connection_options.clone(), window, cx);
995 let paths_vec = paths.paths().to_vec();
996
997 cx.spawn(async move |_this, cx| {
998 let session = connect_task
999 .await?
1000 .ok_or_else(|| anyhow::anyhow!("Remote connection was cancelled"))?;
1001
1002 let new_project = cx.update(|cx| {
1003 Project::remote(
1004 session,
1005 app_state.client.clone(),
1006 app_state.node_runtime.clone(),
1007 app_state.user_store.clone(),
1008 app_state.languages.clone(),
1009 app_state.fs.clone(),
1010 true,
1011 cx,
1012 )
1013 });
1014
1015 let window_handle =
1016 window_handle.ok_or_else(|| anyhow::anyhow!("Window is not a MultiWorkspace"))?;
1017
1018 open_remote_project_with_existing_connection(
1019 connection_options,
1020 new_project,
1021 paths_vec,
1022 app_state,
1023 window_handle,
1024 provisional_project_group_key,
1025 cx,
1026 )
1027 .await?;
1028
1029 window_handle.update(cx, |multi_workspace, window, cx| {
1030 let workspace = multi_workspace.workspace().clone();
1031 multi_workspace.add(workspace.clone(), window, cx);
1032 workspace
1033 })
1034 })
1035 }
1036
1037 /// Finds an existing workspace in this multi-workspace whose paths match,
1038 /// or creates a new one (deserializing its saved state from the database).
1039 /// Never searches other windows or matches workspaces with a superset of
1040 /// the requested paths.
1041 pub fn find_or_create_local_workspace(
1042 &mut self,
1043 path_list: PathList,
1044 window: &mut Window,
1045 cx: &mut Context<Self>,
1046 ) -> Task<Result<Entity<Workspace>>> {
1047 if let Some(workspace) = self.workspace_for_paths(&path_list, None, cx) {
1048 self.activate(workspace.clone(), window, cx);
1049 return Task::ready(Ok(workspace));
1050 }
1051
1052 if let Some(transient) = self.active_workspace.transient_workspace() {
1053 if transient.read(cx).project_group_key(cx).path_list() == &path_list {
1054 return Task::ready(Ok(transient.clone()));
1055 }
1056 }
1057
1058 let paths = path_list.paths().to_vec();
1059 let app_state = self.workspace().read(cx).app_state().clone();
1060 let requesting_window = window.window_handle().downcast::<MultiWorkspace>();
1061
1062 cx.spawn(async move |_this, cx| {
1063 let result = cx
1064 .update(|cx| {
1065 Workspace::new_local(
1066 paths,
1067 app_state,
1068 requesting_window,
1069 None,
1070 None,
1071 OpenMode::Activate,
1072 cx,
1073 )
1074 })
1075 .await?;
1076 Ok(result.workspace)
1077 })
1078 }
1079
1080 pub fn workspace(&self) -> &Entity<Workspace> {
1081 self.active_workspace.workspace()
1082 }
1083
1084 pub fn workspaces(&self) -> impl Iterator<Item = &Entity<Workspace>> {
1085 self.workspaces
1086 .iter()
1087 .chain(self.active_workspace.transient_workspace())
1088 }
1089
1090 /// Adds a workspace to this window as persistent without changing which
1091 /// workspace is active. Unlike `activate()`, this always inserts into the
1092 /// persistent list regardless of sidebar state — it's used for system-
1093 /// initiated additions like deserialization and worktree discovery.
1094 pub fn add(&mut self, workspace: Entity<Workspace>, window: &Window, cx: &mut Context<Self>) {
1095 self.insert_workspace(workspace, window, cx);
1096 }
1097
1098 /// Ensures the workspace is in the multiworkspace and makes it the active one.
1099 pub fn activate(
1100 &mut self,
1101 workspace: Entity<Workspace>,
1102 window: &mut Window,
1103 cx: &mut Context<Self>,
1104 ) {
1105 if self.workspace() == &workspace {
1106 self.focus_active_workspace(window, cx);
1107 return;
1108 }
1109
1110 let is_persistent = self.workspaces.contains(&workspace);
1111
1112 let old_transient = self.active_workspace.transient_workspace().cloned();
1113
1114 if is_persistent || self.sidebar_open {
1115 if !is_persistent {
1116 self.insert_workspace(workspace.clone(), &*window, cx);
1117 }
1118 self.active_workspace = ActiveWorkspace::Persistent(workspace);
1119 if let Some(old) = old_transient {
1120 if self.sidebar_open {
1121 self.promote_transient(old, cx);
1122 } else {
1123 self.detach_workspace(&old, cx);
1124 }
1125 }
1126 } else {
1127 Self::subscribe_to_workspace(&workspace, window, cx);
1128 let weak_self = cx.weak_entity();
1129 workspace.update(cx, |workspace, cx| {
1130 workspace.set_multi_workspace(weak_self, cx);
1131 });
1132 self.active_workspace = ActiveWorkspace::Transient(workspace);
1133 if let Some(old) = old_transient {
1134 self.detach_workspace(&old, cx);
1135 }
1136 }
1137
1138 cx.emit(MultiWorkspaceEvent::ActiveWorkspaceChanged);
1139 self.serialize(cx);
1140 self.focus_active_workspace(window, cx);
1141 cx.notify();
1142 }
1143
1144 /// Promotes the currently active workspace to persistent if it is
1145 /// transient, so it is retained across workspace switches even when
1146 /// the sidebar is closed. No-op if the workspace is already persistent.
1147 pub fn retain_active_workspace(&mut self, cx: &mut Context<Self>) {
1148 if let ActiveWorkspace::Transient(workspace) = &self.active_workspace {
1149 let workspace = workspace.clone();
1150 self.promote_transient(workspace.clone(), cx);
1151 self.active_workspace = ActiveWorkspace::Persistent(workspace);
1152 self.serialize(cx);
1153 cx.notify();
1154 }
1155 }
1156
1157 fn promote_transient(&mut self, workspace: Entity<Workspace>, cx: &mut Context<Self>) {
1158 let project_group_key = self.project_group_key_for_workspace(&workspace, cx);
1159 self.set_workspace_group_key(&workspace, project_group_key);
1160 self.workspaces.insert(workspace.clone());
1161 cx.emit(MultiWorkspaceEvent::WorkspaceAdded(workspace));
1162 }
1163
1164 /// Collapses to a single transient workspace, discarding all persistent
1165 /// workspaces. Used when multi-workspace is disabled (e.g. disable_ai).
1166 fn collapse_to_single_workspace(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1167 if self.sidebar_open {
1168 self.close_sidebar(window, cx);
1169 }
1170 let active = self.workspace().clone();
1171 for workspace in std::mem::take(&mut self.workspaces) {
1172 if workspace != active {
1173 self.detach_workspace(&workspace, cx);
1174 }
1175 }
1176 self.project_group_keys.clear();
1177 self.workspace_group_keys.clear();
1178 self.active_workspace = ActiveWorkspace::Transient(active);
1179 cx.notify();
1180 }
1181
1182 fn insert_workspace(
1183 &mut self,
1184 workspace: Entity<Workspace>,
1185 window: &Window,
1186 cx: &mut Context<Self>,
1187 ) {
1188 if self.workspaces.contains(&workspace) {
1189 return;
1190 }
1191 let project_group_key = self.project_group_key_for_workspace(&workspace, cx);
1192 Self::subscribe_to_workspace(&workspace, window, cx);
1193 self.sync_sidebar_to_workspace(&workspace, cx);
1194 let weak_self = cx.weak_entity();
1195 workspace.update(cx, |workspace, cx| {
1196 workspace.set_multi_workspace(weak_self, cx);
1197 });
1198 self.set_workspace_group_key(&workspace, project_group_key);
1199 self.workspaces.insert(workspace.clone());
1200 cx.emit(MultiWorkspaceEvent::WorkspaceAdded(workspace));
1201 cx.notify();
1202 }
1203
1204 /// Detaches a workspace: clears session state, DB binding, cached
1205 /// group key, and emits `WorkspaceRemoved`. The DB row is preserved
1206 /// so the workspace still appears in the recent-projects list.
1207 fn detach_workspace(&mut self, workspace: &Entity<Workspace>, cx: &mut Context<Self>) {
1208 self.workspace_group_keys.remove(&workspace.entity_id());
1209 cx.emit(MultiWorkspaceEvent::WorkspaceRemoved(workspace.entity_id()));
1210 workspace.update(cx, |workspace, _cx| {
1211 workspace.session_id.take();
1212 workspace._schedule_serialize_workspace.take();
1213 workspace._serialize_workspace_task.take();
1214 });
1215
1216 if let Some(workspace_id) = workspace.read(cx).database_id() {
1217 let db = crate::persistence::WorkspaceDb::global(cx);
1218 self.pending_removal_tasks.retain(|task| !task.is_ready());
1219 self.pending_removal_tasks
1220 .push(cx.background_spawn(async move {
1221 db.set_session_binding(workspace_id, None, None)
1222 .await
1223 .log_err();
1224 }));
1225 }
1226 }
1227
1228 fn sync_sidebar_to_workspace(&self, workspace: &Entity<Workspace>, cx: &mut Context<Self>) {
1229 if self.sidebar_open() {
1230 let sidebar_focus_handle = self.sidebar.as_ref().map(|s| s.focus_handle(cx));
1231 workspace.update(cx, |workspace, _| {
1232 workspace.set_sidebar_focus_handle(sidebar_focus_handle);
1233 });
1234 }
1235 }
1236
1237 pub(crate) fn serialize(&mut self, cx: &mut Context<Self>) {
1238 self._serialize_task = Some(cx.spawn(async move |this, cx| {
1239 let Some((window_id, state)) = this
1240 .read_with(cx, |this, cx| {
1241 let state = MultiWorkspaceState {
1242 active_workspace_id: this.workspace().read(cx).database_id(),
1243 project_group_keys: this
1244 .project_group_keys()
1245 .cloned()
1246 .map(Into::into)
1247 .collect::<Vec<_>>(),
1248 sidebar_open: this.sidebar_open,
1249 sidebar_state: this.sidebar.as_ref().and_then(|s| s.serialized_state(cx)),
1250 };
1251 (this.window_id, state)
1252 })
1253 .ok()
1254 else {
1255 return;
1256 };
1257 let kvp = cx.update(|cx| db::kvp::KeyValueStore::global(cx));
1258 crate::persistence::write_multi_workspace_state(&kvp, window_id, state).await;
1259 }));
1260 }
1261
1262 /// Returns the in-flight serialization task (if any) so the caller can
1263 /// await it. Used by the quit handler to ensure pending DB writes
1264 /// complete before the process exits.
1265 pub fn flush_serialization(&mut self) -> Task<()> {
1266 self._serialize_task.take().unwrap_or(Task::ready(()))
1267 }
1268
1269 fn app_will_quit(&mut self, _cx: &mut Context<Self>) -> impl Future<Output = ()> + use<> {
1270 let mut tasks: Vec<Task<()>> = Vec::new();
1271 if let Some(task) = self._serialize_task.take() {
1272 tasks.push(task);
1273 }
1274 tasks.extend(std::mem::take(&mut self.pending_removal_tasks));
1275
1276 async move {
1277 futures::future::join_all(tasks).await;
1278 }
1279 }
1280
1281 pub fn focus_active_workspace(&self, window: &mut Window, cx: &mut App) {
1282 // If a dock panel is zoomed, focus it instead of the center pane.
1283 // Otherwise, focusing the center pane triggers dismiss_zoomed_items_to_reveal
1284 // which closes the zoomed dock.
1285 let focus_handle = {
1286 let workspace = self.workspace().read(cx);
1287 let mut target = None;
1288 for dock in workspace.all_docks() {
1289 let dock = dock.read(cx);
1290 if dock.is_open() {
1291 if let Some(panel) = dock.active_panel() {
1292 if panel.is_zoomed(window, cx) {
1293 target = Some(panel.panel_focus_handle(cx));
1294 break;
1295 }
1296 }
1297 }
1298 }
1299 target.unwrap_or_else(|| {
1300 let pane = workspace.active_pane().clone();
1301 pane.read(cx).focus_handle(cx)
1302 })
1303 };
1304 window.focus(&focus_handle, cx);
1305 }
1306
1307 pub fn panel<T: Panel>(&self, cx: &App) -> Option<Entity<T>> {
1308 self.workspace().read(cx).panel::<T>(cx)
1309 }
1310
1311 pub fn active_modal<V: ManagedView + 'static>(&self, cx: &App) -> Option<Entity<V>> {
1312 self.workspace().read(cx).active_modal::<V>(cx)
1313 }
1314
1315 pub fn add_panel<T: Panel>(
1316 &mut self,
1317 panel: Entity<T>,
1318 window: &mut Window,
1319 cx: &mut Context<Self>,
1320 ) {
1321 self.workspace().update(cx, |workspace, cx| {
1322 workspace.add_panel(panel, window, cx);
1323 });
1324 }
1325
1326 pub fn focus_panel<T: Panel>(
1327 &mut self,
1328 window: &mut Window,
1329 cx: &mut Context<Self>,
1330 ) -> Option<Entity<T>> {
1331 self.workspace()
1332 .update(cx, |workspace, cx| workspace.focus_panel::<T>(window, cx))
1333 }
1334
1335 // used in a test
1336 pub fn toggle_modal<V: ModalView, B>(
1337 &mut self,
1338 window: &mut Window,
1339 cx: &mut Context<Self>,
1340 build: B,
1341 ) where
1342 B: FnOnce(&mut Window, &mut gpui::Context<V>) -> V,
1343 {
1344 self.workspace().update(cx, |workspace, cx| {
1345 workspace.toggle_modal(window, cx, build);
1346 });
1347 }
1348
1349 pub fn toggle_dock(
1350 &mut self,
1351 dock_side: DockPosition,
1352 window: &mut Window,
1353 cx: &mut Context<Self>,
1354 ) {
1355 self.workspace().update(cx, |workspace, cx| {
1356 workspace.toggle_dock(dock_side, window, cx);
1357 });
1358 }
1359
1360 pub fn active_item_as<I: 'static>(&self, cx: &App) -> Option<Entity<I>> {
1361 self.workspace().read(cx).active_item_as::<I>(cx)
1362 }
1363
1364 pub fn items_of_type<'a, T: Item>(
1365 &'a self,
1366 cx: &'a App,
1367 ) -> impl 'a + Iterator<Item = Entity<T>> {
1368 self.workspace().read(cx).items_of_type::<T>(cx)
1369 }
1370
1371 pub fn database_id(&self, cx: &App) -> Option<WorkspaceId> {
1372 self.workspace().read(cx).database_id()
1373 }
1374
1375 pub fn take_pending_removal_tasks(&mut self) -> Vec<Task<()>> {
1376 let tasks: Vec<Task<()>> = std::mem::take(&mut self.pending_removal_tasks)
1377 .into_iter()
1378 .filter(|task| !task.is_ready())
1379 .collect();
1380 tasks
1381 }
1382
1383 #[cfg(any(test, feature = "test-support"))]
1384 pub fn assert_project_group_key_integrity(&self, cx: &App) -> anyhow::Result<()> {
1385 let stored_keys: HashSet<&ProjectGroupKey> = self.project_group_keys().collect();
1386
1387 let workspace_group_keys: HashSet<&ProjectGroupKey> =
1388 self.workspace_group_keys.values().collect();
1389 let extra_keys = &workspace_group_keys - &stored_keys;
1390 anyhow::ensure!(
1391 extra_keys.is_empty(),
1392 "workspace_group_keys values not in project_group_keys: {:?}",
1393 extra_keys,
1394 );
1395
1396 let cached_ids: HashSet<EntityId> = self.workspace_group_keys.keys().copied().collect();
1397 let workspace_ids: HashSet<EntityId> =
1398 self.workspaces.iter().map(|ws| ws.entity_id()).collect();
1399 anyhow::ensure!(
1400 cached_ids == workspace_ids,
1401 "workspace_group_keys entity IDs don't match workspaces.\n\
1402 only in cache: {:?}\n\
1403 only in workspaces: {:?}",
1404 &cached_ids - &workspace_ids,
1405 &workspace_ids - &cached_ids,
1406 );
1407
1408 for workspace in self.workspaces() {
1409 let live_key = workspace.read(cx).project_group_key(cx);
1410 let cached_key = &self.workspace_group_keys[&workspace.entity_id()];
1411 anyhow::ensure!(
1412 *cached_key == live_key,
1413 "workspace {:?} has live key {:?} but cached key {:?}",
1414 workspace.entity_id(),
1415 live_key,
1416 cached_key,
1417 );
1418 }
1419
1420 if let ActiveWorkspace::Persistent(ws) = &self.active_workspace {
1421 anyhow::ensure!(
1422 self.workspaces.contains(ws),
1423 "persistent active workspace {:?} is not in the workspaces set",
1424 ws.entity_id(),
1425 );
1426 }
1427
1428 Ok(())
1429 }
1430
1431 #[cfg(any(test, feature = "test-support"))]
1432 pub fn set_random_database_id(&mut self, cx: &mut Context<Self>) {
1433 self.workspace().update(cx, |workspace, _cx| {
1434 workspace.set_random_database_id();
1435 });
1436 }
1437
1438 #[cfg(any(test, feature = "test-support"))]
1439 pub fn test_new(project: Entity<Project>, window: &mut Window, cx: &mut Context<Self>) -> Self {
1440 let workspace = cx.new(|cx| Workspace::test_new(project, window, cx));
1441 Self::new(workspace, window, cx)
1442 }
1443
1444 #[cfg(any(test, feature = "test-support"))]
1445 pub fn test_add_workspace(
1446 &mut self,
1447 project: Entity<Project>,
1448 window: &mut Window,
1449 cx: &mut Context<Self>,
1450 ) -> Entity<Workspace> {
1451 let workspace = cx.new(|cx| Workspace::test_new(project, window, cx));
1452 self.activate(workspace.clone(), window, cx);
1453 workspace
1454 }
1455
1456 #[cfg(any(test, feature = "test-support"))]
1457 pub fn create_test_workspace(
1458 &mut self,
1459 window: &mut Window,
1460 cx: &mut Context<Self>,
1461 ) -> Task<()> {
1462 let app_state = self.workspace().read(cx).app_state().clone();
1463 let project = Project::local(
1464 app_state.client.clone(),
1465 app_state.node_runtime.clone(),
1466 app_state.user_store.clone(),
1467 app_state.languages.clone(),
1468 app_state.fs.clone(),
1469 None,
1470 project::LocalProjectFlags::default(),
1471 cx,
1472 );
1473 let new_workspace = cx.new(|cx| Workspace::new(None, project, app_state, window, cx));
1474 self.activate(new_workspace.clone(), window, cx);
1475
1476 let weak_workspace = new_workspace.downgrade();
1477 let db = crate::persistence::WorkspaceDb::global(cx);
1478 cx.spawn_in(window, async move |this, cx| {
1479 let workspace_id = db.next_id().await.unwrap();
1480 let workspace = weak_workspace.upgrade().unwrap();
1481 let task: Task<()> = this
1482 .update_in(cx, |this, window, cx| {
1483 let session_id = workspace.read(cx).session_id();
1484 let window_id = window.window_handle().window_id().as_u64();
1485 workspace.update(cx, |workspace, _cx| {
1486 workspace.set_database_id(workspace_id);
1487 });
1488 this.serialize(cx);
1489 let db = db.clone();
1490 cx.background_spawn(async move {
1491 db.set_session_binding(workspace_id, session_id, Some(window_id))
1492 .await
1493 .log_err();
1494 })
1495 })
1496 .unwrap();
1497 task.await
1498 })
1499 }
1500
1501 /// Removes one or more workspaces from this multi-workspace.
1502 ///
1503 /// If the active workspace is among those being removed,
1504 /// `fallback_workspace` is called **synchronously before the removal
1505 /// begins** to produce a `Task` that resolves to the workspace that
1506 /// should become active. The fallback must not be one of the
1507 /// workspaces being removed.
1508 ///
1509 /// Returns `true` if any workspaces were actually removed.
1510 pub fn remove(
1511 &mut self,
1512 workspaces: impl IntoIterator<Item = Entity<Workspace>>,
1513 fallback_workspace: impl FnOnce(
1514 &mut Self,
1515 &mut Window,
1516 &mut Context<Self>,
1517 ) -> Task<Result<Entity<Workspace>>>,
1518 window: &mut Window,
1519 cx: &mut Context<Self>,
1520 ) -> Task<Result<bool>> {
1521 let workspaces: Vec<_> = workspaces.into_iter().collect();
1522
1523 if workspaces.is_empty() {
1524 return Task::ready(Ok(false));
1525 }
1526
1527 let removing_active = workspaces.iter().any(|ws| ws == self.workspace());
1528 let original_active = self.workspace().clone();
1529
1530 let fallback_task = removing_active.then(|| fallback_workspace(self, window, cx));
1531
1532 cx.spawn_in(window, async move |this, cx| {
1533 // Prompt each workspace for unsaved changes. If any workspace
1534 // has dirty buffers, save_all_internal will emit Activate to
1535 // bring it into view before showing the save dialog.
1536 for workspace in &workspaces {
1537 let should_continue = workspace
1538 .update_in(cx, |workspace, window, cx| {
1539 workspace.save_all_internal(crate::SaveIntent::Close, window, cx)
1540 })?
1541 .await?;
1542
1543 if !should_continue {
1544 return Ok(false);
1545 }
1546 }
1547
1548 // If we're removing the active workspace, await the
1549 // fallback and switch to it before tearing anything down.
1550 // Otherwise restore the original active workspace in case
1551 // prompting switched away from it.
1552 if let Some(fallback_task) = fallback_task {
1553 let new_active = fallback_task.await?;
1554
1555 this.update_in(cx, |this, window, cx| {
1556 assert!(
1557 !workspaces.contains(&new_active),
1558 "fallback workspace must not be one of the workspaces being removed"
1559 );
1560 this.activate(new_active, window, cx);
1561 })?;
1562 } else {
1563 this.update_in(cx, |this, window, cx| {
1564 if *this.workspace() != original_active {
1565 this.activate(original_active, window, cx);
1566 }
1567 })?;
1568 }
1569
1570 // Actually remove the workspaces.
1571 this.update_in(cx, |this, _, cx| {
1572 let mut removed_workspaces = Vec::new();
1573 for ws in &workspaces {
1574 if this.workspaces.remove(ws) {
1575 removed_workspaces.push(ws.clone());
1576 }
1577 }
1578
1579 for workspace in &removed_workspaces {
1580 this.detach_workspace(workspace, cx);
1581 }
1582
1583 let removed_any = !removed_workspaces.is_empty();
1584
1585 if removed_any {
1586 if !this.workspaces.contains(this.active_workspace.workspace()) {
1587 if let Some(fallback) = this.workspaces.iter().next() {
1588 this.active_workspace = ActiveWorkspace::Persistent(fallback.clone());
1589 }
1590 }
1591
1592 this.serialize(cx);
1593 cx.notify();
1594 }
1595
1596 Ok(removed_any)
1597 })?
1598 })
1599 }
1600
1601 pub fn open_project(
1602 &mut self,
1603 paths: Vec<PathBuf>,
1604 open_mode: OpenMode,
1605 window: &mut Window,
1606 cx: &mut Context<Self>,
1607 ) -> Task<Result<Entity<Workspace>>> {
1608 if self.multi_workspace_enabled(cx) {
1609 self.find_or_create_local_workspace(PathList::new(&paths), window, cx)
1610 } else {
1611 let workspace = self.workspace().clone();
1612 cx.spawn_in(window, async move |_this, cx| {
1613 let should_continue = workspace
1614 .update_in(cx, |workspace, window, cx| {
1615 workspace.prepare_to_close(crate::CloseIntent::ReplaceWindow, window, cx)
1616 })?
1617 .await?;
1618 if should_continue {
1619 workspace
1620 .update_in(cx, |workspace, window, cx| {
1621 workspace.open_workspace_for_paths(open_mode, paths, window, cx)
1622 })?
1623 .await
1624 } else {
1625 Ok(workspace)
1626 }
1627 })
1628 }
1629 }
1630}
1631
1632impl Render for MultiWorkspace {
1633 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1634 let multi_workspace_enabled = self.multi_workspace_enabled(cx);
1635 let sidebar_side = self.sidebar_side(cx);
1636 let sidebar_on_right = sidebar_side == SidebarSide::Right;
1637
1638 let sidebar: Option<AnyElement> = if multi_workspace_enabled && self.sidebar_open() {
1639 self.sidebar.as_ref().map(|sidebar_handle| {
1640 let weak = cx.weak_entity();
1641
1642 let sidebar_width = sidebar_handle.width(cx);
1643 let resize_handle = deferred(
1644 div()
1645 .id("sidebar-resize-handle")
1646 .absolute()
1647 .when(!sidebar_on_right, |el| {
1648 el.right(-SIDEBAR_RESIZE_HANDLE_SIZE / 2.)
1649 })
1650 .when(sidebar_on_right, |el| {
1651 el.left(-SIDEBAR_RESIZE_HANDLE_SIZE / 2.)
1652 })
1653 .top(px(0.))
1654 .h_full()
1655 .w(SIDEBAR_RESIZE_HANDLE_SIZE)
1656 .cursor_col_resize()
1657 .on_drag(DraggedSidebar, |dragged, _, _, cx| {
1658 cx.stop_propagation();
1659 cx.new(|_| dragged.clone())
1660 })
1661 .on_mouse_down(MouseButton::Left, |_, _, cx| {
1662 cx.stop_propagation();
1663 })
1664 .on_mouse_up(MouseButton::Left, move |event, _, cx| {
1665 if event.click_count == 2 {
1666 weak.update(cx, |this, cx| {
1667 if let Some(sidebar) = this.sidebar.as_mut() {
1668 sidebar.set_width(None, cx);
1669 }
1670 this.serialize(cx);
1671 })
1672 .ok();
1673 cx.stop_propagation();
1674 } else {
1675 weak.update(cx, |this, cx| {
1676 this.serialize(cx);
1677 })
1678 .ok();
1679 }
1680 })
1681 .occlude(),
1682 );
1683
1684 div()
1685 .id("sidebar-container")
1686 .relative()
1687 .h_full()
1688 .w(sidebar_width)
1689 .flex_shrink_0()
1690 .child(sidebar_handle.to_any())
1691 .child(resize_handle)
1692 .into_any_element()
1693 })
1694 } else {
1695 None
1696 };
1697
1698 let (left_sidebar, right_sidebar) = if sidebar_on_right {
1699 (None, sidebar)
1700 } else {
1701 (sidebar, None)
1702 };
1703
1704 let ui_font = theme_settings::setup_ui_font(window, cx);
1705 let text_color = cx.theme().colors().text;
1706
1707 let workspace = self.workspace().clone();
1708 let workspace_key_context = workspace.update(cx, |workspace, cx| workspace.key_context(cx));
1709 let root = workspace.update(cx, |workspace, cx| workspace.actions(h_flex(), window, cx));
1710
1711 client_side_decorations(
1712 root.key_context(workspace_key_context)
1713 .relative()
1714 .size_full()
1715 .font(ui_font)
1716 .text_color(text_color)
1717 .on_action(cx.listener(Self::close_window))
1718 .when(self.multi_workspace_enabled(cx), |this| {
1719 this.on_action(cx.listener(
1720 |this: &mut Self, _: &ToggleWorkspaceSidebar, window, cx| {
1721 this.toggle_sidebar(window, cx);
1722 },
1723 ))
1724 .on_action(cx.listener(
1725 |this: &mut Self, _: &CloseWorkspaceSidebar, window, cx| {
1726 this.close_sidebar_action(window, cx);
1727 },
1728 ))
1729 .on_action(cx.listener(
1730 |this: &mut Self, _: &FocusWorkspaceSidebar, window, cx| {
1731 this.focus_sidebar(window, cx);
1732 },
1733 ))
1734 .on_action(cx.listener(
1735 |this: &mut Self, action: &ToggleThreadSwitcher, window, cx| {
1736 if let Some(sidebar) = &this.sidebar {
1737 sidebar.toggle_thread_switcher(action.select_last, window, cx);
1738 }
1739 },
1740 ))
1741 .on_action(cx.listener(|this: &mut Self, _: &NextProject, window, cx| {
1742 if let Some(sidebar) = &this.sidebar {
1743 sidebar.cycle_project(true, window, cx);
1744 }
1745 }))
1746 .on_action(
1747 cx.listener(|this: &mut Self, _: &PreviousProject, window, cx| {
1748 if let Some(sidebar) = &this.sidebar {
1749 sidebar.cycle_project(false, window, cx);
1750 }
1751 }),
1752 )
1753 .on_action(cx.listener(|this: &mut Self, _: &NextThread, window, cx| {
1754 if let Some(sidebar) = &this.sidebar {
1755 sidebar.cycle_thread(true, window, cx);
1756 }
1757 }))
1758 .on_action(cx.listener(
1759 |this: &mut Self, _: &PreviousThread, window, cx| {
1760 if let Some(sidebar) = &this.sidebar {
1761 sidebar.cycle_thread(false, window, cx);
1762 }
1763 },
1764 ))
1765 })
1766 .when(
1767 self.sidebar_open() && self.multi_workspace_enabled(cx),
1768 |this| {
1769 this.on_drag_move(cx.listener(
1770 move |this: &mut Self,
1771 e: &DragMoveEvent<DraggedSidebar>,
1772 window,
1773 cx| {
1774 if let Some(sidebar) = &this.sidebar {
1775 let new_width = if sidebar_on_right {
1776 window.bounds().size.width - e.event.position.x
1777 } else {
1778 e.event.position.x
1779 };
1780 sidebar.set_width(Some(new_width), cx);
1781 }
1782 },
1783 ))
1784 },
1785 )
1786 .children(left_sidebar)
1787 .child(
1788 div()
1789 .flex()
1790 .flex_1()
1791 .size_full()
1792 .overflow_hidden()
1793 .child(self.workspace().clone()),
1794 )
1795 .children(right_sidebar)
1796 .child(self.workspace().read(cx).modal_layer.clone())
1797 .children(self.sidebar_overlay.as_ref().map(|view| {
1798 deferred(div().absolute().size_full().inset_0().occlude().child(
1799 v_flex().h(px(0.0)).top_20().items_center().child(
1800 h_flex().occlude().child(view.clone()).on_mouse_down(
1801 MouseButton::Left,
1802 |_, _, cx| {
1803 cx.stop_propagation();
1804 },
1805 ),
1806 ),
1807 ))
1808 .with_priority(2)
1809 })),
1810 window,
1811 cx,
1812 Tiling {
1813 left: !sidebar_on_right && multi_workspace_enabled && self.sidebar_open(),
1814 right: sidebar_on_right && multi_workspace_enabled && self.sidebar_open(),
1815 ..Tiling::default()
1816 },
1817 )
1818 }
1819}