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" | 8 #include "ash/app_list/app_list_model_view.h" |
10 #include "ash/app_list/drop_shadow_label.h" | 9 #include "ash/app_list/drop_shadow_label.h" |
11 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
12 #include "third_party/skia/include/core/SkColor.h" | 11 #include "third_party/skia/include/core/SkColor.h" |
12 #include "ui/base/animation/throb_animation.h" | |
13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
15 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
16 #include "ui/views/controls/image_view.h" | 16 #include "ui/views/controls/image_view.h" |
17 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
18 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
19 | 19 |
20 namespace ash { | 20 namespace ash { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const double kFocusedScale = 1.1; | 24 const int kIconTitleSpacing = 5; |
25 | 25 |
26 const SkColor kTitleColor = SK_ColorWHITE; | 26 const SkColor kTitleColor = SK_ColorWHITE; |
27 const SkColor kHoverColor = SkColorSetARGB(0x33, 0xFF, 0xFF, 0xFF); // 0.2 white | |
27 | 28 |
28 gfx::Font GetTitleFont() { | 29 gfx::Font GetTitleFont() { |
29 static gfx::Font* font = NULL; | 30 static gfx::Font* font = NULL; |
30 if (!font) { | 31 if (!font) { |
31 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 32 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
32 font = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont).DeriveFont( | 33 font = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont).DeriveFont( |
33 2, gfx::Font::BOLD)); | 34 1, gfx::Font::BOLD)); |
34 } | 35 } |
35 return *font; | 36 return *font; |
36 } | 37 } |
37 | 38 |
39 // An image view that is not interactive. | |
40 class StaticImageView : public views::ImageView { | |
sky
2012/03/01 00:26:57
Why do you need this?
xiyuan
2012/03/01 19:58:47
ImageView returns true for HitTest and causing the
| |
41 public: | |
42 StaticImageView() : ImageView() { | |
43 } | |
44 | |
45 private: | |
46 // views::View overrides: | |
47 virtual bool HitTest(const gfx::Point& l) const OVERRIDE { | |
48 return false; | |
49 } | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(StaticImageView); | |
52 }; | |
53 | |
38 } // namespace | 54 } // namespace |
39 | 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 |
77 gfx::Size AppListItemView::GetPreferredSize() { | 87 gfx::Size AppListItemView::GetPreferredSize() { |
78 return gfx::Size(kTileSize, kTileSize); | 88 gfx::Size icon_size = icon_->GetPreferredSize(); |
89 gfx::Size title_size = title_->GetPreferredSize(); | |
90 | |
91 return gfx::Size(icon_size.width() + kIconTitleSpacing + title_size.width(), | |
92 std::max(icon_size.height(), title_size.height())); | |
79 } | 93 } |
80 | 94 |
81 void AppListItemView::Layout() { | 95 void AppListItemView::Layout() { |
82 gfx::Rect rect(GetContentsBounds()); | 96 gfx::Rect rect(GetContentsBounds()); |
83 gfx::Size title_size = title_->GetPreferredSize(); | |
84 | 97 |
85 if (!HasFocus()) { | 98 int preferred_icon_size = rect.height() - 2 * kPadding; |
86 const int horiz_padding = (kTileSize - kIconSize) / 2; | 99 gfx::Size icon_size(preferred_icon_size, preferred_icon_size); |
87 const int vert_padding = (kTileSize - title_size.height() - kIconSize) / 2; | 100 icon_->SetImageSize(icon_size); |
88 rect.Inset(horiz_padding, vert_padding); | 101 icon_->SetBounds(rect.x() + kPadding, rect.y(), |
89 } | 102 icon_size.width(), rect.height()); |
90 | 103 |
91 icon_->SetBounds(rect.x(), rect.y(), | 104 title_->SetBounds( |
92 rect.width(), rect.height() - title_size.height()); | 105 icon_->bounds().right() + kIconTitleSpacing, |
93 | 106 rect.y(), |
94 title_->SetBounds(rect.x(), rect.bottom() - title_size.height(), | 107 rect.right() - kPadding - icon_->bounds().right() - kIconTitleSpacing, |
95 rect.width(), title_size.height()); | 108 rect.height()); |
96 } | 109 } |
97 | 110 |
98 void AppListItemView::OnFocus() { | 111 void AppListItemView::OnPaint(gfx::Canvas* canvas) { |
99 View::OnFocus(); | 112 gfx::Rect rect(GetContentsBounds()); |
100 | 113 if (hover_animation_->is_animating()) { |
101 gfx::Size icon_size = icon_->GetPreferredSize(); | 114 int alpha = SkColorGetA(kHoverColor) * hover_animation_->GetCurrentValue(); |
102 icon_size.set_width(icon_size.width() * kFocusedScale); | 115 canvas->FillRect(rect, SkColorSetA(kHoverColor, alpha)); |
103 icon_size.set_height(icon_size.height() * kFocusedScale); | 116 } else if (state() == BS_HOT) { |
104 | 117 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 } | 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 } | 119 } |
161 | 120 |
162 } // namespace ash | 121 } // namespace ash |
OLD | NEW |