mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 03:25:07 +00:00
LibGUI: Allow basic keyboard navigation in GTableView.
Pressing Enter will now "activate" the selected index, meaning that the model gets a call to activate(GModelIndex).
This commit is contained in:
parent
e1d0a3f226
commit
b5dcad932e
3 changed files with 32 additions and 1 deletions
|
@ -47,13 +47,18 @@ public:
|
||||||
virtual ColumnMetadata column_metadata(int) const { return { }; }
|
virtual ColumnMetadata column_metadata(int) const { return { }; }
|
||||||
virtual GVariant data(int row, int column) const = 0;
|
virtual GVariant data(int row, int column) const = 0;
|
||||||
virtual void update() = 0;
|
virtual void update() = 0;
|
||||||
|
virtual void activate(const GModelIndex&) { }
|
||||||
|
|
||||||
bool is_valid(GModelIndex index) const
|
bool is_valid(GModelIndex index) const
|
||||||
{
|
{
|
||||||
return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
|
return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_selected_index(const GModelIndex& index) { m_selected_index = index; }
|
void set_selected_index(const GModelIndex& index)
|
||||||
|
{
|
||||||
|
if (is_valid(index))
|
||||||
|
m_selected_index = index;
|
||||||
|
}
|
||||||
GModelIndex selected_index() const { return m_selected_index; }
|
GModelIndex selected_index() const { return m_selected_index; }
|
||||||
|
|
||||||
void register_view(Badge<GTableView>, GTableView&);
|
void register_view(Badge<GTableView>, GTableView&);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <LibGUI/GTableModel.h>
|
#include <LibGUI/GTableModel.h>
|
||||||
#include <LibGUI/GScrollBar.h>
|
#include <LibGUI/GScrollBar.h>
|
||||||
#include <SharedGraphics/Painter.h>
|
#include <SharedGraphics/Painter.h>
|
||||||
|
#include <Kernel/KeyCode.h>
|
||||||
|
|
||||||
GTableView::GTableView(GWidget* parent)
|
GTableView::GTableView(GWidget* parent)
|
||||||
: GWidget(parent)
|
: GWidget(parent)
|
||||||
|
@ -167,3 +168,25 @@ int GTableView::item_count() const
|
||||||
{
|
{
|
||||||
return m_model->row_count();
|
return m_model->row_count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GTableView::keydown_event(GKeyEvent& event)
|
||||||
|
{
|
||||||
|
if (!model())
|
||||||
|
return;
|
||||||
|
auto& model = *this->model();
|
||||||
|
if (event.key() == KeyCode::Key_Return) {
|
||||||
|
model.activate(model.selected_index());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.key() == KeyCode::Key_Up) {
|
||||||
|
model.set_selected_index({ model.selected_index().row() - 1, model.selected_index().column() });
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.key() == KeyCode::Key_Down) {
|
||||||
|
model.set_selected_index({ model.selected_index().row() + 1, model.selected_index().column() });
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return GTableView::keydown_event(event);
|
||||||
|
}
|
||||||
|
|
|
@ -24,12 +24,15 @@ public:
|
||||||
int content_width() const;
|
int content_width() const;
|
||||||
int horizontal_padding() const { return m_horizontal_padding; }
|
int horizontal_padding() const { return m_horizontal_padding; }
|
||||||
|
|
||||||
|
virtual bool accepts_focus() const override { return true; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void model_notification(const GModelNotification&);
|
virtual void model_notification(const GModelNotification&);
|
||||||
|
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
virtual void paint_event(GPaintEvent&) override;
|
||||||
virtual void resize_event(GResizeEvent&) override;
|
virtual void resize_event(GResizeEvent&) override;
|
||||||
virtual void mousedown_event(GMouseEvent&) override;
|
virtual void mousedown_event(GMouseEvent&) override;
|
||||||
|
virtual void keydown_event(GKeyEvent&) override;
|
||||||
|
|
||||||
void update_scrollbar_ranges();
|
void update_scrollbar_ranges();
|
||||||
int item_count() const;
|
int item_count() const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue