[go: nahoru, domu]

blob: ac9632efacbfb63ad1ef1ee54d896af7f4ef3929 [file] [log] [blame]
Tim Songf7f00b02020-09-03 20:57:001// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/system/phonehub/phone_hub_tray.h"
6
7#include "ash/accessibility/accessibility_controller_impl.h"
Meilin Wanga35e0ef2020-10-13 23:04:558#include "ash/focus_cycler.h"
Tim Songc935ade2020-10-27 18:26:519#include "ash/public/cpp/system_tray_client.h"
Tim Songf7f00b02020-09-03 20:57:0010#include "ash/resources/vector_icons/vector_icons.h"
11#include "ash/shelf/shelf.h"
12#include "ash/shell.h"
13#include "ash/strings/grit/ash_strings.h"
14#include "ash/style/ash_color_provider.h"
Tim Songc935ade2020-10-27 18:26:5115#include "ash/system/model/system_tray_model.h"
Meilin Wangd626fde2020-10-23 17:53:3616#include "ash/system/phonehub/phone_hub_content_view.h"
Tim Song947932a2020-10-31 00:38:4517#include "ash/system/phonehub/phone_hub_metrics.h"
Andre Led52e4672020-09-09 19:24:3818#include "ash/system/phonehub/quick_actions_view.h"
Andre Leed1afef2020-09-11 01:06:5519#include "ash/system/phonehub/task_continuation_view.h"
Meilin Wangc14a36f2020-10-31 00:17:5120#include "ash/system/phonehub/ui_constants.h"
Tim Songf7f00b02020-09-03 20:57:0021#include "ash/system/tray/system_menu_button.h"
22#include "ash/system/tray/tray_bubble_wrapper.h"
23#include "ash/system/tray/tray_constants.h"
24#include "ash/system/tray/tray_container.h"
Tim Songf7f00b02020-09-03 20:57:0025#include "ash/system/tray/tray_popup_utils.h"
26#include "ash/system/tray/tray_utils.h"
27#include "base/bind.h"
Tim Songb32e7812020-09-11 22:42:1928#include "chromeos/components/phonehub/phone_hub_manager.h"
Andre Le6619bb722020-09-17 17:49:1129#include "chromeos/components/phonehub/phone_model.h"
Tim Songf7f00b02020-09-03 20:57:0030#include "ui/base/l10n/l10n_util.h"
31#include "ui/base/resource/resource_bundle.h"
32#include "ui/gfx/geometry/insets.h"
33#include "ui/gfx/paint_vector_icon.h"
34#include "ui/views/controls/image_view.h"
Tim Songf7f00b02020-09-03 20:57:0035
36namespace ash {
37
38namespace {
39
40// Padding for tray icon (dp; the button that shows the phone_hub menu).
Jiaming Chenge9eeb032021-03-04 20:21:1041constexpr int kTrayIconMainAxisInset = 6;
Tim Songf7f00b02020-09-03 20:57:0042constexpr int kTrayIconCrossAxisInset = 0;
43
Meilin Wangc14a36f2020-10-31 00:17:5144constexpr gfx::Insets kBubblePadding(0, 0, kBubbleBottomPaddingDip, 0);
Andre Le4fb5e912020-09-09 16:30:0345
Tim Songf7f00b02020-09-03 20:57:0046} // namespace
47
Tim Song3049a952020-09-29 06:49:5648PhoneHubTray::PhoneHubTray(Shelf* shelf)
49 : TrayBackgroundView(shelf), ui_controller_(new PhoneHubUiController()) {
Sigurdur Asgeirssone592c482021-01-28 16:17:2950 observed_phone_hub_ui_controller_.Observe(ui_controller_.get());
Tim Song3049a952020-09-29 06:49:5651
Tim Songf7f00b02020-09-03 20:57:0052 auto icon = std::make_unique<views::ImageView>();
Allen Bauerd9daac112020-09-15 21:54:0453 icon->SetTooltipText(
Tim Songf7f00b02020-09-03 20:57:0054 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
55 icon->SetImage(CreateVectorIcon(
Andre Leb2a5ccb2020-10-26 17:50:1656 kPhoneHubPhoneIcon,
Tim Songf7f00b02020-09-03 20:57:0057 AshColorProvider::Get()->GetContentLayerColor(
minch5847c6f2020-09-08 01:13:1758 AshColorProvider::ContentLayerType::kIconColorPrimary)));
Tim Songf7f00b02020-09-03 20:57:0059
60 tray_container()->SetMargin(kTrayIconMainAxisInset, kTrayIconCrossAxisInset);
61 icon_ = tray_container()->AddChildView(std::move(icon));
62}
63
64PhoneHubTray::~PhoneHubTray() {
65 if (bubble_)
66 bubble_->bubble_view()->ResetDelegate();
Tim Songb32e7812020-09-11 22:42:1967}
68
69void PhoneHubTray::SetPhoneHubManager(
70 chromeos::phonehub::PhoneHubManager* phone_hub_manager) {
Tim Song3049a952020-09-29 06:49:5671 ui_controller_->SetPhoneHubManager(phone_hub_manager);
Tim Songf7f00b02020-09-03 20:57:0072}
73
74void PhoneHubTray::ClickedOutsideBubble() {
75 CloseBubble();
76}
77
Jan Wilken Dörrie85285b02021-03-11 23:38:4778std::u16string PhoneHubTray::GetAccessibleNameForTray() {
Tim Songf7f00b02020-09-03 20:57:0079 return l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME);
80}
81
82void PhoneHubTray::HandleLocaleChange() {
Allen Bauerd9daac112020-09-15 21:54:0483 icon_->SetTooltipText(
Tim Songf7f00b02020-09-03 20:57:0084 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
85}
86
87void PhoneHubTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {
88 if (bubble_->bubble_view() == bubble_view)
89 CloseBubble();
90}
91
Jan Wilken Dörrie85285b02021-03-11 23:38:4792std::u16string PhoneHubTray::GetAccessibleNameForBubble() {
Tim Songf7f00b02020-09-03 20:57:0093 return GetAccessibleNameForTray();
94}
95
96bool PhoneHubTray::ShouldEnableExtraKeyboardAccessibility() {
Anatoliy Potapchuke3687582020-09-08 22:50:3297 return Shell::Get()->accessibility_controller()->spoken_feedback().enabled();
Tim Songf7f00b02020-09-03 20:57:0098}
99
100void PhoneHubTray::HideBubble(const TrayBubbleView* bubble_view) {
101 HideBubbleWithView(bubble_view);
102}
103
Tim Song3049a952020-09-29 06:49:56104void PhoneHubTray::OnPhoneHubUiStateChanged() {
105 UpdateVisibility();
106
107 if (!bubble_)
108 return;
109 TrayBubbleView* bubble_view = bubble_->bubble_view();
110
111 DCHECK(ui_controller_.get());
Meilin Wangd626fde2020-10-23 17:53:36112 std::unique_ptr<PhoneHubContentView> content_view =
Meilin Wang65319732020-11-06 20:03:18113 ui_controller_->CreateContentView(this);
Tim Song3049a952020-09-29 06:49:56114 if (!content_view.get()) {
115 CloseBubble();
116 return;
117 }
118
Juliet Levesque0300e11e9b2021-03-09 15:50:54119 if (content_view_) {
Juliet Levesque92bc5b22021-03-11 20:54:26120 // If we are already showing the same content_view, no need to remove and
121 // update the tray.
122 // TODO(crbug.com/1185316) : Find way to update views without work around
123 // when same view is removed and added.
124 if (content_view->GetID() == content_view_->GetID())
125 return;
126
Tim Song3049a952020-09-29 06:49:56127 bubble_view->RemoveChildView(content_view_);
Juliet Levesque0300e11e9b2021-03-09 15:50:54128 delete content_view_;
129 }
Juliet Levesque92bc5b22021-03-11 20:54:26130 content_view_ = bubble_view->AddChildView(std::move(content_view));
Meilin Wang4f250862020-10-02 23:40:19131
132 // Updates bubble to handle possible size change with a different child view.
133 bubble_view->UpdateBubble();
Tim Song3049a952020-09-29 06:49:56134}
135
Tim Songf7f00b02020-09-03 20:57:00136void PhoneHubTray::AnchorUpdated() {
137 if (bubble_)
138 bubble_->bubble_view()->UpdateBubble();
139}
140
141void PhoneHubTray::Initialize() {
142 TrayBackgroundView::Initialize();
143 UpdateVisibility();
144}
145
Ahmed Mehfooz15cebdd2021-03-03 01:07:07146void PhoneHubTray::ShowBubble() {
Tim Songf7f00b02020-09-03 20:57:00147 if (bubble_)
148 return;
149
Kyle Horimotoae02ab32020-12-11 00:35:47150 ui_controller_->HandleBubbleOpened();
Andre Leb8d8ec52020-11-02 23:11:46151
Tim Songf7f00b02020-09-03 20:57:00152 TrayBubbleView::InitParams init_params;
153 init_params.delegate = this;
154 init_params.parent_window = GetBubbleWindowContainer();
Regan Hsua5e24272020-12-22 20:38:28155 init_params.anchor_mode = TrayBubbleView::AnchorMode::kRect;
156 init_params.anchor_rect = shelf()->GetSystemTrayAnchorRect();
157 init_params.insets = GetTrayBubbleInsets();
Tim Songf7f00b02020-09-03 20:57:00158 init_params.shelf_alignment = shelf()->alignment();
Meilin Wangc14a36f2020-10-31 00:17:51159 init_params.preferred_width = kTrayMenuWidth;
Tim Songf7f00b02020-09-03 20:57:00160 init_params.close_on_deactivate = true;
161 init_params.has_shadow = false;
162 init_params.translucent = true;
Ahmed Mehfooz15cebdd2021-03-03 01:07:07163 init_params.reroute_event_handler = true;
Tim Songf7f00b02020-09-03 20:57:00164 init_params.corner_radius = kTrayItemCornerRadius;
Tim Songf7f00b02020-09-03 20:57:00165
166 TrayBubbleView* bubble_view = new TrayBubbleView(init_params);
Andre Le4fb5e912020-09-09 16:30:03167 bubble_view->SetBorder(views::CreateEmptyBorder(kBubblePadding));
Tim Songf7f00b02020-09-03 20:57:00168
Meilin Wang65319732020-11-06 20:03:18169 // Creates header view on top for displaying phone status and settings icon.
170 auto phone_status = ui_controller_->CreateStatusHeaderView(this);
171 phone_status_view_ = phone_status.get();
172 DCHECK(phone_status_view_);
173 bubble_view->AddChildView(std::move(phone_status));
Meilin Wang50d8a7292020-09-25 23:52:33174
175 // Other contents, i.e. the connected view and the interstitial views,
176 // will be positioned underneath the phone status view and updated based
177 // on the current mode.
Meilin Wang65319732020-11-06 20:03:18178 auto content_view = ui_controller_->CreateContentView(this);
Tim Song3049a952020-09-29 06:49:56179 content_view_ = content_view.get();
Tim Song947932a2020-10-31 00:38:45180 DCHECK(content_view_);
181 bubble_view->AddChildView(std::move(content_view));
Tim Songf7f00b02020-09-03 20:57:00182
183 bubble_ = std::make_unique<TrayBubbleWrapper>(this, bubble_view,
184 false /* is_persistent */);
Tim Song3049a952020-09-29 06:49:56185
Tim Songf7f00b02020-09-03 20:57:00186 SetIsActive(true);
Meilin Wanga35e0ef2020-10-13 23:04:55187
Tim Song947932a2020-10-31 00:38:45188 phone_hub_metrics::LogScreenOnBubbleOpen(
189 content_view_->GetScreenForMetrics());
Tim Songf7f00b02020-09-03 20:57:00190}
191
192TrayBubbleView* PhoneHubTray::GetBubbleView() {
193 return bubble_ ? bubble_->bubble_view() : nullptr;
194}
195
Ahmed Mehfooz15cebdd2021-03-03 01:07:07196views::Widget* PhoneHubTray::GetBubbleWidget() const {
197 return bubble_ ? bubble_->GetBubbleWidget() : nullptr;
198}
199
Tim Songf7f00b02020-09-03 20:57:00200const char* PhoneHubTray::GetClassName() const {
201 return "PhoneHubTray";
202}
203
Tim Songc935ade2020-10-27 18:26:51204bool PhoneHubTray::CanOpenConnectedDeviceSettings() {
205 return TrayPopupUtils::CanOpenWebUISettings();
206}
207
208void PhoneHubTray::OpenConnectedDevicesSettings() {
Tim Song947932a2020-10-31 00:38:45209 DCHECK(content_view_);
210 phone_hub_metrics::LogScreenOnSettingsButtonClicked(
211 content_view_->GetScreenForMetrics());
212
Tim Songc935ade2020-10-27 18:26:51213 DCHECK(CanOpenConnectedDeviceSettings());
214 Shell::Get()->system_tray_model()->client()->ShowConnectedDevicesSettings();
215}
216
Meilin Wang65319732020-11-06 20:03:18217void PhoneHubTray::HideStatusHeaderView() {
218 if (!phone_status_view_)
219 return;
220
221 phone_status_view_->SetVisible(false);
222 bubble_->bubble_view()->UpdateBubble();
223}
224
Tim Songf7f00b02020-09-03 20:57:00225void PhoneHubTray::CloseBubble() {
Tim Song947932a2020-10-31 00:38:45226 if (!bubble_)
227 return;
228
229 DCHECK(content_view_);
230 phone_hub_metrics::LogScreenOnBubbleClose(
231 content_view_->GetScreenForMetrics());
232
233 content_view_->OnBubbleClose();
Tim Song3049a952020-09-29 06:49:56234 content_view_ = nullptr;
Tim Songf7f00b02020-09-03 20:57:00235 bubble_.reset();
236 SetIsActive(false);
237 shelf()->UpdateAutoHideState();
238}
239
240void PhoneHubTray::UpdateVisibility() {
Tim Song3049a952020-09-29 06:49:56241 DCHECK(ui_controller_.get());
242 auto ui_state = ui_controller_->ui_state();
243 SetVisiblePreferred(ui_state != PhoneHubUiController::UiState::kHidden);
Tim Songf7f00b02020-09-03 20:57:00244}
245
246} // namespace ash