summary refs log tree commit diff
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-08 15:49:53 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-08 15:49:53 +0300
commit5125433552eb905fe228febc4961bca404f6bae2 (patch)
tree328af902848286d4222bb738ea6125868262f0c4
parentBump version (diff)
downloadnheko-5125433552eb905fe228febc4961bca404f6bae2.tar.xz
Hide emoji panel if it's not under the mouse cursor
fixes #254
fixes #246
-rw-r--r--include/emoji/Panel.h12
-rw-r--r--include/emoji/PickButton.h3
-rw-r--r--src/emoji/Panel.cc8
-rw-r--r--src/emoji/PickButton.cc28
4 files changed, 40 insertions, 11 deletions
diff --git a/include/emoji/Panel.h b/include/emoji/Panel.h
index 523a855e..ad233c27 100644
--- a/include/emoji/Panel.h
+++ b/include/emoji/Panel.h
@@ -37,8 +37,16 @@ signals:
         void emojiSelected(const QString &emoji);
 
 protected:
-        void leaveEvent(QEvent *event);
-        void paintEvent(QPaintEvent *event);
+        void leaveEvent(QEvent *event) override
+        {
+                emit leaving();
+                QWidget::leaveEvent(event);
+        }
+
+        void paintEvent(QPaintEvent *event) override;
+
+signals:
+        void leaving();
 
 private:
         void showCategory(const Category *category);
diff --git a/include/emoji/PickButton.h b/include/emoji/PickButton.h
index 9c30a549..9117e61f 100644
--- a/include/emoji/PickButton.h
+++ b/include/emoji/PickButton.h
@@ -18,6 +18,7 @@
 #pragma once
 
 #include <QEvent>
+#include <QTimer>
 #include <QWidget>
 
 #include "FlatButton.h"
@@ -37,6 +38,7 @@ signals:
 
 protected:
         void enterEvent(QEvent *e) override;
+        void leaveEvent(QEvent *e) override;
 
 private:
         // Vertical distance from panel's bottom.
@@ -46,5 +48,6 @@ private:
         int horizontal_distance_ = 70;
 
         QSharedPointer<Panel> panel_;
+        QTimer hideTimer_;
 };
 } // namespace emoji
diff --git a/src/emoji/Panel.cc b/src/emoji/Panel.cc
index 064dd6d5..89c3f823 100644
--- a/src/emoji/Panel.cc
+++ b/src/emoji/Panel.cc
@@ -39,7 +39,7 @@ Panel::Panel(QWidget *parent)
                       "QScrollBar::handle:vertical { min-height: 30px; }");
 
         setAttribute(Qt::WA_ShowWithoutActivating, true);
-        setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
+        setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
 
         auto mainWidget = new QWidget(this);
         mainWidget->setMaximumSize(width_, height_);
@@ -214,12 +214,6 @@ Panel::showCategory(const Category *category)
 }
 
 void
-Panel::leaveEvent(QEvent *)
-{
-        hide();
-}
-
-void
 Panel::paintEvent(QPaintEvent *event)
 {
         Q_UNUSED(event);
diff --git a/src/emoji/PickButton.cc b/src/emoji/PickButton.cc
index 530c951b..d2b4e9fd 100644
--- a/src/emoji/PickButton.cc
+++ b/src/emoji/PickButton.cc
@@ -15,15 +15,28 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "emoji/PickButton.h"
+#include <QDebug>
+
 #include "emoji/Panel.h"
+#include "emoji/PickButton.h"
 
 using namespace emoji;
 
+// Number of milliseconds after which the panel will be hidden
+// if the mouse cursor is not on top of the widget.
+constexpr int TimeoutDuration = 300;
+
 PickButton::PickButton(QWidget *parent)
   : FlatButton(parent)
   , panel_{nullptr}
-{}
+{
+        connect(&hideTimer_, &QTimer::timeout, this, [this]() {
+                if (panel_ && !panel_->underMouse()) {
+                        hideTimer_.stop();
+                        panel_->hide();
+                }
+        });
+}
 
 void
 PickButton::enterEvent(QEvent *e)
@@ -33,8 +46,12 @@ PickButton::enterEvent(QEvent *e)
         if (panel_.isNull()) {
                 panel_ = QSharedPointer<Panel>(new Panel(this));
                 connect(panel_.data(), &Panel::emojiSelected, this, &PickButton::emojiSelected);
+                connect(panel_.data(), &Panel::leaving, this, [this]() { panel_->hide(); });
         }
 
+        if (panel_->isVisible())
+                return;
+
         QPoint pos(rect().x(), rect().y());
         pos = this->mapToGlobal(pos);
 
@@ -46,3 +63,10 @@ PickButton::enterEvent(QEvent *e)
         panel_->move(x, y);
         panel_->show();
 }
+
+void
+PickButton::leaveEvent(QEvent *e)
+{
+        hideTimer_.start(TimeoutDuration);
+        FlatButton::leaveEvent(e);
+}