LibGUI: Add on_toggle function to TreeView

This adds a function to expose the index and open/close state
of expandible nodes in TreeView.
This commit is contained in:
thankyouverycool 2020-07-07 07:12:12 -04:00 committed by Andreas Kling
parent 27109c3467
commit cbf3c2caeb
Notes: sideshowbarker 2024-07-19 05:02:46 +09:00
2 changed files with 7 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -44,6 +44,8 @@ public:
void expand_tree(const ModelIndex& root = {});
void collapse_tree(const ModelIndex& root = {});
Function<void(const ModelIndex&, const bool)> on_toggle;
protected:
TreeView();