From 1355d09c720cf04e58c7b5478150764781d6bf13 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 23 Mar 2019 04:05:58 +0100 Subject: GItemView: Implement up/down/left/right keyboard navigation. --- LibGUI/GItemView.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/LibGUI/GItemView.cpp b/LibGUI/GItemView.cpp index 4acab7073a..db0499c121 100644 --- a/LibGUI/GItemView.cpp +++ b/LibGUI/GItemView.cpp @@ -144,6 +144,9 @@ void GItemView::keydown_event(GKeyEvent& event) { if (!model()) return; + if (!m_visual_row_count || !m_visual_column_count) + return; + auto& model = *this->model(); if (event.key() == KeyCode::Key_Return) { model.activate(model.selected_index()); @@ -152,7 +155,7 @@ void GItemView::keydown_event(GKeyEvent& event) if (event.key() == KeyCode::Key_Up) { GModelIndex new_index; if (model.selected_index().is_valid()) - new_index = { model.selected_index().row() - 1, model.selected_index().column() }; + new_index = { model.selected_index().row() - m_visual_column_count, model.selected_index().column() }; else new_index = { 0, 0 }; if (model.is_valid(new_index)) { @@ -163,6 +166,32 @@ void GItemView::keydown_event(GKeyEvent& event) return; } if (event.key() == KeyCode::Key_Down) { + GModelIndex new_index; + if (model.selected_index().is_valid()) + new_index = { model.selected_index().row() + m_visual_column_count, model.selected_index().column() }; + else + new_index = { 0, 0 }; + if (model.is_valid(new_index)) { + model.set_selected_index(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } + return; + } + if (event.key() == KeyCode::Key_Left) { + GModelIndex new_index; + if (model.selected_index().is_valid()) + new_index = { model.selected_index().row() - 1, model.selected_index().column() }; + else + new_index = { 0, 0 }; + if (model.is_valid(new_index)) { + model.set_selected_index(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } + return; + } + if (event.key() == KeyCode::Key_Right) { GModelIndex new_index; if (model.selected_index().is_valid()) new_index = { model.selected_index().row() + 1, model.selected_index().column() }; -- cgit v1.2.3