diff options
author | martinfalisse <martinmotteditfalisse@gmail.com> | 2022-02-17 08:19:38 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-17 20:09:47 +0100 |
commit | b6c3fad0780faf00802f24797a80b01459f24e2e (patch) | |
tree | c21932ebfe8ce9fd6fb49a8ec1715caa690961cb | |
parent | fd24782d856ebe0f393c8e798bad7a7c2ab4da81 (diff) | |
download | serenity-b6c3fad0780faf00802f24797a80b01459f24e2e.zip |
LibGUI: JsonArrayModel update without invalidating indices
Add function to update a JsonArrayModel without invalidating it. Now,
for example in the SystemMonitor in the Network tab, a selected line
will not be deselected whenever the data is updated.
-rw-r--r-- | Userland/Applications/SystemMonitor/NetworkStatisticsWidget.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/JsonArrayModel.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/JsonArrayModel.h | 1 |
3 files changed, 22 insertions, 3 deletions
diff --git a/Userland/Applications/SystemMonitor/NetworkStatisticsWidget.cpp b/Userland/Applications/SystemMonitor/NetworkStatisticsWidget.cpp index 89c5414f62..4a42fe4ef6 100644 --- a/Userland/Applications/SystemMonitor/NetworkStatisticsWidget.cpp +++ b/Userland/Applications/SystemMonitor/NetworkStatisticsWidget.cpp @@ -114,7 +114,7 @@ NetworkStatisticsWidget::NetworkStatisticsWidget() void NetworkStatisticsWidget::update_models() { - m_adapter_model->invalidate(); - m_tcp_socket_model->invalidate(); - m_udp_socket_model->invalidate(); + m_adapter_model->update(); + m_tcp_socket_model->update(); + m_udp_socket_model->update(); } diff --git a/Userland/Libraries/LibGUI/JsonArrayModel.cpp b/Userland/Libraries/LibGUI/JsonArrayModel.cpp index f5f609e50a..51b8ff9632 100644 --- a/Userland/Libraries/LibGUI/JsonArrayModel.cpp +++ b/Userland/Libraries/LibGUI/JsonArrayModel.cpp @@ -28,6 +28,24 @@ void JsonArrayModel::invalidate() did_update(); } +void JsonArrayModel::update() +{ + auto file = Core::File::construct(m_json_path); + if (!file->open(Core::OpenMode::ReadOnly)) { + dbgln("Unable to open {}", file->filename()); + m_array.clear(); + did_update(); + return; + } + + auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors(); + + VERIFY(json.is_array()); + m_array = json.as_array(); + + did_update(GUI::Model::UpdateFlag::DontInvalidateIndices); +} + bool JsonArrayModel::store() { auto file = Core::File::construct(m_json_path); diff --git a/Userland/Libraries/LibGUI/JsonArrayModel.h b/Userland/Libraries/LibGUI/JsonArrayModel.h index e898f98146..faec864f26 100644 --- a/Userland/Libraries/LibGUI/JsonArrayModel.h +++ b/Userland/Libraries/LibGUI/JsonArrayModel.h @@ -51,6 +51,7 @@ public: virtual String column_name(int column) const override { return m_fields[column].column_name; } virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override; virtual void invalidate() override; + virtual void update(); const String& json_path() const { return m_json_path; } void set_json_path(const String& json_path); |