Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: ui/views/controls/button/label_button_border.cc

Issue 14921006: Fix LabelButtonBorder image blending. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use a skia::RefPtr to free the SkXfermode after use. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/button/label_button_border.h" 5 #include "ui/views/controls/button/label_button_border.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "grit/ui_resources.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"
9 #include "ui/base/animation/animation.h" 11 #include "ui/base/animation/animation.h"
10 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/rect.h" 13 #include "ui/gfx/rect.h"
12 #include "ui/native_theme/native_theme.h" 14 #include "ui/native_theme/native_theme.h"
13 #include "ui/views/controls/button/label_button.h" 15 #include "ui/views/controls/button/label_button.h"
14 #include "ui/views/native_theme_delegate.h" 16 #include "ui/views/native_theme_delegate.h"
15 17
16 namespace views { 18 namespace views {
17 19
18 namespace { 20 namespace {
(...skipping 18 matching lines...) Expand all
37 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL; 39 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL;
38 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED; 40 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED;
39 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state; 41 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state;
40 } 42 }
41 return Button::STATE_NORMAL; 43 return Button::STATE_NORMAL;
42 } 44 }
43 45
44 // A helper function to paint the native theme or images as appropriate. 46 // A helper function to paint the native theme or images as appropriate.
45 void PaintHelper(LabelButtonBorder* border, 47 void PaintHelper(LabelButtonBorder* border,
46 gfx::Canvas* canvas, 48 gfx::Canvas* canvas,
49 const SkPaint& paint,
47 const ui::NativeTheme* theme, 50 const ui::NativeTheme* theme,
48 ui::NativeTheme::Part part, 51 ui::NativeTheme::Part part,
49 ui::NativeTheme::State state, 52 ui::NativeTheme::State state,
50 const gfx::Rect& rect, 53 const gfx::Rect& rect,
51 const ui::NativeTheme::ExtraParams& extra) { 54 const ui::NativeTheme::ExtraParams& extra) {
52 if (border->style() == Button::STYLE_NATIVE_TEXTBUTTON) { 55 if (border->style() == Button::STYLE_NATIVE_TEXTBUTTON) {
53 theme->Paint(canvas->sk_canvas(), part, state, rect, extra); 56 theme->Paint(canvas->sk_canvas(), part, state, rect, extra);
54 } else { 57 } else {
55 Painter* painter = 58 Painter* painter =
56 border->GetPainter(extra.button.is_focused, GetButtonState(state)); 59 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));
60 if (painter) 60 if (painter)
61 painter->Paint(canvas, rect.size()); 61 painter->PaintWithSkPaint(canvas, rect.size(), paint);
62 } 62 }
63 } 63 }
64 64
65 } // namespace 65 } // namespace
66 66
67 LabelButtonBorder::LabelButtonBorder(Button::ButtonStyle style) 67 LabelButtonBorder::LabelButtonBorder(Button::ButtonStyle style)
68 : style_(style) { 68 : style_(style) {
69 if (style == Button::STYLE_BUTTON) { 69 if (style == Button::STYLE_BUTTON) {
70 set_insets(gfx::Insets(9, 13, 9, 13)); 70 set_insets(gfx::Insets(9, 13, 9, 13));
71 SetPainter(false, Button::STATE_NORMAL, 71 SetPainter(false, Button::STATE_NORMAL,
(...skipping 19 matching lines...) Expand all
91 SetPainter(false, Button::STATE_PRESSED, 91 SetPainter(false, Button::STATE_PRESSED,
92 Painter::CreateImageGridPainter(kTextPressedImages)); 92 Painter::CreateImageGridPainter(kTextPressedImages));
93 } else if (style == Button::STYLE_NATIVE_TEXTBUTTON) { 93 } else if (style == Button::STYLE_NATIVE_TEXTBUTTON) {
94 set_insets(gfx::Insets(5, 12, 5, 12)); 94 set_insets(gfx::Insets(5, 12, 5, 12));
95 } 95 }
96 } 96 }
97 97
98 LabelButtonBorder::~LabelButtonBorder() {} 98 LabelButtonBorder::~LabelButtonBorder() {}
99 99
100 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) { 100 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) {
101 const NativeThemeDelegate* native_theme_delegate = 101 const NativeThemeDelegate* delegate = static_cast<const LabelButton*>(&view);
102 static_cast<const LabelButton*>(&view); 102 const ui::NativeTheme::Part part = delegate->GetThemePart();
103 ui::NativeTheme::Part part = native_theme_delegate->GetThemePart(); 103 const gfx::Rect rect(delegate->GetThemePaintRect());
104 gfx::Rect rect(native_theme_delegate->GetThemePaintRect());
105 ui::NativeTheme::ExtraParams extra; 104 ui::NativeTheme::ExtraParams extra;
106 const ui::NativeTheme* theme = view.GetNativeTheme(); 105 const ui::NativeTheme* theme = view.GetNativeTheme();
107 const ui::Animation* animation = native_theme_delegate->GetThemeAnimation(); 106 const ui::Animation* animation = delegate->GetThemeAnimation();
108 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra); 107 SkPaint paint;
109 108
110 if (animation && animation->is_animating()) { 109 if (animation && animation->is_animating()) {
111 // Composite the background and foreground painters during state animations. 110 // Composite the background and foreground painters during state animations.
112 int alpha = animation->CurrentValueBetween(0, 0xff); 111 ui::NativeTheme::State back = delegate->GetBackgroundThemeState(&extra);
113 state = native_theme_delegate->GetBackgroundThemeState(&extra); 112 ui::NativeTheme::State fore = delegate->GetForegroundThemeState(&extra);
sky 2013/06/13 16:22:04 foreground, or fg.
114 canvas->SaveLayerAlpha(static_cast<uint8>(0xff - alpha)); 113 if (style() == Button::STYLE_NATIVE_TEXTBUTTON) {
115 PaintHelper(this, canvas, theme, part, state, rect, extra); 114 // NativeTheme does not support image interpolation, use alpha blending.
116 canvas->Restore(); 115 PaintHelper(this, canvas, paint, theme, part, back, rect, extra);
117 116 const int alpha = animation->CurrentValueBetween(0, 0xff);
118 state = native_theme_delegate->GetForegroundThemeState(&extra); 117 canvas->SaveLayerAlpha(static_cast<uint8>(alpha));
119 canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); 118 PaintHelper(this, canvas, paint, theme, part, fore, rect, extra);
120 PaintHelper(this, canvas, theme, part, state, rect, extra); 119 canvas->Restore();
121 canvas->Restore(); 120 } else {
121 canvas->Save();
122 // Use linear interpolation to blend background and foreground images.
123 PaintHelper(this, canvas, paint, theme, part, back, rect, extra);
124 skia::RefPtr<SkXfermode> mode(
125 skia::AdoptRef(SkLerpXfermode::Create(animation->GetCurrentValue())));
126 paint.setXfermode(mode.get());
127 PaintHelper(this, canvas, paint, theme, part, fore, rect, extra);
128 canvas->Restore();
129 }
122 } else { 130 } else {
123 PaintHelper(this, canvas, theme, part, state, rect, extra); 131 ui::NativeTheme::State state = delegate->GetThemeState(&extra);
132 PaintHelper(this, canvas, paint, theme, part, state, rect, extra);
124 } 133 }
125 134
126 // Draw the Views focus border for the native theme style. 135 // Draw the Views focus border for the native theme style.
127 if (style() == Button::STYLE_NATIVE_TEXTBUTTON && 136 if (style() == Button::STYLE_NATIVE_TEXTBUTTON &&
128 view.focus_border() && extra.button.is_focused) 137 view.focus_border() && extra.button.is_focused)
129 view.focus_border()->Paint(view, canvas); 138 view.focus_border()->Paint(view, canvas);
130 } 139 }
131 140
132 gfx::Insets LabelButtonBorder::GetInsets() const { 141 gfx::Insets LabelButtonBorder::GetInsets() const {
133 return insets_; 142 return insets_;
134 } 143 }
135 144
136 Painter* LabelButtonBorder::GetPainter(bool focused, 145 Painter* LabelButtonBorder::GetPainter(bool focused,
137 Button::ButtonState state) { 146 Button::ButtonState state) {
147 // Use any corresponding unfocused painter if there is no focused painter.
148 if (focused && !painters_[1][state])
149 return painters_[0][state].get();
138 return painters_[focused ? 1 : 0][state].get(); 150 return painters_[focused ? 1 : 0][state].get();
139 } 151 }
140 152
141 void LabelButtonBorder::SetPainter(bool focused, 153 void LabelButtonBorder::SetPainter(bool focused,
142 Button::ButtonState state, 154 Button::ButtonState state,
143 Painter* painter) { 155 Painter* painter) {
144 painters_[focused ? 1 : 0][state].reset(painter); 156 painters_[focused ? 1 : 0][state].reset(painter);
145 } 157 }
146 158
147 } // namespace views 159 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/blue_button.cc ('k') | ui/views/painter.h » ('j') | ui/views/painter.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698