Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: ui/aura_shell/app_list/tile_view.cc

Issue 8890049: [Aura] Implement views-based applist. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased and refactored Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/tile_view.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/font.h"
11 #include "ui/aura_shell/app_list/app_list_view.h"
12 #include "ui/aura_shell/app_list/app_list_item_model.h"
13 #include "ui/aura_shell/app_list/drop_shadow_label.h"
14 #include "ui/aura_shell/app_list/tiles_page_view.h"
15 #include "ui/views/controls/image_view.h"
16 #include "ui/views/controls/label.h"
17 #include "ui/views/widget/widget.h"
18 #include "third_party/skia/include/core/SkColor.h"
19
20 namespace aura_shell {
21
22 namespace {
23
24 const double kFocusedScale = 1.1;
25
26 // TitleLabel for a TileView.
27 class TitleLabel : public DropShadowLabel {
28 public:
29 TitleLabel() {
30 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
31 SetFont(rb.GetFont(ResourceBundle::BaseFont).DeriveFont(
32 2, gfx::Font::BOLD));
33 SetBackgroundColor(0);
34 SetEnabledColor(SK_ColorWHITE);
35 }
36
37 virtual ~TitleLabel() {}
38
39 private:
40 DISALLOW_COPY_AND_ASSIGN(TitleLabel);
41 };
42
43 } // namespace
44
45 TileView::TileView(AppListItemModel* model)
46 : model_(model),
47 icon_(new views::ImageView),
48 label_(new TitleLabel) {
49 set_focusable(true);
50
51 AddChildView(icon_);
52 ItemIconChanged();
53
54 AddChildView(label_);
55 ItemTitleChanged();
56
57 model_->AddObserver(this);
58 }
59
60 TileView::~TileView() {
61 model_->RemoveObserver(this);
62 }
63
64 void TileView::ItemIconChanged() {
65 icon_->SetImage(model_->icon());
66 }
67
68 void TileView::ItemTitleChanged() {
69 label_->SetText(UTF8ToUTF16(model_->title()));
70 }
71
72 void TileView::Activate(int event_flags) {
73 model_->Activate(event_flags);
74 static_cast<AppListView*>(GetWidget()->widget_delegate())->Close();
75 }
76
77 gfx::Size TileView::GetPreferredSize() {
78 return gfx::Size(kTileSize, kTileSize);
79 }
80
81 void TileView::Layout() {
82 gfx::Rect rect(GetContentsBounds());
83
84 gfx::Size title_size = label_->GetPreferredSize();
85
86 if (!HasFocus()) {
87 const int horiz_padding = (kTileSize - kIconSize) / 2;
88 const int vert_padding = (kTileSize - title_size.height() - kIconSize) / 2;
89 rect.Inset(horiz_padding, vert_padding);
90 }
91
92 icon_->SetBounds(rect.x(), rect.y(),
93 rect.width(), rect.height() - title_size.height());
94
95 label_->SetBounds(rect.x(), rect.bottom() - title_size.height(),
96 rect.width(), title_size.height());
97 }
98
99 void TileView::OnFocus() {
100 gfx::Size icon_size = icon_->GetPreferredSize();
101 icon_size.set_width(icon_size.width() * kFocusedScale);
102 icon_size.set_height(icon_size.height() * kFocusedScale);
103
104 gfx::Size max_size = GetPreferredSize();
105 if (icon_size.width() > max_size.width() ||
106 icon_size.height() > max_size.height()) {
107 double aspect =
108 static_cast<double>(icon_size.width()) / icon_size.height();
109
110 if (aspect > 1) {
111 icon_size.set_width(max_size.width());
112 icon_size.set_height(icon_size.width() / aspect);
113 } else {
114 icon_size.set_height(max_size.height());
115 icon_size.set_width(icon_size.height() * aspect);
116 }
117 }
118
119 icon_->SetImageSize(icon_size);
120 Layout();
121
122 TilesPageView* tiles_page = static_cast<TilesPageView*>(parent());
123 tiles_page->UpdateFocusedTile(this);
124
125 View::OnFocus();
126 }
127
128 void TileView::OnBlur() {
129 icon_->ResetImageSize();
130 Layout();
131 SchedulePaint();
132 }
133
134 bool TileView::OnKeyPressed(const views::KeyEvent& event) {
135 if (event.key_code() == ui::VKEY_RETURN) {
136 Activate(event.flags());
137 return true;
138 }
139
140 return false;
141 }
142
143 bool TileView::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 TileView::OnMouseReleased(const views::MouseEvent& event) {
153 views::View* hit_view = GetEventHandlerForPoint(event.location());
154 if (hit_view != this)
155 Activate(event.flags());
156 }
157
158 void TileView::OnPaintFocusBorder(gfx::Canvas* canvas) {
159 // No focus border for TileView.
160 }
161
162 } // namespace aura_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698