From 6a78db07f11e101901333e00cb0bac76a32874ff Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Tue, 14 Jul 2020 17:18:12 -0400 Subject: [PATCH] LibGUI: Add hover highlighting and keyboard controls to ComboBox Adds a new highlighting effect to the actively selected row in ComboBox ListView. ComboBoxEditor can now be controlled with page up, page down, and the up and down arrow keys. ESC and loss of focus now cause comboboxes to close. Now activates on mouseup as well as return. --- Libraries/LibGUI/AbstractView.cpp | 13 +++++++ Libraries/LibGUI/AbstractView.h | 3 ++ Libraries/LibGUI/ComboBox.cpp | 53 ++++++++++++++++++++++++--- Libraries/LibGUI/ListView.cpp | 61 ++++++++++++++++++++++++++----- Libraries/LibGUI/ListView.h | 6 +++ Libraries/LibGUI/TextEditor.cpp | 16 ++++++++ Libraries/LibGUI/TextEditor.h | 5 +++ 7 files changed, 142 insertions(+), 15 deletions(-) diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp index ab6199d55d1..6635f2d0eaa 100644 --- a/Libraries/LibGUI/AbstractView.cpp +++ b/Libraries/LibGUI/AbstractView.cpp @@ -225,6 +225,16 @@ void AbstractView::set_hovered_index(const ModelIndex& index) if (m_hovered_index == index) return; m_hovered_index = index; + if (m_hovered_index.is_valid()) + m_last_valid_hovered_index = m_hovered_index; + update(); +} + +void AbstractView::set_last_valid_hovered_index(const ModelIndex& index) +{ + if (m_last_valid_hovered_index == index) + return; + m_last_valid_hovered_index = index; update(); } @@ -325,6 +335,9 @@ void AbstractView::mouseup_event(MouseEvent& event) m_might_drag = false; update(); } + + if (activates_on_selection()) + activate_selected(); } void AbstractView::doubleclick_event(MouseEvent& event) diff --git a/Libraries/LibGUI/AbstractView.h b/Libraries/LibGUI/AbstractView.h index 295efa546e1..7910871952a 100644 --- a/Libraries/LibGUI/AbstractView.h +++ b/Libraries/LibGUI/AbstractView.h @@ -74,6 +74,8 @@ public: NonnullRefPtr font_for_index(const ModelIndex&) const; + void set_last_valid_hovered_index(const ModelIndex&); + protected: AbstractView(); virtual ~AbstractView() override; @@ -107,6 +109,7 @@ protected: bool m_might_drag { false }; ModelIndex m_hovered_index; + ModelIndex m_last_valid_hovered_index; private: RefPtr m_model; diff --git a/Libraries/LibGUI/ComboBox.cpp b/Libraries/LibGUI/ComboBox.cpp index 42a0bdf61ec..ae6a236edbd 100644 --- a/Libraries/LibGUI/ComboBox.cpp +++ b/Libraries/LibGUI/ComboBox.cpp @@ -49,6 +49,8 @@ private: virtual void mousewheel_event(MouseEvent& event) override { + if (!is_focused()) + set_focus(true); if (on_mousewheel) on_mousewheel(event.wheel_delta()); } @@ -57,6 +59,7 @@ private: ComboBox::ComboBox() { m_editor = add(); + m_editor->set_has_open_button(true); m_editor->on_change = [this] { if (on_change) on_change(m_editor->text(), m_list_view->selection().first()); @@ -65,6 +68,27 @@ ComboBox::ComboBox() if (on_return_pressed) on_return_pressed(); }; + m_editor->on_up_pressed = [this] { + m_list_view->move_selection(-1); + }; + m_editor->on_down_pressed = [this] { + m_list_view->move_selection(1); + }; + m_editor->on_pageup_pressed = [this] { + m_list_view->move_selection(-m_list_view->selection().first().row()); + }; + m_editor->on_pagedown_pressed = [this] { + if (model()) + m_list_view->move_selection((model()->row_count() - 1) - m_list_view->selection().first().row()); + }; + m_editor->on_mousewheel = [this](int delta) { + m_list_view->move_selection(delta); + }; + m_editor->on_mousedown = [this] { + if (only_allow_values_from_model()) + m_open_button->click(); + }; + m_open_button = add