OLD | NEW |
1 // Copyright (c) 2012 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_model.h" | 7 #include "ash/app_list/app_list_item_model.h" |
8 #include "ash/app_list/app_list_model_view.h" | 8 #include "ash/app_list/app_list_model_view.h" |
9 #include "ash/app_list/drop_shadow_label.h" | 9 #include "ash/app_list/drop_shadow_label.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #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" | 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/menu/menu_item_view.h" | 17 #include "ui/views/controls/menu/menu_item_view.h" |
18 #include "ui/views/controls/menu/menu_model_adapter.h" | 18 #include "ui/views/controls/menu/menu_model_adapter.h" |
19 #include "ui/views/controls/menu/menu_runner.h" | 19 #include "ui/views/controls/menu/menu_runner.h" |
20 | 20 |
21 namespace ash { | 21 namespace ash { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const int kIconTitleSpacing = 5; | 25 const int kPadding = 10; |
| 26 const int kIconTitleSpacing = 10; |
| 27 const int kMinLabelWidth = 150; |
26 | 28 |
27 const SkColor kTitleColor = SK_ColorWHITE; | 29 const SkColor kTitleColor = SK_ColorWHITE; |
28 | 30 |
29 // 0.2 white | 31 // 0.2 white |
30 const SkColor kHoverAndPushedColor = SkColorSetARGB(0x33, 0xFF, 0xFF, 0xFF); | 32 const SkColor kHoverAndPushedColor = SkColorSetARGB(0x33, 0xFF, 0xFF, 0xFF); |
31 | 33 |
32 // 0.1 white | 34 // 0.1 white |
33 const SkColor kSelectedColor = SkColorSetARGB(0x20, 0xFF, 0xFF, 0xFF); | 35 const SkColor kSelectedColor = SkColorSetARGB(0x20, 0xFF, 0xFF, 0xFF); |
34 | 36 |
35 gfx::Font GetTitleFont() { | 37 gfx::Font GetTitleFont() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 views::ButtonListener* listener) | 69 views::ButtonListener* listener) |
68 : CustomButton(listener), | 70 : CustomButton(listener), |
69 model_(model), | 71 model_(model), |
70 list_model_view_(list_model_view), | 72 list_model_view_(list_model_view), |
71 icon_(new StaticImageView), | 73 icon_(new StaticImageView), |
72 title_(new DropShadowLabel), | 74 title_(new DropShadowLabel), |
73 selected_(false) { | 75 selected_(false) { |
74 title_->SetFont(GetTitleFont()); | 76 title_->SetFont(GetTitleFont()); |
75 title_->SetBackgroundColor(0); | 77 title_->SetBackgroundColor(0); |
76 title_->SetEnabledColor(kTitleColor); | 78 title_->SetEnabledColor(kTitleColor); |
77 title_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
78 | 79 |
79 AddChildView(icon_); | 80 AddChildView(icon_); |
80 AddChildView(title_); | 81 AddChildView(title_); |
81 | 82 |
82 ItemIconChanged(); | 83 ItemIconChanged(); |
83 ItemTitleChanged(); | 84 ItemTitleChanged(); |
84 model_->AddObserver(this); | 85 model_->AddObserver(this); |
85 | 86 |
86 set_context_menu_controller(this); | 87 set_context_menu_controller(this); |
87 set_request_focus_on_press(false); | 88 set_request_focus_on_press(false); |
88 } | 89 } |
89 | 90 |
90 AppListItemView::~AppListItemView() { | 91 AppListItemView::~AppListItemView() { |
91 model_->RemoveObserver(this); | 92 model_->RemoveObserver(this); |
92 } | 93 } |
93 | 94 |
| 95 // static |
| 96 gfx::Size AppListItemView::GetPreferredSizeForIconSize( |
| 97 const gfx::Size& icon_size) { |
| 98 gfx::Size size( |
| 99 std::max(icon_size.width() + icon_size.width() / 2, kMinLabelWidth), |
| 100 icon_size.height() + kIconTitleSpacing + GetTitleFont().GetHeight()); |
| 101 size.Enlarge(2 * kPadding, 2 * kPadding); |
| 102 return size; |
| 103 } |
| 104 |
94 void AppListItemView::SetSelected(bool selected) { | 105 void AppListItemView::SetSelected(bool selected) { |
95 if (selected == selected_) | 106 if (selected == selected_) |
96 return; | 107 return; |
97 | 108 |
98 selected_ = selected; | 109 selected_ = selected; |
99 SchedulePaint(); | 110 SchedulePaint(); |
100 } | 111 } |
101 | 112 |
102 void AppListItemView::ItemIconChanged() { | 113 void AppListItemView::ItemIconChanged() { |
103 icon_->SetImage(model_->icon()); | 114 icon_->SetImage(model_->icon()); |
104 } | 115 } |
105 | 116 |
106 void AppListItemView::ItemTitleChanged() { | 117 void AppListItemView::ItemTitleChanged() { |
107 title_->SetText(UTF8ToUTF16(model_->title())); | 118 title_->SetText(UTF8ToUTF16(model_->title())); |
108 } | 119 } |
109 | 120 |
110 std::string AppListItemView::GetClassName() const { | 121 std::string AppListItemView::GetClassName() const { |
111 return kViewClassName; | 122 return kViewClassName; |
112 } | 123 } |
113 | 124 |
114 gfx::Size AppListItemView::GetPreferredSize() { | 125 gfx::Size AppListItemView::GetPreferredSize() { |
115 gfx::Size title_size = title_->GetPreferredSize(); | 126 return GetPreferredSizeForIconSize(icon_size_); |
116 | |
117 gfx::Size preferred_size( | |
118 icon_size_.width() + kIconTitleSpacing + title_size.width(), | |
119 std::max(icon_size_.height(), title_size.height())); | |
120 preferred_size.Enlarge(2 * kPadding, 2 * kPadding); | |
121 return preferred_size; | |
122 } | 127 } |
123 | 128 |
124 void AppListItemView::Layout() { | 129 void AppListItemView::Layout() { |
125 gfx::Rect rect(GetContentsBounds()); | 130 gfx::Rect rect(GetContentsBounds()); |
| 131 rect.Inset(kPadding, kPadding); |
| 132 gfx::Size title_size = title_->GetPreferredSize(); |
126 | 133 |
127 icon_->SetImageSize(icon_size_); | 134 icon_->SetImageSize(icon_size_); |
128 icon_->SetBounds(rect.x() + kPadding, rect.y(), | 135 icon_->SetBounds(rect.x(), |
129 icon_size_.width(), rect.height()); | 136 rect.y(), |
| 137 rect.width(), |
| 138 icon_size_.height()); |
130 | 139 |
131 title_->SetBounds( | 140 title_->SetBounds(rect.x(), |
132 icon_->bounds().right() + kIconTitleSpacing, | 141 icon_->bounds().bottom() + kIconTitleSpacing, |
133 rect.y(), | 142 rect.width(), |
134 rect.right() - kPadding - icon_->bounds().right() - kIconTitleSpacing, | 143 title_size.height()); |
135 rect.height()); | |
136 } | 144 } |
137 | 145 |
138 void AppListItemView::OnPaint(gfx::Canvas* canvas) { | 146 void AppListItemView::OnPaint(gfx::Canvas* canvas) { |
139 gfx::Rect rect(GetContentsBounds()); | 147 gfx::Rect rect(GetContentsBounds()); |
140 | 148 |
141 if (hover_animation_->is_animating()) { | 149 if (hover_animation_->is_animating()) { |
142 int alpha = SkColorGetA(kHoverAndPushedColor) * | 150 int alpha = SkColorGetA(kHoverAndPushedColor) * |
143 hover_animation_->GetCurrentValue(); | 151 hover_animation_->GetCurrentValue(); |
144 canvas->FillRect(rect, SkColorSetA(kHoverAndPushedColor, alpha)); | 152 canvas->FillRect(rect, SkColorSetA(kHoverAndPushedColor, alpha)); |
145 } else if (state() == BS_HOT || state() == BS_PUSHED) { | 153 } else if (state() == BS_HOT || state() == BS_PUSHED) { |
(...skipping 21 matching lines...) Expand all Loading... |
167 } | 175 } |
168 | 176 |
169 void AppListItemView::StateChanged() { | 177 void AppListItemView::StateChanged() { |
170 if (state() == BS_HOT || state() == BS_PUSHED) | 178 if (state() == BS_HOT || state() == BS_PUSHED) |
171 list_model_view_->SetSelectedItem(this); | 179 list_model_view_->SetSelectedItem(this); |
172 else | 180 else |
173 list_model_view_->ClearSelectedItem(this); | 181 list_model_view_->ClearSelectedItem(this); |
174 } | 182 } |
175 | 183 |
176 } // namespace ash | 184 } // namespace ash |
OLD | NEW |