[Bf-blender-cvs] [ae555230136] master: UI: add utility to show icons for the keymap item

Campbell Barton noreply at git.blender.org
Sun Oct 27 17:00:34 CET 2019


Commit: ae55523013642f2897e840f30ad62c74d31df3e0
Author: Campbell Barton
Date:   Mon Oct 28 02:51:26 2019 +1100
Branches: master
https://developer.blender.org/rBae55523013642f2897e840f30ad62c74d31df3e0

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.

===================================================================

M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_templates.c
M	source/blender/makesrna/intern/rna_ui_api.c
M	source/blender/windowmanager/intern/wm_event_system.c

===================================================================

diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index f5721c008b2..8f205173011 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -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,
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index fe484676ddd..021d7733fae 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -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
  * \{ */
diff --git a/source/blender/makesrna/intern/rna_ui_api.c b/source/blender/makesrna/intern/rna_ui_api.c
index 74d1743dfc1..94fe43a0a77 100644
--- a/source/blender/makesrna/intern/rna_ui_api.c
+++ b/source/blender/makesrna/intern/rna_ui_api.c
@@ -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
diff --git a/source/blender/windowmanager/intern/wm_event_system.c b/source/blender/windowmanager/intern/wm_event_system.c
index 97fad9fcf59..1dd37dd2843 100644
--- a/source/blender/windowmanager/intern/wm_event_system.c
+++ b/source/blender/windowmanager/intern/wm_event_system.c
@@ -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;
         }
       }



More information about the Bf-blender-cvs mailing list