OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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); |
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)); |
reed1
2013/06/12 20:05:10
Does this guy *not* actually create a layer if the
msw
2013/06/12 22:10:55
I think Canvas does create a layer for 0xFF, it se
| |
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 paint.setXfermode(SkLerpXfermode::Create(animation->GetCurrentValue())); | |
reed1
2013/06/12 20:05:10
Create returns a new xfermode
setXfermode refs tha
msw
2013/06/12 22:10:55
I'm using skia::RefPtr instead; since SkAutoTUnref
| |
125 PaintHelper(this, canvas, paint, theme, part, fore, rect, extra); | |
126 canvas->Restore(); | |
127 } | |
122 } else { | 128 } else { |
123 PaintHelper(this, canvas, theme, part, state, rect, extra); | 129 ui::NativeTheme::State state = delegate->GetThemeState(&extra); |
130 PaintHelper(this, canvas, paint, theme, part, state, rect, extra); | |
124 } | 131 } |
125 | 132 |
126 // Draw the Views focus border for the native theme style. | 133 // Draw the Views focus border for the native theme style. |
127 if (style() == Button::STYLE_NATIVE_TEXTBUTTON && | 134 if (style() == Button::STYLE_NATIVE_TEXTBUTTON && |
128 view.focus_border() && extra.button.is_focused) | 135 view.focus_border() && extra.button.is_focused) |
129 view.focus_border()->Paint(view, canvas); | 136 view.focus_border()->Paint(view, canvas); |
130 } | 137 } |
131 | 138 |
132 gfx::Insets LabelButtonBorder::GetInsets() const { | 139 gfx::Insets LabelButtonBorder::GetInsets() const { |
133 return insets_; | 140 return insets_; |
134 } | 141 } |
135 | 142 |
136 Painter* LabelButtonBorder::GetPainter(bool focused, | 143 Painter* LabelButtonBorder::GetPainter(bool focused, |
137 Button::ButtonState state) { | 144 Button::ButtonState state) { |
145 // Use any corresponding unfocused painter if there is no focused painter. | |
146 if (focused && !painters_[1][state]) | |
147 return painters_[0][state].get(); | |
138 return painters_[focused ? 1 : 0][state].get(); | 148 return painters_[focused ? 1 : 0][state].get(); |
139 } | 149 } |
140 | 150 |
141 void LabelButtonBorder::SetPainter(bool focused, | 151 void LabelButtonBorder::SetPainter(bool focused, |
142 Button::ButtonState state, | 152 Button::ButtonState state, |
143 Painter* painter) { | 153 Painter* painter) { |
144 painters_[focused ? 1 : 0][state].reset(painter); | 154 painters_[focused ? 1 : 0][state].reset(painter); |
145 } | 155 } |
146 | 156 |
147 } // namespace views | 157 } // namespace views |
OLD | NEW |