From 8e2943fe11414617b9f400f36cd3336d89de4ed9 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 13 Jul 2015 14:14:15 +0300 Subject: [PATCH 1/2] filling optimized --- Telegram/SourceFiles/gui/flattextarea.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Telegram/SourceFiles/gui/flattextarea.cpp b/Telegram/SourceFiles/gui/flattextarea.cpp index 730c72bf10..1edd4a0d28 100644 --- a/Telegram/SourceFiles/gui/flattextarea.cpp +++ b/Telegram/SourceFiles/gui/flattextarea.cpp @@ -128,7 +128,8 @@ int32 FlatTextarea::fakeMargin() const { void FlatTextarea::paintEvent(QPaintEvent *e) { QPainter p(viewport()); - p.fillRect(rect(), _st.bgColor->b); + QRect r(rect().intersected(e->rect())); + p.fillRect(r, _st.bgColor->b); bool phDraw = _phVisible; if (animating()) { p.setOpacity(a_phAlpha.current()); @@ -136,7 +137,7 @@ void FlatTextarea::paintEvent(QPaintEvent *e) { } if (phDraw) { p.save(); - p.setClipRect(rect()); + p.setClipRect(r); QRect phRect(_st.textMrg.left() - _fakeMargin + _st.phPos.x() + a_phLeft.current(), _st.textMrg.top() - _fakeMargin + _st.phPos.y(), width() - _st.textMrg.left() - _st.textMrg.right(), height() - _st.textMrg.top() - _st.textMrg.bottom()); p.setFont(_st.font->f); p.setPen(a_phColor.current()); From b352d38acf4cd07939ea7b1419b7d659e325a3cc Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 13 Jul 2015 20:55:59 +0300 Subject: [PATCH 2/2] multimedia keys handle added --- Telegram/SourceFiles/application.cpp | 46 +++++++----- Telegram/SourceFiles/playerwidget.cpp | 93 +++++++++++++++++++------ Telegram/SourceFiles/playerwidget.h | 7 ++ Telegram/SourceFiles/pspecific_mac.cpp | 6 +- Telegram/SourceFiles/pspecific_mac.h | 4 +- Telegram/SourceFiles/pspecific_mac_p.h | 4 +- Telegram/SourceFiles/pspecific_mac_p.mm | 38 ++++++++++ 7 files changed, 158 insertions(+), 40 deletions(-) diff --git a/Telegram/SourceFiles/application.cpp b/Telegram/SourceFiles/application.cpp index 41f744844b..1d75d78245 100644 --- a/Telegram/SourceFiles/application.cpp +++ b/Telegram/SourceFiles/application.cpp @@ -48,28 +48,43 @@ namespace { } } - class EventFilterForMac : public QObject { + class EventFilterForKeys : public QObject { public: - EventFilterForMac(QObject *parent) : QObject(parent) { + EventFilterForKeys(QObject *parent) : QObject(parent) { } bool eventFilter(QObject *o, QEvent *e) { if (e->type() == QEvent::KeyPress) { QKeyEvent *ev = static_cast(e); - if (ev->key() == Qt::Key_W && (ev->modifiers() & (Qt::MetaModifier | Qt::ControlModifier))) { - if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) { - App::wnd()->minimizeToTray(); - return true; - } else { - App::wnd()->hide(); - App::wnd()->updateIsActive(cOfflineBlurTimeout()); - App::wnd()->updateGlobalMenu(); + if (cPlatform() == dbipMac) { + if (ev->key() == Qt::Key_W && (ev->modifiers() & (Qt::MetaModifier | Qt::ControlModifier))) { + if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) { + App::wnd()->minimizeToTray(); + return true; + } else { + App::wnd()->hide(); + App::wnd()->updateIsActive(cOfflineBlurTimeout()); + App::wnd()->updateGlobalMenu(); + return true; + } + } else if (ev->key() == Qt::Key_M && (ev->modifiers() & (Qt::MetaModifier | Qt::ControlModifier))) { + App::wnd()->setWindowState(Qt::WindowMinimized); return true; } - } else if (ev->key() == Qt::Key_M && (ev->modifiers() & (Qt::MetaModifier | Qt::ControlModifier))) { - App::wnd()->setWindowState(Qt::WindowMinimized); - return true; + } + if (ev->key() == Qt::Key_MediaPlay) { + if (App::main()) App::main()->player()->playPressed(); + } else if (ev->key() == Qt::Key_MediaPause) { + if (App::main()) App::main()->player()->pausePressed(); + } else if (ev->key() == Qt::Key_MediaTogglePlayPause) { + if (App::main()) App::main()->player()->playPausePressed(); + } else if (ev->key() == Qt::Key_MediaStop) { + if (App::main()) App::main()->player()->stopPressed(); + } else if (ev->key() == Qt::Key_MediaPrevious) { + if (App::main()) App::main()->player()->prevPressed(); + } else if (ev->key() == Qt::Key_MediaNext) { + if (App::main()) App::main()->player()->nextPressed(); } } return QObject::eventFilter(o, e); @@ -95,9 +110,8 @@ Application::Application(int &argc, char **argv) : PsApplication(argc, argv), } mainApp = this; - if (cPlatform() == dbipMac) { - installEventFilter(new EventFilterForMac(this)); - } + + installEventFilter(new EventFilterForKeys(this)); QFontDatabase::addApplicationFont(qsl(":/gui/art/fonts/OpenSans-Regular.ttf")); QFontDatabase::addApplicationFont(qsl(":/gui/art/fonts/OpenSans-Bold.ttf")); diff --git a/Telegram/SourceFiles/playerwidget.cpp b/Telegram/SourceFiles/playerwidget.cpp index 3e4b85dcea..c0676ff70a 100644 --- a/Telegram/SourceFiles/playerwidget.cpp +++ b/Telegram/SourceFiles/playerwidget.cpp @@ -149,26 +149,12 @@ void PlayerWidget::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::LeftButton) { _down = OverNone; if (_song && _over == OverPlay) { - SongMsgId playing; - AudioPlayerState playingState = AudioPlayerStopped; - audioPlayer()->currentState(&playing, &playingState); - if (playing == _song && !(playingState & AudioPlayerStoppedMask)) { - audioPlayer()->pauseresume(OverviewDocuments); - } else { - audioPlayer()->play(_song); - if (App::main()) App::main()->documentPlayProgress(_song); - } + playPausePressed(); return; } else if (_over == OverPrev) { - const History::MediaOverview *o = _history ? &_history->_overview[OverviewAudioDocuments] : 0; - if (audioPlayer() && o && _index > 0 && _index <= o->size() && !o->isEmpty()) { - startPlay(o->at(_index - 1)); - } + prevPressed(); } else if (_over == OverNext) { - const History::MediaOverview *o = _history ? &_history->_overview[OverviewAudioDocuments] : 0; - if (audioPlayer() && o && _index >= 0 && _index < o->size() - 1) { - startPlay(o->at(_index + 1)); - } + nextPressed(); } else if (_over == OverClose) { _down = OverClose; } else if (_over == OverVolume) { @@ -411,14 +397,79 @@ void PlayerWidget::mouseReleaseEvent(QMouseEvent *e) { } update(); } else if (_down == OverClose && _over == OverClose) { - if (_song) { - audioPlayer()->stop(OverviewDocuments); - if (App::main()) App::main()->hidePlayer(); - } + stopPressed(); } _down = OverNone; } +void PlayerWidget::playPressed() { + if (!_song || isHidden()) return; + + SongMsgId playing; + AudioPlayerState playingState = AudioPlayerStopped; + audioPlayer()->currentState(&playing, &playingState); + if (playing == _song && !(playingState & AudioPlayerStoppedMask)) { + if (playingState == AudioPlayerPausing || playingState == AudioPlayerPaused || playingState == AudioPlayerPausedAtEnd) { + audioPlayer()->pauseresume(OverviewDocuments); + } + } else { + audioPlayer()->play(_song); + if (App::main()) App::main()->documentPlayProgress(_song); + } +} + +void PlayerWidget::pausePressed() { + if (!_song || isHidden()) return; + + SongMsgId playing; + AudioPlayerState playingState = AudioPlayerStopped; + audioPlayer()->currentState(&playing, &playingState); + if (playing == _song && !(playingState & AudioPlayerStoppedMask)) { + if (playingState == AudioPlayerStarting || playingState == AudioPlayerResuming || playingState == AudioPlayerPlaying || playingState == AudioPlayerFinishing) { + audioPlayer()->pauseresume(OverviewDocuments); + } + } +} + +void PlayerWidget::playPausePressed() { + if (!_song || isHidden()) return; + + SongMsgId playing; + AudioPlayerState playingState = AudioPlayerStopped; + audioPlayer()->currentState(&playing, &playingState); + if (playing == _song && !(playingState & AudioPlayerStoppedMask)) { + audioPlayer()->pauseresume(OverviewDocuments); + } else { + audioPlayer()->play(_song); + if (App::main()) App::main()->documentPlayProgress(_song); + } +} + +void PlayerWidget::prevPressed() { + if (isHidden() || !_prevAvailable) return; + + const History::MediaOverview *o = _history ? &_history->_overview[OverviewAudioDocuments] : 0; + if (audioPlayer() && o && _index > 0 && _index <= o->size() && !o->isEmpty()) { + startPlay(o->at(_index - 1)); + } +} + +void PlayerWidget::nextPressed() { + if (isHidden() || !_nextAvailable) return; + + const History::MediaOverview *o = _history ? &_history->_overview[OverviewAudioDocuments] : 0; + if (audioPlayer() && o && _index >= 0 && _index < o->size() - 1) { + startPlay(o->at(_index + 1)); + } +} + +void PlayerWidget::stopPressed() { + if (!_song || isHidden()) return; + + audioPlayer()->stop(OverviewDocuments); + if (App::main()) App::main()->hidePlayer(); +} + void PlayerWidget::resizeEvent(QResizeEvent *e) { int32 availh = (height() - st::playerLineHeight); int32 ch = st::playerPlay.pxHeight() + st::playerSkip, ct = (availh - ch) / 2; diff --git a/Telegram/SourceFiles/playerwidget.h b/Telegram/SourceFiles/playerwidget.h index 8656e0d0b8..f7ca316960 100644 --- a/Telegram/SourceFiles/playerwidget.h +++ b/Telegram/SourceFiles/playerwidget.h @@ -33,6 +33,13 @@ public: void mouseReleaseEvent(QMouseEvent *e); void resizeEvent(QResizeEvent *e); + void playPressed(); + void pausePressed(); + void playPausePressed(); + void prevPressed(); + void nextPressed(); + void stopPressed(); + bool progressStep(float64 ms); bool stateStep(float64 ms); diff --git a/Telegram/SourceFiles/pspecific_mac.cpp b/Telegram/SourceFiles/pspecific_mac.cpp index f957384e28..b7fc572141 100644 --- a/Telegram/SourceFiles/pspecific_mac.cpp +++ b/Telegram/SourceFiles/pspecific_mac.cpp @@ -41,7 +41,7 @@ namespace { Window *wnd = Application::wnd(); if (!wnd) return false; - return false; + return wnd->psFilterNativeEvent(message); } }; _PsEventFilter *_psEventFilter = 0; @@ -460,6 +460,10 @@ void PsMainWindow::psActivateNotify(NotifyWindow *w) { objc_activateWnd(w->winId()); } +bool PsMainWindow::psFilterNativeEvent(void *event) { + return _private.filterNativeEvent(event); +} + namespace { QRect _monitorRect; uint64 _monitorLastGot = 0; diff --git a/Telegram/SourceFiles/pspecific_mac.h b/Telegram/SourceFiles/pspecific_mac.h index ffca8718af..5dcda28775 100644 --- a/Telegram/SourceFiles/pspecific_mac.h +++ b/Telegram/SourceFiles/pspecific_mac.h @@ -36,7 +36,7 @@ public: void darkModeChanged(); void notifyClicked(unsigned long long peer, int msgid); void notifyReplied(unsigned long long peer, int msgid, const char *str); - + }; class NotifyWindow; @@ -72,6 +72,8 @@ public: return posInited; } + bool psFilterNativeEvent(void *event); + void psActivateNotify(NotifyWindow *w); void psClearNotifies(PeerId peerId = 0); void psNotifyShown(NotifyWindow *w); diff --git a/Telegram/SourceFiles/pspecific_mac_p.h b/Telegram/SourceFiles/pspecific_mac_p.h index 385dc0ab11..2470ae0a7b 100644 --- a/Telegram/SourceFiles/pspecific_mac_p.h +++ b/Telegram/SourceFiles/pspecific_mac_p.h @@ -33,7 +33,9 @@ public: void clearNotifies(uint64 peer = 0); void enableShadow(WId winId); - + + bool filterNativeEvent(void *event); + virtual void activeSpaceChanged() { } virtual void darkModeChanged() { diff --git a/Telegram/SourceFiles/pspecific_mac_p.mm b/Telegram/SourceFiles/pspecific_mac_p.mm index fe5c91c150..8df6af2248 100644 --- a/Telegram/SourceFiles/pspecific_mac_p.mm +++ b/Telegram/SourceFiles/pspecific_mac_p.mm @@ -19,6 +19,7 @@ Copyright (c) 2014 John Preston, https://desktop.telegram.org #include "pspecific_mac_p.h" #include "window.h" +#include "mainwidget.h" #include "application.h" #include "lang.h" @@ -27,6 +28,8 @@ Copyright (c) 2014 John Preston, https://desktop.telegram.org #include #include +#include + @interface qVisualize : NSObject { } @@ -304,6 +307,41 @@ void PsMacWindowPrivate::enableShadow(WId winId) { // [[(NSView*)winId window] setHasShadow:YES]; } +bool PsMacWindowPrivate::filterNativeEvent(void *event) { + NSEvent *e = static_cast(event); + if (e && [e type] == NSSystemDefined && [e subtype] == 8) { + int keyCode = (([e data1] & 0xFFFF0000) >> 16); + int keyFlags = ([e data1] & 0x0000FFFF); + int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA; + int keyRepeat = (keyFlags & 0x1); + + switch (keyCode) { + case NX_KEYTYPE_PLAY: + if (keyState == 0) { // Play pressed and released + if (App::main()) App::main()->player()->playPausePressed(); + return true; + } + break; + + case NX_KEYTYPE_FAST: + if (keyState == 0) { // Next pressed and released + if (App::main()) App::main()->player()->nextPressed(); + return true; + } + break; + + case NX_KEYTYPE_REWIND: + if (keyState == 0) { // Previous pressed and released + if (App::main()) App::main()->player()->prevPressed(); + return true; + } + break; + } + } + return false; +} + + void PsMacWindowPrivate::clearNotifies(unsigned long long peer) { NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter]; if (peer) {