unified_system_tray_bubble.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright 2018 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/unified/unified_system_tray_bubble.h"
  5. #include "ash/app_list/app_list_controller_impl.h"
  6. #include "ash/bubble/bubble_constants.h"
  7. #include "ash/shelf/shelf.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/message_center/unified_message_center_bubble.h"
  10. #include "ash/system/status_area_widget.h"
  11. #include "ash/system/time/calendar_metrics.h"
  12. #include "ash/system/tray/tray_background_view.h"
  13. #include "ash/system/tray/tray_constants.h"
  14. #include "ash/system/tray/tray_event_filter.h"
  15. #include "ash/system/tray/tray_utils.h"
  16. #include "ash/system/unified/unified_system_tray.h"
  17. #include "ash/system/unified/unified_system_tray_controller.h"
  18. #include "ash/system/unified/unified_system_tray_view.h"
  19. #include "ash/wm/container_finder.h"
  20. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  21. #include "ash/wm/work_area_insets.h"
  22. #include "base/metrics/histogram_macros.h"
  23. #include "ui/aura/window.h"
  24. #include "ui/compositor/layer.h"
  25. #include "ui/events/event.h"
  26. #include "ui/wm/core/window_util.h"
  27. #include "ui/wm/public/activation_client.h"
  28. namespace ash {
  29. UnifiedSystemTrayBubble::UnifiedSystemTrayBubble(UnifiedSystemTray* tray)
  30. : controller_(std::make_unique<UnifiedSystemTrayController>(tray->model(),
  31. this,
  32. tray)),
  33. tray_(tray) {
  34. time_opened_ = base::TimeTicks::Now();
  35. TrayBubbleView::InitParams init_params;
  36. init_params.shelf_alignment = tray_->shelf()->alignment();
  37. init_params.preferred_width = kTrayMenuWidth;
  38. init_params.delegate = tray->GetWeakPtr();
  39. init_params.parent_window = tray->GetBubbleWindowContainer();
  40. init_params.anchor_view = nullptr;
  41. init_params.anchor_mode = TrayBubbleView::AnchorMode::kRect;
  42. init_params.anchor_rect = tray->shelf()->GetSystemTrayAnchorRect();
  43. init_params.insets = GetTrayBubbleInsets();
  44. init_params.close_on_deactivate = false;
  45. init_params.reroute_event_handler = true;
  46. init_params.translucent = true;
  47. bubble_view_ = new TrayBubbleView(init_params);
  48. unified_view_ = controller_->CreateView();
  49. time_to_click_recorder_ =
  50. std::make_unique<TimeToClickRecorder>(this, unified_view_);
  51. int max_height = CalculateMaxHeight();
  52. unified_view_->SetMaxHeight(max_height);
  53. bubble_view_->SetMaxHeight(max_height);
  54. controller_->ResetToCollapsedIfRequired();
  55. bubble_view_->AddChildView(unified_view_);
  56. bubble_widget_ = views::BubbleDialogDelegateView::CreateBubble(bubble_view_);
  57. bubble_widget_->AddObserver(this);
  58. TrayBackgroundView::InitializeBubbleAnimations(bubble_widget_);
  59. bubble_view_->InitializeAndShowBubble();
  60. // Notify accessibility features that the status tray has opened.
  61. NotifyAccessibilityEvent(ax::mojom::Event::kShow, true);
  62. // Explicitly close the app list in clamshell mode.
  63. if (!Shell::Get()->tablet_mode_controller()->InTabletMode())
  64. Shell::Get()->app_list_controller()->DismissAppList();
  65. }
  66. UnifiedSystemTrayBubble::~UnifiedSystemTrayBubble() {
  67. if (controller_->showing_calendar_view())
  68. tray_->NotifyLeavingCalendarView();
  69. Shell::Get()->activation_client()->RemoveObserver(this);
  70. if (Shell::Get()->tablet_mode_controller())
  71. Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
  72. tray_->tray_event_filter()->RemoveBubble(this);
  73. tray_->shelf()->RemoveObserver(this);
  74. // Unified view children depend on `controller_` which is about to go away.
  75. // Remove child views synchronously to ensure they don't try to access
  76. // `controller_` after `this` goes out of scope.
  77. bubble_view_->RemoveAllChildViews();
  78. bubble_view_->ResetDelegate();
  79. if (bubble_widget_) {
  80. bubble_widget_->RemoveObserver(this);
  81. bubble_widget_->Close();
  82. }
  83. CHECK(!IsInObserverList());
  84. }
  85. void UnifiedSystemTrayBubble::InitializeObservers() {
  86. tray_->tray_event_filter()->AddBubble(this);
  87. tray_->shelf()->AddObserver(this);
  88. Shell::Get()->tablet_mode_controller()->AddObserver(this);
  89. Shell::Get()->activation_client()->AddObserver(this);
  90. }
  91. gfx::Rect UnifiedSystemTrayBubble::GetBoundsInScreen() const {
  92. DCHECK(bubble_view_);
  93. return bubble_view_->GetBoundsInScreen();
  94. }
  95. bool UnifiedSystemTrayBubble::IsBubbleActive() const {
  96. return bubble_widget_ && bubble_widget_->IsActive();
  97. }
  98. void UnifiedSystemTrayBubble::EnsureCollapsed() {
  99. if (!bubble_widget_)
  100. return;
  101. DCHECK(unified_view_);
  102. DCHECK(controller_);
  103. controller_->EnsureCollapsed();
  104. }
  105. void UnifiedSystemTrayBubble::EnsureExpanded() {
  106. if (!bubble_widget_)
  107. return;
  108. DCHECK(unified_view_);
  109. DCHECK(controller_);
  110. controller_->EnsureExpanded();
  111. }
  112. void UnifiedSystemTrayBubble::CollapseWithoutAnimating() {
  113. if (!bubble_widget_)
  114. return;
  115. DCHECK(unified_view_);
  116. DCHECK(controller_);
  117. controller_->CollapseWithoutAnimating();
  118. }
  119. void UnifiedSystemTrayBubble::CollapseMessageCenter() {
  120. tray_->CollapseMessageCenter();
  121. }
  122. void UnifiedSystemTrayBubble::ExpandMessageCenter() {
  123. tray_->ExpandMessageCenter();
  124. }
  125. void UnifiedSystemTrayBubble::ShowAudioDetailedView() {
  126. if (!bubble_widget_)
  127. return;
  128. DCHECK(unified_view_);
  129. DCHECK(controller_);
  130. controller_->ShowAudioDetailedView();
  131. }
  132. void UnifiedSystemTrayBubble::ShowCalendarView(
  133. calendar_metrics::CalendarViewShowSource show_source,
  134. calendar_metrics::CalendarEventSource event_source) {
  135. if (!bubble_widget_)
  136. return;
  137. if (event_source == calendar_metrics::CalendarEventSource::kKeyboard) {
  138. bubble_view_->SetCanActivate(true);
  139. bubble_widget_->Activate();
  140. }
  141. DCHECK(unified_view_);
  142. DCHECK(controller_);
  143. controller_->ShowCalendarView(show_source, event_source);
  144. }
  145. void UnifiedSystemTrayBubble::ShowNetworkDetailedView(bool force) {
  146. if (!bubble_widget_)
  147. return;
  148. DCHECK(unified_view_);
  149. DCHECK(controller_);
  150. controller_->ShowNetworkDetailedView(force);
  151. }
  152. void UnifiedSystemTrayBubble::UpdateBubble() {
  153. if (!bubble_widget_)
  154. return;
  155. DCHECK(bubble_view_);
  156. bubble_view_->UpdateBubble();
  157. }
  158. TrayBackgroundView* UnifiedSystemTrayBubble::GetTray() const {
  159. return tray_;
  160. }
  161. TrayBubbleView* UnifiedSystemTrayBubble::GetBubbleView() const {
  162. return bubble_view_;
  163. }
  164. views::Widget* UnifiedSystemTrayBubble::GetBubbleWidget() const {
  165. return bubble_widget_;
  166. }
  167. int UnifiedSystemTrayBubble::GetCurrentTrayHeight() const {
  168. return unified_view_->GetCurrentHeight();
  169. }
  170. int UnifiedSystemTrayBubble::CalculateMaxHeight() const {
  171. // We use the system tray anchor rect's bottom position to calculate the free
  172. // space height. Here 'GetSystemTrayAnchorRect' gets the rect that those
  173. // bubble views will be anchored. The calculation of this rect has considered
  174. // the position of the tray (bottom, left, right), the status of the tray
  175. // (tray_->is_active()), etc.
  176. int bottom = tray_->shelf()->GetSystemTrayAnchorRect().bottom();
  177. WorkAreaInsets* work_area =
  178. WorkAreaInsets::ForWindow(tray_->shelf()->GetWindow()->GetRootWindow());
  179. int free_space_height_above_anchor =
  180. bottom - work_area->user_work_area_bounds().y();
  181. return free_space_height_above_anchor - kBubbleMenuPadding * 2;
  182. }
  183. bool UnifiedSystemTrayBubble::FocusOut(bool reverse) {
  184. return tray_->FocusMessageCenter(reverse);
  185. }
  186. void UnifiedSystemTrayBubble::FocusEntered(bool reverse) {
  187. unified_view_->FocusEntered(reverse);
  188. }
  189. void UnifiedSystemTrayBubble::OnMessageCenterActivated() {
  190. // When the message center is activated, we no longer need to reroute key
  191. // events to this bubble. Otherwise, we interfere with notifications that may
  192. // require key input like inline replies. See crbug.com/1040738.
  193. bubble_view_->StopReroutingEvents();
  194. }
  195. void UnifiedSystemTrayBubble::OnDisplayConfigurationChanged() {
  196. UpdateBubbleBounds();
  197. }
  198. void UnifiedSystemTrayBubble::OnWidgetDestroying(views::Widget* widget) {
  199. CHECK_EQ(bubble_widget_, widget);
  200. bubble_widget_->RemoveObserver(this);
  201. bubble_widget_ = nullptr;
  202. // `tray_->CloseBubble()` will delete `this`.
  203. tray_->CloseBubble();
  204. }
  205. void UnifiedSystemTrayBubble::OnWindowActivated(ActivationReason reason,
  206. aura::Window* gained_active,
  207. aura::Window* lost_active) {
  208. if (!gained_active || !bubble_widget_)
  209. return;
  210. // Check for the CloseBubble() lock.
  211. if (!TrayBackgroundView::ShouldCloseBubbleOnWindowActivated())
  212. return;
  213. // Don't close the bubble if a transient child is gaining or losing
  214. // activation.
  215. if (bubble_widget_ == views::Widget::GetWidgetForNativeView(gained_active) ||
  216. ::wm::HasTransientAncestor(gained_active,
  217. bubble_widget_->GetNativeWindow()) ||
  218. (lost_active && ::wm::HasTransientAncestor(
  219. lost_active, bubble_widget_->GetNativeWindow()))) {
  220. return;
  221. }
  222. // Don't close the bubble if the message center is gaining activation.
  223. if (tray_->IsMessageCenterBubbleShown()) {
  224. views::Widget* message_center_widget =
  225. tray_->message_center_bubble()->GetBubbleWidget();
  226. if (message_center_widget ==
  227. views::Widget::GetWidgetForNativeView(gained_active)) {
  228. return;
  229. }
  230. // If the message center is not visible, ignore activation changes.
  231. // Otherwise, this may cause a crash when closing the dialog via
  232. // accelerator. See crbug.com/1041174.
  233. if (!message_center_widget->IsVisible())
  234. return;
  235. }
  236. tray_->CloseBubble();
  237. }
  238. void UnifiedSystemTrayBubble::RecordTimeToClick() {
  239. if (!time_opened_)
  240. return;
  241. tray_->MaybeRecordFirstInteraction(
  242. UnifiedSystemTray::FirstInteractionType::kQuickSettings);
  243. UMA_HISTOGRAM_TIMES("ChromeOS.SystemTray.TimeToClick2",
  244. base::TimeTicks::Now() - time_opened_.value());
  245. time_opened_.reset();
  246. }
  247. void UnifiedSystemTrayBubble::OnTabletModeStarted() {
  248. UpdateBubbleBounds();
  249. }
  250. void UnifiedSystemTrayBubble::OnTabletModeEnded() {
  251. UpdateBubbleBounds();
  252. }
  253. void UnifiedSystemTrayBubble::OnAutoHideStateChanged(
  254. ShelfAutoHideState new_state) {
  255. UpdateBubbleBounds();
  256. }
  257. void UnifiedSystemTrayBubble::UpdateBubbleBounds() {
  258. int max_height = CalculateMaxHeight();
  259. unified_view_->SetMaxHeight(max_height);
  260. bubble_view_->SetMaxHeight(max_height);
  261. bubble_view_->ChangeAnchorAlignment(tray_->shelf()->alignment());
  262. bubble_view_->ChangeAnchorRect(tray_->shelf()->GetSystemTrayAnchorRect());
  263. if (tray_->IsMessageCenterBubbleShown())
  264. tray_->message_center_bubble()->UpdatePosition();
  265. }
  266. void UnifiedSystemTrayBubble::NotifyAccessibilityEvent(ax::mojom::Event event,
  267. bool send_native_event) {
  268. bubble_view_->NotifyAccessibilityEvent(event, send_native_event);
  269. }
  270. bool UnifiedSystemTrayBubble::ShowingAudioDetailedView() const {
  271. return bubble_widget_ && controller_->showing_audio_detailed_view();
  272. }
  273. bool UnifiedSystemTrayBubble::ShowingCalendarView() const {
  274. return bubble_widget_ && controller_->showing_calendar_view();
  275. }
  276. } // namespace ash