-
-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Custom protcol request/response (#387)
* refactor: Custom protcol request/response Signed-off-by: David Lemarier <[email protected]> * fix tests Signed-off-by: David Lemarier <[email protected]> * make clippy happy Signed-off-by: David Lemarier <[email protected]> * implement windows Signed-off-by: David Lemarier <[email protected]> * WIP linux implementation Signed-off-by: David Lemarier <[email protected]> * add request method for windows Signed-off-by: David Lemarier <[email protected]> * fix examples for windows Signed-off-by: David Lemarier <[email protected]> * better doc and add form post handler Signed-off-by: David Lemarier <[email protected]> * implement body on request for windows and fix fmt Signed-off-by: David Lemarier <[email protected]> * fix test and fmt Signed-off-by: David Lemarier <[email protected]> * remove `#[non_exhaustive]` Signed-off-by: David Lemarier <[email protected]> * make mimetype optional and finalize tauri integration Signed-off-by: David Lemarier <[email protected]> * use a string for URI as it doesnt support `file:///` Signed-off-by: David Lemarier <[email protected]> * fmt Signed-off-by: David Lemarier <[email protected]> * fix examples Signed-off-by: David Lemarier <[email protected]> * convert mimetype to `&str` Signed-off-by: David Lemarier <[email protected]> * make clippy happy Signed-off-by: David Lemarier <[email protected]> * remove unwanted logging Signed-off-by: David Lemarier <[email protected]>
- Loading branch information
Showing
21 changed files
with
997 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
"wry": patch | ||
--- | ||
|
||
The custom protocol now return a `Request` and expect a `Response`. | ||
|
||
- This allow us to get the complete request from the Webview. (Method, GET, POST, PUT etc..) | ||
Read the complete header. | ||
|
||
- And allow us to be more flexible in the future without bringing breaking changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ target | |
Cargo.lock | ||
gh-pages | ||
.DS_Store | ||
examples/test_video.mp4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body> | ||
<h1>Welcome to WRY!</h1> | ||
<form action="#" method="POST"> | ||
<label for="fname">First name:</label><br /> | ||
<input type="text" id="fname" name="fname" value="John" /><br /> | ||
<label for="lname">Last name:</label><br /> | ||
<input type="text" id="lname" name="lname" value="Doe" /><br /><br /> | ||
<input type="submit" value="Submit" /> | ||
</form> | ||
<p> | ||
If you click the "Submit" button, the form-data will be sent to the custom | ||
protocol. | ||
</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
fn main() -> wry::Result<()> { | ||
use std::fs::{canonicalize, read}; | ||
|
||
use wry::{ | ||
application::{ | ||
event::{Event, StartCause, WindowEvent}, | ||
event_loop::{ControlFlow, EventLoop}, | ||
window::WindowBuilder, | ||
}, | ||
http::{method::Method, ResponseBuilder}, | ||
webview::WebViewBuilder, | ||
}; | ||
|
||
let event_loop = EventLoop::new(); | ||
let window = WindowBuilder::new() | ||
.with_title("Hello World") | ||
.build(&event_loop) | ||
.unwrap(); | ||
|
||
let _webview = WebViewBuilder::new(window) | ||
.unwrap() | ||
.with_custom_protocol("wry".into(), move |request| { | ||
if request.method() == Method::POST { | ||
let body_string = String::from_utf8_lossy(request.body()); | ||
for body in body_string.split('&') { | ||
println!("Value sent; {:?}", body); | ||
} | ||
} | ||
// Remove url scheme | ||
let path = request.uri().replace("wry://", ""); | ||
ResponseBuilder::new() | ||
.mimetype("text/html") | ||
.body(read(canonicalize(&path)?)?) | ||
}) | ||
// tell the webview to load the custom protocol | ||
.with_url("wry://examples/form.html")? | ||
.build()?; | ||
|
||
event_loop.run(move |event, _, control_flow| { | ||
*control_flow = ControlFlow::Wait; | ||
|
||
match event { | ||
Event::NewEvents(StartCause::Init) => println!("Wry application started!"), | ||
Event::WindowEvent { | ||
event: WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = ControlFlow::Exit, | ||
_ => (), | ||
} | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body> | ||
<video | ||
id="video_source" | ||
style="width: 90vw; height: 90vh" | ||
controls="" | ||
autoplay="" | ||
name="media" | ||
> | ||
<source src="wry://examples/test_video.mp4" type="video/mp4" /> | ||
</video> | ||
|
||
<script> | ||
(function () { | ||
if (navigator.userAgent.includes("Windows")) { | ||
const video = document.getElementById("video_source"); | ||
const sources = video.getElementsByTagName("source"); | ||
sources[0].src = "https://wry.examples/test_video.mp4"; | ||
video.load(); | ||
} | ||
})(); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.