Skip to content

Commit

Permalink
Update modified shortcuts in command palette
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Jan 2, 2024
1 parent 07b8860 commit b712d1c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
32 changes: 24 additions & 8 deletions editor/editor_command_palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void EditorCommandPalette::_update_command_search(const String &search_text) {
CommandEntry r;
r.key_name = E.key;
r.display_name = E.value.name;
r.shortcut_text = E.value.shortcut;
r.shortcut_text = E.value.shortcut_text;
r.last_used = E.value.last_used;

bool is_subsequence_of_key_name = search_text.is_subsequence_ofn(r.key_name);
Expand Down Expand Up @@ -168,6 +168,19 @@ void EditorCommandPalette::_notification(int p_what) {
case NOTIFICATION_THEME_CHANGED: {
command_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
} break;

case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("shortcuts")) {
break;
}

for (KeyValue<String, Command> &kv : commands) {
Command &c = kv.value;
if (c.shortcut.is_valid()) {
c.shortcut_text = c.shortcut->get_as_text();
}
}
} break;
}
}

Expand Down Expand Up @@ -221,7 +234,7 @@ void EditorCommandPalette::remove_command(String p_key_name) {
commands.erase(p_key_name);
}

void EditorCommandPalette::add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, String p_shortcut_text) {
void EditorCommandPalette::add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, const Ref<Shortcut> &p_shortcut) {
ERR_FAIL_COND_MSG(commands.has(p_key_name), "The Command '" + String(p_command_name) + "' already exists. Unable to add it.");

const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * arguments.size());
Expand All @@ -231,7 +244,12 @@ void EditorCommandPalette::add_command(String p_command_name, String p_key_name,
Command command;
command.name = p_command_name;
command.callable = p_action.bindp(argptrs, arguments.size());
command.shortcut = p_shortcut_text;
if (p_shortcut.is_null()) {
command.shortcut_text = "None";
} else {
command.shortcut = p_shortcut;
command.shortcut_text = p_shortcut->get_as_text();
}

commands[p_key_name] = command;
}
Expand All @@ -242,7 +260,7 @@ void EditorCommandPalette::_add_command(String p_command_name, String p_key_name
Command command;
command.name = p_command_name;
command.callable = p_binded_action;
command.shortcut = p_shortcut_text;
command.shortcut_text = p_shortcut_text;

// 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.
Dictionary command_history = EditorSettings::get_singleton()->get_project_metadata("command_palette", "command_history", Dictionary());
Expand Down Expand Up @@ -275,8 +293,7 @@ void EditorCommandPalette::register_shortcuts_as_command() {
Ref<InputEventShortcut> ev;
ev.instantiate();
ev->set_shortcut(shortcut);
String shortcut_text = String(shortcut->get_as_text());
add_command(command_name, E.key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), shortcut_text);
add_command(command_name, E.key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), shortcut);
}
unregistered_shortcuts.clear();

Expand All @@ -296,8 +313,7 @@ Ref<Shortcut> EditorCommandPalette::add_shortcut_command(const String &p_command
Ref<InputEventShortcut> ev;
ev.instantiate();
ev->set_shortcut(p_shortcut);
String shortcut_text = String(p_shortcut->get_as_text());
add_command(p_command, p_key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), shortcut_text);
add_command(p_command, p_key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), p_shortcut);
} else {
const String key_name = String(p_key);
const String command_name = String(p_command);
Expand Down
5 changes: 3 additions & 2 deletions editor/editor_command_palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class EditorCommandPalette : public ConfirmationDialog {
struct Command {
Callable callable;
String name;
String shortcut;
Ref<Shortcut> shortcut;
String shortcut_text;
int last_used = 0; // Store time as int, because doubles have problems with text serialization.
};

Expand Down Expand Up @@ -95,7 +96,7 @@ class EditorCommandPalette : public ConfirmationDialog {
public:
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 add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, const Ref<Shortcut> &p_shortcut);
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);
Expand Down

0 comments on commit b712d1c

Please sign in to comment.