added mechanic for deleting a BaseWindow on focus out

This commit is contained in:
fourtf 2018-07-05 10:34:04 +02:00
parent 335cbf8758
commit f02a89690e
5 changed files with 60 additions and 44 deletions

View file

@ -51,10 +51,6 @@ BaseWindow::BaseWindow(QWidget *parent, Flags _flags)
this->setWindowFlag(Qt::FramelessWindowHint); this->setWindowFlag(Qt::FramelessWindowHint);
} }
if (this->flags_ & DeleteOnFocusOut) {
this->setAttribute(Qt::WA_DeleteOnClose);
}
this->init(); this->init();
this->connections_.managedConnect( this->connections_.managedConnect(
@ -186,6 +182,16 @@ bool BaseWindow::getStayInScreenRect() const
return this->stayInScreenRect_; return this->stayInScreenRect_;
} }
void BaseWindow::setActionOnFocusLoss(ActionOnFocusLoss value)
{
this->actionOnFocusLoss_ = value;
}
BaseWindow::ActionOnFocusLoss BaseWindow::getActionOnFocusLoss() const
{
return this->actionOnFocusLoss_;
}
QWidget *BaseWindow::getLayoutContainer() QWidget *BaseWindow::getLayoutContainer()
{ {
if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
@ -235,9 +241,7 @@ void BaseWindow::themeRefreshEvent()
bool BaseWindow::event(QEvent *event) bool BaseWindow::event(QEvent *event)
{ {
if (event->type() == QEvent::WindowDeactivate /*|| event->type() == QEvent::FocusOut*/) { if (event->type() == QEvent::WindowDeactivate /*|| event->type() == QEvent::FocusOut*/) {
if (this->flags_ & DeleteOnFocusOut) { this->onFocusLost();
this->close();
}
} }
return QWidget::event(event); return QWidget::event(event);
@ -260,6 +264,25 @@ void BaseWindow::wheelEvent(QWheelEvent *event)
} }
} }
void BaseWindow::onFocusLost()
{
switch (this->getActionOnFocusLoss()) {
case Delete: {
this->deleteLater();
} break;
case Close: {
this->close();
} break;
case Hide: {
this->hide();
} break;
default:;
}
}
void BaseWindow::mousePressEvent(QMouseEvent *event) void BaseWindow::mousePressEvent(QMouseEvent *event)
{ {
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
@ -470,12 +493,7 @@ void BaseWindow::paintEvent(QPaintEvent *)
painter.drawRect(0, 0, this->width() - 1, this->height() - 1); painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
} }
// this->drawCustomWindowFrame(painter); this->drawCustomWindowFrame(painter);
// QPainter painter(this);
QColor bg = this->overrideBackgroundColor_.value_or(this->themeManager->window.background);
painter.fillRect(QRect(0, 1, this->width() - 0, this->height() - 0), bg);
} }
void BaseWindow::updateScale() void BaseWindow::updateScale()
@ -514,16 +532,15 @@ void BaseWindow::calcButtonsSizes()
void BaseWindow::drawCustomWindowFrame(QPainter &painter) void BaseWindow::drawCustomWindowFrame(QPainter &painter)
{ {
//#ifdef USEWINSDK #ifdef USEWINSDK
// if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
// QPainter painter(this); QPainter painter(this);
// QColor bg = QColor bg = this->overrideBackgroundColor_.value_or(this->themeManager->window.background);
// this->overrideBackgroundColor_.value_or(this->themeManager->window.background);
// painter.fillRect(QRect(0, 1, this->width() - 0, this->height() - 0), bg); painter.fillRect(QRect(0, 1, this->width() - 0, this->height() - 0), bg);
// } }
//#endif #endif
} }
bool BaseWindow::handleDPICHANGED(MSG *msg) bool BaseWindow::handleDPICHANGED(MSG *msg)
@ -606,7 +623,9 @@ bool BaseWindow::handleSIZE(MSG *msg)
{ {
#ifdef USEWINSDK #ifdef USEWINSDK
if (this->ui_.windowLayout) { if (this->ui_.windowLayout) {
if (this->hasCustomWindowFrame() && !this->frameless_) { if (this->frameless_) {
//
} else if (this->hasCustomWindowFrame()) {
if (msg->wParam == SIZE_MAXIMIZED) { if (msg->wParam == SIZE_MAXIMIZED) {
auto offset = int(this->getScale() * 8); auto offset = int(this->getScale() * 8);

View file

@ -26,13 +26,13 @@ public:
EnableCustomFrame = 1, EnableCustomFrame = 1,
Frameless = 2, Frameless = 2,
TopMost = 4, TopMost = 4,
DeleteOnFocusOut = 8, DisableCustomScaling = 8,
DisableCustomScaling = 16, FramelessDraggable = 16,
FramelessDraggable = 32,
}; };
enum ActionOnFocusLoss { Nothing, Delete, Close, Hide };
explicit BaseWindow(QWidget *parent = nullptr, Flags flags_ = None); explicit BaseWindow(QWidget *parent = nullptr, Flags flags_ = None);
virtual ~BaseWindow() = default;
QWidget *getLayoutContainer(); QWidget *getLayoutContainer();
bool hasCustomWindowFrame(); bool hasCustomWindowFrame();
@ -43,6 +43,9 @@ public:
void setStayInScreenRect(bool value); void setStayInScreenRect(bool value);
bool getStayInScreenRect() const; bool getStayInScreenRect() const;
void setActionOnFocusLoss(ActionOnFocusLoss value);
ActionOnFocusLoss getActionOnFocusLoss() const;
void moveTo(QWidget *widget, QPoint point, bool offset = true); void moveTo(QWidget *widget, QPoint point, bool offset = true);
virtual float getScale() const override; virtual float getScale() const override;
@ -78,6 +81,7 @@ private:
void moveIntoDesktopRect(QWidget *parent); void moveIntoDesktopRect(QWidget *parent);
void calcButtonsSizes(); void calcButtonsSizes();
void drawCustomWindowFrame(QPainter &painter); void drawCustomWindowFrame(QPainter &painter);
void onFocusLost();
bool handleDPICHANGED(MSG *msg); bool handleDPICHANGED(MSG *msg);
bool handleSHOWWINDOW(MSG *msg); bool handleSHOWWINDOW(MSG *msg);
@ -86,6 +90,7 @@ private:
bool handleNCHITTEST(MSG *msg, long *result); bool handleNCHITTEST(MSG *msg, long *result);
bool enableCustomFrame_; bool enableCustomFrame_;
ActionOnFocusLoss actionOnFocusLoss_ = Nothing;
bool frameless_; bool frameless_;
bool stayInScreenRect_ = false; bool stayInScreenRect_ = false;
bool shown_ = false; bool shown_ = false;

View file

@ -63,7 +63,7 @@ Window::Window(WindowType _type)
update->setPixmap(QPixmap(":/images/download_update.png")); update->setPixmap(QPixmap(":/images/download_update.png"));
QObject::connect(update, &TitleBarButton::clicked, this, [this, update] { QObject::connect(update, &TitleBarButton::clicked, this, [this, update] {
auto dialog = new UpdatePromptDialog(); auto dialog = new UpdatePromptDialog();
dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->setActionOnFocusLoss(BaseWindow::Delete);
dialog->move(update->mapToGlobal(QPoint(-100 * this->getScale(), update->height()))); dialog->move(update->mapToGlobal(QPoint(-100 * this->getScale(), update->height())));
dialog->show(); dialog->show();
dialog->raise(); dialog->raise();

View file

@ -21,8 +21,7 @@
namespace chatterino { namespace chatterino {
UserInfoPopup::UserInfoPopup() UserInfoPopup::UserInfoPopup()
: BaseWindow(nullptr, BaseWindow::Flags(BaseWindow::Frameless | BaseWindow::DeleteOnFocusOut | : BaseWindow(nullptr, BaseWindow::Flags(BaseWindow::Frameless | BaseWindow::FramelessDraggable))
BaseWindow::FramelessDraggable))
, hack_(new bool) , hack_(new bool)
{ {
this->setStayInScreenRect(true); this->setStayInScreenRect(true);
@ -364,11 +363,9 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
} }
a->setBorderColor(color1); a->setBorderColor(color1);
QObject::connect( QObject::connect(a.getElement(), &RippleEffectLabel2::clicked, [
a.getElement(), &RippleEffectLabel2::clicked, this, timeout = std::get<1>(item)
[this, timeout = std::get<1>(item)] { ] { this->buttonClicked.invoke(std::make_pair(Action::Timeout, timeout)); });
this->buttonClicked.invoke(std::make_pair(Action::Timeout, timeout));
});
} }
} }
}; };
@ -377,21 +374,16 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
addTimeouts("sec", {{"1", 1}}); addTimeouts("sec", {{"1", 1}});
addTimeouts("min", { addTimeouts("min", {
{"1", 1 * 60}, {"1", 1 * 60}, {"5", 5 * 60}, {"10", 10 * 60},
{"5", 5 * 60},
{"10", 10 * 60},
}); });
addTimeouts("hour", { addTimeouts("hour", {
{"1", 1 * 60 * 60}, {"1", 1 * 60 * 60}, {"4", 4 * 60 * 60},
{"4", 4 * 60 * 60},
}); });
addTimeouts("days", { addTimeouts("days", {
{"1", 1 * 60 * 60 * 24}, {"1", 1 * 60 * 60 * 24}, {"3", 3 * 60 * 60 * 24},
{"3", 3 * 60 * 60 * 24},
}); });
addTimeouts("weeks", { addTimeouts("weeks", {
{"1", 1 * 60 * 60 * 24 * 7}, {"1", 1 * 60 * 60 * 24 * 7}, {"2", 2 * 60 * 60 * 24 * 7},
{"2", 2 * 60 * 60 * 24 * 7},
}); });
addButton(Ban, "ban", getApp()->resources->buttons.ban); addButton(Ban, "ban", getApp()->resources->buttons.ban);

View file

@ -1142,7 +1142,7 @@ void ChannelView::handleLinkClick(QMouseEvent *event, const Link &link, MessageL
auto *userPopup = new UserInfoPopup; auto *userPopup = new UserInfoPopup;
userPopup->setData(user, this->channel_); userPopup->setData(user, this->channel_);
userPopup->setAttribute(Qt::WA_DeleteOnClose); userPopup->setActionOnFocusLoss(BaseWindow::Delete);
QPoint offset(int(150 * this->getScale()), int(70 * this->getScale())); QPoint offset(int(150 * this->getScale()), int(70 * this->getScale()));
userPopup->move(QCursor::pos() - offset); userPopup->move(QCursor::pos() - offset);
userPopup->show(); userPopup->show();