editor_quick_open_dialog.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /**************************************************************************/
  2. /* editor_quick_open_dialog.cpp */
  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. #include "editor_quick_open_dialog.h"
  31. #include "core/string/fuzzy_search.h"
  32. #include "editor/editor_file_system.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_resource_preview.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/gui/center_container.h"
  39. #include "scene/gui/check_button.h"
  40. #include "scene/gui/flow_container.h"
  41. #include "scene/gui/margin_container.h"
  42. #include "scene/gui/panel_container.h"
  43. #include "scene/gui/separator.h"
  44. #include "scene/gui/texture_rect.h"
  45. #include "scene/gui/tree.h"
  46. void HighlightedLabel::draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing) {
  47. for (int i = get_lines_skipped(); i < p_line_limit; i++) {
  48. RID line = get_line_rid(i);
  49. Vector<Vector2> ranges = TS->shaped_text_get_selection(line, p_substr.x, p_substr.x + p_substr.y);
  50. Rect2 line_rect = get_line_rect(i);
  51. for (const Vector2 &range : ranges) {
  52. Rect2 rect = Rect2(Point2(range.x, 0) + line_rect.position, Size2(range.y - range.x, line_rect.size.y));
  53. rect.position = p_offset + line_rect.position;
  54. rect.position.x += range.x;
  55. rect.size = Size2(range.y - range.x, line_rect.size.y);
  56. rect.size.x = MIN(rect.size.x, line_rect.size.x - range.x);
  57. if (rect.size.x > 0) {
  58. draw_rect(rect, Color(1, 1, 1, 0.07), true);
  59. draw_rect(rect, Color(0.5, 0.7, 1.0, 0.4), false, 1);
  60. }
  61. }
  62. p_offset.y += line_spacing + TS->shaped_text_get_ascent(line) + TS->shaped_text_get_descent(line);
  63. }
  64. }
  65. void HighlightedLabel::add_highlight(const Vector2i &p_interval) {
  66. if (p_interval.y > 0) {
  67. highlights.append(p_interval);
  68. queue_redraw();
  69. }
  70. }
  71. void HighlightedLabel::reset_highlights() {
  72. highlights.clear();
  73. queue_redraw();
  74. }
  75. void HighlightedLabel::_notification(int p_notification) {
  76. if (p_notification == NOTIFICATION_DRAW) {
  77. if (highlights.is_empty()) {
  78. return;
  79. }
  80. Vector2 offset;
  81. int line_limit;
  82. int line_spacing;
  83. get_layout_data(offset, line_limit, line_spacing);
  84. for (const Vector2i &substr : highlights) {
  85. draw_substr_rects(substr, offset, line_limit, line_spacing);
  86. }
  87. }
  88. }
  89. EditorQuickOpenDialog::EditorQuickOpenDialog() {
  90. VBoxContainer *vbc = memnew(VBoxContainer);
  91. vbc->add_theme_constant_override("separation", 0);
  92. add_child(vbc);
  93. {
  94. // Search bar
  95. MarginContainer *mc = memnew(MarginContainer);
  96. mc->add_theme_constant_override("margin_top", 6);
  97. mc->add_theme_constant_override("margin_bottom", 6);
  98. mc->add_theme_constant_override("margin_left", 1);
  99. mc->add_theme_constant_override("margin_right", 1);
  100. vbc->add_child(mc);
  101. search_box = memnew(LineEdit);
  102. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  103. search_box->set_placeholder(TTR("Search files..."));
  104. search_box->set_clear_button_enabled(true);
  105. mc->add_child(search_box);
  106. }
  107. {
  108. container = memnew(QuickOpenResultContainer);
  109. container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::ok_pressed));
  110. vbc->add_child(container);
  111. }
  112. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed));
  113. search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input));
  114. register_text_enter(search_box);
  115. get_ok_button()->hide();
  116. }
  117. String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) {
  118. if (p_base_types.size() > 1) {
  119. return TTR("Select Resource");
  120. }
  121. if (p_base_types[0] == SNAME("PackedScene")) {
  122. return TTR("Select Scene");
  123. }
  124. return TTR("Select") + " " + p_base_types[0];
  125. }
  126. void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) {
  127. ERR_FAIL_COND(p_base_types.is_empty());
  128. ERR_FAIL_COND(!p_item_selected_callback.is_valid());
  129. item_selected_callback = p_item_selected_callback;
  130. container->init(p_base_types);
  131. get_ok_button()->set_disabled(container->has_nothing_selected());
  132. set_title(get_dialog_title(p_base_types));
  133. popup_centered_clamped(Size2(780, 650) * EDSCALE, 0.8f);
  134. search_box->grab_focus();
  135. }
  136. void EditorQuickOpenDialog::ok_pressed() {
  137. item_selected_callback.call(container->get_selected());
  138. container->save_selected_item();
  139. container->cleanup();
  140. search_box->clear();
  141. hide();
  142. }
  143. void EditorQuickOpenDialog::cancel_pressed() {
  144. container->cleanup();
  145. search_box->clear();
  146. }
  147. void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) {
  148. container->set_query_and_update(p_query);
  149. get_ok_button()->set_disabled(container->has_nothing_selected());
  150. }
  151. //------------------------- Result Container
  152. void style_button(Button *p_button) {
  153. p_button->set_flat(true);
  154. p_button->set_focus_mode(Control::FOCUS_NONE);
  155. p_button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
  156. }
  157. QuickOpenResultContainer::QuickOpenResultContainer() {
  158. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  159. set_v_size_flags(Control::SIZE_EXPAND_FILL);
  160. add_theme_constant_override("separation", 0);
  161. {
  162. // Results section
  163. panel_container = memnew(PanelContainer);
  164. panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  165. add_child(panel_container);
  166. {
  167. // No search results
  168. no_results_container = memnew(CenterContainer);
  169. no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  170. no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  171. panel_container->add_child(no_results_container);
  172. no_results_label = memnew(Label);
  173. no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE);
  174. no_results_container->add_child(no_results_label);
  175. no_results_container->hide();
  176. }
  177. {
  178. // Search results
  179. scroll_container = memnew(ScrollContainer);
  180. scroll_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  181. scroll_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  182. scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  183. scroll_container->hide();
  184. panel_container->add_child(scroll_container);
  185. list = memnew(VBoxContainer);
  186. list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  187. list->hide();
  188. scroll_container->add_child(list);
  189. grid = memnew(HFlowContainer);
  190. grid->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  191. grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  192. grid->add_theme_constant_override("v_separation", 18);
  193. grid->add_theme_constant_override("h_separation", 4);
  194. grid->hide();
  195. scroll_container->add_child(grid);
  196. }
  197. }
  198. {
  199. // Selected filepath
  200. file_details_path = memnew(Label);
  201. file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  202. file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  203. file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  204. add_child(file_details_path);
  205. }
  206. {
  207. // Bottom bar
  208. HBoxContainer *bottom_bar = memnew(HBoxContainer);
  209. bottom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  210. bottom_bar->set_alignment(ALIGNMENT_END);
  211. bottom_bar->add_theme_constant_override("separation", 3);
  212. add_child(bottom_bar);
  213. fuzzy_search_toggle = memnew(CheckButton);
  214. style_button(fuzzy_search_toggle);
  215. fuzzy_search_toggle->set_text(TTR("Fuzzy Search"));
  216. fuzzy_search_toggle->set_tooltip_text(TTR("Enable fuzzy matching"));
  217. fuzzy_search_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_fuzzy_search));
  218. bottom_bar->add_child(fuzzy_search_toggle);
  219. include_addons_toggle = memnew(CheckButton);
  220. style_button(include_addons_toggle);
  221. include_addons_toggle->set_text(TTR("Addons"));
  222. include_addons_toggle->set_tooltip_text(TTR("Include files from addons"));
  223. include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons));
  224. bottom_bar->add_child(include_addons_toggle);
  225. VSeparator *vsep = memnew(VSeparator);
  226. vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  227. vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE));
  228. bottom_bar->add_child(vsep);
  229. display_mode_toggle = memnew(Button);
  230. style_button(display_mode_toggle);
  231. display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode));
  232. bottom_bar->add_child(display_mode_toggle);
  233. }
  234. }
  235. void QuickOpenResultContainer::_ensure_result_vector_capacity() {
  236. int target_size = EDITOR_GET("filesystem/quick_open_dialog/max_results");
  237. int initial_size = result_items.size();
  238. for (int i = target_size; i < initial_size; i++) {
  239. result_items[i]->queue_free();
  240. }
  241. result_items.resize(target_size);
  242. for (int i = initial_size; i < target_size; i++) {
  243. QuickOpenResultItem *item = memnew(QuickOpenResultItem);
  244. item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i));
  245. result_items.write[i] = item;
  246. if (!never_opened) {
  247. _layout_result_item(item);
  248. }
  249. }
  250. }
  251. void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) {
  252. _ensure_result_vector_capacity();
  253. base_types = p_base_types;
  254. const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode");
  255. const bool adaptive_display_mode = (display_mode_behavior == 0);
  256. if (adaptive_display_mode) {
  257. _set_display_mode(get_adaptive_display_mode(p_base_types));
  258. } else if (never_opened) {
  259. int last = EditorSettings::get_singleton()->get_project_metadata("quick_open_dialog", "last_mode", (int)QuickOpenDisplayMode::LIST);
  260. _set_display_mode((QuickOpenDisplayMode)last);
  261. }
  262. const bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");
  263. const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons");
  264. fuzzy_search_toggle->set_pressed_no_signal(fuzzy_matching);
  265. include_addons_toggle->set_pressed_no_signal(include_addons);
  266. never_opened = false;
  267. const bool enable_highlights = EDITOR_GET("filesystem/quick_open_dialog/show_search_highlight");
  268. for (QuickOpenResultItem *E : result_items) {
  269. E->enable_highlights = enable_highlights;
  270. }
  271. _create_initial_results();
  272. }
  273. void QuickOpenResultContainer::_create_initial_results() {
  274. file_type_icons.clear();
  275. file_type_icons.insert("__default_icon", get_editor_theme_icon(SNAME("Object")));
  276. filepaths.clear();
  277. filetypes.clear();
  278. _find_filepaths_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), include_addons_toggle->is_pressed());
  279. max_total_results = MIN(filepaths.size(), result_items.size());
  280. update_results();
  281. }
  282. void QuickOpenResultContainer::_find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) {
  283. for (int i = 0; i < p_directory->get_subdir_count(); i++) {
  284. if (p_include_addons || p_directory->get_name() != "addons") {
  285. _find_filepaths_in_folder(p_directory->get_subdir(i), p_include_addons);
  286. }
  287. }
  288. for (int i = 0; i < p_directory->get_file_count(); i++) {
  289. String file_path = p_directory->get_file_path(i);
  290. const StringName engine_type = p_directory->get_file_type(i);
  291. const StringName script_type = p_directory->get_file_resource_script_class(i);
  292. const bool is_engine_type = script_type == StringName();
  293. const StringName &actual_type = is_engine_type ? engine_type : script_type;
  294. for (const StringName &parent_type : base_types) {
  295. bool is_valid = ClassDB::is_parent_class(engine_type, parent_type) || (!is_engine_type && EditorNode::get_editor_data().script_class_is_parent(script_type, parent_type));
  296. if (is_valid) {
  297. filepaths.append(file_path);
  298. filetypes.insert(file_path, actual_type);
  299. break; // Stop testing base types as soon as we get a match.
  300. }
  301. }
  302. }
  303. }
  304. void QuickOpenResultContainer::set_query_and_update(const String &p_query) {
  305. query = p_query;
  306. update_results();
  307. }
  308. void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &candidate, const String &filepath) {
  309. StringName actual_type = *filetypes.lookup_ptr(filepath);
  310. candidate.file_path = filepath;
  311. candidate.result = nullptr;
  312. EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(filepath);
  313. if (item.preview.is_valid()) {
  314. candidate.thumbnail = item.preview;
  315. } else if (file_type_icons.has(actual_type)) {
  316. candidate.thumbnail = *file_type_icons.lookup_ptr(actual_type);
  317. } else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) {
  318. candidate.thumbnail = get_editor_theme_icon(actual_type);
  319. file_type_icons.insert(actual_type, candidate.thumbnail);
  320. } else {
  321. candidate.thumbnail = *file_type_icons.lookup_ptr("__default_icon");
  322. }
  323. }
  324. void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result) {
  325. _setup_candidate(p_candidate, p_result.target);
  326. p_candidate.result = &p_result;
  327. }
  328. void QuickOpenResultContainer::update_results() {
  329. showing_history = false;
  330. candidates.clear();
  331. if (query.is_empty()) {
  332. _use_default_candidates();
  333. } else {
  334. _score_and_sort_candidates();
  335. }
  336. _update_result_items(MIN(candidates.size(), max_total_results), 0);
  337. }
  338. void QuickOpenResultContainer::_use_default_candidates() {
  339. if (filepaths.size() <= SHOW_ALL_FILES_THRESHOLD) {
  340. candidates.resize(filepaths.size());
  341. QuickOpenResultCandidate *candidates_write = candidates.ptrw();
  342. for (const String &filepath : filepaths) {
  343. _setup_candidate(*candidates_write++, filepath);
  344. }
  345. } else if (base_types.size() == 1) {
  346. Vector<QuickOpenResultCandidate> *history = selected_history.lookup_ptr(base_types[0]);
  347. if (history) {
  348. showing_history = true;
  349. candidates.append_array(*history);
  350. }
  351. }
  352. }
  353. void QuickOpenResultContainer::_update_fuzzy_search_results() {
  354. FuzzySearch fuzzy_search;
  355. fuzzy_search.start_offset = 6; // Don't match against "res://" at the start of each filepath.
  356. fuzzy_search.set_query(query);
  357. fuzzy_search.max_results = max_total_results;
  358. bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching");
  359. int max_misses = EDITOR_GET("filesystem/quick_open_dialog/max_fuzzy_misses");
  360. fuzzy_search.allow_subsequences = fuzzy_matching;
  361. fuzzy_search.max_misses = fuzzy_matching ? max_misses : 0;
  362. fuzzy_search.search_all(filepaths, search_results);
  363. }
  364. void QuickOpenResultContainer::_score_and_sort_candidates() {
  365. _update_fuzzy_search_results();
  366. candidates.resize(search_results.size());
  367. QuickOpenResultCandidate *candidates_write = candidates.ptrw();
  368. for (const FuzzySearchResult &result : search_results) {
  369. _setup_candidate(*candidates_write++, result);
  370. }
  371. }
  372. void QuickOpenResultContainer::_update_result_items(int p_new_visible_results_count, int p_new_selection_index) {
  373. // Only need to update items that were not hidden in previous update.
  374. int num_items_needing_updates = MAX(num_visible_results, p_new_visible_results_count);
  375. num_visible_results = p_new_visible_results_count;
  376. for (int i = 0; i < num_items_needing_updates; i++) {
  377. QuickOpenResultItem *item = result_items[i];
  378. if (i < num_visible_results) {
  379. item->set_content(candidates[i]);
  380. } else {
  381. item->reset();
  382. }
  383. };
  384. const bool any_results = num_visible_results > 0;
  385. _select_item(any_results ? p_new_selection_index : -1);
  386. scroll_container->set_visible(any_results);
  387. no_results_container->set_visible(!any_results);
  388. if (!any_results) {
  389. if (filepaths.is_empty()) {
  390. no_results_label->set_text(TTR("No files found for this type"));
  391. } else if (query.is_empty()) {
  392. no_results_label->set_text(TTR("Start searching to find files..."));
  393. } else {
  394. no_results_label->set_text(TTR("No results found"));
  395. }
  396. }
  397. }
  398. void QuickOpenResultContainer::handle_search_box_input(const Ref<InputEvent> &p_ie) {
  399. if (num_visible_results < 0) {
  400. return;
  401. }
  402. Ref<InputEventKey> key_event = p_ie;
  403. if (key_event.is_valid() && key_event->is_pressed()) {
  404. bool move_selection = false;
  405. switch (key_event->get_keycode()) {
  406. case Key::UP:
  407. case Key::DOWN:
  408. case Key::PAGEUP:
  409. case Key::PAGEDOWN: {
  410. move_selection = true;
  411. } break;
  412. case Key::LEFT:
  413. case Key::RIGHT: {
  414. if (content_display_mode == QuickOpenDisplayMode::GRID) {
  415. // Maybe strip off the shift modifier to allow non-selecting navigation by character?
  416. if (key_event->get_modifiers_mask() == 0) {
  417. move_selection = true;
  418. }
  419. }
  420. } break;
  421. default:
  422. break; // Let the event through so it will reach the search box.
  423. }
  424. if (move_selection) {
  425. _move_selection_index(key_event->get_keycode());
  426. queue_redraw();
  427. accept_event();
  428. }
  429. }
  430. }
  431. void QuickOpenResultContainer::_move_selection_index(Key p_key) {
  432. // Don't move selection if there are no results.
  433. if (num_visible_results <= 0) {
  434. return;
  435. }
  436. const int max_index = num_visible_results - 1;
  437. int idx = selection_index;
  438. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  439. if (p_key == Key::UP) {
  440. idx = (idx == 0) ? max_index : (idx - 1);
  441. } else if (p_key == Key::DOWN) {
  442. idx = (idx == max_index) ? 0 : (idx + 1);
  443. } else if (p_key == Key::PAGEUP) {
  444. idx = (idx == 0) ? idx : MAX(idx - 10, 0);
  445. } else if (p_key == Key::PAGEDOWN) {
  446. idx = (idx == max_index) ? idx : MIN(idx + 10, max_index);
  447. }
  448. } else {
  449. int column_count = grid->get_line_max_child_count();
  450. if (p_key == Key::LEFT) {
  451. idx = (idx == 0) ? max_index : (idx - 1);
  452. } else if (p_key == Key::RIGHT) {
  453. idx = (idx == max_index) ? 0 : (idx + 1);
  454. } else if (p_key == Key::UP) {
  455. idx = (idx == 0) ? max_index : MAX(idx - column_count, 0);
  456. } else if (p_key == Key::DOWN) {
  457. idx = (idx == max_index) ? 0 : MIN(idx + column_count, max_index);
  458. } else if (p_key == Key::PAGEUP) {
  459. idx = (idx == 0) ? idx : MAX(idx - (3 * column_count), 0);
  460. } else if (p_key == Key::PAGEDOWN) {
  461. idx = (idx == max_index) ? idx : MIN(idx + (3 * column_count), max_index);
  462. }
  463. }
  464. _select_item(idx);
  465. }
  466. void QuickOpenResultContainer::_select_item(int p_index) {
  467. if (!has_nothing_selected()) {
  468. result_items[selection_index]->highlight_item(false);
  469. }
  470. selection_index = p_index;
  471. if (has_nothing_selected()) {
  472. file_details_path->set_text("");
  473. return;
  474. }
  475. result_items[selection_index]->highlight_item(true);
  476. file_details_path->set_text(get_selected() + (showing_history ? TTR(" (recently opened)") : ""));
  477. const QuickOpenResultItem *item = result_items[selection_index];
  478. // Copied from Tree.
  479. const int selected_position = item->get_position().y;
  480. const int selected_size = item->get_size().y;
  481. const int scroll_window_size = scroll_container->get_size().y;
  482. const int scroll_position = scroll_container->get_v_scroll();
  483. if (selected_position <= scroll_position) {
  484. scroll_container->set_v_scroll(selected_position);
  485. } else if (selected_position + selected_size > scroll_position + scroll_window_size) {
  486. scroll_container->set_v_scroll(selected_position + selected_size - scroll_window_size);
  487. }
  488. }
  489. void QuickOpenResultContainer::_item_input(const Ref<InputEvent> &p_ev, int p_index) {
  490. Ref<InputEventMouseButton> mb = p_ev;
  491. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  492. _select_item(p_index);
  493. emit_signal(SNAME("result_clicked"));
  494. }
  495. }
  496. void QuickOpenResultContainer::_toggle_fuzzy_search(bool p_pressed) {
  497. EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/enable_fuzzy_matching", p_pressed);
  498. update_results();
  499. }
  500. void QuickOpenResultContainer::_toggle_include_addons(bool p_pressed) {
  501. EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/include_addons", p_pressed);
  502. cleanup();
  503. _create_initial_results();
  504. }
  505. void QuickOpenResultContainer::_toggle_display_mode() {
  506. QuickOpenDisplayMode new_display_mode = (content_display_mode == QuickOpenDisplayMode::LIST) ? QuickOpenDisplayMode::GRID : QuickOpenDisplayMode::LIST;
  507. _set_display_mode(new_display_mode);
  508. }
  509. CanvasItem *QuickOpenResultContainer::_get_result_root() {
  510. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  511. return list;
  512. } else {
  513. return grid;
  514. }
  515. }
  516. void QuickOpenResultContainer::_layout_result_item(QuickOpenResultItem *item) {
  517. item->set_display_mode(content_display_mode);
  518. Node *parent = item->get_parent();
  519. if (parent) {
  520. parent->remove_child(item);
  521. }
  522. _get_result_root()->add_child(item);
  523. }
  524. void QuickOpenResultContainer::_set_display_mode(QuickOpenDisplayMode p_display_mode) {
  525. CanvasItem *prev_root = _get_result_root();
  526. if (prev_root->is_visible() && content_display_mode == p_display_mode) {
  527. return;
  528. }
  529. content_display_mode = p_display_mode;
  530. CanvasItem *next_root = _get_result_root();
  531. EditorSettings::get_singleton()->set_project_metadata("quick_open_dialog", "last_mode", (int)content_display_mode);
  532. prev_root->hide();
  533. next_root->show();
  534. for (QuickOpenResultItem *item : result_items) {
  535. _layout_result_item(item);
  536. }
  537. _update_result_items(num_visible_results, selection_index);
  538. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  539. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
  540. display_mode_toggle->set_tooltip_text(TTR("Grid view"));
  541. } else {
  542. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  543. display_mode_toggle->set_tooltip_text(TTR("List view"));
  544. }
  545. }
  546. bool QuickOpenResultContainer::has_nothing_selected() const {
  547. return selection_index < 0;
  548. }
  549. String QuickOpenResultContainer::get_selected() const {
  550. ERR_FAIL_COND_V_MSG(has_nothing_selected(), String(), "Tried to get selected file, but nothing was selected.");
  551. return candidates[selection_index].file_path;
  552. }
  553. QuickOpenDisplayMode QuickOpenResultContainer::get_adaptive_display_mode(const Vector<StringName> &p_base_types) {
  554. static const Vector<StringName> grid_preferred_types = {
  555. "Font",
  556. "Texture2D",
  557. "Material",
  558. "Mesh"
  559. };
  560. for (const StringName &type : grid_preferred_types) {
  561. for (const StringName &base_type : p_base_types) {
  562. if (base_type == type || ClassDB::is_parent_class(base_type, type)) {
  563. return QuickOpenDisplayMode::GRID;
  564. }
  565. }
  566. }
  567. return QuickOpenDisplayMode::LIST;
  568. }
  569. void QuickOpenResultContainer::save_selected_item() {
  570. if (base_types.size() > 1) {
  571. // Getting the type of the file and checking which base type it belongs to should be possible.
  572. // However, for now these are not supported, and we don't record this.
  573. return;
  574. }
  575. const StringName &base_type = base_types[0];
  576. const QuickOpenResultCandidate &selected = candidates[selection_index];
  577. Vector<QuickOpenResultCandidate> *type_history = selected_history.lookup_ptr(base_type);
  578. if (!type_history) {
  579. selected_history.insert(base_type, Vector<QuickOpenResultCandidate>());
  580. type_history = selected_history.lookup_ptr(base_type);
  581. } else {
  582. for (int i = 0; i < type_history->size(); i++) {
  583. if (selected.file_path == type_history->get(i).file_path) {
  584. type_history->remove_at(i);
  585. break;
  586. }
  587. }
  588. }
  589. type_history->insert(0, selected);
  590. type_history->ptrw()->result = nullptr;
  591. if (type_history->size() > MAX_HISTORY_SIZE) {
  592. type_history->resize(MAX_HISTORY_SIZE);
  593. }
  594. }
  595. void QuickOpenResultContainer::cleanup() {
  596. num_visible_results = 0;
  597. candidates.clear();
  598. _select_item(-1);
  599. for (QuickOpenResultItem *item : result_items) {
  600. item->reset();
  601. }
  602. }
  603. void QuickOpenResultContainer::_notification(int p_what) {
  604. switch (p_what) {
  605. case NOTIFICATION_THEME_CHANGED: {
  606. Color text_color = get_theme_color("font_readonly_color", EditorStringName(Editor));
  607. file_details_path->add_theme_color_override(SceneStringName(font_color), text_color);
  608. no_results_label->add_theme_color_override(SceneStringName(font_color), text_color);
  609. panel_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  610. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  611. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
  612. } else {
  613. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  614. }
  615. } break;
  616. }
  617. }
  618. void QuickOpenResultContainer::_bind_methods() {
  619. ADD_SIGNAL(MethodInfo("result_clicked"));
  620. }
  621. //------------------------- Result Item
  622. QuickOpenResultItem::QuickOpenResultItem() {
  623. set_focus_mode(FocusMode::FOCUS_ALL);
  624. _set_enabled(false);
  625. set_default_cursor_shape(CURSOR_POINTING_HAND);
  626. list_item = memnew(QuickOpenResultListItem);
  627. list_item->hide();
  628. add_child(list_item);
  629. grid_item = memnew(QuickOpenResultGridItem);
  630. grid_item->hide();
  631. add_child(grid_item);
  632. }
  633. void QuickOpenResultItem::set_display_mode(QuickOpenDisplayMode p_display_mode) {
  634. if (p_display_mode == QuickOpenDisplayMode::LIST) {
  635. grid_item->hide();
  636. grid_item->reset();
  637. list_item->show();
  638. } else {
  639. list_item->hide();
  640. list_item->reset();
  641. grid_item->show();
  642. }
  643. queue_redraw();
  644. }
  645. void QuickOpenResultItem::set_content(const QuickOpenResultCandidate &p_candidate) {
  646. _set_enabled(true);
  647. if (list_item->is_visible()) {
  648. list_item->set_content(p_candidate, enable_highlights);
  649. } else {
  650. grid_item->set_content(p_candidate, enable_highlights);
  651. }
  652. queue_redraw();
  653. }
  654. void QuickOpenResultItem::reset() {
  655. _set_enabled(false);
  656. is_hovering = false;
  657. is_selected = false;
  658. list_item->reset();
  659. grid_item->reset();
  660. }
  661. void QuickOpenResultItem::highlight_item(bool p_enabled) {
  662. is_selected = p_enabled;
  663. if (list_item->is_visible()) {
  664. if (p_enabled) {
  665. list_item->highlight_item(highlighted_font_color);
  666. } else {
  667. list_item->remove_highlight();
  668. }
  669. } else {
  670. if (p_enabled) {
  671. grid_item->highlight_item(highlighted_font_color);
  672. } else {
  673. grid_item->remove_highlight();
  674. }
  675. }
  676. queue_redraw();
  677. }
  678. void QuickOpenResultItem::_set_enabled(bool p_enabled) {
  679. set_visible(p_enabled);
  680. set_process(p_enabled);
  681. set_process_input(p_enabled);
  682. }
  683. void QuickOpenResultItem::_notification(int p_what) {
  684. switch (p_what) {
  685. case NOTIFICATION_MOUSE_ENTER:
  686. case NOTIFICATION_MOUSE_EXIT: {
  687. is_hovering = is_visible() && p_what == NOTIFICATION_MOUSE_ENTER;
  688. queue_redraw();
  689. } break;
  690. case NOTIFICATION_THEME_CHANGED: {
  691. selected_stylebox = get_theme_stylebox("selected", "Tree");
  692. hovering_stylebox = get_theme_stylebox(SceneStringName(hover), "Tree");
  693. highlighted_font_color = get_theme_color("font_focus_color", EditorStringName(Editor));
  694. } break;
  695. case NOTIFICATION_DRAW: {
  696. if (is_selected) {
  697. draw_style_box(selected_stylebox, Rect2(Point2(), get_size()));
  698. } else if (is_hovering) {
  699. draw_style_box(hovering_stylebox, Rect2(Point2(), get_size()));
  700. }
  701. } break;
  702. }
  703. }
  704. //----------------- List item
  705. static Vector2i _get_path_interval(const Vector2i &p_interval, int p_dir_index) {
  706. if (p_interval.x >= p_dir_index || p_interval.y < 1) {
  707. return { -1, -1 };
  708. }
  709. return { p_interval.x, MIN(p_interval.x + p_interval.y, p_dir_index) - p_interval.x };
  710. }
  711. static Vector2i _get_name_interval(const Vector2i &p_interval, int p_dir_index) {
  712. if (p_interval.x + p_interval.y <= p_dir_index || p_interval.y < 1) {
  713. return { -1, -1 };
  714. }
  715. int first_name_idx = p_dir_index + 1;
  716. int start = MAX(p_interval.x, first_name_idx);
  717. return { start - first_name_idx, p_interval.y - start + p_interval.x };
  718. }
  719. QuickOpenResultListItem::QuickOpenResultListItem() {
  720. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  721. add_theme_constant_override("separation", 4 * EDSCALE);
  722. {
  723. image_container = memnew(MarginContainer);
  724. image_container->add_theme_constant_override("margin_top", 2 * EDSCALE);
  725. image_container->add_theme_constant_override("margin_bottom", 2 * EDSCALE);
  726. image_container->add_theme_constant_override("margin_left", CONTAINER_MARGIN * EDSCALE);
  727. image_container->add_theme_constant_override("margin_right", 0);
  728. add_child(image_container);
  729. thumbnail = memnew(TextureRect);
  730. thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  731. thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  732. thumbnail->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  733. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);
  734. image_container->add_child(thumbnail);
  735. }
  736. {
  737. text_container = memnew(VBoxContainer);
  738. text_container->add_theme_constant_override("separation", -6 * EDSCALE);
  739. text_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  740. text_container->set_v_size_flags(Control::SIZE_FILL);
  741. add_child(text_container);
  742. name = memnew(HighlightedLabel);
  743. name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  744. name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  745. name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT);
  746. text_container->add_child(name);
  747. path = memnew(HighlightedLabel);
  748. path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  749. path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  750. path->add_theme_font_size_override(SceneStringName(font_size), 12 * EDSCALE);
  751. text_container->add_child(path);
  752. }
  753. }
  754. void QuickOpenResultListItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {
  755. thumbnail->set_texture(p_candidate.thumbnail);
  756. name->set_text(p_candidate.file_path.get_file());
  757. path->set_text(p_candidate.file_path.get_base_dir());
  758. name->reset_highlights();
  759. path->reset_highlights();
  760. if (p_highlight && p_candidate.result != nullptr) {
  761. for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {
  762. for (const Vector2i &interval : match.substrings) {
  763. path->add_highlight(_get_path_interval(interval, p_candidate.result->dir_index));
  764. name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));
  765. }
  766. }
  767. }
  768. const int max_size = 32 * EDSCALE;
  769. bool uses_icon = p_candidate.thumbnail->get_width() < max_size;
  770. if (uses_icon) {
  771. thumbnail->set_custom_minimum_size(p_candidate.thumbnail->get_size());
  772. int margin_needed = (max_size - p_candidate.thumbnail->get_width()) / 2;
  773. image_container->add_theme_constant_override("margin_left", CONTAINER_MARGIN + margin_needed);
  774. image_container->add_theme_constant_override("margin_right", margin_needed);
  775. } else {
  776. thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));
  777. image_container->add_theme_constant_override("margin_left", CONTAINER_MARGIN);
  778. image_container->add_theme_constant_override("margin_right", 0);
  779. }
  780. }
  781. void QuickOpenResultListItem::reset() {
  782. thumbnail->set_texture(nullptr);
  783. name->set_text("");
  784. path->set_text("");
  785. name->reset_highlights();
  786. path->reset_highlights();
  787. }
  788. void QuickOpenResultListItem::highlight_item(const Color &p_color) {
  789. name->add_theme_color_override(SceneStringName(font_color), p_color);
  790. }
  791. void QuickOpenResultListItem::remove_highlight() {
  792. name->remove_theme_color_override(SceneStringName(font_color));
  793. }
  794. void QuickOpenResultListItem::_notification(int p_what) {
  795. switch (p_what) {
  796. case NOTIFICATION_THEME_CHANGED: {
  797. path->add_theme_color_override(SceneStringName(font_color), get_theme_color("font_disabled_color", EditorStringName(Editor)));
  798. } break;
  799. }
  800. }
  801. //--------------- Grid Item
  802. QuickOpenResultGridItem::QuickOpenResultGridItem() {
  803. set_h_size_flags(Control::SIZE_FILL);
  804. set_v_size_flags(Control::SIZE_EXPAND_FILL);
  805. add_theme_constant_override("separation", -2 * EDSCALE);
  806. thumbnail = memnew(TextureRect);
  807. thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  808. thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  809. thumbnail->set_custom_minimum_size(Size2i(120 * EDSCALE, 64 * EDSCALE));
  810. add_child(thumbnail);
  811. name = memnew(HighlightedLabel);
  812. name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  813. name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  814. name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  815. name->add_theme_font_size_override(SceneStringName(font_size), 13 * EDSCALE);
  816. add_child(name);
  817. }
  818. void QuickOpenResultGridItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) {
  819. thumbnail->set_texture(p_candidate.thumbnail);
  820. name->set_text(p_candidate.file_path.get_file());
  821. name->set_tooltip_text(p_candidate.file_path);
  822. name->reset_highlights();
  823. if (p_highlight && p_candidate.result != nullptr) {
  824. for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) {
  825. for (const Vector2i &interval : match.substrings) {
  826. name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index));
  827. }
  828. }
  829. }
  830. bool uses_icon = p_candidate.thumbnail->get_width() < (32 * EDSCALE);
  831. if (uses_icon || p_candidate.thumbnail->get_height() <= thumbnail->get_custom_minimum_size().y) {
  832. thumbnail->set_expand_mode(TextureRect::EXPAND_KEEP_SIZE);
  833. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  834. } else {
  835. thumbnail->set_expand_mode(TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL);
  836. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);
  837. }
  838. }
  839. void QuickOpenResultGridItem::reset() {
  840. thumbnail->set_texture(nullptr);
  841. name->set_text("");
  842. name->reset_highlights();
  843. }
  844. void QuickOpenResultGridItem::highlight_item(const Color &p_color) {
  845. name->add_theme_color_override(SceneStringName(font_color), p_color);
  846. }
  847. void QuickOpenResultGridItem::remove_highlight() {
  848. name->remove_theme_color_override(SceneStringName(font_color));
  849. }