connected rust wallpaper functions with frontend

This commit is contained in:
Vomitblood 2024-08-08 11:56:54 +08:00
parent f8ac26c51d
commit a379f74616
5 changed files with 71 additions and 9 deletions

View file

@ -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),
))
}
}

View file

@ -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");

View file

@ -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<String, String> {
// 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(())
}

View file

@ -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<SettingsTabBackgroundProps> = ({ 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<SettingsTabBackgroundProps> = ({ 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);
}
}
};

View file

@ -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() {
</button>
<button
onClick={async () => {
const bruh = settings.background.background_image_path;
console.log(bruh);
try {
const bruh = await invoke("process_wallpaper_image", {
filePathString: "/home/vomitblood/Downloads/asasdf.gif",
await invoke("delete_old_wallpaper_image", {
filePathString: "/home/vomitblood/.local/share/stort/wallpaper.jpeg",
});
} catch (error) {
console.error("deec nuts", error);