From 130c41d711f09c49ceabc4db1baa4bdab1d1a818 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 22 Oct 2016 17:35:37 +0300 Subject: [PATCH] Take control over macOS media keys only when using music player. #2549 --- Telegram/SourceFiles/mainwidget.cpp | 4 +++ Telegram/SourceFiles/pspecific.h | 2 ++ Telegram/SourceFiles/pspecific_linux.cpp | 3 ++ Telegram/SourceFiles/pspecific_mac_p.mm | 43 +++++++++++++++++++++--- Telegram/SourceFiles/pspecific_win.cpp | 3 ++ Telegram/SourceFiles/shortcuts.cpp | 3 ++ Telegram/SourceFiles/ui/flatinput.h | 16 ++++----- 7 files changed, 62 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index a8fc33e8e..6ee85f510 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1640,6 +1640,8 @@ void MainWidget::closeBothPlayers() { if (Media::Player::exists()) { Media::Player::instance()->stop(); } + + Shortcuts::disableMediaShortcuts(); } void MainWidget::createPlayer() { @@ -1657,6 +1659,8 @@ void MainWidget::createPlayer() { _playerHeight = _contentScrollAddToY = _player->contentHeight(); updateControlsGeometry(); } + + Shortcuts::enableMediaShortcuts(); } void MainWidget::playerHeightUpdated() { diff --git a/Telegram/SourceFiles/pspecific.h b/Telegram/SourceFiles/pspecific.h index cc0345c09..fb3dd1697 100644 --- a/Telegram/SourceFiles/pspecific.h +++ b/Telegram/SourceFiles/pspecific.h @@ -39,6 +39,8 @@ namespace Platform { void start(); void finish(); +void SetWatchingMediaKeys(bool watching); + namespace ThirdParty { void start(); diff --git a/Telegram/SourceFiles/pspecific_linux.cpp b/Telegram/SourceFiles/pspecific_linux.cpp index 09543850f..488160a87 100644 --- a/Telegram/SourceFiles/pspecific_linux.cpp +++ b/Telegram/SourceFiles/pspecific_linux.cpp @@ -406,6 +406,9 @@ void finish() { _psEventFilter = nullptr; } +void SetWatchingMediaKeys(bool watching) { +} + namespace ThirdParty { void start() { diff --git a/Telegram/SourceFiles/pspecific_mac_p.mm b/Telegram/SourceFiles/pspecific_mac_p.mm index 20b4d3908..fb69469b1 100644 --- a/Telegram/SourceFiles/pspecific_mac_p.mm +++ b/Telegram/SourceFiles/pspecific_mac_p.mm @@ -87,6 +87,7 @@ bool handleMediaKeyEvent(NSEvent *e); @interface ApplicationDelegate : NSObject { SPMediaKeyTap *keyTap; +BOOL watchingMediaKeys; } @@ -94,6 +95,8 @@ SPMediaKeyTap *keyTap; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification; - (void)applicationDidBecomeActive:(NSNotification *)aNotification; - (void)receiveWakeNote:(NSNotification*)note; +- (void)setWatchingMediaKeys:(BOOL)watching; +- (BOOL)isWatchingMediaKeys; - (void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event; @end @@ -109,15 +112,14 @@ ApplicationDelegate *_sharedDelegate = nil; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { + keyTap = nullptr; + watchingMediaKeys = false; #ifndef OS_MAC_STORE - keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self]; if ([SPMediaKeyTap usesGlobalMediaKeyTap]) { - [keyTap startWatchingMediaKeys]; + keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self]; } else { LOG(("Media key monitoring disabled")); } -#else // !OS_MAC_STORE - keyTap = nullptr; #endif // else for !OS_MAC_STORE } @@ -129,6 +131,25 @@ ApplicationDelegate *_sharedDelegate = nil; if (App::app()) App::app()->checkLocalTime(); } +- (void)setWatchingMediaKeys:(BOOL)watching { + if (watchingMediaKeys != watching) { + watchingMediaKeys = watching; + if (keyTap) { +#ifndef OS_MAC_STORE + if (watchingMediaKeys) { + [keyTap startWatchingMediaKeys]; + } else { + [keyTap stopWatchingMediaKeys]; + } +#endif // else for !OS_MAC_STORE + } + } +} + +- (BOOL)isWatchingMediaKeys { + return watchingMediaKeys; +} + - (void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)e { if (e && [e type] == NSSystemDefined && [e subtype] == SPSystemDefinedEventMediaKeys) { handleMediaKeyEvent(e); @@ -193,6 +214,16 @@ public: @end +namespace Platform { + +void SetWatchingMediaKeys(bool watching) { + if (_sharedDelegate) { + [_sharedDelegate setWatchingMediaKeys:(watching ? YES : NO)]; + } +} + +} // namespace Platform + PsMacWindowPrivate::PsMacWindowPrivate() : data(new PsMacWindowData(this)) { @autoreleasepool { @@ -264,6 +295,10 @@ bool handleMediaKeyEvent(NSEvent *e) { int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA; int keyRepeat = (keyFlags & 0x1); + if (!_sharedDelegate || ![_sharedDelegate isWatchingMediaKeys]) { + return false; + } + switch (keyCode) { case NX_KEYTYPE_PLAY: if (keyState == 0) { // Play pressed and released diff --git a/Telegram/SourceFiles/pspecific_win.cpp b/Telegram/SourceFiles/pspecific_win.cpp index 781a4d6d4..78fe71759 100644 --- a/Telegram/SourceFiles/pspecific_win.cpp +++ b/Telegram/SourceFiles/pspecific_win.cpp @@ -729,6 +729,9 @@ void finish() { EventFilter::destroy(); } +void SetWatchingMediaKeys(bool watching) { +} + namespace ThirdParty { void start() { diff --git a/Telegram/SourceFiles/shortcuts.cpp b/Telegram/SourceFiles/shortcuts.cpp index 322873a87..336f9cd63 100644 --- a/Telegram/SourceFiles/shortcuts.cpp +++ b/Telegram/SourceFiles/shortcuts.cpp @@ -25,6 +25,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org #include "passcodewidget.h" #include "mainwidget.h" #include "media/player/media_player_instance.h" +#include "pspecific.h" namespace ShortcutCommands { @@ -543,6 +544,7 @@ void enableMediaShortcuts() { for_const (auto shortcut, DataPtr->mediaShortcuts) { shortcut->setEnabled(true); } + Platform::SetWatchingMediaKeys(true); } void disableMediaShortcuts() { @@ -550,6 +552,7 @@ void disableMediaShortcuts() { for_const (auto shortcut, DataPtr->mediaShortcuts) { shortcut->setEnabled(false); } + Platform::SetWatchingMediaKeys(false); } void finish() { diff --git a/Telegram/SourceFiles/ui/flatinput.h b/Telegram/SourceFiles/ui/flatinput.h index cd802fa8a..2535a019d 100644 --- a/Telegram/SourceFiles/ui/flatinput.h +++ b/Telegram/SourceFiles/ui/flatinput.h @@ -186,8 +186,8 @@ public: void step_placeholderShift(float64 ms, bool timer); void step_border(float64 ms, bool timer); - QSize sizeHint() const; - QSize minimumSizeHint() const; + QSize sizeHint() const override; + QSize minimumSizeHint() const override; QString getText(int32 start = 0, int32 end = -1) const; bool hasText() const; @@ -267,7 +267,7 @@ private: public: InputAreaInner(InputArea *parent); - QVariant loadResource(int type, const QUrl &name); + QVariant loadResource(int type, const QUrl &name) override; protected: bool viewportEvent(QEvent *e) override; @@ -348,8 +348,8 @@ public: void step_placeholderShift(float64 ms, bool timer); void step_border(float64 ms, bool timer); - QSize sizeHint() const; - QSize minimumSizeHint() const; + QSize sizeHint() const override; + QSize minimumSizeHint() const override; QString getText(int32 start = 0, int32 end = -1) const; bool hasText() const; @@ -436,9 +436,7 @@ private: public: InputFieldInner(InputField *parent); - QMimeData *createMimeDataFromSelection() const; - - QVariant loadResource(int type, const QUrl &name); + QVariant loadResource(int type, const QUrl &name) override; protected: bool viewportEvent(QEvent *e) override; @@ -448,6 +446,8 @@ private: void paintEvent(QPaintEvent *e) override; void contextMenuEvent(QContextMenuEvent *e) override; + QMimeData *createMimeDataFromSelection() const override; + private: InputField *f() const { return static_cast(parentWidget());