Skip to content

Commit 891069d

Browse files
committed
Merge pull request godotengine#114236 from warriormaster12/san-popupmenu-search-bar
Implement search bar for `PopupMenu`
2 parents d72af00 + 18e989c commit 891069d

11 files changed

Lines changed: 215 additions & 17 deletions

File tree

doc/classes/OptionButton.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@
140140
Returns [code]true[/code] if the item at index [param idx] is marked as a separator.
141141
</description>
142142
</method>
143+
<method name="is_search_bar_enabled" qualifiers="const">
144+
<return type="bool" />
145+
<description>
146+
Returns [code]true[/code] if the search bar is enabled.
147+
</description>
148+
</method>
143149
<method name="remove_item">
144150
<return type="void" />
145151
<param index="0" name="idx" type="int" />
@@ -233,6 +239,9 @@
233239
<member name="allow_reselect" type="bool" setter="set_allow_reselect" getter="get_allow_reselect" default="false">
234240
If [code]true[/code], the currently selected item can be selected again.
235241
</member>
242+
<member name="enable_search_bar_on_item_count" type="int" setter="set_search_bar_enabled_on_item_count" getter="get_search_bar_enabled_on_item_count" default="0">
243+
Enables the [PopupMenu] search bar if the item count is greater than [code]0[/code].
244+
</member>
236245
<member name="fit_to_longest_item" type="bool" setter="set_fit_to_longest_item" getter="is_fit_to_longest_item" default="true">
237246
If [code]true[/code], minimum size will be determined by the longest item's text, instead of the currently selected one's.
238247
[b]Note:[/b] For performance reasons, the minimum size doesn't update immediately when adding, removing or modifying items.

doc/classes/PopupMenu.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@
408408
Returns [code]true[/code] if the system native menu is supported and currently used by this [PopupMenu].
409409
</description>
410410
</method>
411+
<method name="is_search_bar_enabled" qualifiers="const">
412+
<return type="bool" />
413+
<description>
414+
Returns [code]true[/code] if search bar is currently enabled.
415+
</description>
416+
</method>
411417
<method name="is_system_menu" qualifiers="const">
412418
<return type="bool" />
413419
<description>
@@ -669,6 +675,10 @@
669675
If [code]true[/code], [MenuBar] will use native menu when supported.
670676
[b]Note:[/b] If [PopupMenu] is linked to [StatusIndicator], [MenuBar], or another [PopupMenu] item it can use native menu regardless of this property, use [method is_native_menu] to check it.
671677
</member>
678+
<member name="search_bar_enabled_on_item_count" type="int" setter="set_search_bar_enabled_on_item_count" getter="get_search_bar_enabled_on_item_count" default="0">
679+
Enables the [PopupMenu] search bar if the item count is greater than [code]0[/code].
680+
[b]Note:[/b] When enabled, [member allow_search] is ignored.
681+
</member>
672682
<member name="shrink_height" type="bool" setter="set_shrink_height" getter="get_shrink_height" default="true">
673683
If [code]true[/code], shrinks [PopupMenu] to minimum height when it's shown.
674684
</member>
@@ -791,6 +801,9 @@
791801
<theme_item name="radio_unchecked_disabled" data_type="icon" type="Texture2D">
792802
[Texture2D] icon for the unchecked radio button items when they are disabled.
793803
</theme_item>
804+
<theme_item name="search" data_type="icon" type="Texture2D">
805+
[Texture2D] icon for the search bar's search icon.
806+
</theme_item>
794807
<theme_item name="submenu" data_type="icon" type="Texture2D">
795808
[Texture2D] icon for the submenu arrow (for left-to-right layouts).
796809
</theme_item>

editor/inspector/editor_properties.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ EditorPropertyTextEnum::EditorPropertyTextEnum() {
578578
option_button->set_h_size_flags(SIZE_EXPAND_FILL);
579579
option_button->set_clip_text(true);
580580
option_button->set_flat(true);
581+
option_button->set_search_bar_enabled_on_item_count(10);
581582
option_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
582583
option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
583584
default_layout->add_child(option_button);
@@ -985,6 +986,7 @@ EditorPropertyEnum::EditorPropertyEnum() {
985986
options->set_flat(true);
986987
options->set_theme_type_variation(SNAME("EditorInspectorButton"));
987988
options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
989+
options->set_search_bar_enabled_on_item_count(10);
988990
add_child(options);
989991
add_focusable(options);
990992
options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyEnum::_option_selected));

editor/themes/theme_classic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ void ThemeClassic::populate_standard_styles(const Ref<EditorTheme> &p_theme, Edi
11261126
p_theme->set_icon("radio_unchecked_disabled", "PopupMenu", p_theme->get_icon(SNAME("GuiRadioUncheckedDisabled"), EditorStringName(EditorIcons)));
11271127
p_theme->set_icon("submenu", "PopupMenu", p_theme->get_icon(SNAME("ArrowRight"), EditorStringName(EditorIcons)));
11281128
p_theme->set_icon("submenu_mirrored", "PopupMenu", p_theme->get_icon(SNAME("ArrowLeft"), EditorStringName(EditorIcons)));
1129+
p_theme->set_icon("search", "PopupMenu", p_theme->get_icon(SNAME("Search"), EditorStringName(EditorIcons)));
11291130

11301131
int v_sep = (p_config.enable_touch_optimizations ? 12 : p_config.forced_even_separation) * EDSCALE;
11311132
p_theme->set_constant("v_separation", "PopupMenu", v_sep);

editor/themes/theme_modern.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ void ThemeModern::populate_standard_styles(const Ref<EditorTheme> &p_theme, Edit
11011101
p_theme->set_icon("radio_unchecked_disabled", "PopupMenu", p_theme->get_icon(SNAME("GuiRadioUncheckedDisabled"), EditorStringName(EditorIcons)));
11021102
p_theme->set_icon("submenu", "PopupMenu", p_theme->get_icon(SNAME("ArrowRight"), EditorStringName(EditorIcons)));
11031103
p_theme->set_icon("submenu_mirrored", "PopupMenu", p_theme->get_icon(SNAME("ArrowLeft"), EditorStringName(EditorIcons)));
1104+
p_theme->set_icon("search", "PopupMenu", p_theme->get_icon(SNAME("Search"), EditorStringName(EditorIcons)));
11041105

11051106
p_theme->set_constant("h_separation", "PopupMenu", p_config.base_margin * 1.75 * EDSCALE);
11061107
int v_sep = (p_config.enable_touch_optimizations ? 12 : p_config.base_margin * 1.75) * EDSCALE;

scene/gui/option_button.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,18 @@ bool OptionButton::get_allow_reselect() const {
390390
return allow_reselect;
391391
}
392392

393+
bool OptionButton::is_search_bar_enabled() const {
394+
return popup->is_search_bar_enabled();
395+
}
396+
397+
void OptionButton::set_search_bar_enabled_on_item_count(int p_count) {
398+
popup->set_search_bar_enabled_on_item_count(p_count);
399+
}
400+
401+
int OptionButton::get_search_bar_enabled_on_item_count() const {
402+
return popup->get_search_bar_enabled_on_item_count();
403+
}
404+
393405
void OptionButton::add_separator(const String &p_text) {
394406
popup->add_separator(p_text);
395407
}
@@ -570,6 +582,7 @@ void OptionButton::_bind_methods() {
570582
ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
571583
ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
572584
ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "idx", "mode"), &OptionButton::set_item_auto_translate_mode);
585+
ClassDB::bind_method(D_METHOD("set_search_bar_enabled_on_item_count", "counts"), &OptionButton::set_search_bar_enabled_on_item_count);
573586
ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
574587
ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
575588
ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
@@ -579,6 +592,8 @@ void OptionButton::_bind_methods() {
579592
ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "idx"), &OptionButton::get_item_auto_translate_mode);
580593
ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
581594
ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
595+
ClassDB::bind_method(D_METHOD("is_search_bar_enabled"), &OptionButton::is_search_bar_enabled);
596+
ClassDB::bind_method(D_METHOD("get_search_bar_enabled_on_item_count"), &OptionButton::get_search_bar_enabled_on_item_count);
582597
ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
583598
ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
584599
ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
@@ -604,6 +619,7 @@ void OptionButton::_bind_methods() {
604619
ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
605620
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
606621
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
622+
ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_search_bar_on_item_count", PROPERTY_HINT_RANGE, "0,20,1,or_greater"), "set_search_bar_enabled_on_item_count", "get_search_bar_enabled_on_item_count");
607623
ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
608624

609625
ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));

scene/gui/option_button.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class OptionButton : public Button {
107107
void set_item_disabled(int p_idx, bool p_disabled);
108108
void set_item_tooltip(int p_idx, const String &p_tooltip);
109109
void set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode);
110+
void set_search_bar_enabled_on_item_count(int p_count);
110111

111112
String get_item_text(int p_idx) const;
112113
Ref<Texture2D> get_item_icon(int p_idx) const;
@@ -117,6 +118,8 @@ class OptionButton : public Button {
117118
bool is_item_separator(int p_idx) const;
118119
String get_item_tooltip(int p_idx) const;
119120
AutoTranslateMode get_item_auto_translate_mode(int p_idx) const;
121+
bool is_search_bar_enabled() const;
122+
int get_search_bar_enabled_on_item_count() const;
120123

121124
bool has_selectable_items() const;
122125
int get_selectable_item(bool p_from_last = false) const;

0 commit comments

Comments
 (0)