From 5a48735f36236659049f7184bb66acddf446cb8a Mon Sep 17 00:00:00 2001 From: cast1e Date: Wed, 16 Oct 2024 12:39:30 +0800 Subject: [PATCH] settings --- .../src-tauri/src/handler/wallpaper.rs | 72 ++++++++++++++++--- .../src/assets/svgs/direction-left.svg | 1 + wallitor-gui/src/assets/svgs/edit.svg | 1 + wallitor-gui/src/components/ApplyBar.vue | 8 +-- wallitor-gui/src/components/EditItem.vue | 18 ++++- wallitor-gui/src/components/TitleBar.vue | 8 ++- wallitor-gui/src/router/index.ts | 9 +-- wallitor-gui/src/ts/types.d.ts | 1 + wallitor-gui/src/views/HomeView.vue | 4 +- wallitor-gui/src/views/SettingsView.vue | 28 ++++++++ 10 files changed, 127 insertions(+), 23 deletions(-) create mode 100644 wallitor-gui/src/assets/svgs/direction-left.svg create mode 100644 wallitor-gui/src/assets/svgs/edit.svg create mode 100644 wallitor-gui/src/views/SettingsView.vue diff --git a/wallitor-gui/src-tauri/src/handler/wallpaper.rs b/wallitor-gui/src-tauri/src/handler/wallpaper.rs index 9180249..fef5b9e 100644 --- a/wallitor-gui/src-tauri/src/handler/wallpaper.rs +++ b/wallitor-gui/src-tauri/src/handler/wallpaper.rs @@ -1,9 +1,8 @@ -use std::path::Path; -use std::fs; use chrono::Local; use serde::{Deserialize, Serialize}; use serde_json::{self, json}; - +use std::fs; +use std::path::Path; #[derive(Deserialize)] pub struct AddInfo { @@ -13,7 +12,7 @@ pub struct AddInfo { description: String, } -#[derive(Serialize,Deserialize)] +#[derive(Serialize, Deserialize)] struct Info { media_type: String, description: String, @@ -21,12 +20,12 @@ struct Info { entry_point: String, } -#[derive(Serialize,Deserialize)] +#[derive(Serialize, Deserialize)] struct Opt { mute: bool, } -#[derive(Serialize,Deserialize)] +#[derive(Serialize, Deserialize)] pub struct Config { name: String, info: Info, @@ -102,9 +101,64 @@ pub struct EditInfo { name: String, preview: String, description: String, + mute: bool, +} + +fn delete_preview(target_dir: &String) -> bool { + if let Ok(entries) = fs::read_dir(target_dir) { + for entry in entries { + if let Ok(entry) = entry { + let path = entry.path(); + if let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) { + if file_name == "preview" { + if fs::remove_file(path).is_err() { + return false; + } + } + } + } + } + } + return true; +} + +fn copy_preview(source_file: &String, target_dir: &String) -> bool { + let preview_path = Path::new(source_file); + if let Ok(true) = fs::exists(preview_path) { + if delete_preview(target_dir) { + if let Some(ext) = preview_path.extension().and_then(|f| f.to_str()) { + if fs::copy( + preview_path, + Path::new(&format!("{}/preview.{}", target_dir, ext)), + ) + .is_ok() + { + return true; + } + } + } + } + return false; } #[tauri::command] -pub async fn edit_wallpaper(title: String) -> bool { - true -} \ No newline at end of file +pub async fn edit_wallpaper(info: EditInfo) -> bool { + let folder_path = Path::new(&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); + if let Ok(file) = fs::read(config_path) { + let mut config: Config = serde_json::from_slice(&file).unwrap(); + config.info.description = info.description; + config.name = info.name; + config.option.mute = info.mute; + if let Ok(()) = fs::write(config_path, json!(config).to_string()) { + if !info.preview.is_empty() { + return copy_preview(&info.preview, &info.path); + } + return true; + } + } + } + false +} diff --git a/wallitor-gui/src/assets/svgs/direction-left.svg b/wallitor-gui/src/assets/svgs/direction-left.svg new file mode 100644 index 0000000..1f8b0ad --- /dev/null +++ b/wallitor-gui/src/assets/svgs/direction-left.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/wallitor-gui/src/assets/svgs/edit.svg b/wallitor-gui/src/assets/svgs/edit.svg new file mode 100644 index 0000000..6e50829 --- /dev/null +++ b/wallitor-gui/src/assets/svgs/edit.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/wallitor-gui/src/components/ApplyBar.vue b/wallitor-gui/src/components/ApplyBar.vue index 32b9ea0..c9f33f0 100644 --- a/wallitor-gui/src/components/ApplyBar.vue +++ b/wallitor-gui/src/components/ApplyBar.vue @@ -67,7 +67,7 @@ const props = defineProps({ }) const store = useStore(); const visible = defineModel(); -const emit = defineEmits(["submit"]); +defineEmits(["submit"]); const visible_ = ref(false); const cell = ref({ img: "", @@ -107,9 +107,9 @@ function open(conFig: Cell) { } function apply() { - store.commit("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" + store.commit("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(); } diff --git a/wallitor-gui/src/components/EditItem.vue b/wallitor-gui/src/components/EditItem.vue index a73ce5c..fe07766 100644 --- a/wallitor-gui/src/components/EditItem.vue +++ b/wallitor-gui/src/components/EditItem.vue @@ -13,9 +13,16 @@ -