From b66f3166cb1a082fd776e4d045593ebee451956d Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sun, 29 Nov 2020 11:08:11 +0330 Subject: [PATCH] LibGUI: Throw less view state away in model_did_update() When `DontInvalidIndexes` is passed, be optimistic and keep the old indices when the model validates them. This is currently fine, as the group of models that use DontInvalidateIndexes use it as "The old indices are still ok" (there's a note about this in ProcessModel.cpp). --- Libraries/LibGUI/AbstractView.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp index c7f40422dc..3cf768e972 100644 --- a/Libraries/LibGUI/AbstractView.cpp +++ b/Libraries/LibGUI/AbstractView.cpp @@ -69,14 +69,23 @@ void AbstractView::set_model(RefPtr model) void AbstractView::model_did_update(unsigned int flags) { - // FIXME: It's unfortunate that we lose so much view state when the model updates in any way. - stop_editing(); - m_edit_index = {}; - m_hovered_index = {}; - m_cursor_index = {}; if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) { + stop_editing(); + m_edit_index = {}; + m_hovered_index = {}; + m_cursor_index = {}; clear_selection(); } else { + // FIXME: These may no longer point to whatever they did before, + // but let's be optimistic until we can be sure about it. + if (!model()->is_valid(m_edit_index)) { + stop_editing(); + m_edit_index = {}; + } + if (!model()->is_valid(m_hovered_index)) + m_hovered_index = {}; + if (!model()->is_valid(m_cursor_index)) + m_cursor_index = {}; selection().remove_matching([this](auto& index) { return !model()->is_valid(index); }); } }