mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 11:36:10 +00:00
GTreeView: Support multiple root-level items
Previously it was only possible to have a single root-level item in a GTreeView. This was an oversight and I didn't realize it because this code was only ever used in the FileManager, which has one root ("/"). Also factored out item toggling into a separate function, and increase the base indentation level so that root items can be toggled as well. Finally, let the user toggle the selected item with the spacebar. :^)
This commit is contained in:
parent
f6cb2fd2fb
commit
5c7bb09a73
Notes:
sideshowbarker
2024-07-19 12:37:16 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/5c7bb09a73d
2 changed files with 25 additions and 8 deletions
|
@ -72,12 +72,17 @@ void GTreeView::mousedown_event(GMouseEvent& event)
|
|||
update();
|
||||
}
|
||||
|
||||
if (is_toggle && model.row_count(index)) {
|
||||
auto& metadata = ensure_metadata_for_index(index);
|
||||
metadata.open = !metadata.open;
|
||||
update_content_size();
|
||||
update();
|
||||
}
|
||||
if (is_toggle && model.row_count(index))
|
||||
toggle_index(index);
|
||||
}
|
||||
|
||||
void GTreeView::toggle_index(const GModelIndex& index)
|
||||
{
|
||||
ASSERT(model()->row_count(index));
|
||||
auto& metadata = ensure_metadata_for_index(index);
|
||||
metadata.open = !metadata.open;
|
||||
update_content_size();
|
||||
update();
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
|
@ -85,7 +90,7 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
|
|||
{
|
||||
ASSERT(model());
|
||||
auto& model = *this->model();
|
||||
int indent_level = 0;
|
||||
int indent_level = 1;
|
||||
int y_offset = 0;
|
||||
|
||||
Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) {
|
||||
|
@ -121,7 +126,11 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
|
|||
--indent_level;
|
||||
return IterationDecision::Continue;
|
||||
};
|
||||
traverse_index(model.index(0, 0, GModelIndex()));
|
||||
int root_count = model.row_count();
|
||||
for (int root_index = 0; root_index < root_count; ++root_index) {
|
||||
if (traverse_index(model.index(root_index, 0, GModelIndex())) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GTreeView::paint_event(GPaintEvent& event)
|
||||
|
@ -257,6 +266,13 @@ void GTreeView::keydown_event(GKeyEvent& event)
|
|||
if (!model())
|
||||
return;
|
||||
auto cursor_index = model()->selected_index();
|
||||
|
||||
if (event.key() == KeyCode::Key_Space) {
|
||||
if (model()->row_count(cursor_index))
|
||||
toggle_index(cursor_index);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key() == KeyCode::Key_Up) {
|
||||
GModelIndex previous_index;
|
||||
GModelIndex found_index;
|
||||
|
|
|
@ -27,6 +27,7 @@ private:
|
|||
int toggle_size() const { return 9; }
|
||||
int text_padding() const { return 2; }
|
||||
void update_content_size();
|
||||
void toggle_index(const GModelIndex&);
|
||||
|
||||
template<typename Callback>
|
||||
void traverse_in_paint_order(Callback) const;
|
||||
|
|
Loading…
Add table
Reference in a new issue