continue_task_container_view.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Copyright 2021 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/app_list/views/continue_task_container_view.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "ash/app_list/app_list_view_delegate.h"
  10. #include "ash/app_list/model/search/search_model.h"
  11. #include "ash/app_list/views/continue_task_view.h"
  12. #include "ash/public/cpp/app_list/app_list_notifier.h"
  13. #include "ash/strings/grit/ash_strings.h"
  14. #include "base/check.h"
  15. #include "base/strings/string_util.h"
  16. #include "extensions/common/constants.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. #include "ui/base/metadata/metadata_impl_macros.h"
  19. #include "ui/compositor/layer.h"
  20. #include "ui/events/keycodes/keyboard_codes_posix.h"
  21. #include "ui/gfx/geometry/transform_util.h"
  22. #include "ui/views/accessibility/view_accessibility.h"
  23. #include "ui/views/animation/animation_builder.h"
  24. #include "ui/views/border.h"
  25. #include "ui/views/controls/label.h"
  26. #include "ui/views/focus/focus_manager.h"
  27. #include "ui/views/layout/box_layout.h"
  28. #include "ui/views/layout/flex_layout.h"
  29. #include "ui/views/layout/table_layout.h"
  30. using views::BoxLayout;
  31. using views::FlexLayout;
  32. using views::TableLayout;
  33. namespace ash {
  34. namespace {
  35. // Suggested tasks layout constants.
  36. constexpr int kColumnInnerSpacingClamshell = 8;
  37. constexpr int kColumnOuterSpacingClamshell = 6;
  38. constexpr int kColumnSpacingTablet = 16;
  39. constexpr int kRowSpacing = 8;
  40. constexpr size_t kMaxFilesForContinueSection = 4;
  41. struct CompareByDisplayIndexAndPositionPriority {
  42. bool operator()(const SearchResult* result1,
  43. const SearchResult* result2) const {
  44. SearchResultDisplayIndex index1 = result1->display_index();
  45. SearchResultDisplayIndex index2 = result2->display_index();
  46. if (index1 != index2)
  47. return index1 < index2;
  48. return result1->position_priority() > result2->position_priority();
  49. }
  50. };
  51. std::vector<SearchResult*> GetTasksResultsForContinueSection(
  52. SearchModel::SearchResults* results) {
  53. auto continue_filter = [](const SearchResult& r) -> bool {
  54. return r.display_type() == SearchResultDisplayType::kContinue;
  55. };
  56. std::vector<SearchResult*> continue_results;
  57. continue_results = SearchModel::FilterSearchResultsByFunction(
  58. results, base::BindRepeating(continue_filter),
  59. /*max_results=*/4);
  60. std::sort(continue_results.begin(), continue_results.end(),
  61. CompareByDisplayIndexAndPositionPriority());
  62. return continue_results;
  63. }
  64. // Fades out continue task view `view` from the container.
  65. void ScheduleFadeOutAnimation(views::View* view,
  66. views::AnimationSequenceBlock* sequence) {
  67. // Animate views for results that have been removed.
  68. // Opacity changes 100% -> 0%, while the size changes from 100% -> 75%
  69. // original size.
  70. gfx::Transform scale;
  71. scale.Scale(0.75f, 0.75f);
  72. sequence->SetTransform(
  73. view->layer(),
  74. gfx::TransformAboutPivot(view->GetLocalBounds().CenterPoint(), scale),
  75. gfx::Tween::FAST_OUT_LINEAR_IN);
  76. sequence->SetOpacity(view->layer(), 0.0f, gfx::Tween::FAST_OUT_LINEAR_IN);
  77. }
  78. // Slides (and fades) in a new result view into the task container.
  79. // The view is translated from right into target position while animating
  80. // opacity from 1 -> 0. `offfset` is the initial horizontal translation from
  81. // which the view will slide in the target position. The offset direction is
  82. // flipped if `is_rtl` is set.
  83. void ScheduleSlideInAnimation(views::View* view,
  84. int offset,
  85. bool is_rtl,
  86. views::AnimationSequenceBlock* sequence) {
  87. gfx::Transform initial_translate;
  88. initial_translate.Translate(offset * (is_rtl ? -1 : 1), 0);
  89. view->layer()->SetTransform(initial_translate);
  90. sequence->SetTransform(view->layer(), gfx::Transform(),
  91. gfx::Tween::ACCEL_LIN_DECEL_100_3);
  92. view->layer()->SetOpacity(0.0f);
  93. sequence->SetOpacity(view->layer(), 1.0f, gfx::Tween::ACCEL_LIN_DECEL_100_3);
  94. }
  95. // Slides (and fades) out an old result views from the task container.
  96. // The view is translated from its current position to the left, while animating
  97. // opacity from 1 -> 0. `offfset` is the target view's horizontal translation
  98. // from the initial position. The offset direction is flipped if `is_rtl` is
  99. // set.
  100. void ScheduleSlideOutAnimation(views::View* view,
  101. int offset,
  102. bool is_rtl,
  103. views::AnimationSequenceBlock* sequence) {
  104. gfx::Transform target_translate;
  105. target_translate.Translate(offset * (is_rtl ? -1 : 1), 0);
  106. sequence->SetTransform(view->layer(), target_translate,
  107. gfx::Tween::FAST_OUT_LINEAR_IN);
  108. sequence->SetOpacity(view->layer(), 0.0f, gfx::Tween::FAST_OUT_LINEAR_IN);
  109. }
  110. } // namespace
  111. ContinueTaskContainerView::ContinueTaskContainerView(
  112. AppListViewDelegate* view_delegate,
  113. int columns,
  114. OnResultsChanged update_callback,
  115. bool tablet_mode)
  116. : view_delegate_(view_delegate),
  117. update_callback_(update_callback),
  118. tablet_mode_(tablet_mode) {
  119. DCHECK(!update_callback_.is_null());
  120. if (tablet_mode_) {
  121. InitializeFlexLayout();
  122. } else {
  123. columns_ = columns;
  124. InitializeTableLayout();
  125. }
  126. GetViewAccessibility().OverrideRole(ax::mojom::Role::kList);
  127. GetViewAccessibility().OverrideName(
  128. l10n_util::GetStringUTF16(IDS_ASH_LAUNCHER_CONTINUE_SECTION_LABEL));
  129. }
  130. ContinueTaskContainerView::~ContinueTaskContainerView() = default;
  131. void ContinueTaskContainerView::ListItemsAdded(size_t start, size_t count) {
  132. ScheduleUpdate();
  133. }
  134. void ContinueTaskContainerView::ListItemsRemoved(size_t start, size_t count) {
  135. ScheduleUpdate();
  136. }
  137. void ContinueTaskContainerView::ListItemMoved(size_t index,
  138. size_t target_index) {
  139. ScheduleUpdate();
  140. }
  141. void ContinueTaskContainerView::ListItemsChanged(size_t start, size_t count) {
  142. ScheduleUpdate();
  143. }
  144. void ContinueTaskContainerView::VisibilityChanged(views::View* starting_from,
  145. bool is_visible) {
  146. if (!is_visible) {
  147. AbortTasksUpdateAnimations();
  148. } else {
  149. animations_timer_.Start(FROM_HERE, base::Seconds(2), base::DoNothing());
  150. }
  151. auto* notifier = view_delegate_->GetNotifier();
  152. if (notifier) {
  153. // NOTE: Use `IsDrawn()` instead of `is_visible` to account for parent
  154. // container visibility - `IsDrawn()` will return false if this view is
  155. // visible but its parent is not.
  156. notifier->NotifyContinueSectionVisibilityChanged(
  157. SearchResultDisplayType::kContinue, IsDrawn());
  158. }
  159. }
  160. bool ContinueTaskContainerView::OnKeyPressed(const ui::KeyEvent &event) {
  161. // No special focus handling in tablet mode.
  162. if (tablet_mode_) {
  163. return false;
  164. }
  165. if (event.key_code() == ui::VKEY_UP) {
  166. MoveFocusUp();
  167. return true;
  168. }
  169. if (event.key_code() == ui::VKEY_DOWN) {
  170. MoveFocusDown();
  171. return true;
  172. }
  173. return false;
  174. }
  175. void ContinueTaskContainerView::Update() {
  176. // Invalidate this callback to cancel a scheduled update.
  177. update_factory_.InvalidateWeakPtrs();
  178. AbortTasksUpdateAnimations();
  179. std::vector<SearchResult*> tasks =
  180. GetTasksResultsForContinueSection(results_);
  181. // Collect updated set of result IDs, which will be used to determine which
  182. // views need to be animated.
  183. std::vector<std::string> new_ids;
  184. for (const SearchResult* task : tasks) {
  185. new_ids.push_back(task->id());
  186. }
  187. // Only animate container contents update - when continue section is being
  188. // initialized, show the contents immediately.
  189. const bool first_show = animations_timer_.IsRunning() || !GetWidget() ||
  190. !IsDrawn() || suggestion_tasks_views_.empty();
  191. std::set<views::View*> views_to_fade_out;
  192. std::map<std::string, views::View*> views_to_slide_out;
  193. std::map<std::string, views::View*> views_remaining_in_place;
  194. // Determine whether an animation is needed and gather information needed to
  195. // configure update animation.
  196. const bool chip_count_changed =
  197. tasks.size() != suggestion_tasks_views_.size();
  198. bool needs_animation = !first_show && chip_count_changed;
  199. if (!first_show) {
  200. for (size_t i = 0; i < suggestion_tasks_views_.size(); ++i) {
  201. ContinueTaskView* result_view = suggestion_tasks_views_[i];
  202. // Some views may be kept around during update animation, so they can
  203. // animate out - remove the from layout manager so they don't affect new
  204. // layout.
  205. RemoveViewFromLayout(result_view);
  206. TaskViewRemovalAnimation animation =
  207. GetRemovalAnimationForTaskView(result_view, i, new_ids);
  208. switch (animation) {
  209. case TaskViewRemovalAnimation::kFadeOut:
  210. views_to_fade_out.insert(result_view);
  211. break;
  212. case TaskViewRemovalAnimation::kSlideOut:
  213. views_to_slide_out.emplace(result_view->result()->id(), result_view);
  214. break;
  215. case TaskViewRemovalAnimation::kNone:
  216. // In tablet mode, if the number of chips has changed, the chip bounds
  217. // and size are likely to change, so slide out existing items even if
  218. // they remain at the same logical position in the container.
  219. if (tablet_mode_ && chip_count_changed) {
  220. views_to_slide_out.emplace(result_view->result()->id(),
  221. result_view);
  222. } else {
  223. views_remaining_in_place.emplace(result_view->result()->id(),
  224. result_view);
  225. }
  226. break;
  227. }
  228. // Unless the result view remains in the same position within the task
  229. // container, the task update requires animation.
  230. if (animation != TaskViewRemovalAnimation::kNone)
  231. needs_animation = true;
  232. }
  233. }
  234. if (needs_animation) {
  235. views_to_remove_after_animation_.swap(suggestion_tasks_views_);
  236. } else {
  237. // When not animating, all views can be removed immediately.
  238. RemoveAllChildViews();
  239. }
  240. suggestion_tasks_views_.clear();
  241. num_results_ = std::min(kMaxFilesForContinueSection, tasks.size());
  242. num_file_results_ = 0;
  243. for (size_t i = 0; i < num_results_; ++i) {
  244. if (tasks[i]->result_type() == AppListSearchResultType::kZeroStateFile ||
  245. tasks[i]->result_type() == AppListSearchResultType::kFileChip ||
  246. tasks[i]->result_type() == AppListSearchResultType::kZeroStateDrive ||
  247. tasks[i]->result_type() == AppListSearchResultType::kDriveChip) {
  248. ++num_file_results_;
  249. }
  250. }
  251. // Create new result views.
  252. for (size_t i = 0; i < num_results_; ++i) {
  253. auto task =
  254. std::make_unique<ContinueTaskView>(view_delegate_, tablet_mode_);
  255. if (i == 0)
  256. task->SetProperty(views::kMarginsKey, gfx::Insets());
  257. task->set_index_in_container(i);
  258. task->SetResult(tasks[i]);
  259. suggestion_tasks_views_.emplace_back(task.get());
  260. AddChildView(std::move(task));
  261. }
  262. // Layout the container so the task bounds are set to their intended
  263. // positions, which will be used to configure container update animation
  264. // sequences when animating.
  265. Layout();
  266. if (needs_animation) {
  267. ScheduleContainerUpdateAnimation(views_to_fade_out, views_to_slide_out,
  268. views_remaining_in_place);
  269. }
  270. auto* notifier = view_delegate_->GetNotifier();
  271. if (notifier) {
  272. std::vector<AppListNotifier::Result> notifier_results;
  273. for (const auto* task : tasks)
  274. notifier_results.emplace_back(task->id(), task->metrics_type());
  275. notifier->NotifyResultsUpdated(SearchResultDisplayType::kContinue,
  276. notifier_results);
  277. }
  278. if (!update_callback_.is_null())
  279. update_callback_.Run();
  280. }
  281. ContinueTaskContainerView::TaskViewRemovalAnimation
  282. ContinueTaskContainerView::GetRemovalAnimationForTaskView(
  283. ContinueTaskView* task_view,
  284. size_t old_index,
  285. const std::vector<std::string>& new_task_ids) {
  286. // If the result associated with the result was reset, animate the view
  287. // out.
  288. if (!task_view->result())
  289. return TaskViewRemovalAnimation::kFadeOut;
  290. const std::string& task_id = task_view->result()->id();
  291. auto new_ids_it =
  292. std::find(new_task_ids.begin(), new_task_ids.end(), task_id);
  293. // If the associated result was removed from the task list, animate it out.
  294. if (new_ids_it == new_task_ids.end())
  295. return TaskViewRemovalAnimation::kFadeOut;
  296. const size_t new_index = (new_ids_it - new_task_ids.begin());
  297. if (old_index != new_index)
  298. return TaskViewRemovalAnimation::kSlideOut;
  299. return TaskViewRemovalAnimation::kNone;
  300. }
  301. void ContinueTaskContainerView::ScheduleContainerUpdateAnimation(
  302. const std::set<views::View*>& views_to_fade_out,
  303. const std::map<std::string, views::View*>& views_to_slide_out,
  304. const std::map<std::string, views::View*>& views_remaining_in_place) {
  305. views::AnimationBuilder animation_builder;
  306. animation_builder.OnEnded(base::BindOnce(
  307. &ContinueTaskContainerView::ClearAnimatingViews, base::Unretained(this)));
  308. animation_builder.OnAborted(base::BindOnce(
  309. &ContinueTaskContainerView::ClearAnimatingViews, base::Unretained(this)));
  310. animation_builder.Once().SetDuration(base::Milliseconds(100));
  311. // Fade out views for results that got removed.
  312. for (auto* view : views_to_fade_out)
  313. ScheduleFadeOutAnimation(view, &animation_builder.GetCurrentSequence());
  314. // Immediately hide views that remained in place, and for which the new result
  315. // views will not be animated in.
  316. for (auto& view : views_remaining_in_place)
  317. view.second->SetVisible(false);
  318. const bool is_rtl = base::i18n::IsRTL();
  319. // Slide out old result views for results whose position changed.
  320. base::TimeDelta delay =
  321. views_to_fade_out.empty() ? base::TimeDelta() : base::Milliseconds(200);
  322. animation_builder.GetCurrentSequence().At(delay).SetDuration(
  323. base::Milliseconds(100));
  324. for (auto& view : views_to_slide_out) {
  325. ScheduleSlideOutAnimation(view.second, tablet_mode_ ? 0 : -34, is_rtl,
  326. &animation_builder.GetCurrentSequence());
  327. }
  328. // Animate new views in.
  329. delay = views_to_fade_out.empty() ? base::Milliseconds(100)
  330. : base::Milliseconds(300);
  331. animation_builder.GetCurrentSequence().At(delay).SetDuration(
  332. base::Milliseconds(300));
  333. for (auto* view : suggestion_tasks_views_) {
  334. const std::string& result_id = view->result()->id();
  335. // If view remained in place, it does not need to be animated in.
  336. auto view_remaining_in_place_it = views_remaining_in_place.find(result_id);
  337. if (view_remaining_in_place_it != views_remaining_in_place.end())
  338. continue;
  339. int initial_offset = 60;
  340. // In tablet mode, direction from which the view slides in depends on
  341. // whether the view is coming in from left or right - if the result existed
  342. // before the update, and its old view bounds were left of the new view
  343. // bounds, slide the view in from the left by flipping offset direction.
  344. if (tablet_mode_) {
  345. const auto& old_view_it = views_to_slide_out.find(result_id);
  346. if (old_view_it != views_to_slide_out.end() &&
  347. old_view_it->second->x() < view->x()) {
  348. initial_offset = -initial_offset;
  349. }
  350. }
  351. ScheduleSlideInAnimation(view, initial_offset, is_rtl,
  352. &animation_builder.GetCurrentSequence());
  353. }
  354. }
  355. void ContinueTaskContainerView::AbortTasksUpdateAnimations() {
  356. for (auto* view : suggestion_tasks_views_)
  357. view->layer()->GetAnimator()->StopAnimating();
  358. ClearAnimatingViews();
  359. }
  360. void ContinueTaskContainerView::ClearAnimatingViews() {
  361. // Clear `views_to_remove_after_animation_` before starting to remove views in
  362. // case view removal causes an aborted view animation that calls back into
  363. // `ClearAnimatingViews()`. Clearing `views_to_remove_after_animation_` mid
  364. // iteraion over the vector would not be safe.
  365. std::vector<ContinueTaskView*> views_to_remove;
  366. views_to_remove_after_animation_.swap(views_to_remove);
  367. for (auto* view : views_to_remove)
  368. RemoveChildViewT(view);
  369. NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged, true);
  370. }
  371. void ContinueTaskContainerView::SetResults(
  372. SearchModel::SearchResults* results) {
  373. list_model_observation_.Reset();
  374. results_ = results;
  375. if (results_)
  376. list_model_observation_.Observe(results);
  377. Update();
  378. }
  379. void ContinueTaskContainerView::DisableFocusForShowingActiveFolder(
  380. bool disabled) {
  381. for (views::View* child : suggestion_tasks_views_)
  382. child->SetEnabled(!disabled);
  383. }
  384. void ContinueTaskContainerView::AnimateSlideInSuggestions(
  385. int available_space,
  386. base::TimeDelta duration,
  387. gfx::Tween::Type tween) {
  388. SetVisible(true);
  389. const int rows =
  390. columns_ ? std::ceil(static_cast<double>(suggestion_tasks_views_.size()) /
  391. columns_)
  392. : 1;
  393. double space_per_row = static_cast<double>(available_space) / rows;
  394. for (size_t i = 0; i < suggestion_tasks_views_.size(); i++) {
  395. views::View* view = suggestion_tasks_views_[i];
  396. gfx::Transform translation;
  397. int row_number = columns_ ? ((i / columns_) + 1) : 1;
  398. // Distribute the space between the elements so that the space between the
  399. // previous element in the parent view and the first row is the same as the
  400. // space between rows. The items in the first row will just be translated by
  401. // `space_per_row`. The items from the second row need to carry over
  402. // the space translated by the first row and translate again
  403. // `space_per_row` to have even space between elements.
  404. translation.Translate(0, space_per_row * row_number);
  405. view->layer()->SetTransform(translation);
  406. view->layer()->SetOpacity(0.0f);
  407. }
  408. views::AnimationBuilder animation_builder;
  409. animation_builder.Once().SetDuration(duration);
  410. for (auto* view : suggestion_tasks_views_) {
  411. animation_builder.GetCurrentSequence()
  412. .SetTransform(view, gfx::Transform(), tween)
  413. .SetOpacity(view, 1.0f, tween);
  414. }
  415. }
  416. void ContinueTaskContainerView::RemoveViewFromLayout(ContinueTaskView* view) {
  417. view->SetEnabled(false);
  418. if (table_layout_) {
  419. table_layout_->SetChildViewIgnoredByLayout(view, true);
  420. } else if (flex_layout_) {
  421. flex_layout_->SetChildViewIgnoredByLayout(view, true);
  422. }
  423. }
  424. void ContinueTaskContainerView::ScheduleUpdate() {
  425. // When search results are added one by one, each addition generates an update
  426. // request. Consolidates those update requests into one Update call.
  427. if (!update_factory_.HasWeakPtrs()) {
  428. base::ThreadTaskRunnerHandle::Get()->PostTask(
  429. FROM_HERE, base::BindOnce(&ContinueTaskContainerView::Update,
  430. update_factory_.GetWeakPtr()));
  431. }
  432. }
  433. void ContinueTaskContainerView::InitializeFlexLayout() {
  434. DCHECK(tablet_mode_);
  435. DCHECK(!table_layout_);
  436. DCHECK(!columns_);
  437. flex_layout_ = SetLayoutManager(std::make_unique<FlexLayout>());
  438. flex_layout_->SetOrientation(views::LayoutOrientation::kHorizontal)
  439. .SetMainAxisAlignment(views::LayoutAlignment::kCenter)
  440. .SetDefault(views::kMarginsKey,
  441. gfx::Insets::TLBR(0, kColumnSpacingTablet, 0, 0))
  442. .SetDefault(views::kFlexBehaviorKey,
  443. views::FlexSpecification(
  444. views::MinimumFlexSizeRule::kScaleToMinimumSnapToZero,
  445. views::MaximumFlexSizeRule::kScaleToMaximum));
  446. }
  447. void ContinueTaskContainerView::InitializeTableLayout() {
  448. DCHECK(!tablet_mode_);
  449. DCHECK(!flex_layout_);
  450. DCHECK_GT(columns_, 0);
  451. table_layout_ = SetLayoutManager(std::make_unique<views::TableLayout>());
  452. std::vector<size_t> linked_columns;
  453. for (int i = 0; i < columns_; i++) {
  454. if (i == 0) {
  455. table_layout_->AddPaddingColumn(views::TableLayout::kFixedSize,
  456. kColumnOuterSpacingClamshell);
  457. } else {
  458. table_layout_->AddPaddingColumn(views::TableLayout::kFixedSize,
  459. kColumnInnerSpacingClamshell);
  460. }
  461. table_layout_->AddColumn(
  462. views::LayoutAlignment::kStretch, views::LayoutAlignment::kCenter,
  463. /*horizontal_resize=*/1.0f, views::TableLayout::ColumnSize::kFixed,
  464. /*fixed_width=*/0, /*min_width=*/0);
  465. linked_columns.push_back(2 * i + 1);
  466. }
  467. table_layout_->AddPaddingColumn(views::TableLayout::kFixedSize,
  468. kColumnOuterSpacingClamshell);
  469. table_layout_->LinkColumnSizes(linked_columns);
  470. // Continue section only shows if there are 3 or more suggestions, so there
  471. // are always 2 rows.
  472. table_layout_->AddRows(1, views::TableLayout::kFixedSize);
  473. table_layout_->AddPaddingRow(views::TableLayout::kFixedSize, kRowSpacing);
  474. table_layout_->AddRows(1, views::TableLayout::kFixedSize);
  475. }
  476. void ContinueTaskContainerView::MoveFocusUp() {
  477. DVLOG(1) << __FUNCTION__;
  478. // This function should only run when a child has focus.
  479. DCHECK(Contains(GetFocusManager()->GetFocusedView()));
  480. DCHECK(!suggestion_tasks_views_.empty());
  481. int focused_index = GetIndexOfFocusedTaskView();
  482. DCHECK_GE(focused_index, 0);
  483. // Try to move up by one row.
  484. int target_index = focused_index - columns_;
  485. // If that would move before the first item, focus the first item and reverse
  486. // focus out of the section.
  487. if (target_index < 0) {
  488. suggestion_tasks_views_[0]->RequestFocus();
  489. GetFocusManager()->AdvanceFocus(/*reverse=*/true);
  490. return;
  491. }
  492. suggestion_tasks_views_[target_index]->RequestFocus();
  493. }
  494. void ContinueTaskContainerView::MoveFocusDown() {
  495. DVLOG(1) << __FUNCTION__;
  496. // This function should only run when a child has focus.
  497. DCHECK(Contains(GetFocusManager()->GetFocusedView()));
  498. DCHECK(!suggestion_tasks_views_.empty());
  499. int focused_index = GetIndexOfFocusedTaskView();
  500. DCHECK_GE(focused_index, 0);
  501. // Try to move down by one row.
  502. int target_index = focused_index + columns_;
  503. // If that would move past the last item, focus the last item and advance
  504. // focus out of the section.
  505. if (target_index >= static_cast<int>(suggestion_tasks_views_.size())) {
  506. suggestion_tasks_views_.back()->RequestFocus();
  507. GetFocusManager()->AdvanceFocus(/*reverse=*/false);
  508. return;
  509. }
  510. suggestion_tasks_views_[target_index]->RequestFocus();
  511. }
  512. int ContinueTaskContainerView::GetIndexOfFocusedTaskView() const {
  513. for (size_t i = 0; i < suggestion_tasks_views_.size(); ++i) {
  514. if (suggestion_tasks_views_[i]->HasFocus())
  515. return i;
  516. }
  517. return -1;
  518. }
  519. BEGIN_METADATA(ContinueTaskContainerView, views::View)
  520. END_METADATA
  521. } // namespace ash