Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / app_list / views / search_result_tile_item_view.cc
blob1823514ad7705001656591dbc88f11c98e4ab94b
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/search_result.h"
8 #include "ui/app_list/views/search_result_container_view.h"
9 #include "ui/views/controls/menu/menu_runner.h"
11 namespace app_list {
13 SearchResultTileItemView::SearchResultTileItemView(
14 SearchResultContainerView* result_container)
15 : result_container_(result_container), item_(NULL) {
16 // When |item_| is null, the tile is invisible. Calling SetSearchResult with a
17 // non-null item makes the tile visible.
18 SetVisible(false);
20 set_context_menu_controller(this);
23 SearchResultTileItemView::~SearchResultTileItemView() {
24 if (item_)
25 item_->RemoveObserver(this);
28 void SearchResultTileItemView::SetSearchResult(SearchResult* item) {
29 // Handle the case where this may be called from a nested run loop while its
30 // context menu is showing. This cancels the menu (it's for the old item).
31 context_menu_runner_.reset();
33 SetVisible(item != NULL);
35 SearchResult* old_item = item_;
36 if (old_item)
37 old_item->RemoveObserver(this);
39 item_ = item;
41 if (!item)
42 return;
44 item_->AddObserver(this);
46 SetTitle(item_->title());
48 // Only refresh the icon if it's different from the old one. This prevents
49 // flickering.
50 if (old_item == NULL ||
51 !item->icon().BackedBySameObjectAs(old_item->icon())) {
52 OnIconChanged();
56 void SearchResultTileItemView::ButtonPressed(views::Button* sender,
57 const ui::Event& event) {
58 item_->Open(event.flags());
61 bool SearchResultTileItemView::OnKeyPressed(const ui::KeyEvent& event) {
62 if (event.key_code() == ui::VKEY_RETURN) {
63 item_->Open(event.flags());
64 return true;
67 return false;
70 void SearchResultTileItemView::OnIconChanged() {
71 SetIcon(item_->icon());
74 void SearchResultTileItemView::OnResultDestroying() {
75 if (item_)
76 item_->RemoveObserver(this);
77 item_ = NULL;
80 void SearchResultTileItemView::ShowContextMenuForView(
81 views::View* source,
82 const gfx::Point& point,
83 ui::MenuSourceType source_type) {
84 // |item_| could be null when result list is changing.
85 if (!item_)
86 return;
88 ui::MenuModel* menu_model = item_->GetContextMenuModel();
89 if (!menu_model)
90 return;
92 if (!selected())
93 result_container_->ClearSelectedIndex();
95 context_menu_runner_.reset(
96 new views::MenuRunner(menu_model, views::MenuRunner::HAS_MNEMONICS));
97 // If RunMenuAt() fails, return immediately. This is future-proofing for
98 // adding code after this call.
99 if (context_menu_runner_->RunMenuAt(
100 GetWidget(), nullptr, gfx::Rect(point, gfx::Size()),
101 views::MENU_ANCHOR_TOPLEFT,
102 source_type) == views::MenuRunner::MENU_DELETED)
103 return;
106 } // namespace app_list