1use super::{ItemHandle, SplitDirection};
2use crate::{Item, Settings, WeakItemHandle, Workspace};
3use collections::{HashMap, VecDeque};
4use gpui::{
5 action,
6 elements::*,
7 geometry::{rect::RectF, vector::vec2f},
8 keymap::Binding,
9 platform::{CursorStyle, NavigationDirection},
10 AnyViewHandle, AppContext, Entity, MutableAppContext, Quad, RenderContext, Task, View,
11 ViewContext, ViewHandle, WeakViewHandle,
12};
13use project::{ProjectEntryId, ProjectPath};
14use std::{
15 any::{Any, TypeId},
16 cell::RefCell,
17 cmp, mem,
18 rc::Rc,
19};
20use util::ResultExt;
21
22action!(Split, SplitDirection);
23action!(ActivateItem, usize);
24action!(ActivatePrevItem);
25action!(ActivateNextItem);
26action!(CloseActiveItem);
27action!(CloseInactiveItems);
28action!(CloseItem, usize);
29action!(GoBack, Option<WeakViewHandle<Pane>>);
30action!(GoForward, Option<WeakViewHandle<Pane>>);
31
32const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
33
34pub fn init(cx: &mut MutableAppContext) {
35 cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
36 pane.activate_item(action.0, cx);
37 });
38 cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
39 pane.activate_prev_item(cx);
40 });
41 cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| {
42 pane.activate_next_item(cx);
43 });
44 cx.add_action(|pane: &mut Pane, _: &CloseActiveItem, cx| {
45 pane.close_active_item(cx);
46 });
47 cx.add_action(|pane: &mut Pane, _: &CloseInactiveItems, cx| {
48 pane.close_inactive_items(cx);
49 });
50 cx.add_action(|pane: &mut Pane, action: &CloseItem, cx| {
51 pane.close_item(action.0, cx);
52 });
53 cx.add_action(|pane: &mut Pane, action: &Split, cx| {
54 pane.split(action.0, cx);
55 });
56 cx.add_action(|workspace: &mut Workspace, action: &GoBack, cx| {
57 Pane::go_back(
58 workspace,
59 action
60 .0
61 .as_ref()
62 .and_then(|weak_handle| weak_handle.upgrade(cx)),
63 cx,
64 )
65 .detach();
66 });
67 cx.add_action(|workspace: &mut Workspace, action: &GoForward, cx| {
68 Pane::go_forward(
69 workspace,
70 action
71 .0
72 .as_ref()
73 .and_then(|weak_handle| weak_handle.upgrade(cx)),
74 cx,
75 )
76 .detach();
77 });
78
79 cx.add_bindings(vec![
80 Binding::new("shift-cmd-{", ActivatePrevItem, Some("Pane")),
81 Binding::new("shift-cmd-}", ActivateNextItem, Some("Pane")),
82 Binding::new("cmd-w", CloseActiveItem, Some("Pane")),
83 Binding::new("alt-cmd-w", CloseInactiveItems, Some("Pane")),
84 Binding::new("cmd-k up", Split(SplitDirection::Up), Some("Pane")),
85 Binding::new("cmd-k down", Split(SplitDirection::Down), Some("Pane")),
86 Binding::new("cmd-k left", Split(SplitDirection::Left), Some("Pane")),
87 Binding::new("cmd-k right", Split(SplitDirection::Right), Some("Pane")),
88 Binding::new("ctrl--", GoBack(None), Some("Pane")),
89 Binding::new("shift-ctrl-_", GoForward(None), Some("Pane")),
90 ]);
91}
92
93pub enum Event {
94 Activate,
95 Remove,
96 Split(SplitDirection),
97}
98
99pub struct Pane {
100 items: Vec<Box<dyn ItemHandle>>,
101 active_item_index: usize,
102 nav_history: Rc<RefCell<NavHistory>>,
103 toolbars: HashMap<TypeId, Box<dyn ToolbarHandle>>,
104 active_toolbar_type: Option<TypeId>,
105 active_toolbar_visible: bool,
106}
107
108pub trait Toolbar: View {
109 fn active_item_changed(
110 &mut self,
111 item: Option<Box<dyn ItemHandle>>,
112 cx: &mut ViewContext<Self>,
113 ) -> bool;
114 fn on_dismiss(&mut self, cx: &mut ViewContext<Self>);
115}
116
117trait ToolbarHandle {
118 fn active_item_changed(
119 &self,
120 item: Option<Box<dyn ItemHandle>>,
121 cx: &mut MutableAppContext,
122 ) -> bool;
123 fn on_dismiss(&self, cx: &mut MutableAppContext);
124 fn to_any(&self) -> AnyViewHandle;
125}
126
127pub struct ItemNavHistory {
128 history: Rc<RefCell<NavHistory>>,
129 item: Rc<dyn WeakItemHandle>,
130}
131
132#[derive(Default)]
133pub struct NavHistory {
134 mode: NavigationMode,
135 backward_stack: VecDeque<NavigationEntry>,
136 forward_stack: VecDeque<NavigationEntry>,
137 paths_by_item: HashMap<usize, ProjectPath>,
138}
139
140#[derive(Copy, Clone)]
141enum NavigationMode {
142 Normal,
143 GoingBack,
144 GoingForward,
145 Disabled,
146}
147
148impl Default for NavigationMode {
149 fn default() -> Self {
150 Self::Normal
151 }
152}
153
154pub struct NavigationEntry {
155 pub item: Rc<dyn WeakItemHandle>,
156 pub data: Option<Box<dyn Any>>,
157}
158
159impl Pane {
160 pub fn new() -> Self {
161 Self {
162 items: Vec::new(),
163 active_item_index: 0,
164 nav_history: Default::default(),
165 toolbars: Default::default(),
166 active_toolbar_type: Default::default(),
167 active_toolbar_visible: false,
168 }
169 }
170
171 pub fn nav_history(&self) -> &Rc<RefCell<NavHistory>> {
172 &self.nav_history
173 }
174
175 pub fn activate(&self, cx: &mut ViewContext<Self>) {
176 cx.emit(Event::Activate);
177 }
178
179 pub fn go_back(
180 workspace: &mut Workspace,
181 pane: Option<ViewHandle<Pane>>,
182 cx: &mut ViewContext<Workspace>,
183 ) -> Task<()> {
184 Self::navigate_history(
185 workspace,
186 pane.unwrap_or_else(|| workspace.active_pane().clone()),
187 NavigationMode::GoingBack,
188 cx,
189 )
190 }
191
192 pub fn go_forward(
193 workspace: &mut Workspace,
194 pane: Option<ViewHandle<Pane>>,
195 cx: &mut ViewContext<Workspace>,
196 ) -> Task<()> {
197 Self::navigate_history(
198 workspace,
199 pane.unwrap_or_else(|| workspace.active_pane().clone()),
200 NavigationMode::GoingForward,
201 cx,
202 )
203 }
204
205 fn navigate_history(
206 workspace: &mut Workspace,
207 pane: ViewHandle<Pane>,
208 mode: NavigationMode,
209 cx: &mut ViewContext<Workspace>,
210 ) -> Task<()> {
211 workspace.activate_pane(pane.clone(), cx);
212
213 let to_load = pane.update(cx, |pane, cx| {
214 // Retrieve the weak item handle from the history.
215 let entry = pane.nav_history.borrow_mut().pop(mode)?;
216
217 // If the item is still present in this pane, then activate it.
218 if let Some(index) = entry
219 .item
220 .upgrade(cx)
221 .and_then(|v| pane.index_for_item(v.as_ref()))
222 {
223 if let Some(item) = pane.active_item() {
224 pane.nav_history.borrow_mut().set_mode(mode);
225 item.deactivated(cx);
226 pane.nav_history
227 .borrow_mut()
228 .set_mode(NavigationMode::Normal);
229 }
230
231 pane.active_item_index = index;
232 pane.focus_active_item(cx);
233 if let Some(data) = entry.data {
234 pane.active_item()?.navigate(data, cx);
235 }
236 cx.notify();
237 None
238 }
239 // If the item is no longer present in this pane, then retrieve its
240 // project path in order to reopen it.
241 else {
242 pane.nav_history
243 .borrow_mut()
244 .paths_by_item
245 .get(&entry.item.id())
246 .cloned()
247 .map(|project_path| (project_path, entry))
248 }
249 });
250
251 if let Some((project_path, entry)) = to_load {
252 // If the item was no longer present, then load it again from its previous path.
253 let pane = pane.downgrade();
254 let task = workspace.load_path(project_path, cx);
255 cx.spawn(|workspace, mut cx| async move {
256 let task = task.await;
257 if let Some(pane) = pane.upgrade(&cx) {
258 if let Some((project_entry_id, build_item)) = task.log_err() {
259 pane.update(&mut cx, |pane, _| {
260 pane.nav_history.borrow_mut().set_mode(mode);
261 });
262 let item = workspace.update(&mut cx, |workspace, cx| {
263 Self::open_item(
264 workspace,
265 pane.clone(),
266 project_entry_id,
267 cx,
268 build_item,
269 )
270 });
271 pane.update(&mut cx, |pane, cx| {
272 pane.nav_history
273 .borrow_mut()
274 .set_mode(NavigationMode::Normal);
275 if let Some(data) = entry.data {
276 item.navigate(data, cx);
277 }
278 });
279 } else {
280 workspace
281 .update(&mut cx, |workspace, cx| {
282 Self::navigate_history(workspace, pane, mode, cx)
283 })
284 .await;
285 }
286 }
287 })
288 } else {
289 Task::ready(())
290 }
291 }
292
293 pub(crate) fn open_item(
294 workspace: &mut Workspace,
295 pane: ViewHandle<Pane>,
296 project_entry_id: ProjectEntryId,
297 cx: &mut ViewContext<Workspace>,
298 build_item: impl FnOnce(&mut MutableAppContext) -> Box<dyn ItemHandle>,
299 ) -> Box<dyn ItemHandle> {
300 let existing_item = pane.update(cx, |pane, cx| {
301 for (ix, item) in pane.items.iter().enumerate() {
302 if item.project_entry_id(cx) == Some(project_entry_id) {
303 let item = item.boxed_clone();
304 pane.activate_item(ix, cx);
305 return Some(item);
306 }
307 }
308 None
309 });
310 if let Some(existing_item) = existing_item {
311 existing_item
312 } else {
313 let item = build_item(cx);
314 Self::add_item(workspace, pane, item.boxed_clone(), cx);
315 item
316 }
317 }
318
319 pub(crate) fn add_item(
320 workspace: &mut Workspace,
321 pane: ViewHandle<Pane>,
322 item: Box<dyn ItemHandle>,
323 cx: &mut ViewContext<Workspace>,
324 ) {
325 // Prevent adding the same item to the pane more than once.
326 if let Some(item_ix) = pane.read(cx).items.iter().position(|i| i.id() == item.id()) {
327 pane.update(cx, |pane, cx| pane.activate_item(item_ix, cx));
328 return;
329 }
330
331 item.set_nav_history(pane.read(cx).nav_history.clone(), cx);
332 item.added_to_pane(workspace, pane.clone(), cx);
333 pane.update(cx, |pane, cx| {
334 let item_idx = cmp::min(pane.active_item_index + 1, pane.items.len());
335 pane.items.insert(item_idx, item);
336 pane.activate_item(item_idx, cx);
337 cx.notify();
338 });
339 }
340
341 pub fn items(&self) -> impl Iterator<Item = &Box<dyn ItemHandle>> {
342 self.items.iter()
343 }
344
345 pub fn items_of_type<'a, T: View>(&'a self) -> impl 'a + Iterator<Item = ViewHandle<T>> {
346 self.items
347 .iter()
348 .filter_map(|item| item.to_any().downcast())
349 }
350
351 pub fn active_item(&self) -> Option<Box<dyn ItemHandle>> {
352 self.items.get(self.active_item_index).cloned()
353 }
354
355 pub fn project_entry_id_for_item(
356 &self,
357 item: &dyn ItemHandle,
358 cx: &AppContext,
359 ) -> Option<ProjectEntryId> {
360 self.items.iter().find_map(|existing| {
361 if existing.id() == item.id() {
362 existing.project_entry_id(cx)
363 } else {
364 None
365 }
366 })
367 }
368
369 pub fn item_for_entry(
370 &self,
371 entry_id: ProjectEntryId,
372 cx: &AppContext,
373 ) -> Option<Box<dyn ItemHandle>> {
374 self.items.iter().find_map(|item| {
375 if item.project_entry_id(cx) == Some(entry_id) {
376 Some(item.boxed_clone())
377 } else {
378 None
379 }
380 })
381 }
382
383 pub fn index_for_item(&self, item: &dyn ItemHandle) -> Option<usize> {
384 self.items.iter().position(|i| i.id() == item.id())
385 }
386
387 pub fn activate_item(&mut self, index: usize, cx: &mut ViewContext<Self>) {
388 if index < self.items.len() {
389 let prev_active_item_ix = mem::replace(&mut self.active_item_index, index);
390 if prev_active_item_ix != self.active_item_index
391 && prev_active_item_ix < self.items.len()
392 {
393 self.items[prev_active_item_ix].deactivated(cx);
394 }
395 self.update_active_toolbar(cx);
396 self.focus_active_item(cx);
397 self.activate(cx);
398 cx.notify();
399 }
400 }
401
402 pub fn activate_prev_item(&mut self, cx: &mut ViewContext<Self>) {
403 let mut index = self.active_item_index;
404 if index > 0 {
405 index -= 1;
406 } else if self.items.len() > 0 {
407 index = self.items.len() - 1;
408 }
409 self.activate_item(index, cx);
410 }
411
412 pub fn activate_next_item(&mut self, cx: &mut ViewContext<Self>) {
413 let mut index = self.active_item_index;
414 if index + 1 < self.items.len() {
415 index += 1;
416 } else {
417 index = 0;
418 }
419 self.activate_item(index, cx);
420 }
421
422 pub fn close_active_item(&mut self, cx: &mut ViewContext<Self>) {
423 if !self.items.is_empty() {
424 self.close_item(self.items[self.active_item_index].id(), cx)
425 }
426 }
427
428 pub fn close_inactive_items(&mut self, cx: &mut ViewContext<Self>) {
429 if !self.items.is_empty() {
430 let active_item_id = self.items[self.active_item_index].id();
431 self.close_items(cx, |id| id != active_item_id);
432 }
433 }
434
435 pub fn close_item(&mut self, view_id_to_close: usize, cx: &mut ViewContext<Self>) {
436 self.close_items(cx, |view_id| view_id == view_id_to_close);
437 }
438
439 pub fn close_items(
440 &mut self,
441 cx: &mut ViewContext<Self>,
442 should_close: impl Fn(usize) -> bool,
443 ) {
444 let mut item_ix = 0;
445 let mut new_active_item_index = self.active_item_index;
446 self.items.retain(|item| {
447 if should_close(item.id()) {
448 if item_ix == self.active_item_index {
449 item.deactivated(cx);
450 }
451
452 if item_ix < self.active_item_index {
453 new_active_item_index -= 1;
454 }
455
456 let mut nav_history = self.nav_history.borrow_mut();
457 if let Some(path) = item.project_path(cx) {
458 nav_history.paths_by_item.insert(item.id(), path);
459 } else {
460 nav_history.paths_by_item.remove(&item.id());
461 }
462
463 item_ix += 1;
464 false
465 } else {
466 item_ix += 1;
467 true
468 }
469 });
470
471 if self.items.is_empty() {
472 cx.emit(Event::Remove);
473 } else {
474 self.active_item_index = cmp::min(new_active_item_index, self.items.len() - 1);
475 self.focus_active_item(cx);
476 self.activate(cx);
477 }
478 self.update_active_toolbar(cx);
479
480 cx.notify();
481 }
482
483 fn focus_active_item(&mut self, cx: &mut ViewContext<Self>) {
484 if let Some(active_item) = self.active_item() {
485 cx.focus(active_item);
486 }
487 }
488
489 pub fn split(&mut self, direction: SplitDirection, cx: &mut ViewContext<Self>) {
490 cx.emit(Event::Split(direction));
491 }
492
493 pub fn show_toolbar<F, V>(&mut self, cx: &mut ViewContext<Self>, build_toolbar: F)
494 where
495 F: FnOnce(&mut ViewContext<V>) -> V,
496 V: Toolbar,
497 {
498 let type_id = TypeId::of::<V>();
499 if self.active_toolbar_type != Some(type_id) {
500 self.dismiss_toolbar(cx);
501
502 let active_item = self.active_item();
503 self.toolbars
504 .entry(type_id)
505 .or_insert_with(|| Box::new(cx.add_view(build_toolbar)));
506
507 self.active_toolbar_type = Some(type_id);
508 self.active_toolbar_visible =
509 self.toolbars[&type_id].active_item_changed(active_item, cx);
510 cx.notify();
511 }
512 }
513
514 pub fn dismiss_toolbar(&mut self, cx: &mut ViewContext<Self>) {
515 if let Some(active_toolbar_type) = self.active_toolbar_type.take() {
516 self.toolbars
517 .get_mut(&active_toolbar_type)
518 .unwrap()
519 .on_dismiss(cx);
520 self.active_toolbar_visible = false;
521 self.focus_active_item(cx);
522 cx.notify();
523 }
524 }
525
526 pub fn toolbar<T: Toolbar>(&self) -> Option<ViewHandle<T>> {
527 self.toolbars
528 .get(&TypeId::of::<T>())
529 .and_then(|toolbar| toolbar.to_any().downcast())
530 }
531
532 pub fn active_toolbar(&self) -> Option<AnyViewHandle> {
533 let type_id = self.active_toolbar_type?;
534 let toolbar = self.toolbars.get(&type_id)?;
535 if self.active_toolbar_visible {
536 Some(toolbar.to_any())
537 } else {
538 None
539 }
540 }
541
542 fn update_active_toolbar(&mut self, cx: &mut ViewContext<Self>) {
543 let active_item = self.items.get(self.active_item_index);
544 for (toolbar_type_id, toolbar) in &self.toolbars {
545 let visible = toolbar.active_item_changed(active_item.cloned(), cx);
546 if Some(*toolbar_type_id) == self.active_toolbar_type {
547 self.active_toolbar_visible = visible;
548 }
549 }
550 }
551
552 fn render_tabs(&self, cx: &mut RenderContext<Self>) -> ElementBox {
553 let theme = cx.global::<Settings>().theme.clone();
554
555 enum Tabs {}
556 let tabs = MouseEventHandler::new::<Tabs, _, _>(0, cx, |mouse_state, cx| {
557 let mut row = Flex::row();
558 for (ix, item) in self.items.iter().enumerate() {
559 let is_active = ix == self.active_item_index;
560
561 row.add_child({
562 let tab_style = if is_active {
563 theme.workspace.active_tab.clone()
564 } else {
565 theme.workspace.tab.clone()
566 };
567 let title = item.tab_content(&tab_style, cx);
568
569 let mut style = if is_active {
570 theme.workspace.active_tab.clone()
571 } else {
572 theme.workspace.tab.clone()
573 };
574 if ix == 0 {
575 style.container.border.left = false;
576 }
577
578 EventHandler::new(
579 Container::new(
580 Flex::row()
581 .with_child(
582 Align::new({
583 let diameter = 7.0;
584 let icon_color = if item.has_conflict(cx) {
585 Some(style.icon_conflict)
586 } else if item.is_dirty(cx) {
587 Some(style.icon_dirty)
588 } else {
589 None
590 };
591
592 ConstrainedBox::new(
593 Canvas::new(move |bounds, _, cx| {
594 if let Some(color) = icon_color {
595 let square = RectF::new(
596 bounds.origin(),
597 vec2f(diameter, diameter),
598 );
599 cx.scene.push_quad(Quad {
600 bounds: square,
601 background: Some(color),
602 border: Default::default(),
603 corner_radius: diameter / 2.,
604 });
605 }
606 })
607 .boxed(),
608 )
609 .with_width(diameter)
610 .with_height(diameter)
611 .boxed()
612 })
613 .boxed(),
614 )
615 .with_child(
616 Container::new(Align::new(title).boxed())
617 .with_style(ContainerStyle {
618 margin: Margin {
619 left: style.spacing,
620 right: style.spacing,
621 ..Default::default()
622 },
623 ..Default::default()
624 })
625 .boxed(),
626 )
627 .with_child(
628 Align::new(
629 ConstrainedBox::new(if mouse_state.hovered {
630 let item_id = item.id();
631 enum TabCloseButton {}
632 let icon = Svg::new("icons/x.svg");
633 MouseEventHandler::new::<TabCloseButton, _, _>(
634 item_id,
635 cx,
636 |mouse_state, _| {
637 if mouse_state.hovered {
638 icon.with_color(style.icon_close_active)
639 .boxed()
640 } else {
641 icon.with_color(style.icon_close).boxed()
642 }
643 },
644 )
645 .with_padding(Padding::uniform(4.))
646 .with_cursor_style(CursorStyle::PointingHand)
647 .on_click(move |cx| {
648 cx.dispatch_action(CloseItem(item_id))
649 })
650 .named("close-tab-icon")
651 } else {
652 Empty::new().boxed()
653 })
654 .with_width(style.icon_width)
655 .boxed(),
656 )
657 .boxed(),
658 )
659 .boxed(),
660 )
661 .with_style(style.container)
662 .boxed(),
663 )
664 .on_mouse_down(move |cx| {
665 cx.dispatch_action(ActivateItem(ix));
666 true
667 })
668 .boxed()
669 })
670 }
671
672 row.add_child(
673 Empty::new()
674 .contained()
675 .with_border(theme.workspace.tab.container.border)
676 .flexible(0., true)
677 .named("filler"),
678 );
679
680 row.boxed()
681 });
682
683 ConstrainedBox::new(tabs.boxed())
684 .with_height(theme.workspace.tab.height)
685 .named("tabs")
686 }
687}
688
689impl Entity for Pane {
690 type Event = Event;
691}
692
693impl View for Pane {
694 fn ui_name() -> &'static str {
695 "Pane"
696 }
697
698 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
699 let this = cx.handle();
700
701 EventHandler::new(if let Some(active_item) = self.active_item() {
702 Flex::column()
703 .with_child(self.render_tabs(cx))
704 .with_children(
705 self.active_toolbar()
706 .as_ref()
707 .map(|view| ChildView::new(view).boxed()),
708 )
709 .with_child(ChildView::new(active_item).flexible(1., true).boxed())
710 .boxed()
711 } else {
712 Empty::new().boxed()
713 })
714 .on_navigate_mouse_down(move |direction, cx| {
715 let this = this.clone();
716 match direction {
717 NavigationDirection::Back => cx.dispatch_action(GoBack(Some(this))),
718 NavigationDirection::Forward => cx.dispatch_action(GoForward(Some(this))),
719 }
720
721 true
722 })
723 .named("pane")
724 }
725
726 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
727 self.focus_active_item(cx);
728 }
729}
730
731impl<T: Toolbar> ToolbarHandle for ViewHandle<T> {
732 fn active_item_changed(
733 &self,
734 item: Option<Box<dyn ItemHandle>>,
735 cx: &mut MutableAppContext,
736 ) -> bool {
737 self.update(cx, |this, cx| this.active_item_changed(item, cx))
738 }
739
740 fn on_dismiss(&self, cx: &mut MutableAppContext) {
741 self.update(cx, |this, cx| this.on_dismiss(cx));
742 }
743
744 fn to_any(&self) -> AnyViewHandle {
745 self.into()
746 }
747}
748
749impl ItemNavHistory {
750 pub fn new<T: Item>(history: Rc<RefCell<NavHistory>>, item: &ViewHandle<T>) -> Self {
751 Self {
752 history,
753 item: Rc::new(item.downgrade()),
754 }
755 }
756
757 pub fn history(&self) -> Rc<RefCell<NavHistory>> {
758 self.history.clone()
759 }
760
761 pub fn push<D: 'static + Any>(&self, data: Option<D>) {
762 self.history.borrow_mut().push(data, self.item.clone());
763 }
764}
765
766impl NavHistory {
767 pub fn disable(&mut self) {
768 self.mode = NavigationMode::Disabled;
769 }
770
771 pub fn enable(&mut self) {
772 self.mode = NavigationMode::Normal;
773 }
774
775 pub fn pop_backward(&mut self) -> Option<NavigationEntry> {
776 self.backward_stack.pop_back()
777 }
778
779 pub fn pop_forward(&mut self) -> Option<NavigationEntry> {
780 self.forward_stack.pop_back()
781 }
782
783 fn pop(&mut self, mode: NavigationMode) -> Option<NavigationEntry> {
784 match mode {
785 NavigationMode::Normal | NavigationMode::Disabled => None,
786 NavigationMode::GoingBack => self.pop_backward(),
787 NavigationMode::GoingForward => self.pop_forward(),
788 }
789 }
790
791 fn set_mode(&mut self, mode: NavigationMode) {
792 self.mode = mode;
793 }
794
795 pub fn push<D: 'static + Any>(&mut self, data: Option<D>, item: Rc<dyn WeakItemHandle>) {
796 match self.mode {
797 NavigationMode::Disabled => {}
798 NavigationMode::Normal => {
799 if self.backward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
800 self.backward_stack.pop_front();
801 }
802 self.backward_stack.push_back(NavigationEntry {
803 item,
804 data: data.map(|data| Box::new(data) as Box<dyn Any>),
805 });
806 self.forward_stack.clear();
807 }
808 NavigationMode::GoingBack => {
809 if self.forward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
810 self.forward_stack.pop_front();
811 }
812 self.forward_stack.push_back(NavigationEntry {
813 item,
814 data: data.map(|data| Box::new(data) as Box<dyn Any>),
815 });
816 }
817 NavigationMode::GoingForward => {
818 if self.backward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
819 self.backward_stack.pop_front();
820 }
821 self.backward_stack.push_back(NavigationEntry {
822 item,
823 data: data.map(|data| Box::new(data) as Box<dyn Any>),
824 });
825 }
826 }
827 }
828}