UI: Show categories in operator search popup

Gives better context especially when operators have generic names.
This commit is contained in:
Campbell Barton 2016-03-02 14:05:49 +11:00
parent 69b66d549b
commit d49985ce48
Notes: blender-bot 2023-02-14 08:08:56 +01:00
Referenced by commit bee0a7587b, Fix crash accessing nodes search menu
Referenced by issue #47660, Crash on invoke_search_popup
Referenced by issue #47665, multiple scenes in different windows -> "DAG zero... not allowed to happen!"
5 changed files with 108 additions and 3 deletions

View File

@ -4411,7 +4411,7 @@ uiBut *uiDefSearchButO_ptr(
but = uiDefSearchBut(block, arg, retval, icon, maxlen, x, y, width, height, a1, a2, tip);
UI_but_func_search_set(
but, ui_searchbox_create_generic, operator_enum_search_cb,
but, ui_searchbox_create_operator, operator_enum_search_cb,
but, operator_enum_call_cb, NULL);
but->optype = ot;

View File

@ -593,6 +593,7 @@ void ui_color_picker_to_rgb(float r_cp0, float r_cp1, float r_cp2, float *r, flo
/* searchbox for string button */
ARegion *ui_searchbox_create_generic(struct bContext *C, struct ARegion *butregion, uiBut *but);
ARegion *ui_searchbox_create_operator(struct bContext *C, struct ARegion *butregion, uiBut *but);
bool ui_searchbox_inside(struct ARegion *ar, int x, int y);
int ui_searchbox_find_index(struct ARegion *ar, const char *name);
void ui_searchbox_update(struct bContext *C, struct ARegion *ar, uiBut *but, const bool reset);

View File

@ -1328,6 +1328,110 @@ ARegion *ui_searchbox_create_generic(bContext *C, ARegion *butregion, uiBut *but
return ar;
}
/**
* Similar to Python's `str.title` except...
*
* - we know words are upper case and ascii only.
* - '_' are replaces by spaces.
*/
static void str_tolower_titlecaps_ascii(char *str, const size_t len)
{
size_t i;
bool prev_delim = true;
for (i = 0; (i < len) && str[i]; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
if (prev_delim == false) {
str[i] += 'a' - 'A';
}
}
else if (str[i] == '_') {
str[i] = ' ';
}
prev_delim = ELEM(str[i], ' ') || (str[i] >= '0' && str[i] <= '9');
}
}
static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARegion *ar)
{
uiSearchboxData *data = ar->regiondata;
/* pixel space */
wmOrtho2_region_pixelspace(ar);
if (data->noback == false)
ui_draw_search_back(NULL, NULL, &data->bbox); /* style not used yet */
/* draw text */
if (data->items.totitem) {
rcti rect;
int a;
/* draw items */
for (a = 0; a < data->items.totitem; a++) {
rcti rect_pre, rect_post;
ui_searchbox_butrect(&rect, data, a);
rect_pre = rect;
rect_post = rect;
rect_pre.xmax = rect_post.xmin = rect.xmin + ((rect.xmax - rect.xmin) / 4);
/* widget itself */
{
wmOperatorType *ot = data->items.pointers[a];
int state = (a == data->active) ? UI_ACTIVE : 0;
char text_pre[128];
char *text_pre_p = strstr(ot->idname, "_OT_");
if (text_pre_p == NULL) {
text_pre[0] = '\0';
}
else {
int text_pre_len;
text_pre_p += 1;
text_pre_len = BLI_strncpy_rlen(
text_pre, ot->idname, min_ii(sizeof(text_pre), text_pre_p - ot->idname));
text_pre[text_pre_len] = ':';
text_pre[text_pre_len + 1] = '\0';
str_tolower_titlecaps_ascii(text_pre, sizeof(text_pre));
}
rect_pre.xmax += 4; /* sneaky, avoid showing ugly margin */
ui_draw_menu_item(&data->fstyle, &rect_pre, text_pre, data->items.icons[a], state, false);
ui_draw_menu_item(&data->fstyle, &rect_post, data->items.names[a], 0, state, data->use_sep);
}
}
/* indicate more */
if (data->items.more) {
ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
glEnable(GL_BLEND);
UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymin - 9, ICON_TRIA_DOWN);
glDisable(GL_BLEND);
}
if (data->items.offset) {
ui_searchbox_butrect(&rect, data, 0);
glEnable(GL_BLEND);
UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymax - 7, ICON_TRIA_UP);
glDisable(GL_BLEND);
}
}
}
ARegion *ui_searchbox_create_operator(bContext *C, ARegion *butregion, uiBut *but)
{
ARegion *ar;
ar = ui_searchbox_create_generic(C, butregion, but);
ar->type->draw = ui_searchbox_region_draw_cb__operator;
return ar;
}
void ui_searchbox_free(bContext *C, ARegion *ar)
{
ui_region_temp_remove(C, CTX_wm_screen(C), ar);

View File

@ -3308,7 +3308,7 @@ static void operator_search_cb(const bContext *C, void *UNUSED(arg), const char
void UI_but_func_operator_search(uiBut *but)
{
UI_but_func_search_set(
but, ui_searchbox_create_generic, operator_search_cb,
but, ui_searchbox_create_operator, operator_search_cb,
NULL, operator_call_cb, NULL);
}

View File

@ -1945,7 +1945,7 @@ static int wm_search_menu_invoke(bContext *C, wmOperator *UNUSED(op), const wmEv
{
struct SearchPopupInit_Data data = {
.size = {
UI_searchbox_size_x(),
UI_searchbox_size_x() * 2,
UI_searchbox_size_y(),
},
};