summaryrefslogtreecommitdiff
path: root/scene/gui
diff options
context:
space:
mode:
authorHaoyu Qiu <timothyqiu32@gmail.com>2021-08-17 11:41:46 +0800
committerHaoyu Qiu <timothyqiu32@gmail.com>2021-08-17 21:11:10 +0800
commit16c2d4ef22a2928489934833bab06d5ea33bd90e (patch)
treefd4bc91870e0684e1fa668eaeed24c55a733e870 /scene/gui
parentca6c5cf7e6252558d8791d41b8b757929ac3819c (diff)
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.
Diffstat (limited to 'scene/gui')
-rw-r--r--scene/gui/line_edit.cpp16
-rw-r--r--scene/gui/line_edit.h2
-rw-r--r--scene/gui/text_edit.cpp19
-rw-r--r--scene/gui/text_edit.h2
4 files changed, 39 insertions, 0 deletions
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();