Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / app_list / views / tile_item_view.cc
blobaf04fd6b08bb209a1d6c5dca1a76d5b167dd075d
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/tile_item_view.h"
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/app_list/views/app_list_main_view.h"
9 #include "ui/views/background.h"
10 #include "ui/views/controls/image_view.h"
11 #include "ui/views/controls/label.h"
12 #include "ui/views/layout/box_layout.h"
14 namespace {
16 const int kTileSize = 90;
17 const int kIconTitleSpacing = 6;
19 } // namespace
21 namespace app_list {
23 TileItemView::TileItemView()
24 : views::CustomButton(this),
25 parent_background_color_(SK_ColorTRANSPARENT),
26 icon_(new views::ImageView),
27 title_(new views::Label),
28 selected_(false) {
29 views::BoxLayout* layout_manager = new views::BoxLayout(
30 views::BoxLayout::kVertical, 0, 0, kIconTitleSpacing);
31 layout_manager->set_main_axis_alignment(
32 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
33 SetLayoutManager(layout_manager);
35 icon_->SetImageSize(gfx::Size(kTileIconSize, kTileIconSize));
36 // Prevent the icon view from interfering with our mouse events.
37 icon_->set_interactive(false);
39 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
40 title_->SetAutoColorReadabilityEnabled(false);
41 title_->SetEnabledColor(kGridTitleColor);
42 title_->SetFontList(rb.GetFontList(kItemTextFontStyle));
43 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
44 title_->SetHandlesTooltips(false);
46 AddChildView(icon_);
47 AddChildView(title_);
50 TileItemView::~TileItemView() {
53 void TileItemView::SetSelected(bool selected) {
54 if (selected == selected_)
55 return;
57 selected_ = selected;
58 UpdateBackgroundColor();
60 if (selected)
61 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
64 void TileItemView::SetParentBackgroundColor(SkColor color) {
65 parent_background_color_ = color;
66 UpdateBackgroundColor();
69 void TileItemView::SetIcon(const gfx::ImageSkia& icon) {
70 icon_->SetImage(icon);
73 void TileItemView::SetTitle(const base::string16& title) {
74 title_->SetText(title);
75 SetAccessibleName(title);
78 void TileItemView::StateChanged() {
79 UpdateBackgroundColor();
82 void TileItemView::UpdateBackgroundColor() {
83 views::Background* background = nullptr;
84 SkColor background_color = parent_background_color_;
86 if (selected_) {
87 background_color = kSelectedColor;
88 background = views::Background::CreateSolidBackground(background_color);
89 } else if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
90 background_color = kHighlightedColor;
91 background = views::Background::CreateSolidBackground(background_color);
94 // Tell the label what color it will be drawn onto. It will use whether the
95 // background color is opaque or transparent to decide whether to use subpixel
96 // rendering. Does not actually set the label's background color.
97 title_->SetBackgroundColor(background_color);
99 set_background(background);
100 SchedulePaint();
103 gfx::Size TileItemView::GetPreferredSize() const {
104 return gfx::Size(kTileSize, kTileSize);
107 bool TileItemView::GetTooltipText(const gfx::Point& p,
108 base::string16* tooltip) const {
109 // Use the label to generate a tooltip, so that it will consider its text
110 // truncation in making the tooltip. We do not want the label itself to have a
111 // tooltip, so we only temporarily enable it to get the tooltip text from the
112 // label, then disable it again.
113 title_->SetHandlesTooltips(true);
114 bool handled = title_->GetTooltipText(p, tooltip);
115 title_->SetHandlesTooltips(false);
116 return handled;
119 } // namespace app_list