File: unified_slider_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 (167 lines) | stat: -rw-r--r-- 6,203 bytes parent folder | download | duplicates (6)
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
// 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.

#include "ash/system/unified/unified_slider_view.h"

#include <memory>
#include <utility>

#include "ash/constants/quick_settings_catalogs.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/quick_settings_metrics_util.h"
#include "ash/system/unified/quick_settings_slider.h"
#include "base/check_op.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/slider.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

constexpr auto kQsSliderRowPadding = gfx::Insets::TLBR(0, 12, 4, 16);
constexpr auto kQsSliderBorder = gfx::Insets::TLBR(4, 4, 4, 16);

std::unique_ptr<views::Slider> CreateSlider(
    UnifiedSliderListener* listener,
    bool read_only,
    QuickSettingsSlider::Style slider_style) {
  return read_only
             ? std::make_unique<ReadOnlySlider>(slider_style)
             : std::make_unique<QuickSettingsSlider>(listener, slider_style);
}

}  // namespace

void UnifiedSliderListener::TrackToggleUMA(bool target_toggle_state) {
  CHECK_NE(GetCatalogName(), QsSliderCatalogName::kUnknown);
  quick_settings_metrics_util::RecordQsSliderToggle(
      GetCatalogName(), /*enable=*/target_toggle_state);
}

void UnifiedSliderListener::TrackValueChangeUMA(bool going_up) {
  CHECK_NE(GetCatalogName(), QsSliderCatalogName::kUnknown);
  quick_settings_metrics_util::RecordQsSliderValueChange(GetCatalogName(),
                                                         /*going_up=*/going_up);
}

UnifiedSliderView::UnifiedSliderView(views::Button::PressedCallback callback,
                                     UnifiedSliderListener* listener,
                                     const gfx::VectorIcon& icon,
                                     int accessible_name_id,
                                     bool is_togglable,
                                     bool read_only,
                                     QuickSettingsSlider::Style slider_style)
    : icon_(&icon), is_togglable_(is_togglable) {
  slider_ = AddChildView(CreateSlider(listener, read_only, slider_style));
  slider_->SetBorder(views::CreateEmptyBorder(kQsSliderBorder));
  // Sets `slider_` to have a `BoxLayout` to align the child view
  // `slider_button_` to the left.
  auto* slider_layout =
      slider_->SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kHorizontal));
  slider_layout->set_main_axis_alignment(
      views::BoxLayout::MainAxisAlignment::kStart);

  const bool is_default_style =
      (slider_style == QuickSettingsSlider::Style::kDefault ||
       slider_style == QuickSettingsSlider::Style::kDefaultMuted);
  slider_button_ = slider_->AddChildView(std::make_unique<IconButton>(
      std::move(callback),
      is_default_style ? IconButton::Type::kMediumFloating
                       : IconButton::Type::kLargeFloating,
      &icon, accessible_name_id,
      /*is_togglable=*/true,
      /*has_border=*/true));
  slider_button_->SetIconColor(cros_tokens::kCrosSysSystemOnPrimaryContainer);
  // The `slider_button_` should be focusable by the ChromeVox.
  slider_button_->SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
  // `slider_button_` should disable event processing if it's not togglable.
  if (!is_togglable_) {
    slider_button_->SetCanProcessEventsWithinSubtree(/*can_process=*/false);
  }

  // Prevent an accessibility event while initiallizing this view.
  // Typically the first update of the slider value is conducted by the
  // caller function to reflect the current value.
  slider_->SetEnableAccessibilityEvents(false);
  slider_->GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(accessible_name_id),
      ax::mojom::NameFrom::kAttribute);

  auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, kQsSliderRowPadding,
      kSliderChildrenViewSpacing));
  layout->SetFlexForView(slider_, /*flex=*/1);
  layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kCenter);

  // Adds a layer to set it non-opaque. Otherwise the full part of the slider
  // will be drawn on top of the previous draw.
  SetPaintToLayer();
  layer()->SetFillsBoundsOpaquely(false);
}

void UnifiedSliderView::SetSliderValue(float value, bool by_user) {
  // SetValue() calls |listener|, so we should ignore the call when the widget
  // is closed, because controllers are already deleted.
  // It should allow the case GetWidget() returning null, so that initial
  // position can be properly set by controllers before the view is attached to
  // a widget.
  if (GetWidget() && GetWidget()->IsClosed()) {
    return;
  }

  slider_->SetValue(value);
  if (by_user) {
    slider_->SetEnableAccessibilityEvents(true);
  }
}

UnifiedSliderView::~UnifiedSliderView() = default;

void UnifiedSliderView::OnEvent(ui::Event* event) {
  // If `slider_button_` is not togglable, pressing the return key should not
  // trigger the clicking callback.
  if (!is_togglable_) {
    views::View::OnEvent(event);
    return;
  }

  if (!event->IsKeyEvent()) {
    views::View::OnEvent(event);
    return;
  }

  if (slider_ && !slider_->GetEnableAccessibilityEvents()) {
    slider_->SetEnableAccessibilityEvents(true);
  }

  auto* key_event = event->AsKeyEvent();
  auto key_code = key_event->key_code();

  // Only handles press event to avoid handling the event again when the key is
  // released.
  if (key_code == ui::VKEY_RETURN &&
      key_event->type() == ui::EventType::kKeyPressed) {
    slider_button_->NotifyClick(*event);
    return;
  }

  views::View::OnEvent(event);
}

BEGIN_METADATA(UnifiedSliderView)
END_METADATA

}  // namespace ash