Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Hide emoji panel if it's not under the mouse cursor
Browse files Browse the repository at this point in the history
fixes #254
fixes #246
  • Loading branch information
mujx committed Apr 8, 2018
1 parent 8dc17cc commit 5125433
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
12 changes: 10 additions & 2 deletions include/emoji/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ class Panel : public QWidget
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);
Expand Down
3 changes: 3 additions & 0 deletions include/emoji/PickButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <QEvent>
#include <QTimer>
#include <QWidget>

#include "FlatButton.h"
Expand All @@ -37,6 +38,7 @@ class PickButton : public FlatButton

protected:
void enterEvent(QEvent *e) override;
void leaveEvent(QEvent *e) override;

private:
// Vertical distance from panel's bottom.
Expand All @@ -46,5 +48,6 @@ class PickButton : public FlatButton
int horizontal_distance_ = 70;

QSharedPointer<Panel> panel_;
QTimer hideTimer_;
};
} // namespace emoji
8 changes: 1 addition & 7 deletions src/emoji/Panel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down Expand Up @@ -213,12 +213,6 @@ Panel::showCategory(const Category *category)
this->scrollArea_->ensureVisible(0, posToGo, 0, 0);
}

void
Panel::leaveEvent(QEvent *)
{
hide();
}

void
Panel::paintEvent(QPaintEvent *event)
{
Expand Down
28 changes: 26 additions & 2 deletions src/emoji/PickButton.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand All @@ -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);
}

0 comments on commit 5125433

Please sign in to comment.