Skip to content

Commit

Permalink
Take control over macOS media keys only when using music player. tele…
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Oct 22, 2016
1 parent 77df38b commit 130c41d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,8 @@ void MainWidget::closeBothPlayers() {
if (Media::Player::exists()) {
Media::Player::instance()->stop();
}

Shortcuts::disableMediaShortcuts();
}

void MainWidget::createPlayer() {
Expand All @@ -1657,6 +1659,8 @@ void MainWidget::createPlayer() {
_playerHeight = _contentScrollAddToY = _player->contentHeight();
updateControlsGeometry();
}

Shortcuts::enableMediaShortcuts();
}

void MainWidget::playerHeightUpdated() {
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/pspecific.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Platform {
void start();
void finish();

void SetWatchingMediaKeys(bool watching);

namespace ThirdParty {

void start();
Expand Down
3 changes: 3 additions & 0 deletions Telegram/SourceFiles/pspecific_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ void finish() {
_psEventFilter = nullptr;
}

void SetWatchingMediaKeys(bool watching) {
}

namespace ThirdParty {

void start() {
Expand Down
43 changes: 39 additions & 4 deletions Telegram/SourceFiles/pspecific_mac_p.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ - (id)debugQuickLookObject {
@interface ApplicationDelegate : NSObject<NSApplicationDelegate> {

SPMediaKeyTap *keyTap;
BOOL watchingMediaKeys;

}

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag;
- (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
Expand All @@ -109,15 +112,14 @@ - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisible
}

- (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
}

Expand All @@ -129,6 +131,25 @@ - (void)receiveWakeNote:(NSNotification*)aNotification {
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);
Expand Down Expand Up @@ -193,6 +214,16 @@ - (void) screenIsUnlocked:(NSNotification *)aNotification {

@end

namespace Platform {

void SetWatchingMediaKeys(bool watching) {
if (_sharedDelegate) {
[_sharedDelegate setWatchingMediaKeys:(watching ? YES : NO)];
}
}

} // namespace Platform

PsMacWindowPrivate::PsMacWindowPrivate() : data(new PsMacWindowData(this)) {
@autoreleasepool {

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Telegram/SourceFiles/pspecific_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,9 @@ void finish() {
EventFilter::destroy();
}

void SetWatchingMediaKeys(bool watching) {
}

namespace ThirdParty {

void start() {
Expand Down
3 changes: 3 additions & 0 deletions Telegram/SourceFiles/shortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -543,13 +544,15 @@ void enableMediaShortcuts() {
for_const (auto shortcut, DataPtr->mediaShortcuts) {
shortcut->setEnabled(true);
}
Platform::SetWatchingMediaKeys(true);
}

void disableMediaShortcuts() {
if (!DataPtr) return;
for_const (auto shortcut, DataPtr->mediaShortcuts) {
shortcut->setEnabled(false);
}
Platform::SetWatchingMediaKeys(false);
}

void finish() {
Expand Down
16 changes: 8 additions & 8 deletions Telegram/SourceFiles/ui/flatinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class InputArea : public TWidget {
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;
Expand Down Expand Up @@ -267,7 +267,7 @@ public slots:
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;
Expand Down Expand Up @@ -348,8 +348,8 @@ class InputField : public TWidget {
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;
Expand Down Expand Up @@ -436,9 +436,7 @@ public slots:
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;
Expand All @@ -448,6 +446,8 @@ public slots:
void paintEvent(QPaintEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;

QMimeData *createMimeDataFromSelection() const override;

private:
InputField *f() const {
return static_cast<InputField*>(parentWidget());
Expand Down

0 comments on commit 130c41d

Please sign in to comment.