Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make muting sites easier #16269

Merged
merged 11 commits into from
Dec 13, 2022
12 changes: 12 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,12 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_TAB_CXMENU_USE_FLOATING_VERTICAL_TABS" desc="The label of the tab context menu item for enabling floating mode for vertical tab strip">
Float on mouseover
</message>
<message name="IDS_TAB_CXMENU_SOUND_MUTE_TAB" desc="The label of the tab context menu item for muting one or more tabs. NOTE: If having no explicit # makes the phrasing awkward, feel free to add a # as necessary. [ICU Syntax]">
{NUM_TABS, plural, =1 {Mute tab} other {Mute tabs}}
</message>
<message name="IDS_TAB_CXMENU_SOUND_UNMUTE_TAB" desc="The label of the tab context menu item for unmuting one or more tabs. NOTE: If having no explicit # makes the phrasing awkward, feel free to add a # as necessary. [ICU Syntax]">
{NUM_TABS, plural, =1 {Unmute tab} other {Unmute tabs}}
</message>
</if>
<if expr="use_titlecase">
<message name="IDS_TAB_CXMENU_BOOKMARK_ALL_TABS" desc="In Title Case: The label of the tab context menu item for creating a bookmark folder containing an entry for each open tab.">
Expand All @@ -866,6 +872,12 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_TAB_CXMENU_USE_FLOATING_VERTICAL_TABS" desc="The label of the tab context menu item for enabling floating mode for vertical tab strip">
Float on Mouseover
</message>
<message name="IDS_TAB_CXMENU_SOUND_MUTE_TAB" desc="The label of the tab context menu item for muting one or more tabs. NOTE: If having no explicit # makes the phrasing awkward, feel free to add a # as necessary. [ICU Syntax]">
{NUM_TABS, plural, =1 {Mute Tab} other {Mute Tabs}}
</message>
<message name="IDS_TAB_CXMENU_SOUND_UNMUTE_TAB" desc="The label of the tab context menu item for unmuting one or more tabs. NOTE: If having no explicit # makes the phrasing awkward, feel free to add a # as necessary. [ICU Syntax]">
{NUM_TABS, plural, =1 {Unmute Tab} other {Unmute Tabs}}
</message>
</if>

<!-- Brave VPN -->
Expand Down
3 changes: 3 additions & 0 deletions app/brave_settings_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,9 @@
<message name="IDS_SETTINGS_CLOSING_LAST_TAB_OPTION_LABEL" desc="The text for settings option">
Close window when closing last tab
</message>
<message name="IDS_SETTINGS_DISABLE_CLICKABLE_MUTE_INDICATORS" desc="The text for the settings option for disabling clickable tab mute indicators">
Disable mute function in tab speaker icons
</message>

<!-- Avatars. Generated via zsh and node:
declare -i a=56
Expand Down
1 change: 1 addition & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(brave_rewards::prefs::kShowButton, true);
registry->RegisterBooleanPref(kMRUCyclingEnabled, false);
registry->RegisterBooleanPref(kTabsSearchShow, true);
registry->RegisterBooleanPref(kTabMuteIndicatorNotClickable, false);

brave_sync::Prefs::RegisterProfilePrefs(registry);

Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_allowlist)[brave_tabs::kTabHoverMode] =
settings_api::PREF_TYPE_NUMBER;
(*s_brave_allowlist)[kTabMuteIndicatorNotClickable] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
#if BUILDFLAG(ENABLE_BRAVE_VPN)
(*s_brave_allowlist)[brave_vpn::prefs::kBraveVPNShowButton] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
5 changes: 5 additions & 0 deletions browser/resources/settings/brave_appearance_page/toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
class="cr-row"
label="$i18n{showFullUrls}">
</settings-toggle-button>
<settings-toggle-button
pref="{{prefs.brave.tabs.mute_indicator_not_clickable}}"
class="cr-row"
label="$i18n{braveDisableClickableMuteIndicators}">
</settings-toggle-button>
<settings-toggle-button
class="cr-row"
pref="{{prefs.brave.autocomplete_enabled}}"
Expand Down
31 changes: 27 additions & 4 deletions browser/ui/tabs/brave_tab_menu_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

#include "brave/browser/ui/tabs/brave_tab_menu_model.h"

#include <algorithm>
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>

#include "brave/browser/ui/tabs/brave_tab_strip_model.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "components/sessions/core/tab_restore_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/list_selection_model.h"

BraveTabMenuModel::BraveTabMenuModel(
ui::SimpleMenuModel::Delegate* delegate,
Expand All @@ -26,7 +33,13 @@ BraveTabMenuModel::BraveTabMenuModel(
TabRestoreServiceFactory::GetForProfile(browser->profile());
}

Build();
auto indices = static_cast<BraveTabStripModel*>(tab_strip_model)
->GetTabIndicesForCommandAt(index);
all_muted_ = std::all_of(
indices.begin(), indices.end(), [&tab_strip_model](int index) {
return tab_strip_model->GetWebContentsAt(index)->IsAudioMuted();
});
Build(indices.size());
}

BraveTabMenuModel::~BraveTabMenuModel() = default;
Expand All @@ -44,15 +57,25 @@ int BraveTabMenuModel::GetRestoreTabCommandStringId() const {
return id;

if (restore_service_->entries().front()->type ==
sessions::TabRestoreService::WINDOW) {
id = IDS_RESTORE_WINDOW;
sessions::TabRestoreService::WINDOW) {
id = IDS_RESTORE_WINDOW;
}

return id;
}

void BraveTabMenuModel::Build() {
void BraveTabMenuModel::Build(int selected_tab_count) {
AddSeparator(ui::NORMAL_SEPARATOR);
auto mute_site_index =
GetIndexOfCommandId(TabStripModel::CommandToggleSiteMuted);

auto toggle_tab_mute_label = l10n_util::GetPluralStringFUTF16(
all_muted() ? IDS_TAB_CXMENU_SOUND_UNMUTE_TAB
: IDS_TAB_CXMENU_SOUND_MUTE_TAB,
selected_tab_count);
InsertItemAt(mute_site_index.value_or(GetItemCount()), CommandToggleTabMuted,
toggle_tab_mute_label);

AddItemWithStringId(CommandRestoreTab, GetRestoreTabCommandStringId());
AddItemWithStringId(CommandBookmarkAllTabs, IDS_TAB_CXMENU_BOOKMARK_ALL_TABS);
AddCheckItemWithStringId(CommandShowVerticalTabs,
Expand Down
6 changes: 5 additions & 1 deletion browser/ui/tabs/brave_tab_menu_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BraveTabMenuModel : public TabMenuModel {
CommandShowVerticalTabs,
CommandShowTitleBar,
CommandUseFloatingVerticalTabStrip,
CommandToggleTabMuted,
CommandLast,
};

Expand All @@ -38,12 +39,15 @@ class BraveTabMenuModel : public TabMenuModel {
BraveTabMenuModel& operator=(const BraveTabMenuModel&) = delete;
~BraveTabMenuModel() override;

bool all_muted() const { return all_muted_; }

private:
void Build();
void Build(int selected_tab_count);
int GetRestoreTabCommandStringId() const;

raw_ptr<content::WebContents> web_contents_ = nullptr;
sessions::TabRestoreService* restore_service_ = nullptr;
bool all_muted_;
};

#endif // BRAVE_BROWSER_UI_TABS_BRAVE_TAB_MENU_MODEL_H_
6 changes: 6 additions & 0 deletions browser/ui/tabs/brave_tab_strip_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "brave/browser/ui/tabs/brave_tab_strip_model.h"

#include <algorithm>
#include <vector>

#include "brave/browser/ui/brave_browser_window.h"
#include "brave/components/constants/pref_names.h"
Expand All @@ -14,6 +15,7 @@
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"

Expand Down Expand Up @@ -76,3 +78,7 @@ void BraveTabStripModel::SelectMRUTab(TabRelativeDirection direction,
void BraveTabStripModel::StopMRUCycling() {
mru_cycle_list_.clear();
}

std::vector<int> BraveTabStripModel::GetTabIndicesForCommandAt(int tab_index) {
return TabStripModel::GetIndicesForCommand(tab_index);
}
3 changes: 3 additions & 0 deletions browser/ui/tabs/brave_tab_strip_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class BraveTabStripModel : public TabStripModel {
// Stop MRU cycling, called when releasing the Ctrl key
void StopMRUCycling();

// Exposes a |TabStripModel| api to |BraveTabMenuModel|.
std::vector<int> GetTabIndicesForCommandAt(int tab_index);
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved

private:
// List of tab indexes sorted by most recently used
std::vector<int> mru_cycle_list_;
Expand Down
46 changes: 39 additions & 7 deletions browser/ui/views/tabs/brave_tab_context_menu_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@

#include "brave/browser/ui/views/tabs/brave_tab_context_menu_contents.h"

#include <algorithm>
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved
#include <iterator>
#include <string>
#include <vector>

#include "brave/browser/ui/browser_commands.h"
#include "brave/browser/ui/tabs/brave_tab_menu_model.h"
#include "brave/browser/ui/tabs/brave_tab_prefs.h"
#include "brave/browser/ui/tabs/brave_tab_strip_model.h"
#include "brave/browser/ui/views/tabs/brave_browser_tab_strip_controller.h"
#include "brave/browser/ui/views/tabs/features.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/tabs/tab_utils.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "components/sessions/core/tab_restore_service.h"
#include "content/public/browser/web_contents.h"
#include "ui/views/controls/menu/menu_runner.h"

BraveTabContextMenuContents::BraveTabContextMenuContents(
Tab* tab,
BraveBrowserTabStripController* controller,
int index)
: tab_(tab),
tab_index_(index),
browser_(const_cast<Browser*>(controller->browser())),
controller_(controller) {
model_ = std::make_unique<BraveTabMenuModel>(
Expand All @@ -42,9 +53,8 @@ void BraveTabContextMenuContents::Cancel() {
controller_ = nullptr;
}

void BraveTabContextMenuContents::RunMenuAt(
const gfx::Point& point,
ui::MenuSourceType source_type) {
void BraveTabContextMenuContents::RunMenuAt(const gfx::Point& point,
ui::MenuSourceType source_type) {
menu_runner_->RunMenuAt(tab_->GetWidget(), nullptr,
gfx::Rect(point, gfx::Size()),
views::MenuAnchorPosition::kTopLeft, source_type);
Expand Down Expand Up @@ -72,8 +82,7 @@ bool BraveTabContextMenuContents::IsCommandIdEnabled(int command_id) const {
return IsBraveCommandIdEnabled(command_id);

return controller_->IsCommandEnabledForTab(
static_cast<TabStripModel::ContextMenuCommand>(command_id),
tab_);
static_cast<TabStripModel::ContextMenuCommand>(command_id), tab_);
}

bool BraveTabContextMenuContents::IsCommandIdVisible(int command_id) const {
Expand Down Expand Up @@ -111,8 +120,7 @@ void BraveTabContextMenuContents::ExecuteCommand(int command_id,
// Executing the command destroys |this|, and can also end up destroying
// |controller_|. So stop the highlights before executing the command.
controller_->ExecuteCommandForTab(
static_cast<TabStripModel::ContextMenuCommand>(command_id),
tab_);
static_cast<TabStripModel::ContextMenuCommand>(command_id), tab_);
}

bool BraveTabContextMenuContents::IsBraveCommandIdEnabled(
Expand All @@ -131,6 +139,14 @@ bool BraveTabContextMenuContents::IsBraveCommandIdEnabled(
case BraveTabMenuModel::CommandShowVerticalTabs:
case BraveTabMenuModel::CommandUseFloatingVerticalTabStrip:
return true;
case BraveTabMenuModel::CommandToggleTabMuted: {
auto* model = static_cast<BraveTabStripModel*>(controller_->model());
for (const auto& index : model->GetTabIndicesForCommandAt(tab_index_)) {
if (!model->GetWebContentsAt(index)->GetLastCommittedURL().is_empty())
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}
default:
NOTREACHED();
break;
Expand Down Expand Up @@ -161,6 +177,22 @@ void BraveTabContextMenuContents::ExecuteBraveCommand(int command_id) {
brave::ToggleVerticalTabStripFloatingMode(browser_);
return;
}
case BraveTabMenuModel::CommandToggleTabMuted: {
auto* model = static_cast<BraveTabStripModel*>(controller_->model());
auto indices = model->GetTabIndicesForCommandAt(tab_index_);
std::vector<content::WebContents*> contentses;
std::transform(
indices.begin(), indices.end(), std::back_inserter(contentses),
[&model](int index) { return model->GetWebContentsAt(index); });

auto all_muted = model_->all_muted();
for (auto* contents : contentses) {
chrome::SetTabAudioMuted(contents, !all_muted,
TabMutedReason::AUDIO_INDICATOR,
/*extension_id=*/std::string());
}
return;
}
default:
NOTREACHED();
return;
Expand Down
1 change: 1 addition & 0 deletions browser/ui/views/tabs/brave_tab_context_menu_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class BraveTabContextMenuContents : public ui::SimpleMenuModel::Delegate {
std::unique_ptr<views::MenuRunner> menu_runner_;

raw_ptr<Tab> tab_ = nullptr;
int tab_index_;
raw_ptr<Browser> browser_ = nullptr;
sessions::TabRestoreService* restore_service_ = nullptr;
raw_ptr<BraveBrowserTabStripController> controller_ = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
{"braveHelpTipsWarnBeforeClosingWindow",
IDS_SETTINGS_WINDOW_CLOSING_CONFIRM_OPTION_LABEL},
{"braveHelpTipsClosingLastTab", IDS_SETTINGS_CLOSING_LAST_TAB_OPTION_LABEL},
{"braveDisableClickableMuteIndicators",
IDS_SETTINGS_DISABLE_CLICKABLE_MUTE_INDICATORS},
// New Tab Page
{"braveNewTab", IDS_SETTINGS_NEW_TAB},
{"braveNewTabBraveRewards", IDS_SETTINGS_NEW_TAB_BRAVE_REWARDS},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "base/no_destructor.h"
#include "brave/components/constants/pref_names.h"
#include "build/build_config.h"
#include "chrome/common/pref_names.h"

Expand All @@ -14,7 +15,7 @@ namespace {
const std::vector<const char*>& GetBravePersistentPrefNames() {
static base::NoDestructor<std::vector<const char*>> brave_allowlist({
#if !BUILDFLAG(IS_ANDROID)
prefs::kSidePanelHorizontalAlignment,
prefs::kSidePanelHorizontalAlignment, kTabMuteIndicatorNotClickable,
#endif
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "chrome/browser/ui/views/tabs/alert_indicator_button.h"

#include "brave/components/constants/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_slot_controller.h"
#include "components/prefs/pref_service.h"

#define AlertIndicatorButton AlertIndicatorButtonBase

#include "src/chrome/browser/ui/views/tabs/alert_indicator_button.cc"

#undef AlertIndicatorButton

void AlertIndicatorButton::UpdateEnabledForMuteToggle() {
const bool was_enabled = GetEnabled();
auto* browser = GetTab()->controller()->GetBrowser();

// We have clickable mute indicators enabled by default. Thus, if our pref is
// disabled we can force the indicator off.
// Note: We have a test which checks the feature is enabled by default. If
// that changes this may need to as well.
// Note: |browser| is |nullptr| in some unit_tests.
if (browser && browser->profile()->GetPrefs()->GetBoolean(
kTabMuteIndicatorNotClickable)) {
if (was_enabled)
SetEnabled(false);
return;
}
AlertIndicatorButtonBase::UpdateEnabledForMuteToggle();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_TABS_ALERT_INDICATOR_BUTTON_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_TABS_ALERT_INDICATOR_BUTTON_H_

class AlertIndicatorButton;
#define AlertIndicatorButton AlertIndicatorButtonBase
#define GetTab \
GetTab_Unused(); \
\
protected: \
Tab* GetTab
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved

#define UpdateEnabledForMuteToggle virtual UpdateEnabledForMuteToggle
#include "src/chrome/browser/ui/views/tabs/alert_indicator_button.h"
#undef UpdateEnabledForMuteToggle
#undef GetTab
#undef AlertIndicatorButton

class AlertIndicatorButton : public AlertIndicatorButtonBase {
public:
using AlertIndicatorButtonBase::AlertIndicatorButtonBase;
void UpdateEnabledForMuteToggle() override;
};

#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_TABS_ALERT_INDICATOR_BUTTON_H_
Loading