Browse Source

Merge pull request #83335 from KoBeWi/focusing_search_power

Fix FindReplaceBar focus problems
Rémi Verschelde 1 year ago
parent
commit
391897bc44
2 changed files with 14 additions and 4 deletions
  1. 12 3
      editor/code_editor.cpp
  2. 2 1
      editor/code_editor.h

+ 12 - 3
editor/code_editor.cpp

@@ -134,6 +134,13 @@ void FindReplaceBar::unhandled_input(const Ref<InputEvent> &p_event) {
 	}
 	}
 }
 }
 
 
+void FindReplaceBar::_focus_lost() {
+	if (Input::get_singleton()->is_action_pressed(SNAME("ui_cancel"))) {
+		// Unfocused after pressing Escape, so hide the bar.
+		_hide_bar(true);
+	}
+}
+
 bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
 bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
 	if (!preserve_cursor) {
 	if (!preserve_cursor) {
 		text_editor->remove_secondary_carets();
 		text_editor->remove_secondary_carets();
@@ -499,8 +506,8 @@ bool FindReplaceBar::search_next() {
 	return _search(flags, line, col);
 	return _search(flags, line, col);
 }
 }
 
 
-void FindReplaceBar::_hide_bar() {
-	if (replace_text->has_focus() || search_text->has_focus()) {
+void FindReplaceBar::_hide_bar(bool p_force_focus) {
+	if (replace_text->has_focus() || search_text->has_focus() || p_force_focus) {
 		text_editor->grab_focus();
 		text_editor->grab_focus();
 	}
 	}
 
 
@@ -698,6 +705,7 @@ FindReplaceBar::FindReplaceBar() {
 	search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
 	search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
 	search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
 	search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
 	search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
 	search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
+	search_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
 
 
 	matches_label = memnew(Label);
 	matches_label = memnew(Label);
 	hbc_button_search->add_child(matches_label);
 	hbc_button_search->add_child(matches_label);
@@ -732,6 +740,7 @@ FindReplaceBar::FindReplaceBar() {
 	vbc_lineedit->add_child(replace_text);
 	vbc_lineedit->add_child(replace_text);
 	replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
 	replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
 	replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
 	replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
+	replace_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
 
 
 	replace = memnew(Button);
 	replace = memnew(Button);
 	hbc_button_replace->add_child(replace);
 	hbc_button_replace->add_child(replace);
@@ -773,7 +782,7 @@ void CodeTextEditor::input(const Ref<InputEvent> &event) {
 	}
 	}
 
 
 	if (!text_editor->has_focus()) {
 	if (!text_editor->has_focus()) {
-		if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner()))) {
+		if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || (get_viewport()->gui_get_focus_owner() && find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner())))) {
 			if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
 			if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
 				find_replace_bar->search_next();
 				find_replace_bar->search_next();
 				accept_event();
 				accept_event();

+ 2 - 1
editor/code_editor.h

@@ -97,7 +97,7 @@ class FindReplaceBar : public HBoxContainer {
 	void _update_matches_label();
 	void _update_matches_label();
 
 
 	void _show_search(bool p_focus_replace = false, bool p_show_only = false);
 	void _show_search(bool p_focus_replace = false, bool p_show_only = false);
-	void _hide_bar();
+	void _hide_bar(bool p_force_focus = false);
 
 
 	void _editor_text_changed();
 	void _editor_text_changed();
 	void _search_options_changed(bool p_pressed);
 	void _search_options_changed(bool p_pressed);
@@ -108,6 +108,7 @@ class FindReplaceBar : public HBoxContainer {
 protected:
 protected:
 	void _notification(int p_what);
 	void _notification(int p_what);
 	virtual void unhandled_input(const Ref<InputEvent> &p_event) override;
 	virtual void unhandled_input(const Ref<InputEvent> &p_event) override;
+	void _focus_lost();
 
 
 	bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
 	bool _search(uint32_t p_flags, int p_from_line, int p_from_col);