[Bf-blender-cvs] [1a8d9988689] blender2.8: Tool System: support for dynamic tooltips

Campbell Barton noreply at git.blender.org
Wed Nov 28 13:54:35 CET 2018


Commit: 1a8d9988689fe2742c590438a4c844dcb1e584bc
Author: Campbell Barton
Date:   Wed Nov 28 23:52:05 2018 +1100
Branches: blender2.8
https://developer.blender.org/rB1a8d9988689fe2742c590438a4c844dcb1e584bc

Tool System: support for dynamic tooltips

Tools can define a function that generates the tooltip using a function,
this takes the tools keymap as an argument which can be used
to extract keys to include in the tip.

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

M	release/scripts/startup/bl_ui/space_toolsystem_common.py
M	release/scripts/startup/bl_ui/space_toolsystem_toolbar.py

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

diff --git a/release/scripts/startup/bl_ui/space_toolsystem_common.py b/release/scripts/startup/bl_ui/space_toolsystem_common.py
index 9349f776c99..a52b9d07c2a 100644
--- a/release/scripts/startup/bl_ui/space_toolsystem_common.py
+++ b/release/scripts/startup/bl_ui/space_toolsystem_common.py
@@ -64,7 +64,8 @@ ToolDef = namedtuple(
     (
         # The name to display in the interface.
         "text",
-        # Description (for tooltip), when not set, use the description of 'operator'.
+        # Description (for tooltip), when not set, use the description of 'operator',
+        # may be a string or a 'function(context, item, keymap) -> string'.
         "description",
         # The name of the icon to use (found in ``release/datafiles/icons``) or None for no icon.
         "icon",
@@ -685,17 +686,17 @@ def description_from_name(context, space_type, text, *, use_operator=True):
     # Custom description.
     description = item.description
     if description is not None:
+        if callable(description):
+            km = _keymap_from_item(context, item)
+            return description(context, item, km)
         return description
 
     # Extract from the operator.
     if use_operator:
         operator = item.operator
-
         if operator is None:
             if item.keymap is not None:
-                wm = context.window_manager
-                keyconf = wm.keyconfigs.active
-                km = keyconf.keymaps.get(item.keymap[0])
+                km = _keymap_from_item(context, item)
                 if km is not None:
                     for kmi in km.keymap_items:
                         if kmi.active:
@@ -721,6 +722,14 @@ def keymap_from_name(context, space_type, text):
     return ""
 
 
+def _keymap_from_item(context, item):
+    if item.keymap is not None:
+        wm = context.window_manager
+        keyconf = wm.keyconfigs.active
+        return keyconf.keymaps.get(item.keymap[0])
+    return None
+
+
 classes = (
     WM_MT_toolsystem_submenu,
 )
diff --git a/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py b/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
index 2274069be41..f0ccfd98e12 100644
--- a/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
@@ -92,16 +92,20 @@ class _defs_view3d_generic:
 
     @ToolDef.from_fn
     def ruler():
-        return dict(
-            text="Measure",
-            description=(
+        def description(context, item, km):
+            return (
                 "Measure distance and angles.\n"
-                "\u2022 Drag anywhere for new measurement.\n"
+                "\u2022 {} anywhere for new measurement.\n"
                 "\u2022 Drag ruler segment to measure an angle.\n"
                 "\u2022 Drag ruler outside the view to remove.\n"
                 "\u2022 Ctrl to snap.\n"
                 "\u2022 Shift to measure surface thickness"
-            ),
+            ).format(
+                km.keymap_items[0].to_string()
+            )
+        return dict(
+            text="Measure",
+            description=description,
             icon="ops.view3d.ruler",
             widget="VIEW3D_GGT_ruler",
             keymap=(),



More information about the Bf-blender-cvs mailing list