UI: Expose an "is first search" boolean to search button callbacks

Currently when you open an RNA collection search button, like a
vertex group selector, the search filter isn't applied until you
start typing, in order to display every option at the start.
Otherwise they wouldn't be visible, since the search filter would
run for the current text.

Currently this check happens in one place, but it relies on the
`changed` value of `uiBut`. This is fine in the interface directory,
but anywhere else it would require exposing `uiBut.changed`, which
is probably too low-level to expose.

The solution is adding an `is_first` argument to the search callbacks,
which is nice for a few reasons:
  - They work at a higher level of abstraction, meaning they don't
    have to worry about how exactly to tell if this is the first
    search.
  - It makes it easier to do special behavior when the search menu
    is first opened.
  - Then, obviously, it makes that state accessible without including
    `interface_intern.h`.

Needed for attribute search: T85658

Differential Revision: https://developer.blender.org/D10528
This commit is contained in:
Hans Goudey 2021-03-02 11:42:05 -06:00
parent 5a3b7c5329
commit 1a8aee0a7c
Notes: blender-bot 2023-02-14 10:11:54 +01:00
Referenced by commit a344f20346, UI: Rename search button variable
Referenced by issue #85658, Lookup for attribute search
11 changed files with 55 additions and 20 deletions

View File

@ -499,10 +499,14 @@ typedef int (*uiButCompleteFunc)(struct bContext *C, char *str, void *arg);
typedef struct ARegion *(*uiButSearchCreateFn)(struct bContext *C,
struct ARegion *butregion,
struct uiButSearch *search_but);
/* `is_first` is typically used to ignore search filtering when the menu is first opened in order
* to display the full list of options. The value will be false after the button's text is edited
* (for every call except the first). */
typedef void (*uiButSearchUpdateFn)(const struct bContext *C,
void *arg,
const char *str,
uiSearchItems *items);
uiSearchItems *items,
const bool is_first);
typedef void (*uiButSearchArgFreeFn)(void *arg);
typedef bool (*uiButSearchContextMenuFn)(struct bContext *C,
void *arg,
@ -1602,6 +1606,7 @@ void UI_but_func_search_set(uiBut *but,
void UI_but_func_search_set_context_menu(uiBut *but, uiButSearchContextMenuFn context_menu_fn);
void UI_but_func_search_set_tooltip(uiBut *but, uiButSearchTooltipFn tooltip_fn);
void UI_but_func_search_set_sep_string(uiBut *but, const char *search_sep_string);
void UI_but_func_search_set_all_strings_valid(uiBut *but, const bool value);
/* height in pixels, it's using hardcoded values still */
int UI_searchbox_size_y(void);

View File

@ -6661,11 +6661,20 @@ void UI_but_func_search_set_tooltip(uiBut *but, uiButSearchTooltipFn tooltip_fn)
but_search->item_tooltip_fn = tooltip_fn;
}
void UI_but_func_search_set_all_strings_valid(uiBut *but, const bool value)
{
uiButSearch *but_search = (uiButSearch *)but;
BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
but_search->all_strings_valid = value;
}
/* Callbacks for operator search button. */
static void operator_enum_search_update_fn(const struct bContext *C,
void *but,
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
wmOperatorType *ot = ((uiBut *)but)->optype;
PropertyRNA *prop = ot->prop;

View File

@ -3408,8 +3408,12 @@ static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
if (data->searchbox) {
if (data->cancel == false) {
BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
uiButSearch *but_search = (uiButSearch *)but;
if ((ui_searchbox_apply(but, data->searchbox) == false) &&
(ui_searchbox_find_index(data->searchbox, but->editstr) == -1)) {
(ui_searchbox_find_index(data->searchbox, but->editstr) == -1) &&
!but_search->all_strings_valid) {
data->cancel = true;
/* ensure menu (popup) too is closed! */

View File

@ -320,6 +320,12 @@ typedef struct uiButSearch {
struct PointerRNA rnasearchpoin;
struct PropertyRNA *rnasearchprop;
/**
* The search box only provides suggestions, it does not force
* the string to match one of the search items when applying.
*/
bool all_strings_valid;
} uiButSearch;
/** Derived struct for #UI_BTYPE_DECORATOR */
@ -1197,7 +1203,8 @@ typedef struct uiRNACollectionSearch {
void ui_rna_collection_search_update_fn(const struct bContext *C,
void *arg,
const char *str,
uiSearchItems *items);
uiSearchItems *items,
const bool is_first);
/* interface_ops.c */
bool ui_jump_to_target_button_poll(struct bContext *C);

View File

@ -468,7 +468,8 @@ static void ui_searchbox_update_fn(bContext *C,
wmWindow *win = CTX_wm_window(C);
WM_tooltip_clear(C, win);
}
search_but->items_update_fn(C, search_but->arg, str, items);
const bool is_first_search = !search_but->but.changed;
search_but->items_update_fn(C, search_but->arg, str, items, is_first_search);
}
/* region is the search box itself */
@ -1052,14 +1053,16 @@ void ui_but_search_refresh(uiButSearch *search_but)
ui_searchbox_update_fn(but->block->evil_C, search_but, but->drawstr, items);
/* Only red-alert when we are sure of it, this can miss cases when >10 matches. */
if (items->totitem == 0) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
else if (items->more == 0) {
if (UI_search_items_find_index(items, but->drawstr) == -1) {
if (!search_but->all_strings_valid) {
/* Only red-alert when we are sure of it, this can miss cases when >10 matches. */
if (items->totitem == 0) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
else if (items->more == 0) {
if (UI_search_items_find_index(items, but->drawstr) == -1) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
}
}
for (x1 = 0; x1 < items->maxitem; x1++) {

View File

@ -990,7 +990,8 @@ static void menu_search_exec_fn(bContext *C, void *UNUSED(arg1), void *arg2)
static void menu_search_update_fn(const bContext *UNUSED(C),
void *arg,
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
struct MenuSearch_Data *data = arg;

View File

@ -59,7 +59,8 @@ static void operator_search_exec_fn(bContext *C, void *UNUSED(arg1), void *arg2)
static void operator_search_update_fn(const bContext *C,
void *UNUSED(arg),
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
GHashIterator iter;

View File

@ -393,7 +393,8 @@ static bool id_search_add(const bContext *C, TemplateID *template_ui, uiSearchIt
static void id_search_cb(const bContext *C,
void *arg_template,
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
TemplateID *template_ui = (TemplateID *)arg_template;
ListBase *lb = template_ui->idlb;
@ -464,7 +465,8 @@ static void id_search_cb_tagged(const bContext *C,
static void id_search_cb_objects_from_scene(const bContext *C,
void *arg_template,
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
TemplateID *template_ui = (TemplateID *)arg_template;
ListBase *lb = template_ui->idlb;
@ -518,7 +520,7 @@ static uiBlock *id_search_menu(bContext *C, ARegion *region, void *arg_litem)
static TemplateID template_ui;
PointerRNA active_item_ptr;
void (*id_search_update_fn)(
const bContext *, void *, const char *, uiSearchItems *) = id_search_cb;
const bContext *, void *, const char *, uiSearchItems *, const bool) = id_search_cb;
/* arg_litem is malloced, can be freed by parent button */
template_ui = *((TemplateID *)arg_litem);

View File

@ -405,7 +405,8 @@ static bool add_collection_search_item(CollItemSearch *cis,
void ui_rna_collection_search_update_fn(const struct bContext *C,
void *arg,
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool is_first)
{
uiRNACollectionSearch *data = arg;
const int flag = RNA_property_flag(data->target_prop);
@ -415,7 +416,7 @@ void ui_rna_collection_search_update_fn(const struct bContext *C,
* match the RNA name exactly. So only for pointer properties, the name can be modified to add
* further UI hints. */
const bool requires_exact_data_name = !is_ptr_target;
const bool skip_filter = data->search_but && !data->search_but->changed;
const bool skip_filter = is_first;
char name_buf[UI_MAX_DRAW_STR];
char *name;
bool has_id_icon = false;

View File

@ -1178,7 +1178,8 @@ static void node_find_create_label(const bNode *node, char *str, int maxlen)
static void node_find_update_fn(const struct bContext *C,
void *UNUSED(arg),
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
SpaceNode *snode = CTX_wm_space_node(C);

View File

@ -554,7 +554,8 @@ static void merged_element_search_fn_recursive(
static void merged_element_search_update_fn(const bContext *UNUSED(C),
void *data,
const char *str,
uiSearchItems *items)
uiSearchItems *items,
const bool UNUSED(is_first))
{
MergedSearchData *search_data = (MergedSearchData *)data;
TreeElement *parent = search_data->parent_element;