✨ 支持下载/支持toast/支持提示多语言
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
use std::fs;
|
||||
use tauri::{api, command, AppHandle, Manager};
|
||||
use crate::util::{check_file_or_append, get_download_message, show_toast};
|
||||
use download_rs::sync_download::Download;
|
||||
use tauri::{api, command, AppHandle, Manager, Window};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DownloadFileParams {
|
||||
url: String,
|
||||
filename: String,
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn drag_window(app: AppHandle) {
|
||||
@@ -22,7 +29,19 @@ pub fn open_browser(app: AppHandle, url: String) {
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn download(_app: AppHandle, name: String, blob: Vec<u8>) {
|
||||
let path = api::path::download_dir().unwrap().join(name);
|
||||
fs::write(&path, blob).unwrap();
|
||||
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
|
||||
let window: Window = app.get_window("pake").unwrap().clone();
|
||||
let output_path = api::path::download_dir().unwrap().join(params.filename);
|
||||
let file_path = check_file_or_append(&output_path.to_str().unwrap());
|
||||
let download = Download::new(¶ms.url, Some(&file_path), None);
|
||||
match download.download() {
|
||||
Ok(_) => {
|
||||
show_toast(&window, &get_download_message());
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
show_toast(&window, &e.to_string());
|
||||
Err(e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,18 +107,31 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
});
|
||||
|
||||
document.addEventListener('click', (e) => {
|
||||
const origin = e.target.closest('a');
|
||||
if (origin && origin.href) {
|
||||
const target = origin.target;
|
||||
origin.target = '_self';
|
||||
const hrefUrl = new URL(origin.href);
|
||||
const anchorElement = e.target.closest('a');
|
||||
|
||||
if (
|
||||
window.location.host !== hrefUrl.host && // 如果 a 标签内链接的域名和当前页面的域名不一致 且
|
||||
target === '_blank' // a 标签内链接的 target 属性为 _blank 时
|
||||
) {
|
||||
if (anchorElement && anchorElement.href) {
|
||||
const target = anchorElement.target;
|
||||
anchorElement.target = '_self';
|
||||
const hrefUrl = new URL(anchorElement.href);
|
||||
const absoluteUrl = hrefUrl.href;
|
||||
|
||||
// 处理外部链接跳转
|
||||
if (window.location.host !== hrefUrl.host && target === '_blank') {
|
||||
e.preventDefault();
|
||||
invoke('open_browser', { url: origin.href });
|
||||
invoke('open_browser', { url: absoluteUrl });
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理下载链接让Rust处理
|
||||
if (/\.[a-zA-Z0-9]+$/i.test(absoluteUrl)) {
|
||||
e.preventDefault();
|
||||
// invoke('open_browser', { url: absoluteUrl });
|
||||
invoke('download_file', {
|
||||
params: {
|
||||
url: absoluteUrl,
|
||||
filename: getFilenameFromUrl(absoluteUrl),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -132,3 +145,26 @@ function setDefaultZoom() {
|
||||
setZoom(htmlZoom);
|
||||
}
|
||||
}
|
||||
|
||||
function getFilenameFromUrl(url) {
|
||||
const urlPath = new URL(url).pathname;
|
||||
const filename = urlPath.substring(urlPath.lastIndexOf('/') + 1);
|
||||
return filename;
|
||||
}
|
||||
|
||||
function pakeToast(msg) {
|
||||
const m = document.createElement('div');
|
||||
m.innerHTML = msg;
|
||||
m.style.cssText =
|
||||
'max-width:60%;min-width: 80px;padding:0 12px;height: 32px;color: rgb(255, 255, 255);line-height: 32px;text-align: center;border-radius: 8px;position: fixed; bottom:24px;right: 28px;z-index: 999999;background: rgba(0, 0, 0,.8);font-size: 13px;';
|
||||
document.body.appendChild(m);
|
||||
setTimeout(function () {
|
||||
const d = 0.5;
|
||||
m.style.transition =
|
||||
'transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
|
||||
m.style.opacity = '0';
|
||||
setTimeout(function () {
|
||||
document.body.removeChild(m);
|
||||
}, d * 1000);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ mod app;
|
||||
mod util;
|
||||
|
||||
use app::{invoke, menu, window};
|
||||
use invoke::{download, drag_window, fullscreen, open_browser};
|
||||
use invoke::{download_file, drag_window, fullscreen, open_browser};
|
||||
use menu::{get_menu, menu_event_handle};
|
||||
use tauri_plugin_window_state::{Builder, StateFlags, WindowExt};
|
||||
use tauri_plugin_window_state::{Builder as windowStatePlugin, StateFlags, WindowExt};
|
||||
use util::{get_data_dir, get_pake_config};
|
||||
use window::get_window;
|
||||
|
||||
@@ -40,12 +40,12 @@ pub fn run_app() {
|
||||
}
|
||||
|
||||
tauri_app
|
||||
.plugin(Builder::default().build())
|
||||
.plugin(windowStatePlugin::default().build())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
drag_window,
|
||||
fullscreen,
|
||||
open_browser,
|
||||
download
|
||||
download_file
|
||||
])
|
||||
.setup(|app| {
|
||||
let _window = get_window(app, pake_config, data_dir);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use crate::app::config::PakeConfig;
|
||||
use dirs::config_dir;
|
||||
use libc::getenv;
|
||||
use std::ffi::CStr;
|
||||
use std::path::PathBuf;
|
||||
use tauri::Config;
|
||||
use tauri::{Config, Window};
|
||||
|
||||
pub fn get_pake_config() -> (PakeConfig, Config) {
|
||||
let pake_config: PakeConfig =
|
||||
@@ -27,3 +29,32 @@ pub fn get_data_dir(_tauri_config: Config) -> PathBuf {
|
||||
data_dir
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_toast(window: &Window, message: &str) {
|
||||
let script = format!(r#"pakeToast("{}");"#, message);
|
||||
window.eval(&script).unwrap();
|
||||
}
|
||||
|
||||
pub fn get_download_message() -> String {
|
||||
let lang_env_var = unsafe { CStr::from_ptr(getenv(b"LANG\0".as_ptr() as *const i8)) };
|
||||
let lang_str = lang_env_var.to_string_lossy().to_lowercase();
|
||||
if lang_str.starts_with("zh") {
|
||||
"下载成功,已保存到下载目录~".to_string()
|
||||
} else {
|
||||
"Download successful, saved to download directory~".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the file exists, if it exists, add a number to file name
|
||||
pub fn check_file_or_append(file_path: &str) -> String {
|
||||
let mut new_path = PathBuf::from(file_path);
|
||||
let mut counter = 1;
|
||||
while new_path.exists() {
|
||||
let file_stem = new_path.file_stem().unwrap().to_string_lossy().to_string();
|
||||
let extension = new_path.extension().unwrap().to_string_lossy().to_string();
|
||||
let parent_dir = new_path.parent().unwrap();
|
||||
new_path = parent_dir.join(format!("{}-{}.{}", file_stem, counter, extension));
|
||||
counter += 1;
|
||||
}
|
||||
new_path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user