mirror of
https://github.com/Tim-Paik/neutauri.git
synced 2024-10-12 23:29:41 +00:00
Compare commits
25
Commits
main
..
93eb7bd1dc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93eb7bd1dc | ||
|
|
15e5cde7e9 | ||
|
|
dd6cd6b85e | ||
|
|
b7408124e6 | ||
|
|
084fe40035 | ||
|
|
f3e9f6e936 | ||
|
|
663f0159f4 | ||
|
|
f3b45d106d | ||
|
|
7555958627 | ||
|
|
5217d374aa | ||
|
|
638ba99d04 | ||
|
|
2f9e3b80ba | ||
|
|
76e13856bd | ||
|
|
101a589bd2 | ||
|
|
5fc1a26dc2 | ||
|
|
ed51c5e264 | ||
|
|
b4cb7da6ad | ||
|
|
9be31e0968 | ||
|
|
4cdfccef05 | ||
|
|
37d1a057fc | ||
|
|
449fb69d75 | ||
|
|
6a9dbb4a19 | ||
|
|
1a94a55cf1 | ||
|
|
2b0084adf6 | ||
|
|
561e19e732 |
-50
@@ -1,50 +0,0 @@
|
|||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: build
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: build
|
|
||||||
image: ubuntu:latest
|
|
||||||
commands:
|
|
||||||
- echo '========Install the necessary environment========'
|
|
||||||
- apt update && apt install -y curl gcc git libwebkit2gtk-4.0-dev libappindicator3-dev
|
|
||||||
- echo '========Install the Rust toolchain========'
|
|
||||||
- curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable -y
|
|
||||||
- echo '========Compile the Neutauri binary========'
|
|
||||||
- $HOME/.cargo/bin/cargo build --release --bin neutauri_runtime
|
|
||||||
- $HOME/.cargo/bin/cargo build --release --bin neutauri_bundler
|
|
||||||
|
|
||||||
- name: gitea_release
|
|
||||||
image: plugins/gitea-release
|
|
||||||
settings:
|
|
||||||
api_key:
|
|
||||||
from_secret: gitea_token
|
|
||||||
base_url: https://git.186526.xyz
|
|
||||||
files:
|
|
||||||
- ./target/release/neutauri_bundler
|
|
||||||
checksum:
|
|
||||||
- md5
|
|
||||||
- sha256
|
|
||||||
when:
|
|
||||||
event:
|
|
||||||
- tag
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: clippy
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: clippy
|
|
||||||
image: ubuntu:latest
|
|
||||||
commands:
|
|
||||||
- echo '========Install the necessary environment========'
|
|
||||||
- apt update && apt install -y curl gcc git libwebkit2gtk-4.0-dev libappindicator3-dev
|
|
||||||
- echo '========Install the Rust toolchain========'
|
|
||||||
- curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable -c clippy -y
|
|
||||||
- echo '========Compile the Neutauri binary========'
|
|
||||||
- $HOME/.cargo/bin/cargo build --release --bin neutauri_runtime
|
|
||||||
- $HOME/.cargo/bin/cargo build --release --bin neutauri_bundler
|
|
||||||
- echo '========Run Cargo Clippy========'
|
|
||||||
- $HOME/.cargo/bin/cargo clippy
|
|
||||||
Generated
+710
-1376
File diff suppressed because it is too large
Load Diff
@@ -2,10 +2,4 @@
|
|||||||
members = [
|
members = [
|
||||||
"neutauri_runtime",
|
"neutauri_runtime",
|
||||||
"neutauri_bundler",
|
"neutauri_bundler",
|
||||||
"neutauri_data",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[profile.release]
|
|
||||||
lto = true
|
|
||||||
strip = true
|
|
||||||
opt-level = "z"
|
|
||||||
|
|||||||
@@ -5,14 +5,14 @@ version = "0.1.0"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
bincode = "1.3"
|
||||||
|
brotli = "3.3"
|
||||||
gumdrop = "0.8"
|
gumdrop = "0.8"
|
||||||
inquire = "0.6"
|
image = "0.24"
|
||||||
neutauri_data = {path = "../neutauri_data", default-features = false, 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.7"
|
toml = "0.5"
|
||||||
vc-ltl = "5.0.5"
|
wry = {version = "0.15", features = ["devtools"]}
|
||||||
wry = {version = "0.27", 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"}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
|
use crate::data;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use neutauri_data as data;
|
|
||||||
#[cfg(windows)]
|
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env, fs,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
|
io::{self, Write},
|
||||||
};
|
};
|
||||||
use std::{fs, io::Write};
|
|
||||||
|
|
||||||
fn options() -> fs::OpenOptions {
|
fn options() -> fs::OpenOptions {
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
@@ -20,10 +19,7 @@ fn options() -> fs::OpenOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
fn get_runtime_data(
|
fn get_runtime_data() -> anyhow::Result<Vec<u8>> {
|
||||||
_icon_path: Option<std::path::PathBuf>,
|
|
||||||
_manifest_path: Option<std::path::PathBuf>,
|
|
||||||
) -> anyhow::Result<Vec<u8>> {
|
|
||||||
Ok(include_bytes!("../../target/release/neutauri_runtime").to_vec())
|
Ok(include_bytes!("../../target/release/neutauri_runtime").to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,17 +53,10 @@ fn get_runtime_data(
|
|||||||
Ok(runtime_data)
|
Ok(runtime_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn bundle(config_path: String) -> anyhow::Result<()> {
|
pub fn bundle(config_path: String) -> anyhow::Result<()> {
|
||||||
let config_path = std::path::Path::new(&config_path)
|
let config_path = std::path::Path::new(&config_path).canonicalize()?;
|
||||||
.canonicalize()
|
|
||||||
.with_context(|| {
|
|
||||||
format!(
|
|
||||||
"Error reading config file from {}\n\n{}",
|
|
||||||
&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")?;
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||||
let source = match config_path.parent() {
|
let source = match config_path.parent() {
|
||||||
Some(path) => path.join(&config.source).canonicalize()?,
|
Some(path) => path.join(&config.source).canonicalize()?,
|
||||||
None => config.source.canonicalize()?,
|
None => config.source.canonicalize()?,
|
||||||
@@ -77,13 +66,14 @@ 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().is_none() && cfg!(windows) {
|
let target = if target.extension() == None && cfg!(windows) {
|
||||||
target.with_extension("exe")
|
target.with_extension("exe")
|
||||||
} else {
|
} else {
|
||||||
target
|
target
|
||||||
};
|
};
|
||||||
if target.extension() == Some(std::ffi::OsStr::new("neu")) {
|
if target.extension() == Some(std::ffi::OsStr::new("neu")) {
|
||||||
return data::pack(config_path);
|
data::pack(config_path)?;
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
let data = data::Data::build_from_dir(source, config.window_attr()?, config.webview_attr()?)?;
|
let data = data::Data::build_from_dir(source, config.window_attr()?, config.webview_attr()?)?;
|
||||||
let mut f = options().open(&target)?;
|
let mut f = options().open(&target)?;
|
||||||
|
|||||||
@@ -104,103 +104,6 @@ pub struct WebViewAttr {
|
|||||||
pub initialization_script: Option<String>,
|
pub initialization_script: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "runtime")]
|
|
||||||
impl File {
|
|
||||||
pub fn decompressed_data(&mut self) -> Result<Vec<u8>> {
|
|
||||||
let mut data = Vec::with_capacity(self.data.len());
|
|
||||||
let mut r = brotli::Decompressor::new(self.data.as_slice(), 4096);
|
|
||||||
r.read_to_end(&mut data)?;
|
|
||||||
Ok(data)
|
|
||||||
}
|
|
||||||
pub fn mimetype(&self) -> String {
|
|
||||||
self.mime.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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()];
|
|
||||||
let mut data_length_data = [0; USIZE_LEN];
|
|
||||||
let mut data = Vec::new();
|
|
||||||
let mut magic_number_end_data = [0; MAGIC_NUMBER_END.len()];
|
|
||||||
base.seek(SeekFrom::Start(base_length - MAGIC_NUMBER_END.len() as u64))?;
|
|
||||||
// 此时指针指向 MAGIC_NUMBER_END 之前
|
|
||||||
base.read_exact(&mut magic_number_end_data)?;
|
|
||||||
if &magic_number_end_data != MAGIC_NUMBER_END {
|
|
||||||
return Err(io::Error::new(
|
|
||||||
io::ErrorKind::InvalidData,
|
|
||||||
"MAGIC_NUMBER_END not found",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
base.seek(SeekFrom::Start(
|
|
||||||
base_length - MAGIC_NUMBER_END.len() as u64 - USIZE_LEN as u64,
|
|
||||||
))?;
|
|
||||||
// 此时指针指向 data_length 之前
|
|
||||||
base.read_exact(&mut data_length_data)?;
|
|
||||||
base.seek(SeekFrom::Start(
|
|
||||||
base_length - u64::from_be_bytes(data_length_data),
|
|
||||||
))?;
|
|
||||||
// 此时指针指向 MAGIC_NUMBER_START
|
|
||||||
base.read_exact(&mut magic_number_start_data)?;
|
|
||||||
if &magic_number_start_data != MAGIC_NUMBER_START {
|
|
||||||
return Err(io::Error::new(
|
|
||||||
io::ErrorKind::InvalidData,
|
|
||||||
"MAGIC_NUMBER_START not found",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
base.read_exact(&mut data_length_data)?;
|
|
||||||
// 此时指针指向 Data 前
|
|
||||||
base.take(u64::from_be_bytes(data_length_data))
|
|
||||||
.read_to_end(&mut data)?;
|
|
||||||
let serialize_options = bincode::DefaultOptions::new()
|
|
||||||
.with_fixint_encoding()
|
|
||||||
.allow_trailing_bytes()
|
|
||||||
.with_limit(104857600 /* 100MiB */);
|
|
||||||
let fs: Self = match serialize_options.deserialize(&data) {
|
|
||||||
Ok(fs) => fs,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(io::Error::new(io::ErrorKind::InvalidData, e));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
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")),
|
|
||||||
};
|
|
||||||
for (name, file) in ¤t_dir.files {
|
|
||||||
if next_path == *name {
|
|
||||||
return Ok(file.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (name, dir) in ¤t_dir.dirs {
|
|
||||||
if next_path == *name {
|
|
||||||
return Self::open_file(dir, path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(io::Error::new(io::ErrorKind::NotFound, "file not found"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn open<P: AsRef<path::Path>>(&self, path: P) -> Result<File> {
|
|
||||||
let path = normalize_path(path.as_ref());
|
|
||||||
let path = if path.starts_with("/") {
|
|
||||||
path.strip_prefix("/")
|
|
||||||
.unwrap_or_else(|_| Path::new(""))
|
|
||||||
.to_path_buf()
|
|
||||||
} else {
|
|
||||||
path
|
|
||||||
};
|
|
||||||
Self::open_file(&self.fs, path.iter())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "bundler")]
|
|
||||||
impl Dir {
|
impl Dir {
|
||||||
// 使用本地文件系统填充 Dir 结构体
|
// 使用本地文件系统填充 Dir 结构体
|
||||||
fn fill_with<P: AsRef<path::Path>>(
|
fn fill_with<P: AsRef<path::Path>>(
|
||||||
@@ -252,7 +155,6 @@ impl Dir {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bundler")]
|
|
||||||
impl Data {
|
impl Data {
|
||||||
pub fn build_from_dir<P: AsRef<path::Path>>(
|
pub fn build_from_dir<P: AsRef<path::Path>>(
|
||||||
source: P,
|
source: P,
|
||||||
@@ -297,7 +199,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;
|
||||||
@@ -308,7 +210,7 @@ impl Data {
|
|||||||
Ok(target)
|
Ok(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pack<P: AsRef<path::Path>>(config_path: P) -> anyhow::Result<()> {
|
pub fn pack<P: AsRef<path::Path>>(config_path: P) -> Result<()> {
|
||||||
let config_path = config_path.as_ref().canonicalize()?;
|
let config_path = config_path.as_ref().canonicalize()?;
|
||||||
let config: Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())?;
|
let config: Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())?;
|
||||||
let source = match config_path.parent() {
|
let source = match config_path.parent() {
|
||||||
@@ -327,7 +229,6 @@ impl Data {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bundler")]
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -354,7 +255,6 @@ impl Default for Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bundler")]
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn window_attr(&self) -> Result<WindowAttr> {
|
pub fn window_attr(&self) -> Result<WindowAttr> {
|
||||||
Ok(WindowAttr {
|
Ok(WindowAttr {
|
||||||
@@ -394,17 +294,10 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "runtime")]
|
pub fn pack<P: AsRef<path::Path>>(config: P) -> Result<()> {
|
||||||
pub fn load<P: AsRef<path::Path> + Copy>(path: P) -> Result<Data> {
|
|
||||||
Data::new(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "bundler")]
|
|
||||||
pub fn pack<P: AsRef<path::Path>>(config: P) -> anyhow::Result<()> {
|
|
||||||
Data::pack(config)
|
Data::pack(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bundler")]
|
|
||||||
fn load_icon(path: &Path) -> Result<Icon> {
|
fn load_icon(path: &Path) -> Result<Icon> {
|
||||||
let image = image::open(path)
|
let image = image::open(path)
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
|
||||||
+33
-32
@@ -1,34 +1,40 @@
|
|||||||
use anyhow::{Context, Result};
|
use std::{fs, io::Read, path::PathBuf};
|
||||||
use neutauri_data as data;
|
|
||||||
use std::{fs, io::Read, path::PathBuf, borrow::Cow};
|
|
||||||
use wry::{
|
use wry::{
|
||||||
application::{
|
application::{
|
||||||
dpi::{PhysicalSize, Size},
|
dpi::{PhysicalSize, Size},
|
||||||
event::{Event, WindowEvent},
|
event::{Event, StartCause, 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 = "dev://localhost";
|
use crate::data;
|
||||||
|
|
||||||
|
const PROTOCOL_PREFIX: &str = "{PROTOCOL}://";
|
||||||
const PROTOCOL: &str = "dev";
|
const PROTOCOL: &str = "dev";
|
||||||
|
|
||||||
fn custom_protocol_uri<T: Into<String>>(path: T) -> String {
|
fn custom_protocol_uri<T: Into<String>>(protocol: T, path: T) -> String {
|
||||||
PROTOCOL_PREFIX.to_owned() + &path.into()
|
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,
|
||||||
|
))),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn dev(config_path: String) -> Result<()> {
|
pub fn dev(config_path: String) -> wry::Result<()> {
|
||||||
let config_path = std::path::Path::new(&config_path)
|
let config_path = std::path::Path::new(&config_path).canonicalize()?;
|
||||||
.canonicalize()
|
let config: data::Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())
|
||||||
.with_context(|| {
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
|
||||||
format!(
|
|
||||||
"Error reading config file from {}\n\n{}",
|
|
||||||
&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())
|
|
||||||
.with_context(|| "toml parsing error")?;
|
|
||||||
let source = config.source.canonicalize()?;
|
let source = config.source.canonicalize()?;
|
||||||
|
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
@@ -81,12 +87,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(&url))?
|
webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))?
|
||||||
} else {
|
} else {
|
||||||
webview_builder.with_url(&url)?
|
webview_builder.with_url(&url)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => webview_builder.with_url(&custom_protocol_uri("/index.html"))?,
|
None => webview_builder.with_url(&custom_protocol_uri(PROTOCOL, "/index.html"))?,
|
||||||
};
|
};
|
||||||
let html = config.webview_attr()?.html;
|
let html = config.webview_attr()?.html;
|
||||||
let webview_builder = match html {
|
let webview_builder = match html {
|
||||||
@@ -136,14 +142,13 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
|
|||||||
WebContext::new(None)
|
WebContext::new(None)
|
||||||
};
|
};
|
||||||
let webview = webview_builder
|
let webview = webview_builder
|
||||||
.with_clipboard(true)
|
|
||||||
.with_visible(config.window_attr()?.visible)
|
.with_visible(config.window_attr()?.visible)
|
||||||
.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 = request.uri().path();
|
let path = custom_protocol_uri_to_path(PROTOCOL, request.uri())?;
|
||||||
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();
|
||||||
let mut mime: String = "application/octet-stream".to_string();
|
let mut mime: String = "application/octet-stream".to_string();
|
||||||
match fs::File::open(&local_path) {
|
match fs::File::open(&local_path) {
|
||||||
@@ -165,11 +170,7 @@ pub(crate) fn dev(config_path: String) -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wry::http::Response::builder()
|
wry::http::ResponseBuilder::new().mimetype(&mime).body(data)
|
||||||
.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| {
|
.with_ipc_handler(|window: &Window, req: String| {
|
||||||
match req.as_str() {
|
match req.as_str() {
|
||||||
@@ -185,14 +186,14 @@ 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))
|
let _ = webview.resize();
|
||||||
.unwrap_or_default(),
|
}
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
use anyhow::Ok;
|
|
||||||
use neutauri_data as data;
|
|
||||||
|
|
||||||
const TEMPLATE: &str = include_str!("../../neutauri.toml.example");
|
|
||||||
|
|
||||||
pub(crate) fn init() -> anyhow::Result<()> {
|
|
||||||
let config = TEMPLATE;
|
|
||||||
let config = config.replace(
|
|
||||||
"Neutauri Demo",
|
|
||||||
&inquire::Text::new("The name of your program? (for window title)")
|
|
||||||
.with_placeholder("Neutauri App")
|
|
||||||
.with_default("Neutauri App")
|
|
||||||
.prompt()?,
|
|
||||||
);
|
|
||||||
let config = config.replace(
|
|
||||||
"web_src",
|
|
||||||
&inquire::Text::new("Where is your web source code? (relative to the current directory)")
|
|
||||||
.with_placeholder("web_src")
|
|
||||||
.with_default("web_src")
|
|
||||||
.prompt()?,
|
|
||||||
);
|
|
||||||
let config = config.replace(
|
|
||||||
"neutauri_demo",
|
|
||||||
&inquire::Text::new("The name of your output target?")
|
|
||||||
.with_placeholder("app")
|
|
||||||
.with_default("app")
|
|
||||||
.prompt()?,
|
|
||||||
);
|
|
||||||
let config = config.replacen(
|
|
||||||
"Small",
|
|
||||||
inquire::Select::new(
|
|
||||||
"The default size of the window?",
|
|
||||||
vec!["Small", "Medium", "Large"],
|
|
||||||
)
|
|
||||||
.prompt()?,
|
|
||||||
1,
|
|
||||||
);
|
|
||||||
let config_path = data::normalize_path(std::path::Path::new("./neutauri.toml"));
|
|
||||||
std::fs::write(&config_path, config)?;
|
|
||||||
eprintln!(
|
|
||||||
"The configuration file has been written to \"{}\"",
|
|
||||||
config_path.display()
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
use gumdrop::Options;
|
use gumdrop::Options;
|
||||||
|
|
||||||
mod bundle;
|
mod bundle;
|
||||||
mod dev;
|
mod dev;
|
||||||
mod init;
|
mod data;
|
||||||
|
|
||||||
#[derive(Debug, Options)]
|
#[derive(Debug, Options)]
|
||||||
struct Args {
|
struct Args {
|
||||||
@@ -20,8 +21,6 @@ enum Command {
|
|||||||
Bundle(BundleOpts),
|
Bundle(BundleOpts),
|
||||||
#[options(help = "run the project in the current directory in development mode")]
|
#[options(help = "run the project in the current directory in development mode")]
|
||||||
Dev(DevOpts),
|
Dev(DevOpts),
|
||||||
#[options(help = "initialize a neutauri project")]
|
|
||||||
Init(InitOpts),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Options)]
|
#[derive(Debug, Clone, Options)]
|
||||||
@@ -40,12 +39,6 @@ struct DevOpts {
|
|||||||
config: Option<String>,
|
config: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Options)]
|
|
||||||
struct InitOpts {
|
|
||||||
#[options(help = "print help information")]
|
|
||||||
help: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_help_and_exit(args: Args) {
|
fn print_help_and_exit(args: Args) {
|
||||||
if args.command.is_some() {
|
if args.command.is_some() {
|
||||||
Args::parse_args_default_or_exit();
|
Args::parse_args_default_or_exit();
|
||||||
@@ -54,6 +47,7 @@ fn print_help_and_exit(args: Args) {
|
|||||||
eprintln!(
|
eprintln!(
|
||||||
"Usage: {:?} [SUBCOMMAND] [OPTIONS]",
|
"Usage: {:?} [SUBCOMMAND] [OPTIONS]",
|
||||||
std::env::args()
|
std::env::args()
|
||||||
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.unwrap_or_else(|| "neutauri_bundler".to_string())
|
.unwrap_or_else(|| "neutauri_bundler".to_string())
|
||||||
);
|
);
|
||||||
@@ -75,8 +69,6 @@ fn main() -> anyhow::Result<()> {
|
|||||||
Some(command) => match command {
|
Some(command) => match command {
|
||||||
Command::Bundle(opts) => {
|
Command::Bundle(opts) => {
|
||||||
if opts.help_requested() {
|
if opts.help_requested() {
|
||||||
eprintln!("Package according to the configuration in neutauri.toml");
|
|
||||||
eprintln!();
|
|
||||||
print_help_and_exit(args);
|
print_help_and_exit(args);
|
||||||
}
|
}
|
||||||
let config_path = opts.config.unwrap_or_else(|| "neutauri.toml".to_string());
|
let config_path = opts.config.unwrap_or_else(|| "neutauri.toml".to_string());
|
||||||
@@ -84,21 +76,11 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
Command::Dev(opts) => {
|
Command::Dev(opts) => {
|
||||||
if opts.help_requested() {
|
if opts.help_requested() {
|
||||||
eprintln!("Check the configuration in neutauri.toml and start directly");
|
|
||||||
eprintln!();
|
|
||||||
print_help_and_exit(args);
|
print_help_and_exit(args);
|
||||||
}
|
}
|
||||||
let config_path = opts.config.unwrap_or_else(|| "neutauri.toml".to_string());
|
let config_path = opts.config.unwrap_or_else(|| "neutauri.toml".to_string());
|
||||||
dev::dev(config_path)?;
|
dev::dev(config_path)?;
|
||||||
}
|
},
|
||||||
Command::Init(opts) => {
|
|
||||||
if opts.help_requested() {
|
|
||||||
eprintln!("Interactively create a neutauri.toml configuration file");
|
|
||||||
eprintln!();
|
|
||||||
print_help_and_exit(args);
|
|
||||||
}
|
|
||||||
init::init()?;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => print_help_and_exit(args),
|
None => print_help_and_exit(args),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
[package]
|
|
||||||
edition = "2021"
|
|
||||||
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.7", optional = true}
|
|
||||||
wry = {version = "0.27", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
|
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["runtime"]
|
|
||||||
bundler = ["anyhow", "new_mime_guess", "toml", "image"]
|
|
||||||
runtime = []
|
|
||||||
@@ -4,9 +4,10 @@ name = "neutauri_runtime"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
neutauri_data = {path = "../neutauri_data", default-features = false, features = ["runtime"]}
|
bincode = "1.3"
|
||||||
vc-ltl = "5.0.5"
|
brotli = "3.3"
|
||||||
wry = {version = "0.27", default-features = false, features = ["protocol", "tray", "transparent", "fullscreen"]}
|
serde = {version = "1.0", features = ["derive"]}
|
||||||
|
wry = "0.15"
|
||||||
|
|
||||||
[target.'cfg(windows)'.build-dependencies]
|
[target.'cfg(windows)'.build-dependencies]
|
||||||
winres = "0.1"
|
winres = "0.1"
|
||||||
|
|||||||
@@ -0,0 +1,255 @@
|
|||||||
|
use bincode::Options;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
io::{self, Read, Result, Seek, SeekFrom},
|
||||||
|
path::{self, Component, Path, PathBuf},
|
||||||
|
};
|
||||||
|
use wry::application::dpi::Position;
|
||||||
|
|
||||||
|
const MAGIC_NUMBER_START: &[u8; 9] = b"NEUTFSv01";
|
||||||
|
const MAGIC_NUMBER_END: &[u8; 9] = b"NEUTFSEnd";
|
||||||
|
const USIZE_LEN: usize = usize::MAX.to_be_bytes().len();
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub enum Compress {
|
||||||
|
Brotli,
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct File {
|
||||||
|
mime: String,
|
||||||
|
data: Vec<u8>,
|
||||||
|
compress: Compress,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct Dir {
|
||||||
|
files: Vec<(String, File)>,
|
||||||
|
dirs: Vec<(String, Dir)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Data {
|
||||||
|
pub window_attr: WindowAttr,
|
||||||
|
pub webview_attr: WebViewAttr,
|
||||||
|
fs: Dir,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
|
||||||
|
pub enum WindowSize {
|
||||||
|
Large,
|
||||||
|
Medium,
|
||||||
|
Small,
|
||||||
|
Fixed { width: f64, height: f64 },
|
||||||
|
Scale { factor: f64 },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct Config {
|
||||||
|
pub source: PathBuf,
|
||||||
|
pub target: PathBuf,
|
||||||
|
pub inner_size: Option<WindowSize>,
|
||||||
|
pub min_inner_size: Option<WindowSize>,
|
||||||
|
pub max_inner_size: Option<WindowSize>,
|
||||||
|
pub resizable: bool,
|
||||||
|
pub fullscreen: bool,
|
||||||
|
pub title: String,
|
||||||
|
pub maximized: bool,
|
||||||
|
pub visible: bool,
|
||||||
|
pub transparent: bool,
|
||||||
|
pub decorations: bool,
|
||||||
|
pub always_on_top: bool,
|
||||||
|
pub icon: Option<PathBuf>,
|
||||||
|
pub spa: bool,
|
||||||
|
pub url: Option<String>,
|
||||||
|
pub html: Option<PathBuf>,
|
||||||
|
pub initialization_script: Option<String>,
|
||||||
|
pub manifest: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
||||||
|
pub struct Icon {
|
||||||
|
pub rgba: Vec<u8>,
|
||||||
|
pub width: u32,
|
||||||
|
pub height: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct WindowAttr {
|
||||||
|
pub inner_size: Option<WindowSize>,
|
||||||
|
pub min_inner_size: Option<WindowSize>,
|
||||||
|
pub max_inner_size: Option<WindowSize>,
|
||||||
|
pub position: Option<Position>,
|
||||||
|
pub resizable: bool,
|
||||||
|
pub fullscreen: bool,
|
||||||
|
pub title: String,
|
||||||
|
pub maximized: bool,
|
||||||
|
pub visible: bool,
|
||||||
|
pub transparent: bool,
|
||||||
|
pub decorations: bool,
|
||||||
|
pub always_on_top: bool,
|
||||||
|
pub icon: Option<Icon>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct WebViewAttr {
|
||||||
|
pub visible: bool,
|
||||||
|
pub transparent: bool,
|
||||||
|
pub spa: bool,
|
||||||
|
pub url: Option<String>,
|
||||||
|
pub html: Option<String>,
|
||||||
|
pub initialization_script: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl File {
|
||||||
|
pub fn decompressed_data(&mut self) -> Result<Vec<u8>> {
|
||||||
|
let mut data = Vec::with_capacity(self.data.len());
|
||||||
|
let mut r = brotli::Decompressor::new(self.data.as_slice(), 4096);
|
||||||
|
r.read_to_end(&mut data)?;
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
pub fn mimetype(&self) -> String {
|
||||||
|
self.mime.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Data {
|
||||||
|
pub fn new<P: AsRef<path::Path> + Copy>(path: P) -> Result<Self> {
|
||||||
|
let mut base = fs::File::open(path)?;
|
||||||
|
let base_length = base.metadata()?.len();
|
||||||
|
let mut magic_number_start_data = [0; MAGIC_NUMBER_START.len()];
|
||||||
|
let mut data_length_data = [0; USIZE_LEN];
|
||||||
|
let mut data = Vec::new();
|
||||||
|
let mut magic_number_end_data = [0; MAGIC_NUMBER_END.len()];
|
||||||
|
base.seek(SeekFrom::Start(base_length - MAGIC_NUMBER_END.len() as u64))?;
|
||||||
|
// 此时指针指向 MAGIC_NUMBER_END 之前
|
||||||
|
base.read_exact(&mut magic_number_end_data)?;
|
||||||
|
if &magic_number_end_data != MAGIC_NUMBER_END {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
"MAGIC_NUMBER_END not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
base.seek(SeekFrom::Start(
|
||||||
|
base_length - MAGIC_NUMBER_END.len() as u64 - USIZE_LEN as u64,
|
||||||
|
))?;
|
||||||
|
// 此时指针指向 data_length 之前
|
||||||
|
base.read_exact(&mut data_length_data)?;
|
||||||
|
base.seek(SeekFrom::Start(
|
||||||
|
base_length - u64::from_be_bytes(data_length_data),
|
||||||
|
))?;
|
||||||
|
// 此时指针指向 MAGIC_NUMBER_START
|
||||||
|
base.read_exact(&mut magic_number_start_data)?;
|
||||||
|
if &magic_number_start_data != MAGIC_NUMBER_START {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
"MAGIC_NUMBER_START not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
base.read_exact(&mut data_length_data)?;
|
||||||
|
// 此时指针指向 Data 前
|
||||||
|
base.take(u64::from_be_bytes(data_length_data))
|
||||||
|
.read_to_end(&mut data)?;
|
||||||
|
let serialize_options = bincode::DefaultOptions::new()
|
||||||
|
.with_fixint_encoding()
|
||||||
|
.allow_trailing_bytes()
|
||||||
|
.with_limit(104857600 /* 100MiB */);
|
||||||
|
let fs: Self = match serialize_options.deserialize(&data) {
|
||||||
|
Ok(fs) => fs,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(io::Error::new(io::ErrorKind::InvalidData, e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_file(&self, 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")),
|
||||||
|
};
|
||||||
|
for (name, file) in ¤t_dir.files {
|
||||||
|
if next_path == *name {
|
||||||
|
return Ok(file.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (name, dir) in ¤t_dir.dirs {
|
||||||
|
if next_path == *name {
|
||||||
|
return self.open_file(dir, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(io::Error::new(io::ErrorKind::NotFound, "file not found"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open<P: AsRef<path::Path>>(&self, path: P) -> Result<File> {
|
||||||
|
let path = normalize_path(path.as_ref());
|
||||||
|
let path = if path.starts_with("/") {
|
||||||
|
path.strip_prefix("/")
|
||||||
|
.unwrap_or_else(|_| Path::new(""))
|
||||||
|
.to_path_buf()
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
};
|
||||||
|
self.open_file(&self.fs, path.iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
source: PathBuf::from("."),
|
||||||
|
target: PathBuf::from("app.neu"),
|
||||||
|
inner_size: Some(WindowSize::Medium),
|
||||||
|
min_inner_size: None,
|
||||||
|
max_inner_size: None,
|
||||||
|
resizable: true,
|
||||||
|
fullscreen: false,
|
||||||
|
title: "".into(),
|
||||||
|
maximized: false,
|
||||||
|
visible: true,
|
||||||
|
transparent: false,
|
||||||
|
decorations: true,
|
||||||
|
always_on_top: false,
|
||||||
|
icon: None,
|
||||||
|
spa: false,
|
||||||
|
url: Some("/index.html".into()),
|
||||||
|
html: None,
|
||||||
|
initialization_script: None,
|
||||||
|
manifest: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load<P: AsRef<path::Path> + Copy>(path: P) -> Result<Data> {
|
||||||
|
Data::new(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||||
|
let mut components = path.components().peekable();
|
||||||
|
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||||
|
components.next();
|
||||||
|
PathBuf::from(c.as_os_str())
|
||||||
|
} else {
|
||||||
|
PathBuf::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
for component in components {
|
||||||
|
match component {
|
||||||
|
Component::Prefix(..) => {}
|
||||||
|
Component::RootDir => {
|
||||||
|
ret.push(component.as_os_str());
|
||||||
|
}
|
||||||
|
Component::CurDir => {}
|
||||||
|
Component::ParentDir => {
|
||||||
|
ret.pop();
|
||||||
|
}
|
||||||
|
Component::Normal(c) => {
|
||||||
|
ret.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
@@ -1,22 +1,35 @@
|
|||||||
#![windows_subsystem = "windows"]
|
#![windows_subsystem = "windows"]
|
||||||
|
|
||||||
use neutauri_data as data;
|
use std::path::PathBuf;
|
||||||
use std::{borrow::Cow, path::PathBuf};
|
|
||||||
use wry::{
|
use wry::{
|
||||||
application::{
|
application::{
|
||||||
dpi::{PhysicalSize, Size},
|
dpi::{PhysicalSize, Size},
|
||||||
event::{Event, WindowEvent},
|
event::{Event, StartCause, 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},
|
||||||
};
|
};
|
||||||
|
mod data;
|
||||||
|
|
||||||
const PROTOCOL_PREFIX: &str = "neu://localhost";
|
const PROTOCOL_PREFIX: &str = "{PROTOCOL}://";
|
||||||
const PROTOCOL: &str = "neu";
|
const PROTOCOL: &str = "neu";
|
||||||
|
|
||||||
fn custom_protocol_uri<T: Into<String>>(path: T) -> String {
|
fn custom_protocol_uri<T: Into<String>>(protocol: T, path: T) -> String {
|
||||||
PROTOCOL_PREFIX.to_owned() + &path.into()
|
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 main() -> wry::Result<()> {
|
fn main() -> wry::Result<()> {
|
||||||
@@ -74,12 +87,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(&url))?
|
webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))?
|
||||||
} else {
|
} else {
|
||||||
webview_builder.with_url(&url)?
|
webview_builder.with_url(&url)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => webview_builder.with_url(&custom_protocol_uri("/index.html"))?,
|
None => webview_builder.with_url(&custom_protocol_uri(PROTOCOL, "/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 {
|
||||||
@@ -129,32 +142,24 @@ fn main() -> wry::Result<()> {
|
|||||||
WebContext::new(None)
|
WebContext::new(None)
|
||||||
};
|
};
|
||||||
let webview = webview_builder
|
let webview = webview_builder
|
||||||
.with_clipboard(true)
|
|
||||||
.with_visible(res.window_attr.visible)
|
.with_visible(res.window_attr.visible)
|
||||||
.with_transparent(res.window_attr.transparent)
|
.with_transparent(res.window_attr.transparent)
|
||||||
.with_web_context(&mut web_context)
|
.with_web_context(&mut web_context)
|
||||||
.with_initialization_script(
|
|
||||||
r#"window.oncontextmenu = (event) => { event.preventDefault(); }"#,
|
|
||||||
)
|
|
||||||
.with_custom_protocol(PROTOCOL.to_string(), move |request| {
|
.with_custom_protocol(PROTOCOL.to_string(), move |request| {
|
||||||
let path = request.uri().path();
|
let path = custom_protocol_uri_to_path(PROTOCOL, request.uri())?;
|
||||||
let mut file = match res.open(path) {
|
let mut file = match res.open(path) {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if e.kind() == std::io::ErrorKind::NotFound
|
if e.kind() == std::io::ErrorKind::NotFound && res.webview_attr.spa {
|
||||||
&& res.webview_attr.spa
|
|
||||||
&& path != "/index.html"
|
|
||||||
{
|
|
||||||
res.open("index.html")?
|
res.open("index.html")?
|
||||||
} else {
|
} else {
|
||||||
return Err(wry::Error::Io(e));
|
return Err(wry::Error::Io(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
wry::http::Response::builder()
|
wry::http::ResponseBuilder::new()
|
||||||
.header("Content-Type", file.mimetype())
|
.mimetype(&file.mimetype())
|
||||||
.body(Cow::Owned(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() {
|
||||||
@@ -170,14 +175,14 @@ 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))
|
let _ = webview.resize();
|
||||||
.unwrap_or_default(),
|
}
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user