single instance & bug fixes

This commit is contained in:
cast1e
2024-10-17 22:13:22 +08:00
parent c7ad45ebbb
commit c71ed9a982
15 changed files with 145 additions and 186 deletions
+46
View File
@@ -103,6 +103,30 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "async-executor"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ca9a001c1e8ba5149f91a74362376cc6bc5b919d92d988668657bd570bdcec"
dependencies = [
"async-task",
"concurrent-queue",
"fastrand",
"futures-lite",
"slab",
]
[[package]]
name = "async-fs"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcd09b382f40fcd159c2d695175b2ae620ffa5f3bd6f664131efff4e8b9e04a"
dependencies = [
"async-lock",
"blocking",
"futures-lite",
]
[[package]]
name = "async-io"
version = "2.3.4"
@@ -3685,6 +3709,21 @@ dependencies = [
"tokio",
]
[[package]]
name = "tauri-plugin-single-instance"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a25ac834491d089699a2bc9266a662faf373c9f779f05a2235bc6e4d9e61769a"
dependencies = [
"log",
"serde",
"serde_json",
"tauri",
"thiserror",
"windows-sys 0.59.0",
"zbus",
]
[[package]]
name = "tauri-runtime"
version = "2.1.0"
@@ -4213,6 +4252,7 @@ dependencies = [
"tauri-plugin-dialog",
"tauri-plugin-fs",
"tauri-plugin-shell",
"tauri-plugin-single-instance",
"window-vibrancy",
]
@@ -4866,9 +4906,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b8e3d6ae3342792a6cc2340e4394334c7402f3d793b390d2c5494a4032b3030"
dependencies = [
"async-broadcast",
"async-executor",
"async-fs",
"async-io",
"async-lock",
"async-process",
"async-recursion",
"async-task",
"async-trait",
"blocking",
"derivative",
"enumflags2",
"event-listener",
+2
View File
@@ -29,4 +29,6 @@ lazy_static = "1.5.0"
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-autostart = "2.0.0-rc"
tauri-plugin-single-instance = "2.0.0-rc"
+25 -13
View File
@@ -1,20 +1,32 @@
use libloading::{self,Library};
use std::{ffi::CString, ops::Deref, sync::Arc};
use crate::handler::get_absolute_path;
use lazy_static::lazy_static;
use libloading::{self, Library};
use std::{ffi::CString, ops::Deref, sync::Arc};
struct CoreModule{
_module:Arc<Library>,
pub set_wallpaper:libloading::os::windows::Symbol<unsafe extern "C" fn(*const i8) -> i8>,
pub any_maximized:libloading::os::windows::Symbol<unsafe extern "C" fn() -> i8>
struct CoreModule {
_module: Arc<Library>,
pub set_wallpaper: libloading::os::windows::Symbol<unsafe extern "C" fn(*const i8) -> i8>,
pub any_maximized: libloading::os::windows::Symbol<unsafe extern "C" fn() -> i8>,
}
impl CoreModule {
fn new(library_path: &str) -> CoreModule {
let library_dir = get_absolute_path(&String::from(library_path));
// 首先加载动态库
let _module = Arc::new(unsafe { Library::new(library_path).unwrap()});
let _module = Arc::new(unsafe { Library::new(library_dir).unwrap() });
// 然后获取符号
let set_wallpaper: libloading::os::windows::Symbol<unsafe extern "C" fn(*const i8) -> i8> = unsafe { _module.get::<unsafe extern "C" fn(*const i8) -> i8>(b"set_wallpaper\0").unwrap().into_raw() };
let any_maximized: libloading::os::windows::Symbol<unsafe extern "C" fn() -> i8> = unsafe { _module.get::<unsafe extern "C" fn() -> i8>(b"any_maximized\0").unwrap().into_raw() };
let set_wallpaper: libloading::os::windows::Symbol<unsafe extern "C" fn(*const i8) -> i8> = unsafe {
_module
.get::<unsafe extern "C" fn(*const i8) -> i8>(b"set_wallpaper\0")
.unwrap()
.into_raw()
};
let any_maximized: libloading::os::windows::Symbol<unsafe extern "C" fn() -> i8> = unsafe {
_module
.get::<unsafe extern "C" fn() -> i8>(b"any_maximized\0")
.unwrap()
.into_raw()
};
// 返回初始化的结构体
CoreModule {
_module,
@@ -24,8 +36,8 @@ impl CoreModule {
}
}
lazy_static!{
static ref core_module:CoreModule = CoreModule::new("wallitor-core.dll");
lazy_static! {
static ref core_module: CoreModule = CoreModule::new("wallitor-core.dll");
}
#[tauri::command]
@@ -41,10 +53,10 @@ pub async fn set_wallpaper(title: String) -> bool {
}
#[tauri::command]
pub async fn any_zoomed()->bool{
pub async fn any_zoomed() -> bool {
unsafe {
let res = core_module.any_maximized.deref()();
if res == 0{
if res == 0 {
return false;
}
}
+14 -12
View File
@@ -1,3 +1,4 @@
use crate::handler::get_absolute_path;
use crate::{reader, VERSION};
use serde::{Deserialize, Serialize};
use serde_json::json;
@@ -7,9 +8,9 @@ use tauri::ipc::Response;
#[tauri::command]
pub async fn get_file(path: String) -> Response {
let p = Path::new(&path);
if let Ok(true) = fs::exists(p) {
let data: Vec<u8> = fs::read(p).unwrap();
let p = get_absolute_path(&path);
if let Ok(true) = fs::exists(&p) {
let data: Vec<u8> = fs::read(&p).unwrap();
return tauri::ipc::Response::new(data);
}
tauri::ipc::Response::new(String::from(""))
@@ -18,12 +19,12 @@ pub async fn get_file(path: String) -> Response {
#[tauri::command]
pub async fn read_resource_dir() -> String {
let mut file_map = reader::FileMap::new();
let path = Path::new(".\\resource");
if let Ok(false) = fs::exists(path) {
fs::create_dir(path).expect("Can't create dir");
let path = get_absolute_path(&String::from(".\\resource"));
if let Ok(false) = fs::exists(&path) {
fs::create_dir(&path).expect("Can't create dir");
}
file_map
.read_resourse_directory(path)
.read_resourse_directory(&path)
.expect("Can't read dir");
serde_json::to_string(&file_map).unwrap()
}
@@ -32,9 +33,10 @@ pub async fn read_resource_dir() -> String {
pub async fn del_folder(path: String) -> bool {
let p = Path::new(&path);
if p.starts_with(".\\") {
if p.is_dir() {
if let Ok(true) = fs::exists(p) {
if fs::remove_dir_all(p).is_ok() {
let folder_path = get_absolute_path(&path);
if folder_path.is_dir() {
if let Ok(true) = fs::exists(&folder_path) {
if fs::remove_dir_all(&folder_path).is_ok() {
return true;
}
}
@@ -66,7 +68,7 @@ static SETTING_PATH: &str = "./settings.json";
#[tauri::command]
pub async fn get_settings() -> String {
let setting_path = Path::new(SETTING_PATH);
let setting_path = get_absolute_path(&String::from(SETTING_PATH));
if let Ok(file) = fs::read(setting_path) {
let mut settings: Settings = serde_json::from_slice(&file).unwrap();
settings.version = String::from(VERSION);
@@ -79,7 +81,7 @@ pub async fn get_settings() -> String {
#[tauri::command]
pub async fn set_settings(settings: Settings) -> bool {
let setting_path = Path::new(SETTING_PATH);
let setting_path = get_absolute_path(&String::from(SETTING_PATH));
if fs::write(setting_path, json!(settings).to_string()).is_ok() {
return true;
}
+9 -1
View File
@@ -1,3 +1,11 @@
pub mod apply;
pub mod file;
pub mod wallpaper;
pub mod apply;
use std::path::{Path, PathBuf};
pub fn get_absolute_path(path: &String) -> PathBuf {
let binding = std::env::current_exe().unwrap();
let base_dir = binding.parent().unwrap();
return base_dir.join(Path::new(path));
}
@@ -1,3 +1,4 @@
use crate::handler::get_absolute_path;
use chrono::Local;
use serde::{Deserialize, Serialize};
use serde_json::{self, json};
@@ -34,7 +35,8 @@ pub struct Config {
#[tauri::command]
pub async fn new_wallpaper(info: AddInfo) -> String {
let base_url = String::from("./resource");
let base_path = get_absolute_path(&String::from("./resource"));
let base_url = base_path.to_str().unwrap();
let current_time: i64 = Local::now().timestamp();
let folder = format!("{}/{}", base_url, current_time);
if fs::create_dir_all(Path::new(&folder)).is_err() {
@@ -143,10 +145,10 @@ fn copy_preview(source_file: &String, target_dir: &String) -> bool {
#[tauri::command]
pub async fn edit_wallpaper(info: EditInfo) -> bool {
let folder_path = Path::new(&info.path);
let folder_path = &get_absolute_path(&info.path);
if let Ok(true) = fs::exists(folder_path) {
let config_path_str = format!("{}/config.json", info.path);
let config_path = Path::new(&config_path_str);
let config_path = &get_absolute_path(&config_path_str);
if let Ok(file) = fs::read(config_path) {
let mut config: Config = serde_json::from_slice(&file).unwrap();
config.info.description = info.description;
+9 -1
View File
@@ -2,6 +2,7 @@ mod handler;
mod reader;
mod setup;
extern crate lazy_static;
use tauri::Manager;
use tauri_plugin_autostart::MacosLauncher;
static VERSION: &str = "1.0.0";
@@ -10,11 +11,18 @@ static VERSION: &str = "1.0.0";
pub fn run() {
tauri::Builder::default()
.setup(setup::init)
.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
let window = app.get_webview_window("main").expect("no main window");
if let Ok(true) = window.is_minimized() {
let _ = window.unminimize();
}
let _ = window.set_focus();
}))
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
Some(vec!["--flag1", "--flag2"]),
None,
))
.invoke_handler(tauri::generate_handler![
handler::file::get_file,
+24 -18
View File
@@ -1,11 +1,11 @@
use serde::{Serialize, Serializer};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use serde::{ Serialize,Serializer};
use std::collections::HashMap;
enum FileType {
File(String),
Directory(HashMap<String,FileType>)
Directory(HashMap<String, FileType>),
}
impl Serialize for FileType {
@@ -14,43 +14,49 @@ impl Serialize for FileType {
S: Serializer,
{
match self {
FileType::File(file) => serializer.serialize_str(file), // 文件只序列化路径
FileType::Directory(dir) => dir.serialize(serializer), // 目录序列化为HashMap
FileType::File(file) => serializer.serialize_str(file), // 文件只序列化路径
FileType::Directory(dir) => dir.serialize(serializer), // 目录序列化为HashMap
}
}
}
#[derive(Serialize)]
pub struct FileMap {
files: HashMap<String, FileType>, // 用于存储文件名和路径
files: HashMap<String, FileType>, // 用于存储文件名和路径
}
impl FileMap {
pub fn new()->FileMap{
FileMap{
files:HashMap::new()
pub fn new() -> FileMap {
FileMap {
files: HashMap::new(),
}
}
fn read_resource(&self,p:&Path)->std::io::Result<HashMap<String,FileType>>{
let mut files:HashMap<String,FileType> = HashMap::new();
for entry in fs::read_dir(p)?{
fn read_resource(&self, p: &Path) -> std::io::Result<HashMap<String, FileType>> {
let mut files: HashMap<String, FileType> = HashMap::new();
for entry in fs::read_dir(p)? {
let dir = entry?;
let path = dir.path();
if path.is_file() {
files.insert(path.file_name().unwrap().to_string_lossy().to_string(), FileType::File(path.to_string_lossy().to_string()));
files.insert(
path.file_name().unwrap().to_string_lossy().to_string(),
FileType::File(path.to_string_lossy().to_string()),
);
}
}
Ok(files)
}
pub fn read_resourse_directory(&mut self,p:&Path)->std::io::Result<()>{
for entry in fs::read_dir(p)?{
pub fn read_resourse_directory(&mut self, p: &Path) -> std::io::Result<()> {
for entry in fs::read_dir(p)? {
let dir = entry?;
let path = dir.path();
if path.is_dir() {
let resource = self.read_resource(path.as_path())?;
self.files.insert(path.to_string_lossy().to_string(), FileType::Directory(resource)) ;
let resource = self.read_resource(path.as_path())?;
self.files.insert(
path.to_string_lossy().to_string(),
FileType::Directory(resource),
);
}
}
Ok(())