diff --git a/terminatorlib/terminal_popup_menu.py b/terminatorlib/terminal_popup_menu.py index 25686cf5..e470e50c 100644 --- a/terminatorlib/terminal_popup_menu.py +++ b/terminatorlib/terminal_popup_menu.py @@ -3,7 +3,7 @@ """terminal_popup_menu.py - classes necessary to provide a terminal context menu""" -from gi.repository import Gtk, Gdk +from gi.repository import Gtk from .version import APP_NAME from .translation import _ @@ -27,61 +27,32 @@ def __init__(self, terminal): self.config = Config() self.accelgrp = Gtk.AccelGroup() - def get_menu_item_mask(self, maskstr): - mask = 0 - if maskstr is None: - return mask - maskstr = maskstr.lower() - if maskstr.find(''.lower()) >= 0: - mask = mask | Gdk.ModifierType.SHIFT_MASK - dbg("adding mask %s" % mask) - - ctrl = (maskstr.find(''.lower()) >= 0 or - maskstr.find(''.lower()) >= 0) - if ctrl: - mask = mask | Gdk.ModifierType.CONTROL_MASK - dbg("adding mask %s" % mask) - - if maskstr.find(''.lower()) >= 0: - mask = mask | Gdk.ModifierType.MOD1_MASK - dbg("adding mask %s" % mask) - - mask = Gdk.ModifierType(mask) - dbg("menu_item_mask :%d" % mask) - return mask + def keybinding(self, actstr): + """The configured keybinding of an action, or None if it is unbound""" + binding = self.config.base.get_item('keybindings').get(actstr) + if binding and binding != "None": + return binding + return None def menu_item(self, menutype, actstr, menustr): - act = self.config.base.get_item('keybindings', actstr) - maskstr = act[actstr] if actstr in act else "" - mask = self.get_menu_item_mask(maskstr) - - accelchar = "" - pos = menustr.lower().find("_") - if (pos >= 0 and pos+1 < len(menustr)): - accelchar = menustr.lower()[pos+1] - - #this may require tweak. what about shortcut function keys ? - if maskstr: - mpos = maskstr.rfind(">") - #can't have a char at 0 position as <> is len 2 - if mpos >= 0 and mpos+1 < len(maskstr): - configaccelchar = maskstr[mpos+1:] - #ensure to take only 1 char else ignore - if len(configaccelchar) == 1: - dbg("found accelchar in config:%s override:%s" - % (configaccelchar, accelchar)) - accelchar = configaccelchar - - dbg("action from config:%s for item:%s with shortcut accelchar:(%s)" - % (maskstr, menustr, accelchar)) + """Menu item for a keybinding action, showing its accelerator""" item = menutype.new_with_mnemonic(menustr) item.set_name(actstr) - if mask: - item.add_accelerator("activate", - self.accelgrp, - Gdk.keyval_from_name(accelchar), - mask, - Gtk.AccelFlags.VISIBLE) + + binding = self.keybinding(actstr) + if binding: + keyval, mask = Gtk.accelerator_parse(binding) + dbg("binding from config:%s for item:%s parsed as (%s, %s)" + % (binding, menustr, keyval, mask)) + if keyval: + item.add_accelerator("activate", + self.accelgrp, + keyval, + mask, + Gtk.AccelFlags.VISIBLE) + else: + dbg("could not parse binding:%s for item:%s" + % (binding, menustr)) return item def close_terminal(self, _menuitem=None): @@ -278,19 +249,26 @@ def show(self, widget, event=None): if not terminal.is_zoomed(): sensitive = not terminal.get_toplevel() == terminal.get_parent() - item = Gtk.MenuItem.new_with_mnemonic(_('_Zoom terminal')) + item = self.menu_item(Gtk.MenuItem, 'scaled_zoom', + _('_Zoom terminal')) item.connect('activate', terminal.zoom) item.set_sensitive(sensitive) menu.append(item) - item = Gtk.MenuItem.new_with_mnemonic(_('Ma_ximize terminal')) + item = self.menu_item(Gtk.MenuItem, 'toggle_zoom', + _('Ma_ximize terminal')) item.connect('activate', terminal.maximise) item.set_sensitive(sensitive) menu.append(item) menu.append(Gtk.SeparatorMenuItem()) else: - item = Gtk.MenuItem.new_with_mnemonic(_('_Restore all terminals')) + # Both zoom bindings restore the layout; show the one that is set + restore_action = 'toggle_zoom' + if not self.keybinding('toggle_zoom'): + restore_action = 'scaled_zoom' + item = self.menu_item(Gtk.MenuItem, restore_action, + _('_Restore all terminals')) item.connect('activate', terminal.unzoom) menu.append(item) diff --git a/tests/test_terminal_popup_menu.py b/tests/test_terminal_popup_menu.py index 24d178a9..70542b72 100644 --- a/tests/test_terminal_popup_menu.py +++ b/tests/test_terminal_popup_menu.py @@ -4,11 +4,13 @@ import sys import gi +import pytest sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))) +gi.require_version("Gdk", "3.0") gi.require_version("Gtk", "3.0") -from gi.repository import Gtk +from gi.repository import Gdk, Gtk from terminatorlib.terminal_popup_menu import TerminalPopupMenu @@ -96,3 +98,109 @@ def test_close_terminal_finds_confirmation_container_above_parent_widget(): assert terminal.closed assert container.confirmations == [(terminal.get_toplevel(), terminal)] + + +@pytest.fixture +def popup_with_keybindings(): + """Factory of popups whose config holds only the given keybindings.""" + popups = [] + + def make(keybindings): + class FakeBase: + def get_item(self, key, profile='default'): + assert key == 'keybindings' + return keybindings + + class FakeConfig: + base = FakeBase() + + popup = TerminalPopupMenu.__new__(TerminalPopupMenu) + popup.config = FakeConfig() + popup.accelgrp = Gtk.AccelGroup() + popup.items = [] + popups.append(popup) + return popup + + yield make + + # Destroy the items while their accel group is still alive: a menu + # item's label disconnects from the group when the item goes away. + for popup in popups: + for item in popup.items: + item.destroy() + + +def menu_item(popup, actstr, menustr): + """Build a menu item on the popup and keep it alive until teardown.""" + item = popup.menu_item(Gtk.MenuItem, actstr, menustr) + popup.items.append(item) + return item + + +def accelerators(popup): + """Sorted (keyval, modifiers) of the accelerators shown on the popup.""" + found = [] + + def collect(key, closure): + assert key.accel_flags & Gtk.AccelFlags.VISIBLE + found.append((key.accel_key, key.accel_mods)) + return False + + popup.accelgrp.find(collect) + return sorted(found) + + +def test_zoom_menu_items_show_their_keybindings(popup_with_keybindings): + """Zoom, maximize and restore items carry the configured shortcuts.""" + popup = popup_with_keybindings({'scaled_zoom': 'z', + 'toggle_zoom': 'x'}) + mods = Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.CONTROL_MASK + + zoom = menu_item(popup, 'scaled_zoom', '_Zoom terminal') + maximize = menu_item(popup, 'toggle_zoom', 'Ma_ximize terminal') + restore = menu_item(popup, 'toggle_zoom', '_Restore all terminals') + + assert zoom.get_name() == 'scaled_zoom' + assert maximize.get_name() == 'toggle_zoom' + assert restore.get_name() == 'toggle_zoom' + assert accelerators(popup) == sorted([ + (Gdk.keyval_from_name('z'), mods), + (Gdk.keyval_from_name('x'), mods), + (Gdk.keyval_from_name('x'), mods)]) + + +@pytest.mark.parametrize('keybindings', [{}, {'scaled_zoom': ''}, + {'scaled_zoom': None}, + {'scaled_zoom': 'None'}]) +def test_menu_item_without_keybinding_has_no_accelerator( + popup_with_keybindings, keybindings): + """An unbound action still produces a plain menu item.""" + popup = popup_with_keybindings(keybindings) + + item = menu_item(popup, 'scaled_zoom', '_Zoom terminal') + + assert item.get_name() == 'scaled_zoom' + assert accelerators(popup) == [] + + +def test_menu_item_shows_named_key_bindings(popup_with_keybindings): + """Bindings on named keys are shown as configured, not as the mnemonic.""" + popup = popup_with_keybindings({'scaled_zoom': 'F5', + 'toggle_zoom': 'x'}) + + menu_item(popup, 'scaled_zoom', '_Zoom terminal') + menu_item(popup, 'toggle_zoom', 'Ma_ximize terminal') + + assert accelerators(popup) == sorted([ + (Gdk.KEY_F5, Gdk.ModifierType.CONTROL_MASK), + (Gdk.keyval_from_name('x'), Gdk.ModifierType.SUPER_MASK)]) + + +def test_menu_item_ignores_unparsable_binding(popup_with_keybindings): + """A binding GTK cannot parse yields a plain menu item, no crash.""" + popup = popup_with_keybindings({'scaled_zoom': 'NotAKey'}) + + item = menu_item(popup, 'scaled_zoom', '_Zoom terminal') + + assert item.get_name() == 'scaled_zoom' + assert accelerators(popup) == []