Delete emote popup when it is closed.

This commit is contained in:
fourtf 2019-08-13 16:39:22 +02:00
parent 18f3a816ed
commit 7bf5a79f8a
6 changed files with 83 additions and 5 deletions

View file

@ -350,6 +350,7 @@ HEADERS += \
src/util/IsBigEndian.hpp \ src/util/IsBigEndian.hpp \
src/util/JsonQuery.hpp \ src/util/JsonQuery.hpp \
src/util/LayoutCreator.hpp \ src/util/LayoutCreator.hpp \
src/util/QObjectRef.hpp \
src/util/QStringHash.hpp \ src/util/QStringHash.hpp \
src/util/rangealgorithm.hpp \ src/util/rangealgorithm.hpp \
src/util/RapidjsonHelpers.hpp \ src/util/RapidjsonHelpers.hpp \

74
src/util/QObjectRef.hpp Normal file
View file

@ -0,0 +1,74 @@
#pragma once
#include <QObject>
#include <type_traits>
namespace chatterino
{
/// Holds a pointer to a QObject and resets it to nullptr if the QObject
/// gets destroyed.
template <typename T>
class QObjectRef
{
public:
QObjectRef()
{
static_assert(std::is_base_of_v<QObject, T>);
}
explicit QObjectRef(T* t)
{
static_assert(std::is_base_of_v<QObject, T>);
this->swap(t);
}
~QObjectRef()
{
this->swap(nullptr);
}
QObjectRef& operator=(T* t)
{
this->swap(t);
return *this;
}
operator bool()
{
return t_;
}
T* operator->()
{
return t_;
}
T* get()
{
return t_;
}
QObject* swap(T* other)
{
// old
if (this->conn_)
{
QObject::disconnect(this->conn_);
}
// new
if (other)
{
QObject::connect(other, &QObject::destroyed,
[this]() { this->swap(nullptr); });
}
return std::exchange(this->t_, other);
}
private:
T* t_{};
QMetaObject::Connection conn_;
};
} // namespace chatterino

View file

@ -100,8 +100,8 @@ namespace {
} }
} // namespace } // namespace
EmotePopup::EmotePopup() EmotePopup::EmotePopup(QWidget *parent)
: BaseWindow(nullptr, BaseWindow::EnableCustomFrame) : BaseWindow(parent, BaseWindow::EnableCustomFrame)
{ {
auto layout = new QVBoxLayout(this); auto layout = new QVBoxLayout(this);
this->getLayoutContainer()->setLayout(layout); this->getLayoutContainer()->setLayout(layout);

View file

@ -14,7 +14,7 @@ using ChannelPtr = std::shared_ptr<Channel>;
class EmotePopup : public BaseWindow class EmotePopup : public BaseWindow
{ {
public: public:
EmotePopup(); EmotePopup(QWidget *parent = nullptr);
void loadChannel(ChannelPtr channel); void loadChannel(ChannelPtr channel);
void loadEmojis(); void loadEmojis();

View file

@ -153,7 +153,9 @@ void SplitInput::openEmotePopup()
{ {
if (!this->emotePopup_) if (!this->emotePopup_)
{ {
this->emotePopup_ = std::make_unique<EmotePopup>(); this->emotePopup_ = new EmotePopup(this);
this->emotePopup_->setAttribute(Qt::WA_DeleteOnClose);
this->emotePopup_->linkClicked.connect([this](const Link &link) { this->emotePopup_->linkClicked.connect([this](const Link &link) {
if (link.type == Link::InsertText) if (link.type == Link::InsertText)
{ {

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "util/QObjectRef.hpp"
#include "widgets/BaseWidget.hpp" #include "widgets/BaseWidget.hpp"
#include <QHBoxLayout> #include <QHBoxLayout>
@ -46,7 +47,7 @@ private:
void openEmotePopup(); void openEmotePopup();
Split *const split_; Split *const split_;
std::shared_ptr<EmotePopup> emotePopup_; QObjectRef<EmotePopup> emotePopup_;
struct { struct {
ResizingTextEdit *textEdit; ResizingTextEdit *textEdit;