From 931973c71af548b80d117efce068bd4ab63c8860 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 18 Apr 2018 22:09:47 -0400 Subject: [PATCH] Use GtkShortcutsWindow for Help tool. --- lib/matplotlib/backends/backend_gtk3.py | 69 ++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 5ed0a8db2d3d..985dbe06c57e 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -847,7 +847,68 @@ def trigger(self, sender, event, data=None): class HelpGTK3(backend_tools.ToolHelpBase): - def trigger(self, *args): + def _normalize_shortcut(self, key): + """ + Convert Matplotlib key presses to GTK+ accelerator identifiers. + + Related to `FigureCanvasGTK3._get_key`. + """ + special = { + 'backspace': 'BackSpace', + 'pagedown': 'Page_Down', + 'pageup': 'Page_Up', + 'scroll_lock': 'Scroll_Lock', + } + + parts = key.split('+') + mods = ['<' + mod + '>' for mod in parts[:-1]] + key = parts[-1] + + if key in special: + key = special[key] + elif len(key) > 1: + key = key.capitalize() + elif key.isupper(): + mods += [''] + + return ''.join(mods) + key + + def _show_shortcuts_window(self): + section = Gtk.ShortcutsSection() + + for name, tool in sorted(self.toolmanager.tools.items()): + if not tool.description: + continue + + # Putting everything in a separate group allows GTK to + # automatically split them into separate columns/pages, which is + # useful because we have lots of shortcuts, some with many keys + # that are very wide. + group = Gtk.ShortcutsGroup() + section.add(group) + # A hack to remove the title since we have no group naming. + group.forall(lambda widget, data: widget.set_visible(False), None) + + shortcut = Gtk.ShortcutsShortcut( + accelerator=' '.join( + self._normalize_shortcut(key) + for key in self.toolmanager.get_tool_keymap(name) + # Will never be sent: + if 'cmd+' not in key), + title=tool.name, + subtitle=tool.description) + group.add(shortcut) + + window = Gtk.ShortcutsWindow( + title='Help', + modal=True, + transient_for=self._figure.canvas.get_toplevel()) + section.show() # Must be done explicitly before add! + window.add(section) + + window.show_all() + + def _show_shortcuts_dialog(self): dialog = Gtk.MessageDialog( self._figure.canvas.get_toplevel(), 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, self._get_help_text(), @@ -855,6 +916,12 @@ def trigger(self, *args): dialog.run() dialog.destroy() + def trigger(self, *args): + if Gtk.check_version(3, 20, 0) is None: + self._show_shortcuts_window() + else: + self._show_shortcuts_dialog() + # Define the file to use as the GTk icon if sys.platform == 'win32':