Files
archived-wry-cef/examples/custom_protocol.rs
Ngo Iok Ui (Wu Yu Wei) 783b14239d Refactor new method entry point to rwh (#1041)
* Refactor new method entry point to rwh

* Update doc and add change file

* Fix mac and ios compile error

* Add new_as_child

* Add rhw_04, 05 06 flags

* Update android port

* Remove winit CI

* Fix android bindings

* windows implementation

* Update Cargo.toml

* Fix macOS and iOS

* Fix Android

* fix is_child on WkWebView

* x11 impl

* expose `new_gtk`

* fix winit-gtk version

* remove undecorated resizing handling

* implement set_position and set_size for child gtk in x11

* fix macos

* more fixes

* one more time

* unreachable

* update actions

* fix windows

* some clippy

* Add documentation and fix clippy

* Fix windows clippy error

* Fix android biuld

* Fix documentation test

* Fix android again

* Reduce clippy noise

* use rc?

* refine some documentation, add set_visible

* fix set_visible

* impl set_visible on Windows

* fix doctests

* more set_visible fixes

* fix windows

* unsafe

* fix set size for child webviews

* fix initial visibility with new_gtk

* refine examples

* fix wpgu example

* fix examples on windows and macos

* use a better workaround

* make set_size work on x11

* Fix size in multiwebview example

* Add visible method on macOS and iOS

* remvoe `tao` from android backend and update documentation

* fix winit example

* Add new_as_content_view on macOS

* allow using raw-window-handle v0.5 [skip ci]

* change trait name [skip ci]

* fix linux impl [skip ci]

* fix android build [skip ci]

* fix windows build

* fix(macos): do not autoresize on child webview [skip ci]

* fix macos coordinates [skip ci]

* fix example [skip ci]

* fixed child on macos [skip ci]

* fix docs typos [skip ci]

* fix webview position when it's not a child [skip ci]

* replace new_as_content_view with new_as_subview

* with_as_subview instead of constructor [skip ci]

* fix position/size when as_subview is true

* lint & fmt

* Fix ios build

* Fix cargo test

* Fix mac build

* Update macOS contrusctors

* cargo fmt

* impl drop on Windows

* fix child transparency on Windows (still needs PRs in tao and winit)

* fix winit usage in the examples, use rwh_05 only for now

* fix tests

* add webview.focus

* fix dropping on Linux

* chore clean examples

* implement focus on Linux

* macos focus

---------

Co-authored-by: amrbashir <amr.bashir2015@gmail.com>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
2023-11-07 20:30:35 +09:00

105 lines
2.6 KiB
Rust

// Copyright 2020-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::path::PathBuf;
use http::Request;
use tao::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::{
http::{header::CONTENT_TYPE, Response},
WebViewBuilder,
};
fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
))]
let builder = WebViewBuilder::new(&window);
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
)))]
let builder = {
use tao::platform::unix::WindowExtUnix;
let vbox = window.default_vbox().unwrap();
WebViewBuilder::new_gtk(vbox)
};
let _webview = builder
.with_custom_protocol("wry".into(), move |request| {
match get_wry_response(request) {
Ok(r) => r.map(Into::into),
Err(e) => http::Response::builder()
.header(CONTENT_TYPE, "text/plain")
.status(500)
.body(e.to_string().as_bytes().to_vec())
.unwrap()
.map(Into::into),
}
})
// tell the webview to load the custom protocol
.with_url("wry://localhost")?
.build()?;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
if let Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} = event
{
*control_flow = ControlFlow::Exit
}
});
}
fn get_wry_response(
request: Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
let path = request.uri().path();
// Read the file content from file path
let root = PathBuf::from("examples/custom_protocol");
let path = if path == "/" {
"index.html"
} else {
// removing leading slash
&path[1..]
};
let content = std::fs::read(std::fs::canonicalize(root.join(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 mimetype = if path.ends_with(".html") || path == "/" {
"text/html"
} else if path.ends_with(".js") {
"text/javascript"
} else if path.ends_with(".png") {
"image/png"
} else if path.ends_with(".wasm") {
"application/wasm"
} else {
unimplemented!();
};
Response::builder()
.header(CONTENT_TYPE, mimetype)
.body(content)
.map_err(Into::into)
}