UI support for showing candidates for string properties

Currently strings are used for cases where a list of identifiers would
be useful to show.

Add support for string properties to reference a callback to populate
candidates to show when editing a string. The user isn't prevented from
typing in text not found in this list, it's just useful as a reference.

Support for expanding the following strings has been added:

- Operator, menu & panel identifiers in the keymap editor.
- WM operators that reference data-paths expand using the
  Python-consoles auto-complete functionality.
- Names of keying sets for insert/delete keyframe operators.

Details:

- `bpy.props.StringProperty` takes an option `search` callback.

- A new string callback has been added, set via
  `RNA_def_property_string_search_func` or
  `RNA_def_property_string_search_func_runtime`.

- Addresses usability issue highlighted by T89560,
  where setting keying set identifiers as strings isn't practical.

- Showing additional right-aligned text in the search results is
  supported but disabled by default as the text is too cramped in most
  string search popups where the feature would make sense. It could be
  enabled as part of other layout tweaks.

Reviewed By: brecht

Ref D14986
This commit is contained in:
2022-05-20 14:30:17 +10:00
parent 11480763b6
commit 3f3d82cfe9
21 changed files with 766 additions and 70 deletions

View File

@@ -99,3 +99,21 @@ bool WM_menutype_poll(bContext *C, MenuType *mt)
}
return true;
}
void WM_menutype_idname_visit_for_search(const bContext *UNUSED(C),
PointerRNA *UNUSED(ptr),
PropertyRNA *UNUSED(prop),
const char *UNUSED(edit_text),
StringPropertySearchVisitFunc visit_fn,
void *visit_user_data)
{
GHashIterator gh_iter;
GHASH_ITER (gh_iter, menutypes_hash) {
MenuType *mt = BLI_ghashIterator_getValue(&gh_iter);
StringPropertySearchVisitParams visit_params = {NULL};
visit_params.text = mt->idname;
visit_params.info = mt->label;
visit_fn(visit_user_data, &visit_params);
}
}

View File

@@ -246,6 +246,27 @@ void WM_operatortype_last_properties_clear_all(void)
}
}
void WM_operatortype_idname_visit_for_search(const bContext *UNUSED(C),
PointerRNA *UNUSED(ptr),
PropertyRNA *UNUSED(prop),
const char *UNUSED(edit_text),
StringPropertySearchVisitFunc visit_fn,
void *visit_user_data)
{
GHashIterator gh_iter;
GHASH_ITER (gh_iter, global_ops_hash) {
wmOperatorType *ot = BLI_ghashIterator_getValue(&gh_iter);
char idname_py[OP_MAX_TYPENAME];
WM_operator_py_idname(idname_py, ot->idname);
StringPropertySearchVisitParams visit_params = {NULL};
visit_params.text = idname_py;
visit_params.info = ot->name;
visit_fn(visit_user_data, &visit_params);
}
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@@ -1870,7 +1870,14 @@ static void WM_OT_call_menu(wmOperatorType *ot)
ot->flag = OPTYPE_INTERNAL;
RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the menu");
PropertyRNA *prop;
prop = RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the menu");
RNA_def_property_string_search_func_runtime(
prop,
WM_menutype_idname_visit_for_search,
/* Only a suggestion as menu items may be referenced from add-ons that have been disabled. */
(PROP_STRING_SEARCH_SORT | PROP_STRING_SEARCH_SUGGESTION));
}
static int wm_call_pie_menu_invoke(bContext *C, wmOperator *op, const wmEvent *event)
@@ -1902,7 +1909,14 @@ static void WM_OT_call_menu_pie(wmOperatorType *ot)
ot->flag = OPTYPE_INTERNAL;
RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the pie menu");
PropertyRNA *prop;
prop = RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the pie menu");
RNA_def_property_string_search_func_runtime(
prop,
WM_menutype_idname_visit_for_search,
/* Only a suggestion as menu items may be referenced from add-ons that have been disabled. */
(PROP_STRING_SEARCH_SORT | PROP_STRING_SEARCH_SUGGESTION));
}
static int wm_call_panel_exec(bContext *C, wmOperator *op)
@@ -1938,6 +1952,11 @@ static void WM_OT_call_panel(wmOperatorType *ot)
PropertyRNA *prop;
prop = RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the menu");
RNA_def_property_string_search_func_runtime(
prop,
WM_paneltype_idname_visit_for_search,
/* Only a suggestion as menu items may be referenced from add-ons that have been disabled. */
(PROP_STRING_SEARCH_SORT | PROP_STRING_SEARCH_SUGGESTION));
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
prop = RNA_def_boolean(ot->srna, "keep_open", true, "Keep Open", "");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);

View File

@@ -65,3 +65,21 @@ void WM_paneltype_clear(void)
{
BLI_ghash_free(g_paneltypes_hash, NULL, NULL);
}
void WM_paneltype_idname_visit_for_search(const bContext *UNUSED(C),
PointerRNA *UNUSED(ptr),
PropertyRNA *UNUSED(prop),
const char *UNUSED(edit_text),
StringPropertySearchVisitFunc visit_fn,
void *visit_user_data)
{
GHashIterator gh_iter;
GHASH_ITER (gh_iter, g_paneltypes_hash) {
PanelType *pt = BLI_ghashIterator_getValue(&gh_iter);
StringPropertySearchVisitParams visit_params = {NULL};
visit_params.text = pt->idname;
visit_params.info = pt->label;
visit_fn(visit_user_data, &visit_params);
}
}