diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-13 21:24:27 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-13 21:24:27 +0200 |
commit | 3fe5dc35f28c4fb0f4295eebb207b794245af428 (patch) | |
tree | e92cc9409ada01c08fd3d2de6bcc6d375cce36b3 /Libraries/LibGUI/AbstractTableView.cpp | |
parent | 4cf3f00bab654bd665384d5abc427bc92886560c (diff) | |
download | serenity-3fe5dc35f28c4fb0f4295eebb207b794245af428.zip |
LibGUI: Add AbstractTableView::move_selection(int steps)
This allows embedders to step the selection up/down and also simplifies
AbstractTableView by sharing code between Key_Up and Key_Down. :^)
Diffstat (limited to 'Libraries/LibGUI/AbstractTableView.cpp')
-rw-r--r-- | Libraries/LibGUI/AbstractTableView.cpp | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/Libraries/LibGUI/AbstractTableView.cpp b/Libraries/LibGUI/AbstractTableView.cpp index ecfb85c266..e480ba5983 100644 --- a/Libraries/LibGUI/AbstractTableView.cpp +++ b/Libraries/LibGUI/AbstractTableView.cpp @@ -428,6 +428,25 @@ int AbstractTableView::item_count() const return model()->row_count(); } +void AbstractTableView::move_selection(int steps) +{ + if (!model()) + return; + auto& model = *this->model(); + ModelIndex new_index; + if (!selection().is_empty()) { + auto old_index = selection().first(); + new_index = model.index(old_index.row() + steps, old_index.column()); + } else { + new_index = model.index(0, 0); + } + if (model.is_valid(new_index)) { + selection().set(new_index); + scroll_into_view(new_index, Orientation::Vertical); + update(); + } +} + void AbstractTableView::keydown_event(KeyEvent& event) { if (!model()) @@ -438,33 +457,11 @@ void AbstractTableView::keydown_event(KeyEvent& event) return; } if (event.key() == KeyCode::Key_Up) { - ModelIndex new_index; - if (!selection().is_empty()) { - auto old_index = selection().first(); - new_index = model.index(old_index.row() - 1, old_index.column()); - } else { - new_index = model.index(0, 0); - } - if (model.is_valid(new_index)) { - selection().set(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_selection(-1); return; } if (event.key() == KeyCode::Key_Down) { - ModelIndex new_index; - if (!selection().is_empty()) { - auto old_index = selection().first(); - new_index = model.index(old_index.row() + 1, old_index.column()); - } else { - new_index = model.index(0, 0); - } - if (model.is_valid(new_index)) { - selection().set(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_selection(1); return; } if (event.key() == KeyCode::Key_PageUp) { |