editor_help_search.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*************************************************************************/
  2. /* editor_help_search.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_help_search.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor_feature_profile.h"
  33. #include "editor_node.h"
  34. #include "editor_scale.h"
  35. void EditorHelpSearch::_update_icons() {
  36. search_box->set_right_icon(results_tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  37. search_box->set_clear_button_enabled(true);
  38. search_box->add_theme_icon_override("right_icon", results_tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  39. case_sensitive_button->set_icon(results_tree->get_theme_icon(SNAME("MatchCase"), SNAME("EditorIcons")));
  40. hierarchy_button->set_icon(results_tree->get_theme_icon(SNAME("ClassList"), SNAME("EditorIcons")));
  41. if (is_visible()) {
  42. _update_results();
  43. }
  44. }
  45. void EditorHelpSearch::_update_results() {
  46. String term = search_box->get_text();
  47. int search_flags = filter_combo->get_selected_id();
  48. if (case_sensitive_button->is_pressed()) {
  49. search_flags |= SEARCH_CASE_SENSITIVE;
  50. }
  51. if (hierarchy_button->is_pressed()) {
  52. search_flags |= SEARCH_SHOW_HIERARCHY;
  53. }
  54. search = Ref<Runner>(memnew(Runner(results_tree, results_tree, term, search_flags)));
  55. set_process(true);
  56. }
  57. void EditorHelpSearch::_search_box_gui_input(const Ref<InputEvent> &p_event) {
  58. // Redirect up and down navigational key events to the results list.
  59. Ref<InputEventKey> key = p_event;
  60. if (key.is_valid()) {
  61. switch (key->get_keycode()) {
  62. case KEY_UP:
  63. case KEY_DOWN:
  64. case KEY_PAGEUP:
  65. case KEY_PAGEDOWN: {
  66. results_tree->gui_input(key);
  67. search_box->accept_event();
  68. } break;
  69. default:
  70. break;
  71. }
  72. }
  73. }
  74. void EditorHelpSearch::_search_box_text_changed(const String &p_text) {
  75. _update_results();
  76. }
  77. void EditorHelpSearch::_filter_combo_item_selected(int p_option) {
  78. _update_results();
  79. }
  80. void EditorHelpSearch::_confirmed() {
  81. TreeItem *item = results_tree->get_selected();
  82. if (!item) {
  83. return;
  84. }
  85. // Activate the script editor and emit the signal with the documentation link to display.
  86. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  87. emit_signal(SNAME("go_to_help"), item->get_metadata(0));
  88. hide();
  89. }
  90. void EditorHelpSearch::_notification(int p_what) {
  91. switch (p_what) {
  92. case NOTIFICATION_VISIBILITY_CHANGED: {
  93. if (!is_visible()) {
  94. results_tree->call_deferred(SNAME("clear")); // Wait for the Tree's mouse event propagation.
  95. get_ok_button()->set_disabled(true);
  96. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "search_help", Rect2(get_position(), get_size()));
  97. }
  98. } break;
  99. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  100. _update_icons();
  101. } break;
  102. case NOTIFICATION_ENTER_TREE: {
  103. connect("confirmed", callable_mp(this, &EditorHelpSearch::_confirmed));
  104. _update_icons();
  105. } break;
  106. case NOTIFICATION_PROCESS: {
  107. // Update background search.
  108. if (search.is_valid()) {
  109. if (search->work()) {
  110. // Search done.
  111. // Only point to the match if it's a new search, and not just reopening a old one.
  112. if (!old_search) {
  113. results_tree->ensure_cursor_is_visible();
  114. } else {
  115. old_search = false;
  116. }
  117. get_ok_button()->set_disabled(!results_tree->get_selected());
  118. search = Ref<Runner>();
  119. set_process(false);
  120. }
  121. } else {
  122. set_process(false);
  123. }
  124. } break;
  125. }
  126. }
  127. void EditorHelpSearch::_bind_methods() {
  128. ADD_SIGNAL(MethodInfo("go_to_help"));
  129. }
  130. void EditorHelpSearch::popup_dialog() {
  131. popup_dialog(search_box->get_text());
  132. }
  133. void EditorHelpSearch::popup_dialog(const String &p_term) {
  134. // Restore valid window bounds or pop up at default size.
  135. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "search_help", Rect2());
  136. if (saved_size != Rect2()) {
  137. popup(saved_size);
  138. } else {
  139. popup_centered_ratio(0.5F);
  140. }
  141. if (p_term == "") {
  142. search_box->clear();
  143. } else {
  144. if (old_term == p_term) {
  145. old_search = true;
  146. } else {
  147. old_term = p_term;
  148. }
  149. search_box->set_text(p_term);
  150. search_box->select_all();
  151. }
  152. search_box->grab_focus();
  153. _update_results();
  154. }
  155. EditorHelpSearch::EditorHelpSearch() {
  156. old_search = false;
  157. set_hide_on_ok(false);
  158. set_title(TTR("Search Help"));
  159. get_ok_button()->set_disabled(true);
  160. get_ok_button()->set_text(TTR("Open"));
  161. // Split search and results area.
  162. VBoxContainer *vbox = memnew(VBoxContainer);
  163. add_child(vbox);
  164. // Create the search box and filter controls (at the top).
  165. HBoxContainer *hbox = memnew(HBoxContainer);
  166. vbox->add_child(hbox);
  167. search_box = memnew(LineEdit);
  168. search_box->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  169. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  170. search_box->connect("gui_input", callable_mp(this, &EditorHelpSearch::_search_box_gui_input));
  171. search_box->connect("text_changed", callable_mp(this, &EditorHelpSearch::_search_box_text_changed));
  172. register_text_enter(search_box);
  173. hbox->add_child(search_box);
  174. case_sensitive_button = memnew(Button);
  175. case_sensitive_button->set_flat(true);
  176. case_sensitive_button->set_tooltip(TTR("Case Sensitive"));
  177. case_sensitive_button->connect("pressed", callable_mp(this, &EditorHelpSearch::_update_results));
  178. case_sensitive_button->set_toggle_mode(true);
  179. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  180. hbox->add_child(case_sensitive_button);
  181. hierarchy_button = memnew(Button);
  182. hierarchy_button->set_flat(true);
  183. hierarchy_button->set_tooltip(TTR("Show Hierarchy"));
  184. hierarchy_button->connect("pressed", callable_mp(this, &EditorHelpSearch::_update_results));
  185. hierarchy_button->set_toggle_mode(true);
  186. hierarchy_button->set_pressed(true);
  187. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  188. hbox->add_child(hierarchy_button);
  189. filter_combo = memnew(OptionButton);
  190. filter_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  191. filter_combo->set_stretch_ratio(0); // Fixed width.
  192. filter_combo->add_item(TTR("Display All"), SEARCH_ALL);
  193. filter_combo->add_separator();
  194. filter_combo->add_item(TTR("Classes Only"), SEARCH_CLASSES);
  195. filter_combo->add_item(TTR("Constructors Only"), SEARCH_CONSTRUCTORS);
  196. filter_combo->add_item(TTR("Methods Only"), SEARCH_METHODS);
  197. filter_combo->add_item(TTR("Operators Only"), SEARCH_OPERATORS);
  198. filter_combo->add_item(TTR("Signals Only"), SEARCH_SIGNALS);
  199. filter_combo->add_item(TTR("Constants Only"), SEARCH_CONSTANTS);
  200. filter_combo->add_item(TTR("Properties Only"), SEARCH_PROPERTIES);
  201. filter_combo->add_item(TTR("Theme Properties Only"), SEARCH_THEME_ITEMS);
  202. filter_combo->connect("item_selected", callable_mp(this, &EditorHelpSearch::_filter_combo_item_selected));
  203. hbox->add_child(filter_combo);
  204. // Create the results tree.
  205. results_tree = memnew(Tree);
  206. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  207. results_tree->set_columns(2);
  208. results_tree->set_column_title(0, TTR("Name"));
  209. results_tree->set_column_clip_content(0, true);
  210. results_tree->set_column_title(1, TTR("Member Type"));
  211. results_tree->set_column_expand(1, false);
  212. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  213. results_tree->set_column_clip_content(1, true);
  214. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  215. results_tree->set_hide_root(true);
  216. results_tree->set_select_mode(Tree::SELECT_ROW);
  217. results_tree->connect("item_activated", callable_mp(this, &EditorHelpSearch::_confirmed));
  218. results_tree->connect("item_selected", callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled), varray(false));
  219. vbox->add_child(results_tree, true);
  220. }
  221. bool EditorHelpSearch::Runner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  222. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  223. if (profile.is_null()) {
  224. return false;
  225. }
  226. StringName class_name = p_class;
  227. while (class_name != StringName()) {
  228. if (!ClassDB::class_exists(class_name)) {
  229. return false;
  230. }
  231. if (profile->is_class_disabled(class_name)) {
  232. return true;
  233. }
  234. class_name = ClassDB::get_parent_class(class_name);
  235. }
  236. return false;
  237. }
  238. bool EditorHelpSearch::Runner::_slice() {
  239. bool phase_done = false;
  240. switch (phase) {
  241. case PHASE_MATCH_CLASSES_INIT:
  242. phase_done = _phase_match_classes_init();
  243. break;
  244. case PHASE_MATCH_CLASSES:
  245. phase_done = _phase_match_classes();
  246. break;
  247. case PHASE_CLASS_ITEMS_INIT:
  248. phase_done = _phase_class_items_init();
  249. break;
  250. case PHASE_CLASS_ITEMS:
  251. phase_done = _phase_class_items();
  252. break;
  253. case PHASE_MEMBER_ITEMS_INIT:
  254. phase_done = _phase_member_items_init();
  255. break;
  256. case PHASE_MEMBER_ITEMS:
  257. phase_done = _phase_member_items();
  258. break;
  259. case PHASE_SELECT_MATCH:
  260. phase_done = _phase_select_match();
  261. break;
  262. case PHASE_MAX:
  263. return true;
  264. default:
  265. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  266. return true;
  267. };
  268. if (phase_done) {
  269. phase++;
  270. }
  271. return false;
  272. }
  273. bool EditorHelpSearch::Runner::_phase_match_classes_init() {
  274. iterator_doc = EditorHelp::get_doc_data()->class_list.front();
  275. matches.clear();
  276. matched_item = nullptr;
  277. match_highest_score = 0;
  278. return true;
  279. }
  280. bool EditorHelpSearch::Runner::_phase_match_classes() {
  281. DocData::ClassDoc &class_doc = iterator_doc->value();
  282. if (!_is_class_disabled_by_feature_profile(class_doc.name)) {
  283. matches[class_doc.name] = ClassMatch();
  284. ClassMatch &match = matches[class_doc.name];
  285. match.doc = &class_doc;
  286. // Match class name.
  287. if (search_flags & SEARCH_CLASSES) {
  288. match.name = term == "" || _match_string(term, class_doc.name);
  289. }
  290. // Match members if the term is long enough.
  291. if (term.length() > 1) {
  292. if (search_flags & SEARCH_CONSTRUCTORS) {
  293. for (int i = 0; i < class_doc.constructors.size(); i++) {
  294. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.constructors[i].name : class_doc.constructors[i].name.to_lower();
  295. if (method_name.find(term) > -1 ||
  296. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  297. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  298. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  299. match.constructors.push_back(const_cast<DocData::MethodDoc *>(&class_doc.constructors[i]));
  300. }
  301. }
  302. }
  303. if (search_flags & SEARCH_METHODS) {
  304. for (int i = 0; i < class_doc.methods.size(); i++) {
  305. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.methods[i].name : class_doc.methods[i].name.to_lower();
  306. if (method_name.find(term) > -1 ||
  307. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  308. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  309. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  310. match.methods.push_back(const_cast<DocData::MethodDoc *>(&class_doc.methods[i]));
  311. }
  312. }
  313. }
  314. if (search_flags & SEARCH_OPERATORS) {
  315. for (int i = 0; i < class_doc.operators.size(); i++) {
  316. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.operators[i].name : class_doc.operators[i].name.to_lower();
  317. if (method_name.find(term) > -1 ||
  318. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  319. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  320. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  321. match.operators.push_back(const_cast<DocData::MethodDoc *>(&class_doc.operators[i]));
  322. }
  323. }
  324. }
  325. if (search_flags & SEARCH_SIGNALS) {
  326. for (int i = 0; i < class_doc.signals.size(); i++) {
  327. if (_match_string(term, class_doc.signals[i].name)) {
  328. match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
  329. }
  330. }
  331. }
  332. if (search_flags & SEARCH_CONSTANTS) {
  333. for (int i = 0; i < class_doc.constants.size(); i++) {
  334. if (_match_string(term, class_doc.constants[i].name)) {
  335. match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
  336. }
  337. }
  338. }
  339. if (search_flags & SEARCH_PROPERTIES) {
  340. for (int i = 0; i < class_doc.properties.size(); i++) {
  341. if (_match_string(term, class_doc.properties[i].name) || _match_string(term, class_doc.properties[i].getter) || _match_string(term, class_doc.properties[i].setter)) {
  342. match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
  343. }
  344. }
  345. }
  346. if (search_flags & SEARCH_THEME_ITEMS) {
  347. for (int i = 0; i < class_doc.theme_properties.size(); i++) {
  348. if (_match_string(term, class_doc.theme_properties[i].name)) {
  349. match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
  350. }
  351. }
  352. }
  353. }
  354. }
  355. iterator_doc = iterator_doc->next();
  356. return !iterator_doc;
  357. }
  358. bool EditorHelpSearch::Runner::_phase_class_items_init() {
  359. iterator_match = matches.front();
  360. results_tree->clear();
  361. root_item = results_tree->create_item();
  362. class_items.clear();
  363. return true;
  364. }
  365. bool EditorHelpSearch::Runner::_phase_class_items() {
  366. ClassMatch &match = iterator_match->value();
  367. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  368. if (match.required()) {
  369. _create_class_hierarchy(match);
  370. }
  371. } else {
  372. if (match.name) {
  373. _create_class_item(root_item, match.doc, false);
  374. }
  375. }
  376. iterator_match = iterator_match->next();
  377. return !iterator_match;
  378. }
  379. bool EditorHelpSearch::Runner::_phase_member_items_init() {
  380. iterator_match = matches.front();
  381. return true;
  382. }
  383. bool EditorHelpSearch::Runner::_phase_member_items() {
  384. ClassMatch &match = iterator_match->value();
  385. TreeItem *parent = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  386. bool constructor_created = false;
  387. for (int i = 0; i < match.methods.size(); i++) {
  388. String text = match.methods[i]->name;
  389. if (!constructor_created) {
  390. if (match.doc->name == match.methods[i]->name) {
  391. text += " " + TTR("(constructors)");
  392. constructor_created = true;
  393. }
  394. } else {
  395. if (match.doc->name == match.methods[i]->name) {
  396. continue;
  397. }
  398. }
  399. _create_method_item(parent, match.doc, text, match.methods[i]);
  400. }
  401. for (int i = 0; i < match.signals.size(); i++) {
  402. _create_signal_item(parent, match.doc, match.signals[i]);
  403. }
  404. for (int i = 0; i < match.constants.size(); i++) {
  405. _create_constant_item(parent, match.doc, match.constants[i]);
  406. }
  407. for (int i = 0; i < match.properties.size(); i++) {
  408. _create_property_item(parent, match.doc, match.properties[i]);
  409. }
  410. for (int i = 0; i < match.theme_properties.size(); i++) {
  411. _create_theme_property_item(parent, match.doc, match.theme_properties[i]);
  412. }
  413. iterator_match = iterator_match->next();
  414. return !iterator_match;
  415. }
  416. bool EditorHelpSearch::Runner::_phase_select_match() {
  417. if (matched_item) {
  418. matched_item->select(0);
  419. }
  420. return true;
  421. }
  422. bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
  423. if (search_flags & SEARCH_CASE_SENSITIVE) {
  424. return p_string.find(p_term) > -1;
  425. } else {
  426. return p_string.findn(p_term) > -1;
  427. }
  428. }
  429. void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text) {
  430. float inverse_length = 1.f / float(p_text.length());
  431. // Favor types where search term is a substring close to the start of the type.
  432. float w = 0.5f;
  433. int pos = p_text.findn(term);
  434. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  435. // Favor shorter items: they resemble the search term more.
  436. w = 0.1f;
  437. score *= (1 - w) + w * (term.length() * inverse_length);
  438. if (match_highest_score == 0 || score > match_highest_score) {
  439. matched_item = p_item;
  440. match_highest_score = score;
  441. }
  442. }
  443. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const ClassMatch &p_match) {
  444. if (class_items.has(p_match.doc->name)) {
  445. return class_items[p_match.doc->name];
  446. }
  447. // Ensure parent nodes are created first.
  448. TreeItem *parent = root_item;
  449. if (p_match.doc->inherits != "") {
  450. if (class_items.has(p_match.doc->inherits)) {
  451. parent = class_items[p_match.doc->inherits];
  452. } else {
  453. ClassMatch &base_match = matches[p_match.doc->inherits];
  454. parent = _create_class_hierarchy(base_match);
  455. }
  456. }
  457. TreeItem *class_item = _create_class_item(parent, p_match.doc, !p_match.name);
  458. class_items[p_match.doc->name] = class_item;
  459. return class_item;
  460. }
  461. TreeItem *EditorHelpSearch::Runner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray) {
  462. Ref<Texture2D> icon = empty_icon;
  463. if (ui_service->has_theme_icon(p_doc->name, "EditorIcons")) {
  464. icon = ui_service->get_theme_icon(p_doc->name, "EditorIcons");
  465. } else if (ClassDB::class_exists(p_doc->name) && ClassDB::is_parent_class(p_doc->name, "Object")) {
  466. icon = ui_service->get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  467. }
  468. String tooltip = p_doc->brief_description.strip_edges();
  469. TreeItem *item = results_tree->create_item(p_parent);
  470. item->set_icon(0, icon);
  471. item->set_text(0, p_doc->name);
  472. item->set_text(1, TTR("Class"));
  473. item->set_tooltip(0, tooltip);
  474. item->set_tooltip(1, tooltip);
  475. item->set_metadata(0, "class_name:" + p_doc->name);
  476. if (p_gray) {
  477. item->set_custom_color(0, disabled_color);
  478. item->set_custom_color(1, disabled_color);
  479. }
  480. _match_item(item, p_doc->name);
  481. return item;
  482. }
  483. TreeItem *EditorHelpSearch::Runner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  484. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  485. for (int i = 0; i < p_doc->arguments.size(); i++) {
  486. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  487. tooltip += arg.type + " " + arg.name;
  488. if (arg.default_value != "") {
  489. tooltip += " = " + arg.default_value;
  490. }
  491. if (i < p_doc->arguments.size() - 1) {
  492. tooltip += ", ";
  493. }
  494. }
  495. tooltip += ")";
  496. return _create_member_item(p_parent, p_class_doc->name, "MemberMethod", p_doc->name, p_text, TTRC("Method"), "method", tooltip);
  497. }
  498. TreeItem *EditorHelpSearch::Runner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) {
  499. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  500. for (int i = 0; i < p_doc->arguments.size(); i++) {
  501. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  502. tooltip += arg.type + " " + arg.name;
  503. if (arg.default_value != "") {
  504. tooltip += " = " + arg.default_value;
  505. }
  506. if (i < p_doc->arguments.size() - 1) {
  507. tooltip += ", ";
  508. }
  509. }
  510. tooltip += ")";
  511. return _create_member_item(p_parent, p_class_doc->name, "MemberSignal", p_doc->name, p_doc->name, TTRC("Signal"), "signal", tooltip);
  512. }
  513. TreeItem *EditorHelpSearch::Runner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ConstantDoc *p_doc) {
  514. String tooltip = p_class_doc->name + "." + p_doc->name;
  515. return _create_member_item(p_parent, p_class_doc->name, "MemberConstant", p_doc->name, p_doc->name, TTRC("Constant"), "constant", tooltip);
  516. }
  517. TreeItem *EditorHelpSearch::Runner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::PropertyDoc *p_doc) {
  518. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  519. tooltip += "\n " + p_class_doc->name + "." + p_doc->setter + "(value) setter";
  520. tooltip += "\n " + p_class_doc->name + "." + p_doc->getter + "() getter";
  521. return _create_member_item(p_parent, p_class_doc->name, "MemberProperty", p_doc->name, p_doc->name, TTRC("Property"), "property", tooltip);
  522. }
  523. TreeItem *EditorHelpSearch::Runner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ThemeItemDoc *p_doc) {
  524. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  525. return _create_member_item(p_parent, p_class_doc->name, "MemberTheme", p_doc->name, p_doc->name, TTRC("Theme Property"), "theme_item", tooltip);
  526. }
  527. TreeItem *EditorHelpSearch::Runner::_create_member_item(TreeItem *p_parent, const String &p_class_name, const String &p_icon, const String &p_name, const String &p_text, const String &p_type, const String &p_metatype, const String &p_tooltip) {
  528. Ref<Texture2D> icon;
  529. String text;
  530. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  531. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  532. text = p_text;
  533. } else {
  534. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  535. /*// In flat mode, show the class icon.
  536. if (ui_service->has_icon(p_class_name, "EditorIcons"))
  537. icon = ui_service->get_icon(p_class_name, "EditorIcons");
  538. else if (ClassDB::is_parent_class(p_class_name, "Object"))
  539. icon = ui_service->get_icon("Object", "EditorIcons");*/
  540. text = p_class_name + "." + p_text;
  541. }
  542. TreeItem *item = results_tree->create_item(p_parent);
  543. item->set_icon(0, icon);
  544. item->set_text(0, text);
  545. item->set_text(1, TTRGET(p_type));
  546. item->set_tooltip(0, p_tooltip);
  547. item->set_tooltip(1, p_tooltip);
  548. item->set_metadata(0, "class_" + p_metatype + ":" + p_class_name + ":" + p_name);
  549. _match_item(item, p_name);
  550. return item;
  551. }
  552. bool EditorHelpSearch::Runner::work(uint64_t slot) {
  553. // Return true when the search has been completed, otherwise false.
  554. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  555. while (!_slice()) {
  556. if (OS::get_singleton()->get_ticks_usec() > until) {
  557. return false;
  558. }
  559. }
  560. return true;
  561. }
  562. EditorHelpSearch::Runner::Runner(Control *p_icon_service, Tree *p_results_tree, const String &p_term, int p_search_flags) :
  563. ui_service(p_icon_service),
  564. results_tree(p_results_tree),
  565. term((p_search_flags & SEARCH_CASE_SENSITIVE) == 0 ? p_term.strip_edges().to_lower() : p_term.strip_edges()),
  566. search_flags(p_search_flags),
  567. empty_icon(ui_service->get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"))),
  568. disabled_color(ui_service->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))) {
  569. }