diff options
author | Andreas Kling <kling@serenityos.org> | 2020-08-27 17:47:19 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-27 17:47:19 +0200 |
commit | 0b9d765f6bc080eda22594120ed95815ad839d10 (patch) | |
tree | 3958f2775c24685e94c6fbec64947ee24cdd684d /Libraries/LibGUI/TableView.cpp | |
parent | 1b3169f405ac9250b65ee3608e2962f51d2d8e3c (diff) | |
download | serenity-0b9d765f6bc080eda22594120ed95815ad839d10.zip |
LibGUI: Add AbstractView::move_cursor() and share some movement logic
A view can now be told to move its cursor in one of multiple directions
as specified by the CursorMovement enum.
View subclasses can override move_cursor(CursorMovement) to implement
their own cursor behavior. By default, AbstractView::move_cursor() is
a no-op.
This patch improves code sharing between TableView and TreeView. :^)
Diffstat (limited to 'Libraries/LibGUI/TableView.cpp')
-rw-r--r-- | Libraries/LibGUI/TableView.cpp | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/Libraries/LibGUI/TableView.cpp b/Libraries/LibGUI/TableView.cpp index dc773f1349..b5feb595c0 100644 --- a/Libraries/LibGUI/TableView.cpp +++ b/Libraries/LibGUI/TableView.cpp @@ -154,28 +154,44 @@ void TableView::keydown_event(KeyEvent& event) { if (!model()) return; - auto& model = *this->model(); if (event.key() == KeyCode::Key_Return) { activate_or_edit_selected(); return; } - if (event.key() == KeyCode::Key_Left) { - move_selection(0, -1); + return AbstractTableView::keydown_event(event); +} + +void TableView::move_cursor(CursorMovement movement) +{ + if (!model()) return; - } - if (event.key() == KeyCode::Key_Right) { + auto& model = *this->model(); + switch (movement) { + case CursorMovement::Left: + move_selection(0, -1); + break; + case CursorMovement::Right: move_selection(0, 1); - return; - } - if (event.key() == KeyCode::Key_Up) { + break; + case CursorMovement::Up: move_selection(-1, 0); - return; - } - if (event.key() == KeyCode::Key_Down) { + break; + case CursorMovement::Down: move_selection(1, 0); - return; + break; + case CursorMovement::Home: { + auto index = model.index(0, 0); + set_selection(index); + scroll_into_view(index, Gfx::Orientation::Vertical); + break; } - if (event.key() == KeyCode::Key_PageUp) { + case CursorMovement::End: { + auto index = model.index(model.row_count() - 1, 0); + set_selection(index); + scroll_into_view(index, Gfx::Orientation::Vertical); + break; + } + case CursorMovement::PageUp: { int items_per_page = visible_content_rect().height() / row_height(); auto old_index = selection().first(); auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); @@ -184,9 +200,9 @@ void TableView::keydown_event(KeyEvent& event) scroll_into_view(new_index, Orientation::Vertical); update(); } - return; + break; } - if (event.key() == KeyCode::Key_PageDown) { + case CursorMovement::PageDown: { int items_per_page = visible_content_rect().height() / row_height(); auto old_index = selection().first(); auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column()); @@ -195,9 +211,9 @@ void TableView::keydown_event(KeyEvent& event) scroll_into_view(new_index, Orientation::Vertical); update(); } - return; + break; + } } - return Widget::keydown_event(event); } } |