File: unified_system_tray_bubble.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (345 lines) | stat: -rw-r--r-- 11,406 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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/unified/unified_system_tray_bubble.h"

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/bubble/bubble_constants.h"
#include "ash/constants/ash_features.h"
#include "ash/keyboard/keyboard_controller_impl.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/system/notification_center/ash_message_popup_collection.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/time/calendar_metrics.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_event_filter.h"
#include "ash/system/tray/tray_utils.h"
#include "ash/system/unified/quick_settings_metrics_util.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "ash/wm/container_finder.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/debug/crash_logging.h"
#include "base/metrics/histogram_macros.h"
#include "ui/aura/window.h"
#include "ui/compositor/layer.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"

namespace ash {

namespace {
constexpr int kDetailedViewHeight = 464;
constexpr int kSystemTrayBubbleCornerRadius = 24;
}  // namespace

UnifiedSystemTrayBubble::UnifiedSystemTrayBubble(UnifiedSystemTray* tray)
    : controller_(std::make_unique<UnifiedSystemTrayController>(tray->model(),
                                                                this,
                                                                tray)),
      unified_system_tray_(tray) {
  time_opened_ = base::TimeTicks::Now();

  TrayBubbleView::InitParams init_params =
      CreateInitParamsForTrayBubble(tray, /*anchor_to_shelf_corner=*/true);
  init_params.preferred_width = kWideTrayMenuWidth;
  init_params.close_on_deactivate = false;
  init_params.corner_radius = kSystemTrayBubbleCornerRadius;
  bubble_view_ = new TrayBubbleView(init_params);

  // Max height calculated from the maximum available height of the screen.
  int max_height = CalculateMaxTrayBubbleHeight(
      unified_system_tray_->GetBubbleWindowContainer());

  auto quick_settings_view = controller_->CreateQuickSettingsView(max_height);
  bubble_view_->SetMaxHeight(max_height);
  quick_settings_view_ =
      bubble_view_->AddChildView(std::move(quick_settings_view));
  time_to_click_recorder_ = std::make_unique<TimeToClickRecorder>(
      /*delegate=*/this, /*target_view=*/quick_settings_view_);

  bubble_widget_ = views::BubbleDialogDelegateView::CreateBubble(bubble_view_);
  bubble_widget_->AddObserver(this);

  TrayBackgroundView::InitializeBubbleAnimations(bubble_widget_);
  bubble_view_->InitializeAndShowBubble();

  // Notify accessibility features that the status tray has opened.
  NotifyAccessibilityEventDeprecated(ax::mojom::Event::kShow, true);

  // Explicitly close the app list in clamshell mode.
  if (!display::Screen::GetScreen()->InTabletMode()) {
    Shell::Get()->app_list_controller()->DismissAppList();
  }
}

UnifiedSystemTrayBubble::~UnifiedSystemTrayBubble() {
  // Record the number of quick settings pages.
  auto page_count = unified_system_tray_controller()
                        ->model()
                        ->pagination_model()
                        ->total_pages();
  DCHECK_GT(page_count, 0);
  quick_settings_metrics_util::RecordQsPageCountOnClose(page_count);

  if (controller_->showing_calendar_view()) {
    unified_system_tray_->NotifyLeavingCalendarView();
  }

  KeyboardController::Get()->RemoveObserver(this);
  if (Shell::Get()->tablet_mode_controller()) {
    Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
  }
  unified_system_tray_->shelf()->RemoveObserver(this);

  // Unified view children depend on `controller_` which is about to go away.
  // Remove child views synchronously to ensure they don't try to access
  // `controller_` after `this` goes out of scope.
  if (bubble_view_) {
    controller_->ShutDownDetailedViewController();
    bubble_view_->RemoveAllChildViews();
    quick_settings_view_ = nullptr;
    bubble_view_->ResetDelegate();
    bubble_view_ = nullptr;
  }

  if (bubble_widget_) {
    bubble_widget_->RemoveObserver(this);
    bubble_widget_->Close();
    bubble_widget_ = nullptr;
  }

  CHECK(!TrayBubbleBase::IsInObserverList());
}

void UnifiedSystemTrayBubble::InitializeObservers() {
  unified_system_tray_->shelf()->AddObserver(this);
  Shell::Get()->tablet_mode_controller()->AddObserver(this);
  KeyboardController::Get()->AddObserver(this);

  CHECK(bubble_widget_);
  CHECK(bubble_view_);
  tray_event_filter_ = std::make_unique<TrayEventFilter>(
      bubble_widget_, bubble_view_, /*tray_button=*/unified_system_tray_);
}

gfx::Rect UnifiedSystemTrayBubble::GetBoundsInScreen() const {
  DCHECK(bubble_view_);
  return bubble_view_->GetBoundsInScreen();
}

bool UnifiedSystemTrayBubble::IsBubbleActive() const {
  return bubble_widget_ && bubble_widget_->IsActive();
}

void UnifiedSystemTrayBubble::ShowAudioDetailedView() {
  if (!bubble_widget_) {
    return;
  }

  DCHECK(quick_settings_view_);
  DCHECK(controller_);
  controller_->ShowAudioDetailedView();
}

void UnifiedSystemTrayBubble::ShowDisplayDetailedView() {
  if (!bubble_widget_) {
    return;
  }

  DCHECK(quick_settings_view_);
  DCHECK(controller_);
  controller_->ShowDisplayDetailedView();
}

void UnifiedSystemTrayBubble::ShowCalendarView(
    calendar_metrics::CalendarViewShowSource show_source,
    calendar_metrics::CalendarEventSource event_source) {
  if (!bubble_widget_) {
    return;
  }

  if (event_source == calendar_metrics::CalendarEventSource::kKeyboard) {
    auto weak_this = weak_factory_.GetWeakPtr();
    bubble_view_->SetCanActivate(true);
    bubble_widget_->Activate();
    // Calling `bubble_widget_->Activate()` can cause `this` to be deleted. We
    // should not continue if that happens.
    if (!weak_this) {
      return;
    }
  }

  DCHECK(quick_settings_view_);
  DCHECK(controller_);
  controller_->ShowCalendarView(show_source, event_source);
}

void UnifiedSystemTrayBubble::ShowNetworkDetailedView() {
  if (!bubble_widget_) {
    return;
  }

  DCHECK(quick_settings_view_);
  DCHECK(controller_);
  controller_->ShowNetworkDetailedView();
}

void UnifiedSystemTrayBubble::UpdateBubble() {
  if (!bubble_widget_) {
    return;
  }
  DCHECK(bubble_view_);

  bubble_view_->UpdateBubble();
}

TrayBackgroundView* UnifiedSystemTrayBubble::GetTray() const {
  return unified_system_tray_;
}

TrayBubbleView* UnifiedSystemTrayBubble::GetBubbleView() const {
  return bubble_view_;
}

views::Widget* UnifiedSystemTrayBubble::GetBubbleWidget() const {
  return bubble_widget_;
}

int UnifiedSystemTrayBubble::GetCurrentTrayHeight() const {
  return quick_settings_view_->GetCurrentHeight();
}

void UnifiedSystemTrayBubble::OnDidApplyDisplayChanges() {
  UpdateBubbleBounds();
}

void UnifiedSystemTrayBubble::OnWidgetDestroying(views::Widget* widget) {
  CHECK_EQ(bubble_widget_, widget);
  bubble_widget_->RemoveObserver(this);
  bubble_widget_ = nullptr;

  controller_->ShutDownDetailedViewController();
  bubble_view_->RemoveAllChildViews();
  quick_settings_view_ = nullptr;
  bubble_view_->ResetDelegate();
  bubble_view_ = nullptr;

  // `unified_system_tray_->CloseBubble()` will delete `this`.
  unified_system_tray_->CloseBubble();
}

void UnifiedSystemTrayBubble::RecordTimeToClick() {
  if (!time_opened_) {
    return;
  }

  unified_system_tray_->MaybeRecordFirstInteraction(
      UnifiedSystemTray::FirstInteractionType::kQuickSettings);

  UMA_HISTOGRAM_TIMES("ChromeOS.SystemTray.TimeToClick2",
                      base::TimeTicks::Now() - time_opened_.value());

  time_opened_.reset();
}

void UnifiedSystemTrayBubble::OnTabletPhysicalStateChanged() {
  // Deletes this.
  unified_system_tray_->CloseBubble();
}

void UnifiedSystemTrayBubble::OnAutoHideStateChanged(
    ShelfAutoHideState new_state) {
  UpdateBubbleBounds();
}

void UnifiedSystemTrayBubble::OnKeyboardVisibilityChanged(
    const bool is_visible) {
  // When keyboard visibility changes, delay updating the bubble bounds until
  // after all the keyboard changes have been processed.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(&UnifiedSystemTrayBubble::UpdateBubbleBounds,
                                weak_factory_.GetWeakPtr()));
}

void UnifiedSystemTrayBubble::UpdateBubbleHeight(bool is_showing_detiled_view) {
  if (!bubble_view_) {
    return;
  }
  bubble_view_->SetShouldUseFixedHeight(is_showing_detiled_view);
  UpdateBubbleBounds();
}

void UnifiedSystemTrayBubble::UpdateBubbleBounds() {
  // USTB_UBB stands for `UnifiedSystemTrayBubble::UpdateBubbleBounds`. Here
  // using the short version since the log method has a character count limit
  // of 40.
  SCOPED_CRASH_KEY_BOOL("USTB_UBB", "bubble_view_", !!bubble_view_);
  SCOPED_CRASH_KEY_BOOL("USTB_UBB", "unified_system_tray_",
                        !!unified_system_tray_);
  SCOPED_CRASH_KEY_BOOL(
      "USTB_UBB", "unified_system_tray_->shelf()",
      !!unified_system_tray_ && !!unified_system_tray_->shelf());
  SCOPED_CRASH_KEY_BOOL("USTB_UBB", "bubble_widget_", !!bubble_widget_);
  SCOPED_CRASH_KEY_BOOL("USTB_UBB", "bubble_widget_->IsClosed()",
                        !!bubble_widget_ && !!bubble_widget_->IsClosed());

  // `bubble_view_` or `Shelf` may be null, see https://b/293264371,
  if (!bubble_view_ || !quick_settings_view_) {
    return;
  }
  if (!unified_system_tray_->shelf()) {
    return;
  }

  int max_height = CalculateMaxTrayBubbleHeight(
      unified_system_tray_->GetBubbleWindowContainer());
  if (bubble_view_->ShouldUseFixedHeight()) {
    const int qs_current_height = quick_settings_view_->height();
    max_height =
        std::min(max_height, std::max(qs_current_height, kDetailedViewHeight));
  }

  // Setting the max height can result in the popup baseline being updated,
  // closing this bubble.
  quick_settings_view_->SetMaxHeight(max_height);

  if (!bubble_view_) {
    // Updating the maximum height can result in popup baseline changing. If
    // there is not enough room for popups, the bubble will be closed, and this
    // `bubble_view_` will not exist. This is a corner case, and we should
    // probably not close the bubble in this case.  See https://b/302172146.
    return;
  }
  bubble_view_->SetMaxHeight(max_height);
  bubble_view_->ChangeAnchorAlignment(
      unified_system_tray_->shelf()->alignment());
  bubble_view_->ChangeAnchorRect(
      unified_system_tray_->shelf()->GetSystemTrayAnchorRect());
}

void UnifiedSystemTrayBubble::NotifyAccessibilityEventDeprecated(
    ax::mojom::Event event,
    bool send_native_event) {
  if (!bubble_view_) {
    return;
  }
  bubble_view_->NotifyAccessibilityEventDeprecated(event, send_native_event);
}

bool UnifiedSystemTrayBubble::ShowingAudioDetailedView() const {
  return bubble_widget_ && controller_->showing_audio_detailed_view();
}

bool UnifiedSystemTrayBubble::ShowingDisplayDetailedView() const {
  return bubble_widget_ && controller_->showing_display_detailed_view();
}

bool UnifiedSystemTrayBubble::ShowingCalendarView() const {
  return bubble_widget_ && controller_->showing_calendar_view();
}

}  // namespace ash