1 // Copyright 2013 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.
5 #include "ui/app_list/views/cached_label.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/base/layout.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/widget/widget.h"
14 CachedLabel::CachedLabel()
15 : needs_repaint_(true) {
18 void CachedLabel::PaintToBackingImage() {
19 if (image_
.size() == size() && !needs_repaint_
)
22 bool is_opaque
= SkColorGetA(background_color()) == 0xFF;
24 ui::GetScaleFactorForNativeView(GetWidget()->GetNativeView());
25 gfx::Canvas
canvas(size(), scale_factor
, is_opaque
);
26 // If a background is provided, it will initialize the canvas in
27 // View::OnPaintBackground(). Otherwise, the background must be set here.
30 GetLocalBounds(), background_color(), SkXfermode::kSrc_Mode
);
32 Label::OnPaint(&canvas
);
33 image_
= gfx::ImageSkia(canvas
.ExtractImageRep());
34 needs_repaint_
= false;
38 void CachedLabel::OnPaint(gfx::Canvas
* canvas
) {
39 PaintToBackingImage();
40 canvas
->DrawImageInt(image_
, 0, 0);
44 void CachedLabel::OnDeviceScaleFactorChanged(
45 float device_scale_factor
) {
47 View::OnDeviceScaleFactorChanged(device_scale_factor
);
50 } // namespace app_list