Workaround for xkbcommon dead keys.
[chromium-blink-merge.git] / ui / app_list / views / search_result_tile_item_view.cc
blob7c9ddcffdd5d178a1f28fd27b0a8546abc124b11
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"
10 namespace app_list {
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.
15 SetVisible(false);
17 set_context_menu_controller(this);
20 SearchResultTileItemView::~SearchResultTileItemView() {
21 if (item_)
22 item_->RemoveObserver(this);
25 void SearchResultTileItemView::SetSearchResult(SearchResult* item) {
26 SetVisible(item != NULL);
28 SearchResult* old_item = item_;
29 if (old_item)
30 old_item->RemoveObserver(this);
32 item_ = item;
34 if (!item)
35 return;
37 item_->AddObserver(this);
39 SetTitle(item_->title());
41 // Only refresh the icon if it's different from the old one. This prevents
42 // flickering.
43 if (old_item == NULL ||
44 !item->icon().BackedBySameObjectAs(old_item->icon())) {
45 OnIconChanged();
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());
57 return true;
60 return false;
63 void SearchResultTileItemView::OnIconChanged() {
64 SetIcon(item_->icon());
67 void SearchResultTileItemView::OnResultDestroying() {
68 if (item_)
69 item_->RemoveObserver(this);
70 item_ = NULL;
73 void SearchResultTileItemView::ShowContextMenuForView(
74 views::View* source,
75 const gfx::Point& point,
76 ui::MenuSourceType source_type) {
77 // |item_| could be null when result list is changing.
78 if (!item_)
79 return;
81 ui::MenuModel* menu_model = item_->GetContextMenuModel();
82 if (!menu_model)
83 return;
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)
93 return;
96 } // namespace app_list