Skip to content

Commit

Permalink
Fix not being able to click suggestions in space origin selection dro…
Browse files Browse the repository at this point in the history
…pdown (#6200)

### What
* Closes #6143

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6200?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6200?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6200)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
emilk committed May 2, 2024
1 parent 9b7a0e8 commit ffb935c
Showing 1 changed file with 59 additions and 57 deletions.
116 changes: 59 additions & 57 deletions crates/re_viewer/src/ui/space_view_space_origin_ui.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<usize>,
},
/// The index of the currently selected suggestion (for keyboard navigation).
selected_suggestion: Option<usize>,
}

/// Display the space origin of a space view.
Expand All @@ -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;
}
}
}
}
Expand All @@ -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<usize>,
) -> bool {
let mut keep_editing = true;
state: &mut EditState,
) -> ControlFlow<Option<EntityPath>, ()> {
let mut control_flow = ControlFlow::Continue(());

let popup_id = ui.make_persistent_id("suggestions");

//
// Build and filter the suggestion lists
Expand All @@ -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::<Vec<_>>();

Expand All @@ -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);
Expand All @@ -138,46 +142,41 @@ 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()));
}
}

//
// 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));
}
Expand All @@ -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()));
}
}

Expand All @@ -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
}

0 comments on commit ffb935c

Please sign in to comment.