mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-24 23:23:58 +00:00
LibGUI: Allow expand/collapse subtrees in TreeView with Alt+Left/Right
This makes TreeView a lot more keyboard friendly.
This commit is contained in:
parent
63d98bbd76
commit
9d9a31384e
Notes:
sideshowbarker
2024-07-19 06:17:32 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9d9a31384e1
2 changed files with 46 additions and 0 deletions
|
@ -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);
|
||||
|
|
|
@ -41,6 +41,9 @@ public:
|
|||
virtual int item_count() const override;
|
||||
virtual void toggle_index(const ModelIndex&) override;
|
||||
|
||||
void expand_tree(const ModelIndex& root = {});
|
||||
void collapse_tree(const ModelIndex& root = {});
|
||||
|
||||
protected:
|
||||
TreeView();
|
||||
|
||||
|
@ -69,6 +72,7 @@ private:
|
|||
struct MetadataForIndex;
|
||||
|
||||
MetadataForIndex& ensure_metadata_for_index(const ModelIndex&) const;
|
||||
void set_open_state_of_all_in_subtree(const ModelIndex& root, bool open);
|
||||
|
||||
mutable HashMap<void*, NonnullOwnPtr<MetadataForIndex>> m_view_metadata;
|
||||
|
||||
|
|
Loading…
Reference in a new issue