filesystem_dock.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**************************************************************************/
  2. /* filesystem_dock.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "editor/file_system/dependency_editor.h"
  32. #include "editor/file_system/editor_file_system.h"
  33. #include "editor/file_system/file_info.h"
  34. #include "editor/script/script_create_dialog.h"
  35. #include "editor/script/script_editor_plugin.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/gui/dialogs.h"
  39. #include "scene/gui/menu_button.h"
  40. #include "scene/gui/split_container.h"
  41. #include "scene/gui/tree.h"
  42. class CreateDialog;
  43. class EditorDirDialog;
  44. class ItemList;
  45. class LineEdit;
  46. class ProgressBar;
  47. class SceneCreateDialog;
  48. class ShaderCreateDialog;
  49. class DirectoryCreateDialog;
  50. class EditorResourceTooltipPlugin;
  51. class FileSystemTree : public Tree {
  52. virtual Control *make_custom_tooltip(const String &p_text) const;
  53. };
  54. class FileSystemList : public ItemList {
  55. GDCLASS(FileSystemList, ItemList);
  56. bool popup_edit_committed = true;
  57. VBoxContainer *popup_editor_vb = nullptr;
  58. Popup *popup_editor = nullptr;
  59. LineEdit *line_editor = nullptr;
  60. virtual Control *make_custom_tooltip(const String &p_text) const override;
  61. void _line_editor_submit(const String &p_text);
  62. void _text_editor_popup_modal_close();
  63. protected:
  64. static void _bind_methods();
  65. public:
  66. bool edit_selected();
  67. String get_edit_text();
  68. FileSystemList();
  69. };
  70. class FileSystemDock : public VBoxContainer {
  71. GDCLASS(FileSystemDock, VBoxContainer);
  72. public:
  73. enum FileListDisplayMode {
  74. FILE_LIST_DISPLAY_THUMBNAILS,
  75. FILE_LIST_DISPLAY_LIST
  76. };
  77. enum DisplayMode {
  78. DISPLAY_MODE_TREE_ONLY,
  79. DISPLAY_MODE_VSPLIT,
  80. DISPLAY_MODE_HSPLIT,
  81. };
  82. enum Overwrite {
  83. OVERWRITE_UNDECIDED,
  84. OVERWRITE_REPLACE,
  85. OVERWRITE_RENAME,
  86. };
  87. private:
  88. enum FileMenu {
  89. FILE_MENU_OPEN,
  90. FILE_MENU_INHERIT,
  91. FILE_MENU_MAIN_SCENE,
  92. FILE_MENU_INSTANTIATE,
  93. FILE_MENU_ADD_FAVORITE,
  94. FILE_MENU_REMOVE_FAVORITE,
  95. FILE_MENU_SHOW_IN_FILESYSTEM,
  96. FILE_MENU_DEPENDENCIES,
  97. FILE_MENU_OWNERS,
  98. FILE_MENU_MOVE,
  99. FILE_MENU_RENAME,
  100. FILE_MENU_REMOVE,
  101. FILE_MENU_DUPLICATE,
  102. FILE_MENU_REIMPORT,
  103. FILE_MENU_NEW,
  104. FILE_MENU_SHOW_IN_EXPLORER,
  105. FILE_MENU_OPEN_EXTERNAL,
  106. FILE_MENU_OPEN_IN_TERMINAL,
  107. FILE_MENU_COPY_PATH,
  108. FILE_MENU_COPY_ABSOLUTE_PATH,
  109. FILE_MENU_COPY_UID,
  110. FILE_MENU_EXPAND_ALL,
  111. FILE_MENU_COLLAPSE_ALL,
  112. FILE_MENU_NEW_RESOURCE,
  113. FILE_MENU_NEW_TEXTFILE,
  114. FILE_MENU_NEW_FOLDER,
  115. FILE_MENU_NEW_SCRIPT,
  116. FILE_MENU_NEW_SCENE,
  117. FILE_MENU_RUN_SCRIPT,
  118. FILE_MENU_MAX,
  119. // Extra shortcuts that don't exist in the menu.
  120. EXTRA_FOCUS_PATH,
  121. EXTRA_FOCUS_FILTER,
  122. CONVERT_BASE_ID = 1000,
  123. };
  124. HashMap<String, TreeItem *> folder_map;
  125. HashMap<String, Color> folder_colors;
  126. Dictionary assigned_folder_colors;
  127. FileSortOption file_sort = FileSortOption::FILE_SORT_NAME;
  128. VBoxContainer *scanning_vb = nullptr;
  129. ProgressBar *scanning_progress = nullptr;
  130. SplitContainer *split_box = nullptr;
  131. VBoxContainer *file_list_vb = nullptr;
  132. int split_box_offset_h = 0;
  133. int split_box_offset_v = 0;
  134. HashSet<String> favorites;
  135. Button *button_dock_placement = nullptr;
  136. Button *button_toggle_display_mode = nullptr;
  137. Button *button_file_list_display_mode = nullptr;
  138. Button *button_hist_next = nullptr;
  139. Button *button_hist_prev = nullptr;
  140. LineEdit *current_path_line_edit = nullptr;
  141. HBoxContainer *toolbar2_hbc = nullptr;
  142. LineEdit *tree_search_box = nullptr;
  143. MenuButton *tree_button_sort = nullptr;
  144. LineEdit *file_list_search_box = nullptr;
  145. MenuButton *file_list_button_sort = nullptr;
  146. PackedStringArray searched_tokens;
  147. Vector<String> uncollapsed_paths_before_search;
  148. TextureRect *search_icon = nullptr;
  149. HBoxContainer *path_hb = nullptr;
  150. FileListDisplayMode file_list_display_mode;
  151. DisplayMode display_mode;
  152. DisplayMode old_display_mode;
  153. PopupMenu *file_list_popup = nullptr;
  154. PopupMenu *tree_popup = nullptr;
  155. DependencyEditor *deps_editor = nullptr;
  156. DependencyEditorOwners *owners_editor = nullptr;
  157. DependencyRemoveDialog *remove_dialog = nullptr;
  158. EditorDirDialog *move_dialog = nullptr;
  159. DirectoryCreateDialog *make_dir_dialog = nullptr;
  160. ConfirmationDialog *overwrite_dialog = nullptr;
  161. ScrollContainer *overwrite_dialog_scroll = nullptr;
  162. Label *overwrite_dialog_header = nullptr;
  163. Label *overwrite_dialog_footer = nullptr;
  164. Label *overwrite_dialog_file_list = nullptr;
  165. ConfirmationDialog *conversion_dialog = nullptr;
  166. SceneCreateDialog *make_scene_dialog = nullptr;
  167. ScriptCreateDialog *make_script_dialog = nullptr;
  168. ShaderCreateDialog *make_shader_dialog = nullptr;
  169. CreateDialog *new_resource_dialog = nullptr;
  170. bool always_show_folders = false;
  171. int thumbnail_size_setting = 0;
  172. bool editor_is_dark_theme = false;
  173. class FileOrFolder {
  174. public:
  175. String path;
  176. bool is_file = false;
  177. FileOrFolder() {}
  178. FileOrFolder(const String &p_path, bool p_is_file) :
  179. path(p_path),
  180. is_file(p_is_file) {}
  181. };
  182. FileOrFolder to_rename;
  183. FileOrFolder to_duplicate;
  184. Vector<FileOrFolder> to_move;
  185. String to_move_path;
  186. bool to_move_or_copy = false;
  187. Vector<String> to_convert;
  188. int selected_conversion_id = 0;
  189. Vector<String> history;
  190. int history_pos;
  191. int history_max_size;
  192. String current_path;
  193. String select_after_scan;
  194. bool updating_tree = false;
  195. int tree_update_id;
  196. FileSystemTree *tree = nullptr;
  197. FileSystemList *files = nullptr;
  198. bool import_dock_needs_update = false;
  199. TreeItem *resources_item = nullptr;
  200. TreeItem *favorites_item = nullptr;
  201. Control *had_focus = nullptr;
  202. bool holding_branch = false;
  203. Vector<TreeItem *> tree_items_selected_on_drag_begin;
  204. PackedInt32Array list_items_selected_on_drag_begin;
  205. LocalVector<Ref<EditorResourceTooltipPlugin>> tooltip_plugins;
  206. HashSet<String> cached_valid_conversion_targets;
  207. void _tree_mouse_exited();
  208. void _reselect_items_selected_on_drag_begin(bool reset = false);
  209. Ref<Texture2D> _get_tree_item_icon(bool p_is_valid, const String &p_file_type, const String &p_icon_path);
  210. void _create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths, bool p_select_in_favorites, bool p_unfold_path = false);
  211. void _update_tree(const Vector<String> &p_uncollapsed_paths = Vector<String>(), bool p_uncollapse_root = false, bool p_scroll_to_selected = true);
  212. void _navigate_to_path(const String &p_path, bool p_select_in_favorites = false, bool p_grab_focus = false);
  213. bool _update_filtered_items(TreeItem *p_tree_item = nullptr);
  214. void _file_list_gui_input(Ref<InputEvent> p_event);
  215. void _tree_gui_input(Ref<InputEvent> p_event);
  216. HashSet<String> _get_valid_conversions_for_file_paths(const Vector<String> &p_paths);
  217. void _update_file_list(bool p_keep_selection);
  218. void _toggle_file_display();
  219. void _set_file_display(bool p_active);
  220. void _fs_changed();
  221. void _select_file(const String &p_path, bool p_select_in_favorites = false, bool p_navigate = true);
  222. void _tree_activate_file();
  223. void _file_list_activate_file(int p_idx);
  224. void _file_multi_selected(int p_index, bool p_selected);
  225. void _tree_multi_selected(Object *p_item, int p_column, bool p_selected);
  226. bool _get_imported_files(const String &p_path, String &r_extension, Vector<String> &r_files) const;
  227. void _update_import_dock();
  228. void _get_all_items_in_dir(EditorFileSystemDirectory *p_efsd, Vector<String> &r_files, Vector<String> &r_folders) const;
  229. void _find_file_owners(EditorFileSystemDirectory *p_efsd, const HashSet<String> &p_renames, HashSet<String> &r_file_owners) const;
  230. void _try_move_item(const FileOrFolder &p_item, const String &p_new_path, HashMap<String, String> &p_file_renames, HashMap<String, String> &p_folder_renames);
  231. void _try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const;
  232. void _before_move(HashMap<String, ResourceUID::ID> &r_uids, HashSet<String> &r_file_owners) const;
  233. void _update_dependencies_after_move(const HashMap<String, String> &p_renames, const HashSet<String> &p_file_owners) const;
  234. void _update_resource_paths_after_move(const HashMap<String, String> &p_renames, const HashMap<String, ResourceUID::ID> &p_uids) const;
  235. void _update_favorites_after_move(const HashMap<String, String> &p_files_renames, const HashMap<String, String> &p_folders_renames) const;
  236. void _update_project_settings_after_move(const HashMap<String, String> &p_renames, const HashMap<String, String> &p_folders_renames);
  237. String _get_unique_name(const FileOrFolder &p_entry, const String &p_at_path);
  238. void _update_folder_colors_setting();
  239. void _resource_removed(const Ref<Resource> &p_resource);
  240. void _file_removed(const String &p_file);
  241. void _folder_removed(const String &p_folder);
  242. void _resource_created();
  243. void _make_scene_confirm();
  244. void _rename_operation_confirm();
  245. void _duplicate_operation_confirm(const String &p_path);
  246. void _overwrite_dialog_action(bool p_overwrite);
  247. void _convert_dialog_action();
  248. Vector<String> _check_existing();
  249. void _move_operation_confirm(const String &p_to_path, bool p_copy = false, Overwrite p_overwrite = OVERWRITE_UNDECIDED);
  250. void _tree_rmb_option(int p_option);
  251. void _file_list_rmb_option(int p_option);
  252. void _generic_rmb_option_selected(int p_option);
  253. void _file_option(int p_option, const Vector<String> &p_selected);
  254. int _get_menu_option_from_key(const Ref<InputEventKey> &p_key);
  255. void _fw_history();
  256. void _bw_history();
  257. void _update_history();
  258. void _push_to_history();
  259. void _set_scanning_mode();
  260. void _rescan();
  261. void _change_split_mode();
  262. void _split_dragged(int p_offset);
  263. void _search_changed(const String &p_text, const Control *p_from);
  264. bool _matches_all_search_tokens(const String &p_text);
  265. MenuButton *_create_file_menu_button();
  266. void _file_sort_popup(int p_id);
  267. void _folder_color_index_pressed(int p_index, PopupMenu *p_menu);
  268. void _file_and_folders_fill_popup(PopupMenu *p_popup, const Vector<String> &p_paths, bool p_display_path_dependent_options = true);
  269. void _tree_rmb_select(const Vector2 &p_pos, MouseButton p_button);
  270. void _file_list_item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_mouse_button_index);
  271. void _file_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index);
  272. void _tree_empty_click(const Vector2 &p_pos, MouseButton p_button);
  273. void _tree_empty_selected();
  274. void _search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items);
  275. void _set_current_path_line_edit_text(const String &p_path);
  276. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  277. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  278. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  279. void _get_drag_target_folder(String &target, bool &target_favorites, const Point2 &p_point, Control *p_from) const;
  280. void _preview_invalidated(const String &p_path);
  281. void _file_list_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata);
  282. void _tree_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata);
  283. void _update_display_mode(bool p_force = false);
  284. Vector<String> _tree_get_selected(bool remove_self_inclusion = true, bool p_include_unselected_cursor = false) const;
  285. Vector<String> _file_list_get_selected() const;
  286. bool _is_file_type_disabled_by_feature_profile(const StringName &p_class);
  287. void _feature_profile_changed();
  288. void _project_settings_changed();
  289. static Vector<String> _remove_self_included_paths(Vector<String> selected_strings);
  290. void _change_bottom_dock_placement();
  291. bool _can_dock_horizontal() const;
  292. void _set_dock_horizontal(bool p_enable);
  293. void _save_layout_to_config(Ref<ConfigFile> p_layout, const String &p_section) const;
  294. void _load_layout_from_config(Ref<ConfigFile> p_layout, const String &p_section);
  295. private:
  296. inline static FileSystemDock *singleton = nullptr;
  297. public:
  298. static FileSystemDock *get_singleton() { return singleton; }
  299. protected:
  300. void _notification(int p_what);
  301. static void _bind_methods();
  302. public:
  303. static constexpr double ITEM_COLOR_SCALE = 1.75;
  304. static constexpr double ITEM_ALPHA_MIN = 0.1;
  305. static constexpr double ITEM_ALPHA_MAX = 0.15;
  306. static constexpr double ITEM_BG_DARK_SCALE = 0.3;
  307. static Color get_dir_icon_color(const String &p_dir_path, const Color &p_default);
  308. const HashMap<String, Color> &get_folder_colors() const;
  309. Dictionary get_assigned_folder_colors() const;
  310. Vector<String> get_selected_paths() const;
  311. Vector<String> get_uncollapsed_paths() const;
  312. String get_current_path() const;
  313. String get_current_directory() const;
  314. String get_folder_path_at_mouse_position() const;
  315. void navigate_to_path(const String &p_path);
  316. void focus_on_path();
  317. void focus_on_filter();
  318. void create_directory(const String &p_path, const String &p_base_dir);
  319. ScriptCreateDialog *get_script_create_dialog() const;
  320. void fix_dependencies(const String &p_for_file);
  321. void update_all();
  322. int get_h_split_offset() const { return split_box_offset_h; }
  323. void set_h_split_offset(int p_offset) { split_box_offset_h = p_offset; }
  324. int get_v_split_offset() const { return split_box_offset_v; }
  325. void set_v_split_offset(int p_offset) { split_box_offset_v = p_offset; }
  326. void select_file(const String &p_file);
  327. void set_display_mode(DisplayMode p_display_mode);
  328. DisplayMode get_display_mode() const { return display_mode; }
  329. void set_file_sort(FileSortOption p_file_sort);
  330. FileSortOption get_file_sort() const { return file_sort; }
  331. void set_file_list_display_mode(FileListDisplayMode p_mode);
  332. FileListDisplayMode get_file_list_display_mode() const { return file_list_display_mode; }
  333. Tree *get_tree_control() { return tree; }
  334. void add_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin);
  335. void remove_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin);
  336. Control *create_tooltip_for_path(const String &p_path) const;
  337. FileSystemDock();
  338. ~FileSystemDock();
  339. };
  340. VARIANT_ENUM_CAST(FileSystemDock::Overwrite);