editor_quick_open_dialog.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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 "editor/editor_file_system.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_resource_preview.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_string_names.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "scene/gui/center_container.h"
  38. #include "scene/gui/check_button.h"
  39. #include "scene/gui/flow_container.h"
  40. #include "scene/gui/margin_container.h"
  41. #include "scene/gui/panel_container.h"
  42. #include "scene/gui/separator.h"
  43. #include "scene/gui/texture_rect.h"
  44. #include "scene/gui/tree.h"
  45. EditorQuickOpenDialog::EditorQuickOpenDialog() {
  46. VBoxContainer *vbc = memnew(VBoxContainer);
  47. vbc->add_theme_constant_override("separation", 0);
  48. add_child(vbc);
  49. {
  50. // Search bar
  51. MarginContainer *mc = memnew(MarginContainer);
  52. mc->add_theme_constant_override("margin_top", 6);
  53. mc->add_theme_constant_override("margin_bottom", 6);
  54. mc->add_theme_constant_override("margin_left", 1);
  55. mc->add_theme_constant_override("margin_right", 1);
  56. vbc->add_child(mc);
  57. search_box = memnew(LineEdit);
  58. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  59. search_box->set_placeholder(TTR("Search files..."));
  60. search_box->set_clear_button_enabled(true);
  61. mc->add_child(search_box);
  62. }
  63. {
  64. container = memnew(QuickOpenResultContainer);
  65. container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::ok_pressed));
  66. vbc->add_child(container);
  67. }
  68. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed));
  69. search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input));
  70. register_text_enter(search_box);
  71. get_ok_button()->hide();
  72. }
  73. String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) {
  74. if (p_base_types.size() > 1) {
  75. return TTR("Select Resource");
  76. }
  77. if (p_base_types[0] == SNAME("PackedScene")) {
  78. return TTR("Select Scene");
  79. }
  80. return TTR("Select") + " " + p_base_types[0];
  81. }
  82. void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) {
  83. ERR_FAIL_COND(p_base_types.is_empty());
  84. ERR_FAIL_COND(!p_item_selected_callback.is_valid());
  85. item_selected_callback = p_item_selected_callback;
  86. container->init(p_base_types);
  87. get_ok_button()->set_disabled(container->has_nothing_selected());
  88. set_title(get_dialog_title(p_base_types));
  89. popup_centered_clamped(Size2(710, 650) * EDSCALE, 0.8f);
  90. search_box->grab_focus();
  91. }
  92. void EditorQuickOpenDialog::ok_pressed() {
  93. item_selected_callback.call(container->get_selected());
  94. container->save_selected_item();
  95. container->cleanup();
  96. search_box->clear();
  97. hide();
  98. }
  99. void EditorQuickOpenDialog::cancel_pressed() {
  100. container->cleanup();
  101. search_box->clear();
  102. }
  103. void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) {
  104. container->update_results(p_query.to_lower());
  105. get_ok_button()->set_disabled(container->has_nothing_selected());
  106. }
  107. //------------------------- Result Container
  108. QuickOpenResultContainer::QuickOpenResultContainer() {
  109. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  110. set_v_size_flags(Control::SIZE_EXPAND_FILL);
  111. add_theme_constant_override("separation", 0);
  112. {
  113. // Results section
  114. panel_container = memnew(PanelContainer);
  115. panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  116. add_child(panel_container);
  117. {
  118. // No search results
  119. no_results_container = memnew(CenterContainer);
  120. no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  121. no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  122. panel_container->add_child(no_results_container);
  123. no_results_label = memnew(Label);
  124. no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE);
  125. no_results_container->add_child(no_results_label);
  126. no_results_container->hide();
  127. }
  128. {
  129. // Search results
  130. scroll_container = memnew(ScrollContainer);
  131. scroll_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  132. scroll_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  133. scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  134. scroll_container->hide();
  135. panel_container->add_child(scroll_container);
  136. list = memnew(VBoxContainer);
  137. list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  138. list->hide();
  139. scroll_container->add_child(list);
  140. grid = memnew(HFlowContainer);
  141. grid->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  142. grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  143. grid->add_theme_constant_override("v_separation", 18);
  144. grid->add_theme_constant_override("h_separation", 4);
  145. grid->hide();
  146. scroll_container->add_child(grid);
  147. }
  148. }
  149. {
  150. // Bottom bar
  151. HBoxContainer *bottom_bar = memnew(HBoxContainer);
  152. add_child(bottom_bar);
  153. file_details_path = memnew(Label);
  154. file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  155. file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  156. file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  157. bottom_bar->add_child(file_details_path);
  158. {
  159. HBoxContainer *hbc = memnew(HBoxContainer);
  160. hbc->add_theme_constant_override("separation", 3);
  161. bottom_bar->add_child(hbc);
  162. include_addons_toggle = memnew(CheckButton);
  163. include_addons_toggle->set_flat(true);
  164. include_addons_toggle->set_focus_mode(Control::FOCUS_NONE);
  165. include_addons_toggle->set_default_cursor_shape(CURSOR_POINTING_HAND);
  166. include_addons_toggle->set_tooltip_text(TTR("Include files from addons"));
  167. include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons));
  168. hbc->add_child(include_addons_toggle);
  169. VSeparator *vsep = memnew(VSeparator);
  170. vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  171. vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE));
  172. hbc->add_child(vsep);
  173. display_mode_toggle = memnew(Button);
  174. display_mode_toggle->set_flat(true);
  175. display_mode_toggle->set_focus_mode(Control::FOCUS_NONE);
  176. display_mode_toggle->set_default_cursor_shape(CURSOR_POINTING_HAND);
  177. display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode));
  178. hbc->add_child(display_mode_toggle);
  179. }
  180. }
  181. // Creating and deleting nodes while searching is slow, so we allocate
  182. // a bunch of result nodes and fill in the content based on result ranking.
  183. result_items.resize(TOTAL_ALLOCATED_RESULT_ITEMS);
  184. for (int i = 0; i < TOTAL_ALLOCATED_RESULT_ITEMS; i++) {
  185. QuickOpenResultItem *item = memnew(QuickOpenResultItem);
  186. item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i));
  187. result_items.write[i] = item;
  188. }
  189. }
  190. QuickOpenResultContainer::~QuickOpenResultContainer() {
  191. if (never_opened) {
  192. for (QuickOpenResultItem *E : result_items) {
  193. memdelete(E);
  194. }
  195. }
  196. }
  197. void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) {
  198. base_types = p_base_types;
  199. never_opened = false;
  200. const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode");
  201. const bool adaptive_display_mode = (display_mode_behavior == 0);
  202. if (adaptive_display_mode) {
  203. _set_display_mode(get_adaptive_display_mode(p_base_types));
  204. }
  205. const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons");
  206. include_addons_toggle->set_pressed_no_signal(include_addons);
  207. _create_initial_results(include_addons);
  208. }
  209. void QuickOpenResultContainer::_create_initial_results(bool p_include_addons) {
  210. file_type_icons.insert("__default_icon", get_editor_theme_icon(SNAME("Object")));
  211. _find_candidates_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), p_include_addons);
  212. max_total_results = MIN(candidates.size(), TOTAL_ALLOCATED_RESULT_ITEMS);
  213. file_type_icons.clear();
  214. update_results(query);
  215. }
  216. void QuickOpenResultContainer::_find_candidates_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) {
  217. for (int i = 0; i < p_directory->get_subdir_count(); i++) {
  218. if (p_include_addons || p_directory->get_name() != "addons") {
  219. _find_candidates_in_folder(p_directory->get_subdir(i), p_include_addons);
  220. }
  221. }
  222. for (int i = 0; i < p_directory->get_file_count(); i++) {
  223. String file_path = p_directory->get_file_path(i);
  224. const StringName engine_type = p_directory->get_file_type(i);
  225. const StringName script_type = p_directory->get_file_resource_script_class(i);
  226. const bool is_engine_type = script_type == StringName();
  227. const StringName &actual_type = is_engine_type ? engine_type : script_type;
  228. for (const StringName &parent_type : base_types) {
  229. 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));
  230. if (is_valid) {
  231. Candidate c;
  232. c.file_name = file_path.get_file();
  233. c.file_directory = file_path.get_base_dir();
  234. EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(file_path);
  235. if (item.preview.is_valid()) {
  236. c.thumbnail = item.preview;
  237. } else if (file_type_icons.has(actual_type)) {
  238. c.thumbnail = *file_type_icons.lookup_ptr(actual_type);
  239. } else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) {
  240. c.thumbnail = get_editor_theme_icon(actual_type);
  241. file_type_icons.insert(actual_type, c.thumbnail);
  242. } else {
  243. c.thumbnail = *file_type_icons.lookup_ptr("__default_icon");
  244. }
  245. candidates.push_back(c);
  246. break; // Stop testing base types as soon as we get a match.
  247. }
  248. }
  249. }
  250. }
  251. void QuickOpenResultContainer::update_results(const String &p_query) {
  252. query = p_query;
  253. int relevant_candidates = _sort_candidates(p_query);
  254. _update_result_items(MIN(relevant_candidates, max_total_results), 0);
  255. }
  256. int QuickOpenResultContainer::_sort_candidates(const String &p_query) {
  257. if (p_query.is_empty()) {
  258. return 0;
  259. }
  260. const PackedStringArray search_tokens = p_query.to_lower().replace("/", " ").split(" ", false);
  261. if (search_tokens.is_empty()) {
  262. return 0;
  263. }
  264. // First, we assign a score to each candidate.
  265. int num_relevant_candidates = 0;
  266. for (Candidate &c : candidates) {
  267. c.score = 0;
  268. int prev_token_match_pos = -1;
  269. for (const String &token : search_tokens) {
  270. const int file_pos = c.file_name.findn(token);
  271. const int dir_pos = c.file_directory.findn(token);
  272. const bool file_match = file_pos > -1;
  273. const bool dir_match = dir_pos > -1;
  274. if (!file_match && !dir_match) {
  275. c.score = -1.0f;
  276. break;
  277. }
  278. float token_score = file_match ? 0.6f : 0.1999f;
  279. // Add bias for shorter filenames/paths: they resemble the query more.
  280. const String &matched_string = file_match ? c.file_name : c.file_directory;
  281. int matched_string_token_pos = file_match ? file_pos : dir_pos;
  282. token_score += 0.1f * (1.0f - ((float)matched_string_token_pos / (float)matched_string.length()));
  283. // Add bias if the match happened in the file name, not the extension.
  284. if (file_match) {
  285. int ext_pos = matched_string.rfind(".");
  286. if (ext_pos == -1 || ext_pos > matched_string_token_pos) {
  287. token_score += 0.1f;
  288. }
  289. }
  290. // Add bias if token is in order.
  291. {
  292. int candidate_string_token_pos = file_match ? (c.file_directory.length() + file_pos) : dir_pos;
  293. if (prev_token_match_pos != -1 && candidate_string_token_pos > prev_token_match_pos) {
  294. token_score += 0.2f;
  295. }
  296. prev_token_match_pos = candidate_string_token_pos;
  297. }
  298. c.score += token_score;
  299. }
  300. if (c.score > 0.0f) {
  301. num_relevant_candidates++;
  302. }
  303. }
  304. // Now we will sort the candidates based on score, resolving ties by favoring:
  305. // 1. Shorter file length.
  306. // 2. Shorter directory length.
  307. // 3. Lower alphabetic order.
  308. struct CandidateComparator {
  309. _FORCE_INLINE_ bool operator()(const Candidate &p_a, const Candidate &p_b) const {
  310. if (!Math::is_equal_approx(p_a.score, p_b.score)) {
  311. return p_a.score > p_b.score;
  312. }
  313. if (p_a.file_name.length() != p_b.file_name.length()) {
  314. return p_a.file_name.length() < p_b.file_name.length();
  315. }
  316. if (p_a.file_directory.length() != p_b.file_directory.length()) {
  317. return p_a.file_directory.length() < p_b.file_directory.length();
  318. }
  319. return p_a.file_name < p_b.file_name;
  320. }
  321. };
  322. candidates.sort_custom<CandidateComparator>();
  323. return num_relevant_candidates;
  324. }
  325. void QuickOpenResultContainer::_update_result_items(int p_new_visible_results_count, int p_new_selection_index) {
  326. List<Candidate> *type_history = nullptr;
  327. showing_history = false;
  328. if (query.is_empty()) {
  329. if (candidates.size() <= SHOW_ALL_FILES_THRESHOLD) {
  330. p_new_visible_results_count = candidates.size();
  331. } else {
  332. p_new_visible_results_count = 0;
  333. if (base_types.size() == 1) {
  334. type_history = selected_history.lookup_ptr(base_types[0]);
  335. if (type_history) {
  336. p_new_visible_results_count = type_history->size();
  337. showing_history = true;
  338. }
  339. }
  340. }
  341. }
  342. // Only need to update items that were not hidden in previous update.
  343. int num_items_needing_updates = MAX(num_visible_results, p_new_visible_results_count);
  344. num_visible_results = p_new_visible_results_count;
  345. for (int i = 0; i < num_items_needing_updates; i++) {
  346. QuickOpenResultItem *item = result_items[i];
  347. if (i < num_visible_results) {
  348. if (type_history) {
  349. const Candidate &c = type_history->get(i);
  350. item->set_content(c.thumbnail, c.file_name, c.file_directory);
  351. } else {
  352. const Candidate &c = candidates[i];
  353. item->set_content(c.thumbnail, c.file_name, c.file_directory);
  354. }
  355. } else {
  356. item->reset();
  357. }
  358. };
  359. const bool any_results = num_visible_results > 0;
  360. _select_item(any_results ? p_new_selection_index : -1);
  361. scroll_container->set_visible(any_results);
  362. no_results_container->set_visible(!any_results);
  363. if (!any_results) {
  364. if (candidates.is_empty()) {
  365. no_results_label->set_text(TTR("No files found for this type"));
  366. } else if (query.is_empty()) {
  367. no_results_label->set_text(TTR("Start searching to find files..."));
  368. } else {
  369. no_results_label->set_text(TTR("No results found"));
  370. }
  371. }
  372. }
  373. void QuickOpenResultContainer::handle_search_box_input(const Ref<InputEvent> &p_ie) {
  374. if (num_visible_results < 0) {
  375. return;
  376. }
  377. Ref<InputEventKey> key_event = p_ie;
  378. if (key_event.is_valid() && key_event->is_pressed()) {
  379. bool move_selection = false;
  380. switch (key_event->get_keycode()) {
  381. case Key::UP:
  382. case Key::DOWN:
  383. case Key::PAGEUP:
  384. case Key::PAGEDOWN: {
  385. move_selection = true;
  386. } break;
  387. case Key::LEFT:
  388. case Key::RIGHT: {
  389. // Both grid and the search box use left/right keys. By default, grid will take it.
  390. // It would be nice if we could check for ALT to give the event to the searchbox cursor.
  391. // However, if you press ALT, the searchbox also denies the input.
  392. move_selection = (content_display_mode == QuickOpenDisplayMode::GRID);
  393. } break;
  394. default:
  395. break; // Let the event through so it will reach the search box.
  396. }
  397. if (move_selection) {
  398. _move_selection_index(key_event->get_keycode());
  399. queue_redraw();
  400. accept_event();
  401. }
  402. }
  403. }
  404. void QuickOpenResultContainer::_move_selection_index(Key p_key) {
  405. const int max_index = num_visible_results - 1;
  406. int idx = selection_index;
  407. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  408. if (p_key == Key::UP) {
  409. idx = (idx == 0) ? max_index : (idx - 1);
  410. } else if (p_key == Key::DOWN) {
  411. idx = (idx == max_index) ? 0 : (idx + 1);
  412. } else if (p_key == Key::PAGEUP) {
  413. idx = (idx == 0) ? idx : MAX(idx - 10, 0);
  414. } else if (p_key == Key::PAGEDOWN) {
  415. idx = (idx == max_index) ? idx : MIN(idx + 10, max_index);
  416. }
  417. } else {
  418. int column_count = grid->get_line_max_child_count();
  419. if (p_key == Key::LEFT) {
  420. idx = (idx == 0) ? max_index : (idx - 1);
  421. } else if (p_key == Key::RIGHT) {
  422. idx = (idx == max_index) ? 0 : (idx + 1);
  423. } else if (p_key == Key::UP) {
  424. idx = (idx == 0) ? max_index : MAX(idx - column_count, 0);
  425. } else if (p_key == Key::DOWN) {
  426. idx = (idx == max_index) ? 0 : MIN(idx + column_count, max_index);
  427. } else if (p_key == Key::PAGEUP) {
  428. idx = (idx == 0) ? idx : MAX(idx - (3 * column_count), 0);
  429. } else if (p_key == Key::PAGEDOWN) {
  430. idx = (idx == max_index) ? idx : MIN(idx + (3 * column_count), max_index);
  431. }
  432. }
  433. _select_item(idx);
  434. }
  435. void QuickOpenResultContainer::_select_item(int p_index) {
  436. if (!has_nothing_selected()) {
  437. result_items[selection_index]->highlight_item(false);
  438. }
  439. selection_index = p_index;
  440. if (has_nothing_selected()) {
  441. file_details_path->set_text("");
  442. return;
  443. }
  444. result_items[selection_index]->highlight_item(true);
  445. file_details_path->set_text(get_selected() + (showing_history ? TTR(" (recently opened)") : ""));
  446. const QuickOpenResultItem *item = result_items[selection_index];
  447. // Copied from Tree.
  448. const int selected_position = item->get_position().y;
  449. const int selected_size = item->get_size().y;
  450. const int scroll_window_size = scroll_container->get_size().y;
  451. const int scroll_position = scroll_container->get_v_scroll();
  452. if (selected_position <= scroll_position) {
  453. scroll_container->set_v_scroll(selected_position);
  454. } else if (selected_position + selected_size > scroll_position + scroll_window_size) {
  455. scroll_container->set_v_scroll(selected_position + selected_size - scroll_window_size);
  456. }
  457. }
  458. void QuickOpenResultContainer::_item_input(const Ref<InputEvent> &p_ev, int p_index) {
  459. Ref<InputEventMouseButton> mb = p_ev;
  460. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  461. _select_item(p_index);
  462. emit_signal(SNAME("result_clicked"));
  463. }
  464. }
  465. void QuickOpenResultContainer::_toggle_include_addons(bool p_pressed) {
  466. EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/include_addons", p_pressed);
  467. cleanup();
  468. _create_initial_results(p_pressed);
  469. }
  470. void QuickOpenResultContainer::_toggle_display_mode() {
  471. QuickOpenDisplayMode new_display_mode = (content_display_mode == QuickOpenDisplayMode::LIST) ? QuickOpenDisplayMode::GRID : QuickOpenDisplayMode::LIST;
  472. _set_display_mode(new_display_mode);
  473. }
  474. void QuickOpenResultContainer::_set_display_mode(QuickOpenDisplayMode p_display_mode) {
  475. content_display_mode = p_display_mode;
  476. const bool show_list = (content_display_mode == QuickOpenDisplayMode::LIST);
  477. if ((show_list && list->is_visible()) || (!show_list && grid->is_visible())) {
  478. return;
  479. }
  480. hide();
  481. // Move result item nodes from one container to the other.
  482. CanvasItem *prev_root;
  483. CanvasItem *next_root;
  484. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  485. prev_root = Object::cast_to<CanvasItem>(grid);
  486. next_root = Object::cast_to<CanvasItem>(list);
  487. } else {
  488. prev_root = Object::cast_to<CanvasItem>(list);
  489. next_root = Object::cast_to<CanvasItem>(grid);
  490. }
  491. const bool first_time = !list->is_visible() && !grid->is_visible();
  492. prev_root->hide();
  493. for (QuickOpenResultItem *item : result_items) {
  494. item->set_display_mode(content_display_mode);
  495. if (!first_time) {
  496. prev_root->remove_child(item);
  497. }
  498. next_root->add_child(item);
  499. }
  500. next_root->show();
  501. show();
  502. _update_result_items(num_visible_results, selection_index);
  503. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  504. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
  505. display_mode_toggle->set_tooltip_text(TTR("Grid view"));
  506. } else {
  507. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  508. display_mode_toggle->set_tooltip_text(TTR("List view"));
  509. }
  510. }
  511. bool QuickOpenResultContainer::has_nothing_selected() const {
  512. return selection_index < 0;
  513. }
  514. String QuickOpenResultContainer::get_selected() const {
  515. ERR_FAIL_COND_V_MSG(has_nothing_selected(), String(), "Tried to get selected file, but nothing was selected.");
  516. if (showing_history) {
  517. const List<Candidate> *type_history = selected_history.lookup_ptr(base_types[0]);
  518. const Candidate &c = type_history->get(selection_index);
  519. return c.file_directory.path_join(c.file_name);
  520. } else {
  521. const Candidate &c = candidates[selection_index];
  522. return c.file_directory.path_join(c.file_name);
  523. }
  524. }
  525. QuickOpenDisplayMode QuickOpenResultContainer::get_adaptive_display_mode(const Vector<StringName> &p_base_types) {
  526. static const Vector<StringName> grid_preferred_types = {
  527. "Font",
  528. "Texture2D",
  529. "Material",
  530. "Mesh"
  531. };
  532. for (const StringName &type : grid_preferred_types) {
  533. for (const StringName &base_type : p_base_types) {
  534. if (base_type == type || ClassDB::is_parent_class(base_type, type))
  535. return QuickOpenDisplayMode::GRID;
  536. }
  537. }
  538. return QuickOpenDisplayMode::LIST;
  539. }
  540. void QuickOpenResultContainer::save_selected_item() {
  541. if (base_types.size() > 1) {
  542. // Getting the type of the file and checking which base type it belongs to should be possible.
  543. // However, for now these are not supported, and we don't record this.
  544. return;
  545. }
  546. if (showing_history) {
  547. // Selecting from history, so already added.
  548. return;
  549. }
  550. const StringName &base_type = base_types[0];
  551. List<Candidate> *type_history = selected_history.lookup_ptr(base_type);
  552. if (!type_history) {
  553. selected_history.insert(base_type, List<Candidate>());
  554. type_history = selected_history.lookup_ptr(base_type);
  555. } else {
  556. const Candidate &selected = candidates[selection_index];
  557. for (const Candidate &candidate : *type_history) {
  558. if (candidate.file_directory == selected.file_directory && candidate.file_name == selected.file_name) {
  559. return;
  560. }
  561. }
  562. if (type_history->size() > 8) {
  563. type_history->pop_back();
  564. }
  565. }
  566. type_history->push_front(candidates[selection_index]);
  567. }
  568. void QuickOpenResultContainer::cleanup() {
  569. num_visible_results = 0;
  570. candidates.clear();
  571. _select_item(-1);
  572. for (QuickOpenResultItem *item : result_items) {
  573. item->reset();
  574. }
  575. }
  576. void QuickOpenResultContainer::_notification(int p_what) {
  577. switch (p_what) {
  578. case NOTIFICATION_THEME_CHANGED: {
  579. Color text_color = get_theme_color("font_readonly_color", EditorStringName(Editor));
  580. file_details_path->add_theme_color_override(SceneStringName(font_color), text_color);
  581. no_results_label->add_theme_color_override(SceneStringName(font_color), text_color);
  582. panel_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  583. if (content_display_mode == QuickOpenDisplayMode::LIST) {
  584. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail")));
  585. } else {
  586. display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  587. }
  588. } break;
  589. }
  590. }
  591. void QuickOpenResultContainer::_bind_methods() {
  592. ADD_SIGNAL(MethodInfo("result_clicked"));
  593. }
  594. //------------------------- Result Item
  595. QuickOpenResultItem::QuickOpenResultItem() {
  596. set_focus_mode(FocusMode::FOCUS_ALL);
  597. _set_enabled(false);
  598. set_default_cursor_shape(CURSOR_POINTING_HAND);
  599. list_item = memnew(QuickOpenResultListItem);
  600. list_item->hide();
  601. add_child(list_item);
  602. grid_item = memnew(QuickOpenResultGridItem);
  603. grid_item->hide();
  604. add_child(grid_item);
  605. }
  606. void QuickOpenResultItem::set_display_mode(QuickOpenDisplayMode p_display_mode) {
  607. if (p_display_mode == QuickOpenDisplayMode::LIST) {
  608. grid_item->hide();
  609. list_item->show();
  610. } else {
  611. list_item->hide();
  612. grid_item->show();
  613. }
  614. queue_redraw();
  615. }
  616. void QuickOpenResultItem::set_content(const Ref<Texture2D> &p_thumbnail, const String &p_file, const String &p_file_directory) {
  617. _set_enabled(true);
  618. if (list_item->is_visible()) {
  619. list_item->set_content(p_thumbnail, p_file, p_file_directory);
  620. } else {
  621. grid_item->set_content(p_thumbnail, p_file);
  622. }
  623. }
  624. void QuickOpenResultItem::reset() {
  625. _set_enabled(false);
  626. is_hovering = false;
  627. is_selected = false;
  628. if (list_item->is_visible()) {
  629. list_item->reset();
  630. } else {
  631. grid_item->reset();
  632. }
  633. }
  634. void QuickOpenResultItem::highlight_item(bool p_enabled) {
  635. is_selected = p_enabled;
  636. if (list_item->is_visible()) {
  637. if (p_enabled) {
  638. list_item->highlight_item(highlighted_font_color);
  639. } else {
  640. list_item->remove_highlight();
  641. }
  642. } else {
  643. if (p_enabled) {
  644. grid_item->highlight_item(highlighted_font_color);
  645. } else {
  646. grid_item->remove_highlight();
  647. }
  648. }
  649. queue_redraw();
  650. }
  651. void QuickOpenResultItem::_set_enabled(bool p_enabled) {
  652. set_visible(p_enabled);
  653. set_process(p_enabled);
  654. set_process_input(p_enabled);
  655. }
  656. void QuickOpenResultItem::_notification(int p_what) {
  657. switch (p_what) {
  658. case NOTIFICATION_MOUSE_ENTER:
  659. case NOTIFICATION_MOUSE_EXIT: {
  660. is_hovering = is_visible() && p_what == NOTIFICATION_MOUSE_ENTER;
  661. queue_redraw();
  662. } break;
  663. case NOTIFICATION_THEME_CHANGED: {
  664. selected_stylebox = get_theme_stylebox("selected", "Tree");
  665. hovering_stylebox = get_theme_stylebox("hover", "Tree");
  666. highlighted_font_color = get_theme_color("font_focus_color", EditorStringName(Editor));
  667. } break;
  668. case NOTIFICATION_DRAW: {
  669. if (is_selected) {
  670. draw_style_box(selected_stylebox, Rect2(Point2(), get_size()));
  671. } else if (is_hovering) {
  672. draw_style_box(hovering_stylebox, Rect2(Point2(), get_size()));
  673. }
  674. } break;
  675. }
  676. }
  677. //----------------- List item
  678. QuickOpenResultListItem::QuickOpenResultListItem() {
  679. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  680. add_theme_constant_override("separation", 4 * EDSCALE);
  681. {
  682. image_container = memnew(MarginContainer);
  683. image_container->add_theme_constant_override("margin_top", 2 * EDSCALE);
  684. image_container->add_theme_constant_override("margin_bottom", 2 * EDSCALE);
  685. image_container->add_theme_constant_override("margin_left", CONTAINER_MARGIN * EDSCALE);
  686. image_container->add_theme_constant_override("margin_right", 0);
  687. add_child(image_container);
  688. thumbnail = memnew(TextureRect);
  689. thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  690. thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  691. thumbnail->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  692. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);
  693. image_container->add_child(thumbnail);
  694. }
  695. {
  696. text_container = memnew(VBoxContainer);
  697. text_container->add_theme_constant_override("separation", -6 * EDSCALE);
  698. text_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  699. text_container->set_v_size_flags(Control::SIZE_FILL);
  700. add_child(text_container);
  701. name = memnew(Label);
  702. name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  703. name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  704. name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT);
  705. text_container->add_child(name);
  706. path = memnew(Label);
  707. path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  708. path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  709. path->add_theme_font_size_override(SceneStringName(font_size), 12 * EDSCALE);
  710. text_container->add_child(path);
  711. }
  712. }
  713. void QuickOpenResultListItem::set_content(const Ref<Texture2D> &p_thumbnail, const String &p_file, const String &p_file_directory) {
  714. thumbnail->set_texture(p_thumbnail);
  715. name->set_text(p_file);
  716. path->set_text(p_file_directory);
  717. const int max_size = 32 * EDSCALE;
  718. bool uses_icon = p_thumbnail->get_width() < max_size;
  719. if (uses_icon) {
  720. thumbnail->set_custom_minimum_size(p_thumbnail->get_size());
  721. int margin_needed = (max_size - p_thumbnail->get_width()) / 2;
  722. image_container->add_theme_constant_override("margin_left", CONTAINER_MARGIN + margin_needed);
  723. image_container->add_theme_constant_override("margin_right", margin_needed);
  724. } else {
  725. thumbnail->set_custom_minimum_size(Size2i(max_size, max_size));
  726. image_container->add_theme_constant_override("margin_left", CONTAINER_MARGIN);
  727. image_container->add_theme_constant_override("margin_right", 0);
  728. }
  729. }
  730. void QuickOpenResultListItem::reset() {
  731. name->set_text("");
  732. thumbnail->set_texture(nullptr);
  733. path->set_text("");
  734. }
  735. void QuickOpenResultListItem::highlight_item(const Color &p_color) {
  736. name->add_theme_color_override(SceneStringName(font_color), p_color);
  737. }
  738. void QuickOpenResultListItem::remove_highlight() {
  739. name->remove_theme_color_override(SceneStringName(font_color));
  740. }
  741. void QuickOpenResultListItem::_notification(int p_what) {
  742. switch (p_what) {
  743. case NOTIFICATION_THEME_CHANGED: {
  744. path->add_theme_color_override(SceneStringName(font_color), get_theme_color("font_disabled_color", EditorStringName(Editor)));
  745. } break;
  746. }
  747. }
  748. //--------------- Grid Item
  749. QuickOpenResultGridItem::QuickOpenResultGridItem() {
  750. set_h_size_flags(Control::SIZE_FILL);
  751. set_v_size_flags(Control::SIZE_EXPAND_FILL);
  752. add_theme_constant_override("separation", -2 * EDSCALE);
  753. thumbnail = memnew(TextureRect);
  754. thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  755. thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  756. thumbnail->set_custom_minimum_size(Size2i(80 * EDSCALE, 64 * EDSCALE));
  757. add_child(thumbnail);
  758. name = memnew(Label);
  759. name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  760. name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  761. name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  762. name->add_theme_font_size_override(SceneStringName(font_size), 13 * EDSCALE);
  763. add_child(name);
  764. }
  765. void QuickOpenResultGridItem::set_content(const Ref<Texture2D> &p_thumbnail, const String &p_file) {
  766. thumbnail->set_texture(p_thumbnail);
  767. const String &file_name = p_file.get_basename();
  768. name->set_text(file_name);
  769. name->set_tooltip_text(file_name);
  770. bool uses_icon = p_thumbnail->get_width() < (32 * EDSCALE);
  771. if (uses_icon || p_thumbnail->get_height() <= thumbnail->get_custom_minimum_size().y) {
  772. thumbnail->set_expand_mode(TextureRect::EXPAND_KEEP_SIZE);
  773. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  774. } else {
  775. thumbnail->set_expand_mode(TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL);
  776. thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE);
  777. }
  778. }
  779. void QuickOpenResultGridItem::reset() {
  780. name->set_text("");
  781. thumbnail->set_texture(nullptr);
  782. }
  783. void QuickOpenResultGridItem::highlight_item(const Color &p_color) {
  784. name->add_theme_color_override(SceneStringName(font_color), p_color);
  785. }
  786. void QuickOpenResultGridItem::remove_highlight() {
  787. name->remove_theme_color_override(SceneStringName(font_color));
  788. }