diff --git a/wallitor-core/dllmain.cpp b/wallitor-core/dllmain.cpp index 96df458..2dc9cb3 100644 --- a/wallitor-core/dllmain.cpp +++ b/wallitor-core/dllmain.cpp @@ -34,6 +34,6 @@ playerInstance* player_instance = NULL; return set_as_wallpaper(window_title); } - extern "C" __declspec(dllexport) BOOL anyMaximized() { + extern "C" __declspec(dllexport) BOOL any_maximized() { return !detectWindowMaximized(); } \ No newline at end of file diff --git a/wallitor-gui/src-tauri/Cargo.lock b/wallitor-gui/src-tauri/Cargo.lock index 6182b1e..32e881d 100644 --- a/wallitor-gui/src-tauri/Cargo.lock +++ b/wallitor-gui/src-tauri/Cargo.lock @@ -4048,6 +4048,7 @@ name = "wallitor-gui" version = "0.1.0" dependencies = [ "chrono", + "lazy_static", "libloading 0.8.5", "serde", "serde_json", diff --git a/wallitor-gui/src-tauri/Cargo.toml b/wallitor-gui/src-tauri/Cargo.toml index da1b191..403d361 100644 --- a/wallitor-gui/src-tauri/Cargo.toml +++ b/wallitor-gui/src-tauri/Cargo.toml @@ -25,4 +25,5 @@ tauri-plugin-fs = "2.0.0-rc" tauri-plugin-dialog = "2.0.0-rc" chrono = "0.4.38" libloading = "0.8.5" +lazy_static = "1.5.0" diff --git a/wallitor-gui/src-tauri/src/handler/apply.rs b/wallitor-gui/src-tauri/src/handler/apply.rs index f073398..b64afba 100644 --- a/wallitor-gui/src-tauri/src/handler/apply.rs +++ b/wallitor-gui/src-tauri/src/handler/apply.rs @@ -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, - set_wallpaper:Arc i8>>, - any_maximized:Arc i8>> +struct CoreModule{ + _module:Arc, + pub set_wallpaper:libloading::os::windows::Symbol i8>, + pub any_maximized:libloading::os::windows::Symbol i8> } -impl<'a> CoreModule<'a> { - fn new(library_path: &str) -> Result { - 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:: i8>(b"set_wallpaper\0")? - }; - let any_fn:&Symbol<'a,unsafe extern "C" fn() -> i8> = unsafe { - &lib.get:: 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 i8> = unsafe { _module.get:: i8>(b"set_wallpaper\0").unwrap().into_raw() }; + let any_maximized: libloading::os::windows::Symbol i8> = unsafe { _module.get:: 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 = 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 } diff --git a/wallitor-gui/src-tauri/src/lib.rs b/wallitor-gui/src-tauri/src/lib.rs index 4a019c6..109fb09 100644 --- a/wallitor-gui/src-tauri/src/lib.rs +++ b/wallitor-gui/src-tauri/src/lib.rs @@ -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"); diff --git a/wallitor-gui/src/assets/svgs/direction-left.svg b/wallitor-gui/src/assets/svgs/direction-left.svg index 1f8b0ad..24c1dc3 100644 --- a/wallitor-gui/src/assets/svgs/direction-left.svg +++ b/wallitor-gui/src/assets/svgs/direction-left.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/wallitor-gui/src/components/ApplyBar.vue b/wallitor-gui/src/components/ApplyBar.vue index c9f33f0..f8bdd99 100644 --- a/wallitor-gui/src/components/ApplyBar.vue +++ b/wallitor-gui/src/components/ApplyBar.vue @@ -107,11 +107,10 @@ function open(conFig: Cell) { } function apply() { - store.commit("apply_wallpaper", { + store.dispatch("apply_wallpaper", { url: `/video/?url=${cell.value.path}\\res\\${cell.value.config.info.entry_point}&mute=${cell.value.config.option.mute}`, title: "wallitor_video_playback" - }) - appWindow.minimize(); + }).then(()=>appWindow.minimize()) } diff --git a/wallitor-gui/src/components/ItemCard.vue b/wallitor-gui/src/components/ItemCard.vue index 90a0d32..1d3fcab 100644 --- a/wallitor-gui/src/components/ItemCard.vue +++ b/wallitor-gui/src/components/ItemCard.vue @@ -25,6 +25,10 @@ const props = defineProps({ \ No newline at end of file diff --git a/wallitor-gui/video/playback.ts b/wallitor-gui/video/playback.ts index 8dd25cf..17fdd91 100644 --- a/wallitor-gui/video/playback.ts +++ b/wallitor-gui/video/playback.ts @@ -1,7 +1,8 @@ import { invoke } from '@tauri-apps/api/core' +import { listen } from '@tauri-apps/api/event' import { getCurrentWindow } from '@tauri-apps/api/window' interface Params { - url?: string, + url?: string mute?: string } function getSearchParamsAsObject() { @@ -13,15 +14,73 @@ function getSearchParamsAsObject() { return paramsObject } +listen('video_pause', () => { + const player = document.getElementById('player') as HTMLVideoElement + if (player) player.pause() +}) + +class player{ + playing:boolean + params:Params + interval:number + constructor(params:Params) { + this.params = params; + this.playing = false; + this.interval = 0; + } + init_player(videoUrl:string){ + const player = document.getElementById('player') as HTMLVideoElement + if (player) { + player.src = videoUrl + player.play() + this.playing = true; + if (this.params.mute) { + player.muted = JSON.parse(this.params.mute) + } + player.loop = true + setTimeout(() => { + player.style.opacity = '1' + }, 0) + } + this.detect_zoom(); + } + detect_zoom(){ + setInterval(() => { + invoke('any_zoomed').then((res) => { + if (res as boolean) { + if(this.playing) { + const player = document.getElementById('player') as HTMLVideoElement + if (player) { + player.pause(); + this.playing = false; + } + } + } + else{ + if(!this.playing) { + const player = document.getElementById('player') as HTMLVideoElement + if (player) { + player.play(); + this.playing = true; + } + } + } + }) + }, 1000) + } +} + +let player_instance:player | null = null; + window.onload = () => { invoke('set_wallpaper', { title: 'wallitor_video_playback' }).then((res) => { - if(!res) { - console.error("Unable to set wallpaper."); - const win = getCurrentWindow(); - win.destroy(); - return; + if (!res) { + console.error('Unable to set wallpaper.') + const win = getCurrentWindow() + win.destroy() + return } const params: Params = getSearchParamsAsObject() if (params.url) { @@ -31,16 +90,8 @@ window.onload = () => { const binary_data_arr = new Uint8Array(res as number[]) const blob = new Blob([binary_data_arr], { type: 'video/mp4' }) const videoUrl = URL.createObjectURL(blob) - const player = document.getElementById('player') as HTMLVideoElement - if (player) { - player.src = videoUrl - player.play() - if(params.mute) { - player.muted = JSON.parse(params.mute); - } - player.loop = true - setTimeout(()=>{player.style.opacity = '1'},0); - } + player_instance = new player(params); + player_instance.init_player(videoUrl); }) } })