File: select_to_speak_speed_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 3,569 bytes parent folder | download | duplicates (3)
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
// 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.

#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/metrics/histogram_functions.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