From 2dcfc5ae972810b68b8a780626e0270ff990deb2 Mon Sep 17 00:00:00 2001 From: Riccardo Ravaioli Date: Fri, 27 Nov 2026 12:55:48 +0100 Subject: [PATCH 1/2] Show keyboard shortcuts for zoom, maximize and restore in the context menu The terminal context menu shows the configured keybinding next to most of its entries (Copy, Paste, Split, Open Tab, Close...), because they are built through TerminalPopupMenu.menu_item(). The "Zoom terminal", "Maximize terminal" and "Restore all terminals" entries were still built with a plain Gtk.MenuItem, so users had no way to discover from the menu that Ctrl+Shift+Z and Ctrl+Shift+X do the same thing as these entries. Build those three entries through menu_item() as well, so they display the scaled_zoom and toggle_zoom shortcuts. Both bindings restore the layout when a terminal is zoomed, so "Restore all terminals" shows toggle_zoom, or scaled_zoom when toggle_zoom is unbound. Add tests covering menu_item() for the two zoom actions. Signed-off-by: Riccardo Ravaioli --- terminatorlib/terminal_popup_menu.py | 13 ++++- tests/test_terminal_popup_menu.py | 87 +++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/terminatorlib/terminal_popup_menu.py b/terminatorlib/terminal_popup_menu.py index 25686cf5..962aaff0 100644 --- a/terminatorlib/terminal_popup_menu.py +++ b/terminatorlib/terminal_popup_menu.py @@ -278,19 +278,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.config.base.get_item('keybindings').get('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..312d0a9b 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,86 @@ 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) == [] From 0e2bd3cc389a5149ebe73a9a112280394e88b068 Mon Sep 17 00:00:00 2001 From: Riccardo Ravaioli Date: Fri, 27 Nov 2026 12:55:49 +0100 Subject: [PATCH 2/2] Parse context menu accelerators with Gtk.accelerator_parse TerminalPopupMenu.menu_item() built the accelerator shown next to a menu entry by hand: it recognised only , / and , and took the key from the last character of the binding, falling back to the entry's mnemonic letter. A binding such as "F5" was shown as Ctrl+Z on "Zoom terminal" while the key handler honoured F5, and a binding on was not shown at all. Let Gtk.accelerator_parse() do the parsing: it handles the modifiers and key names used in the keybindings section, and yields no key for a binding it cannot parse, in which case the entry is shown without a shortcut instead of with a wrong one. While at it, read the keybindings table with get_item('keybindings') instead of passing the action name as the profile argument, and treat "", None and "None" alike as unbound. Signed-off-by: Riccardo Ravaioli --- terminatorlib/terminal_popup_menu.py | 77 +++++++++------------------- tests/test_terminal_popup_menu.py | 23 +++++++++ 2 files changed, 47 insertions(+), 53 deletions(-) diff --git a/terminatorlib/terminal_popup_menu.py b/terminatorlib/terminal_popup_menu.py index 962aaff0..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): @@ -294,7 +265,7 @@ def show(self, widget, event=None): else: # Both zoom bindings restore the layout; show the one that is set restore_action = 'toggle_zoom' - if not self.config.base.get_item('keybindings').get('toggle_zoom'): + if not self.keybinding('toggle_zoom'): restore_action = 'scaled_zoom' item = self.menu_item(Gtk.MenuItem, restore_action, _('_Restore all terminals')) diff --git a/tests/test_terminal_popup_menu.py b/tests/test_terminal_popup_menu.py index 312d0a9b..70542b72 100644 --- a/tests/test_terminal_popup_menu.py +++ b/tests/test_terminal_popup_menu.py @@ -181,3 +181,26 @@ def test_menu_item_without_keybinding_has_no_accelerator( 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) == []