standalone_mode.py 1001 B

123456789101112131415161718192021222324252627282930
  1. import multiprocessing
  2. import os
  3. import signal
  4. import socket
  5. import tempfile
  6. import time
  7. from threading import Thread
  8. import webview
  9. shutdown = multiprocessing.Event()
  10. def open_window(url: str, title: str, width: int, height: int, fullscreen: bool, shutdown: multiprocessing.Event) -> None:
  11. window = webview.create_window(title, url=url, width=width, height=height, fullscreen=fullscreen)
  12. window.events.closing += shutdown.set # signal that the program should be closed to the main process
  13. webview.start(storage_path=tempfile.mkdtemp())
  14. def check_shutdown() -> None:
  15. while True:
  16. if shutdown.is_set():
  17. os.kill(os.getpgid(os.getpid()), signal.SIGTERM)
  18. time.sleep(0.1)
  19. def activate(url: str, title: str, width: int, height: int, fullscreen: bool) -> None:
  20. args = url, title, width, height, fullscreen, shutdown
  21. multiprocessing.Process(target=open_window, args=args, daemon=False).start()
  22. Thread(target=check_shutdown, daemon=True).start()