diff options
author | Rhin <ryanrhin@gmail.com> | 2019-11-09 01:50:39 -0600 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-09 08:50:39 +0100 |
commit | 503fe37eaaebeaf700476c9f27f17ea9477e2a55 (patch) | |
tree | b5e3d5143d93bc8b62846c9f06da4e09b34bfd48 | |
parent | 3e84ea9a53d58263aad1cf57c83464a1f7341298 (diff) | |
download | serenity-503fe37eaaebeaf700476c9f27f17ea9477e2a55.zip |
TextEditor: Enable/disable undo & redo buttons based on availability (#740)
-rw-r--r-- | Libraries/LibGUI/GTextEditor.cpp | 10 | ||||
-rw-r--r-- | Libraries/LibGUI/GTextEditor.h | 2 |
2 files changed, 10 insertions, 2 deletions
diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index a1df10cb3a..39fd42cfa8 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -48,6 +48,8 @@ void GTextEditor::create_actions() { m_undo_action = GCommonActions::make_undo_action([&](auto&) { undo(); }, this); m_redo_action = GCommonActions::make_redo_action([&](auto&) { redo(); }, this); + m_undo_action->set_enabled(false); + m_redo_action->set_enabled(false); m_cut_action = GCommonActions::make_cut_action([&](auto&) { cut(); }, this); m_copy_action = GCommonActions::make_copy_action([&](auto&) { copy(); }, this); m_paste_action = GCommonActions::make_paste_action([&](auto&) { paste(); }, this); @@ -469,7 +471,7 @@ void GTextEditor::select_all() void GTextEditor::undo() { - if (m_undo_stack_index >= m_undo_stack.size() || m_undo_stack.is_empty()) + if (!can_undo()) return; auto& undo_container = m_undo_stack[m_undo_stack_index]; @@ -488,11 +490,12 @@ void GTextEditor::undo() } m_undo_stack_index++; + did_change(); } void GTextEditor::redo() { - if (m_undo_stack_index <= 0 || m_undo_stack.is_empty()) + if (!can_redo()) return; auto& undo_container = m_undo_stack[m_undo_stack_index - 1]; @@ -504,6 +507,7 @@ void GTextEditor::redo() } m_undo_stack_index--; + did_change(); } void GTextEditor::get_selection_line_boundaries(int& first_line, int& last_line) @@ -1293,6 +1297,8 @@ void GTextEditor::did_change() ASSERT(!is_readonly()); update_content_size(); recompute_all_visual_lines(); + m_undo_action->set_enabled(can_undo()); + m_redo_action->set_enabled(can_redo()); if (!m_has_pending_change_notification) { m_has_pending_change_notification = true; deferred_invoke([this](auto&) { diff --git a/Libraries/LibGUI/GTextEditor.h b/Libraries/LibGUI/GTextEditor.h index 283f0d73de..8012aad532 100644 --- a/Libraries/LibGUI/GTextEditor.h +++ b/Libraries/LibGUI/GTextEditor.h @@ -69,6 +69,8 @@ public: bool has_selection() const { return m_selection.is_valid(); } String selected_text() const; void set_selection(const GTextRange&); + bool can_undo() const { return m_undo_stack_index < m_undo_stack.size() && !m_undo_stack.is_empty(); } + bool can_redo() const { return m_undo_stack_index > 0 && m_undo_stack[m_undo_stack_index - 1].m_undo_vector.size() > 0 && !m_undo_stack.is_empty(); } String text() const; |