1
0
mirror of https://github.com/Tim-Paik/neutauri.git synced 2024-10-12 23:29:41 +00:00

Update wry to 0.22, fix clippy

This commit is contained in:
2022-11-28 19:47:41 +08:00
parent 915b34cb5d
commit 69531170b9
8 changed files with 856 additions and 395 deletions

View File

@ -6,12 +6,12 @@ version = "0.1.0"
[dependencies]
anyhow = "1.0"
gumdrop = "0.8"
inquire = "0.2"
inquire = "0.5"
neutauri_data = {path = "../neutauri_data", features = ["bundler"]}
new_mime_guess = "4.0"
serde = {version = "1.0", features = ["derive"]}
toml = "0.5"
wry = {version = "0.20", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen", "devtools"]}
wry = {version = "0.22", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen", "devtools"]}
[target.'cfg(windows)'.dependencies]
rcedit = {git = "https://github.com/Tim-Paik/rcedit-rs.git", rev = "2805fca"}

View File

@ -77,7 +77,7 @@ pub(crate) fn bundle(config_path: String) -> anyhow::Result<()> {
None => data::normalize_path(&config.target),
};
fs::create_dir_all(target.parent().unwrap_or_else(|| std::path::Path::new("/")))?;
let target = if target.extension() == None && cfg!(windows) {
let target = if target.extension().is_none() && cfg!(windows) {
target.with_extension("exe")
} else {
target

View File

@ -4,30 +4,18 @@ use std::{fs, io::Read, path::PathBuf};
use wry::{
application::{
dpi::{PhysicalSize, Size},
event::{Event, StartCause, WindowEvent},
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Fullscreen, Icon, Window, WindowBuilder},
},
webview::{WebContext, WebViewBuilder},
};
const PROTOCOL_PREFIX: &str = "{PROTOCOL}://";
const PROTOCOL_PREFIX: &str = "dev://localhost";
const PROTOCOL: &str = "dev";
fn custom_protocol_uri<T: Into<String>>(protocol: T, path: T) -> String {
PROTOCOL_PREFIX.replacen("{PROTOCOL}", &protocol.into(), 1) + &path.into()
}
fn custom_protocol_uri_to_path<T: Into<String>>(protocol: T, uri: T) -> wry::Result<String> {
let prefix = PROTOCOL_PREFIX.replacen("{PROTOCOL}", &protocol.into(), 1);
let uri = uri.into();
let path = uri.strip_prefix(&prefix);
match path {
Some(str) => Ok(str.to_string()),
None => Err(wry::Error::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
prefix + " is not found in " + &uri,
))),
}
fn custom_protocol_uri<T: Into<String>>(path: T) -> String {
PROTOCOL_PREFIX.to_owned() + &path.into()
}
pub(crate) fn dev(config_path: String) -> Result<()> {
@ -93,12 +81,12 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
let webview_builder = match url {
Some(url) => {
if url.starts_with('/') {
webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))?
webview_builder.with_url(&custom_protocol_uri(&url))?
} else {
webview_builder.with_url(&url)?
}
}
None => webview_builder.with_url(&custom_protocol_uri(PROTOCOL, "/index.html"))?,
None => webview_builder.with_url(&custom_protocol_uri("/index.html"))?,
};
let html = config.webview_attr()?.html;
let webview_builder = match html {
@ -153,7 +141,7 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
.with_transparent(config.window_attr()?.transparent)
.with_web_context(&mut web_context)
.with_custom_protocol(PROTOCOL.to_string(), move |request| {
let path = custom_protocol_uri_to_path(PROTOCOL, request.uri())?;
let path = request.uri().path();
let mut local_path = source.clone();
local_path.push(path.strip_prefix('/').unwrap_or(&path));
let mut data = Vec::new();
@ -177,7 +165,10 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
}
}
}
wry::http::ResponseBuilder::new().mimetype(&mime).body(data)
wry::http::Response::builder()
.header("Content-Type", mime)
.body(data)
.map_err(|e| e.into())
})
.with_ipc_handler(|window: &Window, req: String| {
match req.as_str() {
@ -193,11 +184,13 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
*control_flow = ControlFlow::Wait;
match event {
Event::NewEvents(StartCause::Init) => webview.focus(),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::GlobalShortcutEvent(id) => webview
.evaluate_script(&format!("GlobalShortcutEvent({:})", id.0))
.unwrap_or_default(),
_ => (),
}
});