Skip to content

Commit

Permalink
Also parse x hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentijnvdBeek committed Mar 9, 2018
1 parent dfe5198 commit e233ce7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -104,3 +104,7 @@ ENV/

# mypy
.mypy_cache/

# Editor
#*
*~
1 change: 1 addition & 0 deletions .pylintrc
Expand Up @@ -4,6 +4,7 @@ max-args = 8

[messages control]
disable =
duplicate-code,
too-few-public-methods,
too-many-public-methods,
too-many-instance-attributes,
Expand Down
4 changes: 2 additions & 2 deletions clay/app.py
Expand Up @@ -281,11 +281,11 @@ def toggle_repeat_one():
"""
player.set_repeat_one(not player.get_is_repeat_one())

@staticmethod
def quit():
def quit(self):
"""
Quit app.
"""
self.loop = None
sys.exit(0)

def handle_escape(self):
Expand Down
77 changes: 49 additions & 28 deletions clay/hotkeys.py
Expand Up @@ -45,7 +45,7 @@ class _HotkeyManager(object):
Runs Gtk main loop in a thread.
"""
def __init__(self):
self.hotkeys = {}
self._x_hotkeys = {}
self._hotkeys = self._parse_hotkeys()
self.config = None

Expand All @@ -56,7 +56,6 @@ def __init__(self):
if IS_INIT:
Keybinder.init()
self.initialize()

threading.Thread(target=Gtk.main).start()
else:
logger.debug("Not loading the global shortcuts.")
Expand All @@ -66,45 +65,65 @@ def __init__(self):
"You can check the log for more details."
)

def _parse_hotkeys(self):
@staticmethod
def _to_gtk_modifier(key):
"""
Read out the configuration file and parse them into a dictionary readable for urwid.
Translates the modifies to the way that GTK likes them.
"""
hotkey_config = settings.get_default_config_section('hotkeys', 'clay_hotkeys')
key = key.strip()

if key == "meta":
key = "<alt>"
elif key in ("ctrl", "alt", "shift"):
key = "<" + key + ">"
else:
key = key

return key

def _parse_x_hotkeys(self):
"""
Reads out them configuration file and parses them into hotkeys readable by GTK.
"""
hotkey_default_config = settings.get_default_config_section('hotkeys', 'x_hotkeys')
mod_key = settings.get('mod_key', 'hotkeys')
hotkeys = {}

for hotkey_name, hotkey_dict in hotkey_config.items():
hotkeys[hotkey_name] = {}
for action in hotkey_dict.keys():
key_seq = settings.get(action, 'hotkeys', 'clay_hotkeys', hotkey_name)\
.replace(' ', '')
for action in hotkey_default_config:
key_seq = settings.get(action, 'hotkeys', 'x_hotkeys')

for key in key_seq.split(','):
hotkey = key.split('+') if key != '+' else key
for key in key_seq.split(', '):
hotkey = key.split(' + ')

if hotkey[0] == 'mod':
hotkey[0] = mod_key
if hotkey[0].strip() == 'mod':
hotkey[0] = mod_key

hotkeys[hotkey_name][' '.join(hotkey)] = action
hotkey = [self._to_gtk_modifier(key) for key in hotkey]

hotkeys[action] = ''.join(hotkey)

return hotkeys

@staticmethod
def load_keys():
def _parse_hotkeys(self):
"""
Load hotkey config from settings.
Reads out the configuration file and parse them into a hotkeys for urwid.
"""
hotkeys = settings.get_section('hotkeys', 'x_hotkeys')
default_hotkeys = settings.get_default_config_section('hotkeys', 'x_hotkeys')
hotkey_config = settings.get_default_config_section('hotkeys', 'clay_hotkeys')
mod_key = settings.get('mod_key', 'hotkeys')
hotkeys = {}

for hotkey_name, hotkey_dict in hotkey_config.items():
hotkeys[hotkey_name] = {}
for action in hotkey_dict.keys():
key_seq = settings.get(action, 'hotkeys', 'clay_hotkeys', hotkey_name)

if hotkeys is default_hotkeys:
return default_hotkeys
for key in key_seq.split(', '):
hotkey = key.split(' + ')

for operation, default_key in default_hotkeys.items():
if operation not in hotkeys:
hotkeys[operation] = default_key
if hotkey[0].strip() == 'mod':
hotkey[0] = mod_key

hotkeys[hotkey_name][' '.join(hotkey)] = action
return hotkeys

def keypress(self, name, caller, super_, size, key):
Expand All @@ -127,10 +146,12 @@ def initialize(self):
"""
Unbind previous hotkeys, re-read config & bind new hotkeys.
"""
for operation, key in self.hotkeys.items():
for operation, key in self._x_hotkeys.items():
Keybinder.unbind(key)
self.hotkeys = self.load_keys()
for operation, key in self.hotkeys.items():

self._x_hotkeys = self._parse_x_hotkeys()

for operation, key in self._x_hotkeys.items():
Keybinder.bind(key, self.fire_hook, operation)

def fire_hook(self, key, operation):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -23,6 +23,6 @@
]
},
package_data={
'clay': ['config.yaml'],
'clay': ['config.yaml', 'colours.yaml'],
},
)

0 comments on commit e233ce7

Please sign in to comment.