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/views/controls/menu/menu_runner.h"
12 SearchResultTileItemView::SearchResultTileItemView() : item_(NULL
) {
13 // When |item_| is null, the tile is invisible. Calling SetSearchResult with a
14 // non-null item makes the tile visible.
17 set_context_menu_controller(this);
20 SearchResultTileItemView::~SearchResultTileItemView() {
22 item_
->RemoveObserver(this);
25 void SearchResultTileItemView::SetSearchResult(SearchResult
* item
) {
26 SetVisible(item
!= NULL
);
28 SearchResult
* old_item
= item_
;
30 old_item
->RemoveObserver(this);
37 item_
->AddObserver(this);
39 SetTitle(item_
->title());
41 // Only refresh the icon if it's different from the old one. This prevents
43 if (old_item
== NULL
||
44 !item
->icon().BackedBySameObjectAs(old_item
->icon())) {
49 void SearchResultTileItemView::ButtonPressed(views::Button
* sender
,
50 const ui::Event
& event
) {
51 item_
->Open(event
.flags());
54 bool SearchResultTileItemView::OnKeyPressed(const ui::KeyEvent
& event
) {
55 if (event
.key_code() == ui::VKEY_RETURN
) {
56 item_
->Open(event
.flags());
63 void SearchResultTileItemView::OnIconChanged() {
64 SetIcon(item_
->icon());
67 void SearchResultTileItemView::OnResultDestroying() {
69 item_
->RemoveObserver(this);
73 void SearchResultTileItemView::ShowContextMenuForView(
75 const gfx::Point
& point
,
76 ui::MenuSourceType source_type
) {
77 // |item_| could be null when result list is changing.
81 ui::MenuModel
* menu_model
= item_
->GetContextMenuModel();
85 context_menu_runner_
.reset(
86 new views::MenuRunner(menu_model
, views::MenuRunner::HAS_MNEMONICS
));
87 // If RunMenuAt() fails, return immediately. This is future-proofing for
88 // adding code after this call.
89 if (context_menu_runner_
->RunMenuAt(
90 GetWidget(), nullptr, gfx::Rect(point
, gfx::Size()),
91 views::MENU_ANCHOR_TOPLEFT
,
92 source_type
) == views::MenuRunner::MENU_DELETED
)
96 } // namespace app_list