From 16f3ca87f57c12eab5afbaa6f60f4d2cab2fa3f0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 6 Jun 2018 14:51:27 +0300 Subject: [PATCH] Allow boxes that don't hide by escape / click. --- Telegram/SourceFiles/boxes/abstract_box.cpp | 20 ++++++++++++++++++-- Telegram/SourceFiles/boxes/abstract_box.h | 13 +++++++++++++ Telegram/SourceFiles/messenger.cpp | 10 +++++++--- Telegram/SourceFiles/window/layer_widget.cpp | 5 +++++ Telegram/SourceFiles/window/layer_widget.h | 3 +++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Telegram/SourceFiles/boxes/abstract_box.cpp b/Telegram/SourceFiles/boxes/abstract_box.cpp index 52c1a1300..5e141167c 100644 --- a/Telegram/SourceFiles/boxes/abstract_box.cpp +++ b/Telegram/SourceFiles/boxes/abstract_box.cpp @@ -32,8 +32,8 @@ QPointer BoxContent::addLeftButton( Fn textFactory, Fn clickCallback) { return getDelegate()->addLeftButton( - std::move(textFactory), - std::move(clickCallback), + std::move(textFactory), + std::move(clickCallback), st::defaultBoxButton); } @@ -190,6 +190,14 @@ void BoxContent::resizeEvent(QResizeEvent *e) { } } +void BoxContent::keyPressEvent(QKeyEvent *e) { + if (e->key() == Qt::Key_Escape && !_closeByEscape) { + e->accept(); + } else { + RpWidget::keyPressEvent(e); + } +} + void BoxContent::updateScrollAreaGeometry() { auto newScrollHeight = height() - _innerTopSkip - _innerBottomSkip; auto changed = (_scroll->height() != newScrollHeight); @@ -316,6 +324,14 @@ void AbstractBox::setAdditionalTitle(Fn additionalFactory) { refreshAdditionalTitle(); } +void AbstractBox::setCloseByOutsideClick(bool close) { + _closeByOutsideClick = close; +} + +bool AbstractBox::closeByOutsideClick() const { + return _closeByOutsideClick; +} + void AbstractBox::refreshAdditionalTitle() { _additionalTitle = _additionalTitleFactory ? _additionalTitleFactory() : QString(); update(); diff --git a/Telegram/SourceFiles/boxes/abstract_box.h b/Telegram/SourceFiles/boxes/abstract_box.h index 2c61e172d..8bc9d5d35 100644 --- a/Telegram/SourceFiles/boxes/abstract_box.h +++ b/Telegram/SourceFiles/boxes/abstract_box.h @@ -30,6 +30,7 @@ public: virtual void setLayerType(bool layerType) = 0; virtual void setTitle(Fn titleFactory) = 0; virtual void setAdditionalTitle(Fn additionalFactory) = 0; + virtual void setCloseByOutsideClick(bool close) = 0; virtual void clearButtons() = 0; virtual QPointer addButton(Fn textFactory, Fn clickCallback, const style::RoundButton &st) = 0; @@ -85,6 +86,12 @@ public: void setAdditionalTitle(Fn additional) { getDelegate()->setAdditionalTitle(std::move(additional)); } + void setCloseByEscape(bool close) { + _closeByEscape = close; + } + void setCloseByOutsideClick(bool close) { + getDelegate()->setCloseByOutsideClick(close); + } void scrollToWidget(not_null widget); @@ -178,6 +185,7 @@ protected: void resizeEvent(QResizeEvent *e) override; void paintEvent(QPaintEvent *e) override; + void keyPressEvent(QKeyEvent *e) override; not_null getDelegate() const { return _delegate; @@ -203,6 +211,7 @@ private: bool _preparing = false; bool _noContentMargin = false; + bool _closeByEscape = true; int _innerTopSkip = 0; int _innerBottomSkip = 0; object_ptr _scroll = { nullptr }; @@ -256,6 +265,9 @@ public: closeLayer(); } + void setCloseByOutsideClick(bool close) override; + bool closeByOutsideClick() const override; + protected: void keyPressEvent(QKeyEvent *e) override; void resizeEvent(QResizeEvent *e) override; @@ -298,6 +310,7 @@ private: int _titleLeft = 0; int _titleTop = 0; bool _layerType = false; + bool _closeByOutsideClick = true; std::vector> _buttons; object_ptr _leftButton = { nullptr }; diff --git a/Telegram/SourceFiles/messenger.cpp b/Telegram/SourceFiles/messenger.cpp index 0a9476a0c..06e577178 100644 --- a/Telegram/SourceFiles/messenger.cpp +++ b/Telegram/SourceFiles/messenger.cpp @@ -659,10 +659,14 @@ void Messenger::forceLogOut(const TextWithEntities &explanation) { const auto box = Ui::show(Box( explanation, lang(lng_passcode_logout))); + box->setCloseByEscape(false); + box->setCloseByOutsideClick(false); connect(box, &QObject::destroyed, [=] { - InvokeQueued(this, [=] { - resetAuthorizationKeys(); - loggedOut(); + crl::on_main(this, [=] { + if (AuthSession::Exists()) { + resetAuthorizationKeys(); + loggedOut(); + } }); }); } diff --git a/Telegram/SourceFiles/window/layer_widget.cpp b/Telegram/SourceFiles/window/layer_widget.cpp index d28a24908..f08d4ea46 100644 --- a/Telegram/SourceFiles/window/layer_widget.cpp +++ b/Telegram/SourceFiles/window/layer_widget.cpp @@ -357,6 +357,11 @@ void LayerStackWidget::keyPressEvent(QKeyEvent *e) { void LayerStackWidget::mousePressEvent(QMouseEvent *e) { if (_hideByBackgroundClick) { + if (const auto layer = currentLayer()) { + if (!layer->closeByOutsideClick()) { + return; + } + } hideCurrent(anim::type::normal); } } diff --git a/Telegram/SourceFiles/window/layer_widget.h b/Telegram/SourceFiles/window/layer_widget.h index b54aa1fcf..f55efef78 100644 --- a/Telegram/SourceFiles/window/layer_widget.h +++ b/Telegram/SourceFiles/window/layer_widget.h @@ -49,6 +49,9 @@ public: const SectionShow ¶ms) { return false; } + virtual bool closeByOutsideClick() const { + return true; + } protected: void closeLayer() {