Take control over macOS media keys only when using music player. #2549
This commit is contained in:
		
							parent
							
								
									77df38b4fd
								
							
						
					
					
						commit
						130c41d711
					
				
					 7 changed files with 62 additions and 12 deletions
				
			
		| 
						 | 
				
			
			@ -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() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,6 +39,8 @@ namespace Platform {
 | 
			
		|||
void start();
 | 
			
		||||
void finish();
 | 
			
		||||
 | 
			
		||||
void SetWatchingMediaKeys(bool watching);
 | 
			
		||||
 | 
			
		||||
namespace ThirdParty {
 | 
			
		||||
 | 
			
		||||
void start();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -406,6 +406,9 @@ void finish() {
 | 
			
		|||
	_psEventFilter = nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SetWatchingMediaKeys(bool watching) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
namespace ThirdParty {
 | 
			
		||||
 | 
			
		||||
void start() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -87,6 +87,7 @@ bool handleMediaKeyEvent(NSEvent *e);
 | 
			
		|||
@interface ApplicationDelegate : NSObject<NSApplicationDelegate> {
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -729,6 +729,9 @@ void finish() {
 | 
			
		|||
	EventFilter::destroy();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SetWatchingMediaKeys(bool watching) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
namespace ThirdParty {
 | 
			
		||||
 | 
			
		||||
void start() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<InputField*>(parentWidget());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue