From a379f74616815fa8b54f6d04be20a696668b4278 Mon Sep 17 00:00:00 2001 From: Vomitblood Date: Thu, 8 Aug 2024 11:56:54 +0800 Subject: [PATCH] connected rust wallpaper functions with frontend --- src-tauri/src/fs.rs | 13 ++++++ src-tauri/src/main.rs | 3 +- src-tauri/src/wallpaper.rs | 13 +++++- .../SettingsTabs/SettingsTabBackground.tsx | 40 +++++++++++++++++-- src/pages/testing.tsx | 11 +++-- 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src-tauri/src/fs.rs b/src-tauri/src/fs.rs index d98d382..0fb858d 100644 --- a/src-tauri/src/fs.rs +++ b/src-tauri/src/fs.rs @@ -91,3 +91,16 @@ pub fn copy_file( // why need to use map??? std::fs::copy(target, &final_destination).map(|_| ()) } + +pub fn delete_file(file_path: &std::path::Path) -> std::io::Result<()> { + // attempt to delete the file + if file_path.exists() { + std::fs::remove_file(file_path)?; + Ok(()) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + format!("File not found: {:?}", file_path), + )) + } +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 77073e8..b256508 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,7 +4,8 @@ fn main() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![ - app::wallpaper::process_wallpaper_image + app::wallpaper::process_wallpaper_image, + app::wallpaper::delete_old_wallpaper_image, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/wallpaper.rs b/src-tauri/src/wallpaper.rs index c9d1849..c2066e3 100644 --- a/src-tauri/src/wallpaper.rs +++ b/src-tauri/src/wallpaper.rs @@ -10,7 +10,7 @@ enum ImageType { /// function to interface with the tauri api on the javascript side #[tauri::command] -pub fn process_wallpaper_image(file_path_string: String) -> Result<(), String> { +pub fn process_wallpaper_image(file_path_string: String) -> Result { // convert the string to a path let file_path = std::path::Path::new(&file_path_string); @@ -39,6 +39,17 @@ pub fn process_wallpaper_image(file_path_string: String) -> Result<(), String> { crate::fs::copy_file(file_path, &destination_path, true) .map_err(|e| format!("Failed to move file: {e}"))?; + Ok(destination_path.to_string_lossy().to_string()) +} + +#[tauri::command] +pub fn delete_old_wallpaper_image(file_path_string: String) -> Result<(), String> { + // convert the strings to paths + let file_path: &std::path::Path = std::path::Path::new(&file_path_string); + + // delete the old wallpaper + crate::fs::delete_file(file_path).map_err(|e| format!("Failed to delete file: {e}"))?; + Ok(()) } diff --git a/src/components/HeaderBar/Settings/SettingsTabs/SettingsTabBackground.tsx b/src/components/HeaderBar/Settings/SettingsTabs/SettingsTabBackground.tsx index 69b70bb..df390db 100644 --- a/src/components/HeaderBar/Settings/SettingsTabs/SettingsTabBackground.tsx +++ b/src/components/HeaderBar/Settings/SettingsTabs/SettingsTabBackground.tsx @@ -1,12 +1,14 @@ import { DeleteOutline, FileOpenOutlined } from "@mui/icons-material"; import { Box, Button, Stack, TextField } from "@mui/material"; import { open } from "@tauri-apps/api/dialog"; +import { invoke } from "@tauri-apps/api/tauri"; import { useAtom } from "jotai"; import Image from "next/image"; import { FC } from "react"; import { stagedSettingsAtom } from "../../../../lib/store/jotai/settings"; import { CategoryTitle } from "../CategoryTitle"; import { SettingsItem } from "../SettingsItem"; +import Path from "../../../../lib/path"; interface SettingsTabBackgroundProps { sx?: any; @@ -31,7 +33,9 @@ export const SettingsTabBackground: FC = ({ sx }) => }; const selectImage = async () => { - const selected = await open({ + const { basename } = await import("@tauri-apps/api/path"); + + let selectedFilePath = await open({ multiple: false, filters: [ { @@ -41,8 +45,38 @@ export const SettingsTabBackground: FC = ({ sx }) => ], }); - if (selected) { - console.log(selected); + // if the user somehow manages to select multiple files, take the first file + if (Array.isArray(selectedFilePath)) { + selectedFilePath = selectedFilePath[0]; + } + + if (selectedFilePath) { + try { + // get the last filename from the path + const filename = await basename(selectedFilePath); + + console.log(filename); + console.log(selectedFilePath); + + // if there is already a wallpaper file, delete it + if (stagedSettings.background.background_image_path) { + try { + await invoke("delete_old_wallpaper_image", { + filePathString: stagedSettings.background.background_image_path, + }); + } catch (error) { + console.error("Failed to delete old wallpaper image", error); + } + } + + const destinationFilePath = (await invoke("process_wallpaper_image", { + filePathString: selectedFilePath, + })) as string; + + handleSettingsBackgroundValueChange("background_image_path", destinationFilePath); + } catch (error) { + console.error("deec nuts", error); + } } }; diff --git a/src/pages/testing.tsx b/src/pages/testing.tsx index 6dced9d..ee75894 100644 --- a/src/pages/testing.tsx +++ b/src/pages/testing.tsx @@ -1,11 +1,11 @@ import { BugReport } from "@mui/icons-material"; -import { Box, Button, IconButton, TextField, Typography } from "@mui/material"; +import { Box, IconButton, TextField, Typography } from "@mui/material"; +import { invoke } from "@tauri-apps/api/tauri"; import { useRouter } from "next/router"; import { useState } from "react"; import { SettingsItem } from "../components/HeaderBar/Settings/SettingsItem"; import { useSettings } from "../contexts/SettingsContext"; import { testing } from "../lib/testing"; -import { invoke } from "@tauri-apps/api/tauri"; export default function Testing() { // contexts @@ -49,9 +49,12 @@ export default function Testing() {