IO: Store last file select directory info in file handlers #116999

Closed
Guillermo Venegas wants to merge 3 commits from guishe:fh-last-dirpath into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
4 changed files with 91 additions and 3 deletions

View File

@ -33,6 +33,11 @@ struct FileHandlerType {
/** List of file extensions supported by the file handler. */
Vector<std::string> file_extensions;
/** Last directory in the file select window where the import operator where executed. */
std::string last_import_dir;
/** Last filename used in the file select window where the import operator where executed. */
std::string last_import_filename;
/** RNA integration. */
ExtensionRNA rna_ext;
@ -71,4 +76,11 @@ Span<std::unique_ptr<FileHandlerType>> file_handlers();
blender::Vector<FileHandlerType *> file_handlers_poll_file_drop(
const bContext *C, const blender::Span<std::string> paths);
void file_handler_last_dir_set(StringRef opname, StringRef dir);
StringRef file_handler_last_dir_get(StringRef opname);
void file_handler_last_filename_set(StringRef opname, StringRef dir);
StringRef file_handler_last_filename_get(StringRef opname);
} // namespace blender::bke

View File

@ -117,4 +117,61 @@ blender::Vector<int64_t> FileHandlerType::filter_supported_paths(
return indices;
}
void file_handler_last_dir_set(StringRef opname, StringRef dir)
{
{
auto itr = std::find_if(file_handlers().begin(),
file_handlers().end(),
[opname](const std::unique_ptr<FileHandlerType> &file_handler) {
return opname == file_handler->import_operator;
});
if (itr != file_handlers().end()) {
itr->get()->last_import_dir = dir;
}
}
}
StringRef file_handler_last_dir_get(StringRef opname)
{
{
auto itr = std::find_if(file_handlers().begin(),
file_handlers().end(),
[opname](const std::unique_ptr<FileHandlerType> &file_handler) {
return opname == file_handler->import_operator;
});
if (itr != file_handlers().end()) {
return itr->get()->last_import_dir;
}
}
return nullptr;
}
void file_handler_last_filename_set(StringRef opname, StringRef dir)
{
{
auto itr = std::find_if(file_handlers().begin(),
file_handlers().end(),
[opname](const std::unique_ptr<FileHandlerType> &file_handler) {
return opname == file_handler->import_operator;
});
if (itr != file_handlers().end()) {
itr->get()->last_import_filename = dir;
}
}
}
StringRef file_handler_last_filename_get(StringRef opname)
{
{
auto itr = std::find_if(file_handlers().begin(),
file_handlers().end(),
[opname](const std::unique_ptr<FileHandlerType> &file_handler) {
return opname == file_handler->import_operator;
});
if (itr != file_handlers().end()) {
return itr->get()->last_import_filename;
}
}
return nullptr;
}
} // namespace blender::bke

View File

@ -14,6 +14,7 @@
#include "BKE_appdir.h"
#include "BKE_blendfile.hh"
#include "BKE_context.hh"
#include "BKE_file_handler.hh"
#include "BKE_global.h"
#include "BKE_main.hh"
#include "BKE_report.h"
@ -2086,7 +2087,9 @@ static bool file_execute(bContext *C, SpaceFile *sfile)
char filepath[FILE_MAX];
sfile->op = nullptr;
FileSelectParams *params = ED_fileselect_get_active_params(sfile);
blender::bke::file_handler_last_dir_set(op->idname, params->dir);
blender::bke::file_handler_last_filename_set(op->idname, params->file);
file_sfile_to_operator_ex(C, bmain, op, sfile, filepath);
if (BLI_exists(params->dir)) {

View File

@ -43,6 +43,7 @@
#include "BKE_appdir.h"
#include "BKE_context.hh"
#include "BKE_file_handler.hh"
#include "BKE_idtype.h"
#include "BKE_main.hh"
#include "BKE_preferences.h"
@ -180,8 +181,23 @@ static FileSelectParams *fileselect_ensure_updated_file_params(SpaceFile *sfile)
else {
params->type = FILE_SPECIAL;
}
if (is_filepath && RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
/* Check before if the operator has explicit requested to load a directory in the fileselect.
*/
bool has_explicit_filepath = (is_filepath &&
RNA_struct_property_is_set(op->ptr, "filepath")) ||
(is_directory &&
RNA_struct_property_is_set(op->ptr, "directory"));
/* Use the file handler to get previously used settings. */
if (auto dir = blender::bke::file_handler_last_dir_get(op->idname);
!has_explicit_filepath && dir.data())
{
STRNCPY(params->dir, dir.data());
auto filename = blender::bke::file_handler_last_filename_get(op->idname);
if (filename.data()) {
STRNCPY(params->dir, dir.data());
}
}
else if (is_filepath && RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
char filepath[FILE_MAX];
RNA_string_get(op->ptr, "filepath", filepath);
if (params->type == FILE_LOADLIB) {