UI: simplify tool-tip logic for operators

- Use WM_operatortype_description to get the operator description.
- Pass properties to WM_operatortype_name,
  so the operator name callback is used.
- Add UI_but_operatortype_get_from_enum_menu function
  to access the operator from enum menus.
- Change WM_operatortype_description to return NULL when there is no
  description, use WM_operatortype_description_or_name
  when either can be used.
This commit is contained in:
2020-09-02 12:54:06 +10:00
parent ddea2f234f
commit 89ed6b1293
6 changed files with 56 additions and 22 deletions

View File

@@ -1863,6 +1863,8 @@ uiBlock *uiLayoutGetBlock(uiLayout *layout);
void uiLayoutSetFunc(uiLayout *layout, uiMenuHandleFunc handlefunc, void *argv);
void uiLayoutSetContextPointer(uiLayout *layout, const char *name, struct PointerRNA *ptr);
void uiLayoutContextCopy(uiLayout *layout, struct bContextStore *context);
struct wmOperatorType *UI_but_operatortype_get_from_enum_menu(struct uiBut *but,
PropertyRNA **r_prop);
struct MenuType *UI_but_menutype_get(uiBut *but);
struct PanelType *UI_but_paneltype_get(uiBut *but);
void UI_menutype_draw(struct bContext *C, struct MenuType *mt, struct uiLayout *layout);

View File

@@ -6758,9 +6758,6 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...)
else if (but->tip && but->tip[0]) {
tmp = BLI_strdup(but->tip);
}
else if (but->optype && but->optype->get_description) {
tmp = WM_operatortype_description(C, but->optype, but->opptr);
}
else {
type = BUT_GET_RNA_TIP; /* Fail-safe solution... */
}
@@ -6805,13 +6802,10 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...)
}
else if (but->optype) {
if (type == BUT_GET_RNA_LABEL) {
tmp = BLI_strdup(WM_operatortype_name(but->optype, NULL));
tmp = BLI_strdup(WM_operatortype_name(but->optype, but->opptr));
}
else {
const char *t = RNA_struct_ui_description(but->optype->srna);
if (t && t[0]) {
tmp = BLI_strdup(t);
}
tmp = WM_operatortype_description(C, but->optype, but->opptr);
}
}
else if (ELEM(but->type, UI_BTYPE_MENU, UI_BTYPE_PULLDOWN, UI_BTYPE_POPOVER)) {
@@ -6833,6 +6827,18 @@ void UI_but_string_info_get(bContext *C, uiBut *but, ...)
}
}
if (tmp == NULL) {
wmOperatorType *ot = UI_but_operatortype_get_from_enum_menu(but, NULL);
if (ot) {
if (type == BUT_GET_RNA_LABEL) {
tmp = BLI_strdup(WM_operatortype_name(ot, NULL));
}
else {
tmp = WM_operatortype_description(C, ot, NULL);
}
}
}
if (tmp == NULL) {
PanelType *pt = UI_but_paneltype_get(but);
if (pt) {

View File

@@ -3404,14 +3404,7 @@ void uiItemMenuEnumO_ptr(uiLayout *layout,
BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
lvl->opcontext = layout->root->opcontext;
but = ui_item_menu(layout,
name,
icon,
menu_item_enum_opname_menu,
NULL,
lvl,
RNA_struct_ui_description(ot->srna),
true);
but = ui_item_menu(layout, name, icon, menu_item_enum_opname_menu, NULL, lvl, NULL, true);
/* add hotkey here, lower UI code can't detect it */
if ((layout->root->block->flag & UI_BLOCK_LOOP) && (ot->prop && ot->invoke)) {
@@ -5500,6 +5493,24 @@ void uiLayoutSetContextFromBut(uiLayout *layout, uiBut *but)
}
}
/* this is a bit of a hack but best keep it in one place at least */
wmOperatorType *UI_but_operatortype_get_from_enum_menu(uiBut *but, PropertyRNA **r_prop)
{
if (r_prop != NULL) {
*r_prop = NULL;
}
if (but->menu_create_func == menu_item_enum_opname_menu) {
MenuItemLevel *lvl = but->func_argN;
wmOperatorType *ot = WM_operatortype_find(lvl->opname, false);
if ((ot != NULL) && (r_prop != NULL)) {
*r_prop = RNA_struct_type_find_property(ot->srna, lvl->propname);
}
return ot;
}
return NULL;
}
/* this is a bit of a hack but best keep it in one place at least */
MenuType *UI_but_menutype_get(uiBut *but)
{

View File

@@ -1075,7 +1075,7 @@ static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz)
NULL;
if (gzop != NULL) {
/* Description */
char *info = WM_operatortype_description(C, gzop->type, &gzop->ptr);
char *info = WM_operatortype_description_or_name(C, gzop->type, &gzop->ptr);
if (info != NULL) {
char *text = info;

View File

@@ -578,6 +578,9 @@ const char *WM_operatortype_name(struct wmOperatorType *ot, struct PointerRNA *p
char *WM_operatortype_description(struct bContext *C,
struct wmOperatorType *ot,
struct PointerRNA *properties);
char *WM_operatortype_description_or_name(struct bContext *C,
struct wmOperatorType *ot,
struct PointerRNA *properties);
/* wm_operator_utils.c */
void WM_operator_type_modal_from_exec_for_object_edit_coords(struct wmOperatorType *ot);

View File

@@ -611,15 +611,27 @@ char *WM_operatortype_description(struct bContext *C,
}
const char *info = RNA_struct_ui_description(ot->srna);
if (!(info && info[0])) {
info = RNA_struct_ui_name(ot->srna);
}
if (info && info[0]) {
return BLI_strdup(info);
}
return NULL;
}
/**
* Use when we want a label, preferring the description.
*/
char *WM_operatortype_description_or_name(struct bContext *C,
struct wmOperatorType *ot,
struct PointerRNA *properties)
{
char *text = WM_operatortype_description(C, ot, properties);
if (text == NULL) {
const char *text_orig = WM_operatortype_name(ot, properties);
if (text_orig != NULL) {
text = BLI_strdup(text_orig);
}
}
return text;
}
/** \} */