pane.rs

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