Python-With-Gtk-Template/src/core/controller.py

87 lines
2.6 KiB
Python
Raw Normal View History

2022-01-23 22:56:27 +00:00
# Python imports
import subprocess
2022-01-23 22:56:27 +00:00
2023-01-29 06:06:52 +00:00
# Lib imports
2022-01-23 22:56:27 +00:00
import gi
gi.require_version('Gtk', '3.0')
2022-03-07 23:45:37 +00:00
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
2022-01-23 22:56:27 +00:00
# Application imports
2022-02-25 23:53:58 +00:00
from .mixins.dummy_mixin import DummyMixin
from .controller_data import ControllerData
from .core_widget import CoreWidget
2022-01-23 22:56:27 +00:00
2023-01-29 06:06:52 +00:00
class Controller(DummyMixin, ControllerData):
def __init__(self, args, unknownargs):
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self.setup_controller_data()
2022-01-23 22:56:27 +00:00
self.print_hello_world() # A mixin method from the DummyMixin file
2022-10-01 04:35:03 +00:00
def _setup_styling(self):
...
2022-10-01 04:35:03 +00:00
def _setup_signals(self):
...
2022-01-23 22:56:27 +00:00
def _subscribe_to_events(self):
event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc)
2022-01-23 22:56:27 +00:00
2022-03-25 03:11:02 +00:00
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())
settings.set_builder(self.builder)
self.core_widget = CoreWidget()
settings.register_signals_to_builder([self, self.core_widget])
def get_core_widget(self):
return self.core_widget
2022-03-25 03:11:02 +00:00
def on_global_key_release_controller(self, widget: type, event: type) -> None:
2022-03-07 23:45:37 +00:00
"""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)
2022-03-07 23:45:37 +00:00
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__}")
2022-03-25 03:11:02 +00:00
def get_clipboard_data(self) -> str:
2022-01-23 22:56:27 +00:00
proc = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout=subprocess.PIPE)
retcode = proc.wait()
data = proc.stdout.read()
return data.decode("utf-8").strip()
2022-03-25 03:11:02 +00:00
def set_clipboard_data(self, data: type) -> None:
2022-01-23 22:56:27 +00:00
proc = subprocess.Popen(['xclip','-selection','clipboard'], stdin=subprocess.PIPE)
2022-12-11 20:52:09 +00:00
proc.stdin.write(data.encode("utf-8"))
2022-01-23 22:56:27 +00:00
proc.stdin.close()
retcode = proc.wait()