diff --git a/Telegram/SourceFiles/application.cpp b/Telegram/SourceFiles/application.cpp index 149965ce4..3d297fc7e 100644 --- a/Telegram/SourceFiles/application.cpp +++ b/Telegram/SourceFiles/application.cpp @@ -455,6 +455,30 @@ bool Application::nativeEventFilter( return false; } +void Application::activateWindowDelayed(not_null widget) { + if (_delayedActivationsPaused) { + return; + } else if (std::exchange(_windowForDelayedActivation, widget.get())) { + return; + } + crl::on_main(this, [=] { + if (const auto widget = base::take(_windowForDelayedActivation)) { + if (!widget->isHidden()) { + widget->activateWindow(); + } + } + }); +} + +void Application::pauseDelayedWindowActivations() { + _windowForDelayedActivation = nullptr; + _delayedActivationsPaused = true; +} + +void Application::resumeDelayedWindowActivations() { + _delayedActivationsPaused = false; +} + void Application::closeApplication() { if (App::launchState() == App::QuitProcessed) return; App::setLaunchState(App::QuitProcessed); diff --git a/Telegram/SourceFiles/application.h b/Telegram/SourceFiles/application.h index ad849ff2e..c58644571 100644 --- a/Telegram/SourceFiles/application.h +++ b/Telegram/SourceFiles/application.h @@ -29,6 +29,10 @@ public: void postponeCall(FnMut &&callable); bool notify(QObject *receiver, QEvent *e) override; + void activateWindowDelayed(not_null widget); + void pauseDelayedWindowActivations(); + void resumeDelayedWindowActivations(); + ~Application(); signals: @@ -73,6 +77,9 @@ private: std::vector _previousLoopNestingLevels; std::vector _postponedCalls; + QPointer _windowForDelayedActivation; + bool _delayedActivationsPaused = false; + not_null _launcher; std::unique_ptr _messengerInstance; @@ -89,6 +96,16 @@ private: }; +namespace Core { + +inline Application &App() { + Expects(QCoreApplication::instance() != nullptr); + + return *static_cast(QCoreApplication::instance()); +} + +} // namespace Core + namespace Sandbox { QRect availableGeometry(); diff --git a/Telegram/SourceFiles/core/file_utilities.cpp b/Telegram/SourceFiles/core/file_utilities.cpp index 5783dcda3..9df8e7774 100644 --- a/Telegram/SourceFiles/core/file_utilities.cpp +++ b/Telegram/SourceFiles/core/file_utilities.cpp @@ -7,10 +7,18 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "core/file_utilities.h" -#include "mainwindow.h" #include "storage/localstorage.h" #include "platform/platform_file_utilities.h" #include "messenger.h" +#include "application.h" +#include "mainwindow.h" + +void PreventWindowActivation() { + Core::App().pauseDelayedWindowActivations(); + Core::App().postponeCall([] { + Core::App().resumeDelayedWindowActivations(); + }); +} bool filedialogGetSaveFile( QPointer parent, @@ -20,6 +28,7 @@ bool filedialogGetSaveFile( const QString &initialPath) { QStringList files; QByteArray remoteContent; + PreventWindowActivation(); bool result = Platform::FileDialog::Get( parent, files, @@ -112,6 +121,7 @@ namespace File { void OpenEmailLink(const QString &email) { crl::on_main([=] { + PreventWindowActivation(); Platform::File::UnsafeOpenEmailLink(email); }); } @@ -119,6 +129,7 @@ void OpenEmailLink(const QString &email) { void OpenWith(const QString &filepath, QPoint menuPosition) { InvokeQueued(QApplication::instance(), [=] { if (!Platform::File::UnsafeShowOpenWithDropdown(filepath, menuPosition)) { + PreventWindowActivation(); if (!Platform::File::UnsafeShowOpenWith(filepath)) { Platform::File::UnsafeLaunch(filepath); } @@ -128,12 +139,14 @@ void OpenWith(const QString &filepath, QPoint menuPosition) { void Launch(const QString &filepath) { crl::on_main([=] { + PreventWindowActivation(); Platform::File::UnsafeLaunch(filepath); }); } void ShowInFolder(const QString &filepath) { crl::on_main([=] { + PreventWindowActivation(); Platform::File::UnsafeShowInFolder(filepath); }); } @@ -173,6 +186,7 @@ void GetOpenPath( InvokeQueued(QApplication::instance(), [=] { auto files = QStringList(); auto remoteContent = QByteArray(); + PreventWindowActivation(); const auto success = Platform::FileDialog::Get( parent, files, @@ -206,6 +220,7 @@ void GetOpenPaths( InvokeQueued(QApplication::instance(), [=] { auto files = QStringList(); auto remoteContent = QByteArray(); + PreventWindowActivation(); const auto success = Platform::FileDialog::Get( parent, files, @@ -254,6 +269,7 @@ void GetFolder( InvokeQueued(QApplication::instance(), [=] { auto files = QStringList(); auto remoteContent = QByteArray(); + PreventWindowActivation(); const auto success = Platform::FileDialog::Get( parent, files, diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index bd6d5d20d..4a437e8df 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -112,13 +112,10 @@ ApiWrap::RequestMessageDataCallback replyEditMessageDataCallback() { }; } -void ActivateWindowDelayed(not_null controller) { +void ActivateWindow(not_null controller) { const auto window = controller->window(); - const auto weak = make_weak(window.get()); window->activateWindow(); - crl::on_main(window, [=] { - window->activateWindow(); - }); + Core::App().activateWindowDelayed(window); } void InsertEmojiToField(not_null field, EmojiPtr emoji) { @@ -353,11 +350,11 @@ HistoryWidget::HistoryWidget( _attachDragDocument->setDroppedCallback([this](const QMimeData *data) { confirmSendingFiles(data, CompressConfirm::No); - ActivateWindowDelayed(this->controller()); + ActivateWindow(this->controller()); }); _attachDragPhoto->setDroppedCallback([this](const QMimeData *data) { confirmSendingFiles(data, CompressConfirm::Yes); - ActivateWindowDelayed(this->controller()); + ActivateWindow(this->controller()); }); connect(&_updateEditTimeLeftDisplay, SIGNAL(timeout()), this, SLOT(updateField())); @@ -1247,7 +1244,7 @@ void HistoryWidget::onRecordDone( qint32 samples) { if (!canWriteMessage() || result.isEmpty()) return; - ActivateWindowDelayed(controller()); + ActivateWindow(controller()); const auto duration = samples / Media::Player::kDefaultFrequency; auto options = ApiWrap::SendOptions(_history); options.replyTo = replyToId(); @@ -4047,7 +4044,7 @@ bool HistoryWidget::confirmSendingFiles( } })); - ActivateWindowDelayed(controller()); + ActivateWindow(controller()); const auto shown = Ui::show(std::move(box)); shown->setCloseByOutsideClick(false); @@ -4138,7 +4135,7 @@ bool HistoryWidget::confirmSendingFiles( void HistoryWidget::uploadFiles( Storage::PreparedList &&list, SendMediaType type) { - ActivateWindowDelayed(controller()); + ActivateWindow(controller()); uploadFilesAfterConfirmation( std::move(list), diff --git a/Telegram/SourceFiles/ui/widgets/popup_menu.cpp b/Telegram/SourceFiles/ui/widgets/popup_menu.cpp index 2c9adbefa..1ac1b367f 100644 --- a/Telegram/SourceFiles/ui/widgets/popup_menu.cpp +++ b/Telegram/SourceFiles/ui/widgets/popup_menu.cpp @@ -503,12 +503,8 @@ PopupMenu::~PopupMenu() { delete submenu; } if (const auto parent = parentWidget()) { - if (qApp->focusWidget() != nullptr) { - crl::on_main(parent, [=] { - if (!parent->isHidden()) { - parent->activateWindow(); - } - }); + if (Core::App().focusWidget() != nullptr) { + Core::App().activateWindowDelayed(parent); } } if (_destroyedCallback) {