Did updated event system

This commit is contained in:
itdominator 2022-10-06 16:57:08 -05:00
parent 2e4d1cd803
commit 0b54243be6
5 changed files with 109 additions and 70 deletions

View File

@ -5,7 +5,8 @@ import builtins, threading
# Application imports
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):
def __init__(self):
self.isCtrlOn = False
@ -57,17 +43,17 @@ keys_json = {
},
"row2": {
"pKeys": ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
"sKeys": ['\\', '^', '#', '$', '%', '&', '-', '_', '<', '>'],
"sKeys": ['\\', '^', '#', '$', '%', '&', '-', '_', '"', '*'],
"eKeys": ['', '', '', '', '', '', '', '', '', '']
},
"row3": {
"pKeys": ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', "'"],
"sKeys": ['\\', '/', '|', ':', '=', '+', '"', '*', ';', '!'],
"sKeys": ['/', '|', ':', '=', '+', '', '', '', ';', '!'],
"eKeys": ['', '', '', '', '', '', '', '', '', '']
},
"row4": {
"pKeys": ['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '?'],
"sKeys": ['`', '', '', '', '[', ']', '(', ')', '{', '}'],
"sKeys": ['', '', '<', '>', '[', ']', '(', ')', '{', '}'],
"eKeys": ['', '', '', '', '', '', '', '', '', '']
},
}
@ -79,11 +65,12 @@ keys_json = {
# NOTE: Just reminding myself we can add to builtins two different ways...
# __builtins__.update({"event_system": Builtins()})
builtins.app_name = "Mouse Keyboard"
builtins.endpoint_registry = EndpointRegistry()
builtins.typwriter = Pyautogui_Controller()
builtins.threaded = threaded_wrapper
builtins.daemon_threaded = daemon_threaded_wrapper
builtins.keys_set = keys_json
builtins.trace_debug = False
builtins.debug = False
builtins.app_settings = None
builtins.endpoint_registry = EndpointRegistry()
builtins.event_system = EventSystem()
builtins.typwriter = Pyautogui_Controller()

View File

@ -15,14 +15,14 @@ from .key import Key
class Esc_Key(Key):
def __init__(self):
super(Esc_Key, self).__init__("Esc", "Esc")
super(Esc_Key, self).__init__("Esc", "Esc", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Symbols_Key(Key):
def __init__(self):
super(Symbols_Key, self).__init__("Symbols", "Symbols")
super(Symbols_Key, self).__init__("Symbols", "Symbols", iscontrol=True)
def setup_signals(self):
self.connect("released", self._clicked)
@ -30,15 +30,11 @@ class Symbols_Key(Key):
def _clicked(self, widget = None):
ctx = widget.get_style_context()
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
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", ())
event_system.emit("toggle_symbol_keys")
class CAPS_Key(Key):
def __init__(self):
super(CAPS_Key, self).__init__("Caps", "Caps")
super(CAPS_Key, self).__init__("Caps", "Caps", iscontrol=True)
self.setup_styling()
self.show_all()
@ -52,18 +48,14 @@ class CAPS_Key(Key):
def _clicked(self, widget = None):
ctx = widget.get_style_context()
ctx.remove_class("toggled_bttn") if ctx.has_class("toggled_bttn") else ctx.add_class("toggled_bttn")
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", ())
event_system.emit("toggle_caps")
############################## Right_Column Keys ##############################
class Backspace_Key(Key):
def __init__(self):
super(Backspace_Key, self).__init__("Backspace", "Backspace")
super(Backspace_Key, self).__init__("Backspace", "Backspace", iscontrol=True)
def setup_signals(self):
self.connect("released", self._clicked)
@ -73,7 +65,7 @@ class Backspace_Key(Key):
class Emoji_Keys(Key):
def __init__(self):
super(Emoji_Keys, self).__init__("Emoji", "Emoji")
super(Emoji_Keys, self).__init__("Emoji", "Emoji", iscontrol=True)
def setup_signals(self):
self.connect("released", self._clicked)
@ -89,7 +81,7 @@ class Emoji_Keys(Key):
class Enter_Key(Key):
def __init__(self):
super(Enter_Key, self).__init__("Enter", "Enter")
super(Enter_Key, self).__init__("Enter", "Enter", iscontrol=True)
self.setup_styling()
def setup_styling(self):
@ -103,21 +95,21 @@ class Enter_Key(Key):
class Esc_Key(Key):
def __init__(self):
super(Esc_Key, self).__init__("Esc", "Esc")
super(Esc_Key, self).__init__("Esc", "Esc", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Space_Key(Key):
def __init__(self):
super(Space_Key, self).__init__("Space", "Space")
super(Space_Key, self).__init__("Space", "Space", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class AT_Key(Key):
def __init__(self):
super(AT_Key, self).__init__("@", "@")
super(AT_Key, self).__init__("@", "@", iscontrol=True)
class COM_Key(Key):
def __init__(self):
@ -128,70 +120,70 @@ class COM_Key(Key):
class Tab_Key(Key):
def __init__(self):
super(Tab_Key, self).__init__("Tab", "Tab")
super(Tab_Key, self).__init__("Tab", "Tab", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Del_Key(Key):
def __init__(self):
super(Del_Key, self).__init__("Del", "Del")
super(Del_Key, self).__init__("Del", "Del", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Ctrl_Key(Key):
def __init__(self):
super(Ctrl_Key, self).__init__("Ctrl", "Ctrl")
super(Ctrl_Key, self).__init__("Ctrl", "Ctrl", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Shift_Key(Key):
def __init__(self):
super(Shift_Key, self).__init__("Shift", "Shift")
super(Shift_Key, self).__init__("Shift", "Shift", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Alt_Key(Key):
def __init__(self):
super(Alt_Key, self).__init__("Alt", "Alt")
super(Alt_Key, self).__init__("Alt", "Alt", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class PrtSc_Key(Key):
def __init__(self):
super(PrtSc_Key, self).__init__("PrtSc", "PrtSc")
super(PrtSc_Key, self).__init__("PrtSc", "PrtSc", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Up_Key(Key):
def __init__(self):
super(Up_Key, self).__init__("Up", "Up")
super(Up_Key, self).__init__("Up", "Up", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Down_Key(Key):
def __init__(self):
super(Down_Key, self).__init__("Down", "Down")
super(Down_Key, self).__init__("Down", "Down", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Left_Key(Key):
def __init__(self):
super(Left_Key, self).__init__("Left", "Left")
super(Left_Key, self).__init__("Left", "Left", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)
class Right_Key(Key):
def __init__(self):
super(Right_Key, self).__init__("Right", "Right")
super(Right_Key, self).__init__("Right", "Right", iscontrol=True)
def setup_signals(self):
self.connect("released", self._do_press_special_key)

View File

@ -9,9 +9,10 @@ from gi.repository import Gtk
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__()
self.iscontrol = iscontrol
self._primary_symbol = primary
self._secondary_symbol = secondary
self._emoji_symbol = emoji
@ -20,13 +21,17 @@ class Key(Gtk.Button or Gtk.ToggleButton):
self._is_emoji = False
self.set_label(self._primary_symbol)
self.setup_custom_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):
self.connect("released", self._do_type)
self.connect("toggle-caps", self.toggle_caps)
self.connect("toggle-symbol-keys", self.toggle_symbol_keys)
# self.connect("toggle-caps", self.toggle_caps)
# self.connect("toggle-symbol-keys", self.toggle_symbol_keys)
self.connect("toggle-emoji-keys", self.toggle_emoji_keys)
def _do_type(self, widget = None):
@ -42,25 +47,28 @@ class Key(Gtk.Button or Gtk.ToggleButton):
typwriter.press_special_keys(key)
def toggle_symbol_keys(self, widget = None, eve = None):
self._is_symbol = not self._is_symbol
if self._is_symbol:
self.set_label(self._secondary_symbol)
elif self._is_emoji:
self.set_label(self._emoji_symbol)
else:
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
if not self.iscontrol:
self._is_symbol = not self._is_symbol
if self._is_symbol:
self.set_label(self._secondary_symbol)
elif self._is_emoji:
self.set_label(self._emoji_symbol)
else:
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
# NOTE: Might use name attrib on widgets and de-duplicate this and the above logic.
def toggle_emoji_keys(self, widget = None, eve = None):
self._is_emoji = not self._is_emoji
if self._is_emoji:
self.set_label(self._emoji_symbol)
elif self._is_symbol:
self.set_label(self._secondary_symbol)
else:
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
if not self.iscontrol:
self._is_emoji = not self._is_emoji
if self._is_emoji:
self.set_label(self._emoji_symbol)
elif self._is_symbol:
self.set_label(self._secondary_symbol)
else:
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):
self._is_upper = not self._is_upper
if not self._is_symbol:
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())
if not self.iscontrol:
self._is_upper = not self._is_upper
if not self._is_symbol:
self.set_label(self._primary_symbol.upper()) if self._is_upper else self.set_label(self._primary_symbol.lower())

View File

@ -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

30
src/utils/event_system.py Normal file
View File

@ -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()