diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-21 13:36:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-21 13:36:08 +0200 |
commit | 9d9a31384e10c557a6e6da5fc4d7b56951f7d2bc (patch) | |
tree | 18c9be5d42c90b8f5cb7ae3a376f29adb82d6e09 /Libraries/LibGUI/TreeView.cpp | |
parent | 63d98bbd76da598a204c5b239667b5c04f864e02 (diff) | |
download | serenity-9d9a31384e10c557a6e6da5fc4d7b56951f7d2bc.zip |
LibGUI: Allow expand/collapse subtrees in TreeView with Alt+Left/Right
This makes TreeView a lot more keyboard friendly.
Diffstat (limited to 'Libraries/LibGUI/TreeView.cpp')
-rw-r--r-- | Libraries/LibGUI/TreeView.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp index 1af3799c32..0f6320734b 100644 --- a/Libraries/LibGUI/TreeView.cpp +++ b/Libraries/LibGUI/TreeView.cpp @@ -109,6 +109,38 @@ void TreeView::doubleclick_event(MouseEvent& event) } } +void TreeView::set_open_state_of_all_in_subtree(const ModelIndex& root, bool open) +{ + if (root.is_valid()) + ensure_metadata_for_index(root).open = open; + int row_count = model()->row_count(root); + int column = model()->tree_column(); + for (int row = 0; row < row_count; ++row) { + auto index = model()->index(row, column, root); + set_open_state_of_all_in_subtree(index, open); + } +} + +void TreeView::expand_tree(const ModelIndex& root) +{ + if (!model()) + return; + set_open_state_of_all_in_subtree(root, true); + update_column_sizes(); + update_content_size(); + update(); +} + +void TreeView::collapse_tree(const ModelIndex& root) +{ + if (!model()) + return; + set_open_state_of_all_in_subtree(root, false); + update_column_sizes(); + update_content_size(); + update(); +} + void TreeView::toggle_index(const ModelIndex& index) { ASSERT(model()->row_count(index)); @@ -426,6 +458,11 @@ void TreeView::keydown_event(KeyEvent& event) if (event.key() == KeyCode::Key_Left) { if (cursor_index.is_valid() && model()->row_count(cursor_index)) { + if (event.alt()) { + collapse_tree(cursor_index); + return; + } + auto& metadata = ensure_metadata_for_index(cursor_index); if (metadata.open) { open_tree_node(false, metadata); @@ -441,6 +478,11 @@ void TreeView::keydown_event(KeyEvent& event) if (event.key() == KeyCode::Key_Right) { if (cursor_index.is_valid() && model()->row_count(cursor_index)) { + if (event.alt()) { + expand_tree(cursor_index); + return; + } + auto& metadata = ensure_metadata_for_index(cursor_index); if (!metadata.open) { open_tree_node(true, metadata); |