From 16c2d4ef22a2928489934833bab06d5ea33bd90e Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Tue, 17 Aug 2021 11:41:46 +0800 Subject: Improve Undo/Redo menu items * Make Undo/Redo menu items disabled when clicking it does nothing. * Context menu of `TextEdit` * Context menu of `LineEdit` * Editor's Scene menu * Script editor's Edit menu and context menu (for Script and Text) * Make editor undo/redo log messages translatable. * Mark `UndoRedo`'s `has_{un,re}do()` methods as `const`. * Expose `TextEdit`'s `has_{un,re}do()` to scripts since `{un,re}do()` are already available. --- scene/gui/line_edit.cpp | 16 ++++++++++++++++ scene/gui/line_edit.h | 2 ++ scene/gui/text_edit.cpp | 19 +++++++++++++++++++ scene/gui/text_edit.h | 2 ++ 4 files changed, 39 insertions(+) (limited to 'scene/gui') diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 2b3be1d5c2..11e08b231e 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -946,6 +946,17 @@ void LineEdit::paste_text() { } } +bool LineEdit::has_undo() const { + if (undo_stack_pos == nullptr) { + return undo_stack.size() > 1; + } + return undo_stack_pos != undo_stack.front(); +} + +bool LineEdit::has_redo() const { + return undo_stack_pos != nullptr && undo_stack_pos != undo_stack.back(); +} + void LineEdit::undo() { if (!editable) { return; @@ -2277,6 +2288,11 @@ void LineEdit::_ensure_menu() { 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_RTL), text_direction == TEXT_DIRECTION_RTL); + + if (editable) { + menu->set_item_disabled(menu->get_item_index(MENU_UNDO), !has_undo()); + menu->set_item_disabled(menu->get_item_index(MENU_REDO), !has_redo()); + } } LineEdit::LineEdit() { diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index c5c92d60aa..0e9c032e88 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -285,6 +285,8 @@ public: void copy_text(); void cut_text(); void paste_text(); + bool has_undo() const; + bool has_redo() const; void undo(); void redo(); diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 12f0c9e89a..87f06445ac 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -2963,6 +2963,18 @@ void TextEdit::end_complex_operation() { undo_stack.back()->get().chain_backward = true; } +bool TextEdit::has_undo() const { + if (undo_stack_pos == nullptr) { + int pending = current_op.type == TextOperation::TYPE_NONE ? 0 : 1; + return undo_stack.size() + pending > 0; + } + return undo_stack_pos != undo_stack.front(); +} + +bool TextEdit::has_redo() const { + return undo_stack_pos != nullptr; +} + void TextEdit::undo() { if (!editable) { return; @@ -4482,6 +4494,8 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("begin_complex_operation"), &TextEdit::begin_complex_operation); ClassDB::bind_method(D_METHOD("end_complex_operation"), &TextEdit::end_complex_operation); + ClassDB::bind_method(D_METHOD("has_undo"), &TextEdit::has_undo); + ClassDB::bind_method(D_METHOD("has_redo"), &TextEdit::has_redo); ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo); ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo); ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history); @@ -5070,6 +5084,11 @@ void TextEdit::_generate_context_menu() { 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_RTL), text_direction == TEXT_DIRECTION_RTL); + + if (editable) { + menu->set_item_disabled(menu->get_item_index(MENU_UNDO), !has_undo()); + menu->set_item_disabled(menu->get_item_index(MENU_REDO), !has_redo()); + } } int TextEdit::_get_menu_action_accelerator(const String &p_action) { diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index da322a7bcd..69468978ab 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -659,6 +659,8 @@ public: void begin_complex_operation(); void end_complex_operation(); + bool has_undo() const; + bool has_redo() const; void undo(); void redo(); void clear_undo_history(); -- cgit v1.2.3