Ladybird/AppKit: Display an audio button on tabs that are playing audio

When audio begins playing, add a button as an accessory view 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.
This commit is contained in:
Timothy Flynn 2024-03-28 12:59:15 -04:00 committed by Andreas Kling
parent 18821d3509
commit 8d765f1084
Notes: sideshowbarker 2024-07-17 05:00:08 +09:00
3 changed files with 27 additions and 0 deletions

View file

@ -11,6 +11,7 @@
#include <LibURL/Forward.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/HTML/ActivateTab.h>
#include <LibWeb/HTML/AudioPlayState.h>
#include <LibWebView/Forward.h>
#import <System/Cocoa.h>
@ -30,6 +31,7 @@
- (void)onTitleChange:(ByteString const&)title;
- (void)onFaviconChange:(Gfx::Bitmap const&)bitmap;
- (void)onAudioPlayStateChange:(Web::HTML::AudioPlayState)play_state;
- (void)onNavigateBack;
- (void)onNavigateForward;

View file

@ -809,6 +809,10 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
if (pasteboard_type)
copy_data_to_clipboard(data, pasteboard_type);
};
m_web_view_bridge->on_audio_play_state_changed = [self](auto play_state) {
[self.observer onAudioPlayStateChange:play_state];
};
}
- (void)selectDropdownAdd:(NSMenu*)menu item:(Web::HTML::SelectItem const&)item

View file

@ -275,6 +275,27 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
[self updateTabTitleAndFavicon];
}
- (void)onAudioPlayStateChange:(Web::HTML::AudioPlayState)play_state
{
switch (play_state) {
case Web::HTML::AudioPlayState::Paused:
[[self tab] setAccessoryView:nil];
break;
case Web::HTML::AudioPlayState::Playing:
auto* icon = [NSImage imageNamed:NSImageNameTouchBarAudioOutputVolumeHighTemplate];
auto* button = [NSButton buttonWithImage:icon target:nil action:nil];
// FIXME: Add a click handler to mute the tab.
NSButtonCell* cell = [button cell];
[cell setImageDimsWhenDisabled:NO];
[button setEnabled:NO];
[[self tab] setAccessoryView:button];
break;
}
}
- (void)onNavigateBack
{
[[self tabController] navigateBack:nil];