re_viewer 0.25.0

The Rerun viewer
Documentation
use re_viewer_context::DisplayMode;

/// The navigation history of the viewer.
///
/// This object should never be exposed to directly via contexts. Instead,
/// we retrieve the display mode and pass that around.
pub(crate) struct Navigation {
    history: Vec<DisplayMode>,
    start_mode: DisplayMode,
}

impl Default for Navigation {
    fn default() -> Self {
        Self {
            history: Default::default(),
            start_mode: DisplayMode::RedapServer(re_redap_browser::EXAMPLES_ORIGIN.clone()),
        }
    }
}

impl Navigation {
    // TODO(grtlr): In the future we should have something like `push_unique`, but for
    // this we first need all display modes to contain more information.

    pub fn push(&mut self, display_mode: DisplayMode) {
        re_log::debug!("Pushed display mode `{:?}`", display_mode);
        self.history.push(display_mode);
    }

    /// Pushes the start display mode to history which is also the fallback mode for
    /// navigation.
    ///
    /// This is defined in the default implementation for [`Navigation`]
    pub fn push_start_mode(&mut self) {
        self.push(self.start_mode.clone());
    }

    pub fn replace(&mut self, new_mode: DisplayMode) -> Option<DisplayMode> {
        let previous = self.history.pop();

        if previous.as_ref().is_none_or(|prev| prev != &new_mode) {
            re_log::trace!("Navigated from {previous:?} to {new_mode:?}");
        }

        self.history.push(new_mode);
        previous
    }

    pub fn pop(&mut self) -> Option<DisplayMode> {
        let previous = self.history.pop();
        re_log::debug!("Popped display mode `{:?}`", previous);
        previous
    }

    pub fn peek(&self) -> &DisplayMode {
        self.history.last().unwrap_or(&self.start_mode)
    }
}