LibGUI: Allow scrolling through a ComboBox with the mouse wheel
This commit is contained in:
parent
18ff75e67b
commit
e064999e0d
Notes:
sideshowbarker
2024-07-19 06:42:47 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/e064999e0d9
4 changed files with 51 additions and 26 deletions
Libraries/LibGUI
|
@ -35,9 +35,28 @@
|
|||
|
||||
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()
|
||||
{
|
||||
m_editor = add<TextBox>();
|
||||
m_editor = add<ComboBoxEditor>();
|
||||
m_editor->on_change = [this] {
|
||||
if (on_change)
|
||||
on_change(m_editor->text(), m_list_view->selection().first());
|
||||
|
@ -74,6 +93,10 @@ ComboBox::ComboBox()
|
|||
on_change(m_editor->text(), index);
|
||||
});
|
||||
};
|
||||
|
||||
m_editor->on_mousewheel = [this](int delta) {
|
||||
m_list_view->move_selection(delta);
|
||||
};
|
||||
}
|
||||
|
||||
ComboBox::~ComboBox()
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
|
||||
namespace GUI {
|
||||
|
||||
class ComboBoxEditor;
|
||||
|
||||
class ComboBox : public Widget {
|
||||
C_OBJECT(ComboBox)
|
||||
public:
|
||||
|
@ -62,7 +64,7 @@ protected:
|
|||
virtual void resize_event(ResizeEvent&) override;
|
||||
|
||||
private:
|
||||
RefPtr<TextBox> m_editor;
|
||||
RefPtr<ComboBoxEditor> m_editor;
|
||||
RefPtr<Button> m_open_button;
|
||||
RefPtr<Window> m_list_window;
|
||||
RefPtr<ListView> m_list_view;
|
||||
|
|
|
@ -182,6 +182,25 @@ 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()) {
|
||||
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)
|
||||
{
|
||||
if (!model())
|
||||
|
@ -192,33 +211,11 @@ void ListView::keydown_event(KeyEvent& event)
|
|||
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() - 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();
|
||||
}
|
||||
move_selection(-1);
|
||||
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() + 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();
|
||||
}
|
||||
move_selection(1);
|
||||
return;
|
||||
}
|
||||
if (event.key() == KeyCode::Key_PageUp) {
|
||||
|
|
|
@ -53,6 +53,9 @@ public:
|
|||
void set_model_column(int column) { m_model_column = column; }
|
||||
|
||||
virtual void select_all() override;
|
||||
|
||||
void move_selection(int steps);
|
||||
|
||||
private:
|
||||
ListView();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue