Merge pull request #662 from Demonthos/rootname-desktop

allow changing the root name for desktop
This commit is contained in:
Jon Kelley 2022-12-18 21:06:43 -08:00 committed by GitHub
commit fcdd142c7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 deletions

View file

@ -21,6 +21,7 @@ pub struct Config {
pub(crate) resource_dir: Option<PathBuf>,
pub(crate) custom_head: Option<String>,
pub(crate) custom_index: Option<String>,
pub(crate) root_name: String,
}
type DropHandler = Box<dyn Fn(&Window, FileDropEvent) -> bool>;
@ -46,6 +47,7 @@ impl Config {
resource_dir: None,
custom_head: None,
custom_index: None,
root_name: "main".to_string(),
}
}
@ -126,6 +128,14 @@ impl Config {
self.custom_index = Some(index);
self
}
/// Set the name of the element that Dioxus will use as the root.
///
/// This is akint to calling React.render() on the element with the specified name.
pub fn with_root_name(mut self, name: impl Into<String>) -> Self {
self.root_name = name.into();
self
}
}
impl Default for Config {

View file

@ -164,6 +164,7 @@ fn build_webview(
let custom_head = cfg.custom_head.clone();
let resource_dir = cfg.resource_dir.clone();
let index_file = cfg.custom_index.clone();
let root_name = cfg.root_name.clone();
// We assume that if the icon is None in cfg, then the user just didnt set it
if cfg.window.window.window_icon.is_none() {
@ -220,6 +221,7 @@ fn build_webview(
resource_dir.clone(),
custom_head.clone(),
index_file.clone(),
&root_name,
)
})
.with_file_drop_handler(move |window, evet| {

View file

@ -4,19 +4,25 @@ use wry::{
Result,
};
const MODULE_LOADER: &str = r#"
fn module_loader(root_name: &str) -> String {
format!(
"
<script>
import("./index.js").then(function (module) {
module.main();
});
import(\"./index.js\").then(function (module) {{
module.main(\"{}\");
}});
</script>
"#;
",
root_name
)
}
pub(super) fn desktop_handler(
request: &Request<Vec<u8>>,
asset_root: Option<PathBuf>,
custom_head: Option<String>,
custom_index: Option<String>,
root_name: &str,
) -> Result<Response<Vec<u8>>> {
// Any content that uses the `dioxus://` scheme will be shuttled through this handler as a "special case".
// For now, we only serve two pieces of content which get included as bytes into the final binary.
@ -30,7 +36,7 @@ pub(super) fn desktop_handler(
// we'll look for the closing </body> tag and insert our little module loader there.
if let Some(custom_index) = custom_index {
let rendered = custom_index
.replace("</body>", &format!("{}</body>", MODULE_LOADER))
.replace("</body>", &format!("{}</body>", module_loader(root_name)))
.into_bytes();
Response::builder()
.header("Content-Type", "text/html")
@ -42,7 +48,7 @@ pub(super) fn desktop_handler(
if let Some(custom_head) = custom_head {
template = template.replace("<!-- CUSTOM HEAD -->", &custom_head);
}
template = template.replace("<!-- MODULE LOADER -->", MODULE_LOADER);
template = template.replace("<!-- MODULE LOADER -->", &module_loader(root_name));
Response::builder()
.header("Content-Type", "text/html")

View file

@ -1,5 +1,5 @@
export function main() {
let root = window.document.getElementById("main");
export function main(rootname = "main") {
let root = window.document.getElementById(rootname);
if (root != null) {
window.interpreter = new Interpreter(root);
window.ipc.postMessage(serializeIpcMessage("initialize"));