From a64cdfaf7885f716979dbfac069edbf7738f12e1 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Wed, 8 Dec 2021 18:28:46 +0000 Subject: [PATCH] [GtkUI] Enable remapping of keyboard shortcuts With key shortcuts set in the glade files it is not easy to change the shortcuts on macOS. Add AccelPaths to each MenuItem to allow adding or modifying accelerator in codes. Uses the recommended format `/MenuName` Update menubar code to use new functionality, taking advantage of accelerator_parse to specify accelerators as strings instead of Gdk flags. A future idea would be to use `Gtk.AccelMap.load` to have shortcuts loaded from user config. Co-authored-by: Gregorio Litenstein --- deluge/ui/gtk3/glade/main_window.ui | 40 ++++++++++----------- deluge/ui/gtk3/menubar.py | 16 +++++++++ deluge/ui/gtk3/menubar_osx.py | 54 ++++++++++------------------- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/deluge/ui/gtk3/glade/main_window.ui b/deluge/ui/gtk3/glade/main_window.ui index ecbb8f61c..e7267560c 100644 --- a/deluge/ui/gtk3/glade/main_window.ui +++ b/deluge/ui/gtk3/glade/main_window.ui @@ -1,5 +1,5 @@ - + @@ -7,7 +7,6 @@ False help-about-symbolic - True False @@ -67,6 +66,7 @@ zoom-fit-best-symbolic 1 + True False @@ -93,9 +93,9 @@ False Deluge - + - + @@ -114,8 +114,10 @@ _File True - + False + main_accelgroup + <Deluge-MainWindow>/File _Add Torrent @@ -126,7 +128,6 @@ add-image False - @@ -138,7 +139,6 @@ new-image False - @@ -155,7 +155,6 @@ False True - @@ -172,9 +171,8 @@ True quit_image False - accelgroup1 + main_accelgroup - @@ -188,9 +186,11 @@ _Edit True - + True False + main_accelgroup + <Deluge-MainWindow>/Edit _Preferences @@ -199,9 +199,8 @@ True prefs-image False - accelgroup1 + main_accelgroup - @@ -213,7 +212,6 @@ connection-image False - @@ -223,6 +221,7 @@ False + <MainWindow>/Torrent _Torrent True @@ -234,9 +233,11 @@ _View True - + True False + main_accelgroup + <Deluge-MainWindow>/View True @@ -297,7 +298,6 @@ _Find ... True - @@ -356,8 +356,10 @@ _Help True - + False + main_accelgroup + <Deluge-MainWindow>/Help _Homepage @@ -377,7 +379,6 @@ True False - @@ -405,7 +406,7 @@ True about-image False - accelgroup1 + main_accelgroup @@ -467,7 +468,6 @@ This will filter torrents for the current selection on the sidebar. Filter system-search-symbolic - False diff --git a/deluge/ui/gtk3/menubar.py b/deluge/ui/gtk3/menubar.py index a812a8cac..328cb7e4d 100644 --- a/deluge/ui/gtk3/menubar.py +++ b/deluge/ui/gtk3/menubar.py @@ -23,6 +23,17 @@ from .path_chooser import PathChooser log = logging.getLogger(__name__) +default_main_window_accelmap = { + '/File/Add Torrent': 'o', + '/File/Create Torrent': 'n', + '/File/Quit & Shutdown Daemon': 'q', + '/File/Quit': 'q', + '/Edit/Preferences': 'p', + '/Edit/Connection Manager': 'm', + '/View/Find ...': 'f', + '/Help/FAQ': 'F1', +} + class MenuBar(component.Component): def __init__(self): @@ -112,6 +123,11 @@ class MenuBar(component.Component): # Attach the torrent_menu to the Torrent file menu self.menu_torrent.set_submenu(self.torrentmenu) + # Set keyboard shortcuts + for accel_path, accelerator in default_main_window_accelmap.items(): + accel_key, accel_mods = Gtk.accelerator_parse(accelerator) + Gtk.AccelMap.change_entry(accel_path, accel_key, accel_mods, True) + # Make sure the view menuitems are showing the correct active state self.main_builder.get_object('menuitem_toolbar').set_active( self.config['show_toolbar'] diff --git a/deluge/ui/gtk3/menubar_osx.py b/deluge/ui/gtk3/menubar_osx.py index 53150fbf3..7f846dc72 100644 --- a/deluge/ui/gtk3/menubar_osx.py +++ b/deluge/ui/gtk3/menubar_osx.py @@ -5,48 +5,36 @@ # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. # - -from gi.repository.Gdk import ModifierType -from gi.repository.Gtk import SeparatorMenuItem, accel_groups_from_object -from gi.repository.Gtk.AccelFlags import VISIBLE +from gi.repository import Gtk from deluge.configmanager import ConfigManager - -def accel_swap(item, group, skey, smod, dkey, dmod): - # Accel map hack broken, see ticket #3078 - # item.remove_accelerator(group, ord(skey), smod) - item.add_accelerator('activate', group, ord(dkey), dmod, VISIBLE) - - -def accel_meta(item, group, key): - accel_swap(item, group, key, ModifierType.CONTROL_MASK, key, ModifierType.META_MASK) +macos_main_window_accelmap = { + '/File/Add Torrent': 'o', + '/File/Create Torrent': 'n', + '/File/Quit & Shutdown Daemon': 'q', + '/File/Quit': 'q', + '/Edit/Preferences': 'comma', + '/Edit/Connection Manager': 'm', + '/View/Find ...': 'f', + '/Help/FAQ': 'question', +} def menubar_osx(gtkui, osxapp): + # Change key shortcuts + for accel_path, accelerator in macos_main_window_accelmap.items(): + accel_key, accel_mods = Gtk.accelerator_parse(accelerator) + Gtk.AccelMap.change_entry(accel_path, accel_key, accel_mods, True) + main_builder = gtkui.mainwindow.get_builder() menubar = main_builder.get_object('menubar') - group = accel_groups_from_object(gtkui.mainwindow.window)[0] config = ConfigManager('gtk3ui.conf') - - # NOTE: accel maps doesn't work with glade file format - # because of libglade not setting MenuItem accel groups - # That's why we remove / set accelerators by hand... (dirty) - # Clean solution: migrate glades files to gtkbuilder format file_menu = main_builder.get_object('menu_file').get_submenu() file_items = file_menu.get_children() - accel_meta(file_items[0], group, 'o') - accel_meta(file_items[1], group, 'n') quit_all_item = file_items[3] - accel_swap( - quit_all_item, - group, - 'q', - ModifierType.SHIFT_MASK | ModifierType.CONTROL_MASK, - 'q', - ModifierType.SHIFT_MASK | ModifierType.META_MASK, - ) + for item in range(2, len(file_items)): # remove quits file_menu.remove(file_items[item]) @@ -54,13 +42,9 @@ def menubar_osx(gtkui, osxapp): edit_menu = menu_widget.get_submenu() edit_items = edit_menu.get_children() pref_item = edit_items[0] - accel_swap( - pref_item, group, 'p', ModifierType.CONTROL_MASK, ',', ModifierType.META_MASK - ) edit_menu.remove(pref_item) conn_item = edit_items[1] - accel_meta(conn_item, group, 'm') edit_menu.remove(conn_item) menubar.remove(menu_widget) @@ -75,10 +59,10 @@ def menubar_osx(gtkui, osxapp): osxapp.set_menu_bar(menubar) # populate app menu osxapp.insert_app_menu_item(about_item, 0) - osxapp.insert_app_menu_item(SeparatorMenuItem(), 1) + osxapp.insert_app_menu_item(Gtk.SeparatorMenuItem(), 1) osxapp.insert_app_menu_item(pref_item, 2) if not config['standalone']: osxapp.insert_app_menu_item(conn_item, 3) if quit_all_item.get_visible(): - osxapp.insert_app_menu_item(SeparatorMenuItem(), 4) + osxapp.insert_app_menu_item(Gtk.SeparatorMenuItem(), 4) osxapp.insert_app_menu_item(quit_all_item, 5)