diff --git a/editor/editor_command_palette.cpp b/editor/editor_command_palette.cpp index 25250e231e..4ad45f9649 100644 --- a/editor/editor_command_palette.cpp +++ b/editor/editor_command_palette.cpp @@ -70,6 +70,7 @@ void EditorCommandPalette::_update_command_search(const String &search_text) { r.key_name = command_keys[i]; r.display_name = commands[r.key_name].name; r.shortcut_text = commands[r.key_name].shortcut; + r.last_used = commands[r.key_name].last_used; if (search_text.is_subsequence_ofi(r.display_name)) { if (!search_text.is_empty()) { @@ -94,6 +95,9 @@ void EditorCommandPalette::_update_command_search(const String &search_text) { if (!search_text.is_empty()) { SortArray sorter; sorter.sort(entries.ptrw(), entries.size()); + } else { + SortArray sorter; + sorter.sort(entries.ptrw(), entries.size()); } const int entry_limit = MIN(entries.size(), 300); @@ -213,7 +217,9 @@ void EditorCommandPalette::_add_command(String p_command_name, String p_key_name void EditorCommandPalette::execute_command(String &p_command_key) { ERR_FAIL_COND_MSG(!commands.has(p_command_key), p_command_key + " not found."); + commands[p_command_key].last_used = OS::get_singleton()->get_unix_time(); commands[p_command_key].callable.call_deferred(nullptr, 0); + _save_history(); } void EditorCommandPalette::register_shortcuts_as_command() { @@ -230,6 +236,14 @@ void EditorCommandPalette::register_shortcuts_as_command() { key = unregistered_shortcuts.next(key); } unregistered_shortcuts.clear(); + + // Load command use history. + Dictionary command_history = EditorSettings::get_singleton()->get_project_metadata("command_palette", "command_history", Dictionary()); + Array history_entries = command_history.keys(); + for (int i = 0; i < history_entries.size(); i++) { + const String &history_key = history_entries[i]; + commands[history_key].last_used = command_history[history_key]; + } } Ref EditorCommandPalette::add_shortcut_command(const String &p_command, const String &p_key, Ref p_shortcut) { @@ -252,6 +266,19 @@ void EditorCommandPalette::_theme_changed() { command_search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons")); } +void EditorCommandPalette::_save_history() const { + Dictionary command_history; + List command_keys; + commands.get_key_list(&command_keys); + + for (const String &key : command_keys) { + if (commands[key].last_used > 0) { + command_history[key] = commands[key].last_used; + } + } + EditorSettings::get_singleton()->set_project_metadata("command_palette", "command_history", command_history); +} + EditorCommandPalette *EditorCommandPalette::get_singleton() { if (singleton == nullptr) { singleton = memnew(EditorCommandPalette); diff --git a/editor/editor_command_palette.h b/editor/editor_command_palette.h index 093f4b797d..39821a1169 100644 --- a/editor/editor_command_palette.h +++ b/editor/editor_command_palette.h @@ -47,13 +47,15 @@ class EditorCommandPalette : public ConfirmationDialog { Callable callable; String name; String shortcut; + int last_used = 0; // Store time as int, because doubles have problems with text serialization. }; struct CommandEntry { String key_name; String display_name; String shortcut_text; - float score; + int last_used = 0; + float score = 0; }; struct CommandEntryComparator { @@ -62,6 +64,12 @@ class EditorCommandPalette : public ConfirmationDialog { } }; + struct CommandHistoryComparator { + _FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const { + return A.last_used > B.last_used; + } + }; + HashMap commands; HashMap>> unregistered_shortcuts; @@ -74,6 +82,7 @@ class EditorCommandPalette : public ConfirmationDialog { void _update_command_keys(); void _add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text = "None"); void _theme_changed(); + void _save_history() const; EditorCommandPalette(); protected: diff --git a/editor/editor_node.h b/editor/editor_node.h index 51d01d07ef..4536a4f2e9 100644 --- a/editor/editor_node.h +++ b/editor/editor_node.h @@ -32,7 +32,6 @@ #define EDITOR_NODE_H #include "core/templates/safe_refcount.h" -#include "editor/editor_command_palette.h" #include "editor/editor_data.h" #include "editor/editor_export.h" #include "editor/editor_folding.h" diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index 73ea4fb5ef..5baffb6f9d 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -30,6 +30,7 @@ #include "editor_plugin.h" +#include "editor/editor_command_palette.h" #include "editor/editor_export.h" #include "editor/editor_node.h" #include "editor/editor_paths.h"