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.
This commit is contained in:
Campbell Barton
2018-11-28 23:52:05 +11:00
parent 43248164a4
commit 1a8d998868
2 changed files with 23 additions and 10 deletions

View File

@@ -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,
)