UI: add utility to show icons for the keymap item
This is useful for drawing keymap items into the header or status bar While these icons are available directly, mapping them from the keymap item isn't trivial.
This commit is contained in:
parent
c036db9f57
commit
ae55523013
|
@ -2021,6 +2021,12 @@ void uiTemplateEditModeSelection(uiLayout *layout, struct bContext *C);
|
|||
void uiTemplateReportsBanner(uiLayout *layout, struct bContext *C);
|
||||
void uiTemplateInputStatus(uiLayout *layout, struct bContext *C);
|
||||
void uiTemplateKeymapItemProperties(uiLayout *layout, struct PointerRNA *ptr);
|
||||
|
||||
bool uiTemplateEventFromKeymapItem(struct uiLayout *layout,
|
||||
const char *text,
|
||||
const struct wmKeyMapItem *kmi,
|
||||
bool text_fallback);
|
||||
|
||||
void uiTemplateComponentMenu(uiLayout *layout,
|
||||
struct PointerRNA *ptr,
|
||||
const char *propname,
|
||||
|
|
|
@ -6656,6 +6656,42 @@ void uiTemplateKeymapItemProperties(uiLayout *layout, PointerRNA *ptr)
|
|||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Event Icon Template
|
||||
*
|
||||
* \{ */
|
||||
|
||||
bool uiTemplateEventFromKeymapItem(struct uiLayout *layout,
|
||||
const char *text,
|
||||
const struct wmKeyMapItem *kmi,
|
||||
bool text_fallback)
|
||||
{
|
||||
bool ok = false;
|
||||
|
||||
int icon_mod[4];
|
||||
#ifdef WITH_HEADLESS
|
||||
int icon = 0;
|
||||
#else
|
||||
int icon = UI_icon_from_keymap_item(kmi, icon_mod);
|
||||
#endif
|
||||
if (icon != 0) {
|
||||
for (int j = 0; j < ARRAY_SIZE(icon_mod) && icon_mod[j]; j++) {
|
||||
uiItemL(layout, "", icon_mod[j]);
|
||||
}
|
||||
uiItemL(layout, text, icon);
|
||||
ok = true;
|
||||
}
|
||||
else if (text_fallback) {
|
||||
const char *event_text = WM_key_event_string(kmi->type, true);
|
||||
uiItemL(layout, event_text, ICON_NONE);
|
||||
uiItemL(layout, text, ICON_NONE);
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Color Management Template
|
||||
* \{ */
|
||||
|
|
|
@ -479,6 +479,14 @@ static void rna_uiTemplatePathBuilder(uiLayout *layout,
|
|||
uiTemplatePathBuilder(layout, ptr, propname, root_ptr, name);
|
||||
}
|
||||
|
||||
static void rna_uiTemplateEventFromKeymapItem(
|
||||
uiLayout *layout, wmKeyMapItem *kmi, const char *name, const char *text_ctxt, bool translate)
|
||||
{
|
||||
/* Get translated name (label). */
|
||||
name = rna_translate_ui_text(name, text_ctxt, NULL, NULL, translate);
|
||||
uiTemplateEventFromKeymapItem(layout, name, kmi, true);
|
||||
}
|
||||
|
||||
static int rna_ui_get_rnaptr_icon(bContext *C, PointerRNA *ptr_icon)
|
||||
{
|
||||
return UI_rnaptr_icon_get(C, ptr_icon, RNA_struct_ui_icon(ptr_icon->type), false);
|
||||
|
@ -1526,6 +1534,15 @@ void RNA_api_ui_layout(StructRNA *srna)
|
|||
parm = RNA_def_pointer(func, "params", "FileSelectParams", "", "");
|
||||
RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
|
||||
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
|
||||
|
||||
func = RNA_def_function(
|
||||
srna, "template_event_from_keymap_item", "rna_uiTemplateEventFromKeymapItem");
|
||||
RNA_def_function_ui_description(func, "Display keymap item as icons/text");
|
||||
parm = RNA_def_property(func, "item", PROP_POINTER, PROP_NONE);
|
||||
RNA_def_property_struct_type(parm, "KeyMapItem");
|
||||
RNA_def_property_ui_text(parm, "Item", "");
|
||||
RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
|
||||
api_ui_item_common_text(func);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -5318,17 +5318,7 @@ bool WM_window_modal_keymap_status_draw(bContext *UNUSED(C), wmWindow *win, uiLa
|
|||
/* Assume release events just disable something which was toggled on. */
|
||||
continue;
|
||||
}
|
||||
int icon_mod[4];
|
||||
#ifdef WITH_HEADLESS
|
||||
int icon = 0;
|
||||
#else
|
||||
int icon = UI_icon_from_keymap_item(kmi, icon_mod);
|
||||
#endif
|
||||
if (icon != 0) {
|
||||
for (int j = 0; j < ARRAY_SIZE(icon_mod) && icon_mod[j]; j++) {
|
||||
uiItemL(row, "", icon_mod[j]);
|
||||
}
|
||||
uiItemL(row, items[i].name, icon);
|
||||
if (uiTemplateEventFromKeymapItem(row, items[i].name, kmi, false)) {
|
||||
show_text = false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue