fix headless windows

This commit is contained in:
Evan Almloff committed 2023-12-08 15:14:32 -06:00
1 parent 0e3fd225a8
commit 8984482b63
4 files changed
+18 -9

No files matched your search

+10 -4
View File
@@ -51,15 +51,21 @@ fn main() {
}"#; }"#;
let polling_request = format!( let polling_request = format!(
r#"// Poll for requests r#"// Poll for requests
window.interpreter.wait_for_request = () => {{ window.interpreter.wait_for_request = (headless) => {{
fetch(new Request("{EDITS_PATH}")) fetch(new Request("{EDITS_PATH}"))
.then(response => {{ .then(response => {{
response.arrayBuffer() response.arrayBuffer()
.then(bytes => {{ .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); run_from_bytes(bytes);
}}); }}
window.interpreter.wait_for_request(); else {{
requestAnimationFrame(() => {{
run_from_bytes(bytes);
}});
}}
window.interpreter.wait_for_request(headless);
}}); }});
}}) }})
}}"# }}"#
+1 -1
View File
@@ -17,7 +17,7 @@ pub(crate) fn check_app_exits(app: Component) {
dioxus_desktop::launch_cfg( dioxus_desktop::launch_cfg(
app, app,
Config::new().with_window(WindowBuilder::new().with_visible(true)), Config::new().with_window(WindowBuilder::new().with_visible(false)),
); );
// Stop deadman's switch // Stop deadman's switch
+5 -4
View File
@@ -12,7 +12,7 @@ use crate::desktop_context::EditQueue;
static MINIFIED: &str = include_str!("./minified.js"); static MINIFIED: &str = include_str!("./minified.js");
fn module_loader(root_name: &str) -> String { fn module_loader(root_name: &str, headless: bool) -> String {
format!( format!(
r#" r#"
<script type="module"> <script type="module">
@@ -25,7 +25,7 @@ fn module_loader(root_name: &str) -> String {
window.interpreter.initialize(root_element); window.interpreter.initialize(root_element);
window.ipc.postMessage(window.interpreter.serializeIpcMessage("initialize")); window.ipc.postMessage(window.interpreter.serializeIpcMessage("initialize"));
}} }}
window.interpreter.wait_for_request(); window.interpreter.wait_for_request({headless});
}} }}
</script> </script>
"# "#
@@ -39,6 +39,7 @@ pub(super) fn desktop_handler(
custom_index: Option<String>, custom_index: Option<String>,
root_name: &str, root_name: &str,
edit_queue: &EditQueue, edit_queue: &EditQueue,
headless: bool,
) { ) {
// If the request is for the root, we'll serve the index.html file. // If the request is for the root, we'll serve the index.html file.
if request.uri().path() == "/" { 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. // we'll look for the closing </body> tag and insert our little module loader there.
let body = match custom_index { let body = match custom_index {
Some(custom_index) => 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(), .into_bytes(),
None => { None => {
@@ -58,7 +59,7 @@ pub(super) fn desktop_handler(
} }
template template
.replace("<!-- MODULE LOADER -->", &module_loader(root_name)) .replace("<!-- MODULE LOADER -->", &module_loader(root_name, headless))
.into_bytes() .into_bytes()
} }
}; };
+2
View File
@@ -33,6 +33,7 @@ pub(crate) fn build(
let mut web_context = WebContext::new(cfg.data_dir.clone()); let mut web_context = WebContext::new(cfg.data_dir.clone());
let edit_queue = EditQueue::default(); let edit_queue = EditQueue::default();
let headless = !cfg.window.window.visible;
let mut webview = WebViewBuilder::new(window) let mut webview = WebViewBuilder::new(window)
.unwrap() .unwrap()
@@ -55,6 +56,7 @@ pub(crate) fn build(
index_file.clone(), index_file.clone(),
&root_name, &root_name,
&edit_queue, &edit_queue,
headless
) )
} }
}) })