improved split header menu button

This commit is contained in:
fourtf 2018-08-07 09:38:18 +02:00
parent f34063213c
commit 5df231f072
5 changed files with 92 additions and 34 deletions

View file

@ -249,7 +249,8 @@ SOURCES += \
src/util/JsonQuery.cpp \
src/RunGui.cpp \
src/BrowserExtension.cpp \
src/util/FormatTime.cpp
src/util/FormatTime.cpp \
src/util/FunctionEventFilter.cpp
HEADERS += \
src/Application.hpp \
@ -446,7 +447,8 @@ HEADERS += \
src/util/JsonQuery.hpp \
src/RunGui.hpp \
src/BrowserExtension.hpp \
src/util/FormatTime.hpp
src/util/FormatTime.hpp \
src/util/FunctionEventFilter.hpp
RESOURCES += \
resources/resources.qrc \

View file

@ -0,0 +1,17 @@
#include "FunctionEventFilter.hpp"
namespace chatterino {
FunctionEventFilter::FunctionEventFilter(
QObject *parent, std::function<bool(QObject *, QEvent *)> function)
: QObject(parent)
, function_(std::move(function))
{
}
bool FunctionEventFilter::eventFilter(QObject *watched, QEvent *event)
{
return this->function_(watched, event);
}
} // namespace chatterino

View file

@ -0,0 +1,24 @@
#pragma once
#include <QEvent>
#include <QObject>
#include <functional>
namespace chatterino {
class FunctionEventFilter : public QObject
{
Q_OBJECT
public:
FunctionEventFilter(QObject *parent,
std::function<bool(QObject *, QEvent *)> function);
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
private:
std::function<bool(QObject *, QEvent *)> function_;
};
} // namespace chatterino

View file

@ -6,6 +6,7 @@
#include "providers/twitch/TwitchServer.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Theme.hpp"
#include "util/FunctionEventFilter.hpp"
#include "util/LayoutCreator.hpp"
#include "widgets/Label.hpp"
#include "widgets/TooltipWidget.hpp"
@ -86,32 +87,12 @@ SplitHeader::SplitHeader(Split *_split)
// dropdown->setPixmap(*app->resources->splitHeaderContext->getPixmap());
// dropdown->setScaleIndependantSize(23, 23);
this->addDropdownItems(dropdown.getElement());
QObject::connect(
dropdown.getElement(), &RippleEffectButton::leftMousePress, this,
[this] {
QTimer::singleShot(80, this, [this] {
auto point = [this] {
auto bounds =
QApplication::desktop()->availableGeometry(this);
auto point = this->dropdownButton_->mapToGlobal(
QPoint(this->dropdownButton_->width() -
this->dropdownMenu_.width(),
this->dropdownButton_->height()));
if (point.y() + this->dropdownMenu_.height() >
bounds.bottom()) {
point.setY(point.y() -
this->dropdownMenu_.height() -
this->dropdownButton_->height());
QObject::connect(dropdown.getElement(),
&RippleEffectButton::leftMousePress, this, [this] {
if (!this->menuVisible_) {
QTimer::singleShot(
80, this, [this] { this->showMenu(); });
}
return point;
};
this->dropdownMenu_.popup(point());
this->dropdownMenu_.move(point());
});
});
}
@ -147,6 +128,15 @@ SplitHeader::SplitHeader(Split *_split)
getSettings()->showUptime.connect(
[this](const auto &, const auto &) { this->updateChannelText(); },
this->managedConnections_);
this->dropdownMenu_.installEventFilter(
new FunctionEventFilter(this, [this](QObject *, QEvent *event) {
if (event->type() == QEvent::Hide) {
QTimer::singleShot(20, this,
[this] { this->menuVisible_ = false; });
}
return false;
}));
}
SplitHeader::~SplitHeader()
@ -191,6 +181,28 @@ void SplitHeader::addDropdownItems(RippleEffectButton *)
// clang-format on
}
void SplitHeader::showMenu()
{
auto point = [this] {
auto bounds = QApplication::desktop()->availableGeometry(this);
auto point = this->dropdownButton_->mapToGlobal(
QPoint(this->dropdownButton_->width() - this->dropdownMenu_.width(),
this->dropdownButton_->height()));
if (point.y() + this->dropdownMenu_.height() > bounds.bottom()) {
point.setY(point.y() - this->dropdownMenu_.height() -
this->dropdownButton_->height());
}
return point;
};
this->dropdownMenu_.popup(point());
this->dropdownMenu_.move(point());
this->menuVisible_ = true;
}
void SplitHeader::updateRoomModes()
{
this->modeUpdateRequested_.invoke();

View file

@ -54,6 +54,7 @@ private:
void addModeActions(QMenu &menu);
void setupModeLabel(RippleEffectLabel &label);
void addDropdownItems(RippleEffectButton *label);
void showMenu();
Split *const split_;
@ -64,15 +65,17 @@ private:
pajlada::Signals::Connection onlineStatusChangedConnection_;
RippleEffectButton *dropdownButton_ = nullptr;
// Label *titleLabel;
Label *titleLabel = nullptr;
RippleEffectLabel *modeButton_ = nullptr;
RippleEffectButton *moderationButton_ = nullptr;
RippleEffectButton *dropdownButton_{};
// Label *titleLabel{};
Label *titleLabel{};
RippleEffectLabel *modeButton_{};
RippleEffectButton *moderationButton_{};
QMenu dropdownMenu_;
QMenu modeMenu_;
bool menuVisible_{};
pajlada::Signals::NoArgSignal modeUpdateRequested_;
QString tooltip_;