Skip to content

Commit

Permalink
hotkey assembly repaired for ahk2 #266
Browse files Browse the repository at this point in the history
  • Loading branch information
ewerybody committed Apr 24, 2024
1 parent b4e3971 commit 2dfba40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
22 changes: 10 additions & 12 deletions ui/a2runtime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Functionality all around managing the Autohotkey runtime of a2.
"""

import os
import enum
import subprocess
Expand Down Expand Up @@ -273,32 +274,29 @@ def gather(self, mod):
for key in iter_str_or_list(hotkeys):
if not key:
continue
self._scope_types[scope_type][scope_string].setdefault(key, []).append(
(command, mod)
)
self._scope_types[scope_type][scope_string].setdefault(key, []).append((command, mod))

def get_content(self):
scope_modes = {Scope.incl: '#IfWinActive', Scope.excl: '#IfWinNotActive'}
scope_prefix = '#HotIf'
scope_modes = {
Scope.incl: f'\n{scope_prefix} WinActive',
Scope.excl: f'\n{scope_prefix} !WinActive'
}

# write global hotkey text
content = scope_modes[Scope.incl] + ',\n'
content = f'{scope_prefix}\n'
content += self._create_hotkey_code(self.hotkeys_global)
# write the scoped stuff
for mode, scope_dict in self._scope_types.items():
for scope, hotkey_data in scope_dict.items():
content += '\n%s, %s\n' % (scope_modes[mode], scope)
content += f'{scope_modes[mode]}("{scope}")\n'
content += self._create_hotkey_code(hotkey_data)

return content

@property
def has_content(self):
return any(
[
d != {}
for d in [self.hotkeys_global, self.hotkeys_scope_incl, self.hotkeys_scope_excl]
]
)
return any([d != {} for d in [self.hotkeys_global, self.hotkeys_scope_incl, self.hotkeys_scope_excl]])

@staticmethod
def _create_hotkey_code(hotkey_data):
Expand Down
4 changes: 2 additions & 2 deletions ui/a2widget/a2hotkey/edit_func_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ def on_send_mode_change(self):
def on_input(self, code):
current = self.ui.cfg_functionMode.currentText()
if current == FuncTypes.open:
code = 'Run, ' + code
code = f'Run "{code}"'

elif current == FuncTypes.send:
# If `code` is empty we SHOULD strip the whole thing but we also
# detect the Send mode from it :/ This has to happen somewhere else!
send_mode = self.ui.function_send_mode.currentText()
code = '%s, %s' % (send_mode, code)
code = f'{send_mode} "{code}"'

self._config_dict[FUNCTIONS[current]] = code
self.changed.emit()
Expand Down
11 changes: 6 additions & 5 deletions ui/a2widget/a2hotkey/hotkey_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
Common hotkey things.
"""

import unittest


SEND_MODES = ('Send', 'SendRaw', 'SendInput', 'SendPlay', 'SendEvent')
MOD_KEYS = ('! - Alt', '^ - Control', '+ - Shift', '# - Win')
DISPLAY_MODIFIERS = {
Expand Down Expand Up @@ -153,12 +150,16 @@ def strip_mode(code, modes):
Return stripped code and found mode in tuple."""
_code = code.lower()
for mode in modes:
if _code.startswith(f'{mode.lower()},'):
return code[len(mode) :].lstrip(' ,'), mode
_mode = mode.lower()
if _code.startswith(f'{_mode} '):
return code[len(mode) :].strip('" '), mode
if _code.startswith(f'{_mode},'):
return code[len(mode) + 1:].strip('" '), mode
return code, modes[0]


if __name__ == '__main__':
import unittest
from a2widget.a2hotkey.test import test_common

unittest.main(test_common, verbosity=2)

0 comments on commit 2dfba40

Please sign in to comment.