diff --git a/crates/re_viewer/src/ui/space_view_space_origin_ui.rs b/crates/re_viewer/src/ui/space_view_space_origin_ui.rs index 63dbb8c102ee..69e4106a2592 100644 --- a/crates/re_viewer/src/ui/space_view_space_origin_ui.rs +++ b/crates/re_viewer/src/ui/space_view_space_origin_ui.rs @@ -1,6 +1,9 @@ +use std::ops::ControlFlow; + use eframe::emath::NumExt; use egui::{Key, Ui}; +use re_log_types::EntityPath; use re_space_view::SpaceViewBlueprint; use re_ui::{ReUi, SyntaxHighlighting}; use re_viewer_context::ViewerContext; @@ -11,16 +14,19 @@ enum SpaceOriginEditState { #[default] NotEditing, - Editing { - /// The string currently entered by the user. - origin_string: String, + Editing(EditState), +} + +#[derive(Clone)] +struct EditState { + /// The string currently entered by the user. + origin_string: String, - /// Did we just enter editing mode? - entered_editing: bool, + /// Did we just enter editing mode? + entered_editing: bool, - /// The index of the currently selected suggestion (for keyboard navigation). - selected_suggestion: Option, - }, + /// The index of the currently selected suggestion (for keyboard navigation). + selected_suggestion: Option, } /// Display the space origin of a space view. @@ -39,31 +45,29 @@ pub(crate) fn space_view_space_origin_widget_ui( let output = egui::TextEdit::singleline(&mut space_origin_string).show(ui); if output.response.gained_focus() { - state = SpaceOriginEditState::Editing { + state = SpaceOriginEditState::Editing(EditState { origin_string: space_origin_string, entered_editing: true, selected_suggestion: None, - }; + }); } } - SpaceOriginEditState::Editing { - origin_string, - entered_editing, - selected_suggestion, - } => { - let keep_editing = space_view_space_origin_widget_editing_ui( - ui, - ctx, - origin_string, - *entered_editing, - space_view, - selected_suggestion, - ); - - if keep_editing { - *entered_editing = false; - } else { - state = SpaceOriginEditState::NotEditing; + SpaceOriginEditState::Editing(edit_state) => { + let control_flow = + space_view_space_origin_widget_editing_ui(ui, ctx, space_view, edit_state); + + match control_flow { + ControlFlow::Break(Some(new_space_origin)) => { + space_view.set_origin(ctx, &new_space_origin); + state = SpaceOriginEditState::NotEditing; + } + ControlFlow::Break(None) => { + state = SpaceOriginEditState::NotEditing; + } + ControlFlow::Continue(()) => { + // Keep editing + edit_state.entered_editing = false; + } } } } @@ -75,12 +79,12 @@ pub(crate) fn space_view_space_origin_widget_ui( fn space_view_space_origin_widget_editing_ui( ui: &mut Ui, ctx: &ViewerContext<'_>, - space_origin_string: &mut String, - entered_editing: bool, space_view: &SpaceViewBlueprint, - selected_suggestion: &mut Option, -) -> bool { - let mut keep_editing = true; + state: &mut EditState, +) -> ControlFlow, ()> { + let mut control_flow = ControlFlow::Continue(()); + + let popup_id = ui.make_persistent_id("suggestions"); // // Build and filter the suggestion lists @@ -103,7 +107,7 @@ fn space_view_space_origin_widget_editing_ui( suggested_space_view .space_origin .to_string() - .contains(&*space_origin_string) + .contains(&state.origin_string) }) .collect::>(); @@ -116,12 +120,12 @@ fn space_view_space_origin_widget_editing_ui( let arrow_up = ui.input_mut(|i| i.count_and_consume_key(Default::default(), Key::ArrowUp)); // force spawn a selected suggestion if the down arrow is pressed - if arrow_down > 0 && selected_suggestion.is_none() { - *selected_suggestion = Some(0); + if arrow_down > 0 && state.selected_suggestion.is_none() { + state.selected_suggestion = Some(0); arrow_down -= 1; } - *selected_suggestion = selected_suggestion.map(|mut selected_suggestion| { + state.selected_suggestion = state.selected_suggestion.map(|mut selected_suggestion| { selected_suggestion = selected_suggestion .saturating_add(arrow_down) .saturating_sub(arrow_up); @@ -138,12 +142,11 @@ fn space_view_space_origin_widget_editing_ui( let enter_key_hit = ui.input(|i| i.key_pressed(egui::Key::Enter)); - if let Some(selected_suggestion) = selected_suggestion { + if let Some(selected_suggestion) = state.selected_suggestion { if enter_key_hit { - *space_origin_string = filtered_space_view_suggestions[*selected_suggestion] - .space_origin - .to_string(); - keep_editing = false; + let origin = &filtered_space_view_suggestions[selected_suggestion].space_origin; + state.origin_string = origin.to_string(); + control_flow = ControlFlow::Break(Some(origin.clone())); } } @@ -151,33 +154,29 @@ fn space_view_space_origin_widget_editing_ui( // Draw the text edit // - let mut output = egui::TextEdit::singleline(space_origin_string).show(ui); + let mut output = egui::TextEdit::singleline(&mut state.origin_string).show(ui); - if entered_editing { + if state.entered_editing { output.response.request_focus(); let min = egui::text::CCursor::new(0); - let max = egui::text::CCursor::new(space_origin_string.len()); + let max = egui::text::CCursor::new(state.origin_string.len()); let new_range = egui::text::CCursorRange::two(min, max); output.state.cursor.set_char_range(Some(new_range)); output.state.store(ui.ctx(), output.response.id); } if output.response.changed() { - space_view.set_origin(ctx, &space_origin_string.clone().into()); + space_view.set_origin(ctx, &state.origin_string.clone().into()); } - if output.response.lost_focus() { - if enter_key_hit { - space_view.set_origin(ctx, &space_origin_string.clone().into()); - } - keep_editing = false; + if output.response.lost_focus() && enter_key_hit && control_flow.is_continue() { + control_flow = ControlFlow::Break(Some(state.origin_string.clone().into())); } // // Display popup with suggestions // - let popup_id = ui.make_persistent_id("suggestions"); if output.response.has_focus() { ui.memory_mut(|mem| mem.open_popup(popup_id)); } @@ -191,16 +190,15 @@ fn space_view_space_origin_widget_editing_ui( .space_origin .syntax_highlighted(ui.style()), ) - .force_hovered(*selected_suggestion == Some(idx)) + .force_hovered(state.selected_suggestion == Some(idx)) .show_flat(ui); if response.hovered() { - *selected_suggestion = None; + state.selected_suggestion = None; } if response.clicked() { - *space_origin_string = suggested_space_view.space_origin.to_string(); - space_view.set_origin(ctx, &space_origin_string.clone().into()); + control_flow = ControlFlow::Break(Some(suggested_space_view.space_origin.clone())); } } @@ -216,5 +214,9 @@ fn space_view_space_origin_widget_editing_ui( ReUi::list_item_popup(ui, popup_id, &output.response, 4.0, suggestions_ui); - keep_editing + if control_flow.is_continue() && !ui.memory(|mem| mem.is_popup_open(popup_id)) { + control_flow = ControlFlow::Break(None); + }; + + control_flow }