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"
14 SearchResultTileItemView::SearchResultTileItemView(
15 SearchResultContainerView
* result_container
,
16 AppListViewDelegate
* view_delegate
)
17 : result_container_(result_container
),
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.
24 set_context_menu_controller(this);
27 SearchResultTileItemView::~SearchResultTileItemView() {
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_
;
41 old_item
->RemoveObserver(this);
48 item_
->AddObserver(this);
50 SetTitle(item_
->title());
52 // Only refresh the icon if it's different from the old one. This prevents
54 if (old_item
== NULL
||
55 !item
->icon().BackedBySameObjectAs(old_item
->icon())) {
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());
74 void SearchResultTileItemView::OnIconChanged() {
75 SetIcon(item_
->icon());
78 void SearchResultTileItemView::OnResultDestroying() {
80 item_
->RemoveObserver(this);
84 void SearchResultTileItemView::ShowContextMenuForView(
86 const gfx::Point
& point
,
87 ui::MenuSourceType source_type
) {
88 // |item_| could be null when result list is changing.
92 ui::MenuModel
* menu_model
= item_
->GetContextMenuModel();
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
)
110 } // namespace app_list