From 5992b8185086fc7633caa27aafe8e7534e2680cf Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Wed, 18 May 2016 21:54:20 -0400 Subject: [PATCH] Don't show keyhint if there are no hints. Currently, the keyhint window is shown even if the keystring matches no possible bindings. This causes an empty keyhint window to hang around after entering hinting mode. Instead, the window is now hidden if no bindings match the current keystring. Resolves #1507. --- qutebrowser/misc/keyhintwidget.py | 40 +++++++++++++++++-------------- tests/unit/misc/test_keyhints.py | 8 +++++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/qutebrowser/misc/keyhintwidget.py b/qutebrowser/misc/keyhintwidget.py index 9a4d99d95..270983b74 100644 --- a/qutebrowser/misc/keyhintwidget.py +++ b/qutebrowser/misc/keyhintwidget.py @@ -94,28 +94,32 @@ class KeyHintView(QLabel): self.hide() return + keyconf = objreg.get('key-config') + is_special = lambda k: k.startswith('<') and k.endswith('>') + bindings = [(key, cmd) for (key, cmd) + in keyconf.get_bindings_for(modename).items() + if key.startswith(prefix) and not is_special(key)] + + if not bindings: + return + self.show() suffix_color = html.escape(config.get('colors', 'keyhint.fg.suffix')) text = '' - keyconf = objreg.get('key-config') - # this is only fired in normal mode - for key, cmd in keyconf.get_bindings_for(modename).items(): - # for now, special keys can't be part of keychains, so ignore them - is_special_binding = key.startswith('<') and key.endswith('>') - if key.startswith(prefix) and not is_special_binding: - text += ( - "" - "{}" - "{}" - "{}" - "" - ).format( - html.escape(prefix), - suffix_color, - html.escape(key[len(prefix):]), - html.escape(cmd) - ) + for key, cmd in bindings: + text += ( + "" + "{}" + "{}" + "{}" + "" + ).format( + html.escape(prefix), + suffix_color, + html.escape(key[len(prefix):]), + html.escape(cmd) + ) text = '{}
'.format(text) self.setText(text) diff --git a/tests/unit/misc/test_keyhints.py b/tests/unit/misc/test_keyhints.py index 85f6c55ce..339e09df5 100644 --- a/tests/unit/misc/test_keyhints.py +++ b/tests/unit/misc/test_keyhints.py @@ -110,3 +110,11 @@ def test_color_switch(keyhint, config_stub, key_config_stub): ('aa', 'cmd-aa')])) keyhint.update_keyhint('normal', 'a') assert keyhint.text() == expected_text(('a', '#ABCDEF', 'a', 'cmd-aa')) + +def test_no_matches(keyhint, key_config_stub): + """Ensure the widget isn't visible if there are no keystrings to show.""" + key_config_stub.set_bindings_for('normal', OrderedDict([ + ('aa', 'cmd-aa'), + ('ab', 'cmd-ab')])) + keyhint.update_keyhint('normal', 'z') + assert not keyhint.isVisible()