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/controls/button/label_button_border.h"
7 #include "base/logging.h"
8 #include "grit/ui_resources.h"
9 #include "third_party/skia/include/core/SkPaint.h"
10 #include "third_party/skia/include/effects/SkLerpXfermode.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/animation/animation.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/gfx/skia_util.h"
16 #include "ui/gfx/sys_color_change_listener.h"
17 #include "ui/native_theme/native_theme.h"
18 #include "ui/views/controls/button/label_button.h"
19 #include "ui/views/native_theme_delegate.h"
25 // Insets for the unified button images. This assumes that the images
26 // are of a 9 grid, of 5x5 size each.
27 const int kButtonInsets = 5;
29 // The text-button hot and pushed image IDs; normal is unadorned by default.
30 const int kTextHoveredImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER);
31 const int kTextPressedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED);
33 Button::ButtonState GetButtonState(ui::NativeTheme::State state) {
35 case ui::NativeTheme::kDisabled: return Button::STATE_DISABLED;
36 case ui::NativeTheme::kHovered: return Button::STATE_HOVERED;
37 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL;
38 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED;
39 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state;
41 return Button::STATE_NORMAL;
44 // A helper function to paint the native theme or images as appropriate.
45 void PaintHelper(LabelButtonBorder* border,
47 const ui::NativeTheme* theme,
48 ui::NativeTheme::Part part,
49 ui::NativeTheme::State state,
50 const gfx::Rect& rect,
51 const ui::NativeTheme::ExtraParams& extra) {
52 if (border->style() == Button::STYLE_NATIVE_TEXTBUTTON) {
53 theme->Paint(canvas->sk_canvas(), part, state, rect, extra);
56 border->GetPainter(extra.button.is_focused, GetButtonState(state));
57 // Paint any corresponding unfocused painter if there is no focused painter.
58 if (!painter && extra.button.is_focused)
59 painter = border->GetPainter(false, GetButtonState(state));
61 painter->Paint(canvas, rect.size());
67 LabelButtonBorder::LabelButtonBorder(Button::ButtonStyle style)
69 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
70 const gfx::Insets insets(kButtonInsets,
75 if (style == Button::STYLE_BUTTON) {
76 set_insets(gfx::Insets(8, 13, 8, 13));
77 SetPainter(false, Button::STATE_NORMAL,
78 Painter::CreateImagePainter(
79 *rb.GetImageSkiaNamed(IDR_BUTTON_NORMAL), insets));
80 SetPainter(false, Button::STATE_HOVERED,
81 Painter::CreateImagePainter(
82 *rb.GetImageSkiaNamed(IDR_BUTTON_HOVER), insets));
83 SetPainter(false, Button::STATE_PRESSED,
84 Painter::CreateImagePainter(
85 *rb.GetImageSkiaNamed(IDR_BUTTON_PRESSED), insets));
86 SetPainter(false, Button::STATE_DISABLED,
87 Painter::CreateImagePainter(
88 *rb.GetImageSkiaNamed(IDR_BUTTON_DISABLED), insets));
89 SetPainter(true, Button::STATE_NORMAL,
90 Painter::CreateImagePainter(
91 *rb.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_NORMAL), insets));
92 SetPainter(true, Button::STATE_HOVERED,
93 Painter::CreateImagePainter(
94 *rb.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_HOVER), insets));
95 SetPainter(true, Button::STATE_PRESSED,
96 Painter::CreateImagePainter(
97 *rb.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_PRESSED), insets));
98 SetPainter(true, Button::STATE_DISABLED,
99 Painter::CreateImagePainter(
100 *rb.GetImageSkiaNamed(IDR_BUTTON_DISABLED), insets));
101 } else if (style == Button::STYLE_TEXTBUTTON) {
102 set_insets(gfx::Insets(5, 6, 5, 6));
103 SetPainter(false, Button::STATE_HOVERED,
104 Painter::CreateImageGridPainter(kTextHoveredImages));
105 SetPainter(false, Button::STATE_PRESSED,
106 Painter::CreateImageGridPainter(kTextPressedImages));
107 } else if (style == Button::STYLE_NATIVE_TEXTBUTTON) {
108 set_insets(gfx::Insets(5, 12, 5, 12));
112 LabelButtonBorder::~LabelButtonBorder() {}
114 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) {
115 const NativeThemeDelegate* native_theme_delegate =
116 static_cast<const LabelButton*>(&view);
117 ui::NativeTheme::Part part = native_theme_delegate->GetThemePart();
118 gfx::Rect rect(native_theme_delegate->GetThemePaintRect());
119 ui::NativeTheme::ExtraParams extra;
120 const ui::NativeTheme* theme = view.GetNativeTheme();
121 const gfx::Animation* animation = native_theme_delegate->GetThemeAnimation();
122 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra);
124 if (animation && animation->is_animating()) {
125 // Linearly interpolate background and foreground painters during animation.
126 const SkRect sk_rect = gfx::RectToSkRect(rect);
127 canvas->sk_canvas()->saveLayer(&sk_rect, NULL);
128 state = native_theme_delegate->GetBackgroundThemeState(&extra);
129 PaintHelper(this, canvas, theme, part, state, rect, extra);
132 skia::RefPtr<SkXfermode> sk_lerp_xfer =
133 skia::AdoptRef(SkLerpXfermode::Create(animation->GetCurrentValue()));
134 paint.setXfermode(sk_lerp_xfer.get());
135 canvas->sk_canvas()->saveLayer(&sk_rect, &paint);
136 state = native_theme_delegate->GetForegroundThemeState(&extra);
137 PaintHelper(this, canvas, theme, part, state, rect, extra);
138 canvas->sk_canvas()->restore();
140 canvas->sk_canvas()->restore();
142 PaintHelper(this, canvas, theme, part, state, rect, extra);
145 // For inverted color schemes, draw a solid fill with the button color.
146 if (gfx::IsInvertedColorScheme()) {
148 canvas->FillRect(rect, extra.button.background_color);
151 // Draw the Views focus border for the native theme style.
152 if (style() == Button::STYLE_NATIVE_TEXTBUTTON &&
153 view.focus_border() && extra.button.is_focused)
154 view.focus_border()->Paint(view, canvas);
157 gfx::Insets LabelButtonBorder::GetInsets() const {
161 Painter* LabelButtonBorder::GetPainter(bool focused,
162 Button::ButtonState state) {
163 return painters_[focused ? 1 : 0][state].get();
166 void LabelButtonBorder::SetPainter(bool focused,
167 Button::ButtonState state,
169 painters_[focused ? 1 : 0][state].reset(painter);