UI: add UILayout.prop_popover_enum function
Support for RNA enum buttons that activate popovers when clicked. This means we get useful tooltips, shortcuts and Ctrl-Wheel cycling over enum items. It also avoids inconvenient & slow access of enum values currently done via RNA type lookups on the type to get the name & icon to use for a regular popover button. Resolves T57738
This commit is contained in:
@@ -1239,7 +1239,13 @@ void uiItemFullOMenuHold_ptr(
|
||||
PointerRNA *r_opptr);
|
||||
|
||||
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon);
|
||||
void uiItemFullR(uiLayout *layout, struct PointerRNA *ptr, struct PropertyRNA *prop, int index, int value, int flag, const char *name, int icon);
|
||||
void uiItemFullR(
|
||||
uiLayout *layout, struct PointerRNA *ptr, struct PropertyRNA *prop, int index, int value, int flag,
|
||||
const char *name, int icon);
|
||||
void uiItemFullR_with_popover(
|
||||
uiLayout *layout, struct PointerRNA *ptr, struct PropertyRNA *prop, int index, int value, int flag,
|
||||
const char *name, int icon,
|
||||
const char *panel_type);
|
||||
void uiItemEnumR_prop(uiLayout *layout, const char *name, int icon, struct PointerRNA *ptr, PropertyRNA *prop, int value);
|
||||
void uiItemEnumR(uiLayout *layout, const char *name, int icon, struct PointerRNA *ptr, const char *propname, int value);
|
||||
void uiItemEnumR_string_prop(uiLayout *layout, struct PointerRNA *ptr, PropertyRNA *prop, const char *value, const char *name, int icon);
|
||||
|
||||
@@ -85,6 +85,7 @@
|
||||
/* prototypes. */
|
||||
static void ui_but_to_pixelrect(struct rcti *rect, const struct ARegion *ar, struct uiBlock *block, struct uiBut *but);
|
||||
static void ui_def_but_rna__menu(bContext *UNUSED(C), uiLayout *layout, void *but_p);
|
||||
static void ui_def_but_rna__popover(bContext *UNUSED(C), uiLayout *layout, void *but_p);
|
||||
|
||||
/* avoid unneeded calls to ui_but_value_get */
|
||||
#define UI_BUT_VALUE_UNSET DBL_MAX
|
||||
@@ -1149,7 +1150,7 @@ static bool ui_but_event_property_operator_string(
|
||||
if ((but->type == UI_BTYPE_BUT_MENU) &&
|
||||
(but_parent && but_parent->rnaprop) &&
|
||||
(RNA_property_type(but_parent->rnaprop) == PROP_ENUM) &&
|
||||
(but_parent->menu_create_func == ui_def_but_rna__menu))
|
||||
ELEM(but_parent->menu_create_func, ui_def_but_rna__menu, ui_def_but_rna__popover))
|
||||
{
|
||||
prop_enum_value = (int)but->hardmin;
|
||||
ptr = &but_parent->rnapoin;
|
||||
@@ -3831,6 +3832,35 @@ static void ui_def_but_rna__menu(bContext *UNUSED(C), uiLayout *layout, void *bu
|
||||
block->flag |= UI_BLOCK_IS_FLIP;
|
||||
}
|
||||
|
||||
static void ui_def_but_rna__popover(bContext *C, uiLayout *layout, void *but_p)
|
||||
{
|
||||
uiBut *but = but_p;
|
||||
const char *panel_type = but->func_argN;
|
||||
PanelType *pt = WM_paneltype_find(panel_type, true);
|
||||
if (pt) {
|
||||
ui_item_paneltype_func(C, layout, pt);
|
||||
}
|
||||
else {
|
||||
char msg[256];
|
||||
SNPRINTF(msg, "Missing Panel: %s", panel_type);
|
||||
uiItemL(layout, msg, ICON_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
void ui_but_rna_menu_convert_to_popover(uiBut *but, const char *panel_type)
|
||||
{
|
||||
BLI_assert(but->type == UI_BTYPE_MENU);
|
||||
BLI_assert(but->menu_create_func == ui_def_but_rna__menu);
|
||||
BLI_assert((void *)but->poin == but);
|
||||
but->menu_create_func = ui_def_but_rna__popover;
|
||||
but->func_argN = BLI_strdup(panel_type);
|
||||
}
|
||||
|
||||
bool ui_but_menu_draw_as_popover(const uiBut *but)
|
||||
{
|
||||
return (but->menu_create_func == ui_def_but_rna__popover);
|
||||
}
|
||||
|
||||
static void ui_but_submenu_enable(uiBlock *block, uiBut *but)
|
||||
{
|
||||
but->flag |= UI_BUT_ICON_SUBMENU;
|
||||
|
||||
@@ -3725,13 +3725,14 @@ static void ui_block_open_begin(bContext *C, uiBut *but, uiHandleButtonData *dat
|
||||
}
|
||||
break;
|
||||
case UI_BTYPE_MENU:
|
||||
BLI_assert(but->menu_create_func);
|
||||
menufunc = but->menu_create_func;
|
||||
arg = but->poin;
|
||||
break;
|
||||
case UI_BTYPE_POPOVER:
|
||||
BLI_assert(but->menu_create_func);
|
||||
popoverfunc = but->menu_create_func;
|
||||
if ((but->type == UI_BTYPE_POPOVER) || ui_but_menu_draw_as_popover(but)) {
|
||||
popoverfunc = but->menu_create_func;
|
||||
}
|
||||
else {
|
||||
menufunc = but->menu_create_func;
|
||||
}
|
||||
arg = but->poin;
|
||||
break;
|
||||
case UI_BTYPE_COLOR:
|
||||
|
||||
@@ -485,6 +485,9 @@ extern uiButExtraIconType ui_but_icon_extra_get(uiBut *but);
|
||||
|
||||
extern void ui_but_default_set(struct bContext *C, const bool all, const bool use_afterfunc);
|
||||
|
||||
extern void ui_but_rna_menu_convert_to_popover(struct uiBut *but, const char *panel_type);
|
||||
extern bool ui_but_menu_draw_as_popover(const uiBut *but);
|
||||
|
||||
extern void ui_but_update(uiBut *but);
|
||||
extern void ui_but_update_edited(uiBut *but);
|
||||
extern bool ui_but_is_float(const uiBut *but) ATTR_WARN_UNUSED_RESULT;
|
||||
|
||||
@@ -1990,6 +1990,33 @@ void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, int flag,
|
||||
uiItemFullR(layout, ptr, prop, RNA_NO_INDEX, 0, flag, name, icon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a wrapper function since re-implementing all the logic in this function would be messy.
|
||||
*/
|
||||
void uiItemFullR_with_popover(
|
||||
uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index, int value, int flag, const char *name, int icon,
|
||||
const char *panel_type)
|
||||
{
|
||||
uiBlock *block = layout->root->block;
|
||||
uiBut *but = block->buttons.last;
|
||||
uiItemFullR(layout, ptr, prop, index, value, flag, name, icon);
|
||||
but = but->next;
|
||||
while (but) {
|
||||
if (but->rnaprop == prop && but->type == UI_BTYPE_MENU) {
|
||||
ui_but_rna_menu_convert_to_popover(but, panel_type);
|
||||
break;
|
||||
}
|
||||
but = but->next;
|
||||
}
|
||||
if (but) {
|
||||
const char *propname = RNA_property_identifier(prop);
|
||||
ui_item_disabled(layout, panel_type);
|
||||
RNA_warning(
|
||||
"property could not use a popover: %s.%s (%s)",
|
||||
RNA_struct_identifier(ptr->type), propname, panel_type);
|
||||
}
|
||||
}
|
||||
|
||||
void uiItemEnumR_prop(uiLayout *layout, const char *name, int icon, struct PointerRNA *ptr, PropertyRNA *prop, int value)
|
||||
{
|
||||
if (RNA_property_type(prop) != PROP_ENUM) {
|
||||
|
||||
@@ -133,6 +133,28 @@ static void rna_uiItemMenuEnumR(
|
||||
uiItemMenuEnumR_prop(layout, ptr, prop, name, icon);
|
||||
}
|
||||
|
||||
static void rna_uiItemPopoverPanelEnumR(
|
||||
uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *name,
|
||||
const char *text_ctxt, bool translate, int icon,
|
||||
const char *panel_type)
|
||||
{
|
||||
PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
|
||||
|
||||
if (!prop) {
|
||||
RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
|
||||
return;
|
||||
}
|
||||
if (RNA_property_type(prop) != PROP_ENUM) {
|
||||
RNA_warning("property is not an enum: %s.%s", RNA_struct_identifier(ptr->type), propname);
|
||||
return;
|
||||
}
|
||||
int flag = 0;
|
||||
|
||||
/* Get translated name (label). */
|
||||
name = rna_translate_ui_text(name, text_ctxt, NULL, prop, translate);
|
||||
uiItemFullR_with_popover(layout, ptr, prop, -1, 0, flag, name, icon, panel_type);
|
||||
}
|
||||
|
||||
static void rna_uiItemTabsEnumR(
|
||||
uiLayout *layout, bContext *C,
|
||||
struct PointerRNA *ptr, const char *propname,
|
||||
@@ -639,6 +661,12 @@ void RNA_api_ui_layout(StructRNA *srna)
|
||||
api_ui_item_rna_common(func);
|
||||
api_ui_item_common(func);
|
||||
|
||||
func = RNA_def_function(srna, "prop_popover_enum", "rna_uiItemPopoverPanelEnumR");
|
||||
api_ui_item_rna_common(func);
|
||||
api_ui_item_common(func);
|
||||
parm = RNA_def_string(func, "panel", NULL, 0, "", "Identifier of the panel");
|
||||
RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
|
||||
|
||||
func = RNA_def_function(srna, "prop_tabs_enum", "rna_uiItemTabsEnumR");
|
||||
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
|
||||
api_ui_item_rna_common(func);
|
||||
|
||||
Reference in New Issue
Block a user