Skip to content

Commit

Permalink
macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Dec 27, 2022
1 parent a697ced commit 3e155d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
7 changes: 6 additions & 1 deletion examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ fn main() -> wry::Result<()> {
let mut webview = Some(
WebViewBuilder::new(window)
.unwrap()
.with_html(HTML)?
.with_url("https://03d6099e8c.to.intercept.rest/")
.unwrap()
.with_headers(http::HeaderMap::from_iter([(
http::header::HeaderName::from_static("customHeader"),
http::HeaderValue::from_static("Holla from customHeader"),
)]))
.with_ipc_handler(handler)
.with_accept_first_mouse(true)
.build()?,
Expand Down
23 changes: 19 additions & 4 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ r#"Object.defineProperty(window, 'ipc', {
w.navigate_to_string(path);
}
} else {
w.navigate_to_url(url.as_str());
w.navigate_to_url(url.as_str(), attributes.headers);
}
} else if let Some(html) = attributes.html {
w.navigate_to_string(&html);
Expand Down Expand Up @@ -816,14 +816,25 @@ r#"Object.defineProperty(window, 'ipc', {
}

pub fn load_url(&self, url: &str) {
self.navigate_to_url(url)
self.navigate_to_url(url, None)
}

fn navigate_to_url(&self, url: &str) {
pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
self.navigate_to_url(url, Some(headers))
}

fn navigate_to_url(&self, url: &str, headers: Option<http::HeaderMap>) {
// Safety: objc runtime calls are unsafe
unsafe {
let url: id = msg_send![class!(NSURL), URLWithString: NSString::new(url)];
let request: id = msg_send![class!(NSURLRequest), requestWithURL: url];
let request: id = msg_send![class!(NSMutableURLRequest), requestWithURL: url];
if let Some(headers) = headers {
for (name, value) in headers.iter() {
let key = NSString::new(name.as_str());
let value = NSString::new(value.to_str().unwrap_or_default());
let _: () = msg_send![request, addValue:key.as_ptr() forHTTPHeaderField:value.as_ptr()];
}
}
let () = msg_send![self.webview, loadRequest: request];
}
}
Expand Down Expand Up @@ -998,4 +1009,8 @@ impl NSString {
str::from_utf8_unchecked(bytes)
}
}

fn as_ptr(&self) -> id {
self.0
}
}

0 comments on commit 3e155d7

Please sign in to comment.