File: bluetooth_detailed_view_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (309 lines) | stat: -rw-r--r-- 12,539 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2022 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/bluetooth/bluetooth_detailed_view_impl.h"

#include <memory>
#include <utility>

#include "ash/ash_element_identifiers.h"
#include "ash/bubble/bubble_utils.h"
#include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/rounded_container.h"
#include "ash/style/typography.h"
#include "ash/system/bluetooth/bluetooth_device_list_item_view.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/tray/detailed_view_delegate.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tray_toggle_button.h"
#include "ash/system/tray/tri_view.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "chromeos/ash/services/bluetooth_config/public/cpp/cros_bluetooth_config_util.h"
#include "chromeos/ash/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
#include "chromeos/constants/chromeos_features.h"
#include "device/bluetooth/chromeos/bluetooth_utils.h"
#include "ui/accessibility/ax_enums.mojom-shared.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/gfx/geometry/insets.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

namespace ash {
namespace {

using bluetooth_config::IsBluetoothEnabled;
using bluetooth_config::IsBluetoothEnabledOrEnabling;
using bluetooth_config::mojom::BluetoothSystemState;

constexpr auto kToggleRowTriViewInsets = gfx::Insets::VH(8, 24);
constexpr auto kMainContainerMargins = gfx::Insets::TLBR(2, 0, 0, 0);
constexpr auto kPairNewDeviceIconMargins = gfx::Insets::TLBR(0, 2, 0, 0);
constexpr auto kSubHeaderInsets = gfx::Insets::TLBR(10, 24, 10, 16);

}  // namespace

BluetoothDetailedViewImpl::BluetoothDetailedViewImpl(
    DetailedViewDelegate* detailed_view_delegate,
    BluetoothDetailedView::Delegate* delegate)
    : BluetoothDetailedView(delegate),
      TrayDetailedView(detailed_view_delegate) {
  CreateTitleRow(IDS_ASH_STATUS_TRAY_BLUETOOTH);
  CreateScrollableList();
  CreateTopContainer();
  CreateMainContainer();
  if (chromeos::features::IsBluetoothWifiQSPodRefreshEnabled()) {
    CreateZeroStateView(std::make_unique<ZeroStateView>(
        /*image_id=*/IDR_TRAY_BLUETOOTH_UNAVAILABLE_STATE_IMAGE,
        /*title_id=*/IDS_ASH_STATUS_TRAY_BLUETOOTH_UNAVAILABLE_STATE_TITLE,
        /*subtitle_id=*/
        IDS_ASH_STATUS_TRAY_BLUETOOTH_UNAVAILABLE_STATE_SUBTITLE));
  }
  UpdateBluetoothEnabledState(BluetoothSystemState::kDisabled);
  device::RecordUiSurfaceDisplayed(
      device::BluetoothUiSurface::kBluetoothQuickSettings);
}

BluetoothDetailedViewImpl::~BluetoothDetailedViewImpl() = default;

views::View* BluetoothDetailedViewImpl::GetAsView() {
  return this;
}

void BluetoothDetailedViewImpl::UpdateBluetoothEnabledState(
    const BluetoothSystemState system_state) {
  if (chromeos::features::IsBluetoothWifiQSPodRefreshEnabled() &&
      system_state == BluetoothSystemState::kUnavailable) {
    SetZeroStateViewVisibility(true);
    return;
  }
  if (chromeos::features::IsBluetoothWifiQSPodRefreshEnabled()) {
    SetZeroStateViewVisibility(false);
  }

  bool is_enabled_or_enabling = IsBluetoothEnabledOrEnabling(system_state);

  // Use square corners on the bottom edge when Bluetooth is enabled.
  top_container_->SetBehavior(is_enabled_or_enabling
                                  ? RoundedContainer::Behavior::kTopRounded
                                  : RoundedContainer::Behavior::kAllRounded);
  main_container_->SetVisible(is_enabled_or_enabling);

  // Update the top container Bluetooth icon.
  toggle_icon_->SetImage(ui::ImageModel::FromVectorIcon(
      is_enabled_or_enabling ? kSystemMenuBluetoothIcon
                             : kSystemMenuBluetoothDisabledIcon,
      cros_tokens::kCrosSysOnSurface));

  // Update the top container on/off label.
  toggle_row_->text_label()->SetText(l10n_util::GetStringUTF16(
      is_enabled_or_enabling ? IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_SHORT
                             : IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_SHORT));

  // Update the toggle row and button tooltips. The entire row is clickable.
  std::u16string tooltip_template =
      is_enabled_or_enabling
          ? l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_BLUETOOTH_ENABLED_TOOLTIP)
          : l10n_util::GetStringUTF16(
                IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED_TOOLTIP);
  std::u16string tooltip_text = l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_BLUETOOTH_TOGGLE_TOOLTIP, tooltip_template);
  toggle_row_->SetTooltipText(tooltip_text);
  toggle_button_->SetTooltipText(tooltip_text);
  toggle_button_->GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH));

  // Ensure the toggle button is in sync with the current Bluetooth state.
  if (toggle_button_->GetIsOn() != is_enabled_or_enabling) {
    toggle_button_->SetIsOn(is_enabled_or_enabling);
  }

  // Only show "Pair new device" button if bluetooth is enabled.
  // see (b/297044914)
  pair_new_device_view_->SetVisible(IsBluetoothEnabled(system_state));

  InvalidateLayout();
}

BluetoothDeviceListItemView* BluetoothDetailedViewImpl::AddDeviceListItem() {
  return device_list_->AddChildView(
      std::make_unique<BluetoothDeviceListItemView>(/*listener=*/this));
}

views::View* BluetoothDetailedViewImpl::AddDeviceListSubHeader(
    const gfx::VectorIcon& icon,
    int text_id) {
  auto header = std::make_unique<views::BoxLayoutView>();
  header->SetInsideBorderInsets(kSubHeaderInsets);
  std::unique_ptr<views::Label> label = bubble_utils::CreateLabel(
      TypographyToken::kCrosBody2, l10n_util::GetStringUTF16(text_id),
      cros_tokens::kCrosSysOnSurfaceVariant);
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label->SetSubpixelRenderingEnabled(false);
  header->AddChildView(std::move(label));

  return device_list_->AddChildView(std::move(header));
}

void BluetoothDetailedViewImpl::NotifyDeviceListChanged() {
  device_list_->InvalidateLayout();
  DeprecatedLayoutImmediately();
}

views::View* BluetoothDetailedViewImpl::device_list() {
  return device_list_;
}

void BluetoothDetailedViewImpl::HandleViewClicked(views::View* view) {
  // Handle clicks on the on/off toggle row.
  if (view == toggle_row_) {
    // The toggle button has the old state, so switch to the opposite state.
    ToggleBluetoothState(!toggle_button_->GetIsOn());
    return;
  }

  // Handle clicks on the "pair new device" row.
  if (view == pair_new_device_view_) {
    delegate()->OnPairNewDeviceRequested();
    return;
  }

  // Handle clicks on Bluetooth devices.
  DCHECK(views::IsViewClass<BluetoothDeviceListItemView>(view));
  delegate()->OnDeviceListItemSelected(
      static_cast<BluetoothDeviceListItemView*>(view)->device_properties());
}

void BluetoothDetailedViewImpl::CreateExtraTitleRowButtons() {
  DCHECK(!settings_button_);

  tri_view()->SetContainerVisible(TriView::Container::END, /*visible=*/true);

  settings_button_ = CreateSettingsButton(
      base::BindRepeating(&BluetoothDetailedViewImpl::OnSettingsClicked,
                          weak_factory_.GetWeakPtr()),
      IDS_ASH_STATUS_TRAY_BLUETOOTH_SETTINGS);
  settings_button_->SetState(TrayPopupUtils::CanOpenWebUISettings()
                                 ? views::Button::STATE_NORMAL
                                 : views::Button::STATE_DISABLED);
  tri_view()->AddView(TriView::Container::END, settings_button_);
}

void BluetoothDetailedViewImpl::CreateTopContainer() {
  top_container_ =
      scroll_content()->AddChildView(std::make_unique<RoundedContainer>());
  // Ensure the HoverHighlightView ink drop fills the whole container.
  top_container_->SetBorderInsets(gfx::Insets());

  toggle_row_ = top_container_->AddChildView(
      std::make_unique<HoverHighlightView>(/*listener=*/this));
  toggle_row_->SetFocusBehavior(FocusBehavior::NEVER);

  // The icon image and label text depend on whether Bluetooth is enabled. They
  // are set in UpdateBluetoothEnabledState().
  auto icon = std::make_unique<views::ImageView>();
  toggle_icon_ = icon.get();
  toggle_row_->AddViewAndLabel(std::move(icon), u"");
  toggle_row_->text_label()->SetEnabledColor(cros_tokens::kCrosSysOnSurface);
  TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton1,
                                        *toggle_row_->text_label());

  auto toggle = std::make_unique<Switch>(base::BindRepeating(
      &BluetoothDetailedViewImpl::OnToggleClicked, weak_factory_.GetWeakPtr()));
  toggle_button_ = toggle.get();
  toggle_button_->SetProperty(views::kElementIdentifierKey,
                              kBluetoothDetailedViewToggleElementId);
  toggle_row_->AddRightView(toggle.release());

  // Allow the row to be taller than a typical tray menu item.
  toggle_row_->SetExpandable(true);
  toggle_row_->tri_view()->SetInsets(kToggleRowTriViewInsets);

  // ChromeVox users will just use the `toggle_button_` to toggle.
  toggle_icon_->GetViewAccessibility().SetIsIgnored(true);
  toggle_row_->text_label()->GetViewAccessibility().SetIsIgnored(true);
}

void BluetoothDetailedViewImpl::CreateMainContainer() {
  DCHECK(!main_container_);
  main_container_ =
      scroll_content()->AddChildView(std::make_unique<RoundedContainer>(
          RoundedContainer::Behavior::kBottomRounded));

  // Add a small empty space, like a separator, between the containers.
  main_container_->SetProperty(views::kMarginsKey, kMainContainerMargins);

  // Add a row for "pair new device".
  pair_new_device_view_ = main_container_->AddChildView(
      std::make_unique<HoverHighlightView>(/*listener=*/this));
  pair_new_device_view_->SetProperty(
      views::kElementIdentifierKey,
      kBluetoothDetailedViewPairNewDeviceElementId);

  // Create the "+" icon.
  auto icon = std::make_unique<views::ImageView>();
  icon->SetImage(ui::ImageModel::FromVectorIcon(kSystemMenuPlusIcon,
                                                cros_tokens::kCrosSysPrimary));
  icon->SetProperty(views::kMarginsKey, kPairNewDeviceIconMargins);
  pair_new_device_icon_ = icon.get();
  pair_new_device_view_->AddViewAndLabel(
      std::move(icon),
      l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_PAIR_NEW_DEVICE));

  views::Label* label = pair_new_device_view_->text_label();
  label->SetEnabledColor(cros_tokens::kCrosSysPrimary);
  ash::TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton2,
                                             *label);

  // The device list is a separate view because it cannot contain the "pair new
  // device" row.
  device_list_ = main_container_->AddChildView(std::make_unique<views::View>());
  device_list_->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical));
}

void BluetoothDetailedViewImpl::OnSettingsClicked() {
  CloseBubble();  // Deletes |this|.
  Shell::Get()->system_tray_model()->client()->ShowBluetoothSettings();
}

void BluetoothDetailedViewImpl::OnToggleClicked() {
  // The toggle button already has the new state after a click.
  ToggleBluetoothState(toggle_button_->GetIsOn());
}

void BluetoothDetailedViewImpl::ToggleBluetoothState(bool new_state) {
  delegate()->OnToggleClicked(new_state);

  const BluetoothSystemState new_system_state =
      new_state ? BluetoothSystemState::kEnabling
                : BluetoothSystemState::kDisabling;

  // Avoid the situation where there is a delay between the toggle becoming
  // enabled/disabled and Bluetooth becoming enabled/disabled by forcing the
  // view state to match the toggle state.
  UpdateBluetoothEnabledState(new_system_state);
}

BEGIN_METADATA(BluetoothDetailedViewImpl)
END_METADATA

}  // namespace ash