added webkit integration widget; made main method

This commit is contained in:
2024-02-08 21:26:13 -06:00
parent 6ed96c426d
commit 70f10c6263
1700 changed files with 19812 additions and 195 deletions

View File

@@ -6,7 +6,7 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from ..widgets.webkit_ui import WebkitUI
from ..widgets.webkit.webkit_ui import WebkitUI
@@ -45,4 +45,4 @@ class CenterContainer(Gtk.Box):
self.add( WebkitUI() )
def _hello_world(self, widget = None, eve = None):
logger.debug("Hello, World!")
logger.debug("Hello, World!")

View File

@@ -9,9 +9,12 @@ from gi.repository import Gtk
# Application imports
from ..mixins.signals_mixins import SignalsMixins
from ..mixins.dummy_mixin import DummyMixin
from .base_controller_data import BaseControllerData
from ..containers.base_container import BaseContainer
from .base_controller_data import BaseControllerData
from .bridge_controller import BridgeController
class BaseController(DummyMixin, SignalsMixins, BaseControllerData):
@@ -21,6 +24,7 @@ class BaseController(DummyMixin, SignalsMixins, BaseControllerData):
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_controllers()
self.print_hello_world() # A mixin method from the DummyMixin file
@@ -53,6 +57,9 @@ class BaseController(DummyMixin, SignalsMixins, BaseControllerData):
event_system.subscribe("handle_dir_from_ipc", self.handle_dir_from_ipc)
event_system.subscribe("tggl_top_main_menubar", self._tggl_top_main_menubar)
def _load_controllers(self):
BridgeController()
def _tggl_top_main_menubar(self):
logger.debug("_tggl_top_main_menubar > stub...")

View File

@@ -0,0 +1,43 @@
# Python imports
import base64
# Lib imports
# Application imports
class BridgeController:
def __init__(self):
self.opened_files = {}
self._setup_signals()
self._subscribe_to_events()
def _setup_signals(self):
...
def _subscribe_to_events(self):
event_system.subscribe("handle_bridge_event", self.handle_bridge_event)
def handle_bridge_event(self, event):
match event.topic:
case "save":
event_system.emit(f"handle_file_event_{event.originator}", (event,))
case "close":
event_system.emit(f"handle_file_event_{event.originator}", (event,))
case "load_buffer":
event_system.emit(f"handle_file_event_{event.originator}", (event,))
case "load_file":
event_system.emit(f"handle_file_event_{event.originator}", (event,))
case "alert":
content = base64.b64decode( event.content.encode() ).decode("utf-8")
logger.info(f"\nMessage Topic: {event.topic}\nMessage Content: {content}")
case "error":
content = base64.b64decode( event.content.encode() ).decode("utf-8")
logger.info(content)
case _:
...

View File

@@ -0,0 +1,3 @@
"""
WebKit2 UI Module
"""

View File

@@ -0,0 +1,108 @@
# Python imports
import json
# Lib imports
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gdk
from gi.repository import WebKit2
# Application imports
from libs.data_types import Event
class WebkitUI(WebKit2.WebView):
def __init__(self):
super(WebkitUI, self).__init__()
# self.get_context().set_sandbox_enabled(False)
self._load_settings()
self._setup_styling()
self._subscribe_to_events()
self._load_view()
self._setup_content_manager()
self.show_all()
if settings_manager.is_debug():
inspector = self.get_inspector()
inspector.show()
def _setup_styling(self):
self.set_vexpand(True)
self.set_hexpand(True)
self.set_background_color( Gdk.RGBA(0, 0, 0, 0.0) )
def _subscribe_to_events(self):
event_system.subscribe(f"ui_message", self.ui_message)
def _load_settings(self):
self.set_settings( WebkitUISettings() )
def _load_view(self):
path = settings_manager.get_context_path()
data = None
with open(f"{path}/index.html", "r") as f:
data = f.read()
self.load_html(content = data, base_uri = f"file://{path}/")
def _setup_content_manager(self):
content_manager = self.get_user_content_manager()
content_manager.connect("script-message-received", self._process_js_message)
content_manager.register_script_message_handler("backend")
def _process_js_message(self, user_content_manager, js_result):
js_value = js_result.get_js_value()
message = js_value.to_string()
try:
event = Event( **json.loads(message) )
event_system.emit("handle_bridge_event", (event,))
except Exception as e:
logger.info(e)
def ui_message(self, mtype, message):
command = f"displayMessage('{message}', '{mtype}', '3')"
self.run_javascript(command, None, None)
class WebkitUISettings(WebKit2.Settings):
def __init__(self):
super(WebkitUISettings, self).__init__()
self._set_default_settings()
# Note: Highly insecure setup but most "app" like setup I could think of.
# Audit heavily any scripts/links ran/clicked under this setup!
def _set_default_settings(self):
# self.set_enable_xss_auditor(True)
# self.set_enable_hyperlink_auditing(True)
self.set_enable_xss_auditor(False)
self.set_enable_hyperlink_auditing(False)
self.set_enable_dns_prefetching(False)
self.set_allow_file_access_from_file_urls(True)
self.set_allow_universal_access_from_file_urls(True)
# self.set_enable_java(True)
self.set_enable_page_cache(False)
self.set_enable_offline_web_application_cache(False)
self.set_enable_html5_local_storage(False)
self.set_enable_html5_database(False)
self.set_print_backgrounds(False)
self.set_enable_tabs_to_links(False)
self.set_enable_fullscreen(True)
self.set_enable_developer_extras(True)
self.set_enable_webrtc(True)
self.set_enable_webaudio(True)
self.set_enable_accelerated_2d_canvas(True)
self.set_user_agent(f"Mozilla/5.0 {app_name}")

View File

@@ -31,26 +31,43 @@ class WebkitUI(WebKit2.WebView):
self.set_background_color( Gdk.RGBA(0, 0, 0, 0.0) )
def _subscribe_to_events(self):
# event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc)
...
event_system.subscribe(f"ui_message", self.ui_message)
def _load_settings(self):
self.set_settings( WebkitUISettings() )
def _load_view(self):
path = settings_manager.get_context_path()
data = settings_manager.wrap_html_to_body("")
data = None
with open(f"{path}/index.html", "r") as f:
data = f.read()
self.load_html(content = data, base_uri = f"file://{path}/")
# def _load_view(self):
# path = settings_manager.get_context_path()
# data = settings_manager.wrap_html_to_body("")
# self.load_html(content = data, base_uri = f"file://{path}/")
def _setup_content_manager(self):
content_manager = self.get_user_content_manager()
content_manager.register_script_message_handler("backend")
content_manager.connect("script-message-received", self._process_js_message)
content_manager.register_script_message_handler("backend")
def _process_js_message(self, user_content_manager, js_result):
js_value = js_result.get_js_value()
print(js_value.to_string())
# self._web_view.run_javascript("do_stuff()", None, None)
message = js_value.to_string()
try:
event = Event( **json.loads(message) )
# event_system.emit("handle_bridge_event", (event,))
except Exception as e:
logger.info(e)
def ui_message(self, message, mtype):
command = f"displayMessage('{message}', '{mtype}', '3')"
self.run_javascript(command, None, None)
class WebkitUISettings(WebKit2.Settings):
@@ -63,20 +80,25 @@ class WebkitUISettings(WebKit2.Settings):
# Note: Highly insecure setup but most "app" like setup I could think of.
# Audit heavily any scripts/links ran/clicked under this setup!
def _set_default_settings(self):
self.set_enable_offline_web_application_cache(False)
self.enable_html5_local_storage(False)
self.enable_html5_database(False)
self.enable_xss_auditor(False)
self.set_enable_hyperlink_auditing(False)
self.set_enable_tabs_to_links(False)
self.enable_fullscreen(False)
self.set_print_backgrounds(False)
self.enable_webaudio(False)
self.set_enable_page_cache(False)
self.enable_accelerated_2d_canvas(True)
self.set_enable_xss_auditor(True)
self.set_enable_hyperlink_auditing(True)
# self.set_enable_xss_auditor(False)
# self.set_enable_hyperlink_auditing(False)
self.set_allow_file_access_from_file_urls(True)
self.set_allow_universal_access_from_file_urls(True)
self.set_enable_webrtc(True)
sdelf.set_user_agent(f"{app_name}")
self.set_enable_page_cache(False)
self.set_enable_offline_web_application_cache(False)
self.set_enable_html5_local_storage(False)
self.set_enable_html5_database(False)
self.set_enable_fullscreen(False)
self.set_print_backgrounds(False)
self.set_enable_tabs_to_links(False)
self.set_enable_developer_extras(True)
self.set_enable_webrtc(True)
self.set_enable_webaudio(True)
self.set_enable_accelerated_2d_canvas(True)
self.set_user_agent(f"{app_name}")

View File

@@ -36,6 +36,7 @@ class Window(Gtk.ApplicationWindow):
self._set_window_data()
self._set_size_constraints()
self.show()
@@ -50,8 +51,11 @@ class Window(Gtk.ApplicationWindow):
ctx.add_class(f"mw_transparency_{settings.theming.transparency}")
def _setup_signals(self):
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self._tear_down)
self.connect("focus-in-event", self._on_focus_in_event)
self.connect("focus-out-event", self._on_focus_out_event)
self.connect("delete-event", self._tear_down)
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self._tear_down)
def _subscribe_to_events(self):
event_system.subscribe("tear_down", self._tear_down)
@@ -101,6 +105,13 @@ class Window(Gtk.ApplicationWindow):
cr.paint()
cr.set_operator(cairo.OPERATOR_OVER)
def _on_focus_in_event(self, widget, event):
event_system.emit("pause_dnd_signals")
def _on_focus_out_event(self, widget, event):
event_system.emit("listen_dnd_signals")
def _load_interactive_debug(self):
self.set_interactive_debugging(True)