change file/folder label when choosed

This commit is contained in:
ayabusa 2024-02-19 16:28:59 +01:00
parent ea2f60f3c2
commit 01d6db7368
3 changed files with 22 additions and 17 deletions

View File

@ -2,7 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use native_dialog::FileDialog; use native_dialog::FileDialog;
use tauri::{AppHandle, Manager}; use tauri::Manager;
use std::sync::Mutex; use std::sync::Mutex;
#[macro_use] #[macro_use]
@ -22,15 +22,16 @@ struct Payload {
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command] #[tauri::command]
async fn select_file_button(app: tauri::AppHandle) { async fn select_file_button(app: tauri::AppHandle) {
let _ = app.emit_all("my_event", ());
FILE_PATH.lock().unwrap().replace_range(.., &choose_file()); FILE_PATH.lock().unwrap().replace_range(.., &choose_file());
println!("{}",FILE_PATH.lock().unwrap()); println!("{}",FILE_PATH.lock().unwrap());
let _ = app.emit_all("file_path_changed", Payload { message: FILE_PATH.lock().unwrap().to_string() });
} }
#[tauri::command] #[tauri::command]
async fn select_folder_button() { async fn select_folder_button(app: tauri::AppHandle) {
FOLDER_PATH.lock().unwrap().replace_range(.., &choose_folder()); FOLDER_PATH.lock().unwrap().replace_range(.., &choose_folder());
println!("{}",FOLDER_PATH.lock().unwrap()); println!("{}",FOLDER_PATH.lock().unwrap());
let _ = app.emit_all("folder_path_changed", Payload { message: FOLDER_PATH.lock().unwrap().to_string() });
} }
#[tauri::command] #[tauri::command]
@ -44,7 +45,7 @@ fn choose_file() -> String{
let path = FileDialog::new() let path = FileDialog::new()
.show_open_single_file() .show_open_single_file()
.unwrap(); .unwrap();
format!("{:?}", path) // turn the FileDialog into a string format!("{:?}", path).replace("Some(\"", "").replace("\")", "") // turn the FileDialog into a string and remove Some("")
} }
fn choose_folder() -> String{ fn choose_folder() -> String{
@ -52,7 +53,7 @@ fn choose_folder() -> String{
let path = FileDialog::new() let path = FileDialog::new()
.show_open_single_dir() .show_open_single_dir()
.unwrap(); .unwrap();
format!("{:?}", path) // turn the FileDialog into a string format!("{:?}", path).replace("Some(\"", "").replace("\")", "") // turn the FileDialog into a string
} }
fn main() { fn main() {

View File

@ -2,15 +2,14 @@ const { listen } = window.__TAURI__.event
document.getElementById("fileLocation").innerHTML = "hey"; document.getElementById("fileLocation").innerHTML = "hey";
// listen to the `click` event and get a function to remove the event listener // listen for file location from rust backend
// there's also a `once` function that subscribes to an event and automatically unsubscribes the listener on the first event const unlistenfile = await listen('file_path_changed', (event) => {
const unlisten = await listen('my_event', (event) => { document.getElementById("fileLocation").innerHTML = event.payload.message;
// event.event is the event name (useful if you want to use a single callback fn for multiple event types) console.log("changing file label to : ", event.payload.message);
// event.payload is the payload object
document.getElementById("fileLocation").innerHTML = "hello";
}) })
// emits the `click` event with the object payload // listen for folder location from rust backend
emit('click', { const unlistenfolder = await listen('folder_path_changed', (event) => {
theMessage: 'Tauri is awesome!', document.getElementById("folderLocation").innerHTML = event.payload.message;
console.log("changing folder label to : ", event.payload.message);
}) })

View File

@ -16,8 +16,10 @@
<div class="main-container"> <div class="main-container">
<h3>Welcome to the Rusty slicer! <img src="assets/cat-vibe-vibe-cat.gif"></h1> <h3>Welcome to the Rusty slicer! <img src="assets/cat-vibe-vibe-cat.gif"></h1>
<div class="select-file"> <div class="select-file">
<p><b> Input file </b> <br></p> <div>
<p><b> Input file </b></p>
<p id="fileLocation"> no location choosen </p> <p id="fileLocation"> no location choosen </p>
</div>
<button id="fileButton">choose file 📂</button> <button id="fileButton">choose file 📂</button>
</div> </div>
@ -26,7 +28,10 @@
</div> </div>
<div class="select-file"> <div class="select-file">
<p><b> Output folder </b> <br> no location choosen </p> <div>
<p><b> Output folder </b> </p>
<p id="folderLocation"> no location choosen </p>
</div>
<button id="folderButton">choose folder 📂</button> <button id="folderButton">choose folder 📂</button>
</div> </div>