From c8aa35d23cea0e067783ef2f43fba916c9174e72 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 4 Aug 2018 15:06:39 +0300 Subject: [PATCH] Don't show PopupMenu for inactive window on macOS. This is a fixup for 06bf67c146. --- .../platform/linux/specific_linux.cpp | 4 ++++ .../platform/linux/specific_linux.h | 2 ++ .../platform/mac/specific_mac_p.mm | 14 +++++++++++++ .../SourceFiles/platform/platform_specific.h | 1 + .../SourceFiles/platform/win/specific_win.cpp | 4 ++++ .../SourceFiles/platform/win/specific_win.h | 2 ++ .../SourceFiles/ui/widgets/popup_menu.cpp | 21 +++++++++++++++---- 7 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 759b9e815..ed79c6d65 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -47,6 +47,10 @@ bool _psRunCommand(const QByteArray &command) { namespace Platform { +bool IsApplicationActive() { + return static_cast(QApplication::instance())->activeWindow() != nullptr; +} + QString CurrentExecutablePath(int argc, char *argv[]) { constexpr auto kMaxPath = 1024; char result[kMaxPath] = { 0 }; diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.h b/Telegram/SourceFiles/platform/linux/specific_linux.h index acbb8553a..613b04a34 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.h +++ b/Telegram/SourceFiles/platform/linux/specific_linux.h @@ -15,6 +15,8 @@ namespace Platform { inline void SetWatchingMediaKeys(bool watching) { } +bool IsApplicationActive(); + inline void StartTranslucentPaint(QPainter &p, QPaintEvent *e) { } diff --git a/Telegram/SourceFiles/platform/mac/specific_mac_p.mm b/Telegram/SourceFiles/platform/mac/specific_mac_p.mm index 569167088..aad5fecf2 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac_p.mm +++ b/Telegram/SourceFiles/platform/mac/specific_mac_p.mm @@ -29,6 +29,8 @@ namespace { constexpr auto kIgnoreActivationTimeoutMs = 500; +base::optional ApplicationIsActive; + } // namespace using Platform::Q2NSString; @@ -85,6 +87,7 @@ using Platform::NS2QString; - (BOOL) applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag; - (void) applicationDidFinishLaunching:(NSNotification *)aNotification; - (void) applicationDidBecomeActive:(NSNotification *)aNotification; +- (void) applicationDidResignActive:(NSNotification *)aNotification; - (void) receiveWakeNote:(NSNotification*)note; - (void) setWatchingMediaKeys:(bool)watching; @@ -126,6 +129,7 @@ ApplicationDelegate *_sharedDelegate = nil; } - (void) applicationDidBecomeActive:(NSNotification *)aNotification { + ApplicationIsActive = true; if (auto messenger = Messenger::InstancePointer()) { if (!_ignoreActivation) { messenger->handleAppActivated(); @@ -138,6 +142,10 @@ ApplicationDelegate *_sharedDelegate = nil; } } +- (void) applicationDidResignActive:(NSNotification *)aNotification { + ApplicationIsActive = false; +} + - (void) receiveWakeNote:(NSNotification*)aNotification { if (auto messenger = Messenger::InstancePointer()) { messenger->checkLocalTime(); @@ -187,6 +195,12 @@ void SetWatchingMediaKeys(bool watching) { } } +bool IsApplicationActive() { + return ApplicationIsActive + ? *ApplicationIsActive + : (static_cast(QApplication::instance())->activeWindow() != nullptr); +} + void InitOnTopPanel(QWidget *panel) { Expects(!panel->windowHandle()); diff --git a/Telegram/SourceFiles/platform/platform_specific.h b/Telegram/SourceFiles/platform/platform_specific.h index bd3b2f9a0..92485c7b6 100644 --- a/Telegram/SourceFiles/platform/platform_specific.h +++ b/Telegram/SourceFiles/platform/platform_specific.h @@ -13,6 +13,7 @@ void start(); void finish(); void SetWatchingMediaKeys(bool watching); +bool IsApplicationActive(); bool TranslucentWindowsSupported(QPoint globalPosition); void StartTranslucentPaint(QPainter &p, QPaintEvent *e); void InitOnTopPanel(QWidget *panel); diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index e25838e62..1370884c0 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -332,6 +332,10 @@ QString SystemCountry() { return QString(); } +bool IsApplicationActive() { + return static_cast(QApplication::instance())->activeWindow() != nullptr; +} + QString CurrentExecutablePath(int argc, char *argv[]) { WCHAR result[MAX_PATH + 1] = { 0 }; auto count = GetModuleFileName(nullptr, result, MAX_PATH + 1); diff --git a/Telegram/SourceFiles/platform/win/specific_win.h b/Telegram/SourceFiles/platform/win/specific_win.h index e0d6b33b1..fdf24fd09 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.h +++ b/Telegram/SourceFiles/platform/win/specific_win.h @@ -14,6 +14,8 @@ namespace Platform { inline void SetWatchingMediaKeys(bool watching) { } +bool IsApplicationActive(); + inline bool TranslucentWindowsSupported(QPoint globalPosition) { return true; } diff --git a/Telegram/SourceFiles/ui/widgets/popup_menu.cpp b/Telegram/SourceFiles/ui/widgets/popup_menu.cpp index 4ea20b648..bbe114bd3 100644 --- a/Telegram/SourceFiles/ui/widgets/popup_menu.cpp +++ b/Telegram/SourceFiles/ui/widgets/popup_menu.cpp @@ -15,6 +15,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" namespace Ui { +namespace { + +bool InactiveMacApplication() { + return (cPlatform() == dbipMac || cPlatform() == dbipMacOld) + && !Platform::IsApplicationActive(); +} + +} // namespace PopupMenu::PopupMenu(QWidget *parent, const style::PopupMenu &st) : RpWidget(parent) @@ -423,11 +431,16 @@ void PopupMenu::popup(const QPoint &p) { } void PopupMenu::showMenu(const QPoint &p, PopupMenu *parent, TriggeredSource source) { - if (cPlatform() == dbipMac || cPlatform() == dbipMacOld) { - if (!parent && !static_cast(QApplication::instance())->activeWindow()) { - crl::on_main(this, [=] { show(); hideFast(); }); - return; + if (!parent && InactiveMacApplication()) { + _hiding = false; + _a_opacity.finish(); + _a_show.finish(); + _cache = QPixmap(); + hide(); + if (_deleteOnHide) { + deleteLater(); } + return; } _parent = parent;