Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update wry to 0.46 #402

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions templates/apps/wry/Cargo.toml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ frameworks = [ "WebKit" ]
[dependencies]
anyhow = "1.0"
log = "0.4"
wry = "0.37"
tao = "0.26"
wry = "0.46"
tao = "0.30"

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13"
Expand Down
100 changes: 28 additions & 72 deletions templates/apps/wry/src/lib.rs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -83,94 +83,50 @@ fn build_webview(event_loop: &EventLoopWindowTarget<()>) -> anyhow::Result<WebVi
let window = WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)?;

let builder = WebViewBuilder::new()
.with_url("https://tauri.app")
// If you want to use custom protocol, set url like this and add files like index.html to assets directory.
// .with_url("wry://assets/index.html")?
.with_devtools(true)
.with_initialization_script("console.log('hello world from init script');")
.with_ipc_handler(|s| {
dbg!(s);
})
.with_custom_protocol("wry".into(), move |_, _request| {
http::Response::builder()
.header(CONTENT_TYPE, "text/html")
.body(
r#"<html>
<body>
Hello Wry!!
</body>
</html>"#
.as_bytes()
.into(),
)
.unwrap_or_default()
});

#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
))]
let builder = WebViewBuilder::new(&window);
let webview = builder.build(&window)?;
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
)))]
let builder = {
let webview = {
use tao::platform::unix::WindowExtUnix;
use wry::WebViewBuilderExtUnix;
let vbox = window.default_vbox().unwrap();
WebViewBuilder::new_gtk(vbox)
builder.build_gtk(vbox)?
};
let webview = builder
.with_url("https://tauri.app")
// If you want to use custom protocol, set url like this and add files like index.html to assets directory.
// .with_url("wry://assets/index.html")?
.with_devtools(true)
.with_initialization_script("console.log('hello world from init script');")
.with_ipc_handler(|s| {
dbg!(s);
})
.with_custom_protocol("wry".into(), move |request| {
match process_custom_protcol(request) {
Ok(r) => r.map(Into::into),
Err(e) => http::Response::builder()
.header(http::header::CONTENT_TYPE, "text/plain")
.status(500)
.body(e.to_string().as_bytes().to_vec())
.unwrap()
.map(Into::into),
}
})
.build()?;

Ok(webview)
}

fn process_custom_protcol(
_request: http::Request<Vec<u8>>,
) -> anyhow::Result<http::Response<Vec<u8>>> {
#[cfg(not(target_os = "android"))]
{
use std::fs::{canonicalize, read};
use wry::http::header::CONTENT_TYPE;

// Remove url scheme
let path = _request.uri().path();

#[cfg(not(target_os = "ios"))]
let content = read(canonicalize(&path[1..])?)?;

#[cfg(target_os = "ios")]
let content = {
let path = core_foundation::bundle::CFBundle::main_bundle()
.resources_path()
.unwrap()
.join(&path);
read(canonicalize(&path)?)?
};

// Return asset contents and mime types based on file extentions
// If you don't want to do this manually, there are some crates for you.
// Such as `infer` and `mime_guess`.
let (data, meta) = if path.ends_with(".html") {
(content, "text/html")
} else if path.ends_with(".js") {
(content, "text/javascript")
} else if path.ends_with(".png") {
(content, "image/png")
} else {
unimplemented!();
};

Ok(http::Response::builder()
.header(CONTENT_TYPE, meta)
.body(data.into())?)
}

#[cfg(target_os = "android")]
{
Ok(http::Response::builder().body(Vec::new().into())?)
}
}
}
Loading