mirror of
https://github.com/Tim-Paik/neutauri.git
synced 2024-10-12 23:29:41 +00:00
Compare commits
8 Commits
v0.1.0-alp
...
main
Author | SHA1 | Date | |
---|---|---|---|
e1fb1c4494 | |||
9413e2d232 | |||
3449221f81
|
|||
2a8e27995a
|
|||
167bee14e7
|
|||
1bebb857ee
|
|||
a333c2a507
|
|||
69531170b9
|
1693
Cargo.lock
generated
1693
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -6,12 +6,13 @@ version = "0.1.0"
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
gumdrop = "0.8"
|
||||
inquire = "0.2"
|
||||
neutauri_data = {path = "../neutauri_data", features = ["bundler"]}
|
||||
inquire = "0.6"
|
||||
neutauri_data = {path = "../neutauri_data", default-features = false, 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"]}
|
||||
toml = "0.7"
|
||||
vc-ltl = "5.0.5"
|
||||
wry = {version = "0.27", 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"}
|
||||
|
@ -77,14 +77,13 @@ 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
|
||||
};
|
||||
if target.extension() == Some(std::ffi::OsStr::new("neu")) {
|
||||
data::pack(config_path)?;
|
||||
return Ok(());
|
||||
return data::pack(config_path);
|
||||
}
|
||||
let data = data::Data::build_from_dir(source, config.window_attr()?, config.webview_attr()?)?;
|
||||
let mut f = options().open(&target)?;
|
||||
|
@ -1,33 +1,21 @@
|
||||
use anyhow::{Context, Result};
|
||||
use neutauri_data as data;
|
||||
use std::{fs, io::Read, path::PathBuf};
|
||||
use std::{fs, io::Read, path::PathBuf, borrow::Cow};
|
||||
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<()> {
|
||||
@ -39,7 +27,7 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
|
||||
&config_path, "You may want to create a neutauri.toml via the init subcommand?"
|
||||
)
|
||||
})?;
|
||||
let config: data::Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())
|
||||
let config: data::Config = toml::from_str(fs::read_to_string(config_path)?.as_str())
|
||||
.with_context(|| "toml parsing error")?;
|
||||
let source = config.source.canonicalize()?;
|
||||
|
||||
@ -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,9 +141,9 @@ 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));
|
||||
local_path.push(path.strip_prefix('/').unwrap_or(path));
|
||||
let mut data = Vec::new();
|
||||
let mut mime: String = "application/octet-stream".to_string();
|
||||
match fs::File::open(&local_path) {
|
||||
@ -177,7 +165,11 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
wry::http::ResponseBuilder::new().mimetype(&mime).body(data)
|
||||
wry::http::Response::builder()
|
||||
.header("Content-Type", mime)
|
||||
.header("Access-Control-Allow-Origin", "*")
|
||||
.body(Cow::Owned(data))
|
||||
.map_err(|e| e.into())
|
||||
})
|
||||
.with_ipc_handler(|window: &Window, req: String| {
|
||||
match req.as_str() {
|
||||
@ -193,11 +185,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(),
|
||||
_ => (),
|
||||
}
|
||||
});
|
||||
|
@ -54,7 +54,6 @@ fn print_help_and_exit(args: Args) {
|
||||
eprintln!(
|
||||
"Usage: {:?} [SUBCOMMAND] [OPTIONS]",
|
||||
std::env::args()
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap_or_else(|| "neutauri_bundler".to_string())
|
||||
);
|
||||
|
@ -4,14 +4,16 @@ name = "neutauri_data"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
anyhow = {version = "1.0", optional = true}
|
||||
bincode = "1.3"
|
||||
brotli = "3.3"
|
||||
image = {version = "0.24", optional = true}
|
||||
new_mime_guess = {version = "4.0", optional = true}
|
||||
serde = {version = "1.0", features = ["derive"]}
|
||||
toml = {version = "0.5", optional = true}
|
||||
wry = {version = "0.20", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
|
||||
toml = {version = "0.7", optional = true}
|
||||
wry = {version = "0.27", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
|
||||
|
||||
[features]
|
||||
bundler = ["new_mime_guess", "toml", "image"]
|
||||
default = ["runtime"]
|
||||
bundler = ["anyhow", "new_mime_guess", "toml", "image"]
|
||||
runtime = []
|
@ -2,7 +2,7 @@ use bincode::Options;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Read, Result, SeekFrom, Seek},
|
||||
io::{self, Read, Result},
|
||||
path::{self, Component, Path, PathBuf},
|
||||
};
|
||||
use wry::application::dpi::Position;
|
||||
@ -120,6 +120,7 @@ impl File {
|
||||
#[cfg(feature = "runtime")]
|
||||
impl Data {
|
||||
pub fn new<P: AsRef<path::Path> + Copy>(path: P) -> Result<Self> {
|
||||
use std::io::{Seek, SeekFrom};
|
||||
let mut base = fs::File::open(path)?;
|
||||
let base_length = base.metadata()?.len();
|
||||
let mut magic_number_start_data = [0; MAGIC_NUMBER_START.len()];
|
||||
@ -168,7 +169,7 @@ impl Data {
|
||||
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() {
|
||||
Some(str) => str.to_string_lossy().to_string(),
|
||||
None => return Err(io::Error::new(io::ErrorKind::NotFound, "file not found")),
|
||||
@ -180,7 +181,7 @@ impl Data {
|
||||
}
|
||||
for (name, dir) in ¤t_dir.dirs {
|
||||
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"))
|
||||
@ -195,7 +196,7 @@ impl Data {
|
||||
} else {
|
||||
path
|
||||
};
|
||||
self.open_file(&self.fs, path.iter())
|
||||
Self::open_file(&self.fs, path.iter())
|
||||
}
|
||||
}
|
||||
|
||||
@ -296,7 +297,7 @@ impl Data {
|
||||
|
||||
let mut target: Vec<u8> = Vec::new();
|
||||
target.extend(MAGIC_NUMBER_START);
|
||||
target.extend(&data.len().to_be_bytes());
|
||||
target.extend(data.len().to_be_bytes());
|
||||
target.extend(&data);
|
||||
let target_length = target.len();
|
||||
let target_length = target_length + USIZE_LEN;
|
||||
@ -307,7 +308,7 @@ impl Data {
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
pub fn pack<P: AsRef<path::Path>>(config_path: P) -> Result<()> {
|
||||
pub fn pack<P: AsRef<path::Path>>(config_path: P) -> anyhow::Result<()> {
|
||||
let config_path = config_path.as_ref().canonicalize()?;
|
||||
let config: Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())?;
|
||||
let source = match config_path.parent() {
|
||||
@ -399,7 +400,7 @@ pub fn load<P: AsRef<path::Path> + Copy>(path: P) -> Result<Data> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "bundler")]
|
||||
pub fn pack<P: AsRef<path::Path>>(config: P) -> Result<()> {
|
||||
pub fn pack<P: AsRef<path::Path>>(config: P) -> anyhow::Result<()> {
|
||||
Data::pack(config)
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,9 @@ name = "neutauri_runtime"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
neutauri_data = {path = "../neutauri_data", features = ["runtime"]}
|
||||
wry = {version = "0.20", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
|
||||
neutauri_data = {path = "../neutauri_data", default-features = false, features = ["runtime"]}
|
||||
vc-ltl = "5.0.5"
|
||||
wry = {version = "0.27", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
|
||||
|
||||
[target.'cfg(windows)'.build-dependencies]
|
||||
winres = "0.1"
|
||||
|
@ -1,34 +1,22 @@
|
||||
#![windows_subsystem = "windows"]
|
||||
|
||||
use neutauri_data as data;
|
||||
use std::path::PathBuf;
|
||||
use std::{borrow::Cow, 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 = "neu://localhost";
|
||||
const PROTOCOL: &str = "neu";
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
fn main() -> wry::Result<()> {
|
||||
@ -86,12 +74,12 @@ fn main() -> wry::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 = res.webview_attr.html.clone();
|
||||
let webview_builder = match html {
|
||||
@ -149,20 +137,24 @@ fn main() -> wry::Result<()> {
|
||||
r#"window.oncontextmenu = (event) => { event.preventDefault(); }"#,
|
||||
)
|
||||
.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) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
if e.kind() == std::io::ErrorKind::NotFound && res.webview_attr.spa {
|
||||
if e.kind() == std::io::ErrorKind::NotFound
|
||||
&& res.webview_attr.spa
|
||||
&& path != "/index.html"
|
||||
{
|
||||
res.open("index.html")?
|
||||
} else {
|
||||
return Err(wry::Error::Io(e));
|
||||
}
|
||||
}
|
||||
};
|
||||
wry::http::ResponseBuilder::new()
|
||||
.mimetype(&file.mimetype())
|
||||
.body(file.decompressed_data()?)
|
||||
wry::http::Response::builder()
|
||||
.header("Content-Type", file.mimetype())
|
||||
.body(Cow::Owned(file.decompressed_data()?))
|
||||
.map_err(|e| e.into())
|
||||
})
|
||||
.with_ipc_handler(|window: &Window, req: String| {
|
||||
match req.as_str() {
|
||||
@ -178,11 +170,13 @@ fn main() -> wry::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(),
|
||||
_ => (),
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user