Browse Source

Code Editor: Add button to toggle between search and search+replace modes

Elliot Fontaine 1 year ago
parent
commit
86762f0a03
2 changed files with 25 additions and 0 deletions
  1. 22 0
      editor/code_editor.cpp
  2. 3 0
      editor/code_editor.h

+ 22 - 0
editor/code_editor.cpp

@@ -108,6 +108,7 @@ void FindReplaceBar::_notification(int p_what) {
 			hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
 			hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
 			hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
+			_update_toggle_replace_button(replace_text->is_visible_in_tree());
 		} break;
 
 		case NOTIFICATION_VISIBILITY_CHANGED: {
@@ -539,6 +540,14 @@ void FindReplaceBar::_hide_bar(bool p_force_focus) {
 	hide();
 }
 
+void FindReplaceBar::_update_toggle_replace_button(bool p_replace_visible) {
+	String tooltip = p_replace_visible ? TTR("Hide Replace") : TTR("Show Replace");
+	String shortcut = ED_GET_SHORTCUT(p_replace_visible ? "script_text_editor/find" : "script_text_editor/replace")->get_as_text();
+	toggle_replace_button->set_tooltip_text(vformat("%s (%s)", tooltip, shortcut));
+	StringName rtl_compliant_arrow = is_layout_rtl() ? SNAME("GuiTreeArrowLeft") : SNAME("GuiTreeArrowRight");
+	toggle_replace_button->set_icon(get_editor_theme_icon(p_replace_visible ? SNAME("GuiTreeArrowDown") : rtl_compliant_arrow));
+}
+
 void FindReplaceBar::_show_search(bool p_with_replace, bool p_show_only) {
 	show();
 	if (p_show_only) {
@@ -582,6 +591,7 @@ void FindReplaceBar::popup_search(bool p_show_only) {
 	hbc_button_replace->hide();
 	hbc_option_replace->hide();
 	selection_only->set_pressed(false);
+	_update_toggle_replace_button(false);
 
 	_show_search(false, p_show_only);
 }
@@ -591,6 +601,7 @@ void FindReplaceBar::popup_replace() {
 		replace_text->show();
 		hbc_button_replace->show();
 		hbc_option_replace->show();
+		_update_toggle_replace_button(true);
 	}
 
 	selection_only->set_pressed(text_editor->has_selection(0) && text_editor->get_selection_from_line(0) < text_editor->get_selection_to_line(0));
@@ -644,6 +655,11 @@ void FindReplaceBar::_replace_text_submitted(const String &p_text) {
 	}
 }
 
+void FindReplaceBar::_toggle_replace_pressed() {
+	bool replace_visible = replace_text->is_visible_in_tree();
+	replace_visible ? popup_search(true) : popup_replace();
+}
+
 String FindReplaceBar::get_search_text() const {
 	return search_text->get_text();
 }
@@ -702,6 +718,12 @@ void FindReplaceBar::_bind_methods() {
 }
 
 FindReplaceBar::FindReplaceBar() {
+	toggle_replace_button = memnew(Button);
+	add_child(toggle_replace_button);
+	toggle_replace_button->set_flat(true);
+	toggle_replace_button->set_focus_mode(FOCUS_NONE);
+	toggle_replace_button->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_toggle_replace_pressed));
+
 	vbc_lineedit = memnew(VBoxContainer);
 	add_child(vbc_lineedit);
 	vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);

+ 3 - 0
editor/code_editor.h

@@ -70,6 +70,7 @@ class FindReplaceBar : public HBoxContainer {
 		SEARCH_PREV,
 	};
 
+	Button *toggle_replace_button = nullptr;
 	LineEdit *search_text = nullptr;
 	Label *matches_label = nullptr;
 	Button *find_prev = nullptr;
@@ -106,12 +107,14 @@ class FindReplaceBar : public HBoxContainer {
 
 	void _show_search(bool p_with_replace, bool p_show_only);
 	void _hide_bar(bool p_force_focus = false);
+	void _update_toggle_replace_button(bool p_replace_visible);
 
 	void _editor_text_changed();
 	void _search_options_changed(bool p_pressed);
 	void _search_text_changed(const String &p_text);
 	void _search_text_submitted(const String &p_text);
 	void _replace_text_submitted(const String &p_text);
+	void _toggle_replace_pressed();
 
 protected:
 	void _notification(int p_what);