Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/settings/editor_command_palette.cpp
20941 views
1
/**************************************************************************/
2
/* editor_command_palette.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_command_palette.h"
32
33
#include "core/os/keyboard.h"
34
#include "editor/editor_node.h"
35
#include "editor/editor_string_names.h"
36
#include "editor/gui/editor_toaster.h"
37
#include "editor/gui/filter_line_edit.h"
38
#include "editor/settings/editor_settings.h"
39
#include "editor/themes/editor_scale.h"
40
#include "scene/gui/margin_container.h"
41
#include "scene/gui/tree.h"
42
43
EditorCommandPalette *EditorCommandPalette::singleton = nullptr;
44
45
static Rect2i prev_rect = Rect2i();
46
static bool was_showed = false;
47
48
float EditorCommandPalette::_score_path(const String &p_search, const String &p_path) {
49
float score = 0.9f + .1f * (p_search.length() / (float)p_path.length());
50
51
// Positive bias for matches close to the beginning of the file name.
52
int pos = p_path.findn(p_search);
53
if (pos != -1) {
54
return score * (1.0f - 0.1f * (float(pos) / p_path.length()));
55
}
56
57
// Positive bias for matches close to the end of the path.
58
pos = p_path.rfindn(p_search);
59
if (pos != -1) {
60
return score * (0.8f - 0.1f * (float(p_path.length() - pos) / p_path.length()));
61
}
62
63
// Remaining results belong to the same class of results.
64
return score * 0.69f;
65
}
66
67
void EditorCommandPalette::_update_command_search(const String &search_text) {
68
ERR_FAIL_COND(commands.is_empty());
69
70
HashMap<String, TreeItem *> sections;
71
TreeItem *first_section = nullptr;
72
73
// Filter possible candidates.
74
Vector<CommandEntry> entries;
75
for (const KeyValue<String, Command> &E : commands) {
76
CommandEntry r;
77
r.key_name = E.key;
78
r.display_name = E.value.name;
79
r.shortcut_text = E.value.shortcut_text;
80
r.last_used = E.value.last_used;
81
82
bool is_subsequence_of_key_name = search_text.is_subsequence_ofn(r.key_name);
83
bool is_subsequence_of_display_name = search_text.is_subsequence_ofn(r.display_name);
84
85
if (is_subsequence_of_key_name || is_subsequence_of_display_name) {
86
if (!search_text.is_empty()) {
87
float key_name_score = is_subsequence_of_key_name ? _score_path(search_text, r.key_name.to_lower()) : .0f;
88
float display_name_score = is_subsequence_of_display_name ? _score_path(search_text, r.display_name.to_lower()) : .0f;
89
90
r.score = MAX(key_name_score, display_name_score);
91
}
92
93
entries.push_back(r);
94
}
95
}
96
97
TreeItem *root = search_options->get_root();
98
root->clear_children();
99
100
if (entries.is_empty()) {
101
get_ok_button()->set_disabled(true);
102
103
return;
104
}
105
106
if (!search_text.is_empty()) {
107
SortArray<CommandEntry, CommandEntryComparator> sorter;
108
sorter.sort(entries.ptrw(), entries.size());
109
} else {
110
SortArray<CommandEntry, CommandHistoryComparator> sorter;
111
sorter.sort(entries.ptrw(), entries.size());
112
}
113
114
const int entry_limit = MIN(entries.size(), 300);
115
for (int i = 0; i < entry_limit; i++) {
116
String section_name = entries[i].key_name.get_slicec('/', 0);
117
TreeItem *section;
118
119
if (sections.has(section_name)) {
120
section = sections[section_name];
121
} else {
122
section = search_options->create_item(root);
123
124
if (!first_section) {
125
first_section = section;
126
}
127
128
String item_name = section_name.capitalize();
129
section->set_text(0, item_name);
130
section->set_selectable(0, false);
131
section->set_selectable(1, false);
132
section->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
133
section->set_custom_bg_color(1, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
134
135
sections[section_name] = section;
136
}
137
138
TreeItem *ti = search_options->create_item(section);
139
String shortcut_text = entries[i].shortcut_text == "None" ? "" : entries[i].shortcut_text;
140
ti->set_text(0, entries[i].display_name);
141
ti->set_metadata(0, entries[i].key_name);
142
ti->set_text_alignment(1, HORIZONTAL_ALIGNMENT_RIGHT);
143
ti->set_text(1, shortcut_text);
144
Color c = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)) * Color(1, 1, 1, 0.5);
145
ti->set_custom_color(1, c);
146
}
147
148
TreeItem *to_select = first_section->get_first_child();
149
to_select->select(0);
150
to_select->set_as_cursor(0);
151
search_options->ensure_cursor_is_visible();
152
}
153
154
void EditorCommandPalette::_bind_methods() {
155
ClassDB::bind_method(D_METHOD("add_command", "command_name", "key_name", "binded_callable", "shortcut_text"), &EditorCommandPalette::_add_command, DEFVAL("None"));
156
ClassDB::bind_method(D_METHOD("remove_command", "key_name"), &EditorCommandPalette::remove_command);
157
}
158
159
void EditorCommandPalette::_notification(int p_what) {
160
switch (p_what) {
161
case NOTIFICATION_VISIBILITY_CHANGED: {
162
if (!is_visible()) {
163
prev_rect = Rect2i(get_position(), get_size());
164
was_showed = true;
165
}
166
} break;
167
168
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
169
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("shortcuts")) {
170
break;
171
}
172
173
for (KeyValue<String, Command> &kv : commands) {
174
Command &c = kv.value;
175
if (c.shortcut.is_valid()) {
176
c.shortcut_text = c.shortcut->get_as_text();
177
}
178
}
179
} break;
180
}
181
}
182
183
void EditorCommandPalette::_confirmed() {
184
TreeItem *selected_option = search_options->get_selected();
185
const String command_key = selected_option != nullptr ? selected_option->get_metadata(0) : "";
186
if (!command_key.is_empty()) {
187
hide();
188
callable_mp(this, &EditorCommandPalette::execute_command).call_deferred(command_key);
189
}
190
}
191
192
void EditorCommandPalette::open_popup() {
193
if (was_showed) {
194
popup(prev_rect);
195
} else {
196
_update_command_search(String());
197
popup_centered_clamped(Size2(600, 440) * EDSCALE, 0.8f);
198
}
199
200
command_search_box->clear();
201
command_search_box->grab_focus();
202
203
search_options->scroll_to_item(search_options->get_root());
204
}
205
206
void EditorCommandPalette::get_actions_list(List<String> *p_list) const {
207
for (const KeyValue<String, Command> &E : commands) {
208
p_list->push_back(E.key);
209
}
210
}
211
212
void EditorCommandPalette::remove_command(String p_key_name) {
213
ERR_FAIL_COND_MSG(!commands.has(p_key_name), "The Command '" + String(p_key_name) + "' doesn't exists. Unable to remove it.");
214
215
commands.erase(p_key_name);
216
}
217
218
void EditorCommandPalette::add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, const Ref<Shortcut> &p_shortcut) {
219
ERR_FAIL_COND_MSG(commands.has(p_key_name), "The Command '" + String(p_command_name) + "' already exists. Unable to add it.");
220
221
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * arguments.size());
222
for (int i = 0; i < arguments.size(); i++) {
223
argptrs[i] = &arguments[i];
224
}
225
Command command;
226
command.name = p_command_name;
227
command.callable = p_action.bindp(argptrs, arguments.size());
228
if (p_shortcut.is_null()) {
229
command.shortcut_text = "None";
230
} else {
231
command.shortcut = p_shortcut;
232
command.shortcut_text = p_shortcut->get_as_text();
233
}
234
235
commands[p_key_name] = command;
236
}
237
238
void EditorCommandPalette::_add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text) {
239
ERR_FAIL_COND_MSG(commands.has(p_key_name), "The Command '" + String(p_command_name) + "' already exists. Unable to add it.");
240
241
Command command;
242
command.name = p_command_name;
243
command.callable = p_binded_action;
244
command.shortcut_text = p_shortcut_text;
245
246
// Commands added from plugins don't exist yet when the history is loaded, so we assign the last use time here if it was recorded.
247
Dictionary command_history = EditorSettings::get_singleton()->get_project_metadata("command_palette", "command_history", Dictionary());
248
if (command_history.has(p_key_name)) {
249
command.last_used = command_history[p_key_name];
250
}
251
252
commands[p_key_name] = command;
253
}
254
255
void EditorCommandPalette::execute_command(const String &p_command_key) {
256
ERR_FAIL_COND_MSG(!commands.has(p_command_key), p_command_key + " not found.");
257
commands[p_command_key].last_used = OS::get_singleton()->get_unix_time();
258
_save_history();
259
260
Variant ret;
261
Callable::CallError ce;
262
const Callable &callable = commands[p_command_key].callable;
263
callable.callp(nullptr, 0, ret, ce);
264
265
if (ce.error != Callable::CallError::CALL_OK) {
266
EditorToaster::get_singleton()->popup_str(vformat(TTR("Failed to execute command \"%s\":\n%s."), p_command_key, Variant::get_callable_error_text(callable, nullptr, 0, ce)), EditorToaster::SEVERITY_ERROR);
267
}
268
}
269
270
void EditorCommandPalette::register_shortcuts_as_command() {
271
for (const KeyValue<String, Pair<String, Ref<Shortcut>>> &E : unregistered_shortcuts) {
272
String command_name = E.value.first;
273
Ref<Shortcut> shortcut = E.value.second;
274
Ref<InputEventShortcut> ev;
275
ev.instantiate();
276
ev->set_shortcut(shortcut);
277
add_command(command_name, E.key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), shortcut);
278
}
279
unregistered_shortcuts.clear();
280
281
// Load command use history.
282
Dictionary command_history = EditorSettings::get_singleton()->get_project_metadata("command_palette", "command_history", Dictionary());
283
for (const KeyValue<Variant, Variant> &history_kv : command_history) {
284
const String &history_key = history_kv.key;
285
if (commands.has(history_key)) {
286
commands[history_key].last_used = history_kv.value;
287
}
288
}
289
}
290
291
Ref<Shortcut> EditorCommandPalette::add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut) {
292
if (is_inside_tree()) {
293
Ref<InputEventShortcut> ev;
294
ev.instantiate();
295
ev->set_shortcut(p_shortcut);
296
add_command(p_command, p_key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), p_shortcut);
297
} else {
298
const String key_name = String(p_key);
299
const String command_name = String(p_command);
300
Pair pair = Pair(command_name, p_shortcut);
301
unregistered_shortcuts[key_name] = pair;
302
}
303
return p_shortcut;
304
}
305
306
void EditorCommandPalette::_save_history() const {
307
Dictionary command_history;
308
309
for (const KeyValue<String, Command> &E : commands) {
310
if (E.value.last_used > 0) {
311
command_history[E.key] = E.value.last_used;
312
}
313
}
314
EditorSettings::get_singleton()->set_project_metadata("command_palette", "command_history", command_history);
315
}
316
317
EditorCommandPalette *EditorCommandPalette::get_singleton() {
318
if (singleton == nullptr) {
319
singleton = memnew(EditorCommandPalette);
320
}
321
return singleton;
322
}
323
324
EditorCommandPalette::EditorCommandPalette() {
325
set_hide_on_ok(false);
326
connect(SceneStringName(confirmed), callable_mp(this, &EditorCommandPalette::_confirmed));
327
328
VBoxContainer *vbc = memnew(VBoxContainer);
329
add_child(vbc);
330
331
command_search_box = memnew(FilterLineEdit);
332
command_search_box->set_placeholder(TTRC("Filter Commands"));
333
command_search_box->set_accessibility_name(TTRC("Filter Commands"));
334
command_search_box->set_v_size_flags(Control::SIZE_EXPAND_FILL);
335
command_search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorCommandPalette::_update_command_search));
336
MarginContainer *margin_container_csb = memnew(MarginContainer);
337
margin_container_csb->add_child(command_search_box);
338
vbc->add_child(margin_container_csb);
339
register_text_enter(command_search_box);
340
341
MarginContainer *mc = memnew(MarginContainer);
342
mc->set_theme_type_variation("NoBorderHorizontalWindow");
343
mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
344
mc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
345
vbc->add_child(mc);
346
347
search_options = memnew(Tree);
348
command_search_box->set_forward_control(search_options);
349
search_options->connect("item_activated", callable_mp(this, &EditorCommandPalette::_confirmed));
350
search_options->connect(SceneStringName(item_selected), callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
351
search_options->connect("nothing_selected", callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(true));
352
search_options->create_item();
353
search_options->set_hide_root(true);
354
search_options->set_columns(2);
355
search_options->set_column_custom_minimum_width(0, int(8 * EDSCALE));
356
search_options->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH);
357
mc->add_child(search_options, true);
358
}
359
360
Ref<Shortcut> ED_SHORTCUT_AND_COMMAND(const String &p_path, const String &p_name, Key p_keycode, String p_command_name) {
361
if (p_command_name.is_empty()) {
362
p_command_name = p_name;
363
}
364
365
Ref<Shortcut> shortcut = ED_SHORTCUT(p_path, p_name, p_keycode);
366
EditorCommandPalette::get_singleton()->add_shortcut_command(p_command_name, p_path, shortcut);
367
return shortcut;
368
}
369
370
Ref<Shortcut> ED_SHORTCUT_ARRAY_AND_COMMAND(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes, String p_command_name) {
371
if (p_command_name.is_empty()) {
372
p_command_name = p_name;
373
}
374
375
Ref<Shortcut> shortcut = ED_SHORTCUT_ARRAY(p_path, p_name, p_keycodes);
376
EditorCommandPalette::get_singleton()->add_shortcut_command(p_command_name, p_path, shortcut);
377
return shortcut;
378
}
379
380