1use super::{ItemViewHandle, SplitDirection};
2use crate::{ItemView, Settings, WeakItemViewHandle, 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, Entity, MutableAppContext, Quad, RenderContext, Task, View, ViewContext,
11 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 item_views: Vec<(Option<ProjectEntryId>, Box<dyn ItemViewHandle>)>,
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 ItemViewHandle>>,
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 ItemViewHandle>>,
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_view: Rc<dyn WeakItemViewHandle>,
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_view: Rc<dyn WeakItemViewHandle>,
156 pub data: Option<Box<dyn Any>>,
157}
158
159impl Pane {
160 pub fn new() -> Self {
161 Self {
162 item_views: 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_view
220 .upgrade(cx)
221 .and_then(|v| pane.index_for_item_view(v.as_ref()))
222 {
223 if let Some(item_view) = pane.active_item() {
224 pane.nav_history.borrow_mut().set_mode(mode);
225 item_view.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_view.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, cx| {
260 pane.nav_history.borrow_mut().set_mode(mode);
261 let item = pane.open_item(project_entry_id, cx, build_item);
262 pane.nav_history
263 .borrow_mut()
264 .set_mode(NavigationMode::Normal);
265 if let Some(data) = entry.data {
266 item.navigate(data, cx);
267 }
268 });
269 } else {
270 workspace
271 .update(&mut cx, |workspace, cx| {
272 Self::navigate_history(workspace, pane, mode, cx)
273 })
274 .await;
275 }
276 }
277 })
278 } else {
279 Task::ready(())
280 }
281 }
282
283 pub(crate) fn open_item(
284 &mut self,
285 project_entry_id: ProjectEntryId,
286 cx: &mut ViewContext<Self>,
287 build_editor: impl FnOnce(&mut MutableAppContext) -> Box<dyn ItemViewHandle>,
288 ) -> Box<dyn ItemViewHandle> {
289 for (ix, (existing_entry_id, item_view)) in self.item_views.iter().enumerate() {
290 if *existing_entry_id == Some(project_entry_id) {
291 let item_view = item_view.boxed_clone();
292 self.activate_item(ix, cx);
293 return item_view;
294 }
295 }
296
297 let item_view = build_editor(cx);
298 self.add_item(Some(project_entry_id), item_view.boxed_clone(), cx);
299 item_view
300 }
301
302 pub(crate) fn add_item(
303 &mut self,
304 project_entry_id: Option<ProjectEntryId>,
305 mut item: Box<dyn ItemViewHandle>,
306 cx: &mut ViewContext<Self>,
307 ) {
308 item.set_nav_history(self.nav_history.clone(), cx);
309 item.added_to_pane(cx);
310 let item_idx = cmp::min(self.active_item_index + 1, self.item_views.len());
311 self.item_views.insert(item_idx, (project_entry_id, item));
312 self.activate_item(item_idx, cx);
313 cx.notify();
314 }
315
316 pub fn item_views(&self) -> impl Iterator<Item = &Box<dyn ItemViewHandle>> {
317 self.item_views.iter().map(|(_, view)| view)
318 }
319
320 pub fn active_item(&self) -> Option<Box<dyn ItemViewHandle>> {
321 self.item_views
322 .get(self.active_item_index)
323 .map(|(_, view)| view.clone())
324 }
325
326 pub fn project_entry_id_for_item(&self, item: &dyn ItemViewHandle) -> Option<ProjectEntryId> {
327 self.item_views.iter().find_map(|(entry_id, existing)| {
328 if existing.id() == item.id() {
329 *entry_id
330 } else {
331 None
332 }
333 })
334 }
335
336 pub fn item_for_entry(&self, entry_id: ProjectEntryId) -> Option<Box<dyn ItemViewHandle>> {
337 self.item_views.iter().find_map(|(id, view)| {
338 if *id == Some(entry_id) {
339 Some(view.boxed_clone())
340 } else {
341 None
342 }
343 })
344 }
345
346 pub fn index_for_item_view(&self, item_view: &dyn ItemViewHandle) -> Option<usize> {
347 self.item_views
348 .iter()
349 .position(|(_, i)| i.id() == item_view.id())
350 }
351
352 pub fn index_for_item(&self, item: &dyn ItemViewHandle) -> Option<usize> {
353 self.item_views
354 .iter()
355 .position(|(_, my_item)| my_item.id() == item.id())
356 }
357
358 pub fn activate_item(&mut self, index: usize, cx: &mut ViewContext<Self>) {
359 if index < self.item_views.len() {
360 let prev_active_item_ix = mem::replace(&mut self.active_item_index, index);
361 if prev_active_item_ix != self.active_item_index
362 && prev_active_item_ix < self.item_views.len()
363 {
364 self.item_views[prev_active_item_ix].1.deactivated(cx);
365 }
366 self.update_active_toolbar(cx);
367 self.focus_active_item(cx);
368 self.activate(cx);
369 cx.notify();
370 }
371 }
372
373 pub fn activate_prev_item(&mut self, cx: &mut ViewContext<Self>) {
374 let mut index = self.active_item_index;
375 if index > 0 {
376 index -= 1;
377 } else if self.item_views.len() > 0 {
378 index = self.item_views.len() - 1;
379 }
380 self.activate_item(index, cx);
381 }
382
383 pub fn activate_next_item(&mut self, cx: &mut ViewContext<Self>) {
384 let mut index = self.active_item_index;
385 if index + 1 < self.item_views.len() {
386 index += 1;
387 } else {
388 index = 0;
389 }
390 self.activate_item(index, cx);
391 }
392
393 pub fn close_active_item(&mut self, cx: &mut ViewContext<Self>) {
394 if !self.item_views.is_empty() {
395 self.close_item(self.item_views[self.active_item_index].1.id(), cx)
396 }
397 }
398
399 pub fn close_inactive_items(&mut self, cx: &mut ViewContext<Self>) {
400 if !self.item_views.is_empty() {
401 let active_item_id = self.item_views[self.active_item_index].1.id();
402 self.close_items(cx, |id| id != active_item_id);
403 }
404 }
405
406 pub fn close_item(&mut self, view_id_to_close: usize, cx: &mut ViewContext<Self>) {
407 self.close_items(cx, |view_id| view_id == view_id_to_close);
408 }
409
410 pub fn close_items(
411 &mut self,
412 cx: &mut ViewContext<Self>,
413 should_close: impl Fn(usize) -> bool,
414 ) {
415 let mut item_ix = 0;
416 let mut new_active_item_index = self.active_item_index;
417 self.item_views.retain(|(_, item_view)| {
418 if should_close(item_view.id()) {
419 if item_ix == self.active_item_index {
420 item_view.deactivated(cx);
421 }
422
423 if item_ix < self.active_item_index {
424 new_active_item_index -= 1;
425 }
426
427 let mut nav_history = self.nav_history.borrow_mut();
428 if let Some(path) = item_view.project_path(cx) {
429 nav_history.paths_by_item.insert(item_view.id(), path);
430 } else {
431 nav_history.paths_by_item.remove(&item_view.id());
432 }
433
434 item_ix += 1;
435 false
436 } else {
437 item_ix += 1;
438 true
439 }
440 });
441
442 if self.item_views.is_empty() {
443 cx.emit(Event::Remove);
444 } else {
445 self.active_item_index = cmp::min(new_active_item_index, self.item_views.len() - 1);
446 self.focus_active_item(cx);
447 self.activate(cx);
448 }
449 self.update_active_toolbar(cx);
450
451 cx.notify();
452 }
453
454 fn focus_active_item(&mut self, cx: &mut ViewContext<Self>) {
455 if let Some(active_item) = self.active_item() {
456 cx.focus(active_item);
457 }
458 }
459
460 pub fn split(&mut self, direction: SplitDirection, cx: &mut ViewContext<Self>) {
461 cx.emit(Event::Split(direction));
462 }
463
464 pub fn show_toolbar<F, V>(&mut self, cx: &mut ViewContext<Self>, build_toolbar: F)
465 where
466 F: FnOnce(&mut ViewContext<V>) -> V,
467 V: Toolbar,
468 {
469 let type_id = TypeId::of::<V>();
470 if self.active_toolbar_type != Some(type_id) {
471 self.dismiss_toolbar(cx);
472
473 let active_item = self.active_item();
474 self.toolbars
475 .entry(type_id)
476 .or_insert_with(|| Box::new(cx.add_view(build_toolbar)));
477
478 self.active_toolbar_type = Some(type_id);
479 self.active_toolbar_visible =
480 self.toolbars[&type_id].active_item_changed(active_item, cx);
481 cx.notify();
482 }
483 }
484
485 pub fn dismiss_toolbar(&mut self, cx: &mut ViewContext<Self>) {
486 if let Some(active_toolbar_type) = self.active_toolbar_type.take() {
487 self.toolbars
488 .get_mut(&active_toolbar_type)
489 .unwrap()
490 .on_dismiss(cx);
491 self.active_toolbar_visible = false;
492 self.focus_active_item(cx);
493 cx.notify();
494 }
495 }
496
497 pub fn toolbar<T: Toolbar>(&self) -> Option<ViewHandle<T>> {
498 self.toolbars
499 .get(&TypeId::of::<T>())
500 .and_then(|toolbar| toolbar.to_any().downcast())
501 }
502
503 pub fn active_toolbar(&self) -> Option<AnyViewHandle> {
504 let type_id = self.active_toolbar_type?;
505 let toolbar = self.toolbars.get(&type_id)?;
506 if self.active_toolbar_visible {
507 Some(toolbar.to_any())
508 } else {
509 None
510 }
511 }
512
513 fn update_active_toolbar(&mut self, cx: &mut ViewContext<Self>) {
514 let active_item = self.item_views.get(self.active_item_index);
515 for (toolbar_type_id, toolbar) in &self.toolbars {
516 let visible = toolbar.active_item_changed(active_item.map(|i| i.1.clone()), cx);
517 if Some(*toolbar_type_id) == self.active_toolbar_type {
518 self.active_toolbar_visible = visible;
519 }
520 }
521 }
522
523 fn render_tabs(&self, cx: &mut RenderContext<Self>) -> ElementBox {
524 let theme = cx.app_state::<Settings>().theme.clone();
525
526 enum Tabs {}
527 let tabs = MouseEventHandler::new::<Tabs, _, _>(0, cx, |mouse_state, cx| {
528 let mut row = Flex::row();
529 for (ix, (_, item_view)) in self.item_views.iter().enumerate() {
530 let is_active = ix == self.active_item_index;
531
532 row.add_child({
533 let tab_style = if is_active {
534 theme.workspace.active_tab.clone()
535 } else {
536 theme.workspace.tab.clone()
537 };
538 let title = item_view.tab_content(&tab_style, cx);
539
540 let mut style = if is_active {
541 theme.workspace.active_tab.clone()
542 } else {
543 theme.workspace.tab.clone()
544 };
545 if ix == 0 {
546 style.container.border.left = false;
547 }
548
549 EventHandler::new(
550 Container::new(
551 Flex::row()
552 .with_child(
553 Align::new({
554 let diameter = 7.0;
555 let icon_color = if item_view.has_conflict(cx) {
556 Some(style.icon_conflict)
557 } else if item_view.is_dirty(cx) {
558 Some(style.icon_dirty)
559 } else {
560 None
561 };
562
563 ConstrainedBox::new(
564 Canvas::new(move |bounds, _, cx| {
565 if let Some(color) = icon_color {
566 let square = RectF::new(
567 bounds.origin(),
568 vec2f(diameter, diameter),
569 );
570 cx.scene.push_quad(Quad {
571 bounds: square,
572 background: Some(color),
573 border: Default::default(),
574 corner_radius: diameter / 2.,
575 });
576 }
577 })
578 .boxed(),
579 )
580 .with_width(diameter)
581 .with_height(diameter)
582 .boxed()
583 })
584 .boxed(),
585 )
586 .with_child(
587 Container::new(Align::new(title).boxed())
588 .with_style(ContainerStyle {
589 margin: Margin {
590 left: style.spacing,
591 right: style.spacing,
592 ..Default::default()
593 },
594 ..Default::default()
595 })
596 .boxed(),
597 )
598 .with_child(
599 Align::new(
600 ConstrainedBox::new(if mouse_state.hovered {
601 let item_id = item_view.id();
602 enum TabCloseButton {}
603 let icon = Svg::new("icons/x.svg");
604 MouseEventHandler::new::<TabCloseButton, _, _>(
605 item_id,
606 cx,
607 |mouse_state, _| {
608 if mouse_state.hovered {
609 icon.with_color(style.icon_close_active)
610 .boxed()
611 } else {
612 icon.with_color(style.icon_close).boxed()
613 }
614 },
615 )
616 .with_padding(Padding::uniform(4.))
617 .with_cursor_style(CursorStyle::PointingHand)
618 .on_click(move |cx| {
619 cx.dispatch_action(CloseItem(item_id))
620 })
621 .named("close-tab-icon")
622 } else {
623 Empty::new().boxed()
624 })
625 .with_width(style.icon_width)
626 .boxed(),
627 )
628 .boxed(),
629 )
630 .boxed(),
631 )
632 .with_style(style.container)
633 .boxed(),
634 )
635 .on_mouse_down(move |cx| {
636 cx.dispatch_action(ActivateItem(ix));
637 true
638 })
639 .boxed()
640 })
641 }
642
643 row.add_child(
644 Empty::new()
645 .contained()
646 .with_border(theme.workspace.tab.container.border)
647 .flexible(0., true)
648 .named("filler"),
649 );
650
651 row.boxed()
652 });
653
654 ConstrainedBox::new(tabs.boxed())
655 .with_height(theme.workspace.tab.height)
656 .named("tabs")
657 }
658}
659
660impl Entity for Pane {
661 type Event = Event;
662}
663
664impl View for Pane {
665 fn ui_name() -> &'static str {
666 "Pane"
667 }
668
669 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
670 let this = cx.handle();
671
672 EventHandler::new(if let Some(active_item) = self.active_item() {
673 Flex::column()
674 .with_child(self.render_tabs(cx))
675 .with_children(
676 self.active_toolbar()
677 .as_ref()
678 .map(|view| ChildView::new(view).boxed()),
679 )
680 .with_child(ChildView::new(active_item).flexible(1., true).boxed())
681 .boxed()
682 } else {
683 Empty::new().boxed()
684 })
685 .on_navigate_mouse_down(move |direction, cx| {
686 let this = this.clone();
687 match direction {
688 NavigationDirection::Back => cx.dispatch_action(GoBack(Some(this))),
689 NavigationDirection::Forward => cx.dispatch_action(GoForward(Some(this))),
690 }
691
692 true
693 })
694 .named("pane")
695 }
696
697 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
698 self.focus_active_item(cx);
699 }
700}
701
702impl<T: Toolbar> ToolbarHandle for ViewHandle<T> {
703 fn active_item_changed(
704 &self,
705 item: Option<Box<dyn ItemViewHandle>>,
706 cx: &mut MutableAppContext,
707 ) -> bool {
708 self.update(cx, |this, cx| this.active_item_changed(item, cx))
709 }
710
711 fn on_dismiss(&self, cx: &mut MutableAppContext) {
712 self.update(cx, |this, cx| this.on_dismiss(cx));
713 }
714
715 fn to_any(&self) -> AnyViewHandle {
716 self.into()
717 }
718}
719
720impl ItemNavHistory {
721 pub fn new<T: ItemView>(history: Rc<RefCell<NavHistory>>, item_view: &ViewHandle<T>) -> Self {
722 Self {
723 history,
724 item_view: Rc::new(item_view.downgrade()),
725 }
726 }
727
728 pub fn history(&self) -> Rc<RefCell<NavHistory>> {
729 self.history.clone()
730 }
731
732 pub fn push<D: 'static + Any>(&self, data: Option<D>) {
733 self.history.borrow_mut().push(data, self.item_view.clone());
734 }
735}
736
737impl NavHistory {
738 pub fn disable(&mut self) {
739 self.mode = NavigationMode::Disabled;
740 }
741
742 pub fn enable(&mut self) {
743 self.mode = NavigationMode::Normal;
744 }
745
746 pub fn pop_backward(&mut self) -> Option<NavigationEntry> {
747 self.backward_stack.pop_back()
748 }
749
750 pub fn pop_forward(&mut self) -> Option<NavigationEntry> {
751 self.forward_stack.pop_back()
752 }
753
754 fn pop(&mut self, mode: NavigationMode) -> Option<NavigationEntry> {
755 match mode {
756 NavigationMode::Normal | NavigationMode::Disabled => None,
757 NavigationMode::GoingBack => self.pop_backward(),
758 NavigationMode::GoingForward => self.pop_forward(),
759 }
760 }
761
762 fn set_mode(&mut self, mode: NavigationMode) {
763 self.mode = mode;
764 }
765
766 pub fn push<D: 'static + Any>(
767 &mut self,
768 data: Option<D>,
769 item_view: Rc<dyn WeakItemViewHandle>,
770 ) {
771 match self.mode {
772 NavigationMode::Disabled => {}
773 NavigationMode::Normal => {
774 if self.backward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
775 self.backward_stack.pop_front();
776 }
777 self.backward_stack.push_back(NavigationEntry {
778 item_view,
779 data: data.map(|data| Box::new(data) as Box<dyn Any>),
780 });
781 self.forward_stack.clear();
782 }
783 NavigationMode::GoingBack => {
784 if self.forward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
785 self.forward_stack.pop_front();
786 }
787 self.forward_stack.push_back(NavigationEntry {
788 item_view,
789 data: data.map(|data| Box::new(data) as Box<dyn Any>),
790 });
791 }
792 NavigationMode::GoingForward => {
793 if self.backward_stack.len() >= MAX_NAVIGATION_HISTORY_LEN {
794 self.backward_stack.pop_front();
795 }
796 self.backward_stack.push_back(NavigationEntry {
797 item_view,
798 data: data.map(|data| Box::new(data) as Box<dyn Any>),
799 });
800 }
801 }
802 }
803}