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

ash / system / accessibility / select_to_speak / select_to_speak_speed_view.cc [blame]

// Copyright 2020 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/accessibility/select_to_speak/select_to_speak_speed_view.h"

#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/accessibility/select_to_speak/select_to_speak_constants.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/separator.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/layout/box_layout.h"

namespace ash {

namespace {

// Start offset in pixels to use for option views.
constexpr int kOptionInset = 16;

void RecordSpeedMetric(int value) {
  base::UmaHistogramSparse("Accessibility.CrosSelectToSpeak.SpeedSetFromBubble",
                           value);
}

}  // namespace

SelectToSpeakSpeedView::SelectToSpeakSpeedView(Delegate* delegate,
                                               double initial_speech_rate)
    : delegate_(delegate) {
  SetOrientation(views::BoxLayout::Orientation::kVertical);
  SetInitialSpeechRate(initial_speech_rate);
}

void SelectToSpeakSpeedView::SetInitialSpeechRate(double initial_speech_rate) {
  RemoveAllChildViews();

  for (size_t i = 0; i < std::size(kSelectToSpeakSpeechRates); i++) {
    double option_speed = kSelectToSpeakSpeechRates[i];
    bool is_selected = option_speed == initial_speech_rate;
    // Add 1 to the index, because view IDs cannot be 0.
    auto label = base::ASCIIToUTF16(base::StringPrintf("%.1fx", option_speed));
    AddMenuItem(/*option_id=*/i + 1, label, is_selected);
  }
}

void SelectToSpeakSpeedView::AddMenuItem(int option_id,
                                         const std::u16string& label,
                                         bool is_selected) {
  HoverHighlightView* item = new HoverHighlightView(this);
  AddChildView(item);
  item->AddLabelRow(label, kOptionInset);
  item->SetID(option_id);
  TrayPopupUtils::InitializeAsCheckableRow(item, is_selected, false);
}

void SelectToSpeakSpeedView::OnViewClicked(views::View* sender) {
  unsigned int speed_index = sender->GetID() - 1;
  double speech_rate = kSelectToSpeakSpeechRates[speed_index];
  if (speed_index >= 0 && speed_index < std::size(kSelectToSpeakSpeechRates)) {
    delegate_->OnSpeechRateSelected(speech_rate);
    RecordSpeedMetric(floor(speech_rate * 100));
  }
}

void SelectToSpeakSpeedView::SetInitialFocus() {
  if (children().size() == 0)
    return;

  children()[0]->RequestFocus();
}

void SelectToSpeakSpeedView::OnKeyEvent(ui::KeyEvent* key_event) {
  if (key_event->type() != ui::EventType::kKeyPressed ||
      key_event->is_repeat()) {
    // Only process key when first pressed.
    return;
  }

  switch (key_event->key_code()) {
    case ui::KeyboardCode::VKEY_UP:
      GetFocusManager()->AdvanceFocus(/* reverse= */ true);
      break;
    case ui::KeyboardCode::VKEY_DOWN:
      GetFocusManager()->AdvanceFocus(/* reverse= */ false);
      break;
    default:
      // Unhandled key.
      return;
  }
  key_event->SetHandled();
  key_event->StopPropagation();
}

BEGIN_METADATA(SelectToSpeakSpeedView)
END_METADATA

}  // namespace ash