Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
huangmingg committed Feb 7, 2024
1 parent 7066f97 commit 22c0674
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3246,38 +3246,35 @@ fn create_webview<T: UserEvent>(
if window_builder.center {
let _ = center_window(&window, window.inner_size());
}

let url_str = url.to_string();

let mut webview_builder = WebViewBuilder::new(window)
.map_err(|e| Error::CreateWebview(Box::new(e)))?;
let mut webview_builder =
WebViewBuilder::new(window).map_err(|e| Error::CreateWebview(Box::new(e)))?;

// use with_html method if html content can be extracted from url.
// else defaults to with_url method
webview_builder = if let Some(html_string) = tauri_utils::html::extract_html_content(&url_str) {
// we run a decode here on the html_string for sanity check
// the content should be fully decoded when passing to WRY, which
// will be responsible for handling the encoding based on the OS.
let html =
urlencoding::decode(html_string).map_err(|e| Error::CreateWebview(Box::new(e)))?;


webview_builder = match (url.scheme(), tauri_utils::html::is_html(&url_str)) {
// only use with_html method if it is a data url, and is a html.
("data", true) => {
let html = tauri_utils::html::extract_html_content(&url_str)
.map(|s| urlencoding::decode(s))
.unwrap() // safe to unwrap because we validate the URL beforehand
.unwrap(); // safe to unwrap because we validate the URL beforehand

webview_builder
webview_builder
.with_html(html)
.unwrap() // safe to unwrap because we validate the URL beforehand
}

// defaults to with_url method
_ => {
webview_builder
.with_url(&url_str)
.unwrap() // safe to unwrap because we validate the URL beforehand
}
};

webview_builder = webview_builder
.map_err(|e| Error::CreateWebview(Box::new(e)))?
} else {
webview_builder
.with_url(&url_str)
.map_err(|e| Error::CreateWebview(Box::new(e)))?
};

webview_builder = webview_builder
.with_focused(focused)
.with_transparent(is_window_transparent)
.with_accept_first_mouse(webview_attributes.accept_first_mouse);

if webview_attributes.file_drop_handler_enabled {
webview_builder = webview_builder
.with_file_drop_handler(create_file_drop_handler(window_event_listeners.clone()));
Expand Down

0 comments on commit 22c0674

Please sign in to comment.