Skip to content

Commit

Permalink
Merge pull request #60 from KilleenCode/fix-path-selector-mac
Browse files Browse the repository at this point in the history
fix(settings): safeguard default path for file select
  • Loading branch information
RyKilleen authored Mar 23, 2022
2 parents 23bb13d + aef6f1c commit 77a19d8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brancato",
"version": "0.7.1",
"version": "0.7.2",
"private": true,
"dependencies": {
"@algolia/autocomplete-js": "^1.5.3",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brancato"
version = "0.7.1"
version = "0.7.2"
description = "A tool for stage-managing your life"
authors = ["Ryan Killeen"]
license = ""
Expand Down
37 changes: 36 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<PathBuf>) -> Option<PathBuf> {
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<Mutex<AppConfig>>) -> Option<PathBuf> {
let folder_path = FileDialogBuilder::new().pick_folder();
Expand Down Expand Up @@ -202,6 +236,7 @@ fn main() {
run_workflow,
open_settings,
set_shortcut,
select_file,
set_user_config_path
])
.setup(|app_handle| {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"package": {
"productName": "brancato",
"version": "0.7.1"
"version": "0.7.2"
},
"build": {
"distDir": "../build",
Expand Down
11 changes: 6 additions & 5 deletions src/forms/workflow-action.tsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -37,10 +37,11 @@ const WorkflowAction = ({
<button
type="button"
onClick={() => {
let value = getValues(fieldName);
dialog
.open({ defaultPath: value ?? undefined })
.then((path) => setValue(fieldName, path));
let value = getValues(fieldName) as string;
let defaultPath = value && value.length > 0 ? value : undefined;
invoke("select_file", { defaultPath }).then(
(value) => value && setValue(fieldName, value)
);
}}
>
Folder Path
Expand Down

0 comments on commit 77a19d8

Please sign in to comment.