Merge pull request #50588 from bruvzg/menu_gen

Optimize LineEdit and TextEdit menu item generation.
This commit is contained in:
Rémi Verschelde 2021-07-19 22:56:53 +02:00 committed by GitHub
commit a0bc2f359d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 156 deletions

View File

@ -230,7 +230,6 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) {
_ensure_menu(); _ensure_menu();
menu->set_position(get_screen_transform().xform(get_local_mouse_position())); menu->set_position(get_screen_transform().xform(get_local_mouse_position()));
menu->set_size(Vector2(1, 1)); menu->set_size(Vector2(1, 1));
_generate_context_menu();
menu->popup(); menu->popup();
grab_focus(); grab_focus();
accept_event(); accept_event();
@ -353,7 +352,6 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) {
Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + get_theme_font(SNAME("font"))->get_height(get_theme_font_size(SNAME("font_size")))) / 2); Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + get_theme_font(SNAME("font"))->get_height(get_theme_font_size(SNAME("font_size")))) / 2);
menu->set_position(get_global_transform().xform(pos)); menu->set_position(get_global_transform().xform(pos));
menu->set_size(Vector2(1, 1)); menu->set_size(Vector2(1, 1));
_generate_context_menu();
menu->popup(); menu->popup();
menu->grab_focus(); menu->grab_focus();
} }
@ -1602,7 +1600,6 @@ void LineEdit::set_editable(bool p_editable) {
} }
editable = p_editable; editable = p_editable;
_generate_context_menu();
minimum_size_changed(); minimum_size_changed();
update(); update();
@ -1858,8 +1855,6 @@ bool LineEdit::is_clear_button_enabled() const {
void LineEdit::set_shortcut_keys_enabled(bool p_enabled) { void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
shortcut_keys_enabled = p_enabled; shortcut_keys_enabled = p_enabled;
_generate_context_menu();
} }
bool LineEdit::is_shortcut_keys_enabled() const { bool LineEdit::is_shortcut_keys_enabled() const {
@ -1880,8 +1875,6 @@ void LineEdit::set_selecting_enabled(bool p_enabled) {
if (!selecting_enabled) { if (!selecting_enabled) {
deselect(); deselect();
} }
_generate_context_menu();
} }
bool LineEdit::is_selecting_enabled() const { bool LineEdit::is_selecting_enabled() const {
@ -2018,36 +2011,6 @@ int LineEdit::_get_menu_action_accelerator(const String &p_action) {
} }
} }
void LineEdit::_generate_context_menu() {
// Reorganize context menu.
_ensure_menu();
menu->clear();
if (editable) {
menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_cut") : 0);
}
menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_copy") : 0);
if (editable) {
menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_paste") : 0);
}
menu->add_separator();
if (is_selecting_enabled()) {
menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_text_select_all") : 0);
}
if (editable) {
menu->add_item(RTR("Clear"), MENU_CLEAR);
menu->add_separator();
menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_undo") : 0);
menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_redo") : 0);
}
menu->add_separator();
menu->add_submenu_item(RTR("Text writing direction"), "DirMenu");
menu->add_separator();
menu->add_check_item(RTR("Display control characters"), MENU_DISPLAY_UCC);
if (editable) {
menu->add_submenu_item(RTR("Insert control character"), "CTLMenu");
}
}
bool LineEdit::_set(const StringName &p_name, const Variant &p_value) { bool LineEdit::_set(const StringName &p_name, const Variant &p_value) {
String str = p_name; String str = p_name;
if (str.begins_with("opentype_features/")) { if (str.begins_with("opentype_features/")) {
@ -2244,13 +2207,9 @@ void LineEdit::_bind_methods() {
} }
void LineEdit::_ensure_menu() { void LineEdit::_ensure_menu() {
if (menu) { if (!menu) {
return;
}
menu = memnew(PopupMenu); menu = memnew(PopupMenu);
add_child(menu); add_child(menu);
menu->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
menu_dir = memnew(PopupMenu); menu_dir = memnew(PopupMenu);
menu_dir->set_name("DirMenu"); menu_dir->set_name("DirMenu");
@ -2258,7 +2217,6 @@ void LineEdit::_ensure_menu() {
menu_dir->add_radio_check_item(RTR("Auto-detect direction"), MENU_DIR_AUTO); menu_dir->add_radio_check_item(RTR("Auto-detect direction"), MENU_DIR_AUTO);
menu_dir->add_radio_check_item(RTR("Left-to-right"), MENU_DIR_LTR); menu_dir->add_radio_check_item(RTR("Left-to-right"), MENU_DIR_LTR);
menu_dir->add_radio_check_item(RTR("Right-to-left"), MENU_DIR_RTL); menu_dir->add_radio_check_item(RTR("Right-to-left"), MENU_DIR_RTL);
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), true);
menu->add_child(menu_dir); menu->add_child(menu_dir);
menu_ctl = memnew(PopupMenu); menu_ctl = memnew(PopupMenu);
@ -2283,9 +2241,38 @@ void LineEdit::_ensure_menu() {
menu_ctl->add_item(RTR("Soft hyphen (SHY)"), MENU_INSERT_SHY); menu_ctl->add_item(RTR("Soft hyphen (SHY)"), MENU_INSERT_SHY);
menu->add_child(menu_ctl); menu->add_child(menu_ctl);
menu->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
menu_dir->connect("id_pressed", callable_mp(this, &LineEdit::menu_option)); menu_dir->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
menu_ctl->connect("id_pressed", callable_mp(this, &LineEdit::menu_option)); menu_ctl->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
}
// Reorganize context menu.
menu->clear();
if (editable) {
menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_cut") : 0);
}
menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_copy") : 0);
if (editable) {
menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_paste") : 0);
}
menu->add_separator();
if (is_selecting_enabled()) {
menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_text_select_all") : 0);
}
if (editable) {
menu->add_item(RTR("Clear"), MENU_CLEAR);
menu->add_separator();
menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_undo") : 0);
menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_redo") : 0);
}
menu->add_separator();
menu->add_submenu_item(RTR("Text writing direction"), "DirMenu");
menu->add_separator();
menu->add_check_item(RTR("Display control characters"), MENU_DISPLAY_UCC);
menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
if (editable) {
menu->add_submenu_item(RTR("Insert control character"), "CTLMenu");
}
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED); menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO); menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR); menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);

View File

@ -165,7 +165,6 @@ private:
void _create_undo_state(); void _create_undo_state();
int _get_menu_action_accelerator(const String &p_action); int _get_menu_action_accelerator(const String &p_action);
void _generate_context_menu();
void _shape(); void _shape();
void _fit_to_width(); void _fit_to_width();

View File

@ -2473,7 +2473,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
_ensure_menu(); _ensure_menu();
menu->set_position(get_screen_transform().xform(mpos)); menu->set_position(get_screen_transform().xform(mpos));
menu->set_size(Vector2(1, 1)); menu->set_size(Vector2(1, 1));
_generate_context_menu();
menu->popup(); menu->popup();
grab_focus(); grab_focus();
} }
@ -2713,7 +2712,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
_ensure_menu(); _ensure_menu();
menu->set_position(get_screen_transform().xform(_get_cursor_pixel_pos())); menu->set_position(get_screen_transform().xform(_get_cursor_pixel_pos()));
menu->set_size(Vector2(1, 1)); menu->set_size(Vector2(1, 1));
_generate_context_menu();
menu->popup(); menu->popup();
menu->grab_focus(); menu->grab_focus();
} }
@ -3214,36 +3212,6 @@ int TextEdit::_get_menu_action_accelerator(const String &p_action) {
} }
} }
void TextEdit::_generate_context_menu() {
// Reorganize context menu.
_ensure_menu();
menu->clear();
if (!readonly) {
menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_cut") : 0);
}
menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_copy") : 0);
if (!readonly) {
menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_paste") : 0);
}
menu->add_separator();
if (is_selecting_enabled()) {
menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_text_select_all") : 0);
}
if (!readonly) {
menu->add_item(RTR("Clear"), MENU_CLEAR);
menu->add_separator();
menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_undo") : 0);
menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_redo") : 0);
}
menu->add_separator();
menu->add_submenu_item(RTR("Text writing direction"), "DirMenu");
menu->add_separator();
menu->add_check_item(RTR("Display control characters"), MENU_DISPLAY_UCC);
if (!readonly) {
menu->add_submenu_item(RTR("Insert control character"), "CTLMenu");
}
}
int TextEdit::get_visible_rows() const { int TextEdit::get_visible_rows() const {
return _get_control_height() / get_row_height(); return _get_control_height() / get_row_height();
} }
@ -3995,7 +3963,6 @@ void TextEdit::set_readonly(bool p_readonly) {
} }
readonly = p_readonly; readonly = p_readonly;
_generate_context_menu();
update(); update();
} }
@ -5571,8 +5538,6 @@ bool TextEdit::is_context_menu_enabled() {
void TextEdit::set_shortcut_keys_enabled(bool p_enabled) { void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
shortcut_keys_enabled = p_enabled; shortcut_keys_enabled = p_enabled;
_generate_context_menu();
} }
void TextEdit::set_virtual_keyboard_enabled(bool p_enable) { void TextEdit::set_virtual_keyboard_enabled(bool p_enable) {
@ -5585,8 +5550,6 @@ void TextEdit::set_selecting_enabled(bool p_enabled) {
if (!selecting_enabled) { if (!selecting_enabled) {
deselect(); deselect();
} }
_generate_context_menu();
} }
bool TextEdit::is_selecting_enabled() const { bool TextEdit::is_selecting_enabled() const {
@ -5935,10 +5898,7 @@ void TextEdit::_bind_methods() {
} }
void TextEdit::_ensure_menu() { void TextEdit::_ensure_menu() {
if (menu) { if (!menu) {
return;
}
menu = memnew(PopupMenu); menu = memnew(PopupMenu);
add_child(menu); add_child(menu);
@ -5975,7 +5935,35 @@ void TextEdit::_ensure_menu() {
menu->connect("id_pressed", callable_mp(this, &TextEdit::menu_option)); menu->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
menu_dir->connect("id_pressed", callable_mp(this, &TextEdit::menu_option)); menu_dir->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
menu_ctl->connect("id_pressed", callable_mp(this, &TextEdit::menu_option)); menu_ctl->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
}
// Reorganize context menu.
menu->clear();
if (!readonly) {
menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_cut") : 0);
}
menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_copy") : 0);
if (!readonly) {
menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_paste") : 0);
}
menu->add_separator();
if (is_selecting_enabled()) {
menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_text_select_all") : 0);
}
if (!readonly) {
menu->add_item(RTR("Clear"), MENU_CLEAR);
menu->add_separator();
menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_undo") : 0);
menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_redo") : 0);
}
menu->add_separator();
menu->add_submenu_item(RTR("Text writing direction"), "DirMenu");
menu->add_separator();
menu->add_check_item(RTR("Display control characters"), MENU_DISPLAY_UCC);
menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
if (!readonly) {
menu->add_submenu_item(RTR("Insert control character"), "CTLMenu");
}
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED); menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO); menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR); menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);

View File

@ -336,8 +336,6 @@ private:
bool shortcut_keys_enabled = true; bool shortcut_keys_enabled = true;
bool virtual_keyboard_enabled = true; bool virtual_keyboard_enabled = true;
void _generate_context_menu();
int get_visible_rows() const; int get_visible_rows() const;
int get_total_visible_rows() const; int get_total_visible_rows() const;