From 06e69c8816ec993c6ad9bf8dc4bb0db2e194fd4c Mon Sep 17 00:00:00 2001 From: Ryan Killeen Date: Tue, 22 Mar 2022 21:02:02 -0400 Subject: [PATCH 1/3] fix(settings): safeguard default path for file select --- src/forms/workflow-action.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/forms/workflow-action.tsx b/src/forms/workflow-action.tsx index d9094a1..adc4403 100644 --- a/src/forms/workflow-action.tsx +++ b/src/forms/workflow-action.tsx @@ -38,9 +38,11 @@ const WorkflowAction = ({ type="button" onClick={() => { let value = getValues(fieldName); + let defaultPath = value && value.length > 0 ? value : undefined; + dialog - .open({ defaultPath: value ?? undefined }) - .then((path) => setValue(fieldName, path)); + .open({ defaultPath}) + .then((path) => setValue(fieldName, path)); }} > Folder Path From 43b80e7d2b667b004490d27f0d501c548051712a Mon Sep 17 00:00:00 2001 From: Ryan Killeen Date: Tue, 22 Mar 2022 23:47:06 -0400 Subject: [PATCH 2/3] fix(settings): validate path input, set parent folder and filename for parity on OSes --- src-tauri/src/main.rs | 37 ++++++++++++++++++++++++++++++++++- src/forms/workflow-action.tsx | 11 +++++------ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 95a6f80..7fb66df 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -10,7 +10,7 @@ mod workflows; use app_config::{set_custom_user_config_path, AppConfig}; use serde::Serialize; -use std::{env, path::PathBuf, sync::Mutex}; +use std::{path::PathBuf, sync::Mutex}; use tauri::{ api::dialog::blocking::FileDialogBuilder, AppHandle, CustomMenuItem, GlobalShortcutManager, Manager, RunEvent, State, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, @@ -75,6 +75,40 @@ fn get_state( return state; } +fn set_default_path( + mut dialog_builder: FileDialogBuilder, + default_path: PathBuf, +) -> FileDialogBuilder { + if default_path.is_file() || !default_path.exists() { + if let (Some(parent), Some(file_name)) = (default_path.parent(), default_path.file_name()) { + dialog_builder = dialog_builder.set_directory(parent); + dialog_builder = dialog_builder.set_file_name(&file_name.to_string_lossy().to_string()); + } else { + dialog_builder = dialog_builder.set_directory(default_path); + } + dialog_builder + } else { + dialog_builder.set_directory(default_path) + } +} + +#[tauri::command] +async fn select_file(default_path: Option) -> Option { + let dialog = match default_path { + Some(default_path) => { + let file_exists = std::path::Path::exists(&default_path); + if file_exists { + set_default_path(FileDialogBuilder::new(), default_path) + } else { + FileDialogBuilder::new() + } + } + None => FileDialogBuilder::new(), + }; + + dialog.pick_file() +} + #[tauri::command] fn set_user_config_path(app_config_state: State>) -> Option { let folder_path = FileDialogBuilder::new().pick_folder(); @@ -202,6 +236,7 @@ fn main() { run_workflow, open_settings, set_shortcut, + select_file, set_user_config_path ]) .setup(|app_handle| { diff --git a/src/forms/workflow-action.tsx b/src/forms/workflow-action.tsx index adc4403..a0f7cf8 100644 --- a/src/forms/workflow-action.tsx +++ b/src/forms/workflow-action.tsx @@ -1,5 +1,5 @@ import { useFieldArray, UseFieldArrayRemove } from "react-hook-form"; -import { dialog } from "@tauri-apps/api"; +import { invoke } from "@tauri-apps/api"; import { NestedInputProps } from "./workflow-array"; type Props = { @@ -37,12 +37,11 @@ const WorkflowAction = ({