Merge pull request #51800 from KoBeWi/command_museum
Sort palette commands by last use
This commit is contained in:
commit
aff0521d3f
4 changed files with 38 additions and 2 deletions
|
@ -70,6 +70,7 @@ void EditorCommandPalette::_update_command_search(const String &search_text) {
|
||||||
r.key_name = command_keys[i];
|
r.key_name = command_keys[i];
|
||||||
r.display_name = commands[r.key_name].name;
|
r.display_name = commands[r.key_name].name;
|
||||||
r.shortcut_text = commands[r.key_name].shortcut;
|
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_subsequence_ofi(r.display_name)) {
|
||||||
if (!search_text.is_empty()) {
|
if (!search_text.is_empty()) {
|
||||||
|
@ -94,6 +95,9 @@ void EditorCommandPalette::_update_command_search(const String &search_text) {
|
||||||
if (!search_text.is_empty()) {
|
if (!search_text.is_empty()) {
|
||||||
SortArray<CommandEntry, CommandEntryComparator> sorter;
|
SortArray<CommandEntry, CommandEntryComparator> sorter;
|
||||||
sorter.sort(entries.ptrw(), entries.size());
|
sorter.sort(entries.ptrw(), entries.size());
|
||||||
|
} else {
|
||||||
|
SortArray<CommandEntry, CommandHistoryComparator> sorter;
|
||||||
|
sorter.sort(entries.ptrw(), entries.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
const int entry_limit = MIN(entries.size(), 300);
|
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) {
|
void EditorCommandPalette::execute_command(String &p_command_key) {
|
||||||
ERR_FAIL_COND_MSG(!commands.has(p_command_key), p_command_key + " not found.");
|
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);
|
commands[p_command_key].callable.call_deferred(nullptr, 0);
|
||||||
|
_save_history();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditorCommandPalette::register_shortcuts_as_command() {
|
void EditorCommandPalette::register_shortcuts_as_command() {
|
||||||
|
@ -230,6 +236,14 @@ void EditorCommandPalette::register_shortcuts_as_command() {
|
||||||
key = unregistered_shortcuts.next(key);
|
key = unregistered_shortcuts.next(key);
|
||||||
}
|
}
|
||||||
unregistered_shortcuts.clear();
|
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<Shortcut> EditorCommandPalette::add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut) {
|
Ref<Shortcut> EditorCommandPalette::add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut) {
|
||||||
|
@ -252,6 +266,19 @@ void EditorCommandPalette::_theme_changed() {
|
||||||
command_search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons"));
|
command_search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EditorCommandPalette::_save_history() const {
|
||||||
|
Dictionary command_history;
|
||||||
|
List<String> 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() {
|
EditorCommandPalette *EditorCommandPalette::get_singleton() {
|
||||||
if (singleton == nullptr) {
|
if (singleton == nullptr) {
|
||||||
singleton = memnew(EditorCommandPalette);
|
singleton = memnew(EditorCommandPalette);
|
||||||
|
|
|
@ -47,13 +47,15 @@ class EditorCommandPalette : public ConfirmationDialog {
|
||||||
Callable callable;
|
Callable callable;
|
||||||
String name;
|
String name;
|
||||||
String shortcut;
|
String shortcut;
|
||||||
|
int last_used = 0; // Store time as int, because doubles have problems with text serialization.
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CommandEntry {
|
struct CommandEntry {
|
||||||
String key_name;
|
String key_name;
|
||||||
String display_name;
|
String display_name;
|
||||||
String shortcut_text;
|
String shortcut_text;
|
||||||
float score;
|
int last_used = 0;
|
||||||
|
float score = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CommandEntryComparator {
|
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<String, Command> commands;
|
HashMap<String, Command> commands;
|
||||||
HashMap<String, Pair<String, Ref<Shortcut>>> unregistered_shortcuts;
|
HashMap<String, Pair<String, Ref<Shortcut>>> unregistered_shortcuts;
|
||||||
|
|
||||||
|
@ -74,6 +82,7 @@ class EditorCommandPalette : public ConfirmationDialog {
|
||||||
void _update_command_keys();
|
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 _add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text = "None");
|
||||||
void _theme_changed();
|
void _theme_changed();
|
||||||
|
void _save_history() const;
|
||||||
EditorCommandPalette();
|
EditorCommandPalette();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -32,7 +32,6 @@
|
||||||
#define EDITOR_NODE_H
|
#define EDITOR_NODE_H
|
||||||
|
|
||||||
#include "core/templates/safe_refcount.h"
|
#include "core/templates/safe_refcount.h"
|
||||||
#include "editor/editor_command_palette.h"
|
|
||||||
#include "editor/editor_data.h"
|
#include "editor/editor_data.h"
|
||||||
#include "editor/editor_export.h"
|
#include "editor/editor_export.h"
|
||||||
#include "editor/editor_folding.h"
|
#include "editor/editor_folding.h"
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
|
|
||||||
#include "editor_plugin.h"
|
#include "editor_plugin.h"
|
||||||
|
|
||||||
|
#include "editor/editor_command_palette.h"
|
||||||
#include "editor/editor_export.h"
|
#include "editor/editor_export.h"
|
||||||
#include "editor/editor_node.h"
|
#include "editor/editor_node.h"
|
||||||
#include "editor/editor_paths.h"
|
#include "editor/editor_paths.h"
|
||||||
|
|
Loading…
Reference in a new issue