Browse Source

fix headless windows

Evan Almloff 1 năm trước cách đây
mục cha
commit
8984482b63

+ 10 - 4
packages/desktop/build.rs

@@ -51,15 +51,21 @@ fn main() {
     }"#;
     let polling_request = format!(
         r#"// Poll for requests
-    window.interpreter.wait_for_request = () => {{
+    window.interpreter.wait_for_request = (headless) => {{
       fetch(new Request("{EDITS_PATH}"))
           .then(response => {{
               response.arrayBuffer()
                   .then(bytes => {{
-                      requestAnimationFrame(() => {{
+                      // In headless mode, the requestAnimationFrame callback is never called, so we need to run the bytes directly
+                      if (headless) {{
                         run_from_bytes(bytes);
-                      }});
-                      window.interpreter.wait_for_request();
+                      }}
+                      else {{
+                        requestAnimationFrame(() => {{
+                          run_from_bytes(bytes);
+                        }});
+                      }}
+                      window.interpreter.wait_for_request(headless);
                   }});
           }})
     }}"#

+ 1 - 1
packages/desktop/headless_tests/events.rs

@@ -17,7 +17,7 @@ pub(crate) fn check_app_exits(app: Component) {
 
     dioxus_desktop::launch_cfg(
         app,
-        Config::new().with_window(WindowBuilder::new().with_visible(true)),
+        Config::new().with_window(WindowBuilder::new().with_visible(false)),
     );
 
     // Stop deadman's switch

+ 5 - 4
packages/desktop/src/protocol.rs

@@ -12,7 +12,7 @@ use crate::desktop_context::EditQueue;
 
 static MINIFIED: &str = include_str!("./minified.js");
 
-fn module_loader(root_name: &str) -> String {
+fn module_loader(root_name: &str, headless: bool) -> String {
     format!(
         r#"
 <script type="module">
@@ -25,7 +25,7 @@ fn module_loader(root_name: &str) -> String {
             window.interpreter.initialize(root_element);
             window.ipc.postMessage(window.interpreter.serializeIpcMessage("initialize"));
         }}
-        window.interpreter.wait_for_request();
+        window.interpreter.wait_for_request({headless});
     }}
 </script>
 "#
@@ -39,6 +39,7 @@ pub(super) fn desktop_handler(
     custom_index: Option<String>,
     root_name: &str,
     edit_queue: &EditQueue,
+    headless: bool,
 ) {
     // If the request is for the root, we'll serve the index.html file.
     if request.uri().path() == "/" {
@@ -46,7 +47,7 @@ pub(super) fn desktop_handler(
         // we'll look for the closing </body> tag and insert our little module loader there.
         let body = match custom_index {
             Some(custom_index) => custom_index
-                .replace("</body>", &format!("{}</body>", module_loader(root_name)))
+                .replace("</body>", &format!("{}</body>", module_loader(root_name, headless)))
                 .into_bytes(),
 
             None => {
@@ -58,7 +59,7 @@ pub(super) fn desktop_handler(
                 }
 
                 template
-                    .replace("<!-- MODULE LOADER -->", &module_loader(root_name))
+                    .replace("<!-- MODULE LOADER -->", &module_loader(root_name, headless))
                     .into_bytes()
             }
         };

+ 2 - 0
packages/desktop/src/webview.rs

@@ -33,6 +33,7 @@ pub(crate) fn build(
 
     let mut web_context = WebContext::new(cfg.data_dir.clone());
     let edit_queue = EditQueue::default();
+    let headless = !cfg.window.window.visible;
 
     let mut webview = WebViewBuilder::new(window)
         .unwrap()
@@ -55,6 +56,7 @@ pub(crate) fn build(
                     index_file.clone(),
                     &root_name,
                     &edit_queue,
+                    headless
                 )
             }
         })