Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Menu > Import and associated command #7882

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/viewer/re_ui/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub trait UICommandSender {
pub enum UICommand {
// Listed in the order they show up in the command palette by default!
Open,
Import,
SaveRecording,
SaveRecordingSelection,
SaveBlueprint,
Expand Down Expand Up @@ -111,7 +112,8 @@ impl UICommand {

Self::SaveBlueprint => ("Save blueprint…", "Save the current viewer setup as a Rerun blueprint file (.rbl)"),

Self::Open => ("Open…", "Open any supported files (.rrd, images, meshes, …)"),
Self::Open => ("Open…", "Open any supported files (.rrd, images, meshes, …) in a new recording"),
Self::Import => ("Import…", "Import any supported files (.rrd, images, meshes, …) in the current recording"),

Self::CloseCurrentRecording => (
"Close current recording",
Expand Down Expand Up @@ -271,6 +273,10 @@ impl UICommand {
KeyboardShortcut::new(Modifiers::COMMAND, key)
}

fn cmd_shift(key: Key) -> KeyboardShortcut {
KeyboardShortcut::new(Modifiers::COMMAND.plus(Modifiers::SHIFT), key)
}

fn cmd_alt(key: Key) -> KeyboardShortcut {
KeyboardShortcut::new(Modifiers::COMMAND.plus(Modifiers::ALT), key)
}
Expand All @@ -284,6 +290,7 @@ impl UICommand {
Self::SaveRecordingSelection => Some(cmd_alt(Key::S)),
Self::SaveBlueprint => None,
Self::Open => Some(cmd(Key::O)),
Self::Import => Some(cmd_shift(Key::O)),
Self::CloseCurrentRecording => None,
Self::CloseAllRecordings => None,

Expand Down
81 changes: 75 additions & 6 deletions crates/viewer/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ const MIN_ZOOM_FACTOR: f32 = 0.2;
#[cfg(not(target_arch = "wasm32"))]
const MAX_ZOOM_FACTOR: f32 = 5.0;

#[cfg(target_arch = "wasm32")]
struct PendingFilePromise {
recommended_application_id: Option<ApplicationId>,
recommended_recording_id: Option<re_log_types::StoreId>,
promise: poll_promise::Promise<Vec<re_data_source::FileContents>>,
}

/// The Rerun Viewer as an [`eframe`] application.
pub struct App {
build_info: re_build_info::BuildInfo,
Expand All @@ -169,7 +176,7 @@ pub struct App {
rx: ReceiveSet<LogMsg>,

#[cfg(target_arch = "wasm32")]
open_files_promise: Option<poll_promise::Promise<Vec<re_data_source::FileContents>>>,
open_files_promise: Option<PendingFilePromise>,

/// What is serialized
pub(crate) state: AppState,
Expand Down Expand Up @@ -564,6 +571,18 @@ impl App {
store_context: Option<&StoreContext<'_>>,
cmd: UICommand,
) {
let active_application_id = store_context
.and_then(|ctx| {
ctx.hub
.active_app()
// Don't redirect data to the welcome screen.
.filter(|&app_id| app_id != &StoreHub::welcome_screen_app_id())
})
.cloned();
let active_recording_id = store_context
.and_then(|ctx| ctx.hub.active_recording_id())
.cloned();

match cmd {
UICommand::SaveRecording => {
if let Err(err) = save_recording(self, store_context, None) {
Expand Down Expand Up @@ -602,12 +621,57 @@ impl App {
#[cfg(target_arch = "wasm32")]
UICommand::Open => {
let egui_ctx = egui_ctx.clone();
self.open_files_promise = Some(poll_promise::Promise::spawn_local(async move {

// Open: we want to try and load into a new dedicated recording.
let recommended_application_id = None;
let recommended_recording_id = None;
let promise = poll_promise::Promise::spawn_local(async move {
let file = async_open_rrd_dialog().await;
egui_ctx.request_repaint(); // Wake ui thread
file
}));
});

self.open_files_promise = Some(PendingFilePromise {
recommended_application_id,
recommended_recording_id,
promise,
});
}

#[cfg(not(target_arch = "wasm32"))]
UICommand::Import => {
for file_path in open_file_dialog_native() {
self.command_sender
.send_system(SystemCommand::LoadDataSource(DataSource::FilePath(
FileSource::FileDialog {
recommended_application_id: active_application_id.clone(),
recommended_recording_id: active_recording_id.clone(),
},
file_path,
)));
}
}
#[cfg(target_arch = "wasm32")]
UICommand::Import => {
let egui_ctx = egui_ctx.clone();

// Import: we want to try and load into the current recording.
let recommended_application_id = active_application_id;
let recommended_recording_id = active_recording_id;

let promise = poll_promise::Promise::spawn_local(async move {
let file = async_open_rrd_dialog().await;
egui_ctx.request_repaint(); // Wake ui thread
file
});

self.open_files_promise = Some(PendingFilePromise {
recommended_application_id,
recommended_recording_id,
promise,
});
}

UICommand::CloseCurrentRecording => {
let cur_rec = store_context.map(|ctx| ctx.recording.store_id());
if let Some(cur_rec) = cur_rec {
Expand Down Expand Up @@ -1586,14 +1650,19 @@ impl eframe::App for App {
}

#[cfg(target_arch = "wasm32")]
if let Some(promise) = &self.open_files_promise {
if let Some(PendingFilePromise {
recommended_application_id,
recommended_recording_id,
promise,
}) = &self.open_files_promise
{
if let Some(files) = promise.ready() {
for file in files {
self.command_sender
.send_system(SystemCommand::LoadDataSource(DataSource::FileContents(
FileSource::FileDialog {
recommended_application_id: None,
recommended_recording_id: None,
recommended_application_id: recommended_application_id.clone(),
recommended_recording_id: recommended_recording_id.clone(),
},
file.clone(),
)));
Expand Down
1 change: 1 addition & 0 deletions crates/viewer/re_viewer/src/ui/rerun_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl App {
ui.add_space(SPACING);

UICommand::Open.menu_button_ui(ui, &self.command_sender);
UICommand::Import.menu_button_ui(ui, &self.command_sender);

self.save_buttons_ui(ui, _store_context);

Expand Down
Loading