setting ui

This commit is contained in:
cast1e
2024-10-16 20:50:22 +08:00
parent 31fcf4ea83
commit b8f1b81c0f
11 changed files with 225 additions and 75 deletions
+34 -31
View File
@@ -1,49 +1,52 @@
use libloading::{Library, Symbol};
use std::{ffi::CString, rc::Rc, sync::Arc};
use libloading::{self,Library};
use std::{ffi::CString, ops::Deref, sync::Arc};
use lazy_static::lazy_static;
struct CoreModule<'a>{
module:Arc<Library>,
set_wallpaper:Arc<Symbol<'a,unsafe extern "C" fn(*const i8) -> i8>>,
any_maximized:Arc<Symbol<'a,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<'a> CoreModule<'a> {
fn new(library_path: &str) -> Result<Self, libloading::Error> {
let lib = Arc::new(unsafe { Library::new(library_path).unwrap() });
let set_fn:&Symbol<'a,unsafe extern "C" fn(*const i8) -> i8> = unsafe {
&lib.get::<unsafe extern "C" fn(*const i8) -> i8>(b"set_wallpaper\0")?
};
let any_fn:&Symbol<'a,unsafe extern "C" fn() -> i8> = unsafe {
&lib.get::<unsafe extern "C" fn() -> i8>(b"any_maximized\0")?
};
Ok(CoreModule {
module: Arc::clone(&lib),
set_wallpaper:Arc::new(set_fn),
any_maximized:Arc::new(any_fn),
})
impl CoreModule {
fn new(library_path: &str) -> CoreModule {
// 首先加载动态库
let _module = Arc::new(unsafe { Library::new(library_path).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() };
// 返回初始化的结构体
CoreModule {
_module,
set_wallpaper,
any_maximized,
}
}
}
lazy_static!{
static ref core_module:CoreModule = CoreModule::new("wallitor-core.dll");
}
#[tauri::command]
pub async fn set_wallpaper(title: String) -> bool {
let lib = unsafe { Library::new("wallitor-core.dll").unwrap() };
type SetFn = unsafe extern "C" fn(*const i8) -> i8;
let set: Symbol<SetFn> = unsafe { lib.get(b"set_wallpaper\0").unwrap() };
let title = CString::new(title.to_string()).unwrap();
unsafe {
let res = set(title.as_ptr());
let res = core_module.set_wallpaper.deref()(title.as_ptr());
if res == 0 {
drop(lib);
return false;
};
}
drop(lib);
return true;
true
}
#[tauri::command]
pub async fn anyZoomed()->bool{
let Library
pub async fn any_zoomed()->bool{
unsafe {
let res = core_module.any_maximized.deref()();
if res == 0{
return false;
}
}
true
}
+2
View File
@@ -1,6 +1,7 @@
mod setup;
mod handler;
mod reader;
extern crate lazy_static;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
@@ -15,6 +16,7 @@ pub fn run() {
handler::wallpaper::new_wallpaper,
handler::apply::set_wallpaper,
handler::wallpaper::edit_wallpaper,
handler::apply::any_zoomed
])
.run(tauri::generate_context!())
.expect("error while running tauri application");