WIP
This commit is contained in:
parent
68fc75c087
commit
e466857046
2 changed files with 35 additions and 14 deletions
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
|
|
@ -51,6 +51,7 @@
|
||||||
"muxdelay",
|
"muxdelay",
|
||||||
"nodesc",
|
"nodesc",
|
||||||
"noprogress",
|
"noprogress",
|
||||||
|
"onefile",
|
||||||
"pathex",
|
"pathex",
|
||||||
"plexmatch",
|
"plexmatch",
|
||||||
"postprocessor",
|
"postprocessor",
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import threading
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# 1) create a global event
|
|
||||||
ready = threading.Event()
|
ready = threading.Event()
|
||||||
|
|
||||||
APP_NAME = "YTPTube"
|
APP_NAME = "YTPTube"
|
||||||
|
|
@ -13,7 +11,7 @@ APP_NAME = "YTPTube"
|
||||||
try:
|
try:
|
||||||
import webview # type: ignore
|
import webview # type: ignore
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
msg = "Please install the 'pywebview' to run YTPTube in native mode."
|
msg = "Please run 'pipenv install pywebview[qt]' package to run YTPTube in native mode."
|
||||||
raise ImportError(msg) from e
|
raise ImportError(msg) from e
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -34,7 +32,7 @@ def set_env():
|
||||||
if os.getenv("YTP_ACCESS_LOG", None) is None:
|
if os.getenv("YTP_ACCESS_LOG", None) is None:
|
||||||
dct["YTP_ACCESS_LOG"] = "false"
|
dct["YTP_ACCESS_LOG"] = "false"
|
||||||
|
|
||||||
if len(dct) > 0:
|
if dct:
|
||||||
os.environ.update(dct)
|
os.environ.update(dct)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -54,26 +52,48 @@ def app_start(host: str, port: int) -> None:
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
|
|
||||||
set_env()
|
set_env()
|
||||||
|
|
||||||
cfg = Path(os.getenv("YTP_CONFIG_PATH")) / "webview.json"
|
cfg_path: Path = Path(os.getenv("YTP_CONFIG_PATH")) / "webview.json"
|
||||||
if cfg.exists():
|
|
||||||
port = json.loads(cfg.read_text())["port"]
|
port = None
|
||||||
else:
|
win_conf: dict[str, int] = {}
|
||||||
logging.info(f"Creating configuration file at '{cfg}'.")
|
if cfg_path.exists():
|
||||||
|
data = json.loads(cfg_path.read_text())
|
||||||
|
port = data.get("port")
|
||||||
|
for key in ("width", "height", "x", "y"):
|
||||||
|
if key in data:
|
||||||
|
win_conf[key] = data[key]
|
||||||
|
|
||||||
|
if not port:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
s.bind((host, 0))
|
s.bind((host, 0))
|
||||||
port = s.getsockname()[1]
|
port = s.getsockname()[1]
|
||||||
cfg.write_text(json.dumps({"port": port}))
|
cfg_path.write_text(json.dumps({"port": port}))
|
||||||
|
|
||||||
threading.Thread(target=app_start, args=(host, port), daemon=True).start()
|
threading.Thread(target=app_start, args=(host, port), daemon=True).start()
|
||||||
|
|
||||||
ready.wait()
|
ready.wait()
|
||||||
|
|
||||||
webview.create_window(APP_NAME, f"http://{host}:{port}")
|
create_kwargs = {**win_conf, "resizable": True}
|
||||||
|
|
||||||
|
window = webview.create_window(APP_NAME, f"http://{host}:{port}", **create_kwargs)
|
||||||
|
|
||||||
|
def save_geometry():
|
||||||
|
cfg = {
|
||||||
|
"port": port,
|
||||||
|
"width": window.width,
|
||||||
|
"height": window.height,
|
||||||
|
"x": window.x,
|
||||||
|
"y": window.y,
|
||||||
|
}
|
||||||
|
cfg_path.write_text(json.dumps(cfg))
|
||||||
|
|
||||||
|
window.events.resized += lambda *_: save_geometry()
|
||||||
|
window.events.moved += lambda *_: save_geometry()
|
||||||
|
|
||||||
webview.start(
|
webview.start(
|
||||||
|
gui=str(os.getenv("YTP_WV_GUI", "qt")), # e.g. "qt" or "gtk"
|
||||||
debug=os.getenv("YTP_WV_DEBUG", "false").lower() in ("true", "1", "yes"),
|
debug=os.getenv("YTP_WV_DEBUG", "false").lower() in ("true", "1", "yes"),
|
||||||
storage_path=os.path.join(os.getenv("YTP_CONFIG_PATH", os.getcwd()), "webview"),
|
storage_path=str(Path(os.getenv("YTP_TEMP_PATH", os.getcwd())) / "webview"),
|
||||||
private_mode=False,
|
private_mode=False,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue