Take control over macOS media keys only when using music player. #2549

This commit is contained in:
John Preston 2016-10-22 17:35:37 +03:00
parent 77df38b4fd
commit 130c41d711
7 changed files with 62 additions and 12 deletions

View File

@ -1640,6 +1640,8 @@ void MainWidget::closeBothPlayers() {
if (Media::Player::exists()) { if (Media::Player::exists()) {
Media::Player::instance()->stop(); Media::Player::instance()->stop();
} }
Shortcuts::disableMediaShortcuts();
} }
void MainWidget::createPlayer() { void MainWidget::createPlayer() {
@ -1657,6 +1659,8 @@ void MainWidget::createPlayer() {
_playerHeight = _contentScrollAddToY = _player->contentHeight(); _playerHeight = _contentScrollAddToY = _player->contentHeight();
updateControlsGeometry(); updateControlsGeometry();
} }
Shortcuts::enableMediaShortcuts();
} }
void MainWidget::playerHeightUpdated() { void MainWidget::playerHeightUpdated() {

View File

@ -39,6 +39,8 @@ namespace Platform {
void start(); void start();
void finish(); void finish();
void SetWatchingMediaKeys(bool watching);
namespace ThirdParty { namespace ThirdParty {
void start(); void start();

View File

@ -406,6 +406,9 @@ void finish() {
_psEventFilter = nullptr; _psEventFilter = nullptr;
} }
void SetWatchingMediaKeys(bool watching) {
}
namespace ThirdParty { namespace ThirdParty {
void start() { void start() {

View File

@ -87,6 +87,7 @@ bool handleMediaKeyEvent(NSEvent *e);
@interface ApplicationDelegate : NSObject<NSApplicationDelegate> { @interface ApplicationDelegate : NSObject<NSApplicationDelegate> {
SPMediaKeyTap *keyTap; SPMediaKeyTap *keyTap;
BOOL watchingMediaKeys;
} }
@ -94,6 +95,8 @@ SPMediaKeyTap *keyTap;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
- (void)applicationDidBecomeActive:(NSNotification *)aNotification; - (void)applicationDidBecomeActive:(NSNotification *)aNotification;
- (void)receiveWakeNote:(NSNotification*)note; - (void)receiveWakeNote:(NSNotification*)note;
- (void)setWatchingMediaKeys:(BOOL)watching;
- (BOOL)isWatchingMediaKeys;
- (void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event; - (void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event;
@end @end
@ -109,15 +112,14 @@ ApplicationDelegate *_sharedDelegate = nil;
} }
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
keyTap = nullptr;
watchingMediaKeys = false;
#ifndef OS_MAC_STORE #ifndef OS_MAC_STORE
keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self];
if ([SPMediaKeyTap usesGlobalMediaKeyTap]) { if ([SPMediaKeyTap usesGlobalMediaKeyTap]) {
[keyTap startWatchingMediaKeys]; keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self];
} else { } else {
LOG(("Media key monitoring disabled")); LOG(("Media key monitoring disabled"));
} }
#else // !OS_MAC_STORE
keyTap = nullptr;
#endif // else for !OS_MAC_STORE #endif // else for !OS_MAC_STORE
} }
@ -129,6 +131,25 @@ ApplicationDelegate *_sharedDelegate = nil;
if (App::app()) App::app()->checkLocalTime(); 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 { - (void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)e {
if (e && [e type] == NSSystemDefined && [e subtype] == SPSystemDefinedEventMediaKeys) { if (e && [e type] == NSSystemDefined && [e subtype] == SPSystemDefinedEventMediaKeys) {
handleMediaKeyEvent(e); handleMediaKeyEvent(e);
@ -193,6 +214,16 @@ public:
@end @end
namespace Platform {
void SetWatchingMediaKeys(bool watching) {
if (_sharedDelegate) {
[_sharedDelegate setWatchingMediaKeys:(watching ? YES : NO)];
}
}
} // namespace Platform
PsMacWindowPrivate::PsMacWindowPrivate() : data(new PsMacWindowData(this)) { PsMacWindowPrivate::PsMacWindowPrivate() : data(new PsMacWindowData(this)) {
@autoreleasepool { @autoreleasepool {
@ -264,6 +295,10 @@ bool handleMediaKeyEvent(NSEvent *e) {
int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA; int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
int keyRepeat = (keyFlags & 0x1); int keyRepeat = (keyFlags & 0x1);
if (!_sharedDelegate || ![_sharedDelegate isWatchingMediaKeys]) {
return false;
}
switch (keyCode) { switch (keyCode) {
case NX_KEYTYPE_PLAY: case NX_KEYTYPE_PLAY:
if (keyState == 0) { // Play pressed and released if (keyState == 0) { // Play pressed and released

View File

@ -729,6 +729,9 @@ void finish() {
EventFilter::destroy(); EventFilter::destroy();
} }
void SetWatchingMediaKeys(bool watching) {
}
namespace ThirdParty { namespace ThirdParty {
void start() { void start() {

View File

@ -25,6 +25,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "passcodewidget.h" #include "passcodewidget.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "media/player/media_player_instance.h" #include "media/player/media_player_instance.h"
#include "pspecific.h"
namespace ShortcutCommands { namespace ShortcutCommands {
@ -543,6 +544,7 @@ void enableMediaShortcuts() {
for_const (auto shortcut, DataPtr->mediaShortcuts) { for_const (auto shortcut, DataPtr->mediaShortcuts) {
shortcut->setEnabled(true); shortcut->setEnabled(true);
} }
Platform::SetWatchingMediaKeys(true);
} }
void disableMediaShortcuts() { void disableMediaShortcuts() {
@ -550,6 +552,7 @@ void disableMediaShortcuts() {
for_const (auto shortcut, DataPtr->mediaShortcuts) { for_const (auto shortcut, DataPtr->mediaShortcuts) {
shortcut->setEnabled(false); shortcut->setEnabled(false);
} }
Platform::SetWatchingMediaKeys(false);
} }
void finish() { void finish() {

View File

@ -186,8 +186,8 @@ public:
void step_placeholderShift(float64 ms, bool timer); void step_placeholderShift(float64 ms, bool timer);
void step_border(float64 ms, bool timer); void step_border(float64 ms, bool timer);
QSize sizeHint() const; QSize sizeHint() const override;
QSize minimumSizeHint() const; QSize minimumSizeHint() const override;
QString getText(int32 start = 0, int32 end = -1) const; QString getText(int32 start = 0, int32 end = -1) const;
bool hasText() const; bool hasText() const;
@ -267,7 +267,7 @@ private:
public: public:
InputAreaInner(InputArea *parent); InputAreaInner(InputArea *parent);
QVariant loadResource(int type, const QUrl &name); QVariant loadResource(int type, const QUrl &name) override;
protected: protected:
bool viewportEvent(QEvent *e) override; bool viewportEvent(QEvent *e) override;
@ -348,8 +348,8 @@ public:
void step_placeholderShift(float64 ms, bool timer); void step_placeholderShift(float64 ms, bool timer);
void step_border(float64 ms, bool timer); void step_border(float64 ms, bool timer);
QSize sizeHint() const; QSize sizeHint() const override;
QSize minimumSizeHint() const; QSize minimumSizeHint() const override;
QString getText(int32 start = 0, int32 end = -1) const; QString getText(int32 start = 0, int32 end = -1) const;
bool hasText() const; bool hasText() const;
@ -436,9 +436,7 @@ private:
public: public:
InputFieldInner(InputField *parent); InputFieldInner(InputField *parent);
QMimeData *createMimeDataFromSelection() const; QVariant loadResource(int type, const QUrl &name) override;
QVariant loadResource(int type, const QUrl &name);
protected: protected:
bool viewportEvent(QEvent *e) override; bool viewportEvent(QEvent *e) override;
@ -448,6 +446,8 @@ private:
void paintEvent(QPaintEvent *e) override; void paintEvent(QPaintEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override; void contextMenuEvent(QContextMenuEvent *e) override;
QMimeData *createMimeDataFromSelection() const override;
private: private:
InputField *f() const { InputField *f() const {
return static_cast<InputField*>(parentWidget()); return static_cast<InputField*>(parentWidget());