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