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