|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 | // SPDX-License-Identifier: MIT
|
4 | 4 |
|
5 |
| -use std::path::PathBuf; |
6 |
| - |
7 |
| -use tao::{ |
8 |
| - event::{Event, WindowEvent}, |
9 |
| - event_loop::{ControlFlow, EventLoop}, |
10 |
| - window::WindowBuilder, |
11 |
| -}; |
12 |
| -use wry::{ |
13 |
| - http::{header::CONTENT_TYPE, Request, Response}, |
14 |
| - WebViewBuilder, |
15 |
| -}; |
16 |
| - |
17 | 5 | fn main() -> wry::Result<()> {
|
18 |
| - let event_loop = EventLoop::new(); |
19 |
| - let window = WindowBuilder::new().build(&event_loop).unwrap(); |
| 6 | + imp::main() |
| 7 | +} |
20 | 8 |
|
21 |
| - let builder = WebViewBuilder::new() |
22 |
| - .with_asynchronous_custom_protocol("wry".into(), move |_webview_id, request, responder| { |
23 |
| - match get_wry_response(request) { |
24 |
| - Ok(http_response) => responder.respond(http_response), |
25 |
| - Err(e) => responder.respond( |
26 |
| - http::Response::builder() |
27 |
| - .header(CONTENT_TYPE, "text/plain") |
28 |
| - .status(500) |
29 |
| - .body(e.to_string().as_bytes().to_vec()) |
30 |
| - .unwrap(), |
31 |
| - ), |
32 |
| - } |
33 |
| - }) |
34 |
| - // tell the webview to load the custom protocol |
35 |
| - .with_url("wry://localhost"); |
| 9 | +#[cfg(not(feature = "protocol"))] |
| 10 | +mod imp { |
| 11 | + pub fn main() -> wry::Result<()> { |
| 12 | + unimplemented!() |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +#[cfg(feature = "protocol")] |
| 17 | +mod imp { |
| 18 | + use std::path::PathBuf; |
36 | 19 |
|
37 |
| - #[cfg(any( |
38 |
| - target_os = "windows", |
39 |
| - target_os = "macos", |
40 |
| - target_os = "ios", |
41 |
| - target_os = "android" |
42 |
| - ))] |
43 |
| - let _webview = builder.build(&window)?; |
44 |
| - #[cfg(not(any( |
45 |
| - target_os = "windows", |
46 |
| - target_os = "macos", |
47 |
| - target_os = "ios", |
48 |
| - target_os = "android" |
49 |
| - )))] |
50 |
| - let _webview = { |
51 |
| - use tao::platform::unix::WindowExtUnix; |
52 |
| - use wry::WebViewBuilderExtUnix; |
53 |
| - let vbox = window.default_vbox().unwrap(); |
54 |
| - builder.build_gtk(vbox)? |
| 20 | + use tao::{ |
| 21 | + event::{Event, WindowEvent}, |
| 22 | + event_loop::{ControlFlow, EventLoop}, |
| 23 | + window::WindowBuilder, |
| 24 | + }; |
| 25 | + use wry::{ |
| 26 | + http::{header::CONTENT_TYPE, Request, Response}, |
| 27 | + WebViewBuilder, |
55 | 28 | };
|
56 | 29 |
|
57 |
| - event_loop.run(move |event, _, control_flow| { |
58 |
| - *control_flow = ControlFlow::Wait; |
| 30 | + pub fn main() -> wry::Result<()> { |
| 31 | + let event_loop = EventLoop::new(); |
| 32 | + let window = WindowBuilder::new().build(&event_loop).unwrap(); |
59 | 33 |
|
60 |
| - if let Event::WindowEvent { |
61 |
| - event: WindowEvent::CloseRequested, |
62 |
| - .. |
63 |
| - } = event |
64 |
| - { |
65 |
| - *control_flow = ControlFlow::Exit |
66 |
| - } |
67 |
| - }); |
68 |
| -} |
| 34 | + let builder = WebViewBuilder::new() |
| 35 | + .with_asynchronous_custom_protocol("wry".into(), move |_webview_id, request, responder| { |
| 36 | + match get_wry_response(request) { |
| 37 | + Ok(http_response) => responder.respond(http_response), |
| 38 | + Err(e) => responder.respond( |
| 39 | + http::Response::builder() |
| 40 | + .header(CONTENT_TYPE, "text/plain") |
| 41 | + .status(500) |
| 42 | + .body(e.to_string().as_bytes().to_vec()) |
| 43 | + .unwrap(), |
| 44 | + ), |
| 45 | + } |
| 46 | + }) |
| 47 | + // tell the webview to load the custom protocol |
| 48 | + .with_url("wry://localhost"); |
69 | 49 |
|
70 |
| -fn get_wry_response( |
71 |
| - request: Request<Vec<u8>>, |
72 |
| -) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> { |
73 |
| - let path = request.uri().path(); |
74 |
| - // Read the file content from file path |
75 |
| - let root = PathBuf::from("examples/custom_protocol"); |
76 |
| - let path = if path == "/" { |
77 |
| - "index.html" |
78 |
| - } else { |
79 |
| - // removing leading slash |
80 |
| - &path[1..] |
81 |
| - }; |
82 |
| - let content = std::fs::read(std::fs::canonicalize(root.join(path))?)?; |
| 50 | + #[cfg(any( |
| 51 | + target_os = "windows", |
| 52 | + target_os = "macos", |
| 53 | + target_os = "ios", |
| 54 | + target_os = "android" |
| 55 | + ))] |
| 56 | + let _webview = builder.build(&window)?; |
| 57 | + #[cfg(not(any( |
| 58 | + target_os = "windows", |
| 59 | + target_os = "macos", |
| 60 | + target_os = "ios", |
| 61 | + target_os = "android" |
| 62 | + )))] |
| 63 | + let _webview = { |
| 64 | + use tao::platform::unix::WindowExtUnix; |
| 65 | + use wry::WebViewBuilderExtUnix; |
| 66 | + let vbox = window.default_vbox().unwrap(); |
| 67 | + builder.build_gtk(vbox)? |
| 68 | + }; |
83 | 69 |
|
84 |
| - // Return asset contents and mime types based on file extentions |
85 |
| - // If you don't want to do this manually, there are some crates for you. |
86 |
| - // Such as `infer` and `mime_guess`. |
87 |
| - let mimetype = if path.ends_with(".html") || path == "/" { |
88 |
| - "text/html" |
89 |
| - } else if path.ends_with(".js") { |
90 |
| - "text/javascript" |
91 |
| - } else if path.ends_with(".png") { |
92 |
| - "image/png" |
93 |
| - } else if path.ends_with(".wasm") { |
94 |
| - "application/wasm" |
95 |
| - } else { |
96 |
| - unimplemented!(); |
97 |
| - }; |
| 70 | + event_loop.run(move |event, _, control_flow| { |
| 71 | + *control_flow = ControlFlow::Wait; |
| 72 | + |
| 73 | + if let Event::WindowEvent { |
| 74 | + event: WindowEvent::CloseRequested, |
| 75 | + .. |
| 76 | + } = event |
| 77 | + { |
| 78 | + *control_flow = ControlFlow::Exit |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + fn get_wry_response( |
| 84 | + request: Request<Vec<u8>>, |
| 85 | + ) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> { |
| 86 | + let path = request.uri().path(); |
| 87 | + // Read the file content from file path |
| 88 | + let root = PathBuf::from("examples/custom_protocol"); |
| 89 | + let path = if path == "/" { |
| 90 | + "index.html" |
| 91 | + } else { |
| 92 | + // removing leading slash |
| 93 | + &path[1..] |
| 94 | + }; |
| 95 | + let content = std::fs::read(std::fs::canonicalize(root.join(path))?)?; |
| 96 | + |
| 97 | + // Return asset contents and mime types based on file extentions |
| 98 | + // If you don't want to do this manually, there are some crates for you. |
| 99 | + // Such as `infer` and `mime_guess`. |
| 100 | + let mimetype = if path.ends_with(".html") || path == "/" { |
| 101 | + "text/html" |
| 102 | + } else if path.ends_with(".js") { |
| 103 | + "text/javascript" |
| 104 | + } else if path.ends_with(".png") { |
| 105 | + "image/png" |
| 106 | + } else if path.ends_with(".wasm") { |
| 107 | + "application/wasm" |
| 108 | + } else { |
| 109 | + unimplemented!(); |
| 110 | + }; |
98 | 111 |
|
99 |
| - Response::builder() |
100 |
| - .header(CONTENT_TYPE, mimetype) |
101 |
| - .body(content) |
102 |
| - .map_err(Into::into) |
| 112 | + Response::builder() |
| 113 | + .header(CONTENT_TYPE, mimetype) |
| 114 | + .body(content) |
| 115 | + .map_err(Into::into) |
| 116 | + } |
103 | 117 | }
|
0 commit comments