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.
4
5#include "ui/app_list/views/cached_label.h"
6
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"
11
12namespace app_list {
13
14CachedLabel::CachedLabel()
15    : needs_repaint_(true) {
16}
17
18void CachedLabel::PaintToBackingImage() {
19  if (image_.size() == size() && !needs_repaint_)
20    return;
21
22  bool is_opaque = SkColorGetA(background_color()) == 0xFF;
23  float scale_factor =
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.
28  if (!background()) {
29    canvas.FillRect(
30        GetLocalBounds(), background_color(), SkXfermode::kSrc_Mode);
31  }
32  Label::OnPaint(&canvas);
33  image_ = gfx::ImageSkia(canvas.ExtractImageRep());
34  needs_repaint_ = false;
35}
36
37#if defined(OS_WIN)
38void CachedLabel::OnPaint(gfx::Canvas* canvas) {
39  PaintToBackingImage();
40  canvas->DrawImageInt(image_, 0, 0);
41}
42#endif
43
44void CachedLabel::OnDeviceScaleFactorChanged(
45    float device_scale_factor) {
46  Invalidate();
47  View::OnDeviceScaleFactorChanged(device_scale_factor);
48}
49
50}  // namespace app_list
51