From 693178c8ee0c22f5b0c168a1dcbca9db1e5dd49c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 1 Mar 2018 20:26:23 +0100 Subject: [PATCH] Refactor KeyInfo.__str__ This removes the special handling for macOS, but this is hopefully not needed anymore as we don't compare strings. --- qutebrowser/keyinput/keyutils.py | 46 ++++++---------------------- tests/unit/keyinput/test_keyutils.py | 8 +---- 2 files changed, 10 insertions(+), 44 deletions(-) diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index 5c1097aa8..c45dab290 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -203,54 +203,26 @@ class KeyInfo: A name of the key (combination) as a string or an empty string if only modifiers are pressed. """ - if utils.is_mac: - # Qt swaps Ctrl/Meta on macOS, so we switch it back here so the - # user can use it in the config as expected. See: - # https://github.com/qutebrowser/qutebrowser/issues/110 - # http://doc.qt.io/qt-5.4/osx-issues.html#special-keys - modmask2str = collections.OrderedDict([ - (Qt.MetaModifier, 'Ctrl'), - (Qt.AltModifier, 'Alt'), - (Qt.ControlModifier, 'Meta'), - (Qt.ShiftModifier, 'Shift'), - ]) - else: - modmask2str = collections.OrderedDict([ - (Qt.ControlModifier, 'Ctrl'), - (Qt.AltModifier, 'Alt'), - (Qt.MetaModifier, 'Meta'), - (Qt.ShiftModifier, 'Shift'), - ]) - if is_modifier_key(self.key): return '' - parts = [] - - for (mask, s) in modmask2str.items(): - if self.modifiers & mask and s not in parts: - parts.append(s) - key_string = _key_to_string(self.key) if is_printable(self.key): - # FIXME Add a test to make sure Tab doesn;t become TAB + # "normal" binding + # FIXME Add a test to make sure Tab doesn't become TAB assert len(key_string) == 1 or self.key == Qt.Key_Space, key_string if self.modifiers == Qt.ShiftModifier: - parts = [] - key_string = key_string.upper() + return key_string.upper() + elif self.modifiers == Qt.NoModifier: + return key_string.lower() else: + # Use special binding syntax, but instead of key_string = key_string.lower() - parts.append(key_string) - part_string = '+'.join(parts) - - if len(part_string) > 1: - # "special" binding - return '<{}>'.format(part_string) - else: - # "normal" binding - return part_string + # "special" binding + modifier_string = QKeySequence(self.modifiers).toString() + return '<{}{}>'.format(modifier_string, key_string) def text(self): """Get the text which would be displayed when pressing this key.""" diff --git a/tests/unit/keyinput/test_keyutils.py b/tests/unit/keyinput/test_keyutils.py index c1ddbcb5a..7301b7747 100644 --- a/tests/unit/keyinput/test_keyutils.py +++ b/tests/unit/keyinput/test_keyutils.py @@ -138,13 +138,7 @@ class TestKeyEventToString: key=Qt.Key_A, modifiers=(Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier | Qt.ShiftModifier)) s = str(keyutils.KeyInfo.from_event(evt)) - assert s == '' - - @pytest.mark.fake_os('mac') - def test_mac(self, fake_keyevent_factory): - """Test with a simulated mac.""" - evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier) - assert str(keyutils.KeyInfo.from_event(evt)) == '' + assert s == '' @pytest.mark.parametrize('keystr, expected', [