No more hack

This commit is contained in:
Setzer22 2022-11-15 19:41:03 +01:00
parent 70b60071ac
commit f4009fccc9
2 changed files with 6 additions and 10 deletions

View File

@ -195,7 +195,7 @@ where
should_close_node_finder = true; should_close_node_finder = true;
delayed_responses.push(NodeResponse::CreatedNode(new_node)); delayed_responses.push(NodeResponse::CreatedNode(new_node));
} }
let finder_rect = ui.max_rect(); let finder_rect = ui.min_rect();
// If the cursor is not in the main editor, check if the cursor is in the finder // If the cursor is not in the main editor, check if the cursor is in the finder
// if the cursor is in the finder, then we can consider that also in the editor. // if the cursor is in the finder, then we can consider that also in the editor.
if finder_rect.contains(cursor_pos) { if finder_rect.contains(cursor_pos) {
@ -401,7 +401,7 @@ where
self.connection_in_progress = None; self.connection_in_progress = None;
} }
if mouse.secondary_clicked() && cursor_in_editor && !cursor_in_finder { if mouse.secondary_released() && cursor_in_editor && !cursor_in_finder {
self.node_finder = Some(NodeFinder::new_at(cursor_pos)); self.node_finder = Some(NodeFinder::new_at(cursor_pos));
} }
if ui.ctx().input().key_pressed(Key::Escape) { if ui.ctx().input().key_pressed(Key::Escape) {

View File

@ -10,7 +10,7 @@ pub struct NodeFinder<NodeTemplate> {
pub query: String, pub query: String,
/// Reset every frame. When set, the node finder will be moved at that position /// Reset every frame. When set, the node finder will be moved at that position
pub position: Option<Pos2>, pub position: Option<Pos2>,
pub frames_since_spawned: u32, pub just_spawned: bool,
_phantom: PhantomData<NodeTemplate>, _phantom: PhantomData<NodeTemplate>,
} }
@ -22,7 +22,7 @@ where
NodeFinder { NodeFinder {
query: "".into(), query: "".into(),
position: Some(pos), position: Some(pos),
frames_since_spawned: 0, just_spawned: true,
_phantom: Default::default(), _phantom: Default::default(),
} }
} }
@ -58,13 +58,9 @@ where
frame.show(ui, |ui| { frame.show(ui, |ui| {
ui.vertical(|ui| { ui.vertical(|ui| {
let resp = ui.text_edit_singleline(&mut self.query); let resp = ui.text_edit_singleline(&mut self.query);
// HACK: The request_focus call doesn't succeed if we do it if self.just_spawned {
// right after spawning the node finder, so we do it for a few
// frames until it works. This is could be a bug inside egui.
// The value 3 is the smallest number of frames that works.
if self.frames_since_spawned <= 3 {
resp.request_focus(); resp.request_focus();
self.frames_since_spawned += 1; self.just_spawned = false;
} }
let mut query_submit = resp.lost_focus() && ui.input().key_down(Key::Enter); let mut query_submit = resp.lost_focus() && ui.input().key_down(Key::Enter);