Skip to content

Commit 86762f0

Browse files
Code Editor: Add button to toggle between search and search+replace modes
1 parent 5675c76 commit 86762f0

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

editor/code_editor.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void FindReplaceBar::_notification(int p_what) {
108108
hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
109109
hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
110110
hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
111+
_update_toggle_replace_button(replace_text->is_visible_in_tree());
111112
} break;
112113

113114
case NOTIFICATION_VISIBILITY_CHANGED: {
@@ -539,6 +540,14 @@ void FindReplaceBar::_hide_bar(bool p_force_focus) {
539540
hide();
540541
}
541542

543+
void FindReplaceBar::_update_toggle_replace_button(bool p_replace_visible) {
544+
String tooltip = p_replace_visible ? TTR("Hide Replace") : TTR("Show Replace");
545+
String shortcut = ED_GET_SHORTCUT(p_replace_visible ? "script_text_editor/find" : "script_text_editor/replace")->get_as_text();
546+
toggle_replace_button->set_tooltip_text(vformat("%s (%s)", tooltip, shortcut));
547+
StringName rtl_compliant_arrow = is_layout_rtl() ? SNAME("GuiTreeArrowLeft") : SNAME("GuiTreeArrowRight");
548+
toggle_replace_button->set_icon(get_editor_theme_icon(p_replace_visible ? SNAME("GuiTreeArrowDown") : rtl_compliant_arrow));
549+
}
550+
542551
void FindReplaceBar::_show_search(bool p_with_replace, bool p_show_only) {
543552
show();
544553
if (p_show_only) {
@@ -582,6 +591,7 @@ void FindReplaceBar::popup_search(bool p_show_only) {
582591
hbc_button_replace->hide();
583592
hbc_option_replace->hide();
584593
selection_only->set_pressed(false);
594+
_update_toggle_replace_button(false);
585595

586596
_show_search(false, p_show_only);
587597
}
@@ -591,6 +601,7 @@ void FindReplaceBar::popup_replace() {
591601
replace_text->show();
592602
hbc_button_replace->show();
593603
hbc_option_replace->show();
604+
_update_toggle_replace_button(true);
594605
}
595606

596607
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) {
644655
}
645656
}
646657

658+
void FindReplaceBar::_toggle_replace_pressed() {
659+
bool replace_visible = replace_text->is_visible_in_tree();
660+
replace_visible ? popup_search(true) : popup_replace();
661+
}
662+
647663
String FindReplaceBar::get_search_text() const {
648664
return search_text->get_text();
649665
}
@@ -702,6 +718,12 @@ void FindReplaceBar::_bind_methods() {
702718
}
703719

704720
FindReplaceBar::FindReplaceBar() {
721+
toggle_replace_button = memnew(Button);
722+
add_child(toggle_replace_button);
723+
toggle_replace_button->set_flat(true);
724+
toggle_replace_button->set_focus_mode(FOCUS_NONE);
725+
toggle_replace_button->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_toggle_replace_pressed));
726+
705727
vbc_lineedit = memnew(VBoxContainer);
706728
add_child(vbc_lineedit);
707729
vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);

editor/code_editor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class FindReplaceBar : public HBoxContainer {
7070
SEARCH_PREV,
7171
};
7272

73+
Button *toggle_replace_button = nullptr;
7374
LineEdit *search_text = nullptr;
7475
Label *matches_label = nullptr;
7576
Button *find_prev = nullptr;
@@ -106,12 +107,14 @@ class FindReplaceBar : public HBoxContainer {
106107

107108
void _show_search(bool p_with_replace, bool p_show_only);
108109
void _hide_bar(bool p_force_focus = false);
110+
void _update_toggle_replace_button(bool p_replace_visible);
109111

110112
void _editor_text_changed();
111113
void _search_options_changed(bool p_pressed);
112114
void _search_text_changed(const String &p_text);
113115
void _search_text_submitted(const String &p_text);
114116
void _replace_text_submitted(const String &p_text);
117+
void _toggle_replace_pressed();
115118

116119
protected:
117120
void _notification(int p_what);

0 commit comments

Comments
 (0)