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
|
// Copyright 2013 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/views/app_list_main_view.h"
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include "ash/app_list/app_list_metrics.h"
#include "ash/app_list/app_list_model_provider.h"
#include "ash/app_list/app_list_util.h"
#include "ash/app_list/app_list_view_delegate.h"
#include "ash/app_list/model/app_list_folder_item.h"
#include "ash/app_list/model/app_list_item.h"
#include "ash/app_list/views/app_list_folder_view.h"
#include "ash/app_list/views/app_list_item_view.h"
#include "ash/app_list/views/app_list_search_view.h"
#include "ash/app_list/views/app_list_view.h"
#include "ash/app_list/views/apps_container_view.h"
#include "ash/app_list/views/apps_grid_view.h"
#include "ash/app_list/views/contents_view.h"
#include "ash/app_list/views/paged_apps_grid_view.h"
#include "ash/app_list/views/search_box_view.h"
#include "ash/app_list/views/search_result_base_view.h"
#include "ash/app_list/views/search_result_page_view.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/app_list/app_list_features.h"
#include "ash/public/cpp/pagination/pagination_model.h"
#include "ash/search_box/search_box_view_base.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/string_util.h"
#include "ui/aura/window.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/public/activation_client.h"
namespace ash {
////////////////////////////////////////////////////////////////////////////////
// AppListMainView:
AppListMainView::AppListMainView(AppListViewDelegate* delegate,
AppListView* app_list_view)
: delegate_(delegate), app_list_view_(app_list_view) {
// We need a layer to apply transform to in small display so that the apps
// grid fits in the display.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
}
AppListMainView::~AppListMainView() = default;
void AppListMainView::Init(int initial_apps_page,
SearchBoxView* search_box_view) {
search_box_view_ = search_box_view;
AddContentsViews();
// Switch the apps grid view to the specified page.
PaginationModel* pagination_model = GetAppsPaginationModel();
if (pagination_model->is_valid_page(initial_apps_page))
pagination_model->SelectPage(initial_apps_page, false);
}
void AppListMainView::AddContentsViews() {
DCHECK(search_box_view_);
auto contents_view = std::make_unique<ContentsView>(app_list_view_);
contents_view->Init();
contents_view->SetPaintToLayer(ui::LAYER_NOT_DRAWN);
contents_view->layer()->SetMasksToBounds(true);
contents_view_ = AddChildView(std::move(contents_view));
}
void AppListMainView::ShowAppListWhenReady() {
// After switching to tablet mode, other app windows may be active. Show the
// app list without activating it to avoid breaking other windows' state.
const aura::Window* active_window =
wm::GetActivationClient(
app_list_view_->GetWidget()->GetNativeView()->GetRootWindow())
->GetActiveWindow();
if (active_window)
GetWidget()->ShowInactive();
else
GetWidget()->Show();
}
void AppListMainView::SetDragAndDropHostOfCurrentAppList(
ApplicationDragAndDropHost* drag_and_drop_host) {
contents_view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
}
PaginationModel* AppListMainView::GetAppsPaginationModel() {
return contents_view_->apps_container_view()
->apps_grid_view()
->pagination_model();
}
void AppListMainView::Layout() {
gfx::Rect rect = GetContentsBounds();
if (!rect.IsEmpty()) {
contents_view_->SetBoundsRect(rect);
}
}
void AppListMainView::QueryChanged(const std::u16string& trimmed_query,
bool initiated_by_user) {
app_list_view_->SetStateFromSearchBoxView(trimmed_query.empty(),
initiated_by_user);
contents_view_->ShowSearchResults(search_box_view_->is_search_box_active() ||
!trimmed_query.empty());
contents_view_->search_result_page_view()->UpdateForNewSearch();
}
void AppListMainView::ActiveChanged(SearchBoxViewBase* sender) {
// Do not update views on closing.
if (app_list_view_->app_list_state() == AppListViewState::kClosed) {
return;
}
if (search_box_view_->is_search_box_active()) {
// Show zero state suggestions when search box is activated with an empty
// query.
const bool is_query_empty = sender->IsSearchBoxTrimmedQueryEmpty();
app_list_view_->SetStateFromSearchBoxView(
is_query_empty, true /*triggered_by_contents_change*/);
contents_view_->ShowSearchResults(true);
} else {
// Close the search results page if the search box is inactive.
contents_view_->ShowSearchResults(false);
}
}
void AppListMainView::OnSearchBoxKeyEvent(ui::KeyEvent* event) {
app_list_view_->RedirectKeyEventToSearchBox(event);
if (!IsUnhandledUpDownKeyEvent(*event)) {
return;
}
// Handles arrow key events from the search box while the search box is
// inactive. This covers both folder traversal and apps grid traversal. Search
// result traversal is handled in |HandleKeyEvent|
AppListPage* page =
contents_view_->GetPageView(contents_view_->GetActivePageIndex());
views::View* next_view = nullptr;
if (event->key_code() == ui::VKEY_UP) {
next_view = page->GetLastFocusableView();
} else {
next_view = page->GetFirstFocusableView();
}
if (next_view) {
next_view->RequestFocus();
}
event->SetHandled();
}
bool AppListMainView::CanSelectSearchResults() {
// If there's a result, keyboard selection is allowed.
return !!contents_view_->search_result_page_view()->CanSelectSearchResults();
}
bool AppListMainView::HandleFocusMoveAboveSearchResults(
const ui::KeyEvent& key_event) {
return contents_view_->search_result_page_view()
->search_view()
->OverrideKeyNavigationAboveSearchResults(key_event);
}
void AppListMainView::AssistantButtonPressed() {
delegate_->StartAssistant();
}
void AppListMainView::CloseButtonPressed() {
// Deactivate the search box.
search_box_view_->SetSearchBoxActive(false, ui::ET_UNKNOWN);
search_box_view_->ClearSearch();
}
BEGIN_METADATA(AppListMainView)
END_METADATA
} // namespace ash
|