summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2021-05-31 23:47:01 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-01 08:22:51 +0200
commit7a4445a1fe4a53746444d8c128a08ffbf8404493 (patch)
tree565f883710691092722dde9a2df7a6c9780d07a3 /Userland/Libraries
parent67a5e9f0184eac51a7451f540845e49dfdc0b34a (diff)
downloadserenity-7a4445a1fe4a53746444d8c128a08ffbf8404493.zip
LibGUI/TreeView: Select parent on collapse
When collapsing a tree that contains the current selection, the parent node becomes selected instead.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibGUI/ModelIndex.cpp11
-rw-r--r--Userland/Libraries/LibGUI/ModelIndex.h1
-rw-r--r--Userland/Libraries/LibGUI/TreeView.cpp4
3 files changed, 16 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/ModelIndex.cpp b/Userland/Libraries/LibGUI/ModelIndex.cpp
index ac7108bd41..e095764d4d 100644
--- a/Userland/Libraries/LibGUI/ModelIndex.cpp
+++ b/Userland/Libraries/LibGUI/ModelIndex.cpp
@@ -19,6 +19,17 @@ Variant ModelIndex::data(ModelRole role) const
return model()->data(*this, role);
}
+bool ModelIndex::is_parent_of(const ModelIndex& child) const
+{
+ auto current_index = child.parent();
+ while (current_index.is_valid()) {
+ if (current_index == *this)
+ return true;
+ current_index = current_index.parent();
+ }
+ return false;
+}
+
ModelIndex ModelIndex::sibling(int row, int column) const
{
if (!is_valid())
diff --git a/Userland/Libraries/LibGUI/ModelIndex.h b/Userland/Libraries/LibGUI/ModelIndex.h
index a637471b1e..92d040c704 100644
--- a/Userland/Libraries/LibGUI/ModelIndex.h
+++ b/Userland/Libraries/LibGUI/ModelIndex.h
@@ -26,6 +26,7 @@ public:
void* internal_data() const { return m_internal_data; }
ModelIndex parent() const;
+ bool is_parent_of(const ModelIndex&) const;
bool operator==(const ModelIndex& other) const
{
diff --git a/Userland/Libraries/LibGUI/TreeView.cpp b/Userland/Libraries/LibGUI/TreeView.cpp
index cdf6031680..5613384733 100644
--- a/Userland/Libraries/LibGUI/TreeView.cpp
+++ b/Userland/Libraries/LibGUI/TreeView.cpp
@@ -148,6 +148,10 @@ void TreeView::toggle_index(const ModelIndex& index)
VERIFY(model()->row_count(index));
auto& metadata = ensure_metadata_for_index(index);
metadata.open = !metadata.open;
+
+ if (!metadata.open && index.is_parent_of(cursor_index()))
+ set_cursor(index, SelectionUpdate::Set);
+
if (on_toggle)
on_toggle(index, metadata.open);
update_column_sizes();