mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:38:11 +00:00
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. :^)
This commit is contained in:
parent
4cf3f00bab
commit
3fe5dc35f2
2 changed files with 24 additions and 24 deletions
|
@ -428,6 +428,25 @@ int AbstractTableView::item_count() const
|
||||||
return model()->row_count();
|
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)
|
void AbstractTableView::keydown_event(KeyEvent& event)
|
||||||
{
|
{
|
||||||
if (!model())
|
if (!model())
|
||||||
|
@ -438,33 +457,11 @@ void AbstractTableView::keydown_event(KeyEvent& event)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event.key() == KeyCode::Key_Up) {
|
if (event.key() == KeyCode::Key_Up) {
|
||||||
ModelIndex new_index;
|
move_selection(-1);
|
||||||
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();
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event.key() == KeyCode::Key_Down) {
|
if (event.key() == KeyCode::Key_Down) {
|
||||||
ModelIndex new_index;
|
move_selection(1);
|
||||||
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();
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event.key() == KeyCode::Key_PageUp) {
|
if (event.key() == KeyCode::Key_PageUp) {
|
||||||
|
|
|
@ -71,6 +71,9 @@ public:
|
||||||
virtual ModelIndex index_at_event_position(const Gfx::Point&) const override;
|
virtual ModelIndex index_at_event_position(const Gfx::Point&) const override;
|
||||||
|
|
||||||
virtual void select_all() override;
|
virtual void select_all() override;
|
||||||
|
|
||||||
|
void move_selection(int steps);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ~AbstractTableView() override;
|
virtual ~AbstractTableView() override;
|
||||||
AbstractTableView();
|
AbstractTableView();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue