mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 03:08:11 +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
2 changed files with 25 additions and 8 deletions
|
@ -72,12 +72,17 @@ void GTreeView::mousedown_event(GMouseEvent& event)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_toggle && model.row_count(index)) {
|
if (is_toggle && model.row_count(index))
|
||||||
auto& metadata = ensure_metadata_for_index(index);
|
toggle_index(index);
|
||||||
metadata.open = !metadata.open;
|
}
|
||||||
update_content_size();
|
|
||||||
update();
|
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>
|
template<typename Callback>
|
||||||
|
@ -85,7 +90,7 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
|
||||||
{
|
{
|
||||||
ASSERT(model());
|
ASSERT(model());
|
||||||
auto& model = *this->model();
|
auto& model = *this->model();
|
||||||
int indent_level = 0;
|
int indent_level = 1;
|
||||||
int y_offset = 0;
|
int y_offset = 0;
|
||||||
|
|
||||||
Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) {
|
Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) {
|
||||||
|
@ -121,7 +126,11 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
|
||||||
--indent_level;
|
--indent_level;
|
||||||
return IterationDecision::Continue;
|
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)
|
void GTreeView::paint_event(GPaintEvent& event)
|
||||||
|
@ -257,6 +266,13 @@ void GTreeView::keydown_event(GKeyEvent& event)
|
||||||
if (!model())
|
if (!model())
|
||||||
return;
|
return;
|
||||||
auto cursor_index = model()->selected_index();
|
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) {
|
if (event.key() == KeyCode::Key_Up) {
|
||||||
GModelIndex previous_index;
|
GModelIndex previous_index;
|
||||||
GModelIndex found_index;
|
GModelIndex found_index;
|
||||||
|
|
|
@ -27,6 +27,7 @@ private:
|
||||||
int toggle_size() const { return 9; }
|
int toggle_size() const { return 9; }
|
||||||
int text_padding() const { return 2; }
|
int text_padding() const { return 2; }
|
||||||
void update_content_size();
|
void update_content_size();
|
||||||
|
void toggle_index(const GModelIndex&);
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void traverse_in_paint_order(Callback) const;
|
void traverse_in_paint_order(Callback) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue