mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:37:35 +00:00
LibGUI: Allow scrolling through a ComboBox with the mouse wheel
This commit is contained in:
parent
18ff75e67b
commit
e064999e0d
4 changed files with 51 additions and 26 deletions
|
@ -35,9 +35,28 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
|
class ComboBoxEditor final : public TextEditor {
|
||||||
|
C_OBJECT(ComboBoxEditor);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Function<void(int delta)> on_mousewheel;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ComboBoxEditor()
|
||||||
|
: TextEditor(TextEditor::SingleLine)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void mousewheel_event(MouseEvent& event) override
|
||||||
|
{
|
||||||
|
if (on_mousewheel)
|
||||||
|
on_mousewheel(event.wheel_delta());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ComboBox::ComboBox()
|
ComboBox::ComboBox()
|
||||||
{
|
{
|
||||||
m_editor = add<TextBox>();
|
m_editor = add<ComboBoxEditor>();
|
||||||
m_editor->on_change = [this] {
|
m_editor->on_change = [this] {
|
||||||
if (on_change)
|
if (on_change)
|
||||||
on_change(m_editor->text(), m_list_view->selection().first());
|
on_change(m_editor->text(), m_list_view->selection().first());
|
||||||
|
@ -74,6 +93,10 @@ ComboBox::ComboBox()
|
||||||
on_change(m_editor->text(), index);
|
on_change(m_editor->text(), index);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
m_editor->on_mousewheel = [this](int delta) {
|
||||||
|
m_list_view->move_selection(delta);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ComboBox::~ComboBox()
|
ComboBox::~ComboBox()
|
||||||
|
|
|
@ -30,6 +30,8 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
|
class ComboBoxEditor;
|
||||||
|
|
||||||
class ComboBox : public Widget {
|
class ComboBox : public Widget {
|
||||||
C_OBJECT(ComboBox)
|
C_OBJECT(ComboBox)
|
||||||
public:
|
public:
|
||||||
|
@ -62,7 +64,7 @@ protected:
|
||||||
virtual void resize_event(ResizeEvent&) override;
|
virtual void resize_event(ResizeEvent&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RefPtr<TextBox> m_editor;
|
RefPtr<ComboBoxEditor> m_editor;
|
||||||
RefPtr<Button> m_open_button;
|
RefPtr<Button> m_open_button;
|
||||||
RefPtr<Window> m_list_window;
|
RefPtr<Window> m_list_window;
|
||||||
RefPtr<ListView> m_list_view;
|
RefPtr<ListView> m_list_view;
|
||||||
|
|
|
@ -182,6 +182,25 @@ int ListView::item_count() const
|
||||||
return model()->row_count();
|
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)
|
void ListView::keydown_event(KeyEvent& event)
|
||||||
{
|
{
|
||||||
if (!model())
|
if (!model())
|
||||||
|
@ -192,33 +211,11 @@ void ListView::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) {
|
||||||
|
|
|
@ -53,6 +53,9 @@ public:
|
||||||
void set_model_column(int column) { m_model_column = column; }
|
void set_model_column(int column) { m_model_column = column; }
|
||||||
|
|
||||||
virtual void select_all() override;
|
virtual void select_all() override;
|
||||||
|
|
||||||
|
void move_selection(int steps);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ListView();
|
ListView();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue