OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "ui/aura_shell/app_list/app_list_item_view.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "third_party/skia/include/core/SkColor.h" |
| 9 #include "ui/aura_shell/app_list/app_list_item_group_view.h" |
| 10 #include "ui/aura_shell/app_list/app_list_item_model.h" |
| 11 #include "ui/aura_shell/app_list/app_list_item_view_listener.h" |
| 12 #include "ui/aura_shell/app_list/drop_shadow_label.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/font.h" |
| 16 #include "ui/views/controls/image_view.h" |
| 17 #include "ui/views/controls/label.h" |
| 18 #include "ui/views/widget/widget.h" |
| 19 |
| 20 namespace aura_shell { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const double kFocusedScale = 1.1; |
| 25 |
| 26 const SkColor kTitleColor = SK_ColorWHITE; |
| 27 |
| 28 gfx::Font GetTitleFont() { |
| 29 static gfx::Font* font = NULL; |
| 30 if (!font) { |
| 31 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 32 font = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont).DeriveFont( |
| 33 2, gfx::Font::BOLD)); |
| 34 } |
| 35 return *font; |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 AppListItemView::AppListItemView(AppListItemModel* model, |
| 41 AppListItemViewListener* listener) |
| 42 : model_(model), |
| 43 listener_(listener), |
| 44 icon_(new views::ImageView), |
| 45 title_(new DropShadowLabel) { |
| 46 set_focusable(true); |
| 47 |
| 48 title_->SetFont(GetTitleFont()); |
| 49 title_->SetBackgroundColor(0); |
| 50 title_->SetEnabledColor(kTitleColor); |
| 51 |
| 52 AddChildView(icon_); |
| 53 AddChildView(title_); |
| 54 |
| 55 ItemIconChanged(); |
| 56 ItemTitleChanged(); |
| 57 model_->AddObserver(this); |
| 58 } |
| 59 |
| 60 AppListItemView::~AppListItemView() { |
| 61 model_->RemoveObserver(this); |
| 62 } |
| 63 |
| 64 void AppListItemView::NotifyActivated(int event_flags) { |
| 65 if (listener_) |
| 66 listener_->AppListItemActivated(this, event_flags); |
| 67 } |
| 68 |
| 69 void AppListItemView::ItemIconChanged() { |
| 70 icon_->SetImage(model_->icon()); |
| 71 } |
| 72 |
| 73 void AppListItemView::ItemTitleChanged() { |
| 74 title_->SetText(UTF8ToUTF16(model_->title())); |
| 75 } |
| 76 |
| 77 gfx::Size AppListItemView::GetPreferredSize() { |
| 78 return gfx::Size(kTileSize, kTileSize); |
| 79 } |
| 80 |
| 81 void AppListItemView::Layout() { |
| 82 gfx::Rect rect(GetContentsBounds()); |
| 83 gfx::Size title_size = title_->GetPreferredSize(); |
| 84 |
| 85 if (!HasFocus()) { |
| 86 const int horiz_padding = (kTileSize - kIconSize) / 2; |
| 87 const int vert_padding = (kTileSize - title_size.height() - kIconSize) / 2; |
| 88 rect.Inset(horiz_padding, vert_padding); |
| 89 } |
| 90 |
| 91 icon_->SetBounds(rect.x(), rect.y(), |
| 92 rect.width(), rect.height() - title_size.height()); |
| 93 |
| 94 title_->SetBounds(rect.x(), rect.bottom() - title_size.height(), |
| 95 rect.width(), title_size.height()); |
| 96 } |
| 97 |
| 98 void AppListItemView::OnFocus() { |
| 99 View::OnFocus(); |
| 100 |
| 101 gfx::Size icon_size = icon_->GetPreferredSize(); |
| 102 icon_size.set_width(icon_size.width() * kFocusedScale); |
| 103 icon_size.set_height(icon_size.height() * kFocusedScale); |
| 104 |
| 105 gfx::Size max_size = GetPreferredSize(); |
| 106 if (icon_size.width() > max_size.width() || |
| 107 icon_size.height() > max_size.height()) { |
| 108 double aspect = |
| 109 static_cast<double>(icon_size.width()) / icon_size.height(); |
| 110 |
| 111 if (aspect > 1) { |
| 112 icon_size.set_width(max_size.width()); |
| 113 icon_size.set_height(icon_size.width() / aspect); |
| 114 } else { |
| 115 icon_size.set_height(max_size.height()); |
| 116 icon_size.set_width(icon_size.height() * aspect); |
| 117 } |
| 118 } |
| 119 |
| 120 icon_->SetImageSize(icon_size); |
| 121 Layout(); |
| 122 |
| 123 AppListItemGroupView* group_view = |
| 124 static_cast<AppListItemGroupView*>(parent()); |
| 125 group_view->UpdateFocusedTile(this); |
| 126 } |
| 127 |
| 128 void AppListItemView::OnBlur() { |
| 129 icon_->ResetImageSize(); |
| 130 Layout(); |
| 131 SchedulePaint(); |
| 132 } |
| 133 |
| 134 bool AppListItemView::OnKeyPressed(const views::KeyEvent& event) { |
| 135 if (event.key_code() == ui::VKEY_RETURN) { |
| 136 NotifyActivated(event.flags()); |
| 137 return true; |
| 138 } |
| 139 |
| 140 return false; |
| 141 } |
| 142 |
| 143 bool AppListItemView::OnMousePressed(const views::MouseEvent& event) { |
| 144 views::View* hit_view = GetEventHandlerForPoint(event.location()); |
| 145 bool hit = hit_view != this; |
| 146 if (hit) |
| 147 RequestFocus(); |
| 148 |
| 149 return hit; |
| 150 } |
| 151 |
| 152 void AppListItemView::OnMouseReleased(const views::MouseEvent& event) { |
| 153 views::View* hit_view = GetEventHandlerForPoint(event.location()); |
| 154 if (hit_view != this) |
| 155 NotifyActivated(event.flags()); |
| 156 } |
| 157 |
| 158 void AppListItemView::OnPaintFocusBorder(gfx::Canvas* canvas) { |
| 159 // No focus border for AppListItemView. |
| 160 } |
| 161 |
| 162 } // namespace aura_shell |
OLD | NEW |