diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-12 15:10:31 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-12 15:11:06 +0200 |
commit | e064999e0d9d6d91b846a9d8afe3e05e45559dd3 (patch) | |
tree | 0ebcb3ecdaf30dc6f02930e2a8af56da67d1ff17 | |
parent | 18ff75e67bab9ea840bd57fcd6ef33f9275eb9dc (diff) | |
download | serenity-e064999e0d9d6d91b846a9d8afe3e05e45559dd3.zip |
LibGUI: Allow scrolling through a ComboBox with the mouse wheel
-rw-r--r-- | Libraries/LibGUI/ComboBox.cpp | 25 | ||||
-rw-r--r-- | Libraries/LibGUI/ComboBox.h | 4 | ||||
-rw-r--r-- | Libraries/LibGUI/ListView.cpp | 45 | ||||
-rw-r--r-- | Libraries/LibGUI/ListView.h | 3 |
4 files changed, 51 insertions, 26 deletions
diff --git a/Libraries/LibGUI/ComboBox.cpp b/Libraries/LibGUI/ComboBox.cpp index 62461a8399..160994c8fa 100644 --- a/Libraries/LibGUI/ComboBox.cpp +++ b/Libraries/LibGUI/ComboBox.cpp @@ -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() diff --git a/Libraries/LibGUI/ComboBox.h b/Libraries/LibGUI/ComboBox.h index 8e3232996b..bb8d51877b 100644 --- a/Libraries/LibGUI/ComboBox.h +++ b/Libraries/LibGUI/ComboBox.h @@ -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; diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp index 5e55158ca4..68592a0f79 100644 --- a/Libraries/LibGUI/ListView.cpp +++ b/Libraries/LibGUI/ListView.cpp @@ -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) { diff --git a/Libraries/LibGUI/ListView.h b/Libraries/LibGUI/ListView.h index 44abe208e2..f9a7c42319 100644 --- a/Libraries/LibGUI/ListView.h +++ b/Libraries/LibGUI/ListView.h @@ -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(); |