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

View File

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