Skip to main content

re_viewer_context/view/
view_states.rs

1//! Storage for the state of each `View`.
2//!
3//! The `Viewer` has ownership of this state and pass it around to users (mainly viewport and
4//! selection panel).
5
6use ahash::HashMap;
7
8use crate::view::system_execution_output::VisualizerViewReport;
9use crate::{SystemExecutionOutput, ViewClass, ViewId, ViewState, VisualizerTypeReport};
10
11/// State for the `View`s that persists across frames but otherwise
12/// is not saved.
13#[derive(Default)]
14pub struct ViewStates {
15    states: HashMap<ViewId, Box<dyn ViewState>>,
16
17    /// List of all errors that occurred in visualizers of this view.
18    ///
19    /// This is cleared out each frame and populated after all visualizers have been executed.
20    // TODO(andreas): Would be nice to bundle this with `ViewState` by making `ViewState` a struct containing errors & generic data.
21    // But at point of writing this causes too much needless churn.
22    visualizer_reports: HashMap<ViewId, VisualizerViewReport>,
23}
24
25impl re_byte_size::SizeBytes for ViewStates {
26    fn heap_size_bytes(&self) -> u64 {
27        let Self {
28            states,
29            visualizer_reports: visualizer_errors,
30        } = self;
31        states
32            .iter()
33            .map(|(key, state)| key.total_size_bytes() + state.size_bytes())
34            .sum::<u64>()
35            + visualizer_errors.heap_size_bytes()
36    }
37}
38
39impl ViewStates {
40    pub fn get(&self, view_id: ViewId) -> Option<&dyn ViewState> {
41        self.states.get(&view_id).map(|s| s.as_ref())
42    }
43
44    pub fn get_mut_or_create(
45        &mut self,
46        view_id: ViewId,
47        view_class: &dyn ViewClass,
48    ) -> &mut dyn ViewState {
49        self.states
50            .entry(view_id)
51            .or_insert_with(|| view_class.new_state())
52            .as_mut()
53    }
54
55    pub fn ensure_state_exists(&mut self, view_id: ViewId, view_class: &dyn ViewClass) {
56        self.states
57            .entry(view_id)
58            .or_insert_with(|| view_class.new_state());
59    }
60
61    /// Removes all previously stored visualizer reports.
62    pub fn reset_visualizer_reports(&mut self) {
63        self.visualizer_reports.clear();
64    }
65
66    /// Adds visualizer reports from a system execution output for a given view.
67    pub fn add_visualizer_reports_from_output(
68        &mut self,
69        view_id: ViewId,
70        system_output: &SystemExecutionOutput,
71    ) {
72        let per_visualizer_reports = self.visualizer_reports.entry(view_id).or_default();
73
74        per_visualizer_reports.extend(system_output.visualizer_execution_output.iter().filter_map(
75            |(id, result)| VisualizerTypeReport::from_result(result).map(|error| (*id, error)),
76        ));
77    }
78
79    /// Access latest visualizer reports (warnings and errors) for a given view.
80    pub fn per_visualizer_type_reports(&self, view_id: ViewId) -> Option<&VisualizerViewReport> {
81        self.visualizer_reports.get(&view_id)
82    }
83}