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

View file

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

View file

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

View file

@ -21,8 +21,7 @@
namespace chatterino {
UserInfoPopup::UserInfoPopup()
: BaseWindow(nullptr, BaseWindow::Flags(BaseWindow::Frameless | BaseWindow::DeleteOnFocusOut |
BaseWindow::FramelessDraggable))
: BaseWindow(nullptr, BaseWindow::Flags(BaseWindow::Frameless | BaseWindow::FramelessDraggable))
, hack_(new bool)
{
this->setStayInScreenRect(true);
@ -364,11 +363,9 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
}
a->setBorderColor(color1);
QObject::connect(
a.getElement(), &RippleEffectLabel2::clicked,
[this, timeout = std::get<1>(item)] {
this->buttonClicked.invoke(std::make_pair(Action::Timeout, timeout));
});
QObject::connect(a.getElement(), &RippleEffectLabel2::clicked, [
this, timeout = std::get<1>(item)
] { this->buttonClicked.invoke(std::make_pair(Action::Timeout, timeout)); });
}
}
};
@ -377,21 +374,16 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
addTimeouts("sec", {{"1", 1}});
addTimeouts("min", {
{"1", 1 * 60},
{"5", 5 * 60},
{"10", 10 * 60},
{"1", 1 * 60}, {"5", 5 * 60}, {"10", 10 * 60},
});
addTimeouts("hour", {
{"1", 1 * 60 * 60},
{"4", 4 * 60 * 60},
{"1", 1 * 60 * 60}, {"4", 4 * 60 * 60},
});
addTimeouts("days", {
{"1", 1 * 60 * 60 * 24},
{"3", 3 * 60 * 60 * 24},
{"1", 1 * 60 * 60 * 24}, {"3", 3 * 60 * 60 * 24},
});
addTimeouts("weeks", {
{"1", 1 * 60 * 60 * 24 * 7},
{"2", 2 * 60 * 60 * 24 * 7},
{"1", 1 * 60 * 60 * 24 * 7}, {"2", 2 * 60 * 60 * 24 * 7},
});
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;
userPopup->setData(user, this->channel_);
userPopup->setAttribute(Qt::WA_DeleteOnClose);
userPopup->setActionOnFocusLoss(BaseWindow::Delete);
QPoint offset(int(150 * this->getScale()), int(70 * this->getScale()));
userPopup->move(QCursor::pos() - offset);
userPopup->show();