1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294

ash / system / audio / unified_volume_view.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ash/system/audio/unified_volume_view.h"

#include <memory>
#include <utility>

#include "ash/accessibility/accessibility_controller.h"
#include "ash/ash_element_identifiers.h"
#include "ash/constants/ash_features.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/icon_button.h"
#include "ash/system/audio/unified_volume_slider_controller.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/quick_settings_slider.h"
#include "ash/wm/screen_pinning_controller.h"
#include "ash/wm/window_state.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"

namespace ash {

using Style = QuickSettingsSlider::Style;

UnifiedVolumeView::UnifiedVolumeView(
    UnifiedVolumeSliderController* controller,
    UnifiedVolumeSliderController::Delegate* delegate,
    bool is_active_output_node)
    : UnifiedSliderView(base::BindRepeating(
                            &UnifiedVolumeSliderController::SliderButtonPressed,
                            base::Unretained(controller)),
                        controller,
                        kSystemMenuVolumeHighIcon,
                        IDS_ASH_STATUS_TRAY_VOLUME_SLIDER_LABEL),
      more_button_(AddChildView(std::make_unique<IconButton>(
          base::BindRepeating(&UnifiedVolumeSliderController::Delegate::
                                  OnAudioSettingsButtonClicked,
                              delegate->weak_ptr_factory_.GetWeakPtr()),
          IconButton::Type::kMediumFloating,
          &kQuickSettingsRightArrowIcon,
          IDS_ASH_STATUS_TRAY_AUDIO))),
      is_active_output_node_(is_active_output_node),
      device_id_(CrasAudioHandler::Get()->GetPrimaryActiveOutputNode()) {
  CrasAudioHandler::Get()->AddAudioObserver(this);

  // In the case that there is a trusted pinned window (fullscreen lock mode)
  // and the volume slider popup is shown, do not allow the more_button_ to
  // open quick settings.
  auto* window = Shell::Get()->screen_pinning_controller()->pinned_window();
  if (window && WindowState::Get(window)->IsTrustedPinned()) {
    more_button_->SetEnabled(false);
  }

  more_button_->SetIconColor(cros_tokens::kCrosSysSecondary);
  more_button_->SetProperty(views::kElementIdentifierKey,
                            kQuickSettingsAudioDetailedViewButtonElementId);

  // TODO(b/257151067): Update the a11y name id.
  // Adds the live caption button before `more_button_`.
  Shell::Get()->accessibility_controller()->AddObserver(this);
  const bool enabled =
      Shell::Get()->accessibility_controller()->live_caption().enabled();
  live_caption_button_ = AddChildViewAt(
      std::make_unique<IconButton>(
          base::BindRepeating(&UnifiedVolumeView::OnLiveCaptionButtonPressed,
                              base::Unretained(this)),
          IconButton::Type::kMedium,
          enabled ? &kUnifiedMenuLiveCaptionIcon
                  : &kUnifiedMenuLiveCaptionOffIcon,
          l10n_util::GetStringFUTF16(
              IDS_ASH_STATUS_TRAY_LIVE_CAPTION_TOGGLE_TOOLTIP,
              l10n_util::GetStringUTF16(
                  enabled
                      ? IDS_ASH_STATUS_TRAY_LIVE_CAPTION_ENABLED_STATE_TOOLTIP
                      : IDS_ASH_STATUS_TRAY_LIVE_CAPTION_DISABLED_STATE_TOOLTIP)),
          /*is_togglable=*/true,
          /*has_border=*/true),
      GetIndexOf(more_button_).value());
  // Sets the icon, icon color, background color for `live_caption_button_`
  // when it's toggled.
  live_caption_button_->SetToggledVectorIcon(kUnifiedMenuLiveCaptionIcon);
  live_caption_button_->SetIconToggledColor(
      cros_tokens::kCrosSysSystemOnPrimaryContainer);
  live_caption_button_->SetBackgroundToggledColor(
      cros_tokens::kCrosSysSystemPrimaryContainer);
  // Sets the icon, icon color, background color for `live_caption_button_`
  // when it's not toggled.
  live_caption_button_->SetVectorIcon(kUnifiedMenuLiveCaptionOffIcon);
  live_caption_button_->SetIconColor(cros_tokens::kCrosSysOnSurface);
  live_caption_button_->SetBackgroundColor(cros_tokens::kCrosSysSystemOnBase);

  live_caption_button_->SetToggled(enabled);

  Update(/*by_user=*/false);
}

UnifiedVolumeView::UnifiedVolumeView(UnifiedVolumeSliderController* controller,
                                     uint64_t device_id,
                                     bool is_active_output_node,
                                     const gfx::Insets& inside_padding)
    : UnifiedSliderView(base::BindRepeating(
                            &UnifiedVolumeSliderController::SliderButtonPressed,
                            base::Unretained(controller)),
                        controller,
                        kSystemMenuVolumeHighIcon,
                        IDS_ASH_STATUS_TRAY_VOLUME_SLIDER_LABEL,
                        /*is_togglable=*/true,
                        /*read_only=*/false,
                        Style::kRadioActive),
      more_button_(nullptr),
      is_active_output_node_(is_active_output_node),
      device_id_(device_id) {
  CrasAudioHandler::Get()->AddAudioObserver(this);

  auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, inside_padding,
      kSliderChildrenViewSpacing));
  slider()->SetBorder(views::CreateEmptyBorder(kRadioSliderPadding));
  slider()->SetPreferredSize(kRadioSliderPreferredSize);
  slider_button()->SetBorder(views::CreateEmptyBorder(kRadioSliderIconPadding));
  layout->SetFlexForView(slider(), /*flex=*/1);
  layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kCenter);

  Update(/*by_user=*/false);
}

UnifiedVolumeView::~UnifiedVolumeView() {
  CrasAudioHandler::Get()->RemoveAudioObserver(this);
  Shell::Get()->accessibility_controller()->RemoveObserver(this);
}

void UnifiedVolumeView::Update(bool by_user) {
  auto* audio_handler = CrasAudioHandler::Get();
  float level = audio_handler->GetOutputVolumePercent() / 100.f;
  level = audio_handler->GetOutputVolumePercentForDevice(device_id_) / 100.f;

  // Still needs to check if `level` is 0 because toggling the output mute by
  // keyboard will not set it to be muted in `UnifiedVolumeSliderController`.
  const bool is_muted =
      audio_handler->IsOutputMutedForDevice(device_id_) || level == 0;

  auto active_device_id = audio_handler->GetPrimaryActiveOutputNode();

  // Updates the style before updating the slider icon.
  auto* qs_slider = static_cast<QuickSettingsSlider*>(slider());
  const Style slider_style = qs_slider->slider_style();
  // The default style is for the slider in the main page and in the toast.
  const bool is_default_style =
      (slider_style == Style::kDefault || slider_style == Style::kDefaultMuted);
  if (is_default_style) {
    qs_slider->SetSliderStyle(is_muted ? Style::kDefaultMuted
                                       : Style::kDefault);
  } else if (active_device_id == device_id_) {
    qs_slider->SetSliderStyle(is_muted ? Style::kRadioActiveMuted
                                       : Style::kRadioActive);
  }

  // Updates the slider icon based on mute state and the style for radio
  // sliders.
  switch (slider_style) {
    case Style::kDefault:
    case Style::kDefaultMuted: {
      // TODO(b/257151067): Adds tooltip.
      slider_button()->SetVectorIcon(is_muted ? kUnifiedMenuVolumeMuteIcon
                                              : GetVolumeIconForLevel(level));
      slider_button()->SetIconColor(
          is_muted ? cros_tokens::kCrosSysOnSurfaceVariant
                   : cros_tokens::kCrosSysSystemOnPrimaryContainer);

      break;
    }
    case Style::kRadioActive:
    case Style::kRadioActiveMuted:
    case Style::kRadioInactive: {
      qs_slider->SetSliderStyle(
          active_device_id == device_id_
              ? (is_muted ? Style::kRadioActiveMuted : Style::kRadioActive)
              : Style::kRadioInactive);
      slider_button()->SetVectorIcon(is_muted ? kUnifiedMenuVolumeMuteIcon
                                              : GetVolumeIconForLevel(level));
      slider_button()->SetIconColor(
          active_device_id == device_id_
              ? (is_muted ? cros_tokens::kCrosSysOnSurface
                          : cros_tokens::kCrosSysSystemOnPrimaryContainer)
              : cros_tokens::kCrosSysOnSurfaceVariant);
      break;
    }
    default:
      NOTREACHED();
  }

  // Updates the tooltip for `slider_button()` based on the mute state.
  std::u16string state_tooltip_text = l10n_util::GetStringUTF16(
      is_muted ? IDS_ASH_STATUS_TRAY_VOLUME_STATE_MUTED
               : IDS_ASH_STATUS_TRAY_VOLUME_STATE_ON);
  slider_button()->SetTooltipText(l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_VOLUME, state_tooltip_text));

  // Slider's value is in finer granularity than audio volume level(0.01),
  // there will be a small discrepancy between slider's value and volume level
  // on audio side. To avoid the jittering in slider UI, use the slider's
  // current value.
  if (level != 1.0 && std::abs(level - slider()->GetValue()) <
                          kAudioSliderIgnoreUpdateThreshold) {
    level = slider()->GetValue();
  }
  // Note: even if the value does not change, we still need to call this
  // function to enable accessibility events (crbug.com/1013251).
  SetSliderValue(level, by_user);
}

const gfx::VectorIcon& UnifiedVolumeView::GetVolumeIconForLevel(float level) {
  int index = static_cast<int>(std::ceil(level * kQsVolumeLevels));
  CHECK(index >= 0 && index <= kQsVolumeLevels);
  return *kQsVolumeLevelIcons[index];
}

void UnifiedVolumeView::OnLiveCaptionButtonPressed() {
  Shell::Get()->accessibility_controller()->live_caption().SetEnabled(
      !Shell::Get()->accessibility_controller()->live_caption().enabled());
}

void UnifiedVolumeView::OnOutputNodeVolumeChanged(uint64_t node_id,
                                                  int volume) {
  Update(/*by_user=*/true);
}

void UnifiedVolumeView::OnOutputMuteChanged(bool mute_on) {
  Update(/*by_user=*/true);
}

void UnifiedVolumeView::OnAudioNodesChanged() {
  Update(/*by_user=*/true);
}

void UnifiedVolumeView::OnActiveOutputNodeChanged() {
  // If this view is for the active output node, we need to update the
  // `device_id_` before repaint.
  if (is_active_output_node_) {
    device_id_ = CrasAudioHandler::Get()->GetPrimaryActiveOutputNode();
  }

  Update(/*by_user=*/true);
}

void UnifiedVolumeView::OnActiveInputNodeChanged() {
  Update(/*by_user=*/true);
}

void UnifiedVolumeView::OnAccessibilityStatusChanged() {
  const bool enabled =
      Shell::Get()->accessibility_controller()->live_caption().enabled();

  // Sets `live_caption_button_` toggle state to update its icon, icon color,
  // and background color.
  live_caption_button_->SetToggled(enabled);

  // Updates the tooltip of `live_caption_button_`.
  std::u16string toggle_tooltip = l10n_util::GetStringUTF16(
      enabled ? IDS_ASH_STATUS_TRAY_LIVE_CAPTION_ENABLED_STATE_TOOLTIP
              : IDS_ASH_STATUS_TRAY_LIVE_CAPTION_DISABLED_STATE_TOOLTIP);

  live_caption_button_->SetTooltipText(l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_LIVE_CAPTION_TOGGLE_TOOLTIP, toggle_tooltip));
}

void UnifiedVolumeView::ChildVisibilityChanged(views::View* child) {
  DeprecatedLayoutImmediately();
}

void UnifiedVolumeView::VisibilityChanged(View* starting_from,
                                          bool is_visible) {
  Update(/*by_user=*/true);
}

BEGIN_METADATA(UnifiedVolumeView)
END_METADATA

}  // namespace ash