| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/app_list/app_list_item_view.h" | 5 #include "ash/app_list/app_list_item_view.h" |
| 6 | 6 |
| 7 #include "ash/app_list/app_list_item_group_view.h" | |
| 8 #include "ash/app_list/app_list_item_model.h" | 7 #include "ash/app_list/app_list_item_model.h" |
| 9 #include "ash/app_list/app_list_item_view_listener.h" | |
| 10 #include "ash/app_list/drop_shadow_label.h" | 8 #include "ash/app_list/drop_shadow_label.h" |
| 11 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 12 #include "third_party/skia/include/core/SkColor.h" | 10 #include "third_party/skia/include/core/SkColor.h" |
| 11 #include "ui/base/animation/throb_animation.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
| 16 #include "ui/views/controls/image_view.h" | 15 #include "ui/views/controls/image_view.h" |
| 17 #include "ui/views/controls/label.h" | |
| 18 #include "ui/views/widget/widget.h" | |
| 19 | 16 |
| 20 namespace ash { | 17 namespace ash { |
| 21 | 18 |
| 22 namespace { | 19 namespace { |
| 23 | 20 |
| 24 const double kFocusedScale = 1.1; | 21 const int kIconTitleSpacing = 5; |
| 25 | 22 |
| 26 const SkColor kTitleColor = SK_ColorWHITE; | 23 const SkColor kTitleColor = SK_ColorWHITE; |
| 24 const SkColor kHoverColor = SkColorSetARGB(0x33, 0xFF, 0xFF, 0xFF); // 0.2 white |
| 27 | 25 |
| 28 gfx::Font GetTitleFont() { | 26 gfx::Font GetTitleFont() { |
| 29 static gfx::Font* font = NULL; | 27 static gfx::Font* font = NULL; |
| 30 if (!font) { | 28 if (!font) { |
| 31 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 29 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 32 font = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont).DeriveFont( | 30 font = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont).DeriveFont( |
| 33 2, gfx::Font::BOLD)); | 31 1, gfx::Font::BOLD)); |
| 34 } | 32 } |
| 35 return *font; | 33 return *font; |
| 36 } | 34 } |
| 37 | 35 |
| 36 // An image view that is not interactive. |
| 37 class StaticImageView : public views::ImageView { |
| 38 public: |
| 39 StaticImageView() : ImageView() { |
| 40 } |
| 41 |
| 42 private: |
| 43 // views::View overrides: |
| 44 virtual bool HitTest(const gfx::Point& l) const OVERRIDE { |
| 45 return false; |
| 46 } |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(StaticImageView); |
| 49 }; |
| 50 |
| 38 } // namespace | 51 } // namespace |
| 39 | 52 |
| 53 // static |
| 54 const char AppListItemView::kViewClassName[] = "ash/app_list/AppListItemView"; |
| 55 |
| 40 AppListItemView::AppListItemView(AppListItemModel* model, | 56 AppListItemView::AppListItemView(AppListItemModel* model, |
| 41 AppListItemViewListener* listener) | 57 views::ButtonListener* listener) |
| 42 : model_(model), | 58 : CustomButton(listener), |
| 43 listener_(listener), | 59 model_(model), |
| 44 icon_(new views::ImageView), | 60 icon_(new StaticImageView), |
| 45 title_(new DropShadowLabel) { | 61 title_(new DropShadowLabel) { |
| 46 set_focusable(true); | |
| 47 | |
| 48 title_->SetFont(GetTitleFont()); | 62 title_->SetFont(GetTitleFont()); |
| 49 title_->SetBackgroundColor(0); | 63 title_->SetBackgroundColor(0); |
| 50 title_->SetEnabledColor(kTitleColor); | 64 title_->SetEnabledColor(kTitleColor); |
| 65 title_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 51 | 66 |
| 52 AddChildView(icon_); | 67 AddChildView(icon_); |
| 53 AddChildView(title_); | 68 AddChildView(title_); |
| 54 | 69 |
| 55 ItemIconChanged(); | 70 ItemIconChanged(); |
| 56 ItemTitleChanged(); | 71 ItemTitleChanged(); |
| 57 model_->AddObserver(this); | 72 model_->AddObserver(this); |
| 58 } | 73 } |
| 59 | 74 |
| 60 AppListItemView::~AppListItemView() { | 75 AppListItemView::~AppListItemView() { |
| 61 model_->RemoveObserver(this); | 76 model_->RemoveObserver(this); |
| 62 } | 77 } |
| 63 | 78 |
| 64 void AppListItemView::NotifyActivated(int event_flags) { | |
| 65 if (listener_) | |
| 66 listener_->AppListItemActivated(this, event_flags); | |
| 67 } | |
| 68 | |
| 69 void AppListItemView::ItemIconChanged() { | 79 void AppListItemView::ItemIconChanged() { |
| 70 icon_->SetImage(model_->icon()); | 80 icon_->SetImage(model_->icon()); |
| 71 } | 81 } |
| 72 | 82 |
| 73 void AppListItemView::ItemTitleChanged() { | 83 void AppListItemView::ItemTitleChanged() { |
| 74 title_->SetText(UTF8ToUTF16(model_->title())); | 84 title_->SetText(UTF8ToUTF16(model_->title())); |
| 75 } | 85 } |
| 76 | 86 |
| 87 std::string AppListItemView::GetClassName() const { |
| 88 return kViewClassName; |
| 89 } |
| 90 |
| 77 gfx::Size AppListItemView::GetPreferredSize() { | 91 gfx::Size AppListItemView::GetPreferredSize() { |
| 78 return gfx::Size(kTileSize, kTileSize); | 92 gfx::Size icon_size = icon_->GetPreferredSize(); |
| 93 gfx::Size title_size = title_->GetPreferredSize(); |
| 94 |
| 95 return gfx::Size(icon_size.width() + kIconTitleSpacing + title_size.width(), |
| 96 std::max(icon_size.height(), title_size.height())); |
| 79 } | 97 } |
| 80 | 98 |
| 81 void AppListItemView::Layout() { | 99 void AppListItemView::Layout() { |
| 82 gfx::Rect rect(GetContentsBounds()); | 100 gfx::Rect rect(GetContentsBounds()); |
| 83 gfx::Size title_size = title_->GetPreferredSize(); | |
| 84 | 101 |
| 85 if (!HasFocus()) { | 102 int preferred_icon_size = rect.height() - 2 * kPadding; |
| 86 const int horiz_padding = (kTileSize - kIconSize) / 2; | 103 gfx::Size icon_size(preferred_icon_size, preferred_icon_size); |
| 87 const int vert_padding = (kTileSize - title_size.height() - kIconSize) / 2; | 104 icon_->SetImageSize(icon_size); |
| 88 rect.Inset(horiz_padding, vert_padding); | 105 icon_->SetBounds(rect.x() + kPadding, rect.y(), |
| 89 } | 106 icon_size.width(), rect.height()); |
| 90 | 107 |
| 91 icon_->SetBounds(rect.x(), rect.y(), | 108 title_->SetBounds( |
| 92 rect.width(), rect.height() - title_size.height()); | 109 icon_->bounds().right() + kIconTitleSpacing, |
| 93 | 110 rect.y(), |
| 94 title_->SetBounds(rect.x(), rect.bottom() - title_size.height(), | 111 rect.right() - kPadding - icon_->bounds().right() - kIconTitleSpacing, |
| 95 rect.width(), title_size.height()); | 112 rect.height()); |
| 96 } | 113 } |
| 97 | 114 |
| 98 void AppListItemView::OnFocus() { | 115 void AppListItemView::OnPaint(gfx::Canvas* canvas) { |
| 99 View::OnFocus(); | 116 gfx::Rect rect(GetContentsBounds()); |
| 100 | 117 if (hover_animation_->is_animating()) { |
| 101 gfx::Size icon_size = icon_->GetPreferredSize(); | 118 int alpha = SkColorGetA(kHoverColor) * hover_animation_->GetCurrentValue(); |
| 102 icon_size.set_width(icon_size.width() * kFocusedScale); | 119 canvas->FillRect(rect, SkColorSetA(kHoverColor, alpha)); |
| 103 icon_size.set_height(icon_size.height() * kFocusedScale); | 120 } else if (state() == BS_HOT) { |
| 104 | 121 canvas->FillRect(rect, kHoverColor); |
| 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 } | 122 } |
| 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 } | 123 } |
| 161 | 124 |
| 162 } // namespace ash | 125 } // namespace ash |
| OLD | NEW |