From 45b03bf75d847c293d0babbf8dbe25e3d7ed05c4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 30 Mar 2024 09:48:33 -0400 Subject: [PATCH] Ladybird/Qt: Support muting an entire page We already display a speaker icon on tabs which are playing audio. This allows the user to click that icon to mute the tab, at which point the icon is replaced with a muted speaker icon. We would previously hide the icon when audio stopped playing. We now do this only if the tab isn't muted. If it is muted, the muted speaker icon remains on the tab so that the page isn't stuck in a muted state. --- Ladybird/Qt/BrowserWindow.cpp | 34 ++++++++++++++++++++++++++++------ Ladybird/Qt/BrowserWindow.h | 2 ++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Ladybird/Qt/BrowserWindow.cpp b/Ladybird/Qt/BrowserWindow.cpp index 426ce39c6f..f3da9733b3 100644 --- a/Ladybird/Qt/BrowserWindow.cpp +++ b/Ladybird/Qt/BrowserWindow.cpp @@ -651,24 +651,46 @@ void BrowserWindow::tab_audio_play_state_changed(int index, Web::HTML::AudioPlay { switch (play_state) { case Web::HTML::AudioPlayState::Paused: - m_tabs_container->tabBar()->setTabButton(index, QTabBar::LeftSide, nullptr); + if (view().page_mute_state() == Web::HTML::MuteState::Unmuted) + m_tabs_container->tabBar()->setTabButton(index, QTabBar::LeftSide, nullptr); break; case Web::HTML::AudioPlayState::Playing: - auto icon = style()->standardIcon(QStyle::SP_MediaVolume); - - auto* button = new QPushButton(icon, {}); + auto* button = new QPushButton(icon_for_page_mute_state(), {}); button->setFlat(true); button->resize({ 20, 20 }); - // FIXME: Add a click handler to mute the tab. - button->setEnabled(false); + connect(button, &QPushButton::clicked, this, [this, index]() { + view().toggle_page_mute_state(); + + switch (view().audio_play_state()) { + case Web::HTML::AudioPlayState::Paused: + m_tabs_container->tabBar()->setTabButton(index, QTabBar::LeftSide, nullptr); + break; + case Web::HTML::AudioPlayState::Playing: + auto* button = m_tabs_container->tabBar()->tabButton(index, QTabBar::LeftSide); + verify_cast(button)->setIcon(icon_for_page_mute_state()); + break; + } + }); m_tabs_container->tabBar()->setTabButton(index, QTabBar::LeftSide, button); break; } } +QIcon BrowserWindow::icon_for_page_mute_state() const +{ + switch (view().page_mute_state()) { + case Web::HTML::MuteState::Muted: + return style()->standardIcon(QStyle::SP_MediaVolumeMuted); + case Web::HTML::MuteState::Unmuted: + return style()->standardIcon(QStyle::SP_MediaVolume); + } + + VERIFY_NOT_REACHED(); +} + void BrowserWindow::open_next_tab() { if (m_tabs_container->count() <= 1) diff --git a/Ladybird/Qt/BrowserWindow.h b/Ladybird/Qt/BrowserWindow.h index 471d1844ac..7b854538a0 100644 --- a/Ladybird/Qt/BrowserWindow.h +++ b/Ladybird/Qt/BrowserWindow.h @@ -128,6 +128,8 @@ private: } } + QIcon icon_for_page_mute_state() const; + QScreen* m_current_screen; double m_device_pixel_ratio { 0 };