From 134737afda5e7a79091b7d94b0298b20eb2f1e89 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 5 Aug 2016 11:50:50 +0300 Subject: [PATCH] Implement audio mute for webtabs (#2019) --- src/lib/data/icons.qrc | 2 ++ src/lib/data/icons/other/audiomuted.png | Bin 0 -> 241 bytes src/lib/data/icons/other/audioplaying.png | Bin 0 -> 113 bytes src/lib/tabwidget/tabbar.cpp | 12 +++++++ src/lib/tabwidget/tabbar.h | 1 + src/lib/tabwidget/tabicon.cpp | 37 ++++++++++++++++++++++ src/lib/tabwidget/tabicon.h | 5 +++ src/lib/webtab/webtab.cpp | 27 ++++++++++++++++ src/lib/webtab/webtab.h | 4 +++ 9 files changed, 88 insertions(+) create mode 100644 src/lib/data/icons/other/audiomuted.png create mode 100644 src/lib/data/icons/other/audioplaying.png diff --git a/src/lib/data/icons.qrc b/src/lib/data/icons.qrc index 95f6838c5..91f48a48f 100644 --- a/src/lib/data/icons.qrc +++ b/src/lib/data/icons.qrc @@ -64,5 +64,7 @@ icons/menu/privatebrowsing.png icons/menu/settings.png icons/other/startpage.png + icons/other/audioplaying.png + icons/other/audiomuted.png diff --git a/src/lib/data/icons/other/audiomuted.png b/src/lib/data/icons/other/audiomuted.png new file mode 100644 index 0000000000000000000000000000000000000000..8c03b192b4e818e667564cfb880d19ef5e6c5b12 GIT binary patch literal 241 zcmV zu`UEr6vyE&Q;_XEfG9@@2!iZx6_ka5FwV!@HLPN^BD!Wi%fdGe7SC55G%DVwEQcvK9%E7$9eu rnfo1?5@QfvEreFFL;Ik$ZPNY#ZRAlCGLe{w00000NkvXXu0mjfEz4eQ literal 0 HcmV?d00001 diff --git a/src/lib/data/icons/other/audioplaying.png b/src/lib/data/icons/other/audioplaying.png new file mode 100644 index 0000000000000000000000000000000000000000..36a77314e4639cb74f383bf8e7389defbe5dabac GIT binary patch literal 113 zcmeAS@N?(olHy`uVBq!ia0vp^LLkh+0wn(&ce?|mEIeHtLn;`PC3qGzN~r$t4E!{I zfzXacZ+U87T>3vzF(k#zu*)vU_oB}szWZkmG+(b5VOMjy>A=Lmr)iRZPQ5Y)Xa<9) LtDnm{r-UW|3ELy= literal 0 HcmV?d00001 diff --git a/src/lib/tabwidget/tabbar.cpp b/src/lib/tabwidget/tabbar.cpp index f342977dc..301a6bd70 100644 --- a/src/lib/tabwidget/tabbar.cpp +++ b/src/lib/tabwidget/tabbar.cpp @@ -330,6 +330,9 @@ void TabBar::contextMenuEvent(QContextMenuEvent* event) } menu.addAction(webTab->isPinned() ? tr("Un&pin Tab") : tr("&Pin Tab"), this, SLOT(pinTab())); +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + menu.addAction(webTab->isMuted() ? tr("Un&mute Tab") : tr("&Mute Tab"), this, SLOT(muteTab())); +#endif menu.addSeparator(); menu.addAction(tr("Re&load All Tabs"), m_tabWidget, SLOT(reloadAllTabs())); menu.addAction(tr("&Bookmark This Tab"), this, SLOT(bookmarkTab())); @@ -460,6 +463,15 @@ void TabBar::pinTab() } } +void TabBar::muteTab() +{ + WebTab* webTab = qobject_cast(m_tabWidget->widget(m_clickedTab)); + + if (webTab) { + webTab->toggleMuted(); + } +} + void TabBar::overrideTabTextColor(int index, QColor color) { if (!m_originalTabTextColor.isValid()) { diff --git a/src/lib/tabwidget/tabbar.h b/src/lib/tabwidget/tabbar.h index 780c7ef1d..397c44b66 100644 --- a/src/lib/tabwidget/tabbar.h +++ b/src/lib/tabwidget/tabbar.h @@ -69,6 +69,7 @@ private slots: void bookmarkTab(); void pinTab(); + void muteTab(); void closeCurrentTab(); void closeAllButCurrent(); diff --git a/src/lib/tabwidget/tabicon.cpp b/src/lib/tabwidget/tabicon.cpp index 0460b0b48..e6d2a4c1a 100644 --- a/src/lib/tabwidget/tabicon.cpp +++ b/src/lib/tabwidget/tabicon.cpp @@ -21,6 +21,7 @@ #include "tabbedwebview.h" #include +#include #define ANIMATION_INTERVAL 70 @@ -29,12 +30,16 @@ TabIcon::TabIcon(QWidget* parent) , m_tab(0) , m_currentFrame(0) , m_animationRunning(false) + , m_audioIconDisplayed(false) { setObjectName(QSL("tab-icon")); m_animationPixmap = QIcon(QSL(":icons/other/loading.png")).pixmap(288, 16); m_framesCount = m_animationPixmap.width() / m_animationPixmap.height(); + m_audioPlayingPixmap = QIcon(QSL(":icons/other/audioplaying.png")).pixmap(15, 15); + m_audioMutedPixmap = QIcon(QSL(":icons/other/audiomuted.png")).pixmap(15, 15); + m_updateTimer = new QTimer(this); m_updateTimer->setInterval(ANIMATION_INTERVAL); connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateAnimationFrame())); @@ -93,6 +98,16 @@ void TabIcon::updateAnimationFrame() m_currentFrame = (m_currentFrame + 1) % m_framesCount; } +void TabIcon::updateAudioIcon(bool recentlyAudible) +{ + if (m_tab->isMuted() || (!m_tab->isMuted() && recentlyAudible)) + m_audioIconDisplayed = true; + else + m_audioIconDisplayed = false; + + update(); +} + void TabIcon::paintEvent(QPaintEvent* event) { Q_UNUSED(event); @@ -113,4 +128,26 @@ void TabIcon::paintEvent(QPaintEvent* event) p.drawPixmap(r, m_animationPixmap, QRect(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize)); else p.drawPixmap(r, m_sitePixmap); + + if (m_audioIconDisplayed) { + if (m_tab->isMuted()) + p.drawPixmap(QPointF(width() * 0.25, 0), m_audioMutedPixmap); + else + p.drawPixmap(QPointF(width() * 0.25, 0), m_audioPlayingPixmap); + } +} + +void TabIcon::mousePressEvent(QMouseEvent *event) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + qreal x = event->localPos().x(); + qreal y = event->localPos().y(); + // if audio icon is clicked - we don't propagate mouse press to the tab + if (m_audioIconDisplayed && x >= width() * 0.25 && y < height() * 0.75) + m_tab->toggleMuted(); + else + QWidget::mousePressEvent(event); +#else + QWidget::mousePressEvent(event); +#endif } diff --git a/src/lib/tabwidget/tabicon.h b/src/lib/tabwidget/tabicon.h index 8340c4a9b..bb5fd1767 100644 --- a/src/lib/tabwidget/tabicon.h +++ b/src/lib/tabwidget/tabicon.h @@ -36,6 +36,7 @@ public: void setWebTab(WebTab* tab); void setIcon(const QIcon &icon); + void updateAudioIcon(bool recentlyAudible); private slots: void showIcon(); @@ -46,15 +47,19 @@ private slots: private: void paintEvent(QPaintEvent* event); + void mousePressEvent(QMouseEvent* event); WebTab* m_tab; QTimer* m_updateTimer; QPixmap m_sitePixmap; QPixmap m_animationPixmap; + QPixmap m_audioPlayingPixmap; + QPixmap m_audioMutedPixmap; int m_currentFrame; int m_framesCount; bool m_animationRunning; + bool m_audioIconDisplayed; }; #endif // TABICON_H diff --git a/src/lib/webtab/webtab.cpp b/src/lib/webtab/webtab.cpp index 8f9a592b7..9310f7387 100644 --- a/src/lib/webtab/webtab.cpp +++ b/src/lib/webtab/webtab.cpp @@ -142,6 +142,9 @@ WebTab::WebTab(BrowserWindow* window) connect(m_webView, SIGNAL(loadStarted()), this, SLOT(loadStarted())); connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished())); connect(m_webView, SIGNAL(titleChanged(QString)), this, SLOT(titleChanged(QString))); +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + connect(m_webView->page(), &QWebEnginePage::recentlyAudibleChanged, tabIcon(), &TabIcon::updateAudioIcon); +#endif } TabbedWebView* WebTab::webView() const @@ -292,6 +295,30 @@ void WebTab::setPinned(bool state) m_isPinned = state; } +bool WebTab::isMuted() const +{ +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + return m_webView->page()->isAudioMuted(); +#else + return false; +#endif +} + +void WebTab::setMuted(bool muted) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + m_webView->page()->setAudioMuted(muted); +#else + Q_UNUSED(muted) +#endif +} + +void WebTab::toggleMuted() +{ + bool muted = isMuted(); + setMuted(!muted); +} + LocationBar* WebTab::locationBar() const { return m_locationBar; diff --git a/src/lib/webtab/webtab.h b/src/lib/webtab/webtab.h index 3adb4155e..3571294db 100644 --- a/src/lib/webtab/webtab.h +++ b/src/lib/webtab/webtab.h @@ -84,6 +84,10 @@ public: void setPinned(bool state); void togglePinned(); + bool isMuted() const; + void setMuted(bool muted); + void toggleMuted(); + int tabIndex() const; bool isCurrentTab() const;