summaryrefslogtreecommitdiffstats
path: root/ui/app_list/contents_view.cc
blob: a213526ef12dbc61de3fb1f45ba18600fd430ea8 (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/app_list/contents_view.h"

#include <algorithm>

#include "ui/app_list/app_list_view.h"
#include "ui/app_list/apps_grid_view.h"
#include "ui/app_list/page_switcher.h"
#include "ui/app_list/pagination_model.h"
#include "ui/app_list/search_result_list_view.h"
#include "ui/base/event.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/view_model.h"
#include "ui/views/view_model_utils.h"

namespace app_list {

namespace {

const int kPreferredIconDimension = 48;
const int kPreferredCols = 4;
const int kPreferredRows = 4;

// Indexes of interesting views in ViewModel of ContentsView.
const int kIndexAppsGrid = 0;
const int kIndexPageSwitcher = 1;
const int kIndexSearchResults = 2;

const int kMinMouseWheelToSwitchPage = 20;
const int kMinScrollToSwitchPage = 20;
const int kMinHorizVelocityToSwitchPage = 800;

const double kFinishTransitionThreshold = 0.33;
const int kTransitionAnimationDurationInMs = 180;

// Helpers to get certain child view from |model|.
AppsGridView* GetAppsGridView(views::ViewModel* model) {
  return static_cast<AppsGridView*>(model->view_at(kIndexAppsGrid));
}

PageSwitcher* GetPageSwitcherView(views::ViewModel* model) {
  return static_cast<PageSwitcher*>(model->view_at(kIndexPageSwitcher));
}

SearchResultListView* GetSearchResultListView(views::ViewModel* model) {
  return static_cast<SearchResultListView*>(
      model->view_at(kIndexSearchResults));
}

}  // namespace

ContentsView::ContentsView(AppListView* app_list_view,
                           PaginationModel* pagination_model)
    : show_state_(SHOW_APPS),
      pagination_model_(pagination_model),
      view_model_(new views::ViewModel),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          bounds_animator_(new views::BoundsAnimator(this))) {
  pagination_model_->SetTransitionDuration(kTransitionAnimationDurationInMs);

  AppsGridView* apps_grid_view = new AppsGridView(app_list_view,
                                                  pagination_model);
  apps_grid_view->SetLayout(kPreferredIconDimension,
                            kPreferredCols,
                            kPreferredRows);
  AddChildView(apps_grid_view);
  view_model_->Add(apps_grid_view, kIndexAppsGrid);

  PageSwitcher* page_switcher_view = new PageSwitcher(pagination_model);
  AddChildView(page_switcher_view);
  view_model_->Add(page_switcher_view, kIndexPageSwitcher);

  SearchResultListView* search_results_view = new SearchResultListView(
      app_list_view);
  AddChildView(search_results_view);
  view_model_->Add(search_results_view, kIndexSearchResults);
}

ContentsView::~ContentsView() {
}

void ContentsView::SetModel(AppListModel* model) {
  if (model) {
    GetAppsGridView(view_model_.get())->SetModel(model->apps());
    GetSearchResultListView(view_model_.get())->SetResults(model->results());
  } else {
    GetAppsGridView(view_model_.get())->SetModel(NULL);
    GetSearchResultListView(view_model_.get())->SetResults(NULL);
  }
}

void ContentsView::SetShowState(ShowState show_state) {
  if (show_state_ == show_state)
    return;

  show_state_ = show_state;
  ShowStateChanged();
}

void ContentsView::ShowStateChanged() {
  if (show_state_ == SHOW_SEARCH_RESULTS) {
    // TODO(xiyuan): Highlight default match instead of the first.
    SearchResultListView* results_view =
        GetSearchResultListView(view_model_.get());
    if (results_view->visible())
      results_view->SetSelectedIndex(0);
  }

  AnimateToIdealBounds();
}

void ContentsView::CalculateIdealBounds() {
  gfx::Rect rect(GetContentsBounds());
  if (rect.IsEmpty())
    return;

  const int x = rect.x();
  const int width = rect.width();

  // AppsGridView and PageSwitcher uses a vertical box layout.
  int y = rect.y();
  const int grid_height =
      GetAppsGridView(view_model_.get())->GetPreferredSize().height();
  gfx::Rect grid_frame(gfx::Point(x, y), gfx::Size(width, grid_height));
  grid_frame = rect.Intersect(grid_frame);

  y = grid_frame.bottom();
  const int page_switcher_height = rect.bottom() - y;
  gfx::Rect page_switcher_frame(gfx::Point(x, y),
                                gfx::Size(width, page_switcher_height));
  page_switcher_frame = rect.Intersect(page_switcher_frame);

  // SearchResultListView occupies the whole space when visible.
  gfx::Rect results_frame(rect);

  // Offsets apps grid, page switcher and result list based on |show_state_|.
  // SearchResultListView is on top of apps grid + page switcher. Visible view
  // is left in visible area and invisible ones is put out of the visible area.
  int contents_area_height = rect.height();
  switch (show_state_) {
    case SHOW_APPS:
      results_frame.Offset(0, -contents_area_height);
      break;
    case SHOW_SEARCH_RESULTS:
      grid_frame.Offset(0, contents_area_height);
      page_switcher_frame.Offset(0, contents_area_height);
      break;
    default:
      NOTREACHED() << "Unknown show_state_ " << show_state_;
      break;
  }

  view_model_->set_ideal_bounds(kIndexAppsGrid, grid_frame);
  view_model_->set_ideal_bounds(kIndexPageSwitcher, page_switcher_frame);
  view_model_->set_ideal_bounds(kIndexSearchResults, results_frame);
}

void ContentsView::AnimateToIdealBounds() {
  CalculateIdealBounds();
  for (int i = 0; i < view_model_->view_size(); ++i) {
    bounds_animator_->AnimateViewTo(view_model_->view_at(i),
                                    view_model_->ideal_bounds(i));
  }
}

void ContentsView::ShowSearchResults(bool show) {
  SetShowState(show ? SHOW_SEARCH_RESULTS : SHOW_APPS);
}

gfx::Size ContentsView::GetPreferredSize() {
  const gfx::Size grid_size =
      GetAppsGridView(view_model_.get())->GetPreferredSize();
  const gfx::Size page_switcher_size =
      GetPageSwitcherView(view_model_.get())->GetPreferredSize();
  const gfx::Size results_size =
      GetSearchResultListView(view_model_.get())->GetPreferredSize();

  int width = std::max(
      std::max(grid_size.width(), page_switcher_size.width()),
      results_size.width());
  int height = std::max(grid_size.height() + page_switcher_size.height(),
                        results_size.height());
  return gfx::Size(width, height);
}

void ContentsView::Layout() {
  CalculateIdealBounds();
  views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
}

ui::GestureStatus ContentsView::OnGestureEvent(
    const ui::GestureEvent& event) {
  if (show_state_ != SHOW_APPS)
    return ui::GESTURE_STATUS_UNKNOWN;

  switch (event.type()) {
    case ui::ET_GESTURE_SCROLL_BEGIN:
      pagination_model_->StartScroll();
      return ui::GESTURE_STATUS_CONSUMED;
    case ui::ET_GESTURE_SCROLL_UPDATE:
      // event.details.scroll_x() > 0 means moving contents to right. That is,
      // transitioning to previous page.
      pagination_model_->UpdateScroll(
          event.details().scroll_x() / GetContentsBounds().width());
      return ui::GESTURE_STATUS_CONSUMED;
    case ui::ET_GESTURE_SCROLL_END:
      pagination_model_->EndScroll(pagination_model_->
          transition().progress < kFinishTransitionThreshold);
      return ui::GESTURE_STATUS_CONSUMED;
    case ui::ET_SCROLL_FLING_START: {
      pagination_model_->EndScroll(true);
      if (fabs(event.details().velocity_x()) > kMinHorizVelocityToSwitchPage) {
        pagination_model_->SelectPageRelative(
            event.details().velocity_x() < 0 ? 1 : -1,
            true);
      }
      return ui::GESTURE_STATUS_CONSUMED;
    }
    default:
      break;
  }

  return ui::GESTURE_STATUS_UNKNOWN;
}

bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) {
  switch (show_state_) {
    case SHOW_APPS:
      return GetAppsGridView(view_model_.get())->OnKeyPressed(event);
    case SHOW_SEARCH_RESULTS:
      return GetSearchResultListView(view_model_.get())->OnKeyPressed(event);
    default:
      NOTREACHED() << "Unknown show state " << show_state_;
  }
  return false;
}

bool ContentsView::OnMouseWheel(const ui::MouseWheelEvent& event) {
  if (show_state_ != SHOW_APPS)
    return false;

  if (abs(event.offset()) > kMinMouseWheelToSwitchPage) {
    if (!pagination_model_->has_transition())
      pagination_model_->SelectPageRelative(event.offset() > 0 ? -1 : 1, true);
    return true;
  }

  return false;
}

bool ContentsView::OnScrollEvent(const ui::ScrollEvent& event) {
  if (show_state_ != SHOW_APPS)
    return false;

  if (abs(event.x_offset()) > kMinScrollToSwitchPage) {
    if (!pagination_model_->has_transition()) {
      pagination_model_->SelectPageRelative(event.x_offset() > 0 ? -1 : 1,
                                            true);
    }
    return true;
  }

  return false;
}

}  // namespace app_list