Rusty-slicer/src-tauri/src/main.rs

66 lines
2.2 KiB
Rust
Raw Normal View History

2024-02-14 16:01:21 +00:00
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
2024-02-18 18:51:53 +00:00
use native_dialog::FileDialog;
2024-02-19 15:28:59 +00:00
use tauri::Manager;
2024-02-18 18:51:53 +00:00
use std::sync::Mutex;
#[macro_use]
extern crate lazy_static;
// define file and folder path variable, don't know if it's the right way of doing it
lazy_static! {
static ref FILE_PATH: Mutex<String> = Mutex::new("".to_string());
static ref FOLDER_PATH: Mutex<String> = Mutex::new("".to_string());
}
#[derive(Clone, serde::Serialize)]
struct Payload {
message: String,
}
2024-02-14 16:01:21 +00:00
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
2024-02-18 18:51:53 +00:00
#[tauri::command]
async fn select_file_button(app: tauri::AppHandle) {
FILE_PATH.lock().unwrap().replace_range(.., &choose_file());
println!("{}",FILE_PATH.lock().unwrap());
2024-02-19 15:28:59 +00:00
let _ = app.emit_all("file_path_changed", Payload { message: FILE_PATH.lock().unwrap().to_string() });
2024-02-14 16:01:21 +00:00
}
#[tauri::command]
2024-02-19 15:28:59 +00:00
async fn select_folder_button(app: tauri::AppHandle) {
2024-02-18 18:51:53 +00:00
FOLDER_PATH.lock().unwrap().replace_range(.., &choose_folder());
println!("{}",FOLDER_PATH.lock().unwrap());
2024-02-19 15:28:59 +00:00
let _ = app.emit_all("folder_path_changed", Payload { message: FOLDER_PATH.lock().unwrap().to_string() });
2024-02-18 18:51:53 +00:00
}
#[tauri::command]
fn debug_call(message: &str){
println!("[DBG] {}", message);
}
// prompt user file chooser using native_dialogue crate
fn choose_file() -> String{
println!("Let's choose a file !");
let path = FileDialog::new()
.show_open_single_file()
.unwrap();
2024-02-19 15:28:59 +00:00
format!("{:?}", path).replace("Some(\"", "").replace("\")", "") // turn the FileDialog into a string and remove Some("")
2024-02-18 18:51:53 +00:00
}
fn choose_folder() -> String{
println!("Let's choose a folder !");
let path = FileDialog::new()
.show_open_single_dir()
.unwrap();
2024-02-19 15:28:59 +00:00
format!("{:?}", path).replace("Some(\"", "").replace("\")", "") // turn the FileDialog into a string
}
2024-02-14 16:01:21 +00:00
fn main() {
2024-02-18 18:51:53 +00:00
// generate the tauri app
2024-02-14 16:01:21 +00:00
tauri::Builder::default()
2024-02-18 18:51:53 +00:00
.invoke_handler(tauri::generate_handler![select_file_button, select_folder_button, debug_call])
2024-02-14 16:01:21 +00:00
.run(tauri::generate_context!())
.expect("error while running tauri application");
}