From 01d6db7368bd10abfc226a20d9007e0956d5f448 Mon Sep 17 00:00:00 2001 From: ayabusa Date: Mon, 19 Feb 2024 16:28:59 +0100 Subject: [PATCH] change file/folder label when choosed --- src-tauri/src/main.rs | 11 ++++++----- src/event_handler.js | 17 ++++++++--------- src/index.html | 11 ++++++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 0d2894f..4636741 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,7 +2,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use native_dialog::FileDialog; -use tauri::{AppHandle, Manager}; +use tauri::Manager; use std::sync::Mutex; #[macro_use] @@ -22,15 +22,16 @@ struct Payload { // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command #[tauri::command] async fn select_file_button(app: tauri::AppHandle) { - let _ = app.emit_all("my_event", ()); FILE_PATH.lock().unwrap().replace_range(.., &choose_file()); println!("{}",FILE_PATH.lock().unwrap()); + let _ = app.emit_all("file_path_changed", Payload { message: FILE_PATH.lock().unwrap().to_string() }); } #[tauri::command] -async fn select_folder_button() { +async fn select_folder_button(app: tauri::AppHandle) { FOLDER_PATH.lock().unwrap().replace_range(.., &choose_folder()); println!("{}",FOLDER_PATH.lock().unwrap()); + let _ = app.emit_all("folder_path_changed", Payload { message: FOLDER_PATH.lock().unwrap().to_string() }); } #[tauri::command] @@ -44,7 +45,7 @@ fn choose_file() -> String{ let path = FileDialog::new() .show_open_single_file() .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{ @@ -52,7 +53,7 @@ fn choose_folder() -> String{ let path = FileDialog::new() .show_open_single_dir() .unwrap(); - format!("{:?}", path) // turn the FileDialog into a string + format!("{:?}", path).replace("Some(\"", "").replace("\")", "") // turn the FileDialog into a string } fn main() { diff --git a/src/event_handler.js b/src/event_handler.js index 4814592..351a683 100644 --- a/src/event_handler.js +++ b/src/event_handler.js @@ -2,15 +2,14 @@ const { listen } = window.__TAURI__.event document.getElementById("fileLocation").innerHTML = "hey"; -// listen to the `click` event and get a function to remove the event listener -// there's also a `once` function that subscribes to an event and automatically unsubscribes the listener on the first event -const unlisten = await listen('my_event', (event) => { - // event.event is the event name (useful if you want to use a single callback fn for multiple event types) - // event.payload is the payload object - document.getElementById("fileLocation").innerHTML = "hello"; +// listen for file location from rust backend +const unlistenfile = await listen('file_path_changed', (event) => { + document.getElementById("fileLocation").innerHTML = event.payload.message; + console.log("changing file label to : ", event.payload.message); }) -// emits the `click` event with the object payload -emit('click', { - theMessage: 'Tauri is awesome!', +// listen for folder location from rust backend +const unlistenfolder = await listen('folder_path_changed', (event) => { + document.getElementById("folderLocation").innerHTML = event.payload.message; + console.log("changing folder label to : ", event.payload.message); }) \ No newline at end of file diff --git a/src/index.html b/src/index.html index fb8c5b1..1ec3f71 100644 --- a/src/index.html +++ b/src/index.html @@ -16,8 +16,10 @@

Welcome to the Rusty slicer!

-

Input file

-

no location choosen

+
+

Input file

+

no location choosen

+
@@ -26,7 +28,10 @@
-

Output folder
no location choosen

+
+

Output folder

+

no location choosen

+