Consolidate bubble border code.
[chromium-blink-merge.git] / ui / views / native_theme_painter.cc
blob8acb532b21212526ae15741d747f6db4a34cee7c
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.
5 #include "ui/views/native_theme_painter.h"
7 #include "base/logging.h"
8 #include "ui/base/animation/animation.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/rect.h"
11 #include "ui/views/native_theme_delegate.h"
13 namespace views {
15 NativeThemePainter::NativeThemePainter(NativeThemeDelegate* delegate)
16 : delegate_(delegate) {
17 DCHECK(delegate_);
20 gfx::Size NativeThemePainter::GetPreferredSize() {
21 const ui::NativeTheme* theme = ui::NativeTheme::instance();
22 ui::NativeTheme::ExtraParams extra;
23 ui::NativeTheme::State state = delegate_->GetThemeState(&extra);
24 return theme->GetPartSize(delegate_->GetThemePart(), state, extra);
27 void NativeThemePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
28 const ui::NativeTheme* native_theme = ui::NativeTheme::instance();
29 ui::NativeTheme::Part part = delegate_->GetThemePart();
30 gfx::Rect rect(size);
32 if (delegate_->GetThemeAnimation() != NULL &&
33 delegate_->GetThemeAnimation()->is_animating()) {
34 // Paint background state.
35 ui::NativeTheme::ExtraParams prev_extra;
36 ui::NativeTheme::State prev_state =
37 delegate_->GetBackgroundThemeState(&prev_extra);
38 native_theme->Paint(canvas->sk_canvas(), part, prev_state, rect,
39 prev_extra);
41 // Composite foreground state above it.
42 ui::NativeTheme::ExtraParams extra;
43 ui::NativeTheme::State state = delegate_->GetForegroundThemeState(&extra);
44 int alpha = delegate_->GetThemeAnimation()->CurrentValueBetween(0, 255);
45 canvas->SaveLayerAlpha(static_cast<uint8>(alpha));
46 native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra);
47 canvas->Restore();
48 } else {
49 ui::NativeTheme::ExtraParams extra;
50 ui::NativeTheme::State state = delegate_->GetThemeState(&extra);
51 native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra);
55 } // namespace views