Fix nix run .#clan-app
This commit is contained in:
@@ -5,17 +5,21 @@ from clan_cli.profiler import profile
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
from clan_cli.custom_logger import setup_logging
|
||||
from clan_app.deps.webview.webview import Webview
|
||||
from pathlib import Path
|
||||
import os
|
||||
import argparse
|
||||
from urllib.parse import quote
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from clan_cli.custom_logger import setup_logging
|
||||
|
||||
from clan_app.deps.webview.webview import Webview
|
||||
|
||||
|
||||
@profile
|
||||
def main(argv: list[str] = sys.argv) -> int:
|
||||
parser = argparse.ArgumentParser(description="Clan App")
|
||||
parser.add_argument("--content-uri", type=str, help="The URI of the content to display")
|
||||
parser.add_argument(
|
||||
"--content-uri", type=str, help="The URI of the content to display"
|
||||
)
|
||||
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
|
||||
args = parser.parse_args(argv[1:])
|
||||
|
||||
@@ -36,16 +40,6 @@ def main(argv: list[str] = sys.argv) -> int:
|
||||
site_index: Path = Path(os.getenv("WEBUI_PATH", ".")).resolve() / "index.html"
|
||||
content_uri = f"file://{site_index}"
|
||||
|
||||
html = """
|
||||
<html>
|
||||
<body>
|
||||
<h1>Hello from Python Webview!</h1>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
webview = Webview()
|
||||
webview.navigate(f"data:text/html,{quote(html)}")
|
||||
#webview.navigate(content_uri)
|
||||
webview.navigate(content_uri)
|
||||
webview.run()
|
||||
|
||||
|
||||
@@ -1,43 +1,47 @@
|
||||
import ctypes
|
||||
import sys
|
||||
import ctypes.util
|
||||
import os
|
||||
import platform
|
||||
import urllib.request
|
||||
from ctypes import CFUNCTYPE, c_char_p, c_int, c_void_p
|
||||
from pathlib import Path
|
||||
from ctypes import c_int, c_char_p, c_void_p, CFUNCTYPE
|
||||
import ctypes.util
|
||||
|
||||
|
||||
def _encode_c_string(s: str) -> bytes:
|
||||
return s.encode("utf-8")
|
||||
|
||||
|
||||
def _get_webview_version():
|
||||
"""Get webview version from environment variable or use default"""
|
||||
return os.getenv("WEBVIEW_VERSION", "0.8.1")
|
||||
|
||||
|
||||
def _get_lib_names():
|
||||
"""Get platform-specific library names."""
|
||||
system = platform.system().lower()
|
||||
machine = platform.machine().lower()
|
||||
|
||||
|
||||
if system == "windows":
|
||||
if machine == "amd64" or machine == "x86_64":
|
||||
return ["webview.dll", "WebView2Loader.dll"]
|
||||
elif machine == "arm64":
|
||||
raise Exception("arm64 is not supported on Windows")
|
||||
elif system == "darwin":
|
||||
if machine == "arm64":
|
||||
msg = "arm64 is not supported on Windows"
|
||||
raise Exception(msg)
|
||||
return None
|
||||
if system == "darwin":
|
||||
if machine == "arm64":
|
||||
return ["libwebview.aarch64.dylib"]
|
||||
else:
|
||||
return ["libwebview.x86_64.dylib"]
|
||||
else: # linux
|
||||
return ["libwebview.so"]
|
||||
return ["libwebview.x86_64.dylib"]
|
||||
# linux
|
||||
return ["libwebview.so"]
|
||||
|
||||
|
||||
def _be_sure_libraries():
|
||||
"""Ensure libraries exist and return paths."""
|
||||
|
||||
lib_dir = os.environ.get("WEBVIEW_LIB_DIR")
|
||||
if not lib_dir:
|
||||
raise RuntimeError("WEBVIEW_LIB_DIR environment variable is not set")
|
||||
msg = "WEBVIEW_LIB_DIR environment variable is not set"
|
||||
raise RuntimeError(msg)
|
||||
lib_dir = Path(lib_dir)
|
||||
lib_names = _get_lib_names()
|
||||
lib_paths = [lib_dir / lib_name for lib_name in lib_names]
|
||||
@@ -46,11 +50,12 @@ def _be_sure_libraries():
|
||||
missing_libs = [path for path in lib_paths if not path.exists()]
|
||||
if not missing_libs:
|
||||
return lib_paths
|
||||
return None
|
||||
|
||||
|
||||
class _WebviewLibrary:
|
||||
def __init__(self):
|
||||
lib_names=_get_lib_names()
|
||||
def __init__(self) -> None:
|
||||
lib_names = _get_lib_names()
|
||||
try:
|
||||
library_path = ctypes.util.find_library(lib_names[0])
|
||||
if not library_path:
|
||||
@@ -99,4 +104,5 @@ class _WebviewLibrary:
|
||||
|
||||
self.CFUNCTYPE = CFUNCTYPE
|
||||
|
||||
|
||||
_webview_lib = _WebviewLibrary()
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from enum import IntEnum
|
||||
from typing import Optional, Callable, Any
|
||||
import json
|
||||
import ctypes
|
||||
from ._webview_ffi import _webview_lib, _encode_c_string
|
||||
import json
|
||||
from collections.abc import Callable
|
||||
from enum import IntEnum
|
||||
from typing import Any
|
||||
|
||||
from ._webview_ffi import _encode_c_string, _webview_lib
|
||||
|
||||
|
||||
class SizeHint(IntEnum):
|
||||
NONE = 0
|
||||
@@ -10,14 +13,18 @@ class SizeHint(IntEnum):
|
||||
MAX = 2
|
||||
FIXED = 3
|
||||
|
||||
|
||||
class Size:
|
||||
def __init__(self, width: int, height: int, hint: SizeHint):
|
||||
def __init__(self, width: int, height: int, hint: SizeHint) -> None:
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.hint = hint
|
||||
|
||||
|
||||
class Webview:
|
||||
def __init__(self, debug: bool = False, size: Optional[Size] = None, window: Optional[int] = None):
|
||||
def __init__(
|
||||
self, debug: bool = False, size: Size | None = None, window: int | None = None
|
||||
) -> None:
|
||||
self._handle = _webview_lib.webview_create(int(debug), window)
|
||||
self._callbacks = {}
|
||||
|
||||
@@ -29,8 +36,10 @@ class Webview:
|
||||
return self._size
|
||||
|
||||
@size.setter
|
||||
def size(self, value: Size):
|
||||
_webview_lib.webview_set_size(self._handle, value.width, value.height, value.hint)
|
||||
def size(self, value: Size) -> None:
|
||||
_webview_lib.webview_set_size(
|
||||
self._handle, value.width, value.height, value.hint
|
||||
)
|
||||
self._size = value
|
||||
|
||||
@property
|
||||
@@ -38,26 +47,26 @@ class Webview:
|
||||
return self._title
|
||||
|
||||
@title.setter
|
||||
def title(self, value: str):
|
||||
def title(self, value: str) -> None:
|
||||
_webview_lib.webview_set_title(self._handle, _encode_c_string(value))
|
||||
self._title = value
|
||||
|
||||
def destroy(self):
|
||||
def destroy(self) -> None:
|
||||
for name in list(self._callbacks.keys()):
|
||||
self.unbind(name)
|
||||
_webview_lib.webview_terminate(self._handle)
|
||||
_webview_lib.webview_destroy(self._handle)
|
||||
self._handle = None
|
||||
|
||||
def navigate(self, url: str):
|
||||
def navigate(self, url: str) -> None:
|
||||
_webview_lib.webview_navigate(self._handle, _encode_c_string(url))
|
||||
|
||||
def run(self):
|
||||
def run(self) -> None:
|
||||
_webview_lib.webview_run(self._handle)
|
||||
self.destroy()
|
||||
|
||||
def bind(self, name: str, callback: Callable[..., Any]):
|
||||
def wrapper(seq: bytes, req: bytes, arg: int):
|
||||
def bind(self, name: str, callback: Callable[..., Any]) -> None:
|
||||
def wrapper(seq: bytes, req: bytes, arg: int) -> None:
|
||||
args = json.loads(req.decode())
|
||||
try:
|
||||
result = callback(*args)
|
||||
@@ -67,24 +76,31 @@ class Webview:
|
||||
success = False
|
||||
self.return_(seq.decode(), 0 if success else 1, json.dumps(result))
|
||||
|
||||
c_callback = _webview_lib.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_void_p)(wrapper)
|
||||
c_callback = _webview_lib.CFUNCTYPE(
|
||||
None, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_void_p
|
||||
)(wrapper)
|
||||
self._callbacks[name] = c_callback
|
||||
_webview_lib.webview_bind(self._handle, _encode_c_string(name), c_callback, None)
|
||||
_webview_lib.webview_bind(
|
||||
self._handle, _encode_c_string(name), c_callback, None
|
||||
)
|
||||
|
||||
def unbind(self, name: str):
|
||||
def unbind(self, name: str) -> None:
|
||||
if name in self._callbacks:
|
||||
_webview_lib.webview_unbind(self._handle, _encode_c_string(name))
|
||||
del self._callbacks[name]
|
||||
|
||||
def return_(self, seq: str, status: int, result: str):
|
||||
_webview_lib.webview_return(self._handle, _encode_c_string(seq), status, _encode_c_string(result))
|
||||
def return_(self, seq: str, status: int, result: str) -> None:
|
||||
_webview_lib.webview_return(
|
||||
self._handle, _encode_c_string(seq), status, _encode_c_string(result)
|
||||
)
|
||||
|
||||
def eval(self, source: str):
|
||||
def eval(self, source: str) -> None:
|
||||
_webview_lib.webview_eval(self._handle, _encode_c_string(source))
|
||||
|
||||
def init(self, source: str):
|
||||
def init(self, source: str) -> None:
|
||||
_webview_lib.webview_init(self._handle, _encode_c_string(source))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
wv = Webview()
|
||||
wv.title = "Hello, World!"
|
||||
|
||||
Reference in New Issue
Block a user