Did updated event system
This commit is contained in:
parent
2e4d1cd803
commit
0b54243be6
|
@ -5,7 +5,8 @@ import builtins, threading
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from utils.pyautogui_control import ControlMixin
|
from utils.pyautogui_control import ControlMixin
|
||||||
|
from utils.endpoint_registry import EndpointRegistry
|
||||||
|
from utils.event_system import EventSystem
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,21 +25,6 @@ def daemon_threaded_wrapper(fn):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class EndpointRegistry():
|
|
||||||
def __init__(self):
|
|
||||||
self._endpoints = {}
|
|
||||||
|
|
||||||
def register(self, rule, **options):
|
|
||||||
def decorator(f):
|
|
||||||
self._endpoints[rule] = f
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
def get_endpoints(self):
|
|
||||||
return self._endpoints
|
|
||||||
|
|
||||||
|
|
||||||
class Pyautogui_Controller(ControlMixin):
|
class Pyautogui_Controller(ControlMixin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.isCtrlOn = False
|
self.isCtrlOn = False
|
||||||
|
@ -57,17 +43,17 @@ keys_json = {
|
||||||
},
|
},
|
||||||
"row2": {
|
"row2": {
|
||||||
"pKeys": ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
|
"pKeys": ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
|
||||||
"sKeys": ['\\', '^', '#', '$', '%', '&', '-', '_', '<', '>'],
|
"sKeys": ['\\', '^', '#', '$', '%', '&', '-', '_', '"', '*'],
|
||||||
"eKeys": ['', '', '', '', '', '', '', '', '', '']
|
"eKeys": ['', '', '', '', '', '', '', '', '', '']
|
||||||
},
|
},
|
||||||
"row3": {
|
"row3": {
|
||||||
"pKeys": ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', "'"],
|
"pKeys": ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', "'"],
|
||||||
"sKeys": ['\\', '/', '|', ':', '=', '+', '"', '*', ';', '!'],
|
"sKeys": ['/', '|', ':', '=', '+', '', '', '', ';', '!'],
|
||||||
"eKeys": ['', '', '', '', '', '', '', '', '', '']
|
"eKeys": ['', '', '', '', '', '', '', '', '', '']
|
||||||
},
|
},
|
||||||
"row4": {
|
"row4": {
|
||||||
"pKeys": ['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '?'],
|
"pKeys": ['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '?'],
|
||||||
"sKeys": ['`', '', '', '', '[', ']', '(', ')', '{', '}'],
|
"sKeys": ['', '', '<', '>', '[', ']', '(', ')', '{', '}'],
|
||||||
"eKeys": ['', '', '', '', '', '', '', '', '', '']
|
"eKeys": ['', '', '', '', '', '', '', '', '', '']
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -79,11 +65,12 @@ keys_json = {
|
||||||
# NOTE: Just reminding myself we can add to builtins two different ways...
|
# NOTE: Just reminding myself we can add to builtins two different ways...
|
||||||
# __builtins__.update({"event_system": Builtins()})
|
# __builtins__.update({"event_system": Builtins()})
|
||||||
builtins.app_name = "Mouse Keyboard"
|
builtins.app_name = "Mouse Keyboard"
|
||||||
builtins.endpoint_registry = EndpointRegistry()
|
|
||||||
builtins.typwriter = Pyautogui_Controller()
|
|
||||||
builtins.threaded = threaded_wrapper
|
builtins.threaded = threaded_wrapper
|
||||||
builtins.daemon_threaded = daemon_threaded_wrapper
|
builtins.daemon_threaded = daemon_threaded_wrapper
|
||||||
builtins.keys_set = keys_json
|
builtins.keys_set = keys_json
|
||||||
builtins.trace_debug = False
|
builtins.trace_debug = False
|
||||||
builtins.debug = False
|
builtins.debug = False
|
||||||
builtins.app_settings = None
|
builtins.app_settings = None
|
||||||
|
builtins.endpoint_registry = EndpointRegistry()
|
||||||
|
builtins.event_system = EventSystem()
|
||||||
|
builtins.typwriter = Pyautogui_Controller()
|
||||||
|
|
|
@ -15,14 +15,14 @@ from .key import Key
|
||||||
|
|
||||||
class Esc_Key(Key):
|
class Esc_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Esc_Key, self).__init__("Esc", "Esc")
|
super(Esc_Key, self).__init__("Esc", "Esc", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Symbols_Key(Key):
|
class Symbols_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Symbols_Key, self).__init__("Symbols", "Symbols")
|
super(Symbols_Key, self).__init__("Symbols", "Symbols", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._clicked)
|
self.connect("released", self._clicked)
|
||||||
|
@ -30,15 +30,11 @@ class Symbols_Key(Key):
|
||||||
def _clicked(self, widget = None):
|
def _clicked(self, widget = None):
|
||||||
ctx = widget.get_style_context()
|
ctx = widget.get_style_context()
|
||||||
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
|
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
|
||||||
|
event_system.emit("toggle_symbol_keys")
|
||||||
key_columns = self.get_parent().get_parent().get_children()[1]
|
|
||||||
for row in key_columns.get_children():
|
|
||||||
for key in row:
|
|
||||||
key.emit("toggle-symbol-keys", ())
|
|
||||||
|
|
||||||
class CAPS_Key(Key):
|
class CAPS_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(CAPS_Key, self).__init__("Caps", "Caps")
|
super(CAPS_Key, self).__init__("Caps", "Caps", iscontrol=True)
|
||||||
|
|
||||||
self.setup_styling()
|
self.setup_styling()
|
||||||
self.show_all()
|
self.show_all()
|
||||||
|
@ -52,18 +48,14 @@ class CAPS_Key(Key):
|
||||||
def _clicked(self, widget = None):
|
def _clicked(self, widget = None):
|
||||||
ctx = widget.get_style_context()
|
ctx = widget.get_style_context()
|
||||||
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
|
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
|
||||||
|
event_system.emit("toggle_caps")
|
||||||
key_columns = self.get_parent().get_parent().get_children()[1]
|
|
||||||
for row in key_columns.get_children():
|
|
||||||
for key in row:
|
|
||||||
key.emit("toggle-caps", ())
|
|
||||||
|
|
||||||
|
|
||||||
############################## Right_Column Keys ##############################
|
############################## Right_Column Keys ##############################
|
||||||
|
|
||||||
class Backspace_Key(Key):
|
class Backspace_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Backspace_Key, self).__init__("Backspace", "Backspace")
|
super(Backspace_Key, self).__init__("Backspace", "Backspace", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._clicked)
|
self.connect("released", self._clicked)
|
||||||
|
@ -73,7 +65,7 @@ class Backspace_Key(Key):
|
||||||
|
|
||||||
class Emoji_Keys(Key):
|
class Emoji_Keys(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Emoji_Keys, self).__init__("Emoji", "Emoji")
|
super(Emoji_Keys, self).__init__("Emoji", "Emoji", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._clicked)
|
self.connect("released", self._clicked)
|
||||||
|
@ -89,7 +81,7 @@ class Emoji_Keys(Key):
|
||||||
|
|
||||||
class Enter_Key(Key):
|
class Enter_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Enter_Key, self).__init__("Enter", "Enter")
|
super(Enter_Key, self).__init__("Enter", "Enter", iscontrol=True)
|
||||||
self.setup_styling()
|
self.setup_styling()
|
||||||
|
|
||||||
def setup_styling(self):
|
def setup_styling(self):
|
||||||
|
@ -103,21 +95,21 @@ class Enter_Key(Key):
|
||||||
|
|
||||||
class Esc_Key(Key):
|
class Esc_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Esc_Key, self).__init__("Esc", "Esc")
|
super(Esc_Key, self).__init__("Esc", "Esc", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Space_Key(Key):
|
class Space_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Space_Key, self).__init__("Space", "Space")
|
super(Space_Key, self).__init__("Space", "Space", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class AT_Key(Key):
|
class AT_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(AT_Key, self).__init__("@", "@")
|
super(AT_Key, self).__init__("@", "@", iscontrol=True)
|
||||||
|
|
||||||
class COM_Key(Key):
|
class COM_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -128,70 +120,70 @@ class COM_Key(Key):
|
||||||
|
|
||||||
class Tab_Key(Key):
|
class Tab_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Tab_Key, self).__init__("Tab", "Tab")
|
super(Tab_Key, self).__init__("Tab", "Tab", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Del_Key(Key):
|
class Del_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Del_Key, self).__init__("Del", "Del")
|
super(Del_Key, self).__init__("Del", "Del", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Ctrl_Key(Key):
|
class Ctrl_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Ctrl_Key, self).__init__("Ctrl", "Ctrl")
|
super(Ctrl_Key, self).__init__("Ctrl", "Ctrl", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Shift_Key(Key):
|
class Shift_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Shift_Key, self).__init__("Shift", "Shift")
|
super(Shift_Key, self).__init__("Shift", "Shift", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Alt_Key(Key):
|
class Alt_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Alt_Key, self).__init__("Alt", "Alt")
|
super(Alt_Key, self).__init__("Alt", "Alt", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class PrtSc_Key(Key):
|
class PrtSc_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(PrtSc_Key, self).__init__("PrtSc", "PrtSc")
|
super(PrtSc_Key, self).__init__("PrtSc", "PrtSc", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Up_Key(Key):
|
class Up_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Up_Key, self).__init__("Up", "Up")
|
super(Up_Key, self).__init__("Up", "Up", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Down_Key(Key):
|
class Down_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Down_Key, self).__init__("Down", "Down")
|
super(Down_Key, self).__init__("Down", "Down", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Left_Key(Key):
|
class Left_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Left_Key, self).__init__("Left", "Left")
|
super(Left_Key, self).__init__("Left", "Left", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
||||||
class Right_Key(Key):
|
class Right_Key(Key):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Right_Key, self).__init__("Right", "Right")
|
super(Right_Key, self).__init__("Right", "Right", iscontrol=True)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_press_special_key)
|
self.connect("released", self._do_press_special_key)
|
||||||
|
|
|
@ -9,9 +9,10 @@ from gi.repository import Gtk
|
||||||
|
|
||||||
|
|
||||||
class Key(Gtk.Button or Gtk.ToggleButton):
|
class Key(Gtk.Button or Gtk.ToggleButton):
|
||||||
def __init__(self, primary = "NULL", secondary = "NULL", emoji = "NULL"):
|
def __init__(self, primary = "NULL", secondary = "NULL", emoji = "NULL", iscontrol=False):
|
||||||
super(Key, self).__init__()
|
super(Key, self).__init__()
|
||||||
|
|
||||||
|
self.iscontrol = iscontrol
|
||||||
self._primary_symbol = primary
|
self._primary_symbol = primary
|
||||||
self._secondary_symbol = secondary
|
self._secondary_symbol = secondary
|
||||||
self._emoji_symbol = emoji
|
self._emoji_symbol = emoji
|
||||||
|
@ -20,13 +21,17 @@ class Key(Gtk.Button or Gtk.ToggleButton):
|
||||||
self._is_emoji = False
|
self._is_emoji = False
|
||||||
|
|
||||||
self.set_label(self._primary_symbol)
|
self.set_label(self._primary_symbol)
|
||||||
|
self.setup_custom_signals()
|
||||||
self.setup_signals()
|
self.setup_signals()
|
||||||
|
|
||||||
|
def setup_custom_signals(self):
|
||||||
|
event_system.subscribe("toggle_caps", self.toggle_caps)
|
||||||
|
event_system.subscribe("toggle_symbol_keys", self.toggle_symbol_keys)
|
||||||
|
|
||||||
def setup_signals(self):
|
def setup_signals(self):
|
||||||
self.connect("released", self._do_type)
|
self.connect("released", self._do_type)
|
||||||
self.connect("toggle-caps", self.toggle_caps)
|
# self.connect("toggle-caps", self.toggle_caps)
|
||||||
self.connect("toggle-symbol-keys", self.toggle_symbol_keys)
|
# self.connect("toggle-symbol-keys", self.toggle_symbol_keys)
|
||||||
self.connect("toggle-emoji-keys", self.toggle_emoji_keys)
|
self.connect("toggle-emoji-keys", self.toggle_emoji_keys)
|
||||||
|
|
||||||
def _do_type(self, widget = None):
|
def _do_type(self, widget = None):
|
||||||
|
@ -42,6 +47,7 @@ class Key(Gtk.Button or Gtk.ToggleButton):
|
||||||
typwriter.press_special_keys(key)
|
typwriter.press_special_keys(key)
|
||||||
|
|
||||||
def toggle_symbol_keys(self, widget = None, eve = None):
|
def toggle_symbol_keys(self, widget = None, eve = None):
|
||||||
|
if not self.iscontrol:
|
||||||
self._is_symbol = not self._is_symbol
|
self._is_symbol = not self._is_symbol
|
||||||
if self._is_symbol:
|
if self._is_symbol:
|
||||||
self.set_label(self._secondary_symbol)
|
self.set_label(self._secondary_symbol)
|
||||||
|
@ -52,6 +58,7 @@ class Key(Gtk.Button or Gtk.ToggleButton):
|
||||||
|
|
||||||
# NOTE: Might use name attrib on widgets and de-duplicate this and the above logic.
|
# NOTE: Might use name attrib on widgets and de-duplicate this and the above logic.
|
||||||
def toggle_emoji_keys(self, widget = None, eve = None):
|
def toggle_emoji_keys(self, widget = None, eve = None):
|
||||||
|
if not self.iscontrol:
|
||||||
self._is_emoji = not self._is_emoji
|
self._is_emoji = not self._is_emoji
|
||||||
if self._is_emoji:
|
if self._is_emoji:
|
||||||
self.set_label(self._emoji_symbol)
|
self.set_label(self._emoji_symbol)
|
||||||
|
@ -61,6 +68,7 @@ class Key(Gtk.Button or Gtk.ToggleButton):
|
||||||
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
|
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
|
||||||
|
|
||||||
def toggle_caps(self, widget = None, eve = None):
|
def toggle_caps(self, widget = None, eve = None):
|
||||||
|
if not self.iscontrol:
|
||||||
self._is_upper = not self._is_upper
|
self._is_upper = not self._is_upper
|
||||||
if not self._is_symbol:
|
if not self._is_symbol:
|
||||||
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
|
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
# Python imports
|
||||||
|
|
||||||
|
# Lib imports
|
||||||
|
|
||||||
|
# Application imports
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class EndpointRegistry():
|
||||||
|
def __init__(self):
|
||||||
|
self._endpoints = {}
|
||||||
|
|
||||||
|
def register(self, rule, **options):
|
||||||
|
def decorator(f):
|
||||||
|
self._endpoints[rule] = f
|
||||||
|
return f
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
def get_endpoints(self):
|
||||||
|
return self._endpoints
|
|
@ -0,0 +1,30 @@
|
||||||
|
# Python imports
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
# Lib imports
|
||||||
|
|
||||||
|
# Application imports
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class EventSystem:
|
||||||
|
""" Create event system. """
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.subscribers = defaultdict(list)
|
||||||
|
|
||||||
|
|
||||||
|
def subscribe(self, event_type, fn):
|
||||||
|
self.subscribers[event_type].append(fn)
|
||||||
|
|
||||||
|
def emit(self, event_type, data = None):
|
||||||
|
if event_type in self.subscribers:
|
||||||
|
for fn in self.subscribers[event_type]:
|
||||||
|
if data:
|
||||||
|
if hasattr(data, '__iter__') and not type(data) is str:
|
||||||
|
fn(*data)
|
||||||
|
else:
|
||||||
|
fn(data)
|
||||||
|
else:
|
||||||
|
fn()
|
Loading…
Reference in New Issue