phone_hub_tray.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // 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. #include "ash/system/phonehub/phone_hub_tray.h"
  5. #include <utility>
  6. #include "ash/accessibility/accessibility_controller_impl.h"
  7. #include "ash/components/phonehub/phone_hub_manager.h"
  8. #include "ash/components/phonehub/phone_model.h"
  9. #include "ash/constants/ash_features.h"
  10. #include "ash/focus_cycler.h"
  11. #include "ash/public/cpp/system_tray_client.h"
  12. #include "ash/resources/vector_icons/vector_icons.h"
  13. #include "ash/shelf/shelf.h"
  14. #include "ash/shell.h"
  15. #include "ash/strings/grit/ash_strings.h"
  16. #include "ash/style/ash_color_provider.h"
  17. #include "ash/system/eche/eche_icon_loading_indicator_view.h"
  18. #include "ash/system/model/system_tray_model.h"
  19. #include "ash/system/phonehub/phone_hub_content_view.h"
  20. #include "ash/system/phonehub/phone_hub_metrics.h"
  21. #include "ash/system/phonehub/quick_actions_view.h"
  22. #include "ash/system/phonehub/task_continuation_view.h"
  23. #include "ash/system/phonehub/ui_constants.h"
  24. #include "ash/system/tray/system_menu_button.h"
  25. #include "ash/system/tray/tray_bubble_wrapper.h"
  26. #include "ash/system/tray/tray_constants.h"
  27. #include "ash/system/tray/tray_container.h"
  28. #include "ash/system/tray/tray_popup_utils.h"
  29. #include "ash/system/tray/tray_utils.h"
  30. #include "base/bind.h"
  31. #include "base/callback_helpers.h"
  32. #include "ui/base/l10n/l10n_util.h"
  33. #include "ui/base/resource/resource_bundle.h"
  34. #include "ui/events/event.h"
  35. #include "ui/gfx/geometry/insets.h"
  36. #include "ui/gfx/paint_vector_icon.h"
  37. #include "ui/views/border.h"
  38. #include "ui/views/controls/image_view.h"
  39. namespace ash {
  40. namespace {
  41. // Padding for tray icons (dp; the button that shows the phone_hub menu).
  42. constexpr int kTrayIconMainAxisInset = 6;
  43. constexpr int kTrayIconCrossAxisInset = 0;
  44. constexpr int kEcheIconMinSize = 24;
  45. constexpr int kIconSpacing = 12;
  46. constexpr auto kBubblePadding =
  47. gfx::Insets::TLBR(0, 0, kBubbleBottomPaddingDip, 0);
  48. } // namespace
  49. PhoneHubTray::PhoneHubTray(Shelf* shelf)
  50. : TrayBackgroundView(shelf), ui_controller_(new PhoneHubUiController()) {
  51. observed_phone_hub_ui_controller_.Observe(ui_controller_.get());
  52. observed_session_.Observe(Shell::Get()->session_controller());
  53. tray_container()->SetMargin(kTrayIconMainAxisInset, kTrayIconCrossAxisInset);
  54. // TODO(nayebi): Think about constructing the eche_icon outside of this class,
  55. // either as an input argument or being set through a setter.
  56. if (features::IsEcheSWAEnabled()) {
  57. auto eche_icon = std::make_unique<views::ImageButton>(base::BindRepeating(
  58. &PhoneHubTray::EcheIconActivated, weak_factory_.GetWeakPtr()));
  59. eche_icon->SetImageVerticalAlignment(
  60. views::ImageButton::VerticalAlignment::ALIGN_MIDDLE);
  61. eche_icon->SetImageHorizontalAlignment(
  62. views::ImageButton::HorizontalAlignment::ALIGN_CENTER);
  63. eche_icon->SetMinimumImageSize(
  64. gfx::Size(kEcheIconMinSize, kEcheIconMinSize));
  65. eche_icon->SetVisible(false);
  66. eche_loading_indicator_ = eche_icon->AddChildView(
  67. std::make_unique<EcheIconLoadingIndicatorView>(eche_icon.get()));
  68. eche_loading_indicator_->SetVisible(false);
  69. eche_icon_ = tray_container()->AddChildView(std::move(eche_icon));
  70. tray_container()->SetSpacingBetweenChildren(kIconSpacing);
  71. }
  72. auto icon = std::make_unique<views::ImageButton>(base::BindRepeating(
  73. &PhoneHubTray::PhoneHubIconActivated, weak_factory_.GetWeakPtr()));
  74. icon->SetTooltipText(
  75. l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
  76. icon->SetImageVerticalAlignment(
  77. views::ImageButton::VerticalAlignment::ALIGN_MIDDLE);
  78. icon->SetImageHorizontalAlignment(
  79. views::ImageButton::HorizontalAlignment::ALIGN_CENTER);
  80. icon_ = tray_container()->AddChildView(std::move(icon));
  81. }
  82. PhoneHubTray::~PhoneHubTray() {
  83. if (bubble_)
  84. bubble_->bubble_view()->ResetDelegate();
  85. }
  86. void PhoneHubTray::SetPhoneHubManager(
  87. phonehub::PhoneHubManager* phone_hub_manager) {
  88. ui_controller_->SetPhoneHubManager(phone_hub_manager);
  89. }
  90. void PhoneHubTray::ClickedOutsideBubble() {
  91. CloseBubble();
  92. }
  93. std::u16string PhoneHubTray::GetAccessibleNameForTray() {
  94. return l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME);
  95. }
  96. void PhoneHubTray::HandleLocaleChange() {
  97. icon_->SetTooltipText(
  98. l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_TRAY_ACCESSIBLE_NAME));
  99. }
  100. void PhoneHubTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {
  101. if (bubble_->bubble_view() == bubble_view)
  102. CloseBubble();
  103. }
  104. std::u16string PhoneHubTray::GetAccessibleNameForBubble() {
  105. return GetAccessibleNameForTray();
  106. }
  107. bool PhoneHubTray::ShouldEnableExtraKeyboardAccessibility() {
  108. return Shell::Get()->accessibility_controller()->spoken_feedback().enabled();
  109. }
  110. void PhoneHubTray::HideBubble(const TrayBubbleView* bubble_view) {
  111. HideBubbleWithView(bubble_view);
  112. }
  113. void PhoneHubTray::OnPhoneHubUiStateChanged() {
  114. UpdateVisibility();
  115. if (!bubble_)
  116. return;
  117. TrayBubbleView* bubble_view = bubble_->bubble_view();
  118. DCHECK(ui_controller_.get());
  119. std::unique_ptr<PhoneHubContentView> content_view =
  120. ui_controller_->CreateContentView(this);
  121. if (!content_view.get()) {
  122. CloseBubble();
  123. return;
  124. }
  125. if (content_view_) {
  126. // If we are already showing the same content_view, no need to remove and
  127. // update the tray.
  128. // TODO(crbug.com/1185316) : Find way to update views without work around
  129. // when same view is removed and added.
  130. if (content_view->GetID() == content_view_->GetID())
  131. return;
  132. bubble_view->RemoveChildView(content_view_);
  133. delete content_view_;
  134. }
  135. content_view_ = bubble_view->AddChildView(std::move(content_view));
  136. // Updates bubble to handle possible size change with a different child view.
  137. bubble_view->UpdateBubble();
  138. }
  139. void PhoneHubTray::OnSessionStateChanged(session_manager::SessionState state) {
  140. icon_->SetImage(views::ImageButton::STATE_NORMAL,
  141. CreateVectorIcon(kPhoneHubPhoneIcon, TrayIconColor(state)));
  142. TemporarilyDisableAnimation();
  143. }
  144. void PhoneHubTray::OnActiveUserSessionChanged(const AccountId& account_id) {
  145. TemporarilyDisableAnimation();
  146. }
  147. void PhoneHubTray::AnchorUpdated() {
  148. if (bubble_)
  149. bubble_->bubble_view()->UpdateBubble();
  150. }
  151. void PhoneHubTray::Initialize() {
  152. TrayBackgroundView::Initialize();
  153. UpdateVisibility();
  154. }
  155. void PhoneHubTray::ShowBubble() {
  156. if (bubble_)
  157. return;
  158. ui_controller_->HandleBubbleOpened();
  159. TrayBubbleView::InitParams init_params;
  160. init_params.delegate = GetWeakPtr();
  161. init_params.parent_window = GetBubbleWindowContainer();
  162. init_params.anchor_mode = TrayBubbleView::AnchorMode::kRect;
  163. init_params.anchor_rect = shelf()->GetSystemTrayAnchorRect();
  164. init_params.insets = GetTrayBubbleInsets();
  165. init_params.shelf_alignment = shelf()->alignment();
  166. init_params.preferred_width = kTrayMenuWidth;
  167. init_params.close_on_deactivate = true;
  168. init_params.translucent = true;
  169. init_params.reroute_event_handler = true;
  170. init_params.corner_radius = kTrayItemCornerRadius;
  171. TrayBubbleView* bubble_view = new TrayBubbleView(init_params);
  172. bubble_view->SetBorder(views::CreateEmptyBorder(kBubblePadding));
  173. // Creates header view on top for displaying phone status and settings icon.
  174. auto phone_status = ui_controller_->CreateStatusHeaderView(this);
  175. phone_status_view_ = phone_status.get();
  176. DCHECK(phone_status_view_);
  177. bubble_view->AddChildView(std::move(phone_status));
  178. // Other contents, i.e. the connected view and the interstitial views,
  179. // will be positioned underneath the phone status view and updated based
  180. // on the current mode.
  181. auto content_view = ui_controller_->CreateContentView(this);
  182. content_view_ = content_view.get();
  183. if (!content_view_) {
  184. CloseBubble();
  185. return;
  186. }
  187. bubble_view->AddChildView(std::move(content_view));
  188. bubble_ = std::make_unique<TrayBubbleWrapper>(this, bubble_view);
  189. SetIsActive(true);
  190. phone_hub_metrics::LogScreenOnBubbleOpen(
  191. content_view_->GetScreenForMetrics());
  192. }
  193. bool PhoneHubTray::PerformAction(const ui::Event& event) {
  194. // By default, if the individual buttons did not handle the event consider it
  195. // as a phone hub icon event.
  196. PhoneHubIconActivated(event);
  197. return true;
  198. }
  199. TrayBubbleView* PhoneHubTray::GetBubbleView() {
  200. return bubble_ ? bubble_->bubble_view() : nullptr;
  201. }
  202. views::Widget* PhoneHubTray::GetBubbleWidget() const {
  203. return bubble_ ? bubble_->GetBubbleWidget() : nullptr;
  204. }
  205. const char* PhoneHubTray::GetClassName() const {
  206. return "PhoneHubTray";
  207. }
  208. void PhoneHubTray::OnThemeChanged() {
  209. TrayBackgroundView::OnThemeChanged();
  210. icon_->SetImage(
  211. views::ImageButton::STATE_NORMAL,
  212. CreateVectorIcon(
  213. kPhoneHubPhoneIcon,
  214. TrayIconColor(
  215. Shell::Get()->session_controller()->GetSessionState())));
  216. }
  217. bool PhoneHubTray::CanOpenConnectedDeviceSettings() {
  218. return TrayPopupUtils::CanOpenWebUISettings();
  219. }
  220. void PhoneHubTray::OpenConnectedDevicesSettings() {
  221. DCHECK(content_view_);
  222. phone_hub_metrics::LogScreenOnSettingsButtonClicked(
  223. content_view_->GetScreenForMetrics());
  224. DCHECK(CanOpenConnectedDeviceSettings());
  225. Shell::Get()->system_tray_model()->client()->ShowConnectedDevicesSettings();
  226. }
  227. void PhoneHubTray::HideStatusHeaderView() {
  228. if (!phone_status_view_)
  229. return;
  230. phone_status_view_->SetVisible(false);
  231. bubble_->bubble_view()->UpdateBubble();
  232. }
  233. void PhoneHubTray::SetEcheIconActivationCallback(
  234. base::RepeatingCallback<bool(const ui::Event&)> callback) {
  235. eche_icon_callback_ = std::move(callback);
  236. }
  237. void PhoneHubTray::CloseBubble() {
  238. if (!bubble_)
  239. return;
  240. auto* bubble_view = bubble_->GetBubbleView();
  241. if (bubble_view)
  242. bubble_view->ResetDelegate();
  243. if (content_view_) {
  244. phone_hub_metrics::LogScreenOnBubbleClose(
  245. content_view_->GetScreenForMetrics());
  246. content_view_->OnBubbleClose();
  247. content_view_ = nullptr;
  248. }
  249. bubble_.reset();
  250. SetIsActive(false);
  251. shelf()->UpdateAutoHideState();
  252. }
  253. void PhoneHubTray::UpdateVisibility() {
  254. DCHECK(ui_controller_.get());
  255. auto ui_state = ui_controller_->ui_state();
  256. SetVisiblePreferred(ui_state != PhoneHubUiController::UiState::kHidden);
  257. }
  258. void PhoneHubTray::TemporarilyDisableAnimation() {
  259. base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
  260. FROM_HERE, DisableShowAnimation().Release(), base::Seconds(5));
  261. }
  262. void PhoneHubTray::EcheIconActivated(const ui::Event& event) {
  263. eche_icon_callback_.Run(event);
  264. }
  265. void PhoneHubTray::PhoneHubIconActivated(const ui::Event& event) {
  266. // Simply toggle between visible/invisibvle
  267. if (bubble_ && bubble_->bubble_view()->GetVisible()) {
  268. CloseBubble();
  269. } else {
  270. ShowBubble();
  271. }
  272. }
  273. } // namespace ash