From 165511fb142e6f2d601b1173cb9b5c5aa7e5fe9e Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 11 Sep 2018 10:46:07 +0300 Subject: [PATCH] Add warning for unsaved Bio section closing. --- Telegram/Resources/langs/lang.strings | 1 + Telegram/SourceFiles/info/info_controller.cpp | 4 +++ Telegram/SourceFiles/info/info_controller.h | 1 + .../SourceFiles/info/info_wrap_widget.cpp | 27 ++++++++++++++++--- Telegram/SourceFiles/info/info_wrap_widget.h | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 967f39d87..0f638e53b 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -349,6 +349,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_settings_username_label" = "Username"; "lng_settings_phone_label" = "Phone number"; "lng_settings_username_add" = "Add username"; +"lng_settings_close_sure" = "Are you sure you want to close this page? You didn't save your changes."; "lng_backgrounds_header" = "Choose your new chat background"; "lng_theme_sure_keep" = "Keep this theme?"; diff --git a/Telegram/SourceFiles/info/info_controller.cpp b/Telegram/SourceFiles/info/info_controller.cpp index ee2d134ea..c3a1f9e44 100644 --- a/Telegram/SourceFiles/info/info_controller.cpp +++ b/Telegram/SourceFiles/info/info_controller.cpp @@ -277,6 +277,10 @@ rpl::producer Controller::canSaveChanges() const { return _canSaveChanges.value(); } +bool Controller::canSaveChangesNow() const { + return _canSaveChanges.current(); +} + Controller::~Controller() = default; } // namespace Info diff --git a/Telegram/SourceFiles/info/info_controller.h b/Telegram/SourceFiles/info/info_controller.h index fc8b217a0..acab65a8a 100644 --- a/Telegram/SourceFiles/info/info_controller.h +++ b/Telegram/SourceFiles/info/info_controller.h @@ -190,6 +190,7 @@ public: void setCanSaveChanges(rpl::producer can); rpl::producer canSaveChanges() const; + bool canSaveChangesNow() const; void saveSearchState(not_null memento); diff --git a/Telegram/SourceFiles/info/info_wrap_widget.cpp b/Telegram/SourceFiles/info/info_wrap_widget.cpp index 4defacf4b..edd1b1d25 100644 --- a/Telegram/SourceFiles/info/info_wrap_widget.cpp +++ b/Telegram/SourceFiles/info/info_wrap_widget.cpp @@ -25,6 +25,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_slide_animation.h" #include "window/window_peer_menu.h" #include "boxes/peer_list_box.h" +#include "boxes/confirm_box.h" #include "auth_session.h" #include "data/data_session.h" #include "mainwidget.h" @@ -350,8 +351,8 @@ void WrapWidget::createTopBar() { if (wrapValue == Wrap::Narrow || hasStackHistory()) { _topBar->enableBackButton(); _topBar->backRequest( - ) | rpl::start_with_next([this] { - _controller->showBackFromStack(); + ) | rpl::start_with_next([=] { + checkBeforeClose([=] { _controller->showBackFromStack(); }); }, _topBar->lifetime()); } else if (wrapValue == Wrap::Side) { auto close = _topBar->addButton( @@ -368,7 +369,9 @@ void WrapWidget::createTopBar() { _topBar, st::infoLayerTopBarClose)); close->addClickHandler([this] { - _controller->parentController()->hideSpecialLayer(); + checkBeforeClose([=] { + _controller->parentController()->hideSpecialLayer(); + }); }); } else if (requireTopBarSearch()) { auto search = _controller->searchFieldController(); @@ -398,6 +401,22 @@ void WrapWidget::createTopBar() { _topBar->show(); } +void WrapWidget::checkBeforeClose(Fn close) { + const auto confirmed = [=] { + const auto copy = close; + Ui::hideLayer(); + copy(); + }; + if (_controller->canSaveChangesNow()) { + Ui::show(Box( + lang(lng_settings_close_sure), + lang(lng_close), + confirmed)); + } else { + confirmed(); + } +} + void WrapWidget::addTopBarMenuButton() { Expects(_topBar != nullptr); @@ -950,7 +969,7 @@ void WrapWidget::resizeEvent(QResizeEvent *e) { void WrapWidget::keyPressEvent(QKeyEvent *e) { if (e->key() == Qt::Key_Escape) { if (hasStackHistory() || wrap() != Wrap::Layer) { - _controller->showBackFromStack(); + checkBeforeClose([=] { _controller->showBackFromStack(); }); return; } } diff --git a/Telegram/SourceFiles/info/info_wrap_widget.h b/Telegram/SourceFiles/info/info_wrap_widget.h index b4a8b0074..6a84fd9fc 100644 --- a/Telegram/SourceFiles/info/info_wrap_widget.h +++ b/Telegram/SourceFiles/info/info_wrap_widget.h @@ -150,6 +150,7 @@ private: void injectActiveFeedProfile(not_null feed); void injectActiveProfileMemento( std::unique_ptr memento); + void checkBeforeClose(Fn close); void restoreHistoryStack( std::vector> stack); bool hasStackHistory() const {