Skip to content

Commit

Permalink
Add call validation to CommandPalette
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Sep 24, 2023
1 parent df0a822 commit 9f0b8c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions editor/editor_command_palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_toaster.h"
#include "scene/gui/control.h"
#include "scene/gui/tree.h"

Expand Down Expand Up @@ -184,10 +185,10 @@ void EditorCommandPalette::_sbox_input(const Ref<InputEvent> &p_ie) {

void EditorCommandPalette::_confirmed() {
TreeItem *selected_option = search_options->get_selected();
String command_key = selected_option != nullptr ? selected_option->get_metadata(0) : "";
const String command_key = selected_option != nullptr ? selected_option->get_metadata(0) : "";
if (!command_key.is_empty()) {
hide();
execute_command(command_key);
callable_mp(this, &EditorCommandPalette::execute_command).call_deferred(command_key);
}
}

Expand Down Expand Up @@ -248,11 +249,19 @@ void EditorCommandPalette::_add_command(String p_command_name, String p_key_name
commands[p_key_name] = command;
}

void EditorCommandPalette::execute_command(String &p_command_key) {
void EditorCommandPalette::execute_command(const 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();
_save_history();

Variant ret;
Callable::CallError ce;
const Callable &callable = commands[p_command_key].callable;
callable.callp(nullptr, 0, ret, ce);

if (ce.error != Callable::CallError::CALL_OK) {
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);
}
}

void EditorCommandPalette::register_shortcuts_as_command() {
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_command_palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class EditorCommandPalette : public ConfirmationDialog {
void open_popup();
void get_actions_list(List<String> *p_list) const;
void add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, String p_shortcut_text = "None");
void execute_command(String &p_command_name);
void execute_command(const String &p_command_name);
void register_shortcuts_as_command();
Ref<Shortcut> add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut);
void remove_command(String p_key_name);
Expand Down

0 comments on commit 9f0b8c0

Please sign in to comment.