From 18821d3509b0d6df6d085bcee8fcdd352b4f5c68 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 28 Mar 2024 11:16:10 -0400 Subject: [PATCH] Ladybird/Qt: Display an audio button on tabs that are playing audio When audio begins playing, add a button to the left of the favicon with a speaker icon to indicate which tab is playing audio. This button is currently disabled, but in the future may be used to mute the tab. --- Ladybird/Qt/BrowserWindow.cpp | 23 +++++++++++++++++++++++ Ladybird/Qt/BrowserWindow.h | 2 ++ Ladybird/Qt/Tab.cpp | 4 ++++ Ladybird/Qt/Tab.h | 2 ++ 4 files changed, 31 insertions(+) diff --git a/Ladybird/Qt/BrowserWindow.cpp b/Ladybird/Qt/BrowserWindow.cpp index 98a69e2e484..426ce39c6f5 100644 --- a/Ladybird/Qt/BrowserWindow.cpp +++ b/Ladybird/Qt/BrowserWindow.cpp @@ -536,6 +536,7 @@ void BrowserWindow::initialize_tab(Tab* tab) { QObject::connect(tab, &Tab::title_changed, this, &BrowserWindow::tab_title_changed); QObject::connect(tab, &Tab::favicon_changed, this, &BrowserWindow::tab_favicon_changed); + QObject::connect(tab, &Tab::audio_play_state_changed, this, &BrowserWindow::tab_audio_play_state_changed); QObject::connect(&tab->view(), &WebContentView::urls_dropped, this, [this](auto& urls) { VERIFY(urls.size()); @@ -646,6 +647,28 @@ void BrowserWindow::tab_favicon_changed(int index, QIcon const& icon) m_tabs_container->setTabIcon(index, icon); } +void BrowserWindow::tab_audio_play_state_changed(int index, Web::HTML::AudioPlayState play_state) +{ + switch (play_state) { + case Web::HTML::AudioPlayState::Paused: + 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, {}); + button->setFlat(true); + button->resize({ 20, 20 }); + + // FIXME: Add a click handler to mute the tab. + button->setEnabled(false); + + m_tabs_container->tabBar()->setTabButton(index, QTabBar::LeftSide, button); + break; + } +} + void BrowserWindow::open_next_tab() { if (m_tabs_container->count() <= 1) diff --git a/Ladybird/Qt/BrowserWindow.h b/Ladybird/Qt/BrowserWindow.h index c1721f0d0ba..471d1844ac8 100644 --- a/Ladybird/Qt/BrowserWindow.h +++ b/Ladybird/Qt/BrowserWindow.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -78,6 +79,7 @@ public slots: void device_pixel_ratio_changed(qreal dpi); void tab_title_changed(int index, QString const&); void tab_favicon_changed(int index, QIcon const& icon); + void tab_audio_play_state_changed(int index, Web::HTML::AudioPlayState); Tab& new_tab_from_url(URL::URL const&, Web::HTML::ActivateTab); Tab& new_tab_from_content(StringView html, Web::HTML::ActivateTab); Tab& new_child_tab(Web::HTML::ActivateTab, Tab& parent, Web::HTML::WebViewHints, Optional page_index); diff --git a/Ladybird/Qt/Tab.cpp b/Ladybird/Qt/Tab.cpp index afefb320e62..a67d8223b4c 100644 --- a/Ladybird/Qt/Tab.cpp +++ b/Ladybird/Qt/Tab.cpp @@ -389,6 +389,10 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St clipboard->setMimeData(mime_data); }; + view().on_audio_play_state_changed = [this](auto play_state) { + emit audio_play_state_changed(tab_index(), play_state); + }; + auto* search_selected_text_action = new QAction("&Search for ", this); search_selected_text_action->setIcon(load_icon_from_uri("resource://icons/16x16/find.png"sv)); QObject::connect(search_selected_text_action, &QAction::triggered, this, [this]() { diff --git a/Ladybird/Qt/Tab.h b/Ladybird/Qt/Tab.h index 0464b36ec5d..d13ddf56842 100644 --- a/Ladybird/Qt/Tab.h +++ b/Ladybird/Qt/Tab.h @@ -9,6 +9,7 @@ #include "LocationEdit.h" #include "WebContentView.h" +#include #include #include #include @@ -61,6 +62,7 @@ public slots: signals: void title_changed(int id, QString const&); void favicon_changed(int id, QIcon const&); + void audio_play_state_changed(int id, Web::HTML::AudioPlayState); private: void select_dropdown_add_item(QMenu* menu, Web::HTML::SelectItem const& item);