Update wry to 0.22, fix clippy

This commit is contained in:
Tim-Paik 2022-11-28 19:47:41 +08:00
parent 915b34cb5d
commit 69531170b9
Signed by: Tim-Paik
GPG Key ID: DC36A050DB42566D
8 changed files with 856 additions and 395 deletions

1163
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,12 +6,12 @@ version = "0.1.0"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
gumdrop = "0.8" gumdrop = "0.8"
inquire = "0.2" inquire = "0.5"
neutauri_data = {path = "../neutauri_data", features = ["bundler"]} neutauri_data = {path = "../neutauri_data", features = ["bundler"]}
new_mime_guess = "4.0" new_mime_guess = "4.0"
serde = {version = "1.0", features = ["derive"]} serde = {version = "1.0", features = ["derive"]}
toml = "0.5" 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] [target.'cfg(windows)'.dependencies]
rcedit = {git = "https://github.com/Tim-Paik/rcedit-rs.git", rev = "2805fca"} 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), None => data::normalize_path(&config.target),
}; };
fs::create_dir_all(target.parent().unwrap_or_else(|| std::path::Path::new("/")))?; 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") target.with_extension("exe")
} else { } else {
target target

View File

@ -4,30 +4,18 @@ use std::{fs, io::Read, path::PathBuf};
use wry::{ use wry::{
application::{ application::{
dpi::{PhysicalSize, Size}, dpi::{PhysicalSize, Size},
event::{Event, StartCause, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::{Fullscreen, Icon, Window, WindowBuilder}, window::{Fullscreen, Icon, Window, WindowBuilder},
}, },
webview::{WebContext, WebViewBuilder}, webview::{WebContext, WebViewBuilder},
}; };
const PROTOCOL_PREFIX: &str = "{PROTOCOL}://"; const PROTOCOL_PREFIX: &str = "dev://localhost";
const PROTOCOL: &str = "dev"; const PROTOCOL: &str = "dev";
fn custom_protocol_uri<T: Into<String>>(protocol: T, path: T) -> String { fn custom_protocol_uri<T: Into<String>>(path: T) -> String {
PROTOCOL_PREFIX.replacen("{PROTOCOL}", &protocol.into(), 1) + &path.into() PROTOCOL_PREFIX.to_owned() + &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,
))),
}
} }
pub(crate) fn dev(config_path: String) -> Result<()> { 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 { let webview_builder = match url {
Some(url) => { Some(url) => {
if url.starts_with('/') { if url.starts_with('/') {
webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))? webview_builder.with_url(&custom_protocol_uri(&url))?
} else { } else {
webview_builder.with_url(&url)? 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 html = config.webview_attr()?.html;
let webview_builder = match 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_transparent(config.window_attr()?.transparent)
.with_web_context(&mut web_context) .with_web_context(&mut web_context)
.with_custom_protocol(PROTOCOL.to_string(), move |request| { .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(); let mut local_path = source.clone();
local_path.push(path.strip_prefix('/').unwrap_or(&path)); local_path.push(path.strip_prefix('/').unwrap_or(&path));
let mut data = Vec::new(); 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| { .with_ipc_handler(|window: &Window, req: String| {
match req.as_str() { match req.as_str() {
@ -193,11 +184,13 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
match event { match event {
Event::NewEvents(StartCause::Init) => webview.focus(),
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => *control_flow = ControlFlow::Exit,
Event::GlobalShortcutEvent(id) => webview
.evaluate_script(&format!("GlobalShortcutEvent({:})", id.0))
.unwrap_or_default(),
_ => (), _ => (),
} }
}); });

View File

@ -10,7 +10,7 @@ image = {version = "0.24", optional = true}
new_mime_guess = {version = "4.0", optional = true} new_mime_guess = {version = "4.0", optional = true}
serde = {version = "1.0", features = ["derive"]} serde = {version = "1.0", features = ["derive"]}
toml = {version = "0.5", optional = true} toml = {version = "0.5", optional = true}
wry = {version = "0.20", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]} wry = {version = "0.22", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
[features] [features]
bundler = ["new_mime_guess", "toml", "image"] bundler = ["new_mime_guess", "toml", "image"]

View File

@ -168,7 +168,7 @@ impl Data {
Ok(fs) Ok(fs)
} }
fn open_file(&self, current_dir: &Dir, mut path: path::Iter) -> Result<File> { fn open_file(current_dir: &Dir, mut path: path::Iter) -> Result<File> {
let next_path = match path.next() { let next_path = match path.next() {
Some(str) => str.to_string_lossy().to_string(), Some(str) => str.to_string_lossy().to_string(),
None => return Err(io::Error::new(io::ErrorKind::NotFound, "file not found")), None => return Err(io::Error::new(io::ErrorKind::NotFound, "file not found")),
@ -180,7 +180,7 @@ impl Data {
} }
for (name, dir) in &current_dir.dirs { for (name, dir) in &current_dir.dirs {
if next_path == *name { if next_path == *name {
return self.open_file(dir, path); return Self::open_file(dir, path);
} }
} }
Err(io::Error::new(io::ErrorKind::NotFound, "file not found")) Err(io::Error::new(io::ErrorKind::NotFound, "file not found"))
@ -195,7 +195,7 @@ impl Data {
} else { } else {
path path
}; };
self.open_file(&self.fs, path.iter()) Self::open_file(&self.fs, path.iter())
} }
} }
@ -296,7 +296,7 @@ impl Data {
let mut target: Vec<u8> = Vec::new(); let mut target: Vec<u8> = Vec::new();
target.extend(MAGIC_NUMBER_START); target.extend(MAGIC_NUMBER_START);
target.extend(&data.len().to_be_bytes()); target.extend(data.len().to_be_bytes());
target.extend(&data); target.extend(&data);
let target_length = target.len(); let target_length = target.len();
let target_length = target_length + USIZE_LEN; let target_length = target_length + USIZE_LEN;

View File

@ -5,7 +5,7 @@ version = "0.1.0"
[dependencies] [dependencies]
neutauri_data = {path = "../neutauri_data", features = ["runtime"]} neutauri_data = {path = "../neutauri_data", features = ["runtime"]}
wry = {version = "0.20", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]} wry = {version = "0.22", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
[target.'cfg(windows)'.build-dependencies] [target.'cfg(windows)'.build-dependencies]
winres = "0.1" winres = "0.1"

View File

@ -5,30 +5,18 @@ use std::path::PathBuf;
use wry::{ use wry::{
application::{ application::{
dpi::{PhysicalSize, Size}, dpi::{PhysicalSize, Size},
event::{Event, StartCause, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::{Fullscreen, Icon, Window, WindowBuilder}, window::{Fullscreen, Icon, Window, WindowBuilder},
}, },
webview::{WebContext, WebViewBuilder}, webview::{WebContext, WebViewBuilder},
}; };
const PROTOCOL_PREFIX: &str = "{PROTOCOL}://"; const PROTOCOL_PREFIX: &str = "neu://localhost";
const PROTOCOL: &str = "neu"; const PROTOCOL: &str = "neu";
fn custom_protocol_uri<T: Into<String>>(protocol: T, path: T) -> String { fn custom_protocol_uri<T: Into<String>>(path: T) -> String {
PROTOCOL_PREFIX.replacen("{PROTOCOL}", &protocol.into(), 1) + &path.into() PROTOCOL_PREFIX.to_owned() + &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 main() -> wry::Result<()> { fn main() -> wry::Result<()> {
@ -86,12 +74,12 @@ fn main() -> wry::Result<()> {
let webview_builder = match url { let webview_builder = match url {
Some(url) => { Some(url) => {
if url.starts_with('/') { if url.starts_with('/') {
webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))? webview_builder.with_url(&custom_protocol_uri(&url))?
} else { } else {
webview_builder.with_url(&url)? 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 = res.webview_attr.html.clone(); let html = res.webview_attr.html.clone();
let webview_builder = match html { let webview_builder = match html {
@ -149,7 +137,7 @@ fn main() -> wry::Result<()> {
r#"window.oncontextmenu = (event) => { event.preventDefault(); }"#, r#"window.oncontextmenu = (event) => { event.preventDefault(); }"#,
) )
.with_custom_protocol(PROTOCOL.to_string(), move |request| { .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 file = match res.open(path) { let mut file = match res.open(path) {
Ok(file) => file, Ok(file) => file,
Err(e) => { Err(e) => {
@ -160,9 +148,10 @@ fn main() -> wry::Result<()> {
} }
} }
}; };
wry::http::ResponseBuilder::new() wry::http::Response::builder()
.mimetype(&file.mimetype()) .header("Content-Type", file.mimetype())
.body(file.decompressed_data()?) .body(file.decompressed_data()?)
.map_err(|e| e.into())
}) })
.with_ipc_handler(|window: &Window, req: String| { .with_ipc_handler(|window: &Window, req: String| {
match req.as_str() { match req.as_str() {
@ -178,11 +167,13 @@ fn main() -> wry::Result<()> {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
match event { match event {
Event::NewEvents(StartCause::Init) => webview.focus(),
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => *control_flow = ControlFlow::Exit,
Event::GlobalShortcutEvent(id) => webview
.evaluate_script(&format!("GlobalShortcutEvent({:})", id.0))
.unwrap_or_default(),
_ => (), _ => (),
} }
}); });