Skip to content

Commit

Permalink
Add filter & sort to editor file dialog
Browse files Browse the repository at this point in the history
Closes godotengine/godot-proposals#2721

On `EditorFileDialog`:
 * Add filter box that only shows folders and files in current directory that match
 * Add sort button to sort files and directories
 * Add a shortcut for CTRL+F for selecting the filter box

Also moved common code between `EditorFileDialog` and `FileSystemDock`
to it's own file.

Co-authored-by: fox <[email protected]>
  • Loading branch information
pafuent and foxydevloper committed Sep 26, 2024
1 parent f7c567e commit 8ead241
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 154 deletions.
61 changes: 61 additions & 0 deletions editor/file_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**************************************************************************/
/* file_info.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "editor/file_info.h"

void sort_file_info_list(List<FileInfo> &r_file_list, FileSortOption p_file_sort_option) {
// Sort the file list if needed.
switch (p_file_sort_option) {
case FileSortOption::FILE_SORT_TYPE:
r_file_list.sort_custom<FileInfoTypeComparator>();
break;
case FileSortOption::FILE_SORT_TYPE_REVERSE:
r_file_list.sort_custom<FileInfoTypeComparator>();
r_file_list.reverse();
break;
case FileSortOption::FILE_SORT_MODIFIED_TIME:
r_file_list.sort_custom<FileInfoModifiedTimeComparator>();
break;
case FileSortOption::FILE_SORT_MODIFIED_TIME_REVERSE:
r_file_list.sort_custom<FileInfoModifiedTimeComparator>();
r_file_list.reverse();
break;
case FileSortOption::FILE_SORT_NAME_REVERSE:
r_file_list.sort();
r_file_list.reverse();
break;
case FileSortOption::FILE_SORT_NAME:
r_file_list.sort();
break;
default:
ERR_FAIL_MSG("Invalid file sort option");
break;
}
}
74 changes: 74 additions & 0 deletions editor/file_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**************************************************************************/
/* file_info.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef FILE_INFO_H
#define FILE_INFO_H

#include "core/variant/variant.h"

enum class FileSortOption {
FILE_SORT_NAME = 0,
FILE_SORT_NAME_REVERSE = 1,
FILE_SORT_TYPE = 2,
FILE_SORT_TYPE_REVERSE = 3,
FILE_SORT_MODIFIED_TIME = 4,
FILE_SORT_MODIFIED_TIME_REVERSE = 5,
FILE_SORT_MAX = 6,
};

struct FileInfo {
String name;
String path;
String icon_path;
StringName type;
Vector<String> sources;
bool import_broken = false;
uint64_t modified_time = 0;

bool operator<(const FileInfo &p_fi) const {
return FileNoCaseComparator()(name, p_fi.name);
}
};

struct FileInfoTypeComparator {
bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
return FileNoCaseComparator()(p_a.name.get_extension() + p_a.type + p_a.name.get_basename(), p_b.name.get_extension() + p_b.type + p_b.name.get_basename());
}
};

struct FileInfoModifiedTimeComparator {
bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
return p_a.modified_time > p_b.modified_time;
}
};

void sort_file_info_list(List<FileInfo> &r_file_list, FileSortOption p_file_sort_option);

#endif // FILE_INFO_H
149 changes: 55 additions & 94 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
}

// Create items for all subdirectories.
bool reversed = file_sort == FILE_SORT_NAME_REVERSE;
bool reversed = file_sort == FileSortOption::FILE_SORT_NAME_REVERSE;
for (int i = reversed ? p_dir->get_subdir_count() - 1 : 0;
reversed ? i >= 0 : i < p_dir->get_subdir_count();
reversed ? i-- : i++) {
Expand Down Expand Up @@ -294,28 +294,28 @@ bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
}
}

FileInfo fi;
fi.name = p_dir->get_file(i);
fi.type = p_dir->get_file_type(i);
fi.icon_path = p_dir->get_file_icon_path(i);
fi.import_broken = !p_dir->get_file_import_is_valid(i);
fi.modified_time = p_dir->get_file_modified_time(i);
FileInfo file_info;
file_info.name = p_dir->get_file(i);
file_info.type = p_dir->get_file_type(i);
file_info.icon_path = p_dir->get_file_icon_path(i);
file_info.import_broken = !p_dir->get_file_import_is_valid(i);
file_info.modified_time = p_dir->get_file_modified_time(i);

file_list.push_back(fi);
file_list.push_back(file_info);
}

// Sort the file list if needed.
_sort_file_info_list(file_list);
sort_file_info_list(file_list, file_sort);

// Build the tree.
const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));

for (const FileInfo &fi : file_list) {
for (const FileInfo &file_info : file_list) {
TreeItem *file_item = tree->create_item(subdirectory_item);
const String file_metadata = lpath.path_join(fi.name);
file_item->set_text(0, fi.name);
const String file_metadata = lpath.path_join(file_info.name);
file_item->set_text(0, file_info.name);
file_item->set_structured_text_bidi_override(0, TextServer::STRUCTURED_TEXT_FILE);
file_item->set_icon(0, _get_tree_item_icon(!fi.import_broken, fi.type, fi.icon_path));
file_item->set_icon(0, _get_tree_item_icon(!file_info.import_broken, file_info.type, file_info.icon_path));
if (da->is_link(file_metadata)) {
file_item->set_icon_overlay(0, get_editor_theme_icon(SNAME("LinkOverlay")));
file_item->set_tooltip_text(0, vformat(TTR("Link to: %s"), da->read_link(file_metadata)));
Expand Down Expand Up @@ -860,65 +860,26 @@ void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *
String file = p_path->get_file(i);

if (_matches_all_search_tokens(file)) {
FileInfo fi;
fi.name = file;
fi.type = p_path->get_file_type(i);
fi.path = p_path->get_file_path(i);
fi.import_broken = !p_path->get_file_import_is_valid(i);
fi.modified_time = p_path->get_file_modified_time(i);

if (_is_file_type_disabled_by_feature_profile(fi.type)) {
FileInfo file_info;
file_info.name = file;
file_info.type = p_path->get_file_type(i);
file_info.path = p_path->get_file_path(i);
file_info.import_broken = !p_path->get_file_import_is_valid(i);
file_info.modified_time = p_path->get_file_modified_time(i);

if (_is_file_type_disabled_by_feature_profile(file_info.type)) {
// This type is disabled, will not appear here.
continue;
}

matches->push_back(fi);
matches->push_back(file_info);
if (matches->size() > p_max_items) {
return;
}
}
}
}

struct FileSystemDock::FileInfoTypeComparator {
bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
return FileNoCaseComparator()(p_a.name.get_extension() + p_a.type + p_a.name.get_basename(), p_b.name.get_extension() + p_b.type + p_b.name.get_basename());
}
};

struct FileSystemDock::FileInfoModifiedTimeComparator {
bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
return p_a.modified_time > p_b.modified_time;
}
};

void FileSystemDock::_sort_file_info_list(List<FileSystemDock::FileInfo> &r_file_list) {
// Sort the file list if needed.
switch (file_sort) {
case FILE_SORT_TYPE:
r_file_list.sort_custom<FileInfoTypeComparator>();
break;
case FILE_SORT_TYPE_REVERSE:
r_file_list.sort_custom<FileInfoTypeComparator>();
r_file_list.reverse();
break;
case FILE_SORT_MODIFIED_TIME:
r_file_list.sort_custom<FileInfoModifiedTimeComparator>();
break;
case FILE_SORT_MODIFIED_TIME_REVERSE:
r_file_list.sort_custom<FileInfoModifiedTimeComparator>();
r_file_list.reverse();
break;
case FILE_SORT_NAME_REVERSE:
r_file_list.sort();
r_file_list.reverse();
break;
default: // FILE_SORT_NAME
r_file_list.sort();
break;
}
}

void FileSystemDock::_update_file_list(bool p_keep_selection) {
// Register the previously current and selected items.
HashSet<String> previous_selection;
Expand Down Expand Up @@ -1005,22 +966,22 @@ void FileSystemDock::_update_file_list(bool p_keep_selection) {
int index;
EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(favorite, &index);

FileInfo fi;
fi.name = favorite.get_file();
fi.path = favorite;
FileInfo file_info;
file_info.name = favorite.get_file();
file_info.path = favorite;
if (efd) {
fi.type = efd->get_file_type(index);
fi.icon_path = efd->get_file_icon_path(index);
fi.import_broken = !efd->get_file_import_is_valid(index);
fi.modified_time = efd->get_file_modified_time(index);
file_info.type = efd->get_file_type(index);
file_info.icon_path = efd->get_file_icon_path(index);
file_info.import_broken = !efd->get_file_import_is_valid(index);
file_info.modified_time = efd->get_file_modified_time(index);
} else {
fi.type = "";
fi.import_broken = true;
fi.modified_time = 0;
file_info.type = "";
file_info.import_broken = true;
file_info.modified_time = 0;
}

if (searched_tokens.is_empty() || _matches_all_search_tokens(fi.name)) {
file_list.push_back(fi);
if (searched_tokens.is_empty() || _matches_all_search_tokens(file_info.name)) {
file_list.push_back(file_info);
}
}
}
Expand Down Expand Up @@ -1077,7 +1038,7 @@ void FileSystemDock::_update_file_list(bool p_keep_selection) {
files->set_item_icon_modulate(-1, editor_is_dark_theme ? inherited_folder_color : inherited_folder_color * ITEM_COLOR_SCALE);
}

bool reversed = file_sort == FILE_SORT_NAME_REVERSE;
bool reversed = file_sort == FileSortOption::FILE_SORT_NAME_REVERSE;
for (int i = reversed ? efd->get_subdir_count() - 1 : 0;
reversed ? i >= 0 : i < efd->get_subdir_count();
reversed ? i-- : i++) {
Expand All @@ -1099,21 +1060,21 @@ void FileSystemDock::_update_file_list(bool p_keep_selection) {

// Display the folder content.
for (int i = 0; i < efd->get_file_count(); i++) {
FileInfo fi;
fi.name = efd->get_file(i);
fi.path = directory.path_join(fi.name);
fi.type = efd->get_file_type(i);
fi.icon_path = efd->get_file_icon_path(i);
fi.import_broken = !efd->get_file_import_is_valid(i);
fi.modified_time = efd->get_file_modified_time(i);
FileInfo file_info;
file_info.name = efd->get_file(i);
file_info.path = directory.path_join(file_info.name);
file_info.type = efd->get_file_type(i);
file_info.icon_path = efd->get_file_icon_path(i);
file_info.import_broken = !efd->get_file_import_is_valid(i);
file_info.modified_time = efd->get_file_modified_time(i);

file_list.push_back(fi);
file_list.push_back(file_info);
}
}
}

// Sort the file list if needed.
_sort_file_info_list(file_list);
sort_file_info_list(file_list, file_sort);

// Fills the ItemList control node from the FileInfos.
String main_scene = GLOBAL_GET("application/run/main_scene");
Expand Down Expand Up @@ -3935,9 +3896,9 @@ void FileSystemDock::_project_settings_changed() {
}

void FileSystemDock::set_file_sort(FileSortOption p_file_sort) {
for (int i = 0; i != FILE_SORT_MAX; i++) {
tree_button_sort->get_popup()->set_item_checked(i, (i == (int)p_file_sort));
file_list_button_sort->get_popup()->set_item_checked(i, (i == (int)p_file_sort));
for (int i = 0; i != static_cast<int>(FileSortOption::FILE_SORT_MAX); i++) {
tree_button_sort->get_popup()->set_item_checked(i, (i == static_cast<int>(p_file_sort)));
file_list_button_sort->get_popup()->set_item_checked(i, (i == static_cast<int>(p_file_sort)));
}
file_sort = p_file_sort;

Expand Down Expand Up @@ -3965,13 +3926,13 @@ MenuButton *FileSystemDock::_create_file_menu_button() {

PopupMenu *p = button->get_popup();
p->connect(SceneStringName(id_pressed), callable_mp(this, &FileSystemDock::_file_sort_popup));
p->add_radio_check_item(TTR("Sort by Name (Ascending)"), FILE_SORT_NAME);
p->add_radio_check_item(TTR("Sort by Name (Descending)"), FILE_SORT_NAME_REVERSE);
p->add_radio_check_item(TTR("Sort by Type (Ascending)"), FILE_SORT_TYPE);
p->add_radio_check_item(TTR("Sort by Type (Descending)"), FILE_SORT_TYPE_REVERSE);
p->add_radio_check_item(TTR("Sort by Last Modified"), FILE_SORT_MODIFIED_TIME);
p->add_radio_check_item(TTR("Sort by First Modified"), FILE_SORT_MODIFIED_TIME_REVERSE);
p->set_item_checked(file_sort, true);
p->add_radio_check_item(TTR("Sort by Name (Ascending)"), static_cast<int>(FileSortOption::FILE_SORT_NAME));
p->add_radio_check_item(TTR("Sort by Name (Descending)"), static_cast<int>(FileSortOption::FILE_SORT_NAME_REVERSE));
p->add_radio_check_item(TTR("Sort by Type (Ascending)"), static_cast<int>(FileSortOption::FILE_SORT_TYPE));
p->add_radio_check_item(TTR("Sort by Type (Descending)"), static_cast<int>(FileSortOption::FILE_SORT_TYPE_REVERSE));
p->add_radio_check_item(TTR("Sort by Last Modified"), static_cast<int>(FileSortOption::FILE_SORT_MODIFIED_TIME));
p->add_radio_check_item(TTR("Sort by First Modified"), static_cast<int>(FileSortOption::FILE_SORT_MODIFIED_TIME_REVERSE));
p->set_item_checked(static_cast<int>(file_sort), true);
return button;
}

Expand Down Expand Up @@ -4041,7 +4002,7 @@ void FileSystemDock::save_layout_to_config(Ref<ConfigFile> p_layout, const Strin
p_layout->set_value(p_section, "dock_filesystem_h_split_offset", get_h_split_offset());
p_layout->set_value(p_section, "dock_filesystem_v_split_offset", get_v_split_offset());
p_layout->set_value(p_section, "dock_filesystem_display_mode", get_display_mode());
p_layout->set_value(p_section, "dock_filesystem_file_sort", get_file_sort());
p_layout->set_value(p_section, "dock_filesystem_file_sort", static_cast<int>(get_file_sort()));
p_layout->set_value(p_section, "dock_filesystem_file_list_display_mode", get_file_list_display_mode());
PackedStringArray selected_files = get_selected_paths();
p_layout->set_value(p_section, "dock_filesystem_selected_paths", selected_files);
Expand Down
Loading

0 comments on commit 8ead241

Please sign in to comment.