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"
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.
20 set_context_menu_controller(this);
23 SearchResultTileItemView::~SearchResultTileItemView() {
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_
;
37 old_item
->RemoveObserver(this);
44 item_
->AddObserver(this);
46 SetTitle(item_
->title());
48 // Only refresh the icon if it's different from the old one. This prevents
50 if (old_item
== NULL
||
51 !item
->icon().BackedBySameObjectAs(old_item
->icon())) {
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());
70 void SearchResultTileItemView::OnIconChanged() {
71 SetIcon(item_
->icon());
74 void SearchResultTileItemView::OnResultDestroying() {
76 item_
->RemoveObserver(this);
80 void SearchResultTileItemView::ShowContextMenuForView(
82 const gfx::Point
& point
,
83 ui::MenuSourceType source_type
) {
84 // |item_| could be null when result list is changing.
88 ui::MenuModel
* menu_model
= item_
->GetContextMenuModel();
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
)
106 } // namespace app_list