summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-02 21:15:26 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-02 21:29:31 +0200
commit274a09246b7bc96845574995407b4300418acade (patch)
tree6af3197368bb223d3d7bd749d5fa21e7a2893400 /Libraries/LibGUI
parentc840c47c13e3d4e8597019b77d6ac627f33f04c5 (diff)
downloadserenity-274a09246b7bc96845574995407b4300418acade.zip
LibGUI: Move ListView to using AbstractView::move_cursor()
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r--Libraries/LibGUI/ComboBox.cpp23
-rw-r--r--Libraries/LibGUI/ListView.cpp134
-rw-r--r--Libraries/LibGUI/ListView.h5
3 files changed, 83 insertions, 79 deletions
diff --git a/Libraries/LibGUI/ComboBox.cpp b/Libraries/LibGUI/ComboBox.cpp
index fa082c85c2..b45edcbf89 100644
--- a/Libraries/LibGUI/ComboBox.cpp
+++ b/Libraries/LibGUI/ComboBox.cpp
@@ -62,27 +62,26 @@ ComboBox::ComboBox()
m_editor->set_has_open_button(true);
m_editor->on_change = [this] {
if (on_change)
- on_change(m_editor->text(), m_list_view->selection().first());
+ on_change(m_editor->text(), m_list_view->cursor_index());
};
m_editor->on_return_pressed = [this] {
if (on_return_pressed)
on_return_pressed();
};
m_editor->on_up_pressed = [this] {
- m_list_view->move_selection(-1);
+ m_list_view->move_cursor(AbstractView::CursorMovement::Up, AbstractView::SelectionUpdate::Set);
};
m_editor->on_down_pressed = [this] {
- m_list_view->move_selection(1);
+ m_list_view->move_cursor(AbstractView::CursorMovement::Down, AbstractView::SelectionUpdate::Set);
};
m_editor->on_pageup_pressed = [this] {
- m_list_view->move_selection(-m_list_view->selection().first().row());
+ m_list_view->move_cursor(AbstractView::CursorMovement::PageUp, AbstractView::SelectionUpdate::Set);
};
m_editor->on_pagedown_pressed = [this] {
- if (model())
- m_list_view->move_selection((model()->row_count() - 1) - m_list_view->selection().first().row());
+ m_list_view->move_cursor(AbstractView::CursorMovement::PageDown, AbstractView::SelectionUpdate::Set);
};
m_editor->on_mousewheel = [this](int delta) {
- m_list_view->move_selection(delta);
+ m_list_view->move_cursor_relative(delta, AbstractView::SelectionUpdate::Set);
};
m_editor->on_mousedown = [this] {
if (only_allow_values_from_model())
@@ -156,16 +155,14 @@ void ComboBox::set_model(NonnullRefPtr<Model> model)
void ComboBox::set_selected_index(size_t index)
{
- auto model = this->m_list_view->model();
-
- auto model_index = model->index(index, 0);
- if (model->is_valid(model_index))
- this->m_list_view->selection().set(model_index);
+ if (!m_list_view->model())
+ return;
+ m_list_view->set_cursor(m_list_view->model()->index(index, 0), AbstractView::SelectionUpdate::Set);
}
size_t ComboBox::selected_index() const
{
- return m_list_view->selection().first().row();
+ return m_list_view->cursor_index().row();
}
void ComboBox::select_all()
diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp
index a5ac557131..c1d69fb3e9 100644
--- a/Libraries/LibGUI/ListView.cpp
+++ b/Libraries/LibGUI/ListView.cpp
@@ -185,44 +185,14 @@ 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()) {
- if (hover_highlighting() && m_last_valid_hovered_index.is_valid()) {
- new_index = model.index(m_last_valid_hovered_index.row() + steps, m_last_valid_hovered_index.column());
- } else {
- auto old_index = selection().first();
- new_index = model.index(old_index.row() + steps, old_index.column());
- }
- } else {
- if (hover_highlighting() && m_last_valid_hovered_index.is_valid()) {
- new_index = model.index(m_last_valid_hovered_index.row() + steps, m_last_valid_hovered_index.column());
- } else {
- new_index = model.index(0, 0);
- }
- }
- if (model.is_valid(new_index)) {
- set_last_valid_hovered_index({});
- selection().set(new_index);
- scroll_into_view(new_index, false, true);
- update();
- } else {
- if (hover_highlighting() && m_last_valid_hovered_index.is_valid()) {
- new_index = model.index(m_last_valid_hovered_index.row(), m_last_valid_hovered_index.column());
- selection().set(new_index);
- }
- }
-}
-
void ListView::keydown_event(KeyEvent& event)
{
if (!model())
return;
auto& model = *this->model();
+
+ SelectionUpdate selection_update = SelectionUpdate::Set;
+
ModelIndex new_index;
if (event.key() == KeyCode::Key_Return) {
if (hover_highlighting() && m_last_valid_hovered_index.is_valid()) {
@@ -233,45 +203,19 @@ void ListView::keydown_event(KeyEvent& event)
return;
}
if (event.key() == KeyCode::Key_Up) {
- move_selection(-1);
+ move_cursor(CursorMovement::Up, selection_update);
return;
}
if (event.key() == KeyCode::Key_Down) {
- move_selection(1);
+ move_cursor(CursorMovement::Down, selection_update);
return;
}
if (event.key() == KeyCode::Key_PageUp) {
- if (hover_highlighting())
- set_last_valid_hovered_index({});
- if (!selection().is_empty()) {
- int items_per_page = visible_content_rect().height() / item_height();
- auto old_index = selection().first();
- new_index = model.index(max(0, old_index.row() - items_per_page), 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, false, true);
- update();
- }
+ move_cursor(CursorMovement::PageUp, selection_update);
return;
}
if (event.key() == KeyCode::Key_PageDown) {
- if (hover_highlighting())
- set_last_valid_hovered_index({});
- if (!selection().is_empty()) {
- int items_per_page = visible_content_rect().height() / item_height();
- auto old_index = selection().first();
- new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), 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, false, true);
- update();
- }
+ move_cursor(CursorMovement::PageDown, selection_update);
return;
}
if (event.key() == KeyCode::Key_Escape) {
@@ -279,7 +223,69 @@ void ListView::keydown_event(KeyEvent& event)
on_escape_pressed();
return;
}
- return Widget::keydown_event(event);
+ return AbstractView::keydown_event(event);
+}
+
+void ListView::move_cursor_relative(int steps, SelectionUpdate selection_update)
+{
+ if (!model())
+ return;
+ auto& model = *this->model();
+ ModelIndex new_index;
+ if (cursor_index().is_valid()) {
+ new_index = model.index(cursor_index().row() + steps, cursor_index().column());
+ } else {
+ new_index = model.index(0, 0);
+ }
+ set_cursor(new_index, selection_update);
+}
+
+void ListView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
+{
+ if (!model())
+ return;
+ auto& model = *this->model();
+
+ if (!cursor_index().is_valid()) {
+ set_cursor(model.index(0, 0), SelectionUpdate::Set);
+ return;
+ }
+
+ ModelIndex new_index;
+
+ switch (movement) {
+ case CursorMovement::Up:
+ new_index = model.index(cursor_index().row() - 1, cursor_index().column());
+ break;
+ case CursorMovement::Down:
+ new_index = model.index(cursor_index().row() + 1, cursor_index().column());
+ break;
+ case CursorMovement::Home:
+ new_index = model.index(0, 0);
+ break;
+ case CursorMovement::End:
+ new_index = model.index(model.row_count() - 1, 0);
+ break;
+ case CursorMovement::PageUp: {
+ if (hover_highlighting())
+ set_last_valid_hovered_index({});
+ int items_per_page = visible_content_rect().height() / item_height();
+ new_index = model.index(max(0, cursor_index().row() - items_per_page), cursor_index().column());
+ break;
+ }
+ case CursorMovement::PageDown: {
+ if (hover_highlighting())
+ set_last_valid_hovered_index({});
+ int items_per_page = visible_content_rect().height() / item_height();
+ new_index = model.index(min(model.row_count() - 1, cursor_index().row() + items_per_page), cursor_index().column());
+ break;
+ }
+ default:
+ break;
+ }
+
+ if (new_index.is_valid())
+ set_cursor(new_index, selection_update);
}
void ListView::scroll_into_view(const ModelIndex& index, bool scroll_horizontally, bool scroll_vertically)
diff --git a/Libraries/LibGUI/ListView.h b/Libraries/LibGUI/ListView.h
index 15158d7f77..7b75067afc 100644
--- a/Libraries/LibGUI/ListView.h
+++ b/Libraries/LibGUI/ListView.h
@@ -57,10 +57,11 @@ public:
virtual void select_all() override;
- void move_selection(int steps);
-
Function<void()> on_escape_pressed;
+ virtual void move_cursor(CursorMovement, SelectionUpdate) override;
+ void move_cursor_relative(int steps, SelectionUpdate);
+
private:
ListView();