Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / app_list / views / search_result_tile_item_view.cc
blob3a8ecbe06be630b9581e64b5d75a3ab50d1ac99f
1 // Copyright 2014 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.
5 #include "ui/app_list/views/search_result_tile_item_view.h"
7 #include "ui/app_list/app_list_view_delegate.h"
8 #include "ui/app_list/search_result.h"
9 #include "ui/app_list/views/search_result_container_view.h"
10 #include "ui/views/controls/menu/menu_runner.h"
12 namespace app_list {
14 SearchResultTileItemView::SearchResultTileItemView(
15 SearchResultContainerView* result_container,
16 AppListViewDelegate* view_delegate)
17 : result_container_(result_container),
18 item_(nullptr),
19 view_delegate_(view_delegate) {
20 // When |item_| is null, the tile is invisible. Calling SetSearchResult with a
21 // non-null item makes the tile visible.
22 SetVisible(false);
24 set_context_menu_controller(this);
27 SearchResultTileItemView::~SearchResultTileItemView() {
28 if (item_)
29 item_->RemoveObserver(this);
32 void SearchResultTileItemView::SetSearchResult(SearchResult* item) {
33 // Handle the case where this may be called from a nested run loop while its
34 // context menu is showing. This cancels the menu (it's for the old item).
35 context_menu_runner_.reset();
37 SetVisible(item != NULL);
39 SearchResult* old_item = item_;
40 if (old_item)
41 old_item->RemoveObserver(this);
43 item_ = item;
45 if (!item)
46 return;
48 item_->AddObserver(this);
50 SetTitle(item_->title());
52 // Only refresh the icon if it's different from the old one. This prevents
53 // flickering.
54 if (old_item == NULL ||
55 !item->icon().BackedBySameObjectAs(old_item->icon())) {
56 OnIconChanged();
60 void SearchResultTileItemView::ButtonPressed(views::Button* sender,
61 const ui::Event& event) {
62 view_delegate_->OpenSearchResult(item_, false, event.flags());
65 bool SearchResultTileItemView::OnKeyPressed(const ui::KeyEvent& event) {
66 if (event.key_code() == ui::VKEY_RETURN) {
67 view_delegate_->OpenSearchResult(item_, false, event.flags());
68 return true;
71 return false;
74 void SearchResultTileItemView::OnIconChanged() {
75 SetIcon(item_->icon());
78 void SearchResultTileItemView::OnResultDestroying() {
79 if (item_)
80 item_->RemoveObserver(this);
81 item_ = NULL;
84 void SearchResultTileItemView::ShowContextMenuForView(
85 views::View* source,
86 const gfx::Point& point,
87 ui::MenuSourceType source_type) {
88 // |item_| could be null when result list is changing.
89 if (!item_)
90 return;
92 ui::MenuModel* menu_model = item_->GetContextMenuModel();
93 if (!menu_model)
94 return;
96 if (!selected())
97 result_container_->ClearSelectedIndex();
99 context_menu_runner_.reset(
100 new views::MenuRunner(menu_model, views::MenuRunner::HAS_MNEMONICS));
101 // If RunMenuAt() fails, return immediately. This is future-proofing for
102 // adding code after this call.
103 if (context_menu_runner_->RunMenuAt(
104 GetWidget(), nullptr, gfx::Rect(point, gfx::Size()),
105 views::MENU_ANCHOR_TOPLEFT,
106 source_type) == views::MenuRunner::MENU_DELETED)
107 return;
110 } // namespace app_list