clan-app: Add webview hot-reloading
This commit is contained in:
@@ -10,6 +10,8 @@ from clan_app.singletons.toast import InfoToast, ToastOverlay
|
||||
gi.require_version("Gtk", "4.0")
|
||||
gi.require_version("Adw", "1")
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from clan_cli.custom_logger import setup_logging
|
||||
from gi.repository import Adw, Gdk, Gio, Gtk
|
||||
|
||||
@@ -47,6 +49,19 @@ class MainApplication(Adw.Application):
|
||||
None,
|
||||
)
|
||||
|
||||
self.add_main_option(
|
||||
"content-uri",
|
||||
GLib.OptionFlags.NONE,
|
||||
GLib.OptionFlags.NONE,
|
||||
GLib.OptionArg.STRING,
|
||||
"set the webview content uri",
|
||||
None,
|
||||
)
|
||||
|
||||
site_index: Path = (
|
||||
Path(__file__).parent.parent / Path("clan_app/.webui/index.html")
|
||||
).resolve()
|
||||
self.content_uri = f"file://{site_index}"
|
||||
self.window: MainWindow | None = None
|
||||
self.connect("activate", self.on_activate)
|
||||
self.connect("shutdown", self.on_shutdown)
|
||||
@@ -82,13 +97,22 @@ class MainApplication(Adw.Application):
|
||||
InfoToast("Debug logging enabled").toast, "info.debugging.enabled"
|
||||
)
|
||||
|
||||
if "content-uri" in options:
|
||||
self.content_uri = options["content-uri"]
|
||||
log.debug(f"Setting content uri to {self.content_uri}")
|
||||
|
||||
args = command_line.get_arguments()
|
||||
|
||||
self.activate()
|
||||
|
||||
# Check if there are arguments that are not inside the options
|
||||
if len(args) > 1:
|
||||
uri = args[1]
|
||||
self.emit("join_request", uri)
|
||||
non_option_args = [arg for arg in args[1:] if arg not in options.values()]
|
||||
breakpoint()
|
||||
if non_option_args:
|
||||
uri = non_option_args[0]
|
||||
self.emit("join_request", uri)
|
||||
|
||||
return 0
|
||||
|
||||
def on_window_hide_unhide(self, *_args: Any) -> None:
|
||||
@@ -106,7 +130,9 @@ class MainApplication(Adw.Application):
|
||||
def on_activate(self, source: "MainApplication") -> None:
|
||||
if not self.window:
|
||||
self.init_style()
|
||||
self.window = MainWindow(config=ClanConfig(initial_view="webview"))
|
||||
self.window = MainWindow(
|
||||
config=ClanConfig(initial_view="webview", content_uri=self.content_uri)
|
||||
)
|
||||
self.window.set_application(self)
|
||||
|
||||
self.window.show()
|
||||
|
||||
@@ -8,3 +8,4 @@ gi.require_version("Gtk", "4.0")
|
||||
@dataclass
|
||||
class ClanConfig:
|
||||
initial_view: str
|
||||
content_uri: str
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import dataclasses
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
import threading
|
||||
from collections.abc import Callable
|
||||
from dataclasses import fields, is_dataclass
|
||||
@@ -18,10 +17,6 @@ gi.require_version("WebKit", "6.0")
|
||||
|
||||
from gi.repository import Gio, GLib, Gtk, WebKit
|
||||
|
||||
site_index: Path = (
|
||||
Path(sys.argv[0]).absolute() / Path("../..") / Path("clan_app/.webui/index.html")
|
||||
).resolve()
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -201,7 +196,7 @@ def from_dict(t: type, data: dict[str, Any] | None) -> Any:
|
||||
|
||||
|
||||
class WebView:
|
||||
def __init__(self, methods: dict[str, Callable]) -> None:
|
||||
def __init__(self, content_uri: str, methods: dict[str, Callable]) -> None:
|
||||
self.method_registry: dict[str, Callable] = methods
|
||||
|
||||
self.webview = WebKit.WebView()
|
||||
@@ -217,7 +212,7 @@ class WebView:
|
||||
self.manager.register_script_message_handler("gtk")
|
||||
self.manager.connect("script-message-received", self.on_message_received)
|
||||
|
||||
self.webview.load_uri(f"file://{site_index}")
|
||||
self.webview.load_uri(content_uri)
|
||||
|
||||
# global mutex lock to ensure functions run sequentially
|
||||
self.mutex_lock = Lock()
|
||||
|
||||
@@ -54,7 +54,7 @@ class MainWindow(Adw.ApplicationWindow):
|
||||
# Override platform specific functions
|
||||
API.register(open_file)
|
||||
|
||||
webview = WebView(methods=API._registry)
|
||||
webview = WebView(methods=API._registry, content_uri=config.content_uri)
|
||||
|
||||
stack_view.add_named(webview.get_webview(), "webview")
|
||||
stack_view.set_visible_child_name(config.initial_view)
|
||||
|
||||
Reference in New Issue
Block a user