Skip to content

Commit

Permalink
Split out ViewportState
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed May 5, 2023
1 parent 27e6bb8 commit fea25e9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
21 changes: 16 additions & 5 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use re_viewer_context::{
AppOptions, Caches, ComponentUiRegistry, PlayState, RecordingConfig, ViewerContext,
};

use crate::{ui::Blueprint, viewer_analytics::ViewerAnalytics};
use crate::{
ui::{Blueprint, ViewportState},
viewer_analytics::ViewerAnalytics,
};

#[cfg(not(target_arch = "wasm32"))]
use re_log_types::TimeRangeF;
Expand Down Expand Up @@ -115,7 +118,7 @@ impl App {
);
}

let state: AppState = if startup_options.persist_state {
let persist_state: AppState = if startup_options.persist_state {
storage
.and_then(|storage| eframe::get_value(storage, eframe::APP_KEY))
.unwrap_or_default()
Expand All @@ -135,7 +138,7 @@ impl App {
component_ui_registry: re_data_ui::create_component_ui_registry(),
rx,
log_dbs: Default::default(),
state,
state: persist_state,
shutdown,
pending_promises: Default::default(),
toasts: toasts::Toasts::new(),
Expand Down Expand Up @@ -1026,6 +1029,11 @@ struct AppState {
#[cfg(not(target_arch = "wasm32"))]
#[serde(skip)]
profiler: crate::Profiler,

// This is sort of a weird place to put this but makes more sense than the
// blueprint
#[serde(skip)]
viewport_state: ViewportState,
}

impl AppState {
Expand Down Expand Up @@ -1053,6 +1061,7 @@ impl AppState {
time_panel,
#[cfg(not(target_arch = "wasm32"))]
profiler: _,
viewport_state,
} = self;

let rec_cfg =
Expand All @@ -1069,7 +1078,7 @@ impl AppState {
};

time_panel.show_panel(&mut ctx, blueprint, ui);
selection_panel.show_panel(&mut ctx, ui, blueprint);
selection_panel.show_panel(viewport_state, &mut ctx, ui, blueprint);

let central_panel_frame = egui::Frame {
fill: ui.style().visuals.panel_fill,
Expand All @@ -1080,7 +1089,9 @@ impl AppState {
egui::CentralPanel::default()
.frame(central_panel_frame)
.show_inside(ui, |ui| match *panel_selection {
PanelSelection::Viewport => blueprint.blueprint_panel_and_viewport(&mut ctx, ui),
PanelSelection::Viewport => {
blueprint.blueprint_panel_and_viewport(viewport_state, &mut ctx, ui);
}
});

// move time last, so we get to see the first data first!
Expand Down
11 changes: 8 additions & 3 deletions crates/re_viewer/src/ui/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use re_viewer_context::Item;
use crate::misc::space_info::SpaceInfoCollection;
use re_viewer_context::ViewerContext;

use super::viewport::Viewport;
use super::{viewport::Viewport, ViewportState};

/// Defines the layout of the whole Viewer (or will, eventually).
#[derive(Clone, Default, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -32,7 +32,12 @@ impl Blueprint {
}
}

pub fn blueprint_panel_and_viewport(&mut self, ctx: &mut ViewerContext<'_>, ui: &mut egui::Ui) {
pub fn blueprint_panel_and_viewport(
&mut self,
viewport_state: &mut ViewportState,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
) {
crate::profile_function!();

let spaces_info = SpaceInfoCollection::new(&ctx.log_db.entity_db);
Expand All @@ -49,7 +54,7 @@ impl Blueprint {
egui::CentralPanel::default()
.frame(viewport_frame)
.show_inside(ui, |ui| {
self.viewport.viewport_ui(ui, ctx);
self.viewport.viewport_ui(viewport_state, ui, ctx);
});
}

Expand Down
1 change: 0 additions & 1 deletion crates/re_viewer/src/ui/blueprint_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ fn load_viewport(blueprint_db: &re_data_store::LogDb) -> Option<Viewport> {
trees: Default::default(),
maximized: v.maximized,
has_been_user_edited: v.has_been_user_edited,
space_view_entity_window: Default::default(),
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ pub(crate) use self::blueprint::Blueprint;
pub use self::space_view::{item_ui, SpaceView};

pub use self::view_category::ViewCategory;
pub use self::viewport::Viewport;
pub use self::viewport::{Viewport, ViewportState};
12 changes: 7 additions & 5 deletions crates/re_viewer/src/ui/selection_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::ui::Blueprint;

use super::{
selection_history_ui::SelectionHistoryUi, space_view::ViewState,
view_spatial::SpatialNavigationMode,
view_spatial::SpatialNavigationMode, ViewportState,
};

// ---
Expand All @@ -29,6 +29,7 @@ pub(crate) struct SelectionPanel {
impl SelectionPanel {
pub fn show_panel(
&mut self,
viewport_state: &mut ViewportState,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
blueprint: &mut Blueprint,
Expand Down Expand Up @@ -75,7 +76,7 @@ impl SelectionPanel {
..Default::default()
}
.show(ui, |ui| {
self.contents(ui, ctx, blueprint);
self.contents(viewport_state, ui, ctx, blueprint);
});
});
},
Expand All @@ -85,6 +86,7 @@ impl SelectionPanel {
#[allow(clippy::unused_self)]
fn contents(
&mut self,
viewport_state: &mut ViewportState,
ui: &mut egui::Ui,
ctx: &mut ViewerContext<'_>,
blueprint: &mut Blueprint,
Expand All @@ -111,7 +113,7 @@ impl SelectionPanel {

ctx.re_ui
.large_collapsing_header(ui, "Blueprint", true, |ui| {
blueprint_ui(ui, ctx, blueprint, item);
blueprint_ui(viewport_state, ui, ctx, blueprint, item);
});

if i + 1 < num_selections {
Expand Down Expand Up @@ -220,6 +222,7 @@ pub fn what_is_selected_ui(

/// What is the blueprint stuff for this item?
fn blueprint_ui(
viewport_state: &mut ViewportState,
ui: &mut egui::Ui,
ctx: &mut ViewerContext<'_>,
blueprint: &mut Blueprint,
Expand All @@ -237,8 +240,7 @@ fn blueprint_ui(
.on_hover_text("Manually add or remove entities from the Space View.")
.clicked()
{
blueprint
.viewport
viewport_state
.show_add_remove_entities_window(*space_view_id);
}

Expand Down
40 changes: 22 additions & 18 deletions crates/re_viewer/src/ui/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ pub struct Viewport {
/// Before this is set we automatically add new spaces to the viewport
/// when they show up in the data.
pub(crate) has_been_user_edited: bool,

#[serde(skip)]
pub(crate) space_view_entity_window: Option<SpaceViewEntityPicker>,
}

impl PartialEq for Viewport {
Expand Down Expand Up @@ -93,15 +90,8 @@ impl Viewport {
trees,
maximized,
has_been_user_edited,
space_view_entity_window,
} = self;

if let Some(window) = space_view_entity_window {
if window.space_view_id == *space_view_id {
*space_view_entity_window = None;
}
}

*has_been_user_edited = true;

trees.retain(|vis_set, _| !vis_set.contains(space_view_id));
Expand Down Expand Up @@ -373,10 +363,6 @@ impl Viewport {
id
}

pub fn show_add_remove_entities_window(&mut self, space_view_id: SpaceViewId) {
self.space_view_entity_window = Some(SpaceViewEntityPicker { space_view_id });
}

pub fn on_frame_start(
&mut self,
ctx: &mut ViewerContext<'_>,
Expand Down Expand Up @@ -420,15 +406,20 @@ impl Viewport {
true
}

pub fn viewport_ui(&mut self, ui: &mut egui::Ui, ctx: &mut ViewerContext<'_>) {
if let Some(window) = &mut self.space_view_entity_window {
pub fn viewport_ui(
&mut self,
state: &mut ViewportState,
ui: &mut egui::Ui,
ctx: &mut ViewerContext<'_>,
) {
if let Some(window) = &mut state.space_view_entity_window {
if let Some(space_view) = self.space_views.get_mut(&window.space_view_id) {
if !window.ui(ctx, ui, space_view) {
self.space_view_entity_window = None;
state.space_view_entity_window = None;
}
} else {
// The space view no longer exist, close the window!
self.space_view_entity_window = None;
state.space_view_entity_window = None;
}
}

Expand Down Expand Up @@ -571,6 +562,19 @@ impl Viewport {
}
}

/// State for the blueprint that persists across frames but otherwise
/// is not saved.
#[derive(Clone, Default)]
pub struct ViewportState {
pub(crate) space_view_entity_window: Option<SpaceViewEntityPicker>,
}

impl ViewportState {
pub fn show_add_remove_entities_window(&mut self, space_view_id: SpaceViewId) {
self.space_view_entity_window = Some(SpaceViewEntityPicker { space_view_id });
}
}

/// Show a single button (`add_content`), justified,
/// and show a visibility button if the row is hovered.
///
Expand Down

0 comments on commit fea25e9

Please sign in to comment.