summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-18 10:17:00 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-18 10:17:00 +0200
commit5c7bb09a73d43779abf4c44156d03f310de99eb6 (patch)
tree6b930f199ce4ca4e1151893ae9b79ab123c27d25 /Libraries
parentf6cb2fd2fbe62f0a80122c2dc97b8b1463a44d19 (diff)
downloadserenity-5c7bb09a73d43779abf4c44156d03f310de99eb6.zip
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. :^)
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibGUI/GTreeView.cpp32
-rw-r--r--Libraries/LibGUI/GTreeView.h1
2 files changed, 25 insertions, 8 deletions
diff --git a/Libraries/LibGUI/GTreeView.cpp b/Libraries/LibGUI/GTreeView.cpp
index 08af08e5a1..0bc5943591 100644
--- a/Libraries/LibGUI/GTreeView.cpp
+++ b/Libraries/LibGUI/GTreeView.cpp
@@ -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;
diff --git a/Libraries/LibGUI/GTreeView.h b/Libraries/LibGUI/GTreeView.h
index 421c88dcea..a4a7ec0d3f 100644
--- a/Libraries/LibGUI/GTreeView.h
+++ b/Libraries/LibGUI/GTreeView.h
@@ -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;