summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/ListView.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-12 15:10:31 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-12 15:11:06 +0200
commite064999e0d9d6d91b846a9d8afe3e05e45559dd3 (patch)
tree0ebcb3ecdaf30dc6f02930e2a8af56da67d1ff17 /Libraries/LibGUI/ListView.cpp
parent18ff75e67bab9ea840bd57fcd6ef33f9275eb9dc (diff)
downloadserenity-e064999e0d9d6d91b846a9d8afe3e05e45559dd3.zip
LibGUI: Allow scrolling through a ComboBox with the mouse wheel
Diffstat (limited to 'Libraries/LibGUI/ListView.cpp')
-rw-r--r--Libraries/LibGUI/ListView.cpp45
1 files changed, 21 insertions, 24 deletions
diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp
index 5e55158ca4..68592a0f79 100644
--- a/Libraries/LibGUI/ListView.cpp
+++ b/Libraries/LibGUI/ListView.cpp
@@ -182,6 +182,25 @@ int ListView::item_count() const
return model()->row_count();
}
+void ListView::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 ListView::keydown_event(KeyEvent& event)
{
if (!model())
@@ -192,33 +211,11 @@ void ListView::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) {