mirror of
https://github.com/Vomitblood/stort.git
synced 2024-11-26 13:55:27 +08:00
connected rust wallpaper functions with frontend
This commit is contained in:
parent
f8ac26c51d
commit
a379f74616
|
@ -91,3 +91,16 @@ pub fn copy_file(
|
||||||
// why need to use map???
|
// why need to use map???
|
||||||
std::fs::copy(target, &final_destination).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),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.invoke_handler(tauri::generate_handler![
|
.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!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|
|
@ -10,7 +10,7 @@ enum ImageType {
|
||||||
|
|
||||||
/// function to interface with the tauri api on the javascript side
|
/// function to interface with the tauri api on the javascript side
|
||||||
#[tauri::command]
|
#[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
|
// convert the string to a path
|
||||||
let file_path = std::path::Path::new(&file_path_string);
|
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)
|
crate::fs::copy_file(file_path, &destination_path, true)
|
||||||
.map_err(|e| format!("Failed to move file: {e}"))?;
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
import { DeleteOutline, FileOpenOutlined } from "@mui/icons-material";
|
import { DeleteOutline, FileOpenOutlined } from "@mui/icons-material";
|
||||||
import { Box, Button, Stack, TextField } from "@mui/material";
|
import { Box, Button, Stack, TextField } from "@mui/material";
|
||||||
import { open } from "@tauri-apps/api/dialog";
|
import { open } from "@tauri-apps/api/dialog";
|
||||||
|
import { invoke } from "@tauri-apps/api/tauri";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { FC } from "react";
|
import { FC } from "react";
|
||||||
import { stagedSettingsAtom } from "../../../../lib/store/jotai/settings";
|
import { stagedSettingsAtom } from "../../../../lib/store/jotai/settings";
|
||||||
import { CategoryTitle } from "../CategoryTitle";
|
import { CategoryTitle } from "../CategoryTitle";
|
||||||
import { SettingsItem } from "../SettingsItem";
|
import { SettingsItem } from "../SettingsItem";
|
||||||
|
import Path from "../../../../lib/path";
|
||||||
|
|
||||||
interface SettingsTabBackgroundProps {
|
interface SettingsTabBackgroundProps {
|
||||||
sx?: any;
|
sx?: any;
|
||||||
|
@ -31,7 +33,9 @@ export const SettingsTabBackground: FC<SettingsTabBackgroundProps> = ({ sx }) =>
|
||||||
};
|
};
|
||||||
|
|
||||||
const selectImage = async () => {
|
const selectImage = async () => {
|
||||||
const selected = await open({
|
const { basename } = await import("@tauri-apps/api/path");
|
||||||
|
|
||||||
|
let selectedFilePath = await open({
|
||||||
multiple: false,
|
multiple: false,
|
||||||
filters: [
|
filters: [
|
||||||
{
|
{
|
||||||
|
@ -41,8 +45,38 @@ export const SettingsTabBackground: FC<SettingsTabBackgroundProps> = ({ sx }) =>
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (selected) {
|
// if the user somehow manages to select multiple files, take the first file
|
||||||
console.log(selected);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { BugReport } from "@mui/icons-material";
|
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 { useRouter } from "next/router";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { SettingsItem } from "../components/HeaderBar/Settings/SettingsItem";
|
import { SettingsItem } from "../components/HeaderBar/Settings/SettingsItem";
|
||||||
import { useSettings } from "../contexts/SettingsContext";
|
import { useSettings } from "../contexts/SettingsContext";
|
||||||
import { testing } from "../lib/testing";
|
import { testing } from "../lib/testing";
|
||||||
import { invoke } from "@tauri-apps/api/tauri";
|
|
||||||
|
|
||||||
export default function Testing() {
|
export default function Testing() {
|
||||||
// contexts
|
// contexts
|
||||||
|
@ -49,9 +49,12 @@ export default function Testing() {
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
|
const bruh = settings.background.background_image_path;
|
||||||
|
console.log(bruh);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const bruh = await invoke("process_wallpaper_image", {
|
await invoke("delete_old_wallpaper_image", {
|
||||||
filePathString: "/home/vomitblood/Downloads/asasdf.gif",
|
filePathString: "/home/vomitblood/.local/share/stort/wallpaper.jpeg",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("deec nuts", error);
|
console.error("deec nuts", error);
|
||||||
|
|
Loading…
Reference in a new issue