From 23d3181eac0851f326189355266886b8bf30333d Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sat, 18 Mar 2023 15:30:29 -0500 Subject: [PATCH] Externalized some signal handler logic to new files, better keybinding control --- src/core/controller.py | 27 +------ src/core/controller_data.py | 11 ++- src/core/mixins/signals/__init__.py | 3 + src/core/mixins/signals/ipc_signals_mixin.py | 17 +++++ .../mixins/signals/keyboard_signals_mixin.py | 75 +++++++++++++++++++ src/core/mixins/signals_mixins.py | 13 ++++ 6 files changed, 117 insertions(+), 29 deletions(-) create mode 100644 src/core/mixins/signals/__init__.py create mode 100644 src/core/mixins/signals/ipc_signals_mixin.py create mode 100644 src/core/mixins/signals/keyboard_signals_mixin.py create mode 100644 src/core/mixins/signals_mixins.py diff --git a/src/core/controller.py b/src/core/controller.py index a632026..191f67f 100644 --- a/src/core/controller.py +++ b/src/core/controller.py @@ -9,6 +9,7 @@ from gi.repository import Gdk from gi.repository import GLib # Application imports +from .mixins.signals_mixins import SignalsMixins from .mixins.dummy_mixin import DummyMixin from .controller_data import ControllerData from .core_widget import CoreWidget @@ -16,7 +17,7 @@ from .core_widget import CoreWidget -class Controller(DummyMixin, ControllerData): +class Controller(DummyMixin, SignalsMixins, ControllerData): def __init__(self, args, unknownargs): self._setup_styling() self._setup_signals() @@ -38,9 +39,6 @@ class Controller(DummyMixin, ControllerData): def _subscribe_to_events(self): event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc) - def handle_file_from_ipc(self, path: str) -> None: - print(f"Path From IPC: {path}") - def load_glade_file(self): self.builder = Gtk.Builder() self.builder.add_from_file(settings.get_glade_file()) @@ -53,24 +51,3 @@ class Controller(DummyMixin, ControllerData): def get_core_widget(self): return self.core_widget - - - def on_global_key_release_controller(self, widget: type, event: type) -> None: - """Handler for keyboard events""" - keyname = Gdk.keyval_name(event.keyval).lower() - if keyname.replace("_l", "").replace("_r", "") in ["control", "alt", "shift"]: - if "control" in keyname: - self.ctrl_down = False - if "shift" in keyname: - self.shift_down = False - if "alt" in keyname: - self.alt_down = False - - - mapping = keybindings.lookup(event) - if mapping: - getattr(self, mapping)() - return True - else: - print(f"on_global_key_release_controller > key > {keyname}") - print(f"Add logic or remove this from: {self.__class__}") diff --git a/src/core/controller_data.py b/src/core/controller_data.py index 2e59e1a..4068f12 100644 --- a/src/core/controller_data.py +++ b/src/core/controller_data.py @@ -14,12 +14,15 @@ class ControllerData: ''' ControllerData contains most of the state of the app at ay given time. It also has some support methods. ''' def setup_controller_data(self) -> None: - self.window = settings.get_main_window() - self.builder = None - self.core_widget = None + self.window = settings.get_main_window() + self.builder = None + self.core_widget = None + self.ctrl_down = False + self.shift_down = False + self.alt_down = False self.load_glade_file() - self.plugins = PluginsController() + self.plugins = PluginsController() def clear_console(self) -> None: diff --git a/src/core/mixins/signals/__init__.py b/src/core/mixins/signals/__init__.py new file mode 100644 index 0000000..03c3ec2 --- /dev/null +++ b/src/core/mixins/signals/__init__.py @@ -0,0 +1,3 @@ +""" + Signals module +""" diff --git a/src/core/mixins/signals/ipc_signals_mixin.py b/src/core/mixins/signals/ipc_signals_mixin.py new file mode 100644 index 0000000..34c6555 --- /dev/null +++ b/src/core/mixins/signals/ipc_signals_mixin.py @@ -0,0 +1,17 @@ +# Python imports + +# Lib imports + +# Application imports + + + + +class IPCSignalsMixin: + """ IPCSignalsMixin handle messages from another starting solarfm process. """ + + def print_to_console(self, message=None): + print(message) + + def handle_file_from_ipc(self, path: str) -> None: + print(f"Path From IPC: {path}") diff --git a/src/core/mixins/signals/keyboard_signals_mixin.py b/src/core/mixins/signals/keyboard_signals_mixin.py new file mode 100644 index 0000000..34833cb --- /dev/null +++ b/src/core/mixins/signals/keyboard_signals_mixin.py @@ -0,0 +1,75 @@ +# Python imports +import re + +# Lib imports +import gi +gi.require_version('Gtk', '3.0') +gi.require_version('Gdk', '3.0') +from gi.repository import Gtk +from gi.repository import Gdk + +# Application imports + + + +valid_keyvalue_pat = re.compile(r"[a-z0-9A-Z-_\[\]\(\)\| ]") + + + +class KeyboardSignalsMixin: + """ KeyboardSignalsMixin keyboard hooks controller. """ + + # TODO: Need to set methods that use this to somehow check the keybindings state instead. + def unset_keys_and_data(self, widget=None, eve=None): + self.ctrl_down = False + self.shift_down = False + self.alt_down = False + + def on_global_key_press_controller(self, eve, user_data): + keyname = Gdk.keyval_name(user_data.keyval).lower() + if keyname.replace("_l", "").replace("_r", "") in ["control", "alt", "shift"]: + if "control" in keyname: + self.ctrl_down = True + if "shift" in keyname: + self.shift_down = True + if "alt" in keyname: + self.alt_down = True + + def on_global_key_release_controller(self, widget, event): + """ Handler for keyboard events """ + keyname = Gdk.keyval_name(event.keyval).lower() + if keyname.replace("_l", "").replace("_r", "") in ["control", "alt", "shift"]: + if "control" in keyname: + self.ctrl_down = False + if "shift" in keyname: + self.shift_down = False + if "alt" in keyname: + self.alt_down = False + + mapping = keybindings.lookup(event) + if mapping: + # See if in controller scope + try: + getattr(self, mapping)() + return True + except Exception: + # Must be plugins scope, event call, OR we forgot to add method to controller scope + if "||" in mapping: + sender, eve_type = mapping.split("||") + else: + sender = "" + eve_type = mapping + + self.handle_key_event_system(sender, eve_type) + else: + logger.debug(f"on_global_key_release_controller > key > {keyname}") + + if self.ctrl_down: + if keyname in ["1", "kp_1", "2", "kp_2", "3", "kp_3", "4", "kp_4"]: + ... + + def handle_key_event_system(self, sender, eve_type): + event_system.emit(eve_type) + + def keyboard_close_tab(self): + ... diff --git a/src/core/mixins/signals_mixins.py b/src/core/mixins/signals_mixins.py new file mode 100644 index 0000000..76515f6 --- /dev/null +++ b/src/core/mixins/signals_mixins.py @@ -0,0 +1,13 @@ +# Python imports + +# Lib imports +from .signals.ipc_signals_mixin import IPCSignalsMixin +from .signals.keyboard_signals_mixin import KeyboardSignalsMixin + +# Application imports + + + + +class SignalsMixins(KeyboardSignalsMixin, IPCSignalsMixin): + ...