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

Side by Side Diff: ash/wm/image_grid.cc

Issue 11275296: Move shadow code to views\corewm (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 1 month 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
« no previous file with comments | « ash/wm/image_grid.h ('k') | ash/wm/image_grid_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/wm/image_grid.h"
6
7 #include <algorithm>
8
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "third_party/skia/include/core/SkXfermode.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/image/image.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/rect_conversions.h"
15 #include "ui/gfx/transform.h"
16
17 using std::max;
18 using std::min;
19
20 namespace ash {
21 namespace internal {
22
23 gfx::RectF ImageGrid::TestAPI::GetTransformedLayerBounds(
24 const ui::Layer& layer) {
25 gfx::RectF bounds = layer.bounds();
26 layer.transform().TransformRect(&bounds);
27 return bounds;
28 }
29
30 ImageGrid::ImageGrid()
31 : layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
32 top_image_height_(0),
33 bottom_image_height_(0),
34 left_image_width_(0),
35 right_image_width_(0),
36 base_top_row_height_(0),
37 base_bottom_row_height_(0),
38 base_left_column_width_(0),
39 base_right_column_width_(0) {
40 }
41
42 ImageGrid::~ImageGrid() {
43 }
44
45 void ImageGrid::SetImages(const gfx::Image* top_left_image,
46 const gfx::Image* top_image,
47 const gfx::Image* top_right_image,
48 const gfx::Image* left_image,
49 const gfx::Image* center_image,
50 const gfx::Image* right_image,
51 const gfx::Image* bottom_left_image,
52 const gfx::Image* bottom_image,
53 const gfx::Image* bottom_right_image) {
54 SetImage(top_left_image, &top_left_layer_, &top_left_painter_);
55 SetImage(top_image, &top_layer_, &top_painter_);
56 SetImage(top_right_image, &top_right_layer_, &top_right_painter_);
57 SetImage(left_image, &left_layer_, &left_painter_);
58 SetImage(center_image, &center_layer_, &center_painter_);
59 SetImage(right_image, &right_layer_, &right_painter_);
60 SetImage(bottom_left_image, &bottom_left_layer_, &bottom_left_painter_);
61 SetImage(bottom_image, &bottom_layer_, &bottom_painter_);
62 SetImage(bottom_right_image, &bottom_right_layer_, &bottom_right_painter_);
63
64 top_image_height_ = GetImageSize(top_image).height();
65 bottom_image_height_ = GetImageSize(bottom_image).height();
66 left_image_width_ = GetImageSize(left_image).width();
67 right_image_width_ = GetImageSize(right_image).width();
68
69 base_top_row_height_ = max(GetImageSize(top_left_image).height(),
70 max(GetImageSize(top_image).height(),
71 GetImageSize(top_right_image).height()));
72 base_bottom_row_height_ = max(GetImageSize(bottom_left_image).height(),
73 max(GetImageSize(bottom_image).height(),
74 GetImageSize(bottom_right_image).height()));
75 base_left_column_width_ = max(GetImageSize(top_left_image).width(),
76 max(GetImageSize(left_image).width(),
77 GetImageSize(bottom_left_image).width()));
78 base_right_column_width_ = max(GetImageSize(top_right_image).width(),
79 max(GetImageSize(right_image).width(),
80 GetImageSize(bottom_right_image).width()));
81
82 // Invalidate previous |size_| so calls to SetSize() will recompute it.
83 size_.SetSize(0, 0);
84 }
85
86 void ImageGrid::SetSize(const gfx::Size& size) {
87 if (size_ == size)
88 return;
89
90 size_ = size;
91
92 gfx::Rect updated_bounds = layer_->bounds();
93 updated_bounds.set_size(size);
94 layer_->SetBounds(updated_bounds);
95
96 // Calculate the available amount of space for corner images on all sides of
97 // the grid. If the images don't fit, we need to clip them.
98 const int left = min(base_left_column_width_, size_.width() / 2);
99 const int right = min(base_right_column_width_, size_.width() - left);
100 const int top = min(base_top_row_height_, size_.height() / 2);
101 const int bottom = min(base_bottom_row_height_, size_.height() - top);
102
103 // The remaining space goes to the center image.
104 int center_width = std::max(size.width() - left - right, 0);
105 int center_height = std::max(size.height() - top - bottom, 0);
106
107 if (top_layer_.get()) {
108 if (center_width > 0) {
109 gfx::Transform transform;
110 transform.SetScaleX(
111 static_cast<float>(center_width) / top_layer_->bounds().width());
112 transform.ConcatTranslate(left, 0);
113 top_layer_->SetTransform(transform);
114 }
115 top_layer_->SetVisible(center_width > 0);
116 }
117 if (bottom_layer_.get()) {
118 if (center_width > 0) {
119 gfx::Transform transform;
120 transform.SetScaleX(
121 static_cast<float>(center_width) / bottom_layer_->bounds().width());
122 transform.ConcatTranslate(
123 left, size.height() - bottom_layer_->bounds().height());
124 bottom_layer_->SetTransform(transform);
125 }
126 bottom_layer_->SetVisible(center_width > 0);
127 }
128 if (left_layer_.get()) {
129 if (center_height > 0) {
130 gfx::Transform transform;
131 transform.SetScaleY(
132 (static_cast<float>(center_height) / left_layer_->bounds().height()));
133 transform.ConcatTranslate(0, top);
134 left_layer_->SetTransform(transform);
135 }
136 left_layer_->SetVisible(center_height > 0);
137 }
138 if (right_layer_.get()) {
139 if (center_height > 0) {
140 gfx::Transform transform;
141 transform.SetScaleY(
142 static_cast<float>(center_height) / right_layer_->bounds().height());
143 transform.ConcatTranslate(
144 size.width() - right_layer_->bounds().width(), top);
145 right_layer_->SetTransform(transform);
146 }
147 right_layer_->SetVisible(center_height > 0);
148 }
149
150 if (top_left_layer_.get()) {
151 // No transformation needed; it should be at (0, 0) and unscaled.
152 top_left_painter_->SetClipRect(
153 LayerExceedsSize(top_left_layer_.get(), gfx::Size(left, top)) ?
154 gfx::Rect(gfx::Rect(0, 0, left, top)) :
155 gfx::Rect(),
156 top_left_layer_.get());
157 }
158 if (top_right_layer_.get()) {
159 gfx::Transform transform;
160 transform.SetTranslateX(size.width() - top_right_layer_->bounds().width());
161 top_right_layer_->SetTransform(transform);
162 top_right_painter_->SetClipRect(
163 LayerExceedsSize(top_right_layer_.get(), gfx::Size(right, top)) ?
164 gfx::Rect(top_right_layer_->bounds().width() - right, 0,
165 right, top) :
166 gfx::Rect(),
167 top_right_layer_.get());
168 }
169 if (bottom_left_layer_.get()) {
170 gfx::Transform transform;
171 transform.SetTranslateY(
172 size.height() - bottom_left_layer_->bounds().height());
173 bottom_left_layer_->SetTransform(transform);
174 bottom_left_painter_->SetClipRect(
175 LayerExceedsSize(bottom_left_layer_.get(), gfx::Size(left, bottom)) ?
176 gfx::Rect(0, bottom_left_layer_->bounds().height() - bottom,
177 left, bottom) :
178 gfx::Rect(),
179 bottom_left_layer_.get());
180 }
181 if (bottom_right_layer_.get()) {
182 gfx::Transform transform;
183 transform.SetTranslate(
184 size.width() - bottom_right_layer_->bounds().width(),
185 size.height() - bottom_right_layer_->bounds().height());
186 bottom_right_layer_->SetTransform(transform);
187 bottom_right_painter_->SetClipRect(
188 LayerExceedsSize(bottom_right_layer_.get(), gfx::Size(right, bottom)) ?
189 gfx::Rect(bottom_right_layer_->bounds().width() - right,
190 bottom_right_layer_->bounds().height() - bottom,
191 right, bottom) :
192 gfx::Rect(),
193 bottom_right_layer_.get());
194 }
195
196 if (center_layer_.get()) {
197 if (center_width > 0 && center_height > 0) {
198 gfx::Transform transform;
199 transform.SetScale(center_width / center_layer_->bounds().width(),
200 center_height / center_layer_->bounds().height());
201 transform.ConcatTranslate(left, top);
202 center_layer_->SetTransform(transform);
203 }
204 center_layer_->SetVisible(center_width > 0 && center_height > 0);
205 }
206 }
207
208 void ImageGrid::SetContentBounds(const gfx::Rect& content_bounds) {
209 SetSize(gfx::Size(
210 content_bounds.width() + left_image_width_ + right_image_width_,
211 content_bounds.height() + top_image_height_ +
212 bottom_image_height_));
213 layer_->SetBounds(
214 gfx::Rect(content_bounds.x() - left_image_width_,
215 content_bounds.y() - top_image_height_,
216 layer_->bounds().width(),
217 layer_->bounds().height()));
218 }
219
220 void ImageGrid::ImagePainter::SetClipRect(const gfx::Rect& clip_rect,
221 ui::Layer* layer) {
222 if (clip_rect != clip_rect_) {
223 clip_rect_ = clip_rect;
224 layer->SchedulePaint(layer->bounds());
225 }
226 }
227
228 void ImageGrid::ImagePainter::OnPaintLayer(gfx::Canvas* canvas) {
229 if (!clip_rect_.IsEmpty())
230 canvas->ClipRect(clip_rect_);
231 canvas->DrawImageInt(*(image_->ToImageSkia()), 0, 0);
232 }
233
234 void ImageGrid::ImagePainter::OnDeviceScaleFactorChanged(
235 float device_scale_factor) {
236 // Redrawing will take care of scale factor change.
237 }
238
239 base::Closure ImageGrid::ImagePainter::PrepareForLayerBoundsChange() {
240 return base::Closure();
241 }
242
243 // static
244 gfx::Size ImageGrid::GetImageSize(const gfx::Image* image) {
245 return image ?
246 gfx::Size(image->ToImageSkia()->width(), image->ToImageSkia()->height()) :
247 gfx::Size();
248 }
249
250 // static
251 bool ImageGrid::LayerExceedsSize(const ui::Layer* layer,
252 const gfx::Size& size) {
253 return layer->bounds().width() > size.width() ||
254 layer->bounds().height() > size.height();
255 }
256
257 void ImageGrid::SetImage(const gfx::Image* image,
258 scoped_ptr<ui::Layer>* layer_ptr,
259 scoped_ptr<ImagePainter>* painter_ptr) {
260 // Clean out old layers and painters.
261 if (layer_ptr->get())
262 layer_->Remove(layer_ptr->get());
263 layer_ptr->reset();
264 painter_ptr->reset();
265
266 // If we're not using an image, we're done.
267 if (!image)
268 return;
269
270 // Set up the new layer and painter.
271 layer_ptr->reset(new ui::Layer(ui::LAYER_TEXTURED));
272
273 const gfx::Size size = GetImageSize(image);
274 layer_ptr->get()->SetBounds(gfx::Rect(0, 0, size.width(), size.height()));
275
276 painter_ptr->reset(new ImagePainter(image));
277 layer_ptr->get()->set_delegate(painter_ptr->get());
278 layer_ptr->get()->SetFillsBoundsOpaquely(false);
279 layer_ptr->get()->SetVisible(true);
280 layer_->Add(layer_ptr->get());
281 }
282
283 } // namespace internal
284 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/image_grid.h ('k') | ash/wm/image_grid_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698