diff --git a/Source/Core/DolphinQt/MainWindow.cpp b/Source/Core/DolphinQt/MainWindow.cpp index 20e365ae43..022252e6d3 100644 --- a/Source/Core/DolphinQt/MainWindow.cpp +++ b/Source/Core/DolphinQt/MainWindow.cpp @@ -452,7 +452,7 @@ void MainWindow::ConnectMenuBar() connect(m_menu_bar, &MenuBar::ShowList, m_game_list, &GameList::SetListView); connect(m_menu_bar, &MenuBar::ShowGrid, m_game_list, &GameList::SetGridView); connect(m_menu_bar, &MenuBar::PurgeGameListCache, m_game_list, &GameList::PurgeCache); - connect(m_menu_bar, &MenuBar::ToggleSearch, m_search_bar, &SearchBar::Toggle); + connect(m_menu_bar, &MenuBar::ShowSearch, m_search_bar, &SearchBar::Show); connect(m_menu_bar, &MenuBar::ColumnVisibilityToggled, m_game_list, &GameList::OnColumnVisibilityToggled); diff --git a/Source/Core/DolphinQt/MenuBar.cpp b/Source/Core/DolphinQt/MenuBar.cpp index 5553e19f4f..0c872c4aef 100644 --- a/Source/Core/DolphinQt/MenuBar.cpp +++ b/Source/Core/DolphinQt/MenuBar.cpp @@ -473,7 +473,7 @@ void MenuBar::AddViewMenu() view_menu->addSeparator(); view_menu->addAction(tr("Purge Game List Cache"), this, &MenuBar::PurgeGameListCache); view_menu->addSeparator(); - view_menu->addAction(tr("Search"), this, &MenuBar::ToggleSearch, + view_menu->addAction(tr("Search"), this, &MenuBar::ShowSearch, QKeySequence(QStringLiteral("Ctrl+F"))); } diff --git a/Source/Core/DolphinQt/MenuBar.h b/Source/Core/DolphinQt/MenuBar.h index fc702bb5c4..dbb962e2ac 100644 --- a/Source/Core/DolphinQt/MenuBar.h +++ b/Source/Core/DolphinQt/MenuBar.h @@ -96,7 +96,7 @@ signals: void ShowList(); void ShowGrid(); void PurgeGameListCache(); - void ToggleSearch(); + void ShowSearch(); void ColumnVisibilityToggled(const QString& row, bool visible); void GameListPlatformVisibilityToggled(const QString& row, bool visible); void GameListRegionVisibilityToggled(const QString& row, bool visible); diff --git a/Source/Core/DolphinQt/SearchBar.cpp b/Source/Core/DolphinQt/SearchBar.cpp index e546a18d98..9289d1a5a5 100644 --- a/Source/Core/DolphinQt/SearchBar.cpp +++ b/Source/Core/DolphinQt/SearchBar.cpp @@ -4,7 +4,9 @@ #include "DolphinQt/SearchBar.h" +#include #include +#include #include #include @@ -16,6 +18,8 @@ SearchBar::SearchBar(QWidget* parent) : QWidget(parent) setFixedHeight(32); setHidden(true); + + installEventFilter(this); } void SearchBar::CreateWidgets() @@ -34,20 +38,40 @@ void SearchBar::CreateWidgets() setLayout(layout); } -void SearchBar::Toggle() +void SearchBar::Show() { - m_search_edit->clear(); + m_search_edit->setFocus(); + m_search_edit->selectAll(); - setHidden(isVisible()); + // Re-apply the filter string. + emit Search(m_search_edit->text()); - if (isVisible()) - m_search_edit->setFocus(); - else - m_search_edit->clearFocus(); + show(); +} + +void SearchBar::Hide() +{ + // Clear the filter string. + emit Search(QString()); + + m_search_edit->clearFocus(); + + hide(); } void SearchBar::ConnectWidgets() { connect(m_search_edit, &QLineEdit::textChanged, this, &SearchBar::Search); - connect(m_close_button, &QPushButton::pressed, this, &SearchBar::Toggle); + connect(m_close_button, &QPushButton::pressed, this, &SearchBar::Hide); +} + +bool SearchBar::eventFilter(QObject* object, QEvent* event) +{ + if (event->type() == QEvent::KeyPress) + { + if (static_cast(event)->key() == Qt::Key_Escape) + Hide(); + } + + return false; } diff --git a/Source/Core/DolphinQt/SearchBar.h b/Source/Core/DolphinQt/SearchBar.h index 7db5bc0f1a..957becf474 100644 --- a/Source/Core/DolphinQt/SearchBar.h +++ b/Source/Core/DolphinQt/SearchBar.h @@ -15,7 +15,9 @@ class SearchBar : public QWidget public: explicit SearchBar(QWidget* parent = nullptr); - void Toggle(); + void Show(); + void Hide(); + signals: void Search(const QString& serach); @@ -23,6 +25,8 @@ private: void CreateWidgets(); void ConnectWidgets(); + bool eventFilter(QObject* object, QEvent* event) final override; + QLineEdit* m_search_edit; QPushButton* m_close_button; };