refactor: 升级 tauri 到 2.x
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use crate::util::{check_file_or_append, get_download_message, show_toast, MessageType};
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
use tauri::api::http::{ClientBuilder, HttpRequestBuilder, ResponseType};
|
||||
use tauri::{api, command, AppHandle, Manager, Window};
|
||||
use std::str::FromStr;
|
||||
use tauri::http::Method;
|
||||
use tauri_plugin_http::reqwest::{ClientBuilder, Request};
|
||||
// use tauri::http::{ClientBuilder, HttpRequestBuilder, ResponseType};
|
||||
use tauri::{command, AppHandle, Manager, Url, WebviewWindow};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DownloadFileParams {
|
||||
@@ -18,24 +21,22 @@ pub struct BinaryDownloadParams {
|
||||
|
||||
#[command]
|
||||
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
|
||||
let window: Window = app.get_window("pake").unwrap();
|
||||
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
|
||||
show_toast(&window, &get_download_message(MessageType::Start));
|
||||
|
||||
let output_path = api::path::download_dir().unwrap().join(params.filename);
|
||||
let output_path = app.path().download_dir().unwrap().join(params.filename);
|
||||
let file_path = check_file_or_append(output_path.to_str().unwrap());
|
||||
let client = ClientBuilder::new().build().unwrap();
|
||||
|
||||
let response = client
|
||||
.send(
|
||||
HttpRequestBuilder::new("GET", ¶ms.url)
|
||||
.unwrap()
|
||||
.response_type(ResponseType::Binary),
|
||||
.execute(
|
||||
Request::new(Method::GET, Url::from_str(¶ms.url).unwrap())
|
||||
)
|
||||
.await;
|
||||
|
||||
match response {
|
||||
Ok(res) => {
|
||||
let bytes = res.bytes().await.unwrap().data;
|
||||
let bytes = res.bytes().await.unwrap();
|
||||
|
||||
let mut file = File::create(file_path).unwrap();
|
||||
file.write_all(&bytes).unwrap();
|
||||
@@ -54,9 +55,9 @@ pub async fn download_file_by_binary(
|
||||
app: AppHandle,
|
||||
params: BinaryDownloadParams,
|
||||
) -> Result<(), String> {
|
||||
let window: Window = app.get_window("pake").unwrap();
|
||||
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
|
||||
show_toast(&window, &get_download_message(MessageType::Start));
|
||||
let output_path = api::path::download_dir().unwrap().join(params.filename);
|
||||
let output_path = app.path().download_dir().unwrap().join(params.filename);
|
||||
let file_path = check_file_or_append(output_path.to_str().unwrap());
|
||||
let download_file_result = fs::write(file_path, ¶ms.binary);
|
||||
match download_file_result {
|
||||
|
||||
@@ -1,31 +1,30 @@
|
||||
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
|
||||
use tauri::{
|
||||
menu::{MenuBuilder, MenuItemBuilder}, tray::TrayIconBuilder, AppHandle, Manager
|
||||
};
|
||||
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
|
||||
|
||||
pub fn get_system_tray() -> SystemTray {
|
||||
let hide_app = CustomMenuItem::new("hide_app".to_string(), "Hide");
|
||||
let show_app = CustomMenuItem::new("show_app".to_string(), "Show");
|
||||
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
|
||||
let tray_menu = SystemTrayMenu::new()
|
||||
.add_item(show_app)
|
||||
.add_item(hide_app)
|
||||
.add_item(quit);
|
||||
SystemTray::new().with_menu(tray_menu)
|
||||
}
|
||||
pub fn set_system_tray(app: &AppHandle) -> tauri::Result<()> {
|
||||
let hide_app = MenuItemBuilder::with_id("hide_app", "Hide").build(app)?;
|
||||
let show_app = MenuItemBuilder::with_id("show_app", "Show").build(app)?;
|
||||
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
|
||||
let menu = MenuBuilder::new(app).items(&[&hide_app, &show_app, &quit]).build()?;
|
||||
|
||||
pub fn system_tray_handle(app: &tauri::AppHandle, event: SystemTrayEvent) {
|
||||
if let SystemTrayEvent::MenuItemClick { tray_id: _, id, .. } = event {
|
||||
match id.as_str() {
|
||||
TrayIconBuilder::new()
|
||||
.menu(&menu)
|
||||
.on_menu_event(move |app, event| match event.id().as_ref() {
|
||||
"hide_app" => {
|
||||
app.get_window("pake").unwrap().minimize().unwrap();
|
||||
app.get_webview_window("pake").unwrap().minimize().unwrap();
|
||||
}
|
||||
"show_app" => {
|
||||
app.get_window("pake").unwrap().show().unwrap();
|
||||
app.get_webview_window("pake").unwrap().show().unwrap();
|
||||
}
|
||||
"quit" => {
|
||||
let _res = app.save_window_state(StateFlags::all());
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
_ => (),
|
||||
})
|
||||
.build(app)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::app::config::PakeConfig;
|
||||
use std::path::PathBuf;
|
||||
use tauri::{App, Window, WindowBuilder, WindowUrl};
|
||||
use tauri::{App, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
use tauri::TitleBarStyle;
|
||||
|
||||
pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Window {
|
||||
pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> WebviewWindow {
|
||||
let window_config = config
|
||||
.windows
|
||||
.first()
|
||||
@@ -14,8 +14,8 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Wind
|
||||
let user_agent = config.user_agent.get();
|
||||
|
||||
let url = match window_config.url_type.as_str() {
|
||||
"web" => WindowUrl::App(window_config.url.parse().unwrap()),
|
||||
"local" => WindowUrl::App(PathBuf::from(&window_config.url)),
|
||||
"web" => WebviewUrl::App(window_config.url.parse().unwrap()),
|
||||
"local" => WebviewUrl::App(PathBuf::from(&window_config.url)),
|
||||
_ => panic!("url type can only be web or local"),
|
||||
};
|
||||
|
||||
@@ -24,14 +24,14 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Wind
|
||||
serde_json::to_string(&window_config).unwrap()
|
||||
);
|
||||
|
||||
let mut window_builder = WindowBuilder::new(app, "pake", url)
|
||||
let mut window_builder = WebviewWindowBuilder::new(app, "pake", url)
|
||||
.title("")
|
||||
.user_agent(user_agent)
|
||||
.visible(false) // Prevent initial shaking
|
||||
.resizable(window_config.resizable)
|
||||
.fullscreen(window_config.fullscreen)
|
||||
.inner_size(window_config.width, window_config.height)
|
||||
.disable_file_drop_handler()
|
||||
.disable_drag_drop_handler()
|
||||
.always_on_top(window_config.always_on_top)
|
||||
.initialization_script(&config_script)
|
||||
.initialization_script(include_str!("../inject/component.js"))
|
||||
|
||||
Reference in New Issue
Block a user