| 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 | |
| 31 | #include "editor_quick_open_dialog.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/string/fuzzy_search.h" |
| 35 | #include "editor/docks/filesystem_dock.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "editor/editor_string_names.h" |
| 38 | #include "editor/editor_undo_redo_manager.h" |
| 39 | #include "editor/file_system/editor_file_system.h" |
| 40 | #include "editor/file_system/editor_paths.h" |
| 41 | #include "editor/gui/editor_toaster.h" |
| 42 | #include "editor/inspector/editor_resource_preview.h" |
| 43 | #include "editor/inspector/multi_node_edit.h" |
| 44 | #include "editor/settings/editor_settings.h" |
| 45 | #include "editor/themes/editor_scale.h" |
| 46 | #include "scene/gui/center_container.h" |
| 47 | #include "scene/gui/check_button.h" |
| 48 | #include "scene/gui/flow_container.h" |
| 49 | #include "scene/gui/line_edit.h" |
| 50 | #include "scene/gui/margin_container.h" |
| 51 | #include "scene/gui/panel_container.h" |
| 52 | #include "scene/gui/separator.h" |
| 53 | #include "scene/gui/texture_rect.h" |
| 54 | #include "scene/gui/tree.h" |
| 55 | |
| 56 | void HighlightedLabel::draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing) { |
| 57 | for (int i = get_lines_skipped(); i < p_line_limit; i++) { |
| 58 | RID line = get_line_rid(i); |
| 59 | Vector<Vector2> ranges = TS->shaped_text_get_selection(line, p_substr.x, p_substr.x + p_substr.y); |
| 60 | Rect2 line_rect = get_line_rect(i); |
| 61 | for (const Vector2 &range : ranges) { |
| 62 | Rect2 rect = Rect2(Point2(range.x, 0) + line_rect.position, Size2(range.y - range.x, line_rect.size.y)); |
| 63 | rect.position = p_offset + line_rect.position; |
| 64 | rect.position.x += range.x; |
| 65 | rect.size = Size2(range.y - range.x, line_rect.size.y); |
| 66 | rect.size.x = MIN(rect.size.x, line_rect.size.x - range.x); |
| 67 | if (rect.size.x > 0) { |
| 68 | draw_rect(rect, Color(1, 1, 1, 0.07), true); |
| 69 | draw_rect(rect, Color(0.5, 0.7, 1.0, 0.4), false, 1); |
| 70 | } |
| 71 | } |
| 72 | p_offset.y += line_spacing + TS->shaped_text_get_ascent(line) + TS->shaped_text_get_descent(line); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void HighlightedLabel::add_highlight(const Vector2i &p_interval) { |
| 77 | if (p_interval.y > 0) { |
| 78 | highlights.append(p_interval); |
| 79 | queue_redraw(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void HighlightedLabel::reset_highlights() { |
| 84 | highlights.clear(); |
| 85 | queue_redraw(); |
| 86 | } |
| 87 | |
| 88 | void HighlightedLabel::_notification(int p_notification) { |
| 89 | if (p_notification == NOTIFICATION_DRAW) { |
| 90 | if (highlights.is_empty()) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | Vector2 offset; |
| 95 | int line_limit; |
| 96 | int line_spacing; |
| 97 | get_layout_data(offset, line_limit, line_spacing); |
| 98 | |
| 99 | for (const Vector2i &substr : highlights) { |
| 100 | draw_substr_rects(substr, offset, line_limit, line_spacing); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | EditorQuickOpenDialog::EditorQuickOpenDialog() { |
| 106 | VBoxContainer *vbc = memnew(VBoxContainer); |
| 107 | vbc->add_theme_constant_override("separation", 0); |
| 108 | add_child(vbc); |
| 109 | |
| 110 | { |
| 111 | // Search bar |
| 112 | MarginContainer *mc = memnew(MarginContainer); |
| 113 | mc->add_theme_constant_override("margin_top", 6); |
| 114 | mc->add_theme_constant_override("margin_bottom", 6); |
| 115 | mc->add_theme_constant_override("margin_left", 1); |
| 116 | mc->add_theme_constant_override("margin_right", 1); |
| 117 | vbc->add_child(mc); |
| 118 | |
| 119 | search_box = memnew(LineEdit); |
| 120 | search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 121 | search_box->set_placeholder(TTR("Search files...")); |
| 122 | search_box->set_accessibility_name(TTRC("Search")); |
| 123 | search_box->set_clear_button_enabled(true); |
| 124 | mc->add_child(search_box); |
| 125 | } |
| 126 | |
| 127 | { |
| 128 | container = memnew(QuickOpenResultContainer); |
| 129 | container->connect("selection_changed", callable_mp(this, &EditorQuickOpenDialog::selection_changed)); |
| 130 | container->connect("result_clicked", callable_mp(this, &EditorQuickOpenDialog::item_pressed)); |
| 131 | vbc->add_child(container); |
| 132 | } |
| 133 | |
| 134 | search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpenDialog::_search_box_text_changed)); |
| 135 | search_box->connect(SceneStringName(gui_input), callable_mp(container, &QuickOpenResultContainer::handle_search_box_input)); |
| 136 | register_text_enter(search_box); |
| 137 | get_ok_button()->hide(); |
| 138 | } |
| 139 | |
| 140 | String EditorQuickOpenDialog::get_dialog_title(const Vector<StringName> &p_base_types) { |
| 141 | if (p_base_types.size() > 1) { |
| 142 | return TTR("Select Resource"); |
| 143 | } |
| 144 | |
| 145 | if (p_base_types[0] == SNAME("PackedScene")) { |
| 146 | return TTR("Select Scene"); |
| 147 | } |
| 148 | |
| 149 | return TTR("Select") + " " + p_base_types[0]; |
| 150 | } |
| 151 | |
| 152 | void EditorQuickOpenDialog::popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback) { |
| 153 | ERR_FAIL_COND(p_base_types.is_empty()); |
| 154 | ERR_FAIL_COND(!p_item_selected_callback.is_valid()); |
| 155 | |
| 156 | property_object = nullptr; |
| 157 | property_path = ""; |
| 158 | item_selected_callback = p_item_selected_callback; |
| 159 | |
| 160 | container->init(p_base_types); |
| 161 | container->set_instant_preview_toggle_visible(false); |
| 162 | _finish_dialog_setup(p_base_types); |
| 163 | } |
| 164 | |
| 165 | void EditorQuickOpenDialog::popup_dialog_for_property(const Vector<StringName> &p_base_types, Object *p_obj, const StringName &p_path, const Callable &p_item_selected_callback) { |
| 166 | ERR_FAIL_NULL(p_obj); |
| 167 | ERR_FAIL_COND(p_base_types.is_empty()); |
| 168 | ERR_FAIL_COND(!p_item_selected_callback.is_valid()); |
| 169 | |
| 170 | property_object = p_obj; |
| 171 | property_path = p_path; |
| 172 | item_selected_callback = p_item_selected_callback; |
| 173 | initial_property_value = property_object->get(property_path); |
| 174 | |
| 175 | // Reset this, so that the property isn't updated immediately upon opening |
| 176 | // the window. |
| 177 | initial_selection_performed = false; |
| 178 | |
| 179 | container->init(p_base_types); |
| 180 | container->set_instant_preview_toggle_visible(true); |
| 181 | _finish_dialog_setup(p_base_types); |
| 182 | } |
| 183 | |
| 184 | void EditorQuickOpenDialog::_finish_dialog_setup(const Vector<StringName> &p_base_types) { |
| 185 | get_ok_button()->set_disabled(container->has_nothing_selected()); |
| 186 | set_title(get_dialog_title(p_base_types)); |
| 187 | popup_centered_clamped(Size2(780, 650) * EDSCALE, 0.8f); |
| 188 | search_box->grab_focus(); |
| 189 | } |
| 190 | |
| 191 | void EditorQuickOpenDialog::ok_pressed() { |
| 192 | container->save_selected_item(); |
| 193 | |
| 194 | update_property(); |
| 195 | container->cleanup(); |
| 196 | search_box->clear(); |
| 197 | hide(); |
| 198 | } |
| 199 | |
| 200 | bool EditorQuickOpenDialog::_is_instant_preview_active() const { |
| 201 | return property_object != nullptr && container->is_instant_preview_enabled(); |
| 202 | } |
| 203 | |
| 204 | void EditorQuickOpenDialog::selection_changed() { |
| 205 | if (!_is_instant_preview_active()) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // This prevents the property from being changed the first time the Quick Open |
| 210 | // window is opened. |
| 211 | if (!initial_selection_performed) { |
| 212 | initial_selection_performed = true; |
| 213 | } else { |
| 214 | preview_property(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void EditorQuickOpenDialog::item_pressed(bool p_double_click) { |
| 219 | // A double-click should always be taken as a "confirm" action. |
| 220 | if (p_double_click) { |
| 221 | ok_pressed(); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // Single-clicks should be taken as a "confirm" action only if Instant Preview |
| 226 | // isn't currently enabled, or the property object is null for some reason. |
| 227 | if (!_is_instant_preview_active()) { |
| 228 | ok_pressed(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void EditorQuickOpenDialog::preview_property() { |
| 233 | Ref<Resource> loaded_resource = ResourceLoader::load(container->get_selected()); |
| 234 | ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + container->get_selected() + "'."); |
| 235 | |
| 236 | Resource *res = Object::cast_to<Resource>(property_object); |
| 237 | if (res) { |
| 238 | HashSet<Resource *> resources_found; |
| 239 | resources_found.insert(res); |
| 240 | if (EditorNode::find_recursive_resources(loaded_resource, resources_found)) { |
| 241 | EditorToaster::get_singleton()->popup_str(TTR("Recursion detected, Instant Preview failed."), EditorToaster::SEVERITY_ERROR); |
| 242 | loaded_resource = Ref<Resource>(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // MultiNodeEdit has adding to the undo/redo stack baked into its set function. |
| 247 | // As such, we have to specifically call a version of its setter that doesn't |
| 248 | // create undo/redo actions. |
| 249 | property_object->set_block_signals(true); |
| 250 | if (Object::cast_to<MultiNodeEdit>(property_object)) { |
| 251 | Object::cast_to<MultiNodeEdit>(property_object)->_set_impl(property_path, loaded_resource, "", false); |
| 252 | } else { |
| 253 | property_object->set(property_path, loaded_resource); |
| 254 | } |
| 255 | property_object->set_block_signals(false); |
| 256 | } |
| 257 | |
| 258 | void EditorQuickOpenDialog::update_property() { |
| 259 | // Set the property back to the initial value first, so that the undo action |
| 260 | // has the correct object. |
| 261 | if (property_object) { |
| 262 | if (Object::cast_to<MultiNodeEdit>(property_object)) { |
| 263 | Object::cast_to<MultiNodeEdit>(property_object)->_set_impl(property_path, initial_property_value, "", false); |
| 264 | } else { |
| 265 | property_object->set(property_path, initial_property_value); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (!item_selected_callback.is_valid()) { |
| 270 | String err_msg = "The callback provided to the Quick Open dialog was invalid."; |
| 271 | if (_is_instant_preview_active()) { |
| 272 | err_msg += " Try disabling \"Instant Preview\" as a workaround."; |
| 273 | } |
| 274 | ERR_FAIL_MSG(err_msg); |
| 275 | } |
| 276 | |
| 277 | item_selected_callback.call(container->get_selected()); |
| 278 | } |
| 279 | |
| 280 | void EditorQuickOpenDialog::cancel_pressed() { |
| 281 | if (property_object) { |
| 282 | if (Object::cast_to<MultiNodeEdit>(property_object)) { |
| 283 | Object::cast_to<MultiNodeEdit>(property_object)->_set_impl(property_path, initial_property_value, "", false); |
| 284 | } else { |
| 285 | property_object->set(property_path, initial_property_value); |
| 286 | } |
| 287 | } |
| 288 | container->cleanup(); |
| 289 | search_box->clear(); |
| 290 | } |
| 291 | |
| 292 | void EditorQuickOpenDialog::_search_box_text_changed(const String &p_query) { |
| 293 | container->set_query_and_update(p_query); |
| 294 | get_ok_button()->set_disabled(container->has_nothing_selected()); |
| 295 | } |
| 296 | |
| 297 | //------------------------- Result Container |
| 298 | |
| 299 | void style_button(Button *p_button) { |
| 300 | p_button->set_flat(true); |
| 301 | p_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 302 | } |
| 303 | |
| 304 | QuickOpenResultContainer::QuickOpenResultContainer() { |
| 305 | set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 306 | set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 307 | add_theme_constant_override("separation", 0); |
| 308 | history_file.instantiate(); |
| 309 | |
| 310 | { |
| 311 | // Results section |
| 312 | panel_container = memnew(PanelContainer); |
| 313 | panel_container->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 314 | add_child(panel_container); |
| 315 | |
| 316 | { |
| 317 | // No search results |
| 318 | no_results_container = memnew(CenterContainer); |
| 319 | no_results_container->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 320 | no_results_container->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 321 | panel_container->add_child(no_results_container); |
| 322 | |
| 323 | no_results_label = memnew(Label); |
| 324 | no_results_label->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 325 | no_results_label->add_theme_font_size_override(SceneStringName(font_size), 24 * EDSCALE); |
| 326 | no_results_container->add_child(no_results_label); |
| 327 | no_results_container->hide(); |
| 328 | } |
| 329 | |
| 330 | { |
| 331 | MarginContainer *mc = memnew(MarginContainer); |
| 332 | mc->set_theme_type_variation("NoBorderHorizontalWindow"); |
| 333 | mc->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 334 | mc->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 335 | panel_container->add_child(mc); |
| 336 | |
| 337 | // Search results |
| 338 | scroll_container = memnew(ScrollContainer); |
| 339 | scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 340 | scroll_container->set_scroll_hint_mode(ScrollContainer::SCROLL_HINT_MODE_ALL); |
| 341 | scroll_container->hide(); |
| 342 | panel_container->add_child(scroll_container); |
| 343 | |
| 344 | list = memnew(VBoxContainer); |
| 345 | list->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 346 | list->add_theme_constant_override(SNAME("separation"), 0); |
| 347 | list->hide(); |
| 348 | scroll_container->add_child(list); |
| 349 | |
| 350 | grid = memnew(HFlowContainer); |
| 351 | grid->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 352 | grid->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 353 | grid->add_theme_constant_override(SNAME("v_separation"), 0); |
| 354 | grid->add_theme_constant_override(SNAME("h_separation"), 0); |
| 355 | grid->hide(); |
| 356 | scroll_container->add_child(grid); |
| 357 | |
| 358 | file_context_menu = memnew(PopupMenu); |
| 359 | file_context_menu->add_item(TTR("Show in FileSystem"), FILE_SHOW_IN_FILESYSTEM); |
| 360 | file_context_menu->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_FILE_MANAGER); |
| 361 | file_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &QuickOpenResultContainer::_menu_option)); |
| 362 | file_context_menu->hide(); |
| 363 | scroll_container->add_child(file_context_menu); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | { |
| 368 | // Selected filepath |
| 369 | file_details_path = memnew(Label); |
| 370 | file_details_path->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 371 | file_details_path->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 372 | file_details_path->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER); |
| 373 | file_details_path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 374 | add_child(file_details_path); |
| 375 | } |
| 376 | |
| 377 | { |
| 378 | // Bottom bar |
| 379 | HBoxContainer *bottom_bar = memnew(HBoxContainer); |
| 380 | bottom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 381 | bottom_bar->set_alignment(ALIGNMENT_END); |
| 382 | bottom_bar->add_theme_constant_override("separation", 3); |
| 383 | add_child(bottom_bar); |
| 384 | |
| 385 | instant_preview_toggle = memnew(CheckButton); |
| 386 | style_button(instant_preview_toggle); |
| 387 | instant_preview_toggle->set_text(TTRC("Instant Preview")); |
| 388 | instant_preview_toggle->set_tooltip_text(TTRC("Selected resource will be previewed in the editor before accepting.")); |
| 389 | instant_preview_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_instant_preview)); |
| 390 | bottom_bar->add_child(instant_preview_toggle); |
| 391 | |
| 392 | fuzzy_search_toggle = memnew(CheckButton); |
| 393 | style_button(fuzzy_search_toggle); |
| 394 | fuzzy_search_toggle->set_text(TTR("Fuzzy Search")); |
| 395 | fuzzy_search_toggle->set_tooltip_text(TTRC("Include approximate matches.")); |
| 396 | fuzzy_search_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_fuzzy_search)); |
| 397 | bottom_bar->add_child(fuzzy_search_toggle); |
| 398 | |
| 399 | include_addons_toggle = memnew(CheckButton); |
| 400 | style_button(include_addons_toggle); |
| 401 | include_addons_toggle->set_text(TTR("Addons")); |
| 402 | include_addons_toggle->set_tooltip_text(TTR("Include files from addons")); |
| 403 | include_addons_toggle->connect(SceneStringName(toggled), callable_mp(this, &QuickOpenResultContainer::_toggle_include_addons)); |
| 404 | bottom_bar->add_child(include_addons_toggle); |
| 405 | |
| 406 | VSeparator *vsep = memnew(VSeparator); |
| 407 | vsep->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 408 | vsep->set_custom_minimum_size(Size2i(0, 14 * EDSCALE)); |
| 409 | bottom_bar->add_child(vsep); |
| 410 | |
| 411 | display_mode_toggle = memnew(Button); |
| 412 | display_mode_toggle->set_accessibility_name(TTRC("Display Mode")); |
| 413 | style_button(display_mode_toggle); |
| 414 | display_mode_toggle->connect(SceneStringName(pressed), callable_mp(this, &QuickOpenResultContainer::_toggle_display_mode)); |
| 415 | bottom_bar->add_child(display_mode_toggle); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void QuickOpenResultContainer::_menu_option(int p_option) { |
| 420 | switch (p_option) { |
| 421 | case FILE_SHOW_IN_FILESYSTEM: { |
| 422 | FileSystemDock::get_singleton()->navigate_to_path(get_selected()); |
| 423 | } break; |
| 424 | case FILE_SHOW_IN_FILE_MANAGER: { |
| 425 | String dir = ProjectSettings::get_singleton()->globalize_path(get_selected()); |
| 426 | OS::get_singleton()->shell_show_in_file_manager(dir, true); |
| 427 | } break; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void QuickOpenResultContainer::_ensure_result_vector_capacity() { |
| 432 | int target_size = EDITOR_GET("filesystem/quick_open_dialog/max_results"); |
| 433 | int initial_size = result_items.size(); |
| 434 | for (int i = target_size; i < initial_size; i++) { |
| 435 | result_items[i]->queue_free(); |
| 436 | } |
| 437 | result_items.resize(target_size); |
| 438 | for (int i = initial_size; i < target_size; i++) { |
| 439 | QuickOpenResultItem *item = memnew(QuickOpenResultItem); |
| 440 | item->connect(SceneStringName(gui_input), callable_mp(this, &QuickOpenResultContainer::_item_input).bind(i)); |
| 441 | result_items.write[i] = item; |
| 442 | if (!never_opened) { |
| 443 | _layout_result_item(item); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void QuickOpenResultContainer::init(const Vector<StringName> &p_base_types) { |
| 449 | _ensure_result_vector_capacity(); |
| 450 | base_types = p_base_types; |
| 451 | |
| 452 | const int display_mode_behavior = EDITOR_GET("filesystem/quick_open_dialog/default_display_mode"); |
| 453 | const bool adaptive_display_mode = (display_mode_behavior == 0); |
| 454 | const bool first_open = never_opened; |
| 455 | |
| 456 | if (adaptive_display_mode) { |
| 457 | _set_display_mode(get_adaptive_display_mode(p_base_types)); |
| 458 | } else if (never_opened) { |
| 459 | int last = EditorSettings::get_singleton()->get_project_metadata("quick_open_dialog", "last_mode", (int)QuickOpenDisplayMode::LIST); |
| 460 | _set_display_mode((QuickOpenDisplayMode)last); |
| 461 | } |
| 462 | |
| 463 | const bool do_instant_preview = EDITOR_GET("filesystem/quick_open_dialog/instant_preview"); |
| 464 | const bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching"); |
| 465 | const bool include_addons = EDITOR_GET("filesystem/quick_open_dialog/include_addons"); |
| 466 | instant_preview_toggle->set_pressed_no_signal(do_instant_preview); |
| 467 | fuzzy_search_toggle->set_pressed_no_signal(fuzzy_matching); |
| 468 | include_addons_toggle->set_pressed_no_signal(include_addons); |
| 469 | never_opened = false; |
| 470 | |
| 471 | const bool enable_highlights = EDITOR_GET("filesystem/quick_open_dialog/show_search_highlight"); |
| 472 | for (QuickOpenResultItem *E : result_items) { |
| 473 | E->enable_highlights = enable_highlights; |
| 474 | } |
| 475 | |
| 476 | if (first_open && history_file->load(_get_cache_file_path()) == OK) { |
| 477 | // Load history when opening for the first time. |
| 478 | file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object"))); |
| 479 | |
| 480 | bool history_modified = false; |
| 481 | Vector<String> history_keys = history_file->get_section_keys("selected_history"); |
| 482 | for (const String &type : history_keys) { |
| 483 | const StringName type_name = type; |
| 484 | const PackedStringArray paths = history_file->get_value("selected_history", type); |
| 485 | |
| 486 | PackedStringArray cleaned_paths; |
| 487 | cleaned_paths.resize(paths.size()); |
| 488 | |
| 489 | Vector<QuickOpenResultCandidate> loaded_candidates; |
| 490 | loaded_candidates.resize(paths.size()); |
| 491 | { |
| 492 | QuickOpenResultCandidate *candidates_write = loaded_candidates.ptrw(); |
| 493 | String *cleanup_write = cleaned_paths.ptrw(); |
| 494 | int i = 0; |
| 495 | for (String path : paths) { |
| 496 | if (path.begins_with("uid://")) { |
| 497 | ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(path); |
| 498 | if (!ResourceUID::get_singleton()->has_id(id)) { |
| 499 | continue; |
| 500 | } |
| 501 | path = ResourceUID::get_singleton()->get_id_path(id); |
| 502 | } |
| 503 | |
| 504 | if (!ResourceLoader::exists(path)) { |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | filetypes.insert(path, type_name); |
| 509 | QuickOpenResultCandidate candidate; |
| 510 | _setup_candidate(candidate, path); |
| 511 | candidates_write[i] = candidate; |
| 512 | cleanup_write[i] = path; |
| 513 | i++; |
| 514 | } |
| 515 | loaded_candidates.resize(i); |
| 516 | cleaned_paths.resize(i); |
| 517 | selected_history.insert(type, loaded_candidates); |
| 518 | |
| 519 | if (i < paths.size()) { |
| 520 | // Some paths removed, need to update history. |
| 521 | if (i == 0) { |
| 522 | history_file->erase_section_key("selected_history", type); |
| 523 | } else { |
| 524 | history_file->set_value("selected_history", type, cleaned_paths); |
| 525 | } |
| 526 | history_modified = true; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | if (history_modified) { |
| 531 | history_file->save(_get_cache_file_path()); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | _create_initial_results(); |
| 536 | } |
| 537 | |
| 538 | void QuickOpenResultContainer::_sort_filepaths(int p_max_results) { |
| 539 | struct FilepathComparator { |
| 540 | bool operator()(const String &p_lhs, const String &p_rhs) const { |
| 541 | // Sort on (length, alphanumeric) to prioritize shorter filepaths |
| 542 | return p_lhs.length() == p_rhs.length() ? p_lhs < p_rhs : p_lhs.length() < p_rhs.length(); |
| 543 | } |
| 544 | }; |
| 545 | |
| 546 | SortArray<String, FilepathComparator> sorter; |
| 547 | if (filepaths.size() > p_max_results) { |
| 548 | sorter.partial_sort(0, filepaths.size(), p_max_results, filepaths.ptrw()); |
| 549 | } else { |
| 550 | sorter.sort(filepaths.ptrw(), filepaths.size()); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void QuickOpenResultContainer::_create_initial_results() { |
| 555 | file_type_icons.clear(); |
| 556 | file_type_icons.insert(SNAME("__default_icon"), get_editor_theme_icon(SNAME("Object"))); |
| 557 | filepaths.clear(); |
| 558 | filetypes.clear(); |
| 559 | history_set.clear(); |
| 560 | Vector<QuickOpenResultCandidate> *history = _get_history(); |
| 561 | if (history) { |
| 562 | for (const QuickOpenResultCandidate &candidate : *history) { |
| 563 | history_set.insert(candidate.file_path); |
| 564 | } |
| 565 | } |
| 566 | _find_filepaths_in_folder(EditorFileSystem::get_singleton()->get_filesystem(), include_addons_toggle->is_pressed()); |
| 567 | _sort_filepaths(result_items.size()); |
| 568 | max_total_results = MIN(filepaths.size(), result_items.size()); |
| 569 | update_results(); |
| 570 | } |
| 571 | |
| 572 | void QuickOpenResultContainer::_find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons) { |
| 573 | for (int i = 0; i < p_directory->get_subdir_count(); i++) { |
| 574 | if (p_include_addons || p_directory->get_name() != "addons") { |
| 575 | _find_filepaths_in_folder(p_directory->get_subdir(i), p_include_addons); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | for (int i = 0; i < p_directory->get_file_count(); i++) { |
| 580 | String file_path = p_directory->get_file_path(i); |
| 581 | |
| 582 | const StringName engine_type = p_directory->get_file_type(i); |
| 583 | const StringName script_type = p_directory->get_file_resource_script_class(i); |
| 584 | |
| 585 | const bool is_engine_type = script_type == StringName(); |
| 586 | const StringName &actual_type = is_engine_type ? engine_type : script_type; |
| 587 | |
| 588 | for (const StringName &parent_type : base_types) { |
| 589 | 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)); |
| 590 | |
| 591 | if (is_valid) { |
| 592 | filepaths.append(file_path); |
| 593 | filetypes.insert(file_path, actual_type); |
| 594 | break; // Stop testing base types as soon as we get a match. |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void QuickOpenResultContainer::set_query_and_update(const String &p_query) { |
| 601 | query = p_query; |
| 602 | update_results(); |
| 603 | } |
| 604 | |
| 605 | Vector<QuickOpenResultCandidate> *QuickOpenResultContainer::_get_history() { |
| 606 | if (base_types.size() == 1) { |
| 607 | return selected_history.getptr(base_types[0]); |
| 608 | } |
| 609 | return nullptr; |
| 610 | } |
| 611 | |
| 612 | void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath) { |
| 613 | p_candidate.file_path = ResourceUID::ensure_path(p_filepath); |
| 614 | p_candidate.result = nullptr; |
| 615 | StringName actual_type; |
| 616 | { |
| 617 | StringName *actual_type_ptr = filetypes.getptr(p_filepath); |
| 618 | if (actual_type_ptr) { |
| 619 | actual_type = *actual_type_ptr; |
| 620 | } else { |
| 621 | ERR_PRINT(vformat("EditorQuickOpenDialog: No type for path %s.", p_candidate.file_path)); |
| 622 | } |
| 623 | } |
| 624 | EditorResourcePreview::PreviewItem item = EditorResourcePreview::get_singleton()->get_resource_preview_if_available(p_candidate.file_path); |
| 625 | if (item.preview.is_valid()) { |
| 626 | p_candidate.thumbnail = item.preview; |
| 627 | } else if (file_type_icons.has(actual_type)) { |
| 628 | p_candidate.thumbnail = *file_type_icons.getptr(actual_type); |
| 629 | } else if (has_theme_icon(actual_type, EditorStringName(EditorIcons))) { |
| 630 | p_candidate.thumbnail = get_editor_theme_icon(actual_type); |
| 631 | file_type_icons.insert(actual_type, p_candidate.thumbnail); |
| 632 | } else { |
| 633 | p_candidate.thumbnail = *file_type_icons.getptr(SNAME("__default_icon")); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void QuickOpenResultContainer::_setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result) { |
| 638 | _setup_candidate(p_candidate, p_result.target); |
| 639 | p_candidate.result = &p_result; |
| 640 | } |
| 641 | |
| 642 | void QuickOpenResultContainer::update_results() { |
| 643 | candidates.clear(); |
| 644 | if (query.is_empty()) { |
| 645 | _use_default_candidates(); |
| 646 | } else { |
| 647 | _score_and_sort_candidates(); |
| 648 | } |
| 649 | _update_result_items(MIN(candidates.size(), max_total_results), 0); |
| 650 | } |
| 651 | |
| 652 | void QuickOpenResultContainer::_use_default_candidates() { |
| 653 | HashSet<String> existing_paths; |
| 654 | Vector<QuickOpenResultCandidate> *history = _get_history(); |
| 655 | if (history) { |
| 656 | candidates.append_array(*history); |
| 657 | for (const QuickOpenResultCandidate &candi : *history) { |
| 658 | existing_paths.insert(candi.file_path); |
| 659 | } |
| 660 | } |
| 661 | int i = candidates.size(); |
| 662 | |
| 663 | candidates.resize(MIN(max_total_results, filepaths.size())); |
| 664 | QuickOpenResultCandidate *candidates_w = candidates.ptrw(); |
| 665 | int count = candidates.size(); |
| 666 | |
| 667 | for (const String &filepath : filepaths) { |
| 668 | if (i >= count) { |
| 669 | break; |
| 670 | } |
| 671 | if (existing_paths.has(filepath)) { |
| 672 | continue; |
| 673 | } |
| 674 | _setup_candidate(candidates_w[i++], filepath); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | void QuickOpenResultContainer::_update_fuzzy_search_results() { |
| 679 | FuzzySearch fuzzy_search; |
| 680 | fuzzy_search.start_offset = 6; // Don't match against "res://" at the start of each filepath. |
| 681 | fuzzy_search.set_query(query); |
| 682 | fuzzy_search.max_results = max_total_results; |
| 683 | bool fuzzy_matching = EDITOR_GET("filesystem/quick_open_dialog/enable_fuzzy_matching"); |
| 684 | int max_misses = EDITOR_GET("filesystem/quick_open_dialog/max_fuzzy_misses"); |
| 685 | fuzzy_search.allow_subsequences = fuzzy_matching; |
| 686 | fuzzy_search.max_misses = fuzzy_matching ? max_misses : 0; |
| 687 | fuzzy_search.search_all(filepaths, search_results); |
| 688 | } |
| 689 | |
| 690 | void QuickOpenResultContainer::_score_and_sort_candidates() { |
| 691 | _update_fuzzy_search_results(); |
| 692 | candidates.resize(search_results.size()); |
| 693 | QuickOpenResultCandidate *candidates_write = candidates.ptrw(); |
| 694 | for (const FuzzySearchResult &result : search_results) { |
| 695 | _setup_candidate(*candidates_write++, result); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | void QuickOpenResultContainer::_update_result_items(int p_new_visible_results_count, int p_new_selection_index) { |
| 700 | // Only need to update items that were not hidden in previous update. |
| 701 | int num_items_needing_updates = MAX(num_visible_results, p_new_visible_results_count); |
| 702 | num_visible_results = p_new_visible_results_count; |
| 703 | |
| 704 | for (int i = 0; i < num_items_needing_updates; i++) { |
| 705 | QuickOpenResultItem *item = result_items[i]; |
| 706 | |
| 707 | if (i < num_visible_results) { |
| 708 | item->set_content(candidates[i]); |
| 709 | } else { |
| 710 | item->reset(); |
| 711 | } |
| 712 | }; |
| 713 | |
| 714 | const bool any_results = num_visible_results > 0; |
| 715 | _select_item(any_results ? p_new_selection_index : -1); |
| 716 | |
| 717 | scroll_container->set_visible(any_results); |
| 718 | no_results_container->set_visible(!any_results); |
| 719 | |
| 720 | if (!any_results) { |
| 721 | if (filepaths.is_empty()) { |
| 722 | no_results_label->set_text(TTR("No files found for this type")); |
| 723 | } else { |
| 724 | no_results_label->set_text(TTR("No results found")); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | void QuickOpenResultContainer::handle_search_box_input(const Ref<InputEvent> &p_ie) { |
| 730 | if (num_visible_results < 0) { |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | Ref<InputEventKey> key_event = p_ie; |
| 735 | if (key_event.is_valid() && key_event->is_pressed()) { |
| 736 | bool move_selection = false; |
| 737 | |
| 738 | switch (key_event->get_keycode()) { |
| 739 | case Key::UP: |
| 740 | case Key::DOWN: |
| 741 | case Key::PAGEUP: |
| 742 | case Key::PAGEDOWN: { |
| 743 | move_selection = true; |
| 744 | } break; |
| 745 | case Key::LEFT: |
| 746 | case Key::RIGHT: { |
| 747 | if (content_display_mode == QuickOpenDisplayMode::GRID) { |
| 748 | // Maybe strip off the shift modifier to allow non-selecting navigation by character? |
| 749 | if (key_event->get_modifiers_mask().is_empty()) { |
| 750 | move_selection = true; |
| 751 | } |
| 752 | } |
| 753 | } break; |
| 754 | default: |
| 755 | break; // Let the event through so it will reach the search box. |
| 756 | } |
| 757 | |
| 758 | if (move_selection) { |
| 759 | _move_selection_index(key_event->get_keycode()); |
| 760 | queue_redraw(); |
| 761 | accept_event(); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void QuickOpenResultContainer::_move_selection_index(Key p_key) { |
| 767 | // Don't move selection if there are no results. |
| 768 | if (num_visible_results <= 0) { |
| 769 | return; |
| 770 | } |
| 771 | const int max_index = num_visible_results - 1; |
| 772 | |
| 773 | int idx = selection_index; |
| 774 | if (content_display_mode == QuickOpenDisplayMode::LIST) { |
| 775 | if (p_key == Key::UP) { |
| 776 | idx = (idx == 0) ? max_index : (idx - 1); |
| 777 | } else if (p_key == Key::DOWN) { |
| 778 | idx = (idx == max_index) ? 0 : (idx + 1); |
| 779 | } else if (p_key == Key::PAGEUP) { |
| 780 | idx = (idx == 0) ? idx : MAX(idx - 10, 0); |
| 781 | } else if (p_key == Key::PAGEDOWN) { |
| 782 | idx = (idx == max_index) ? idx : MIN(idx + 10, max_index); |
| 783 | } |
| 784 | } else { |
| 785 | int column_count = grid->get_line_max_child_count(); |
| 786 | |
| 787 | if (p_key == Key::LEFT) { |
| 788 | idx = (idx == 0) ? max_index : (idx - 1); |
| 789 | } else if (p_key == Key::RIGHT) { |
| 790 | idx = (idx == max_index) ? 0 : (idx + 1); |
| 791 | } else if (p_key == Key::UP) { |
| 792 | idx = (idx == 0) ? max_index : MAX(idx - column_count, 0); |
| 793 | } else if (p_key == Key::DOWN) { |
| 794 | idx = (idx == max_index) ? 0 : MIN(idx + column_count, max_index); |
| 795 | } else if (p_key == Key::PAGEUP) { |
| 796 | idx = (idx == 0) ? idx : MAX(idx - (3 * column_count), 0); |
| 797 | } else if (p_key == Key::PAGEDOWN) { |
| 798 | idx = (idx == max_index) ? idx : MIN(idx + (3 * column_count), max_index); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | _select_item(idx); |
| 803 | } |
| 804 | |
| 805 | void QuickOpenResultContainer::_select_item(int p_index) { |
| 806 | if (!has_nothing_selected()) { |
| 807 | result_items[selection_index]->highlight_item(false); |
| 808 | } |
| 809 | |
| 810 | selection_index = p_index; |
| 811 | |
| 812 | if (has_nothing_selected()) { |
| 813 | file_details_path->set_text(""); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | result_items[selection_index]->highlight_item(true); |
| 818 | bool in_history = history_set.has(candidates[selection_index].file_path); |
| 819 | file_details_path->set_text(get_selected() + (in_history ? TTR(" (recently opened)") : "")); |
| 820 | |
| 821 | emit_signal(SNAME("selection_changed")); |
| 822 | |
| 823 | const QuickOpenResultItem *item = result_items[selection_index]; |
| 824 | |
| 825 | // Copied from Tree. |
| 826 | const int selected_position = item->get_position().y; |
| 827 | const int selected_size = item->get_size().y; |
| 828 | const int scroll_window_size = scroll_container->get_size().y; |
| 829 | const int scroll_position = scroll_container->get_v_scroll(); |
| 830 | |
| 831 | if (selected_position <= scroll_position) { |
| 832 | scroll_container->set_v_scroll(selected_position); |
| 833 | } else if (selected_position + selected_size > scroll_position + scroll_window_size) { |
| 834 | scroll_container->set_v_scroll(selected_position + selected_size - scroll_window_size); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | void QuickOpenResultContainer::_item_input(const Ref<InputEvent> &p_ev, int p_index) { |
| 839 | Ref<InputEventMouseButton> mb = p_ev; |
| 840 | |
| 841 | if (mb.is_valid() && mb->is_pressed()) { |
| 842 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 843 | _select_item(p_index); |
| 844 | emit_signal(SNAME("result_clicked"), mb->is_double_click()); |
| 845 | } else if (mb->get_button_index() == MouseButton::RIGHT) { |
| 846 | _select_item(p_index); |
| 847 | file_context_menu->set_position(result_items[p_index]->get_screen_position() + mb->get_position()); |
| 848 | file_context_menu->reset_size(); |
| 849 | file_context_menu->popup(); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | void QuickOpenResultContainer::_toggle_instant_preview(bool p_pressed) { |
| 855 | EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/instant_preview", p_pressed); |
| 856 | } |
| 857 | |
| 858 | void QuickOpenResultContainer::_toggle_fuzzy_search(bool p_pressed) { |
| 859 | EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/enable_fuzzy_matching", p_pressed); |
| 860 | update_results(); |
| 861 | } |
| 862 | |
| 863 | String QuickOpenResultContainer::_get_cache_file_path() const { |
| 864 | return EditorPaths::get_singleton()->get_project_settings_dir().path_join("quick_open_dialog_cache.cfg"); |
| 865 | } |
| 866 | |
| 867 | void QuickOpenResultContainer::_toggle_include_addons(bool p_pressed) { |
| 868 | EditorSettings::get_singleton()->set("filesystem/quick_open_dialog/include_addons", p_pressed); |
| 869 | cleanup(); |
| 870 | _create_initial_results(); |
| 871 | } |
| 872 | |
| 873 | void QuickOpenResultContainer::_toggle_display_mode() { |
| 874 | QuickOpenDisplayMode new_display_mode = (content_display_mode == QuickOpenDisplayMode::LIST) ? QuickOpenDisplayMode::GRID : QuickOpenDisplayMode::LIST; |
| 875 | _set_display_mode(new_display_mode); |
| 876 | } |
| 877 | |
| 878 | CanvasItem *QuickOpenResultContainer::_get_result_root() { |
| 879 | if (content_display_mode == QuickOpenDisplayMode::LIST) { |
| 880 | return list; |
| 881 | } else { |
| 882 | return grid; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | void QuickOpenResultContainer::_layout_result_item(QuickOpenResultItem *item) { |
| 887 | item->set_display_mode(content_display_mode); |
| 888 | Node *parent = item->get_parent(); |
| 889 | if (parent) { |
| 890 | parent->remove_child(item); |
| 891 | } |
| 892 | _get_result_root()->add_child(item); |
| 893 | } |
| 894 | |
| 895 | void QuickOpenResultContainer::_set_display_mode(QuickOpenDisplayMode p_display_mode) { |
| 896 | CanvasItem *prev_root = _get_result_root(); |
| 897 | |
| 898 | if (prev_root->is_visible() && content_display_mode == p_display_mode) { |
| 899 | return; |
| 900 | } |
| 901 | |
| 902 | content_display_mode = p_display_mode; |
| 903 | CanvasItem *next_root = _get_result_root(); |
| 904 | |
| 905 | EditorSettings::get_singleton()->set_project_metadata("quick_open_dialog", "last_mode", (int)content_display_mode); |
| 906 | |
| 907 | prev_root->hide(); |
| 908 | next_root->show(); |
| 909 | |
| 910 | for (QuickOpenResultItem *item : result_items) { |
| 911 | _layout_result_item(item); |
| 912 | } |
| 913 | |
| 914 | _update_result_items(num_visible_results, selection_index); |
| 915 | |
| 916 | if (content_display_mode == QuickOpenDisplayMode::LIST) { |
| 917 | display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail"))); |
| 918 | display_mode_toggle->set_tooltip_text(TTR("Grid view")); |
| 919 | } else { |
| 920 | display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList"))); |
| 921 | display_mode_toggle->set_tooltip_text(TTR("List view")); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | bool QuickOpenResultContainer::has_nothing_selected() const { |
| 926 | return selection_index < 0; |
| 927 | } |
| 928 | |
| 929 | String QuickOpenResultContainer::get_selected() const { |
| 930 | ERR_FAIL_COND_V_MSG(has_nothing_selected(), String(), "Tried to get selected file, but nothing was selected."); |
| 931 | return candidates[selection_index].file_path; |
| 932 | } |
| 933 | |
| 934 | QuickOpenDisplayMode QuickOpenResultContainer::get_adaptive_display_mode(const Vector<StringName> &p_base_types) { |
| 935 | static const Vector<StringName> grid_preferred_types = { |
| 936 | StringName("Font", true), |
| 937 | StringName("Texture2D", true), |
| 938 | StringName("Material", true), |
| 939 | StringName("Mesh", true), |
| 940 | }; |
| 941 | |
| 942 | for (const StringName &type : grid_preferred_types) { |
| 943 | for (const StringName &base_type : p_base_types) { |
| 944 | if (base_type == type || ClassDB::is_parent_class(base_type, type)) { |
| 945 | return QuickOpenDisplayMode::GRID; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | return QuickOpenDisplayMode::LIST; |
| 951 | } |
| 952 | |
| 953 | String _get_uid_string(const String &p_filepath) { |
| 954 | ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_filepath); |
| 955 | return id == ResourceUID::INVALID_ID ? p_filepath : ResourceUID::get_singleton()->id_to_text(id); |
| 956 | } |
| 957 | |
| 958 | bool QuickOpenResultContainer::is_instant_preview_enabled() const { |
| 959 | return instant_preview_toggle && instant_preview_toggle->is_visible() && instant_preview_toggle->is_pressed(); |
| 960 | } |
| 961 | |
| 962 | void QuickOpenResultContainer::set_instant_preview_toggle_visible(bool p_visible) { |
| 963 | instant_preview_toggle->set_visible(p_visible); |
| 964 | } |
| 965 | |
| 966 | void QuickOpenResultContainer::save_selected_item() { |
| 967 | if (base_types.size() > 1) { |
| 968 | // Getting the type of the file and checking which base type it belongs to should be possible. |
| 969 | // However, for now these are not supported, and we don't record this. |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | const StringName &base_type = base_types[0]; |
| 974 | QuickOpenResultCandidate &selected = candidates.write[selection_index]; |
| 975 | Vector<QuickOpenResultCandidate> *type_history = selected_history.getptr(base_type); |
| 976 | |
| 977 | if (!type_history) { |
| 978 | selected_history.insert(base_type, Vector<QuickOpenResultCandidate>()); |
| 979 | type_history = selected_history.getptr(base_type); |
| 980 | } else { |
| 981 | for (int i = 0; i < type_history->size(); i++) { |
| 982 | if (selected.file_path == type_history->get(i).file_path) { |
| 983 | type_history->remove_at(i); |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | selected.result = nullptr; |
| 990 | history_set.insert(selected.file_path); |
| 991 | type_history->insert(0, selected); |
| 992 | if (type_history->size() > MAX_HISTORY_SIZE) { |
| 993 | type_history->resize(MAX_HISTORY_SIZE); |
| 994 | } |
| 995 | |
| 996 | PackedStringArray paths; |
| 997 | paths.resize(type_history->size()); |
| 998 | { |
| 999 | String *paths_write = paths.ptrw(); |
| 1000 | |
| 1001 | int i = 0; |
| 1002 | for (const QuickOpenResultCandidate &candidate : *type_history) { |
| 1003 | paths_write[i] = _get_uid_string(candidate.file_path); |
| 1004 | i++; |
| 1005 | } |
| 1006 | } |
| 1007 | history_file->set_value("selected_history", base_type, paths); |
| 1008 | history_file->save(_get_cache_file_path()); |
| 1009 | } |
| 1010 | |
| 1011 | void QuickOpenResultContainer::cleanup() { |
| 1012 | num_visible_results = 0; |
| 1013 | candidates.clear(); |
| 1014 | history_set.clear(); |
| 1015 | _select_item(-1); |
| 1016 | |
| 1017 | for (QuickOpenResultItem *item : result_items) { |
| 1018 | item->reset(); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | void QuickOpenResultContainer::_notification(int p_what) { |
| 1023 | switch (p_what) { |
| 1024 | case NOTIFICATION_THEME_CHANGED: { |
| 1025 | Color text_color = get_theme_color("font_readonly_color", EditorStringName(Editor)); |
| 1026 | file_details_path->add_theme_color_override(SceneStringName(font_color), text_color); |
| 1027 | no_results_label->add_theme_color_override(SceneStringName(font_color), text_color); |
| 1028 | |
| 1029 | file_context_menu->set_item_icon(FILE_SHOW_IN_FILESYSTEM, get_editor_theme_icon(SNAME("ShowInFileSystem"))); |
| 1030 | file_context_menu->set_item_icon(FILE_SHOW_IN_FILE_MANAGER, get_editor_theme_icon(SNAME("Filesystem"))); |
| 1031 | |
| 1032 | panel_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree"))); |
| 1033 | |
| 1034 | if (content_display_mode == QuickOpenDisplayMode::LIST) { |
| 1035 | display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileThumbnail"))); |
| 1036 | } else { |
| 1037 | display_mode_toggle->set_button_icon(get_editor_theme_icon(SNAME("FileList"))); |
| 1038 | } |
| 1039 | } break; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | void QuickOpenResultContainer::_bind_methods() { |
| 1044 | ADD_SIGNAL(MethodInfo("selection_changed")); |
| 1045 | ADD_SIGNAL(MethodInfo("result_clicked", PropertyInfo(Variant::BOOL, "double_click"))); |
| 1046 | } |
| 1047 | |
| 1048 | //------------------------- Result Item |
| 1049 | |
| 1050 | QuickOpenResultItem::QuickOpenResultItem() { |
| 1051 | set_focus_mode(FocusMode::FOCUS_NONE); |
| 1052 | _set_enabled(false); |
| 1053 | |
| 1054 | list_item = memnew(QuickOpenResultListItem); |
| 1055 | list_item->hide(); |
| 1056 | add_child(list_item); |
| 1057 | |
| 1058 | grid_item = memnew(QuickOpenResultGridItem); |
| 1059 | grid_item->hide(); |
| 1060 | add_child(grid_item); |
| 1061 | } |
| 1062 | |
| 1063 | void QuickOpenResultItem::set_display_mode(QuickOpenDisplayMode p_display_mode) { |
| 1064 | if (p_display_mode == QuickOpenDisplayMode::LIST) { |
| 1065 | grid_item->hide(); |
| 1066 | grid_item->reset(); |
| 1067 | list_item->show(); |
| 1068 | } else { |
| 1069 | list_item->hide(); |
| 1070 | list_item->reset(); |
| 1071 | grid_item->show(); |
| 1072 | } |
| 1073 | |
| 1074 | queue_redraw(); |
| 1075 | } |
| 1076 | |
| 1077 | void QuickOpenResultItem::set_content(const QuickOpenResultCandidate &p_candidate) { |
| 1078 | _set_enabled(true); |
| 1079 | |
| 1080 | if (list_item->is_visible()) { |
| 1081 | list_item->set_content(p_candidate, enable_highlights); |
| 1082 | } else { |
| 1083 | grid_item->set_content(p_candidate, enable_highlights); |
| 1084 | } |
| 1085 | |
| 1086 | queue_redraw(); |
| 1087 | } |
| 1088 | |
| 1089 | void QuickOpenResultItem::reset() { |
| 1090 | _set_enabled(false); |
| 1091 | is_hovering = false; |
| 1092 | is_selected = false; |
| 1093 | list_item->reset(); |
| 1094 | grid_item->reset(); |
| 1095 | } |
| 1096 | |
| 1097 | void QuickOpenResultItem::highlight_item(bool p_enabled) { |
| 1098 | is_selected = p_enabled; |
| 1099 | |
| 1100 | if (list_item->is_visible()) { |
| 1101 | if (p_enabled) { |
| 1102 | list_item->highlight_item(highlighted_font_color); |
| 1103 | } else { |
| 1104 | list_item->remove_highlight(); |
| 1105 | } |
| 1106 | } else { |
| 1107 | if (p_enabled) { |
| 1108 | grid_item->highlight_item(highlighted_font_color); |
| 1109 | } else { |
| 1110 | grid_item->remove_highlight(); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | queue_redraw(); |
| 1115 | } |
| 1116 | |
| 1117 | void QuickOpenResultItem::_set_enabled(bool p_enabled) { |
| 1118 | set_visible(p_enabled); |
| 1119 | set_process(p_enabled); |
| 1120 | set_process_input(p_enabled); |
| 1121 | } |
| 1122 | |
| 1123 | void QuickOpenResultItem::_notification(int p_what) { |
| 1124 | switch (p_what) { |
| 1125 | case NOTIFICATION_MOUSE_ENTER: |
| 1126 | case NOTIFICATION_MOUSE_EXIT: { |
| 1127 | is_hovering = is_visible() && p_what == NOTIFICATION_MOUSE_ENTER; |
| 1128 | queue_redraw(); |
| 1129 | } break; |
| 1130 | case NOTIFICATION_THEME_CHANGED: { |
| 1131 | selected_stylebox = get_theme_stylebox("selected", "Tree"); |
| 1132 | hovering_stylebox = get_theme_stylebox(SNAME("hovered"), "Tree"); |
| 1133 | highlighted_font_color = get_theme_color("font_focus_color", EditorStringName(Editor)); |
| 1134 | } break; |
| 1135 | case NOTIFICATION_DRAW: { |
| 1136 | if (is_selected) { |
| 1137 | draw_style_box(selected_stylebox, Rect2(Point2(), get_size())); |
| 1138 | } else if (is_hovering) { |
| 1139 | draw_style_box(hovering_stylebox, Rect2(Point2(), get_size())); |
| 1140 | } |
| 1141 | } break; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | //----------------- List item |
| 1146 | |
| 1147 | static Vector2i _get_path_interval(const Vector2i &p_interval, int p_dir_index) { |
| 1148 | if (p_interval.x >= p_dir_index || p_interval.y < 1) { |
| 1149 | return { -1, -1 }; |
| 1150 | } |
| 1151 | return { p_interval.x, MIN(p_interval.x + p_interval.y, p_dir_index) - p_interval.x }; |
| 1152 | } |
| 1153 | |
| 1154 | static Vector2i _get_name_interval(const Vector2i &p_interval, int p_dir_index) { |
| 1155 | if (p_interval.x + p_interval.y <= p_dir_index || p_interval.y < 1) { |
| 1156 | return { -1, -1 }; |
| 1157 | } |
| 1158 | int first_name_idx = p_dir_index + 1; |
| 1159 | int start = MAX(p_interval.x, first_name_idx); |
| 1160 | return { start - first_name_idx, p_interval.y - start + p_interval.x }; |
| 1161 | } |
| 1162 | |
| 1163 | QuickOpenResultListItem::QuickOpenResultListItem() { |
| 1164 | set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1165 | add_theme_constant_override("margin_left", 6 * EDSCALE); |
| 1166 | add_theme_constant_override("margin_right", 6 * EDSCALE); |
| 1167 | |
| 1168 | hbc = memnew(HBoxContainer); |
| 1169 | hbc->add_theme_constant_override(SNAME("separation"), 4 * EDSCALE); |
| 1170 | add_child(hbc); |
| 1171 | |
| 1172 | const int max_size = 36 * EDSCALE; |
| 1173 | |
| 1174 | thumbnail = memnew(TextureRect); |
| 1175 | thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1176 | thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1177 | thumbnail->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 1178 | thumbnail->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
| 1179 | thumbnail->set_custom_minimum_size(Size2i(max_size, max_size)); |
| 1180 | hbc->add_child(thumbnail); |
| 1181 | |
| 1182 | text_container = memnew(VBoxContainer); |
| 1183 | text_container->add_theme_constant_override(SNAME("separation"), -7 * EDSCALE); |
| 1184 | text_container->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1185 | text_container->set_v_size_flags(Control::SIZE_FILL); |
| 1186 | hbc->add_child(text_container); |
| 1187 | |
| 1188 | name = memnew(HighlightedLabel); |
| 1189 | name->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1190 | name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 1191 | name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT); |
| 1192 | text_container->add_child(name); |
| 1193 | |
| 1194 | path = memnew(HighlightedLabel); |
| 1195 | path->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1196 | path->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 1197 | path->add_theme_font_size_override(SceneStringName(font_size), 12 * EDSCALE); |
| 1198 | text_container->add_child(path); |
| 1199 | } |
| 1200 | |
| 1201 | void QuickOpenResultListItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) { |
| 1202 | thumbnail->set_texture(p_candidate.thumbnail); |
| 1203 | name->set_text(p_candidate.file_path.get_file()); |
| 1204 | path->set_text(p_candidate.file_path.get_base_dir()); |
| 1205 | name->reset_highlights(); |
| 1206 | path->reset_highlights(); |
| 1207 | |
| 1208 | if (p_highlight && p_candidate.result != nullptr) { |
| 1209 | for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) { |
| 1210 | for (const Vector2i &interval : match.substrings) { |
| 1211 | path->add_highlight(_get_path_interval(interval, p_candidate.result->dir_index)); |
| 1212 | name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index)); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void QuickOpenResultListItem::reset() { |
| 1219 | thumbnail->set_texture(nullptr); |
| 1220 | name->set_text(""); |
| 1221 | path->set_text(""); |
| 1222 | name->reset_highlights(); |
| 1223 | path->reset_highlights(); |
| 1224 | } |
| 1225 | |
| 1226 | void QuickOpenResultListItem::highlight_item(const Color &p_color) { |
| 1227 | name->add_theme_color_override(SceneStringName(font_color), p_color); |
| 1228 | } |
| 1229 | |
| 1230 | void QuickOpenResultListItem::remove_highlight() { |
| 1231 | name->remove_theme_color_override(SceneStringName(font_color)); |
| 1232 | } |
| 1233 | |
| 1234 | void QuickOpenResultListItem::_notification(int p_what) { |
| 1235 | switch (p_what) { |
| 1236 | case NOTIFICATION_THEME_CHANGED: { |
| 1237 | path->add_theme_color_override(SceneStringName(font_color), get_theme_color("font_disabled_color", EditorStringName(Editor))); |
| 1238 | } break; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | //--------------- Grid Item |
| 1243 | |
| 1244 | QuickOpenResultGridItem::QuickOpenResultGridItem() { |
| 1245 | set_custom_minimum_size(Size2i(120 * EDSCALE, 0)); |
| 1246 | add_theme_constant_override("margin_top", 6 * EDSCALE); |
| 1247 | add_theme_constant_override("margin_left", 2 * EDSCALE); |
| 1248 | add_theme_constant_override("margin_right", 2 * EDSCALE); |
| 1249 | |
| 1250 | vbc = memnew(VBoxContainer); |
| 1251 | vbc->set_h_size_flags(Control::SIZE_FILL); |
| 1252 | vbc->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 1253 | vbc->add_theme_constant_override(SNAME("separation"), 0); |
| 1254 | add_child(vbc); |
| 1255 | |
| 1256 | const int max_size = 64 * EDSCALE; |
| 1257 | |
| 1258 | thumbnail = memnew(TextureRect); |
| 1259 | thumbnail->set_h_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1260 | thumbnail->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1261 | thumbnail->set_custom_minimum_size(Size2i(max_size, max_size)); |
| 1262 | vbc->add_child(thumbnail); |
| 1263 | |
| 1264 | name = memnew(HighlightedLabel); |
| 1265 | name->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1266 | name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 1267 | name->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER); |
| 1268 | name->add_theme_font_size_override(SceneStringName(font_size), 13 * EDSCALE); |
| 1269 | vbc->add_child(name); |
| 1270 | } |
| 1271 | |
| 1272 | void QuickOpenResultGridItem::set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight) { |
| 1273 | thumbnail->set_texture(p_candidate.thumbnail); |
| 1274 | name->set_text(p_candidate.file_path.get_file()); |
| 1275 | name->set_tooltip_text(p_candidate.file_path); |
| 1276 | name->reset_highlights(); |
| 1277 | |
| 1278 | if (p_highlight && p_candidate.result != nullptr) { |
| 1279 | for (const FuzzyTokenMatch &match : p_candidate.result->token_matches) { |
| 1280 | for (const Vector2i &interval : match.substrings) { |
| 1281 | name->add_highlight(_get_name_interval(interval, p_candidate.result->dir_index)); |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | bool uses_icon = p_candidate.thumbnail->get_width() < (32 * EDSCALE); |
| 1287 | |
| 1288 | if (uses_icon || p_candidate.thumbnail->get_height() <= thumbnail->get_custom_minimum_size().y) { |
| 1289 | thumbnail->set_expand_mode(TextureRect::EXPAND_KEEP_SIZE); |
| 1290 | thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED); |
| 1291 | } else { |
| 1292 | thumbnail->set_expand_mode(TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL); |
| 1293 | thumbnail->set_stretch_mode(TextureRect::StretchMode::STRETCH_SCALE); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | void QuickOpenResultGridItem::reset() { |
| 1298 | thumbnail->set_texture(nullptr); |
| 1299 | name->set_text(""); |
| 1300 | name->reset_highlights(); |
| 1301 | } |
| 1302 | |
| 1303 | void QuickOpenResultGridItem::highlight_item(const Color &p_color) { |
| 1304 | name->add_theme_color_override(SceneStringName(font_color), p_color); |
| 1305 | } |
| 1306 | |
| 1307 | void QuickOpenResultGridItem::remove_highlight() { |
| 1308 | name->remove_theme_color_override(SceneStringName(font_color)); |
| 1309 | } |