Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug 889] - simplify and clean keybinding merge code #889 #890

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions terminatorlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
'new_tab_after_current_tab': False,
},
'keybindings': {
'zoom' : '',
'unzoom' : '',
'maximise' : '',
'open_debug_tab' : '',
'zoom_in' : '<Control>plus',
'zoom_out' : '<Control>minus',
'zoom_normal' : '<Control>0',
Expand Down
45 changes: 41 additions & 4 deletions terminatorlib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 16 additions & 18 deletions terminatorlib/prefseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading