1mod dragged_item_receiver;
2
3use super::{ItemHandle, SplitDirection};
4use crate::{
5 dock::{icon_for_dock_anchor, AnchorDockBottom, AnchorDockRight, ExpandDock, HideDock},
6 item::WeakItemHandle,
7 toolbar::Toolbar,
8 Item, NewFile, NewSearch, NewTerminal, Workspace,
9};
10use anyhow::Result;
11use collections::{HashMap, HashSet, VecDeque};
12use context_menu::{ContextMenu, ContextMenuItem};
13use drag_and_drop::Draggable;
14pub use dragged_item_receiver::{dragged_item_receiver, handle_dropped_item};
15use futures::StreamExt;
16use gpui::{
17 actions,
18 elements::*,
19 geometry::{
20 rect::RectF,
21 vector::{vec2f, Vector2F},
22 },
23 impl_actions, impl_internal_actions,
24 platform::{CursorStyle, NavigationDirection},
25 Action, AnyViewHandle, AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, EventContext,
26 ModelHandle, MouseButton, MutableAppContext, PromptLevel, Quad, RenderContext, Task, View,
27 ViewContext, ViewHandle, WeakViewHandle,
28};
29use project::{Project, ProjectEntryId, ProjectPath};
30use serde::Deserialize;
31use settings::{Autosave, DockAnchor, Settings};
32use std::{any::Any, cell::RefCell, cmp, mem, path::Path, rc::Rc};
33use theme::Theme;
34use util::ResultExt;
35
36#[derive(Clone, Deserialize, PartialEq)]
37pub struct ActivateItem(pub usize);
38
39actions!(
40 pane,
41 [
42 ActivatePrevItem,
43 ActivateNextItem,
44 ActivateLastItem,
45 CloseActiveItem,
46 CloseInactiveItems,
47 CloseCleanItems,
48 CloseAllItems,
49 ReopenClosedItem,
50 SplitLeft,
51 SplitUp,
52 SplitRight,
53 SplitDown,
54 ]
55);
56
57#[derive(Clone, PartialEq)]
58pub struct CloseItem {
59 pub item_id: usize,
60 pub pane: WeakViewHandle<Pane>,
61}
62
63#[derive(Clone, PartialEq)]
64pub struct MoveItem {
65 pub item_id: usize,
66 pub from: WeakViewHandle<Pane>,
67 pub to: WeakViewHandle<Pane>,
68 pub destination_index: usize,
69}
70
71#[derive(Clone, Deserialize, PartialEq)]
72pub struct GoBack {
73 #[serde(skip_deserializing)]
74 pub pane: Option<WeakViewHandle<Pane>>,
75}
76
77#[derive(Clone, Deserialize, PartialEq)]
78pub struct GoForward {
79 #[serde(skip_deserializing)]
80 pub pane: Option<WeakViewHandle<Pane>>,
81}
82
83#[derive(Clone, PartialEq)]
84pub struct DeploySplitMenu {
85 position: Vector2F,
86}
87
88#[derive(Clone, PartialEq)]
89pub struct DeployDockMenu {
90 position: Vector2F,
91}
92
93#[derive(Clone, PartialEq)]
94pub struct DeployNewMenu {
95 position: Vector2F,
96}
97
98impl_actions!(pane, [GoBack, GoForward, ActivateItem]);
99impl_internal_actions!(
100 pane,
101 [
102 CloseItem,
103 DeploySplitMenu,
104 DeployNewMenu,
105 DeployDockMenu,
106 MoveItem
107 ]
108);
109
110const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
111
112pub fn init(cx: &mut MutableAppContext) {
113 cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
114 pane.activate_item(action.0, true, true, cx);
115 });
116 cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| {
117 pane.activate_item(pane.items.len() - 1, true, true, cx);
118 });
119 cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
120 pane.activate_prev_item(true, cx);
121 });
122 cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| {
123 pane.activate_next_item(true, cx);
124 });
125 cx.add_async_action(Pane::close_active_item);
126 cx.add_async_action(Pane::close_inactive_items);
127 cx.add_async_action(Pane::close_clean_items);
128 cx.add_async_action(Pane::close_all_items);
129 cx.add_async_action(|workspace: &mut Workspace, action: &CloseItem, cx| {
130 let pane = action.pane.upgrade(cx)?;
131 let task = Pane::close_item(workspace, pane, action.item_id, cx);
132 Some(cx.foreground().spawn(async move {
133 task.await?;
134 Ok(())
135 }))
136 });
137 cx.add_action(
138 |workspace,
139 MoveItem {
140 from,
141 to,
142 item_id,
143 destination_index,
144 },
145 cx| {
146 // Get item handle to move
147 let from = if let Some(from) = from.upgrade(cx) {
148 from
149 } else {
150 return;
151 };
152
153 // Add item to new pane at given index
154 let to = if let Some(to) = to.upgrade(cx) {
155 to
156 } else {
157 return;
158 };
159
160 Pane::move_item(workspace, from, to, *item_id, *destination_index, cx)
161 },
162 );
163 cx.add_action(|pane: &mut Pane, _: &SplitLeft, cx| pane.split(SplitDirection::Left, cx));
164 cx.add_action(|pane: &mut Pane, _: &SplitUp, cx| pane.split(SplitDirection::Up, cx));
165 cx.add_action(|pane: &mut Pane, _: &SplitRight, cx| pane.split(SplitDirection::Right, cx));
166 cx.add_action(|pane: &mut Pane, _: &SplitDown, cx| pane.split(SplitDirection::Down, cx));
167 cx.add_action(Pane::deploy_split_menu);
168 cx.add_action(Pane::deploy_new_menu);
169 cx.add_action(Pane::deploy_dock_menu);
170 cx.add_action(|workspace: &mut Workspace, _: &ReopenClosedItem, cx| {
171 Pane::reopen_closed_item(workspace, cx).detach();
172 });
173 cx.add_action(|workspace: &mut Workspace, action: &GoBack, cx| {
174 Pane::go_back(
175 workspace,
176 action
177 .pane
178 .as_ref()
179 .and_then(|weak_handle| weak_handle.upgrade(cx)),
180 cx,
181 )
182 .detach();
183 });
184 cx.add_action(|workspace: &mut Workspace, action: &GoForward, cx| {
185 Pane::go_forward(
186 workspace,
187 action
188 .pane
189 .as_ref()
190 .and_then(|weak_handle| weak_handle.upgrade(cx)),
191 cx,
192 )
193 .detach();
194 });
195}
196
197#[derive(Debug)]
198pub enum Event {
199 ActivateItem { local: bool },
200 Remove,
201 RemoveItem { item_id: usize },
202 Split(SplitDirection),
203 ChangeItemTitle,
204}
205
206pub struct Pane {
207 items: Vec<Box<dyn ItemHandle>>,
208 activation_history: Vec<usize>,
209 is_active: bool,
210 active_item_index: usize,
211 last_focused_view_by_item: HashMap<usize, AnyWeakViewHandle>,
212 autoscroll: bool,
213 nav_history: Rc<RefCell<NavHistory>>,
214 toolbar: ViewHandle<Toolbar>,
215 tab_bar_context_menu: ViewHandle<ContextMenu>,
216 docked: Option<DockAnchor>,
217}
218
219pub struct ItemNavHistory {
220 history: Rc<RefCell<NavHistory>>,
221 item: Rc<dyn WeakItemHandle>,
222}
223
224struct NavHistory {
225 mode: NavigationMode,
226 backward_stack: VecDeque<NavigationEntry>,
227 forward_stack: VecDeque<NavigationEntry>,
228 closed_stack: VecDeque<NavigationEntry>,
229 paths_by_item: HashMap<usize, ProjectPath>,
230 pane: WeakViewHandle<Pane>,
231}
232
233#[derive(Copy, Clone)]
234enum NavigationMode {
235 Normal,
236 GoingBack,
237 GoingForward,
238 ClosingItem,
239 ReopeningClosedItem,
240 Disabled,
241}
242
243impl Default for NavigationMode {
244 fn default() -> Self {
245 Self::Normal
246 }
247}
248
249pub struct NavigationEntry {
250 pub item: Rc<dyn WeakItemHandle>,
251 pub data: Option<Box<dyn Any>>,
252}
253
254struct DraggedItem {
255 item: Box<dyn ItemHandle>,
256 pane: WeakViewHandle<Pane>,
257}
258
259pub enum ReorderBehavior {
260 None,
261 MoveAfterActive,
262 MoveToIndex(usize),
263}
264
265enum ItemType {
266 Active,
267 Inactive,
268 Clean,
269 All,
270}
271
272impl Pane {
273 pub fn new(docked: Option<DockAnchor>, cx: &mut ViewContext<Self>) -> Self {
274 let handle = cx.weak_handle();
275 let context_menu = cx.add_view(ContextMenu::new);
276 Self {
277 items: Vec::new(),
278 activation_history: Vec::new(),
279 is_active: true,
280 active_item_index: 0,
281 last_focused_view_by_item: Default::default(),
282 autoscroll: false,
283 nav_history: Rc::new(RefCell::new(NavHistory {
284 mode: NavigationMode::Normal,
285 backward_stack: Default::default(),
286 forward_stack: Default::default(),
287 closed_stack: Default::default(),
288 paths_by_item: Default::default(),
289 pane: handle.clone(),
290 })),
291 toolbar: cx.add_view(|_| Toolbar::new(handle)),
292 tab_bar_context_menu: context_menu,
293 docked,
294 }
295 }
296
297 pub fn is_active(&self) -> bool {
298 self.is_active
299 }
300
301 pub fn set_active(&mut self, is_active: bool, cx: &mut ViewContext<Self>) {
302 self.is_active = is_active;
303 cx.notify();
304 }
305
306 pub fn set_docked(&mut self, docked: Option<DockAnchor>, cx: &mut ViewContext<Self>) {
307 self.docked = docked;
308 cx.notify();
309 }
310
311 pub fn nav_history_for_item<T: Item>(&self, item: &ViewHandle<T>) -> ItemNavHistory {
312 ItemNavHistory {
313 history: self.nav_history.clone(),
314 item: Rc::new(item.downgrade()),
315 }
316 }
317
318 pub fn go_back(
319 workspace: &mut Workspace,
320 pane: Option<ViewHandle<Pane>>,
321 cx: &mut ViewContext<Workspace>,
322 ) -> Task<()> {
323 Self::navigate_history(
324 workspace,
325 pane.unwrap_or_else(|| workspace.active_pane().clone()),
326 NavigationMode::GoingBack,
327 cx,
328 )
329 }
330
331 pub fn go_forward(
332 workspace: &mut Workspace,
333 pane: Option<ViewHandle<Pane>>,
334 cx: &mut ViewContext<Workspace>,
335 ) -> Task<()> {
336 Self::navigate_history(
337 workspace,
338 pane.unwrap_or_else(|| workspace.active_pane().clone()),
339 NavigationMode::GoingForward,
340 cx,
341 )
342 }
343
344 pub fn reopen_closed_item(
345 workspace: &mut Workspace,
346 cx: &mut ViewContext<Workspace>,
347 ) -> Task<()> {
348 Self::navigate_history(
349 workspace,
350 workspace.active_pane().clone(),
351 NavigationMode::ReopeningClosedItem,
352 cx,
353 )
354 }
355
356 pub fn disable_history(&mut self) {
357 self.nav_history.borrow_mut().disable();
358 }
359
360 pub fn enable_history(&mut self) {
361 self.nav_history.borrow_mut().enable();
362 }
363
364 pub fn can_navigate_backward(&self) -> bool {
365 !self.nav_history.borrow().backward_stack.is_empty()
366 }
367
368 pub fn can_navigate_forward(&self) -> bool {
369 !self.nav_history.borrow().forward_stack.is_empty()
370 }
371
372 fn history_updated(&mut self, cx: &mut ViewContext<Self>) {
373 self.toolbar.update(cx, |_, cx| cx.notify());
374 }
375
376 fn navigate_history(
377 workspace: &mut Workspace,
378 pane: ViewHandle<Pane>,
379 mode: NavigationMode,
380 cx: &mut ViewContext<Workspace>,
381 ) -> Task<()> {
382 cx.focus(pane.clone());
383
384 let to_load = pane.update(cx, |pane, cx| {
385 loop {
386 // Retrieve the weak item handle from the history.
387 let entry = pane.nav_history.borrow_mut().pop(mode, cx)?;
388
389 // If the item is still present in this pane, then activate it.
390 if let Some(index) = entry
391 .item
392 .upgrade(cx)
393 .and_then(|v| pane.index_for_item(v.as_ref()))
394 {
395 let prev_active_item_index = pane.active_item_index;
396 pane.nav_history.borrow_mut().set_mode(mode);
397 pane.activate_item(index, true, true, cx);
398 pane.nav_history
399 .borrow_mut()
400 .set_mode(NavigationMode::Normal);
401
402 let mut navigated = prev_active_item_index != pane.active_item_index;
403 if let Some(data) = entry.data {
404 navigated |= pane.active_item()?.navigate(data, cx);
405 }
406
407 if navigated {
408 break None;
409 }
410 }
411 // If the item is no longer present in this pane, then retrieve its
412 // project path in order to reopen it.
413 else {
414 break pane
415 .nav_history
416 .borrow()
417 .paths_by_item
418 .get(&entry.item.id())
419 .cloned()
420 .map(|project_path| (project_path, entry));
421 }
422 }
423 });
424
425 if let Some((project_path, entry)) = to_load {
426 // If the item was no longer present, then load it again from its previous path.
427 let pane = pane.downgrade();
428 let task = workspace.load_path(project_path, cx);
429 cx.spawn(|workspace, mut cx| async move {
430 let task = task.await;
431 if let Some(pane) = pane.upgrade(&cx) {
432 let mut navigated = false;
433 if let Some((project_entry_id, build_item)) = task.log_err() {
434 let prev_active_item_id = pane.update(&mut cx, |pane, _| {
435 pane.nav_history.borrow_mut().set_mode(mode);
436 pane.active_item().map(|p| p.id())
437 });
438
439 let item = workspace.update(&mut cx, |workspace, cx| {
440 Self::open_item(
441 workspace,
442 pane.clone(),
443 project_entry_id,
444 true,
445 cx,
446 build_item,
447 )
448 });
449
450 pane.update(&mut cx, |pane, cx| {
451 navigated |= Some(item.id()) != prev_active_item_id;
452 pane.nav_history
453 .borrow_mut()
454 .set_mode(NavigationMode::Normal);
455 if let Some(data) = entry.data {
456 navigated |= item.navigate(data, cx);
457 }
458 });
459 }
460
461 if !navigated {
462 workspace
463 .update(&mut cx, |workspace, cx| {
464 Self::navigate_history(workspace, pane, mode, cx)
465 })
466 .await;
467 }
468 }
469 })
470 } else {
471 Task::ready(())
472 }
473 }
474
475 pub(crate) fn open_item(
476 workspace: &mut Workspace,
477 pane: ViewHandle<Pane>,
478 project_entry_id: ProjectEntryId,
479 focus_item: bool,
480 cx: &mut ViewContext<Workspace>,
481 build_item: impl FnOnce(&mut ViewContext<Pane>) -> Box<dyn ItemHandle>,
482 ) -> Box<dyn ItemHandle> {
483 let existing_item = pane.update(cx, |pane, cx| {
484 for (index, item) in pane.items.iter().enumerate() {
485 if item.is_singleton(cx)
486 && item.project_entry_ids(cx).as_slice() == [project_entry_id]
487 {
488 let item = item.boxed_clone();
489 return Some((index, item));
490 }
491 }
492 None
493 });
494
495 if let Some((index, existing_item)) = existing_item {
496 pane.update(cx, |pane, cx| {
497 pane.activate_item(index, focus_item, focus_item, cx);
498 });
499 existing_item
500 } else {
501 let new_item = pane.update(cx, |_, cx| build_item(cx));
502 Pane::add_item(
503 workspace,
504 &pane,
505 new_item.clone(),
506 true,
507 focus_item,
508 None,
509 cx,
510 );
511 new_item
512 }
513 }
514
515 pub(crate) fn add_item(
516 workspace: &mut Workspace,
517 pane: &ViewHandle<Pane>,
518 item: Box<dyn ItemHandle>,
519 activate_pane: bool,
520 focus_item: bool,
521 destination_index: Option<usize>,
522 cx: &mut ViewContext<Workspace>,
523 ) {
524 // If no destination index is specified, add or move the item after the active item.
525 let mut insertion_index = {
526 let pane = pane.read(cx);
527 cmp::min(
528 if let Some(destination_index) = destination_index {
529 destination_index
530 } else {
531 pane.active_item_index + 1
532 },
533 pane.items.len(),
534 )
535 };
536
537 item.added_to_pane(workspace, pane.clone(), cx);
538
539 // Does the item already exist?
540 let project_entry_id = if item.is_singleton(cx) {
541 item.project_entry_ids(cx).get(0).copied()
542 } else {
543 None
544 };
545
546 let existing_item_index = pane.read(cx).items.iter().position(|existing_item| {
547 if existing_item.id() == item.id() {
548 true
549 } else if existing_item.is_singleton(cx) {
550 existing_item
551 .project_entry_ids(cx)
552 .get(0)
553 .map_or(false, |existing_entry_id| {
554 Some(existing_entry_id) == project_entry_id.as_ref()
555 })
556 } else {
557 false
558 }
559 });
560
561 if let Some(existing_item_index) = existing_item_index {
562 // If the item already exists, move it to the desired destination and activate it
563 pane.update(cx, |pane, cx| {
564 if existing_item_index != insertion_index {
565 cx.reparent(&item);
566 let existing_item_is_active = existing_item_index == pane.active_item_index;
567
568 // If the caller didn't specify a destination and the added item is already
569 // the active one, don't move it
570 if existing_item_is_active && destination_index.is_none() {
571 insertion_index = existing_item_index;
572 } else {
573 pane.items.remove(existing_item_index);
574 if existing_item_index < pane.active_item_index {
575 pane.active_item_index -= 1;
576 }
577 insertion_index = insertion_index.min(pane.items.len());
578
579 pane.items.insert(insertion_index, item.clone());
580
581 if existing_item_is_active {
582 pane.active_item_index = insertion_index;
583 } else if insertion_index <= pane.active_item_index {
584 pane.active_item_index += 1;
585 }
586 }
587
588 cx.notify();
589 }
590
591 pane.activate_item(insertion_index, activate_pane, focus_item, cx);
592 });
593 } else {
594 pane.update(cx, |pane, cx| {
595 cx.reparent(&item);
596 pane.items.insert(insertion_index, item);
597 if insertion_index <= pane.active_item_index {
598 pane.active_item_index += 1;
599 }
600
601 pane.activate_item(insertion_index, activate_pane, focus_item, cx);
602 cx.notify();
603 });
604 }
605 }
606
607 pub fn items_len(&self) -> usize {
608 self.items.len()
609 }
610
611 pub fn items(&self) -> impl Iterator<Item = &Box<dyn ItemHandle>> {
612 self.items.iter()
613 }
614
615 pub fn items_of_type<T: View>(&self) -> impl '_ + Iterator<Item = ViewHandle<T>> {
616 self.items
617 .iter()
618 .filter_map(|item| item.to_any().downcast())
619 }
620
621 pub fn active_item(&self) -> Option<Box<dyn ItemHandle>> {
622 self.items.get(self.active_item_index).cloned()
623 }
624
625 pub fn item_for_entry(
626 &self,
627 entry_id: ProjectEntryId,
628 cx: &AppContext,
629 ) -> Option<Box<dyn ItemHandle>> {
630 self.items.iter().find_map(|item| {
631 if item.is_singleton(cx) && item.project_entry_ids(cx).as_slice() == [entry_id] {
632 Some(item.boxed_clone())
633 } else {
634 None
635 }
636 })
637 }
638
639 pub fn index_for_item(&self, item: &dyn ItemHandle) -> Option<usize> {
640 self.items.iter().position(|i| i.id() == item.id())
641 }
642
643 pub fn activate_item(
644 &mut self,
645 index: usize,
646 activate_pane: bool,
647 focus_item: bool,
648 cx: &mut ViewContext<Self>,
649 ) {
650 use NavigationMode::{GoingBack, GoingForward};
651
652 if index < self.items.len() {
653 let prev_active_item_ix = mem::replace(&mut self.active_item_index, index);
654 if prev_active_item_ix != self.active_item_index
655 || matches!(self.nav_history.borrow().mode, GoingBack | GoingForward)
656 {
657 if let Some(prev_item) = self.items.get(prev_active_item_ix) {
658 prev_item.deactivated(cx);
659 }
660
661 cx.emit(Event::ActivateItem {
662 local: activate_pane,
663 });
664 }
665
666 if let Some(newly_active_item) = self.items.get(index) {
667 self.activation_history
668 .retain(|&previously_active_item_id| {
669 previously_active_item_id != newly_active_item.id()
670 });
671 self.activation_history.push(newly_active_item.id());
672 }
673
674 self.update_toolbar(cx);
675
676 if focus_item {
677 self.focus_active_item(cx);
678 }
679
680 self.autoscroll = true;
681 cx.notify();
682 }
683 }
684
685 pub fn activate_prev_item(&mut self, activate_pane: bool, cx: &mut ViewContext<Self>) {
686 let mut index = self.active_item_index;
687 if index > 0 {
688 index -= 1;
689 } else if !self.items.is_empty() {
690 index = self.items.len() - 1;
691 }
692 self.activate_item(index, activate_pane, activate_pane, cx);
693 }
694
695 pub fn activate_next_item(&mut self, activate_pane: bool, cx: &mut ViewContext<Self>) {
696 let mut index = self.active_item_index;
697 if index + 1 < self.items.len() {
698 index += 1;
699 } else {
700 index = 0;
701 }
702 self.activate_item(index, activate_pane, activate_pane, cx);
703 }
704
705 pub fn close_active_item(
706 workspace: &mut Workspace,
707 _: &CloseActiveItem,
708 cx: &mut ViewContext<Workspace>,
709 ) -> Option<Task<Result<()>>> {
710 Self::close_main(workspace, ItemType::Active, cx)
711 }
712
713 pub fn close_inactive_items(
714 workspace: &mut Workspace,
715 _: &CloseInactiveItems,
716 cx: &mut ViewContext<Workspace>,
717 ) -> Option<Task<Result<()>>> {
718 Self::close_main(workspace, ItemType::Inactive, cx)
719 }
720
721 pub fn close_all_items(
722 workspace: &mut Workspace,
723 _: &CloseAllItems,
724 cx: &mut ViewContext<Workspace>,
725 ) -> Option<Task<Result<()>>> {
726 Self::close_main(workspace, ItemType::All, cx)
727 }
728
729 pub fn close_clean_items(
730 workspace: &mut Workspace,
731 _: &CloseCleanItems,
732 cx: &mut ViewContext<Workspace>,
733 ) -> Option<Task<Result<()>>> {
734 Self::close_main(workspace, ItemType::Clean, cx)
735 }
736
737 fn close_main(
738 workspace: &mut Workspace,
739 close_item_type: ItemType,
740 cx: &mut ViewContext<Workspace>,
741 ) -> Option<Task<Result<()>>> {
742 let pane_handle = workspace.active_pane().clone();
743 let pane = pane_handle.read(cx);
744 if pane.items.is_empty() {
745 return None;
746 }
747
748 let active_item_id = pane.items[pane.active_item_index].id();
749 let clean_item_ids: Vec<_> = pane
750 .items()
751 .filter(|item| !item.is_dirty(cx))
752 .map(|item| item.id())
753 .collect();
754 let task =
755 Self::close_items(
756 workspace,
757 pane_handle,
758 cx,
759 move |item_id| match close_item_type {
760 ItemType::Active => item_id == active_item_id,
761 ItemType::Inactive => item_id != active_item_id,
762 ItemType::Clean => clean_item_ids.contains(&item_id),
763 ItemType::All => true,
764 },
765 );
766
767 Some(cx.foreground().spawn(async move {
768 task.await?;
769 Ok(())
770 }))
771 }
772
773 pub fn close_item(
774 workspace: &mut Workspace,
775 pane: ViewHandle<Pane>,
776 item_id_to_close: usize,
777 cx: &mut ViewContext<Workspace>,
778 ) -> Task<Result<()>> {
779 Self::close_items(workspace, pane, cx, move |view_id| {
780 view_id == item_id_to_close
781 })
782 }
783
784 pub fn close_items(
785 workspace: &mut Workspace,
786 pane: ViewHandle<Pane>,
787 cx: &mut ViewContext<Workspace>,
788 should_close: impl 'static + Fn(usize) -> bool,
789 ) -> Task<Result<()>> {
790 let project = workspace.project().clone();
791
792 // Find the items to close.
793 let mut items_to_close = Vec::new();
794 for item in &pane.read(cx).items {
795 if should_close(item.id()) {
796 items_to_close.push(item.boxed_clone());
797 }
798 }
799
800 // If a buffer is open both in a singleton editor and in a multibuffer, make sure
801 // to focus the singleton buffer when prompting to save that buffer, as opposed
802 // to focusing the multibuffer, because this gives the user a more clear idea
803 // of what content they would be saving.
804 items_to_close.sort_by_key(|item| !item.is_singleton(cx));
805
806 cx.spawn(|workspace, mut cx| async move {
807 let mut saved_project_items_ids = HashSet::default();
808 for item in items_to_close.clone() {
809 // Find the item's current index and its set of project item models. Avoid
810 // storing these in advance, in case they have changed since this task
811 // was started.
812 let (item_ix, mut project_item_ids) = pane.read_with(&cx, |pane, cx| {
813 (pane.index_for_item(&*item), item.project_item_model_ids(cx))
814 });
815 let item_ix = if let Some(ix) = item_ix {
816 ix
817 } else {
818 continue;
819 };
820
821 // Check if this view has any project items that are not open anywhere else
822 // in the workspace, AND that the user has not already been prompted to save.
823 // If there are any such project entries, prompt the user to save this item.
824 workspace.read_with(&cx, |workspace, cx| {
825 for item in workspace.items(cx) {
826 if !items_to_close
827 .iter()
828 .any(|item_to_close| item_to_close.id() == item.id())
829 {
830 let other_project_item_ids = item.project_item_model_ids(cx);
831 project_item_ids.retain(|id| !other_project_item_ids.contains(id));
832 }
833 }
834 });
835 let should_save = project_item_ids
836 .iter()
837 .any(|id| saved_project_items_ids.insert(*id));
838
839 if should_save
840 && !Self::save_item(project.clone(), &pane, item_ix, &*item, true, &mut cx)
841 .await?
842 {
843 break;
844 }
845
846 // Remove the item from the pane.
847 pane.update(&mut cx, |pane, cx| {
848 if let Some(item_ix) = pane.items.iter().position(|i| i.id() == item.id()) {
849 pane.remove_item(item_ix, false, cx);
850 }
851 });
852 }
853
854 pane.update(&mut cx, |_, cx| cx.notify());
855 Ok(())
856 })
857 }
858
859 fn remove_item(&mut self, item_index: usize, activate_pane: bool, cx: &mut ViewContext<Self>) {
860 self.activation_history
861 .retain(|&history_entry| history_entry != self.items[item_index].id());
862
863 if item_index == self.active_item_index {
864 let index_to_activate = self
865 .activation_history
866 .pop()
867 .and_then(|last_activated_item| {
868 self.items.iter().enumerate().find_map(|(index, item)| {
869 (item.id() == last_activated_item).then_some(index)
870 })
871 })
872 // We didn't have a valid activation history entry, so fallback
873 // to activating the item to the left
874 .unwrap_or_else(|| item_index.min(self.items.len()).saturating_sub(1));
875
876 self.activate_item(index_to_activate, activate_pane, activate_pane, cx);
877 }
878
879 let item = self.items.remove(item_index);
880
881 cx.emit(Event::RemoveItem { item_id: item.id() });
882 if self.items.is_empty() {
883 item.deactivated(cx);
884 self.update_toolbar(cx);
885 cx.emit(Event::Remove);
886 }
887
888 if item_index < self.active_item_index {
889 self.active_item_index -= 1;
890 }
891
892 self.nav_history
893 .borrow_mut()
894 .set_mode(NavigationMode::ClosingItem);
895 item.deactivated(cx);
896 self.nav_history
897 .borrow_mut()
898 .set_mode(NavigationMode::Normal);
899
900 if let Some(path) = item.project_path(cx) {
901 self.nav_history
902 .borrow_mut()
903 .paths_by_item
904 .insert(item.id(), path);
905 } else {
906 self.nav_history
907 .borrow_mut()
908 .paths_by_item
909 .remove(&item.id());
910 }
911
912 cx.notify();
913 }
914
915 pub async fn save_item(
916 project: ModelHandle<Project>,
917 pane: &ViewHandle<Pane>,
918 item_ix: usize,
919 item: &dyn ItemHandle,
920 should_prompt_for_save: bool,
921 cx: &mut AsyncAppContext,
922 ) -> Result<bool> {
923 const CONFLICT_MESSAGE: &str =
924 "This file has changed on disk since you started editing it. Do you want to overwrite it?";
925 const DIRTY_MESSAGE: &str = "This file contains unsaved edits. Do you want to save it?";
926
927 let (has_conflict, is_dirty, can_save, is_singleton) = cx.read(|cx| {
928 (
929 item.has_conflict(cx),
930 item.is_dirty(cx),
931 item.can_save(cx),
932 item.is_singleton(cx),
933 )
934 });
935
936 if has_conflict && can_save {
937 let mut answer = pane.update(cx, |pane, cx| {
938 pane.activate_item(item_ix, true, true, cx);
939 cx.prompt(
940 PromptLevel::Warning,
941 CONFLICT_MESSAGE,
942 &["Overwrite", "Discard", "Cancel"],
943 )
944 });
945 match answer.next().await {
946 Some(0) => cx.update(|cx| item.save(project, cx)).await?,
947 Some(1) => cx.update(|cx| item.reload(project, cx)).await?,
948 _ => return Ok(false),
949 }
950 } else if is_dirty && (can_save || is_singleton) {
951 let will_autosave = cx.read(|cx| {
952 matches!(
953 cx.global::<Settings>().autosave,
954 Autosave::OnFocusChange | Autosave::OnWindowChange
955 ) && Self::can_autosave_item(&*item, cx)
956 });
957 let should_save = if should_prompt_for_save && !will_autosave {
958 let mut answer = pane.update(cx, |pane, cx| {
959 pane.activate_item(item_ix, true, true, cx);
960 cx.prompt(
961 PromptLevel::Warning,
962 DIRTY_MESSAGE,
963 &["Save", "Don't Save", "Cancel"],
964 )
965 });
966 match answer.next().await {
967 Some(0) => true,
968 Some(1) => false,
969 _ => return Ok(false),
970 }
971 } else {
972 true
973 };
974
975 if should_save {
976 if can_save {
977 cx.update(|cx| item.save(project, cx)).await?;
978 } else if is_singleton {
979 let start_abs_path = project
980 .read_with(cx, |project, cx| {
981 let worktree = project.visible_worktrees(cx).next()?;
982 Some(worktree.read(cx).as_local()?.abs_path().to_path_buf())
983 })
984 .unwrap_or_else(|| Path::new("").into());
985
986 let mut abs_path = cx.update(|cx| cx.prompt_for_new_path(&start_abs_path));
987 if let Some(abs_path) = abs_path.next().await.flatten() {
988 cx.update(|cx| item.save_as(project, abs_path, cx)).await?;
989 } else {
990 return Ok(false);
991 }
992 }
993 }
994 }
995 Ok(true)
996 }
997
998 fn can_autosave_item(item: &dyn ItemHandle, cx: &AppContext) -> bool {
999 let is_deleted = item.project_entry_ids(cx).is_empty();
1000 item.is_dirty(cx) && !item.has_conflict(cx) && item.can_save(cx) && !is_deleted
1001 }
1002
1003 pub fn autosave_item(
1004 item: &dyn ItemHandle,
1005 project: ModelHandle<Project>,
1006 cx: &mut MutableAppContext,
1007 ) -> Task<Result<()>> {
1008 if Self::can_autosave_item(item, cx) {
1009 item.save(project, cx)
1010 } else {
1011 Task::ready(Ok(()))
1012 }
1013 }
1014
1015 pub fn focus_active_item(&mut self, cx: &mut ViewContext<Self>) {
1016 if let Some(active_item) = self.active_item() {
1017 cx.focus(active_item);
1018 }
1019 }
1020
1021 pub fn move_item(
1022 workspace: &mut Workspace,
1023 from: ViewHandle<Pane>,
1024 to: ViewHandle<Pane>,
1025 item_id_to_move: usize,
1026 destination_index: usize,
1027 cx: &mut ViewContext<Workspace>,
1028 ) {
1029 let item_to_move = from
1030 .read(cx)
1031 .items()
1032 .enumerate()
1033 .find(|(_, item_handle)| item_handle.id() == item_id_to_move);
1034
1035 if item_to_move.is_none() {
1036 log::warn!("Tried to move item handle which was not in `from` pane. Maybe tab was closed during drop");
1037 return;
1038 }
1039 let (item_ix, item_handle) = item_to_move.unwrap();
1040 let item_handle = item_handle.clone();
1041
1042 if from != to {
1043 // Close item from previous pane
1044 from.update(cx, |from, cx| {
1045 from.remove_item(item_ix, false, cx);
1046 });
1047 }
1048
1049 // This automatically removes duplicate items in the pane
1050 Pane::add_item(
1051 workspace,
1052 &to,
1053 item_handle,
1054 true,
1055 true,
1056 Some(destination_index),
1057 cx,
1058 );
1059
1060 cx.focus(to);
1061 }
1062
1063 pub fn split(&mut self, direction: SplitDirection, cx: &mut ViewContext<Self>) {
1064 cx.emit(Event::Split(direction));
1065 }
1066
1067 fn deploy_split_menu(&mut self, action: &DeploySplitMenu, cx: &mut ViewContext<Self>) {
1068 self.tab_bar_context_menu.update(cx, |menu, cx| {
1069 menu.show(
1070 action.position,
1071 AnchorCorner::TopRight,
1072 vec![
1073 ContextMenuItem::item("Split Right", SplitRight),
1074 ContextMenuItem::item("Split Left", SplitLeft),
1075 ContextMenuItem::item("Split Up", SplitUp),
1076 ContextMenuItem::item("Split Down", SplitDown),
1077 ],
1078 cx,
1079 );
1080 });
1081 }
1082
1083 fn deploy_dock_menu(&mut self, action: &DeployDockMenu, cx: &mut ViewContext<Self>) {
1084 self.tab_bar_context_menu.update(cx, |menu, cx| {
1085 menu.show(
1086 action.position,
1087 AnchorCorner::TopRight,
1088 vec![
1089 ContextMenuItem::item("Anchor Dock Right", AnchorDockRight),
1090 ContextMenuItem::item("Anchor Dock Bottom", AnchorDockBottom),
1091 ContextMenuItem::item("Expand Dock", ExpandDock),
1092 ],
1093 cx,
1094 );
1095 });
1096 }
1097
1098 fn deploy_new_menu(&mut self, action: &DeployNewMenu, cx: &mut ViewContext<Self>) {
1099 self.tab_bar_context_menu.update(cx, |menu, cx| {
1100 menu.show(
1101 action.position,
1102 AnchorCorner::TopRight,
1103 vec![
1104 ContextMenuItem::item("New File", NewFile),
1105 ContextMenuItem::item("New Terminal", NewTerminal),
1106 ContextMenuItem::item("New Search", NewSearch),
1107 ],
1108 cx,
1109 );
1110 });
1111 }
1112
1113 pub fn toolbar(&self) -> &ViewHandle<Toolbar> {
1114 &self.toolbar
1115 }
1116
1117 fn update_toolbar(&mut self, cx: &mut ViewContext<Self>) {
1118 let active_item = self
1119 .items
1120 .get(self.active_item_index)
1121 .map(|item| item.as_ref());
1122 self.toolbar.update(cx, |toolbar, cx| {
1123 toolbar.set_active_pane_item(active_item, cx);
1124 });
1125 }
1126
1127 fn render_tabs(&mut self, cx: &mut RenderContext<Self>) -> impl Element {
1128 let theme = cx.global::<Settings>().theme.clone();
1129
1130 let pane = cx.handle();
1131 let autoscroll = if mem::take(&mut self.autoscroll) {
1132 Some(self.active_item_index)
1133 } else {
1134 None
1135 };
1136
1137 let pane_active = self.is_active;
1138
1139 enum Tabs {}
1140 let mut row = Flex::row().scrollable::<Tabs, _>(1, autoscroll, cx);
1141 for (ix, (item, detail)) in self
1142 .items
1143 .iter()
1144 .cloned()
1145 .zip(self.tab_details(cx))
1146 .enumerate()
1147 {
1148 let detail = if detail == 0 { None } else { Some(detail) };
1149 let tab_active = ix == self.active_item_index;
1150
1151 row.add_child({
1152 enum Tab {}
1153 let mut receiver = dragged_item_receiver::<Tab, _>(ix, ix, true, None, cx, {
1154 let item = item.clone();
1155 let pane = pane.clone();
1156 let detail = detail.clone();
1157
1158 let theme = cx.global::<Settings>().theme.clone();
1159
1160 move |mouse_state, cx| {
1161 let tab_style = theme.workspace.tab_bar.tab_style(pane_active, tab_active);
1162 let hovered = mouse_state.hovered();
1163 Self::render_tab(&item, pane, ix == 0, detail, hovered, tab_style, cx)
1164 }
1165 });
1166
1167 if !pane_active || !tab_active {
1168 receiver = receiver.with_cursor_style(CursorStyle::PointingHand);
1169 }
1170
1171 receiver
1172 .on_down(MouseButton::Left, move |_, cx| {
1173 cx.dispatch_action(ActivateItem(ix));
1174 cx.propagate_event();
1175 })
1176 .on_click(MouseButton::Middle, {
1177 let item = item.clone();
1178 let pane = pane.clone();
1179 move |_, cx: &mut EventContext| {
1180 cx.dispatch_action(CloseItem {
1181 item_id: item.id(),
1182 pane: pane.clone(),
1183 })
1184 }
1185 })
1186 .as_draggable(
1187 DraggedItem {
1188 item,
1189 pane: pane.clone(),
1190 },
1191 {
1192 let theme = cx.global::<Settings>().theme.clone();
1193
1194 let detail = detail.clone();
1195 move |dragged_item, cx: &mut RenderContext<Workspace>| {
1196 let tab_style = &theme.workspace.tab_bar.dragged_tab;
1197 Self::render_tab(
1198 &dragged_item.item,
1199 dragged_item.pane.clone(),
1200 false,
1201 detail,
1202 false,
1203 &tab_style,
1204 cx,
1205 )
1206 }
1207 },
1208 )
1209 .boxed()
1210 })
1211 }
1212
1213 // Use the inactive tab style along with the current pane's active status to decide how to render
1214 // the filler
1215 let filler_index = self.items.len();
1216 let filler_style = theme.workspace.tab_bar.tab_style(pane_active, false);
1217 enum Filler {}
1218 row.add_child(
1219 dragged_item_receiver::<Filler, _>(0, filler_index, true, None, cx, |_, _| {
1220 Empty::new()
1221 .contained()
1222 .with_style(filler_style.container)
1223 .with_border(filler_style.container.border)
1224 .boxed()
1225 })
1226 .flex(1., true)
1227 .named("filler"),
1228 );
1229
1230 row
1231 }
1232
1233 fn tab_details(&self, cx: &AppContext) -> Vec<usize> {
1234 let mut tab_details = (0..self.items.len()).map(|_| 0).collect::<Vec<_>>();
1235
1236 let mut tab_descriptions = HashMap::default();
1237 let mut done = false;
1238 while !done {
1239 done = true;
1240
1241 // Store item indices by their tab description.
1242 for (ix, (item, detail)) in self.items.iter().zip(&tab_details).enumerate() {
1243 if let Some(description) = item.tab_description(*detail, cx) {
1244 if *detail == 0
1245 || Some(&description) != item.tab_description(detail - 1, cx).as_ref()
1246 {
1247 tab_descriptions
1248 .entry(description)
1249 .or_insert(Vec::new())
1250 .push(ix);
1251 }
1252 }
1253 }
1254
1255 // If two or more items have the same tab description, increase their level
1256 // of detail and try again.
1257 for (_, item_ixs) in tab_descriptions.drain() {
1258 if item_ixs.len() > 1 {
1259 done = false;
1260 for ix in item_ixs {
1261 tab_details[ix] += 1;
1262 }
1263 }
1264 }
1265 }
1266
1267 tab_details
1268 }
1269
1270 fn render_tab<V: View>(
1271 item: &Box<dyn ItemHandle>,
1272 pane: WeakViewHandle<Pane>,
1273 first: bool,
1274 detail: Option<usize>,
1275 hovered: bool,
1276 tab_style: &theme::Tab,
1277 cx: &mut RenderContext<V>,
1278 ) -> ElementBox {
1279 let title = item.tab_content(detail, &tab_style, cx);
1280 let mut container = tab_style.container.clone();
1281 if first {
1282 container.border.left = false;
1283 }
1284
1285 Flex::row()
1286 .with_child(
1287 Align::new({
1288 let diameter = 7.0;
1289 let icon_color = if item.has_conflict(cx) {
1290 Some(tab_style.icon_conflict)
1291 } else if item.is_dirty(cx) {
1292 Some(tab_style.icon_dirty)
1293 } else {
1294 None
1295 };
1296
1297 ConstrainedBox::new(
1298 Canvas::new(move |bounds, _, cx| {
1299 if let Some(color) = icon_color {
1300 let square = RectF::new(bounds.origin(), vec2f(diameter, diameter));
1301 cx.scene.push_quad(Quad {
1302 bounds: square,
1303 background: Some(color),
1304 border: Default::default(),
1305 corner_radius: diameter / 2.,
1306 });
1307 }
1308 })
1309 .boxed(),
1310 )
1311 .with_width(diameter)
1312 .with_height(diameter)
1313 .boxed()
1314 })
1315 .boxed(),
1316 )
1317 .with_child(
1318 Container::new(Align::new(title).boxed())
1319 .with_style(ContainerStyle {
1320 margin: Margin {
1321 left: tab_style.spacing,
1322 right: tab_style.spacing,
1323 ..Default::default()
1324 },
1325 ..Default::default()
1326 })
1327 .boxed(),
1328 )
1329 .with_child(
1330 Align::new(
1331 ConstrainedBox::new(if hovered {
1332 let item_id = item.id();
1333 enum TabCloseButton {}
1334 let icon = Svg::new("icons/x_mark_8.svg");
1335 MouseEventHandler::<TabCloseButton>::new(item_id, cx, |mouse_state, _| {
1336 if mouse_state.hovered() {
1337 icon.with_color(tab_style.icon_close_active).boxed()
1338 } else {
1339 icon.with_color(tab_style.icon_close).boxed()
1340 }
1341 })
1342 .with_padding(Padding::uniform(4.))
1343 .with_cursor_style(CursorStyle::PointingHand)
1344 .on_click(MouseButton::Left, {
1345 let pane = pane.clone();
1346 move |_, cx| {
1347 cx.dispatch_action(CloseItem {
1348 item_id,
1349 pane: pane.clone(),
1350 })
1351 }
1352 })
1353 .named("close-tab-icon")
1354 } else {
1355 Empty::new().boxed()
1356 })
1357 .with_width(tab_style.icon_width)
1358 .boxed(),
1359 )
1360 .boxed(),
1361 )
1362 .contained()
1363 .with_style(container)
1364 .constrained()
1365 .with_height(tab_style.height)
1366 .boxed()
1367 }
1368
1369 fn render_tab_bar_buttons(
1370 &mut self,
1371 theme: &Theme,
1372 cx: &mut RenderContext<Self>,
1373 ) -> ElementBox {
1374 Flex::row()
1375 // New menu
1376 .with_child(tab_bar_button(0, "icons/plus_12.svg", cx, |position| {
1377 DeployNewMenu { position }
1378 }))
1379 .with_child(
1380 self.docked
1381 .map(|anchor| {
1382 // Add the dock menu button if this pane is a dock
1383 let dock_icon = icon_for_dock_anchor(anchor);
1384
1385 tab_bar_button(1, dock_icon, cx, |position| DeployDockMenu { position })
1386 })
1387 .unwrap_or_else(|| {
1388 // Add the split menu if this pane is not a dock
1389 tab_bar_button(2, "icons/split_12.svg", cx, |position| DeploySplitMenu {
1390 position,
1391 })
1392 }),
1393 )
1394 // Add the close dock button if this pane is a dock
1395 .with_children(
1396 self.docked
1397 .map(|_| tab_bar_button(3, "icons/x_mark_8.svg", cx, |_| HideDock)),
1398 )
1399 .contained()
1400 .with_style(theme.workspace.tab_bar.pane_button_container)
1401 .flex(1., false)
1402 .boxed()
1403 }
1404}
1405
1406impl Entity for Pane {
1407 type Event = Event;
1408}
1409
1410impl View for Pane {
1411 fn ui_name() -> &'static str {
1412 "Pane"
1413 }
1414
1415 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
1416 let this = cx.handle();
1417
1418 enum MouseNavigationHandler {}
1419
1420 Stack::new()
1421 .with_child(
1422 MouseEventHandler::<MouseNavigationHandler>::new(0, cx, |_, cx| {
1423 if let Some(active_item) = self.active_item() {
1424 Flex::column()
1425 .with_child({
1426 let mut tab_row = Flex::row()
1427 .with_child(self.render_tabs(cx).flex(1., true).named("tabs"));
1428
1429 // Render pane buttons
1430 let theme = cx.global::<Settings>().theme.clone();
1431 if self.is_active {
1432 tab_row.add_child(self.render_tab_bar_buttons(&theme, cx))
1433 }
1434
1435 tab_row
1436 .constrained()
1437 .with_height(theme.workspace.tab_bar.height)
1438 .contained()
1439 .with_style(theme.workspace.tab_bar.container)
1440 .flex(1., false)
1441 .named("tab bar")
1442 })
1443 .with_child({
1444 enum PaneContentTabDropTarget {}
1445 dragged_item_receiver::<PaneContentTabDropTarget, _>(
1446 0,
1447 self.active_item_index + 1,
1448 false,
1449 if self.docked.is_some() {
1450 None
1451 } else {
1452 Some(100.)
1453 },
1454 cx,
1455 {
1456 let toolbar = self.toolbar.clone();
1457 move |_, cx| {
1458 Flex::column()
1459 .with_child(
1460 ChildView::new(&toolbar, cx).expanded().boxed(),
1461 )
1462 .with_child(
1463 ChildView::new(active_item, cx)
1464 .flex(1., true)
1465 .boxed(),
1466 )
1467 .boxed()
1468 }
1469 },
1470 )
1471 .flex(1., true)
1472 .boxed()
1473 })
1474 .boxed()
1475 } else {
1476 enum EmptyPane {}
1477 let theme = cx.global::<Settings>().theme.clone();
1478
1479 dragged_item_receiver::<EmptyPane, _>(0, 0, false, None, cx, |_, _| {
1480 Empty::new()
1481 .contained()
1482 .with_background_color(theme.workspace.background)
1483 .boxed()
1484 })
1485 .on_down(MouseButton::Left, |_, cx| {
1486 cx.focus_parent_view();
1487 })
1488 .boxed()
1489 }
1490 })
1491 .on_down(MouseButton::Navigate(NavigationDirection::Back), {
1492 let this = this.clone();
1493 move |_, cx| {
1494 cx.dispatch_action(GoBack {
1495 pane: Some(this.clone()),
1496 });
1497 }
1498 })
1499 .on_down(MouseButton::Navigate(NavigationDirection::Forward), {
1500 let this = this.clone();
1501 move |_, cx| {
1502 cx.dispatch_action(GoForward {
1503 pane: Some(this.clone()),
1504 })
1505 }
1506 })
1507 .boxed(),
1508 )
1509 .with_child(ChildView::new(&self.tab_bar_context_menu, cx).boxed())
1510 .named("pane")
1511 }
1512
1513 fn focus_in(&mut self, focused: AnyViewHandle, cx: &mut ViewContext<Self>) {
1514 if let Some(active_item) = self.active_item() {
1515 if cx.is_self_focused() {
1516 // Pane was focused directly. We need to either focus a view inside the active item,
1517 // or focus the active item itself
1518 if let Some(weak_last_focused_view) =
1519 self.last_focused_view_by_item.get(&active_item.id())
1520 {
1521 if let Some(last_focused_view) = weak_last_focused_view.upgrade(cx) {
1522 cx.focus(last_focused_view);
1523 return;
1524 } else {
1525 self.last_focused_view_by_item.remove(&active_item.id());
1526 }
1527 }
1528
1529 cx.focus(active_item);
1530 } else {
1531 self.last_focused_view_by_item
1532 .insert(active_item.id(), focused.downgrade());
1533 }
1534 }
1535 }
1536}
1537
1538fn tab_bar_button<A: Action>(
1539 index: usize,
1540 icon: &'static str,
1541 cx: &mut RenderContext<Pane>,
1542 action_builder: impl 'static + Fn(Vector2F) -> A,
1543) -> ElementBox {
1544 enum TabBarButton {}
1545
1546 MouseEventHandler::<TabBarButton>::new(index, cx, |mouse_state, cx| {
1547 let theme = &cx.global::<Settings>().theme.workspace.tab_bar;
1548 let style = theme.pane_button.style_for(mouse_state, false);
1549 Svg::new(icon)
1550 .with_color(style.color)
1551 .constrained()
1552 .with_width(style.icon_width)
1553 .aligned()
1554 .constrained()
1555 .with_width(style.button_width)
1556 .with_height(style.button_width)
1557 // .aligned()
1558 .boxed()
1559 })
1560 .with_cursor_style(CursorStyle::PointingHand)
1561 .on_click(MouseButton::Left, move |e, cx| {
1562 cx.dispatch_action(action_builder(e.region.lower_right()));
1563 })
1564 .flex(1., false)
1565 .boxed()
1566}
1567
1568impl ItemNavHistory {
1569 pub fn push<D: 'static + Any>(&self, data: Option<D>, cx: &mut MutableAppContext) {
1570 self.history.borrow_mut().push(data, self.item.clone(), cx);
1571 }
1572
1573 pub fn pop_backward(&self, cx: &mut MutableAppContext) -> Option<NavigationEntry> {
1574 self.history.borrow_mut().pop(NavigationMode::GoingBack, cx)
1575 }
1576
1577 pub fn pop_forward(&self, cx: &mut MutableAppContext) -> Option<NavigationEntry> {
1578 self.history
1579 .borrow_mut()
1580 .pop(NavigationMode::GoingForward, cx)
1581 }
1582}
1583
1584impl NavHistory {
1585 fn set_mode(&mut self, mode: NavigationMode) {
1586 self.mode = mode;
1587 }
1588
1589 fn disable(&mut self) {
1590 self.mode = NavigationMode::Disabled;
1591 }
1592
1593 fn enable(&mut self) {
1594 self.mode = NavigationMode::Normal;
1595 }
1596
1597 fn pop(&mut self, mode: NavigationMode, cx: &mut MutableAppContext) -> Option<NavigationEntry> {
1598 let entry = match mode {
1599 NavigationMode::Normal | NavigationMode::Disabled | NavigationMode::ClosingItem => {
1600 return None
1601 }
1602 NavigationMode::GoingBack => &mut self.backward_stack,
1603 NavigationMode::GoingForward => &mut self.forward_stack,
1604 NavigationMode::ReopeningClosedItem => &mut self.closed_stack,
1605 }
1606 .pop_back();
1607 if entry.is_some() {
1608 self.did_update(cx);
1609 }
1610 entry
1611 }
1612
1613 fn push<D: 'static + Any>(
1614 &mut self,
1615 data: Option<D>,
1616 item: Rc<dyn WeakItemHandle>,
1617 cx: &mut MutableAppContext,
1618 ) {
1619 match self.mode {
1620 NavigationMode::Disabled => {}
1621 NavigationMode::Normal | NavigationMode::ReopeningClosedItem => {
1622 if self.backward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
1623 self.backward_stack.pop_front();
1624 }
1625 self.backward_stack.push_back(NavigationEntry {
1626 item,
1627 data: data.map(|data| Box::new(data) as Box<dyn Any>),
1628 });
1629 self.forward_stack.clear();
1630 }
1631 NavigationMode::GoingBack => {
1632 if self.forward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
1633 self.forward_stack.pop_front();
1634 }
1635 self.forward_stack.push_back(NavigationEntry {
1636 item,
1637 data: data.map(|data| Box::new(data) as Box<dyn Any>),
1638 });
1639 }
1640 NavigationMode::GoingForward => {
1641 if self.backward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
1642 self.backward_stack.pop_front();
1643 }
1644 self.backward_stack.push_back(NavigationEntry {
1645 item,
1646 data: data.map(|data| Box::new(data) as Box<dyn Any>),
1647 });
1648 }
1649 NavigationMode::ClosingItem => {
1650 if self.closed_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
1651 self.closed_stack.pop_front();
1652 }
1653 self.closed_stack.push_back(NavigationEntry {
1654 item,
1655 data: data.map(|data| Box::new(data) as Box<dyn Any>),
1656 });
1657 }
1658 }
1659 self.did_update(cx);
1660 }
1661
1662 fn did_update(&self, cx: &mut MutableAppContext) {
1663 if let Some(pane) = self.pane.upgrade(cx) {
1664 cx.defer(move |cx| pane.update(cx, |pane, cx| pane.history_updated(cx)));
1665 }
1666 }
1667}
1668
1669#[cfg(test)]
1670mod tests {
1671 use std::sync::Arc;
1672
1673 use super::*;
1674 use crate::item::test::{TestItem, TestProjectItem};
1675 use gpui::{executor::Deterministic, TestAppContext};
1676 use project::FakeFs;
1677
1678 #[gpui::test]
1679 async fn test_add_item_with_new_item(cx: &mut TestAppContext) {
1680 cx.foreground().forbid_parking();
1681 Settings::test_async(cx);
1682 let fs = FakeFs::new(cx.background());
1683
1684 let project = Project::test(fs, None, cx).await;
1685 let (_, workspace) = cx.add_window(|cx| {
1686 Workspace::new(Default::default(), 0, project, |_, _| unimplemented!(), cx)
1687 });
1688 let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());
1689
1690 // 1. Add with a destination index
1691 // a. Add before the active item
1692 set_labeled_items(&workspace, &pane, ["A", "B*", "C"], cx);
1693 workspace.update(cx, |workspace, cx| {
1694 Pane::add_item(
1695 workspace,
1696 &pane,
1697 Box::new(cx.add_view(|_| TestItem::new().with_label("D"))),
1698 false,
1699 false,
1700 Some(0),
1701 cx,
1702 );
1703 });
1704 assert_item_labels(&pane, ["D*", "A", "B", "C"], cx);
1705
1706 // b. Add after the active item
1707 set_labeled_items(&workspace, &pane, ["A", "B*", "C"], cx);
1708 workspace.update(cx, |workspace, cx| {
1709 Pane::add_item(
1710 workspace,
1711 &pane,
1712 Box::new(cx.add_view(|_| TestItem::new().with_label("D"))),
1713 false,
1714 false,
1715 Some(2),
1716 cx,
1717 );
1718 });
1719 assert_item_labels(&pane, ["A", "B", "D*", "C"], cx);
1720
1721 // c. Add at the end of the item list (including off the length)
1722 set_labeled_items(&workspace, &pane, ["A", "B*", "C"], cx);
1723 workspace.update(cx, |workspace, cx| {
1724 Pane::add_item(
1725 workspace,
1726 &pane,
1727 Box::new(cx.add_view(|_| TestItem::new().with_label("D"))),
1728 false,
1729 false,
1730 Some(5),
1731 cx,
1732 );
1733 });
1734 assert_item_labels(&pane, ["A", "B", "C", "D*"], cx);
1735
1736 // 2. Add without a destination index
1737 // a. Add with active item at the start of the item list
1738 set_labeled_items(&workspace, &pane, ["A*", "B", "C"], cx);
1739 workspace.update(cx, |workspace, cx| {
1740 Pane::add_item(
1741 workspace,
1742 &pane,
1743 Box::new(cx.add_view(|_| TestItem::new().with_label("D"))),
1744 false,
1745 false,
1746 None,
1747 cx,
1748 );
1749 });
1750 set_labeled_items(&workspace, &pane, ["A", "D*", "B", "C"], cx);
1751
1752 // b. Add with active item at the end of the item list
1753 set_labeled_items(&workspace, &pane, ["A", "B", "C*"], cx);
1754 workspace.update(cx, |workspace, cx| {
1755 Pane::add_item(
1756 workspace,
1757 &pane,
1758 Box::new(cx.add_view(|_| TestItem::new().with_label("D"))),
1759 false,
1760 false,
1761 None,
1762 cx,
1763 );
1764 });
1765 assert_item_labels(&pane, ["A", "B", "C", "D*"], cx);
1766 }
1767
1768 #[gpui::test]
1769 async fn test_add_item_with_existing_item(cx: &mut TestAppContext) {
1770 cx.foreground().forbid_parking();
1771 Settings::test_async(cx);
1772 let fs = FakeFs::new(cx.background());
1773
1774 let project = Project::test(fs, None, cx).await;
1775 let (_, workspace) = cx.add_window(|cx| {
1776 Workspace::new(Default::default(), 0, project, |_, _| unimplemented!(), cx)
1777 });
1778 let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());
1779
1780 // 1. Add with a destination index
1781 // 1a. Add before the active item
1782 let [_, _, _, d] = set_labeled_items(&workspace, &pane, ["A", "B*", "C", "D"], cx);
1783 workspace.update(cx, |workspace, cx| {
1784 Pane::add_item(workspace, &pane, d, false, false, Some(0), cx);
1785 });
1786 assert_item_labels(&pane, ["D*", "A", "B", "C"], cx);
1787
1788 // 1b. Add after the active item
1789 let [_, _, _, d] = set_labeled_items(&workspace, &pane, ["A", "B*", "C", "D"], cx);
1790 workspace.update(cx, |workspace, cx| {
1791 Pane::add_item(workspace, &pane, d, false, false, Some(2), cx);
1792 });
1793 assert_item_labels(&pane, ["A", "B", "D*", "C"], cx);
1794
1795 // 1c. Add at the end of the item list (including off the length)
1796 let [a, _, _, _] = set_labeled_items(&workspace, &pane, ["A", "B*", "C", "D"], cx);
1797 workspace.update(cx, |workspace, cx| {
1798 Pane::add_item(workspace, &pane, a, false, false, Some(5), cx);
1799 });
1800 assert_item_labels(&pane, ["B", "C", "D", "A*"], cx);
1801
1802 // 1d. Add same item to active index
1803 let [_, b, _] = set_labeled_items(&workspace, &pane, ["A", "B*", "C"], cx);
1804 workspace.update(cx, |workspace, cx| {
1805 Pane::add_item(workspace, &pane, b, false, false, Some(1), cx);
1806 });
1807 assert_item_labels(&pane, ["A", "B*", "C"], cx);
1808
1809 // 1e. Add item to index after same item in last position
1810 let [_, _, c] = set_labeled_items(&workspace, &pane, ["A", "B*", "C"], cx);
1811 workspace.update(cx, |workspace, cx| {
1812 Pane::add_item(workspace, &pane, c, false, false, Some(2), cx);
1813 });
1814 assert_item_labels(&pane, ["A", "B", "C*"], cx);
1815
1816 // 2. Add without a destination index
1817 // 2a. Add with active item at the start of the item list
1818 let [_, _, _, d] = set_labeled_items(&workspace, &pane, ["A*", "B", "C", "D"], cx);
1819 workspace.update(cx, |workspace, cx| {
1820 Pane::add_item(workspace, &pane, d, false, false, None, cx);
1821 });
1822 assert_item_labels(&pane, ["A", "D*", "B", "C"], cx);
1823
1824 // 2b. Add with active item at the end of the item list
1825 let [a, _, _, _] = set_labeled_items(&workspace, &pane, ["A", "B", "C", "D*"], cx);
1826 workspace.update(cx, |workspace, cx| {
1827 Pane::add_item(workspace, &pane, a, false, false, None, cx);
1828 });
1829 assert_item_labels(&pane, ["B", "C", "D", "A*"], cx);
1830
1831 // 2c. Add active item to active item at end of list
1832 let [_, _, c] = set_labeled_items(&workspace, &pane, ["A", "B", "C*"], cx);
1833 workspace.update(cx, |workspace, cx| {
1834 Pane::add_item(workspace, &pane, c, false, false, None, cx);
1835 });
1836 assert_item_labels(&pane, ["A", "B", "C*"], cx);
1837
1838 // 2d. Add active item to active item at start of list
1839 let [a, _, _] = set_labeled_items(&workspace, &pane, ["A*", "B", "C"], cx);
1840 workspace.update(cx, |workspace, cx| {
1841 Pane::add_item(workspace, &pane, a, false, false, None, cx);
1842 });
1843 assert_item_labels(&pane, ["A*", "B", "C"], cx);
1844 }
1845
1846 #[gpui::test]
1847 async fn test_add_item_with_same_project_entries(cx: &mut TestAppContext) {
1848 cx.foreground().forbid_parking();
1849 Settings::test_async(cx);
1850 let fs = FakeFs::new(cx.background());
1851
1852 let project = Project::test(fs, None, cx).await;
1853 let (_, workspace) = cx.add_window(|cx| {
1854 Workspace::new(Default::default(), 0, project, |_, _| unimplemented!(), cx)
1855 });
1856 let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());
1857
1858 // singleton view
1859 workspace.update(cx, |workspace, cx| {
1860 let item = TestItem::new()
1861 .with_singleton(true)
1862 .with_label("buffer 1")
1863 .with_project_items(&[TestProjectItem::new(1, "one.txt", cx)]);
1864
1865 Pane::add_item(
1866 workspace,
1867 &pane,
1868 Box::new(cx.add_view(|_| item)),
1869 false,
1870 false,
1871 None,
1872 cx,
1873 );
1874 });
1875 assert_item_labels(&pane, ["buffer 1*"], cx);
1876
1877 // new singleton view with the same project entry
1878 workspace.update(cx, |workspace, cx| {
1879 let item = TestItem::new()
1880 .with_singleton(true)
1881 .with_label("buffer 1")
1882 .with_project_items(&[TestProjectItem::new(1, "1.txt", cx)]);
1883
1884 Pane::add_item(
1885 workspace,
1886 &pane,
1887 Box::new(cx.add_view(|_| item)),
1888 false,
1889 false,
1890 None,
1891 cx,
1892 );
1893 });
1894 assert_item_labels(&pane, ["buffer 1*"], cx);
1895
1896 // new singleton view with different project entry
1897 workspace.update(cx, |workspace, cx| {
1898 let item = TestItem::new()
1899 .with_singleton(true)
1900 .with_label("buffer 2")
1901 .with_project_items(&[TestProjectItem::new(2, "2.txt", cx)]);
1902
1903 Pane::add_item(
1904 workspace,
1905 &pane,
1906 Box::new(cx.add_view(|_| item)),
1907 false,
1908 false,
1909 None,
1910 cx,
1911 );
1912 });
1913 assert_item_labels(&pane, ["buffer 1", "buffer 2*"], cx);
1914
1915 // new multibuffer view with the same project entry
1916 workspace.update(cx, |workspace, cx| {
1917 let item = TestItem::new()
1918 .with_singleton(false)
1919 .with_label("multibuffer 1")
1920 .with_project_items(&[TestProjectItem::new(1, "1.txt", cx)]);
1921
1922 Pane::add_item(
1923 workspace,
1924 &pane,
1925 Box::new(cx.add_view(|_| item)),
1926 false,
1927 false,
1928 None,
1929 cx,
1930 );
1931 });
1932 assert_item_labels(&pane, ["buffer 1", "buffer 2", "multibuffer 1*"], cx);
1933
1934 // another multibuffer view with the same project entry
1935 workspace.update(cx, |workspace, cx| {
1936 let item = TestItem::new()
1937 .with_singleton(false)
1938 .with_label("multibuffer 1b")
1939 .with_project_items(&[TestProjectItem::new(1, "1.txt", cx)]);
1940
1941 Pane::add_item(
1942 workspace,
1943 &pane,
1944 Box::new(cx.add_view(|_| item)),
1945 false,
1946 false,
1947 None,
1948 cx,
1949 );
1950 });
1951 assert_item_labels(
1952 &pane,
1953 ["buffer 1", "buffer 2", "multibuffer 1", "multibuffer 1b*"],
1954 cx,
1955 );
1956 }
1957
1958 #[gpui::test]
1959 async fn test_remove_item_ordering(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
1960 Settings::test_async(cx);
1961 let fs = FakeFs::new(cx.background());
1962
1963 let project = Project::test(fs, None, cx).await;
1964 let (_, workspace) =
1965 cx.add_window(|cx| Workspace::new(None, 0, project, |_, _| unimplemented!(), cx));
1966 let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());
1967
1968 add_labled_item(&workspace, &pane, "A", cx);
1969 add_labled_item(&workspace, &pane, "B", cx);
1970 add_labled_item(&workspace, &pane, "C", cx);
1971 add_labled_item(&workspace, &pane, "D", cx);
1972 assert_item_labels(&pane, ["A", "B", "C", "D*"], cx);
1973
1974 pane.update(cx, |pane, cx| pane.activate_item(1, false, false, cx));
1975 add_labled_item(&workspace, &pane, "1", cx);
1976 assert_item_labels(&pane, ["A", "B", "1*", "C", "D"], cx);
1977
1978 workspace.update(cx, |workspace, cx| {
1979 Pane::close_active_item(workspace, &CloseActiveItem, cx);
1980 });
1981 deterministic.run_until_parked();
1982 assert_item_labels(&pane, ["A", "B*", "C", "D"], cx);
1983
1984 pane.update(cx, |pane, cx| pane.activate_item(3, false, false, cx));
1985 assert_item_labels(&pane, ["A", "B", "C", "D*"], cx);
1986
1987 workspace.update(cx, |workspace, cx| {
1988 Pane::close_active_item(workspace, &CloseActiveItem, cx);
1989 });
1990 deterministic.run_until_parked();
1991 assert_item_labels(&pane, ["A", "B*", "C"], cx);
1992
1993 workspace.update(cx, |workspace, cx| {
1994 Pane::close_active_item(workspace, &CloseActiveItem, cx);
1995 });
1996 deterministic.run_until_parked();
1997 assert_item_labels(&pane, ["A", "C*"], cx);
1998
1999 workspace.update(cx, |workspace, cx| {
2000 Pane::close_active_item(workspace, &CloseActiveItem, cx);
2001 });
2002 deterministic.run_until_parked();
2003 assert_item_labels(&pane, ["A*"], cx);
2004 }
2005
2006 fn add_labled_item(
2007 workspace: &ViewHandle<Workspace>,
2008 pane: &ViewHandle<Pane>,
2009 label: &str,
2010 cx: &mut TestAppContext,
2011 ) -> Box<ViewHandle<TestItem>> {
2012 workspace.update(cx, |workspace, cx| {
2013 let labeled_item = Box::new(cx.add_view(|_| TestItem::new().with_label(label)));
2014
2015 Pane::add_item(
2016 workspace,
2017 pane,
2018 labeled_item.clone(),
2019 false,
2020 false,
2021 None,
2022 cx,
2023 );
2024
2025 labeled_item
2026 })
2027 }
2028
2029 fn set_labeled_items<const COUNT: usize>(
2030 workspace: &ViewHandle<Workspace>,
2031 pane: &ViewHandle<Pane>,
2032 labels: [&str; COUNT],
2033 cx: &mut TestAppContext,
2034 ) -> [Box<ViewHandle<TestItem>>; COUNT] {
2035 pane.update(cx, |pane, _| {
2036 pane.items.clear();
2037 });
2038
2039 workspace.update(cx, |workspace, cx| {
2040 let mut active_item_index = 0;
2041
2042 let mut index = 0;
2043 let items = labels.map(|mut label| {
2044 if label.ends_with("*") {
2045 label = label.trim_end_matches("*");
2046 active_item_index = index;
2047 }
2048
2049 let labeled_item = Box::new(cx.add_view(|_| TestItem::new().with_label(label)));
2050 Pane::add_item(
2051 workspace,
2052 pane,
2053 labeled_item.clone(),
2054 false,
2055 false,
2056 None,
2057 cx,
2058 );
2059 index += 1;
2060 labeled_item
2061 });
2062
2063 pane.update(cx, |pane, cx| {
2064 pane.activate_item(active_item_index, false, false, cx)
2065 });
2066
2067 items
2068 })
2069 }
2070
2071 // Assert the item label, with the active item label suffixed with a '*'
2072 fn assert_item_labels<const COUNT: usize>(
2073 pane: &ViewHandle<Pane>,
2074 expected_states: [&str; COUNT],
2075 cx: &mut TestAppContext,
2076 ) {
2077 pane.read_with(cx, |pane, cx| {
2078 let actual_states = pane
2079 .items
2080 .iter()
2081 .enumerate()
2082 .map(|(ix, item)| {
2083 let mut state = item
2084 .to_any()
2085 .downcast::<TestItem>()
2086 .unwrap()
2087 .read(cx)
2088 .label
2089 .clone();
2090 if ix == pane.active_item_index {
2091 state.push('*');
2092 }
2093 state
2094 })
2095 .collect::<Vec<_>>();
2096
2097 assert_eq!(
2098 actual_states, expected_states,
2099 "pane items do not match expectation"
2100 );
2101 })
2102 }
2103}