diff options
author | Andreas Kling <kling@serenityos.org> | 2020-09-01 14:43:16 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-01 16:17:17 +0200 |
commit | 72443bd1abe8f773516d493dc5e2bf4b91d02573 (patch) | |
tree | 2d0a614e0e204a428ffc98296245d1d40369c356 | |
parent | 22265f144520a18312452d3966960a59bdaca7dc (diff) | |
download | serenity-72443bd1abe8f773516d493dc5e2bf4b91d02573.zip |
LibGUI: Teach IconView to use AbstractView::move_cursor()
This makes it possible to manipulate the cursor programmatically via
the AbstractView interface.
-rw-r--r-- | Libraries/LibGUI/IconView.cpp | 136 | ||||
-rw-r--r-- | Libraries/LibGUI/IconView.h | 2 |
2 files changed, 61 insertions, 77 deletions
diff --git a/Libraries/LibGUI/IconView.cpp b/Libraries/LibGUI/IconView.cpp index 1f24efad5d..0d92e7fb89 100644 --- a/Libraries/LibGUI/IconView.cpp +++ b/Libraries/LibGUI/IconView.cpp @@ -600,112 +600,94 @@ void IconView::keydown_event(KeyEvent& event) if (!m_visual_row_count || !m_visual_column_count) return; - auto& model = *this->model(); + SelectionUpdate selection_update = SelectionUpdate::Set; + if (event.key() == KeyCode::Key_Return) { activate_selected(); return; } if (event.key() == KeyCode::Key_Home) { - auto new_index = model.index(0, 0); - if (model.is_valid(new_index)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::Home, selection_update); return; } if (event.key() == KeyCode::Key_End) { - auto new_index = model.index(model.row_count() - 1, 0); - if (model.is_valid(new_index)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::End, selection_update); 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() - m_visual_column_count, old_index.column()); - } else { - new_index = model.index(0, 0); - } - if (model.is_valid(new_index)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::Up, selection_update); 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() + m_visual_column_count, old_index.column()); - } else { - new_index = model.index(0, 0); - } - if (model.is_valid(new_index)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::Down, selection_update); return; } if (event.key() == KeyCode::Key_Left) { - 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)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::Left, selection_update); return; } if (event.key() == KeyCode::Key_Right) { - 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)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::Right, selection_update); return; } if (event.key() == KeyCode::Key_PageUp) { - int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count; - auto old_index = selection().first(); - auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); - if (model.is_valid(new_index)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::PageUp, selection_update); return; } if (event.key() == KeyCode::Key_PageDown) { - int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count; - auto old_index = selection().first(); - auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column()); - if (model.is_valid(new_index)) { - set_selection(new_index); - scroll_into_view(new_index, Orientation::Vertical); - update(); - } + move_cursor(CursorMovement::PageDown, selection_update); return; } return Widget::keydown_event(event); } +void IconView::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::Right: + new_index = model.index(cursor_index().row() + 1, cursor_index().column()); + break; + case CursorMovement::Left: + new_index = model.index(cursor_index().row() - 1, cursor_index().column()); + break; + case CursorMovement::Up: + new_index = model.index(cursor_index().row() - m_visual_column_count, cursor_index().column()); + break; + case CursorMovement::Down: + new_index = model.index(cursor_index().row() + m_visual_column_count, cursor_index().column()); + break; + case CursorMovement::PageUp: { + int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count; + new_index = model.index(max(0, cursor_index().row() - items_per_page), cursor_index().column()); + break; + } + case CursorMovement::PageDown: { + int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count; + new_index = model.index(min(model.row_count() - 1, cursor_index().row() + items_per_page), 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; + default: + break; + } + if (new_index.is_valid()) + set_cursor(new_index, selection_update); +} + } diff --git a/Libraries/LibGUI/IconView.h b/Libraries/LibGUI/IconView.h index 6ab6fa6933..2e23f2fcf7 100644 --- a/Libraries/LibGUI/IconView.h +++ b/Libraries/LibGUI/IconView.h @@ -64,6 +64,8 @@ private: virtual void keydown_event(KeyEvent&) override; virtual void drag_move_event(DragEvent&) override; + virtual void move_cursor(CursorMovement, SelectionUpdate) override; + struct ItemData { Gfx::IntRect text_rect; Gfx::IntRect icon_rect; |