summary refs log tree commit diff
path: root/src/SuggestionsPopup.cpp
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-14 14:12:36 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-14 14:12:36 +0300
commitca66940ec39047eada21f7900bf0e49d61cb45fb (patch)
tree5c71ab04163061e444c7146f62a356e773a97119 /src/SuggestionsPopup.cpp
parentBump version to v0.3.1 (diff)
downloadnheko-ca66940ec39047eada21f7900bf0e49d61cb45fb.tar.xz
Popup improvements
- ESC closes the popup.
- Up/Down arrows for navigation.
- BackTab for backwards navigation.

fixes #301
fixes #302
Diffstat (limited to 'src/SuggestionsPopup.cpp')
-rw-r--r--src/SuggestionsPopup.cpp59
1 files changed, 43 insertions, 16 deletions
diff --git a/src/SuggestionsPopup.cpp b/src/SuggestionsPopup.cpp
index 174076f9..51229806 100644
--- a/src/SuggestionsPopup.cpp
+++ b/src/SuggestionsPopup.cpp
@@ -1,8 +1,8 @@
-#include "SuggestionsPopup.hpp"
 #include "Avatar.h"
 #include "AvatarProvider.h"
 #include "Config.h"
 #include "DropShadow.h"
+#include "SuggestionsPopup.hpp"
 #include "Utils.h"
 #include "timeline/TimelineViewManager.h"
 
@@ -72,7 +72,6 @@ PopupItem::mousePressEvent(QMouseEvent *event)
 
 SuggestionsPopup::SuggestionsPopup(QWidget *parent)
   : QWidget(parent)
-  , tab_clicks_(0)
 {
         setAttribute(Qt::WA_ShowWithoutActivating, true);
         setWindowFlags(Qt::ToolTip | Qt::NoDropShadowWindowHint);
@@ -103,41 +102,69 @@ SuggestionsPopup::addUsers(const QVector<SearchResult> &users)
                 connect(user, &PopupItem::clicked, this, &SuggestionsPopup::itemSelected);
         }
 
-        tab_clicks_ = 0; // Reset to start from the beginning of pop-up window on next invocation.
+        resetSelection();
 
         resize(geometry().width(), 40 * users.size());
 }
 
 void
-SuggestionsPopup::cycleThroughSuggestions()
+SuggestionsPopup::hoverSelection()
+{
+        resetHovering();
+        setHovering(selectedItem_);
+        update();
+}
+
+void
+SuggestionsPopup::selectNextSuggestion()
+{
+        selectedItem_++;
+        if (selectedItem_ >= layout_->count())
+                selectFirstItem();
+
+        hoverSelection();
+}
+
+void
+SuggestionsPopup::selectPreviousSuggestion()
 {
-        tab_clicks_ %= layout_->count(); // Stay within the number of items in layout.
+        selectedItem_--;
+        if (selectedItem_ < 0)
+                selectLastItem();
+
+        hoverSelection();
+}
 
-        // Reset flag for hovering effect first.
+void
+SuggestionsPopup::resetHovering()
+{
         for (int i = 0; i < layout_->count(); ++i) {
-                const auto &p = qobject_cast<PopupItem *>(layout_->itemAt(i)->widget());
-                p->setHovering(false);
+                const auto item = qobject_cast<PopupItem *>(layout_->itemAt(i)->widget());
+
+                if (item)
+                        item->setHovering(false);
         }
+}
 
-        const auto &item   = layout_->itemAt(tab_clicks_);
+void
+SuggestionsPopup::setHovering(int pos)
+{
+        const auto &item   = layout_->itemAt(pos);
         const auto &widget = qobject_cast<PopupItem *>(item->widget());
-        widget->setHovering(true);
-
-        ++tab_clicks_;
 
-        update(); // Request to update the paint event.
+        if (widget)
+                widget->setHovering(true);
 }
 
 void
 SuggestionsPopup::selectHoveredSuggestion()
 {
-        // Each tab press increments the counter by one, so the element desired is one off.
-        const auto item = layout_->itemAt(tab_clicks_ - 1);
+        const auto item = layout_->itemAt(selectedItem_);
         if (!item)
                 return;
 
         const auto &widget = qobject_cast<PopupItem *>(item->widget());
         emit itemSelected(TimelineViewManager::displayName(widget->user()));
 
-        tab_clicks_ = 0; // Reset to start from the beginning of pop-up window on next invocation.
+        resetSelection();
 }