diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-02 03:18:26 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-02 03:18:26 +0200 |
commit | b797458962a5f95ff15c0143c20d52702b840c4a (patch) | |
tree | 9e230664b39349212fe4a6a4728a7469e62c3264 | |
parent | 6673284b06298cf54af5c46e5bdeeffa6e561c86 (diff) | |
download | serenity-b797458962a5f95ff15c0143c20d52702b840c4a.zip |
GTreeView: Support navigating the tree with the up/down keys.
-rw-r--r-- | LibGUI/GTreeView.cpp | 40 | ||||
-rw-r--r-- | LibGUI/GTreeView.h | 1 |
2 files changed, 41 insertions, 0 deletions
diff --git a/LibGUI/GTreeView.cpp b/LibGUI/GTreeView.cpp index 10638ef8e6..9a1a56fc61 100644 --- a/LibGUI/GTreeView.cpp +++ b/LibGUI/GTreeView.cpp @@ -242,6 +242,7 @@ void GTreeView::did_update_selection() } if (opened_any) update_content_size(); + update(); } void GTreeView::update_content_size() @@ -255,3 +256,42 @@ void GTreeView::update_content_size() }); set_content_size({ width, height }); } + +void GTreeView::keydown_event(GKeyEvent& event) +{ + if (!model()) + return; + auto cursor_index = model()->selected_index(); + if (event.key() == KeyCode::Key_Up) { + GModelIndex previous_index; + GModelIndex found_index; + traverse_in_paint_order([&] (const GModelIndex& index, const Rect&, const Rect&, int) { + if (index == cursor_index) { + found_index = previous_index; + return IterationDecision::Abort; + } + previous_index = index; + return IterationDecision::Continue; + }); + if (found_index.is_valid()) { + model()->set_selected_index(found_index); + update(); + } + return; + } + if (event.key() == KeyCode::Key_Down) { + GModelIndex previous_index; + GModelIndex found_index; + traverse_in_paint_order([&] (const GModelIndex& index, const Rect&, const Rect&, int) { + if (previous_index == cursor_index) { + found_index = index; + return IterationDecision::Abort; + } + previous_index = index; + return IterationDecision::Continue; + }); + if (found_index.is_valid()) + model()->set_selected_index(found_index); + return; + } +} diff --git a/LibGUI/GTreeView.h b/LibGUI/GTreeView.h index 04462839f0..3cd62d8782 100644 --- a/LibGUI/GTreeView.h +++ b/LibGUI/GTreeView.h @@ -13,6 +13,7 @@ public: protected: virtual void paint_event(GPaintEvent&) override; virtual void mousedown_event(GMouseEvent&) override; + virtual void keydown_event(GKeyEvent&) override; virtual void did_update_selection() override; virtual void did_update_model() override; |