Bläddra i källkod

LibGUI: Keep still-valid indexes in selection after a model update

This is a stop-gap patch solution for the annoying problem of models
being bad at updating. At least the process table will retain your
selection in SystemMonitor now.
Andreas Kling 5 år sedan
förälder
incheckning
a06548eaf7

+ 6 - 1
Libraries/LibGUI/AbstractView.cpp

@@ -60,10 +60,15 @@ void AbstractView::set_model(RefPtr<Model> model)
 
 
 void AbstractView::did_update_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();
     stop_editing();
     m_edit_index = {};
     m_edit_index = {};
     m_hovered_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()
 void AbstractView::did_update_selection()

+ 11 - 0
Libraries/LibGUI/ModelSelection.cpp

@@ -31,6 +31,17 @@
 
 
 namespace GUI {
 namespace GUI {
 
 
+void ModelSelection::remove_matching(Function<bool(const ModelIndex&)> filter)
+{
+    Vector<ModelIndex> 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)
 void ModelSelection::set(const ModelIndex& index)
 {
 {
     ASSERT(index.is_valid());
     ASSERT(index.is_valid());

+ 2 - 0
Libraries/LibGUI/ModelSelection.h

@@ -89,6 +89,8 @@ public:
         return *m_indexes.begin();
         return *m_indexes.begin();
     }
     }
 
 
+    void remove_matching(Function<bool(const ModelIndex&)>);
+
 private:
 private:
     AbstractView& m_view;
     AbstractView& m_view;
     HashTable<ModelIndex> m_indexes;
     HashTable<ModelIndex> m_indexes;