From cbf3c2caeb9ea3b3f4d82e84ea38a1a43ad7171f Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Tue, 7 Jul 2020 07:12:12 -0400 Subject: [PATCH] LibGUI: Add on_toggle function to TreeView This adds a function to expose the index and open/close state of expandible nodes in TreeView. --- Libraries/LibGUI/TreeView.cpp | 7 +++++-- Libraries/LibGUI/TreeView.h | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp index daff71f0bc0..583080c7cc1 100644 --- a/Libraries/LibGUI/TreeView.cpp +++ b/Libraries/LibGUI/TreeView.cpp @@ -145,6 +145,8 @@ void TreeView::toggle_index(const ModelIndex& index) ASSERT(model()->row_count(index)); auto& metadata = ensure_metadata_for_index(index); metadata.open = !metadata.open; + if (on_toggle) + on_toggle(index, metadata.open); update_column_sizes(); update_content_size(); update(); @@ -452,6 +454,8 @@ void TreeView::keydown_event(KeyEvent& event) } auto open_tree_node = [&](bool open, MetadataForIndex& metadata) { + if (on_toggle) + on_toggle(cursor_index, open); metadata.open = open; update_column_sizes(); update_content_size(); @@ -499,8 +503,7 @@ void TreeView::keydown_event(KeyEvent& event) if (event.key() == KeyCode::Key_Return) { if (cursor_index.is_valid() && model()->row_count(cursor_index)) { - auto& metadata = ensure_metadata_for_index(cursor_index); - open_tree_node(!metadata.open, metadata); + toggle_index(cursor_index); return; } } diff --git a/Libraries/LibGUI/TreeView.h b/Libraries/LibGUI/TreeView.h index 7f712f7147f..32142429de8 100644 --- a/Libraries/LibGUI/TreeView.h +++ b/Libraries/LibGUI/TreeView.h @@ -44,6 +44,8 @@ public: void expand_tree(const ModelIndex& root = {}); void collapse_tree(const ModelIndex& root = {}); + Function on_toggle; + protected: TreeView();