From 2ff66e7fe6cfa600cacb9d8946f1057f7d22664b Mon Sep 17 00:00:00 2001 From: Vishweshwar Saran Singh Deo Date: Thu, 22 Feb 2024 18:09:18 +0530 Subject: [PATCH] [bug 889] - simplify and clean keybinding merge code #889 - Current list of Keybindings in Preferences->Keybindings are shown after merging - The merge happens in prefseditor.py - Cleaning and moving the code from prefseditor.py - Adding a function to get plugin via name - Adding some missing keybindings in config.py and prefseditor.py and syncing them for consistency - These changes were also part of: Feature: is there any way to Hide or Sort context menu items? #773 and Pull request: [Plugin ContextMenuPlugin] 773-Feature-is-there-any-way-to-Hide-or-Sort-context-menu #842 - So decoupling these as separate issue --- terminatorlib/config.py | 4 ++++ terminatorlib/plugin.py | 45 ++++++++++++++++++++++++++++++++---- terminatorlib/prefseditor.py | 34 +++++++++++++-------------- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 008d1cfa..d07def2f 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -123,6 +123,10 @@ 'new_tab_after_current_tab': False, }, 'keybindings': { + 'zoom' : '', + 'unzoom' : '', + 'maximise' : '', + 'open_debug_tab' : '', 'zoom_in' : 'plus', 'zoom_out' : 'minus', 'zoom_normal' : '0', diff --git a/terminatorlib/plugin.py b/terminatorlib/plugin.py index 49a972e7..144dea93 100644 --- a/terminatorlib/plugin.py +++ b/terminatorlib/plugin.py @@ -118,6 +118,11 @@ def load_plugins(self, force=False): self.done = True + def get_plugin_instance(self, plugin): + instance = self.instances.get(plugin, None) + dbg('get plugin: %s instance: %s' % (plugin, instance)) + return instance + def get_plugins_by_capability(self, capability): """Return a list of plugins with a particular capability""" result = [] @@ -221,8 +226,14 @@ class KeyBindUtil: map_act_to_keys = {} map_act_to_desc = {} + #merged keybindings and plugin key bindings + map_all_act_to_keys = {} + map_all_act_to_desc = {} + + config = Config() + def __init__(self, config=None): - self.config = config + self.load_merge_key_maps() #Example # bind @@ -234,6 +245,17 @@ def __init__(self, config=None): # if act == "url_find_next": + def load_merge_key_maps(self): + + cfg_keybindings = KeyBindUtil.config['keybindings'] + + #TODO need to check if cyclic dep here, we only using keybindingnames + from terminatorlib.prefseditor import PrefsEditor + pref_keybindingnames = PrefsEditor.keybindingnames + + #merge give preference to main bindings over plugin + KeyBindUtil.map_all_act_to_keys = {**self.map_act_to_keys, **cfg_keybindings} + KeyBindUtil.map_all_act_to_desc = {**self.map_act_to_desc, **pref_keybindingnames} #check map key_val_mask -> action def _check_keybind_change(self, key): @@ -313,18 +335,33 @@ def keyaction(self, event): dbg("keyaction: (%s)" % str(ret)) return self.map_key_to_act.get(ret, None) + #functions to get actstr to keys / key mappings or desc / desc mapppings + #for plugins or merged keybindings + def get_act_to_keys(self, key): + return self.map_all_act_to_keys.get(key) + + def get_plugin_act_to_keys(self, key): return self.map_act_to_keys.get(key) - def get_all_act_to_keys(self): + def get_all_plugin_act_to_keys(self): return self.map_act_to_keys - def get_all_act_to_desc(self): - return self.map_act_to_desc + def get_all_act_to_keys(self): + return self.map_all_act_to_keys def get_act_to_desc(self, act): + return self.map_all_act_to_desc.get(act) + + def get_plugin_act_to_desc(self, act): return self.map_act_to_desc.get(act) + def get_all_plugin_act_to_desc(self): + return self.map_act_to_desc + + def get_all_act_to_desc(self): + return self.map_all_act_to_desc + #get action to key binding from config def get_act_to_keys_config(self, act): if not self.config: diff --git a/terminatorlib/prefseditor.py b/terminatorlib/prefseditor.py index db22f6a0..a73d6d0c 100755 --- a/terminatorlib/prefseditor.py +++ b/terminatorlib/prefseditor.py @@ -45,6 +45,7 @@ class PrefsEditor: term = None builder = None layouteditor = None + previous_plugin_selection = None previous_layout_selection = None previous_profile_selection = None colorschemevalues = {'black_on_yellow': 0, @@ -103,7 +104,12 @@ class PrefsEditor: 'gruvbox_dark': '#282828:#cc241d:#98971a:#d79921:\ #458588:#b16286:#689d6a:#a89984:#928374:#fb4934:#b8bb26:#fabd2f:\ #83a598:#d3869b:#8ec07c:#ebdbb2'} - keybindingnames = { 'zoom_in' : _('Increase font size'), + keybindingnames = { + 'zoom' : _('Zoom terminal'), + 'unzoom' : _('Restore all terminals'), + 'maximise' : _('Maximize terminal'), + 'open_debug_tab' : _('Open Debug Tab'), + 'zoom_in' : _('Increase font size'), 'zoom_out' : _('Decrease font size'), 'zoom_normal' : _('Restore original font size'), 'zoom_in_all' : _('Increase font size on all terminals'), @@ -496,20 +502,15 @@ def on_search_refilter(widget): liststore = widget.get_model() liststore.set_sort_column_id(0, Gtk.SortType.ASCENDING) - keybindings = self.config['keybindings'] - keybindutil = KeyBindUtil() - plugin_keyb_act = keybindutil.get_all_act_to_keys() - plugin_keyb_desc = keybindutil.get_all_act_to_desc() - #merge give preference to main bindings over plugin - keybindings = {**plugin_keyb_act, **keybindings} - self.keybindingnames = {**plugin_keyb_desc, **self.keybindingnames} - #dbg("appended actions %s names %s" % (keybindings, self.keybindingnames)) + keybindutil = KeyBindUtil() + act_to_key_map = keybindutil.get_all_act_to_keys() + self.keybindingnames = keybindutil.get_all_act_to_desc() - for keybinding in keybindings: + for keybinding in act_to_key_map: keyval = 0 mask = 0 - value = keybindings[keybinding] + value = act_to_key_map[keybinding] if value is not None and value != '': try: (keyval, mask) = self.keybindings._parsebinding(value) @@ -519,7 +520,7 @@ def on_search_refilter(widget): keyval, mask]) self.treemodelfilter = liststore.filter_new() - self.treemodelfilter.set_visible_func(filter_visible, keybindings) + self.treemodelfilter.set_visible_func(filter_visible, act_to_key_map) widget.set_model(self.treemodelfilter) ## Plugins tab @@ -1955,14 +1956,11 @@ def on_cellrenderer_accel_edited(self, liststore, path, key, mods, _code): current_binding = liststore.get_value(liststore.get_iter(path), 0) parsed_accel = Gtk.accelerator_parse(accel) - keybindutil = KeyBindUtil() - keybindings = self.config["keybindings"] - #merge give preference to main bindings over plugin - plugin_keyb_act = keybindutil.get_all_act_to_keys() - keybindings = {**plugin_keyb_act, **keybindings} + keybindutil = KeyBindUtil() + act_to_key_map = keybindutil.get_all_act_to_keys() duplicate_bindings = [] - for conf_binding, conf_accel in keybindings.items(): + for conf_binding, conf_accel in act_to_key_map.items(): if conf_accel is None: continue