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
|
// 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/accessibility/dictation_button_tray.h"
#include "ash/accessibility/accessibility_controller.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/metrics/user_metrics_recorder.h"
#include "ash/public/cpp/accessibility_controller_enums.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/system/progress_indicator/progress_indicator.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/tray/tray_utils.h"
#include "components/prefs/pref_service.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/base/ime/text_input_client.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/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/image_view.h"
namespace ash {
namespace {
// Helper function that creates an image for the dictation icon.
// |active| means Dictation is actively listening for speech. The icon
// changes to an "on" icon from "off" when Dictation is listening.
// |enabled| indicates whether the tray button is enabled, i.e. clickable.
// A secondary color is used to indicate the icon is not enabled.
ui::ImageModel GetIconImage(bool active, bool enabled) {
// The color will change based on whether this tray is active or not.
ui::ColorId color_id =
enabled ? (active ? cros_tokens::kCrosSysSystemOnPrimaryContainer
: cros_tokens::kCrosSysOnSurface)
: cros_tokens::kCrosSysSecondary;
return active
? ui::ImageModel::FromVectorIcon(kDictationOnNewuiIcon, color_id)
: ui::ImageModel::FromVectorIcon(kDictationOffNewuiIcon, color_id);
}
bool IsDictationActive() {
return Shell::Get()->accessibility_controller()->dictation_active();
}
} // namespace
DictationButtonTray::DictationButtonTray(
Shelf* shelf,
TrayBackgroundViewCatalogName catalog_name)
: TrayBackgroundView(shelf, catalog_name), download_progress_(0) {
SetCallback(base::BindRepeating(
&DictationButtonTray::OnDictationButtonPressed, base::Unretained(this)));
Shell* shell = Shell::Get();
ui::TextInputClient* client =
shell->window_tree_host_manager()->input_method()->GetTextInputClient();
in_text_input_ =
(client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE);
// If a view that accepts text input is focused, make the tray enabled (i.e.
// clickable). However, at this point the dictation is not active (i.e.
// dictation is not listening to speech).
SetEnabled(in_text_input_);
SetIsActive(false);
const ui::ImageModel icon_image =
GetIconImage(/*active=*/false, /*enabled=*/GetEnabled());
const int vertical_padding = (kTrayItemSize - icon_image.Size().height()) / 2;
const int horizontal_padding =
(kTrayItemSize - icon_image.Size().height()) / 2;
auto icon = std::make_unique<views::ImageView>();
icon->SetImage(icon_image);
icon->SetBorder(views::CreateEmptyBorder(
gfx::Insets::VH(vertical_padding, horizontal_padding)));
icon->SetTooltipText(
l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCESSIBILITY_DICTATION));
icon_ = tray_container()->AddChildView(std::move(icon));
shell->AddShellObserver(this);
shell->accessibility_controller()->AddObserver(this);
shell->session_controller()->AddObserver(this);
GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_ASH_DICTATION_BUTTON_ACCESSIBLE_NAME));
}
DictationButtonTray::~DictationButtonTray() {
// This may be called during shutdown in which case some of the
// ash objects may already be destroyed.
Shell* shell = Shell::Get();
if (!shell) {
return;
}
shell->RemoveShellObserver(this);
auto* accessibility_controller = shell->accessibility_controller();
if (accessibility_controller) {
accessibility_controller->RemoveObserver(this);
}
auto* session_controller = shell->session_controller();
if (session_controller) {
session_controller->RemoveObserver(this);
}
input_method_observation_.Reset();
}
void DictationButtonTray::OnDictationStarted() {
UpdateStateAndIcon(/*is_dictation_active=*/true, GetEnabled());
}
void DictationButtonTray::OnDictationEnded() {
UpdateStateAndIcon(/*is_dictation_active=*/false, GetEnabled());
}
void DictationButtonTray::OnAccessibilityStatusChanged() {
UpdateVisibility();
CheckDictationStatusAndUpdateIcon();
}
void DictationButtonTray::OnSessionStateChanged(
session_manager::SessionState state) {
CheckDictationStatusAndUpdateIcon();
}
void DictationButtonTray::Initialize() {
TrayBackgroundView::Initialize();
UpdateVisibility();
}
void DictationButtonTray::ClickedOutsideBubble(const ui::LocatedEvent& event) {}
void DictationButtonTray::UpdateTrayItemColor(bool is_active) {
if (progress_indicator_) {
progress_indicator_->SetColorId(
is_active ? cros_tokens::kCrosSysSystemOnPrimaryContainer
: cros_tokens::kCrosSysPrimary);
}
}
void DictationButtonTray::HandleLocaleChange() {
icon_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCESSIBILITY_DICTATION));
}
void DictationButtonTray::HideBubbleWithView(
const TrayBubbleView* bubble_view) {
// This class has no bubbles to hide.
}
void DictationButtonTray::OnThemeChanged() {
TrayBackgroundView::OnThemeChanged();
if (progress_indicator_)
progress_indicator_->InvalidateLayer();
}
void DictationButtonTray::Layout(PassKey) {
LayoutSuperclass<TrayBackgroundView>(this);
UpdateProgressIndicatorBounds();
}
void DictationButtonTray::HideBubble(const TrayBubbleView* bubble_view) {
// This class has no bubbles to hide.
}
void DictationButtonTray::OnCaretBoundsChanged(
const ui::TextInputClient* client) {
TextInputChanged(client);
}
void DictationButtonTray::OnTextInputStateChanged(
const ui::TextInputClient* client) {
TextInputChanged(client);
}
void DictationButtonTray::UpdateOnSpeechRecognitionDownloadChanged(
int download_progress) {
if (!visible_preferred())
return;
const bool download_in_progress =
download_progress > 0 && download_progress < 100;
const bool is_dictation_enabled = !download_in_progress && in_text_input_;
UpdateStateAndIcon(IsDictationActive(), is_dictation_enabled);
icon_->SetTooltipText(l10n_util::GetStringUTF16(
download_in_progress
? IDS_ASH_ACCESSIBILITY_DICTATION_BUTTON_TOOLTIP_SODA_DOWNLOADING
: IDS_ASH_STATUS_TRAY_ACCESSIBILITY_DICTATION));
// Progress indicator.
download_progress_ = download_progress;
if (!progress_indicator_) {
// A progress indicator that is only visible when a SODA download is
// in-progress and a subscription to receive notification of progress
// changed events.
progress_indicator_ =
ProgressIndicator::CreateDefaultInstance(base::BindRepeating(
[](DictationButtonTray* tray) -> std::optional<float> {
// If download is in-progress, return the progress as a decimal.
// Otherwise, the progress indicator shouldn't be painted.
const int progress = tray->download_progress();
return (progress > 0 && progress < 100)
? progress / 100.f
: ProgressIndicator::kProgressComplete;
},
base::Unretained(this)));
progress_indicator_->SetInnerIconVisible(false);
layer()->Add(progress_indicator_->CreateLayer(base::BindRepeating(
[](const DictationButtonTray* self, ui::ColorId color_id) {
return self->GetColorProvider()->GetColor(color_id);
},
base::Unretained(this))));
UpdateProgressIndicatorBounds();
UpdateTrayItemColor(is_active());
}
progress_indicator_->InvalidateLayer();
}
void DictationButtonTray::OnDictationButtonPressed(const ui::Event& event) {
Shell::Get()->accessibility_controller()->ToggleDictationFromSource(
DictationToggleSource::kButton);
CheckDictationStatusAndUpdateIcon();
}
void DictationButtonTray::UpdateProgressIndicatorBounds() {
if (progress_indicator_)
progress_indicator_->layer()->SetBounds(GetBackgroundBounds());
}
void DictationButtonTray::UpdateVisibility() {
const bool is_visible =
Shell::Get()->accessibility_controller()->dictation().enabled();
if (is_visible && !input_method_observation_.IsObserving()) {
input_method_observation_.Observe(
Shell::Get()->window_tree_host_manager()->input_method());
} else if (!is_visible) {
input_method_observation_.Reset();
}
SetVisiblePreferred(is_visible);
}
void DictationButtonTray::UpdateStateAndIcon(bool is_dictation_active,
bool is_dictation_enabled) {
const bool should_update_icon = is_active() != is_dictation_active ||
GetEnabled() != is_dictation_enabled;
SetIsActive(is_dictation_active);
SetEnabled(is_dictation_enabled);
if (should_update_icon) {
icon_->SetImage(GetIconImage(is_dictation_active, is_dictation_enabled));
}
}
void DictationButtonTray::CheckDictationStatusAndUpdateIcon() {
UpdateStateAndIcon(IsDictationActive(), GetEnabled());
}
void DictationButtonTray::TextInputChanged(const ui::TextInputClient* client) {
in_text_input_ =
client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE;
const bool is_dictation_enabled =
(download_progress_ <= 0 || download_progress_ >= 100) && in_text_input_;
UpdateStateAndIcon(IsDictationActive(), is_dictation_enabled);
}
BEGIN_METADATA(DictationButtonTray)
END_METADATA
} // namespace ash
|