Hide emoji panel if it's not under the mouse cursor

fixes #254
fixes #246
This commit is contained in:
Konstantinos Sideris 2018-04-08 15:49:53 +03:00
parent 8dc17ccecb
commit 5125433552
4 changed files with 40 additions and 11 deletions

View file

@ -37,8 +37,16 @@ signals:
void emojiSelected(const QString &emoji); void emojiSelected(const QString &emoji);
protected: protected:
void leaveEvent(QEvent *event); void leaveEvent(QEvent *event) override
void paintEvent(QPaintEvent *event); {
emit leaving();
QWidget::leaveEvent(event);
}
void paintEvent(QPaintEvent *event) override;
signals:
void leaving();
private: private:
void showCategory(const Category *category); void showCategory(const Category *category);

View file

@ -18,6 +18,7 @@
#pragma once #pragma once
#include <QEvent> #include <QEvent>
#include <QTimer>
#include <QWidget> #include <QWidget>
#include "FlatButton.h" #include "FlatButton.h"
@ -37,6 +38,7 @@ signals:
protected: protected:
void enterEvent(QEvent *e) override; void enterEvent(QEvent *e) override;
void leaveEvent(QEvent *e) override;
private: private:
// Vertical distance from panel's bottom. // Vertical distance from panel's bottom.
@ -46,5 +48,6 @@ private:
int horizontal_distance_ = 70; int horizontal_distance_ = 70;
QSharedPointer<Panel> panel_; QSharedPointer<Panel> panel_;
QTimer hideTimer_;
}; };
} // namespace emoji } // namespace emoji

View file

@ -39,7 +39,7 @@ Panel::Panel(QWidget *parent)
"QScrollBar::handle:vertical { min-height: 30px; }"); "QScrollBar::handle:vertical { min-height: 30px; }");
setAttribute(Qt::WA_ShowWithoutActivating, true); setAttribute(Qt::WA_ShowWithoutActivating, true);
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
auto mainWidget = new QWidget(this); auto mainWidget = new QWidget(this);
mainWidget->setMaximumSize(width_, height_); mainWidget->setMaximumSize(width_, height_);
@ -213,12 +213,6 @@ Panel::showCategory(const Category *category)
this->scrollArea_->ensureVisible(0, posToGo, 0, 0); this->scrollArea_->ensureVisible(0, posToGo, 0, 0);
} }
void
Panel::leaveEvent(QEvent *)
{
hide();
}
void void
Panel::paintEvent(QPaintEvent *event) Panel::paintEvent(QPaintEvent *event)
{ {

View file

@ -15,15 +15,28 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "emoji/PickButton.h" #include <QDebug>
#include "emoji/Panel.h" #include "emoji/Panel.h"
#include "emoji/PickButton.h"
using namespace emoji; 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) PickButton::PickButton(QWidget *parent)
: FlatButton(parent) : FlatButton(parent)
, panel_{nullptr} , panel_{nullptr}
{} {
connect(&hideTimer_, &QTimer::timeout, this, [this]() {
if (panel_ && !panel_->underMouse()) {
hideTimer_.stop();
panel_->hide();
}
});
}
void void
PickButton::enterEvent(QEvent *e) PickButton::enterEvent(QEvent *e)
@ -33,8 +46,12 @@ PickButton::enterEvent(QEvent *e)
if (panel_.isNull()) { if (panel_.isNull()) {
panel_ = QSharedPointer<Panel>(new Panel(this)); panel_ = QSharedPointer<Panel>(new Panel(this));
connect(panel_.data(), &Panel::emojiSelected, this, &PickButton::emojiSelected); 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()); QPoint pos(rect().x(), rect().y());
pos = this->mapToGlobal(pos); pos = this->mapToGlobal(pos);
@ -46,3 +63,10 @@ PickButton::enterEvent(QEvent *e)
panel_->move(x, y); panel_->move(x, y);
panel_->show(); panel_->show();
} }
void
PickButton::leaveEvent(QEvent *e)
{
hideTimer_.start(TimeoutDuration);
FlatButton::leaveEvent(e);
}