소스 검색

Merge pull request #1341 from Smug246/main

added frameless parameter to native mode, also changed NameError to AttributeError
Rodja Trappe 1 년 전
부모
커밋
85188056c9
2개의 변경된 파일29개의 추가작업 그리고 18개의 파일을 삭제
  1. 24 17
      nicegui/native_mode.py
  2. 5 1
      nicegui/run.py

+ 24 - 17
nicegui/native_mode.py

@@ -1,3 +1,5 @@
+from __future__ import annotations
+
 import _thread
 import logging
 import multiprocessing as mp
@@ -17,33 +19,33 @@ try:
         # webview depends on bottle which uses the deprecated CGI function (https://github.com/bottlepy/bottle/issues/1403)
         warnings.filterwarnings('ignore', category=DeprecationWarning)
         import webview
+    globals.optional_features.add('native')
 except ModuleNotFoundError:
-    class webview:
-        class Window:
-            pass
     pass
 
 
 def open_window(
-    host: str, port: int, title: str, width: int, height: int, fullscreen: bool,
+    host: str, port: int, title: str, width: int, height: int, fullscreen: bool, frameless: bool,
     method_queue: mp.Queue, response_queue: mp.Queue,
 ) -> None:
     while not helpers.is_port_open(host, port):
         time.sleep(0.1)
 
-    window_kwargs = dict(url=f'http://{host}:{port}', title=title, width=width, height=height, fullscreen=fullscreen)
+    window_kwargs = dict(
+        url=f'http://{host}:{port}',
+        title=title,
+        width=width,
+        height=height,
+        fullscreen=fullscreen,
+        frameless=frameless,
+    )
     window_kwargs.update(globals.app.native.window_args)
 
-    try:
-        window = webview.create_window(**window_kwargs)
-        closing = Event()
-        window.events.closing += closing.set
-        start_window_method_executor(window, method_queue, response_queue, closing)
-        webview.start(storage_path=tempfile.mkdtemp(), **globals.app.native.start_args)
-    except NameError:
-        logging.error('''Native mode is not supported in this configuration.
-Please run "pip install pywebview" to use it.''')
-        sys.exit(1)
+    window = webview.create_window(**window_kwargs)
+    closing = Event()
+    window.events.closing += closing.set
+    start_window_method_executor(window, method_queue, response_queue, closing)
+    webview.start(storage_path=tempfile.mkdtemp(), **globals.app.native.start_args)
 
 
 def start_window_method_executor(
@@ -90,7 +92,7 @@ def start_window_method_executor(
     Thread(target=window_method_executor).start()
 
 
-def activate(host: str, port: int, title: str, width: int, height: int, fullscreen: bool) -> None:
+def activate(host: str, port: int, title: str, width: int, height: int, fullscreen: bool, frameless: bool) -> None:
     def check_shutdown() -> None:
         while process.is_alive():
             time.sleep(0.1)
@@ -99,8 +101,13 @@ def activate(host: str, port: int, title: str, width: int, height: int, fullscre
             time.sleep(0.1)
         _thread.interrupt_main()
 
+    if 'native' not in globals.optional_features:
+        logging.error('Native mode is not supported in this configuration.\n'
+                      'Please run "pip install pywebview" to use it.')
+        sys.exit(1)
+
     mp.freeze_support()
-    args = host, port, title, width, height, fullscreen, native.method_queue, native.response_queue
+    args = host, port, title, width, height, fullscreen, frameless, native.method_queue, native.response_queue
     process = mp.Process(target=open_window, args=args, daemon=False)
     process.start()
 

+ 5 - 1
nicegui/run.py

@@ -47,6 +47,7 @@ def run(*,
         native: bool = False,
         window_size: Optional[Tuple[int, int]] = None,
         fullscreen: bool = False,
+        frameless: bool = False,
         reload: bool = True,
         uvicorn_logging_level: str = 'warning',
         uvicorn_reload_dirs: str = '.',
@@ -75,6 +76,7 @@ def run(*,
     :param native: open the UI in a native window of size 800x600 (default: `False`, deactivates `show`, automatically finds an open port)
     :param window_size: open the UI in a native window with the provided size (e.g. `(1024, 786)`, default: `None`, also activates `native`)
     :param fullscreen: open the UI in a fullscreen window (default: `False`, also activates `native`)
+    :param frameless: open the UI in a frameless window (default: `False`, also activates `native`)
     :param reload: automatically reload the UI on file changes (default: `True`)
     :param uvicorn_logging_level: logging level for uvicorn server (default: `'warning'`)
     :param uvicorn_reload_dirs: string with comma-separated list for directories to be monitored (default is current working directory only)
@@ -116,6 +118,8 @@ def run(*,
 
     if fullscreen:
         native = True
+    if frameless:
+        native = True
     if window_size:
         native = True
     if native:
@@ -123,7 +127,7 @@ def run(*,
         host = host or '127.0.0.1'
         port = native_mode.find_open_port()
         width, height = window_size or (800, 600)
-        native_mode.activate(host, port, title, width, height, fullscreen)
+        native_mode.activate(host, port, title, width, height, fullscreen, frameless)
     else:
         host = host or '0.0.0.0'