diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp index 05641b0ee58..114aa4253e2 100644 --- a/Libraries/LibGUI/AbstractView.cpp +++ b/Libraries/LibGUI/AbstractView.cpp @@ -60,10 +60,15 @@ void AbstractView::set_model(RefPtr model) void AbstractView::did_update_model() { + // 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 = {}; - selection().clear(); + if (model()) { + selection().remove_matching([this](auto& index) { return !model()->is_valid(index); }); + } else { + selection().clear(); + } } void AbstractView::did_update_selection() diff --git a/Libraries/LibGUI/ModelSelection.cpp b/Libraries/LibGUI/ModelSelection.cpp index 7bcec232cfa..dbae3844666 100644 --- a/Libraries/LibGUI/ModelSelection.cpp +++ b/Libraries/LibGUI/ModelSelection.cpp @@ -31,6 +31,17 @@ namespace GUI { +void ModelSelection::remove_matching(Function filter) +{ + Vector to_remove; + for (auto& index : m_indexes) { + if (filter(index)) + to_remove.append(index); + } + for (auto& index : to_remove) + m_indexes.remove(index); +} + void ModelSelection::set(const ModelIndex& index) { ASSERT(index.is_valid()); diff --git a/Libraries/LibGUI/ModelSelection.h b/Libraries/LibGUI/ModelSelection.h index c16ed825355..7a5946edfac 100644 --- a/Libraries/LibGUI/ModelSelection.h +++ b/Libraries/LibGUI/ModelSelection.h @@ -89,6 +89,8 @@ public: return *m_indexes.begin(); } + void remove_matching(Function); + private: AbstractView& m_view; HashTable m_indexes;