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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
// Copyright 2021 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/app_list/app_list_bubble_presenter.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "ash/app_list/app_list_bubble_event_filter.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/app_list_event_targeter.h"
#include "ash/app_list/apps_collections_controller.h"
#include "ash/app_list/views/app_list_bubble_apps_collections_page.h"
#include "ash/app_list/views/app_list_bubble_apps_page.h"
#include "ash/app_list/views/app_list_bubble_view.h"
#include "ash/app_list/views/search_box_view.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/public/cpp/app_list/app_list_client.h"
#include "ash/public/cpp/app_list/app_list_features.h"
#include "ash/public/cpp/app_list/app_list_types.h"
#include "ash/public/cpp/assistant/controller/assistant_ui_controller.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/scanner/scanner_metrics.h"
#include "ash/shelf/home_button.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_navigation_widget.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/wm/container_finder.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_enums.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/public/activation_client.h"
namespace ash {
namespace {
using assistant::AssistantExitPoint;
// Maximum amount of time to spend refreshing zero state search results before
// opening the launcher.
constexpr base::TimeDelta kZeroStateSearchTimeout = base::Milliseconds(16);
// Space between the edge of the bubble and the edge of the work area.
constexpr int kWorkAreaPadding = 8;
// Space between the AppListBubbleView and the top of the screen should be at
// least this value plus the shelf height.
constexpr int kExtraTopOfScreenSpacing = 16;
gfx::Rect GetWorkAreaForBubble(aura::Window* root_window) {
display::Display display =
display::Screen::GetScreen()->GetDisplayNearestWindow(root_window);
gfx::RectF work_area(display.work_area());
// Subtract the shelf's bounds from the work area, since the shelf should
// always be shown with the app list bubble. This is done because the work
// area includes the area under the shelf when the shelf is set to auto-hide.
gfx::RectF shelf_bounds(Shelf::ForWindow(root_window)->GetIdealBounds());
wm::TranslateRectToScreen(root_window, &shelf_bounds);
work_area.Subtract(shelf_bounds);
return gfx::ToRoundedRect(work_area);
}
int GetBubbleWidth(gfx::Rect work_area, aura::Window* root_window) {
// As of August 2021 the assistant cards require a minimum width of 640. If
// the cards become narrower then this could be reduced.
return work_area.width() < 1200 ? 544 : 640;
}
// Returns the preferred size of the bubble widget in DIPs.
gfx::Size ComputeBubbleSize(aura::Window* root_window,
AppListBubbleView* bubble_view) {
const int default_height = 688;
const int shelf_size = ShelfConfig::Get()->shelf_size();
const gfx::Rect work_area = GetWorkAreaForBubble(root_window);
int height = default_height;
const int width = GetBubbleWidth(work_area, root_window);
// If the work area height is too small to fit the default size bubble, then
// calculate a smaller height to fit in the work area. Otherwise, if the work
// area height is tall enough to fit at least two default sized bubbles, then
// calculate a taller bubble with height taking no more than half the work
// area.
if (work_area.height() <
default_height + shelf_size + kExtraTopOfScreenSpacing) {
height = work_area.height() - shelf_size - kExtraTopOfScreenSpacing;
} else if (work_area.height() >
default_height * 2 + shelf_size + kExtraTopOfScreenSpacing) {
// Calculate the height required to fit the contents of the AppListBubble
// with no scrolling.
int height_to_fit_all_apps = bubble_view->GetHeightToFitAllApps();
int max_height =
(work_area.height() - shelf_size - kExtraTopOfScreenSpacing) / 2;
DCHECK_GE(max_height, default_height);
height = std::clamp(height_to_fit_all_apps, default_height, max_height);
}
return gfx::Size(width, height);
}
// Returns the bounds in root window coordinates for the bubble widget.
gfx::Rect ComputeBubbleBounds(aura::Window* root_window,
AppListBubbleView* bubble_view) {
const gfx::Rect work_area = GetWorkAreaForBubble(root_window);
const gfx::Size bubble_size = ComputeBubbleSize(root_window, bubble_view);
const int padding = kWorkAreaPadding; // Shorten name for readability.
int x = 0;
int y = 0;
switch (Shelf::ForWindow(root_window)->alignment()) {
case ShelfAlignment::kBottom:
case ShelfAlignment::kBottomLocked:
if (base::i18n::IsRTL())
x = work_area.right() - padding - bubble_size.width();
else
x = work_area.x() + padding;
y = work_area.bottom() - padding - bubble_size.height();
break;
case ShelfAlignment::kLeft:
x = work_area.x() + padding;
y = work_area.y() + padding;
break;
case ShelfAlignment::kRight:
x = work_area.right() - padding - bubble_size.width();
y = work_area.y() + padding;
break;
}
return gfx::Rect(x, y, bubble_size.width(), bubble_size.height());
}
// Creates a bubble widget for the display with `root_window`. The widget is
// owned by its native widget.
views::Widget* CreateBubbleWidget(aura::Window* root_window) {
views::Widget* widget = new views::Widget();
views::Widget::InitParams params(
views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.name = "AppListBubble";
params.parent =
Shell::GetContainer(root_window, kShellWindowId_AppListContainer);
// AppListBubbleView handles round corners and blur via layers.
params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
params.layer_type = ui::LAYER_NOT_DRAWN;
widget->Init(std::move(params));
return widget;
}
} // namespace
AppListBubblePresenter::AppListBubblePresenter(
AppListControllerImpl* controller)
: controller_(controller) {
DCHECK(controller_);
}
AppListBubblePresenter::~AppListBubblePresenter() {
CHECK(!views::WidgetObserver::IsInObserverList());
}
void AppListBubblePresenter::Shutdown() {
DVLOG(1) << __PRETTY_FUNCTION__;
// Aborting in-progress animations will run their cleanup callbacks, which
// might close the widget.
if (bubble_view_)
bubble_view_->AbortAllAnimations();
if (bubble_widget_)
bubble_widget_->CloseNow(); // Calls OnWidgetDestroying().
DCHECK(!bubble_widget_);
DCHECK(!bubble_view_);
}
void AppListBubblePresenter::Show(int64_t display_id) {
DVLOG(1) << __PRETTY_FUNCTION__;
if (is_target_visibility_show_)
return;
if (bubble_view_)
bubble_view_->AbortAllAnimations();
is_target_visibility_show_ = true;
target_page_ = AppsCollectionsController::Get()->ShouldShowAppsCollection()
? AppListBubblePage::kAppsCollections
: AppListBubblePage::kApps;
controller_->OnVisibilityWillChange(/*visible=*/true, display_id);
// Refresh the continue tasks before opening the launcher. If a file doesn't
// exist on disk anymore then the launcher should not create or animate the
// continue task view for that suggestion.
controller_->GetClient()->StartZeroStateSearch(
base::BindOnce(&AppListBubblePresenter::OnZeroStateSearchDone,
weak_factory_.GetWeakPtr(), display_id),
kZeroStateSearchTimeout);
}
void AppListBubblePresenter::OnZeroStateSearchDone(int64_t display_id) {
DVLOG(1) << __PRETTY_FUNCTION__;
// Dismiss() might have been called while waiting for zero-state results.
if (!is_target_visibility_show_)
return;
aura::Window* root_window = Shell::GetRootWindowForDisplayId(display_id);
// Display might have disconnected during zero state refresh.
if (!root_window)
return;
Shelf* shelf = Shelf::ForWindow(root_window);
HomeButton* home_button = shelf->navigation_widget()->GetHomeButton();
if (!bubble_widget_) {
// If the bubble widget is null, this is the first show. Construct views.
base::TimeTicks time_shown = base::TimeTicks::Now();
bubble_widget_ = CreateBubbleWidget(root_window);
bubble_widget_->GetNativeWindow()->SetTitle(l10n_util::GetStringUTF16(
IDS_APP_LIST_LAUNCHER_ACCESSIBILITY_ANNOUNCEMENT));
bubble_widget_->GetNativeWindow()->SetEventTargeter(
std::make_unique<AppListEventTargeter>(controller_));
bubble_view_ = bubble_widget_->SetContentsView(
std::make_unique<AppListBubbleView>(controller_));
// Some of Assistant UIs have to be initialized explicitly. See details in
// the comment of AppListBubbleView::InitializeUIForBubbleView.
bubble_view_->InitializeUIForBubbleView();
// Arrow left/right and up/down triggers the same focus movement as
// tab/shift+tab.
bubble_widget_->widget_delegate()->SetEnableArrowKeyTraversal(true);
bubble_widget_->AddObserver(this);
Shell::Get()->activation_client()->AddObserver(this);
// Set up event filter to close the bubble for clicks outside the bubble
// that don't cause window activation changes (e.g. clicks on wallpaper or
// blank areas of shelf).
bubble_event_filter_ = std::make_unique<AppListBubbleEventFilter>(
bubble_widget_, home_button,
base::BindRepeating(&AppListBubblePresenter::OnPressOutsideBubble,
base::Unretained(this)));
UmaHistogramTimes("Apps.AppListBubbleCreationTime",
base::TimeTicks::Now() - time_shown);
} else {
DCHECK(bubble_view_);
// Refresh suggestions now that zero-state search data is updated.
bubble_view_->UpdateSuggestions();
bubble_event_filter_->SetButton(home_button);
}
// The widget bounds sometimes depend on the height of the apps grid, so set
// the bounds after creating and setting the contents. This may cause the
// bubble to change displays.
bubble_widget_->SetBounds(ComputeBubbleBounds(root_window, bubble_view_));
// Bubble launcher is always keyboard traversable. Update every show in case
// we are coming out of tablet mode.
controller_->SetKeyboardTraversalMode(true);
bubble_widget_->Show();
// The page must be set before triggering the show animation so the correct
// animations are triggered.
bubble_view_->ShowPage(target_page_);
const bool is_side_shelf = !shelf->IsHorizontalAlignment();
bubble_view_->StartShowAnimation(is_side_shelf);
controller_->OnVisibilityChanged(/*visible=*/true, display_id);
// Show the sunfish nudge after the widget is shown, so the anchor view is
// visible.
views::ImageButton* sunfish_button =
bubble_view_->search_box_view()->sunfish_button();
// `sunfish_button` is always initialised in `SearchBoxView`'s
// constructor.
CHECK(sunfish_button);
controller_->MaybeShowSunfishLauncherNudge(sunfish_button);
}
ShelfAction AppListBubblePresenter::Toggle(int64_t display_id) {
DVLOG(1) << __PRETTY_FUNCTION__;
if (is_target_visibility_show_) {
Dismiss();
return SHELF_ACTION_APP_LIST_DISMISSED;
}
Show(display_id);
return SHELF_ACTION_APP_LIST_SHOWN;
}
void AppListBubblePresenter::Dismiss() {
DVLOG(1) << __PRETTY_FUNCTION__;
if (!is_target_visibility_show_)
return;
// Check for view because the code could be waiting for zero-state search
// results before first show.
if (bubble_view_)
bubble_view_->AbortAllAnimations();
// Must call before setting `is_target_visibility_show_` to false.
const int64_t display_id = GetDisplayId();
is_target_visibility_show_ = false;
// Reset keyboard traversal in case the user switches to tablet launcher.
// Must happen before widget is destroyed.
controller_->SetKeyboardTraversalMode(false);
controller_->ViewClosing();
controller_->OnVisibilityWillChange(/*visible=*/false, display_id);
if (bubble_view_) {
aura::Window* bubble_window = bubble_view_->GetWidget()->GetNativeWindow();
DCHECK(bubble_window);
Shelf* shelf = Shelf::ForWindow(bubble_window);
const bool is_side_shelf = !shelf->IsHorizontalAlignment();
bubble_view_->StartHideAnimation(
is_side_shelf,
base::BindOnce(&AppListBubblePresenter::OnHideAnimationEnded,
weak_factory_.GetWeakPtr()));
}
controller_->OnVisibilityChanged(/*visible=*/false, display_id);
// Clean up assistant if it is showing.
controller_->ScheduleCloseAssistant();
}
aura::Window* AppListBubblePresenter::GetWindow() const {
return is_target_visibility_show_ && bubble_widget_
? bubble_widget_->GetNativeWindow()
: nullptr;
}
bool AppListBubblePresenter::IsShowing() const {
return is_target_visibility_show_;
}
bool AppListBubblePresenter::IsShowingEmbeddedAssistantUI() const {
if (!is_target_visibility_show_)
return false;
// Bubble view is null while the bubble widget is being initialized for show.
// In this case, return true iff the app list will show the assistant page
// when initialized.
if (!bubble_view_)
return target_page_ == AppListBubblePage::kAssistant;
return bubble_view_->IsShowingEmbeddedAssistantUI();
}
void AppListBubblePresenter::UpdateContinueSectionVisibility() {
if (bubble_view_)
bubble_view_->UpdateContinueSectionVisibility();
}
void AppListBubblePresenter::UpdateForNewSortingOrder(
const std::optional<AppListSortOrder>& new_order,
bool animate,
base::OnceClosure update_position_closure) {
DCHECK_EQ(animate, !update_position_closure.is_null());
if (!bubble_view_) {
// A rare case. Still handle it for safety.
if (update_position_closure)
std::move(update_position_closure).Run();
return;
}
bubble_view_->UpdateForNewSortingOrder(new_order, animate,
std::move(update_position_closure));
}
void AppListBubblePresenter::ShowEmbeddedAssistantUI() {
DVLOG(1) << __PRETTY_FUNCTION__;
target_page_ = AppListBubblePage::kAssistant;
// `bubble_view_` does not exist while waiting for zero-state results.
// OnZeroStateSearchDone() sets the page in that case.
if (bubble_view_) {
DCHECK(bubble_widget_);
bubble_view_->ShowEmbeddedAssistantUI();
}
}
void AppListBubblePresenter::OnWidgetDestroying(views::Widget* widget) {
DVLOG(1) << __PRETTY_FUNCTION__;
// NOTE: While the widget is usually cached after Show(), this method can be
// called on monitor disconnect. Clean up state.
// `bubble_event_filter_` holds a pointer to the widget.
bubble_event_filter_.reset();
Shell::Get()->activation_client()->RemoveObserver(this);
bubble_widget_->RemoveObserver(this);
bubble_widget_ = nullptr;
bubble_view_ = nullptr;
}
void AppListBubblePresenter::OnWindowActivated(ActivationReason reason,
aura::Window* gained_active,
aura::Window* lost_active) {
if (!is_target_visibility_show_)
return;
if (gained_active) {
if (auto* container = GetContainerForWindow(gained_active)) {
const int container_id = container->GetId();
// The bubble can be shown without activation if:
// 1. The bubble or one of its children (e.g. an uninstall dialog) gains
// activation; OR
// 2. The shelf gains activation (e.g. by pressing Alt-Shift-L); OR
// 3. A help bubble container's descendant gains activation.
if (container_id == kShellWindowId_AppListContainer ||
container_id == kShellWindowId_ShelfContainer ||
container_id == kShellWindowId_HelpBubbleContainer) {
return;
}
}
}
// Closing the bubble for "press" type events is handled by
// `bubble_event_filter_`. Activation can change when a user merely moves the
// cursor outside the app list bounds, so losing activation should not close
// the bubble.
if (reason == wm::ActivationChangeObserver::ActivationReason::INPUT_EVENT)
return;
aura::Window* app_list_container =
bubble_widget_->GetNativeWindow()->parent();
// Otherwise, if the bubble or one of its children lost activation or if
// something other than the bubble gains activation, the bubble should close.
if ((lost_active && app_list_container->Contains(lost_active)) ||
(gained_active && !app_list_container->Contains(gained_active))) {
Dismiss();
}
}
void AppListBubblePresenter::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t changed_metrics) {
if (!IsShowing())
return;
// Ignore changes to displays that aren't showing the launcher.
if (display.id() != GetDisplayId())
return;
aura::Window* root_window =
bubble_widget_->GetNativeWindow()->GetRootWindow();
bubble_widget_->SetBounds(ComputeBubbleBounds(root_window, bubble_view_));
}
void AppListBubblePresenter::OnPressOutsideBubble(
const ui::LocatedEvent& event) {
// Presses outside the bubble could be activating a shelf item. Record the
// app list state prior to dismissal.
controller_->RecordAppListState();
// The press outside the bubble might spawn a menu. If the bubble is active at
// the end of the hide animation, an activation change event will cause the
// menu to close. Deactivate now so menus stay open. https://crbug.com/1299088
if (bubble_widget_->IsActive()) {
bubble_widget_->Deactivate();
}
Dismiss();
}
int64_t AppListBubblePresenter::GetDisplayId() const {
if (!is_target_visibility_show_ || !bubble_widget_)
return display::kInvalidDisplayId;
return display::Screen::GetScreen()
->GetDisplayNearestView(bubble_widget_->GetNativeView())
.id();
}
void AppListBubblePresenter::OnHideAnimationEnded() {
// Hiding the launcher causes a window activation change. If the launcher is
// hiding because the user opened a system tray bubble, don't immediately
// close the bubble in response.
auto lock = TrayBackgroundView::DisableCloseBubbleOnWindowActivated();
bubble_widget_->Hide();
controller_->MaybeCloseAssistant();
}
int AppListBubblePresenter::GetPreferredBubbleWidth(
aura::Window* root_window) const {
return GetBubbleWidth(GetWorkAreaForBubble(root_window), root_window);
}
} // namespace ash
|