Chromium Code Reviews| 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 "ui/base/animation/animation.h" | 9 #include "ui/base/animation/animation.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/rect.h" | 11 #include "ui/gfx/rect.h" |
| 12 #include "ui/native_theme/native_theme.h" | 12 #include "ui/native_theme/native_theme.h" |
| 13 #include "ui/views/controls/button/label_button.h" | 13 #include "ui/views/controls/button/label_button.h" |
| 14 #include "ui/views/native_theme_delegate.h" | 14 #include "ui/views/native_theme_delegate.h" |
| 15 | 15 |
| 16 namespace views { | 16 namespace views { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // The button image IDs and focused images IDs used in lieu of a focus border. | 20 // The button image IDs and focused images IDs used in lieu of a focus border. |
| 21 const int kNormalImages[] = IMAGE_GRID(IDR_BUTTON_NORMAL); | 21 const int kNormalImages[] = IMAGE_GRID(IDR_BUTTON_NORMAL); |
| 22 const int kHoveredImages[] = IMAGE_GRID(IDR_BUTTON_HOVER); | 22 const int kHoveredImages[] = IMAGE_GRID(IDR_BUTTON_HOVER); |
| 23 const int kPressedImages[] = IMAGE_GRID(IDR_BUTTON_PRESSED); | 23 const int kPressedImages[] = IMAGE_GRID(IDR_BUTTON_PRESSED); |
| 24 const int kFocusedNormalImages[] = IMAGE_GRID(IDR_BUTTON_FOCUSED_NORMAL); | 24 const int kFocusedNormalImages[] = IMAGE_GRID(IDR_BUTTON_FOCUSED_NORMAL); |
| 25 const int kFocusedHoveredImages[] = IMAGE_GRID(IDR_BUTTON_FOCUSED_HOVER); | 25 const int kFocusedHoveredImages[] = IMAGE_GRID(IDR_BUTTON_FOCUSED_HOVER); |
| 26 const int kFocusedPressedImages[] = IMAGE_GRID(IDR_BUTTON_FOCUSED_PRESSED); | 26 const int kFocusedPressedImages[] = IMAGE_GRID(IDR_BUTTON_FOCUSED_PRESSED); |
| 27 | 27 |
| 28 const int kBlueNormalImages[] = IMAGE_GRID(IDR_BLUE_BUTTON_NORMAL); | |
|
msw
2013/05/13 18:27:20
nit: consider dropping 'Images' from these constan
benwells
2013/05/14 11:30:44
I think I prefer it being consistent with the norm
msw
2013/05/14 16:20:02
Ok, feel free to leave/change both.
| |
| 29 const int kBlueHoveredImages[] = IMAGE_GRID(IDR_BLUE_BUTTON_HOVER); | |
| 30 const int kBluePressedImages[] = IMAGE_GRID(IDR_BLUE_BUTTON_PRESSED); | |
| 31 const int kBlueFocusedNormalImages[] = IMAGE_GRID( | |
| 32 IDR_BLUE_BUTTON_FOCUSED_NORMAL); | |
| 33 const int kBlueFocusedHoveredImages[] = IMAGE_GRID( | |
| 34 IDR_BLUE_BUTTON_FOCUSED_HOVER); | |
| 35 const int kBlueFocusedPressedImages[] = IMAGE_GRID( | |
| 36 IDR_BLUE_BUTTON_FOCUSED_PRESSED); | |
| 37 | |
| 28 // The text-button hot and pushed image IDs; normal is unadorned by default. | 38 // The text-button hot and pushed image IDs; normal is unadorned by default. |
| 29 const int kTextHoveredImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER); | 39 const int kTextHoveredImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER); |
| 30 const int kTextPressedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED); | 40 const int kTextPressedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED); |
| 31 | 41 |
| 32 Button::ButtonState GetButtonState(ui::NativeTheme::State state) { | 42 Button::ButtonState GetButtonState(ui::NativeTheme::State state) { |
| 33 switch(state) { | 43 switch(state) { |
| 34 case ui::NativeTheme::kDisabled: return Button::STATE_DISABLED; | 44 case ui::NativeTheme::kDisabled: return Button::STATE_DISABLED; |
| 35 case ui::NativeTheme::kHovered: return Button::STATE_HOVERED; | 45 case ui::NativeTheme::kHovered: return Button::STATE_HOVERED; |
| 36 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL; | 46 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL; |
| 37 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED; | 47 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 Painter::CreateImageGridPainter(kFocusedHoveredImages)); | 90 Painter::CreateImageGridPainter(kFocusedHoveredImages)); |
| 81 SetPainter(true, Button::STATE_PRESSED, | 91 SetPainter(true, Button::STATE_PRESSED, |
| 82 Painter::CreateImageGridPainter(kFocusedPressedImages)); | 92 Painter::CreateImageGridPainter(kFocusedPressedImages)); |
| 83 SetPainter(true, Button::STATE_DISABLED, | 93 SetPainter(true, Button::STATE_DISABLED, |
| 84 Painter::CreateImageGridPainter(kNormalImages)); | 94 Painter::CreateImageGridPainter(kNormalImages)); |
| 85 } else if (style == Button::STYLE_TEXTBUTTON) { | 95 } else if (style == Button::STYLE_TEXTBUTTON) { |
| 86 SetPainter(false, Button::STATE_HOVERED, | 96 SetPainter(false, Button::STATE_HOVERED, |
| 87 Painter::CreateImageGridPainter(kTextHoveredImages)); | 97 Painter::CreateImageGridPainter(kTextHoveredImages)); |
| 88 SetPainter(false, Button::STATE_PRESSED, | 98 SetPainter(false, Button::STATE_PRESSED, |
| 89 Painter::CreateImageGridPainter(kTextPressedImages)); | 99 Painter::CreateImageGridPainter(kTextPressedImages)); |
| 100 } else if (style == Button::STYLE_BLUE_BUTTON) { | |
| 101 SetPainter(false, Button::STATE_NORMAL, | |
| 102 Painter::CreateImageGridPainter(kBlueNormalImages)); | |
| 103 SetPainter(false, Button::STATE_HOVERED, | |
| 104 Painter::CreateImageGridPainter(kBlueHoveredImages)); | |
| 105 SetPainter(false, Button::STATE_PRESSED, | |
| 106 Painter::CreateImageGridPainter(kBluePressedImages)); | |
| 107 SetPainter(false, Button::STATE_DISABLED, | |
| 108 Painter::CreateImageGridPainter(kBlueNormalImages)); | |
| 109 SetPainter(true, Button::STATE_NORMAL, | |
| 110 Painter::CreateImageGridPainter(kBlueFocusedNormalImages)); | |
| 111 SetPainter(true, Button::STATE_HOVERED, | |
| 112 Painter::CreateImageGridPainter(kBlueFocusedHoveredImages)); | |
| 113 SetPainter(true, Button::STATE_PRESSED, | |
| 114 Painter::CreateImageGridPainter(kBlueFocusedPressedImages)); | |
| 115 SetPainter(true, Button::STATE_DISABLED, | |
| 116 Painter::CreateImageGridPainter(kBlueNormalImages)); | |
| 90 } | 117 } |
| 91 } | 118 } |
| 92 | 119 |
| 93 LabelButtonBorder::~LabelButtonBorder() {} | 120 LabelButtonBorder::~LabelButtonBorder() {} |
| 94 | 121 |
| 95 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) { | 122 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) { |
| 96 DCHECK(!strcmp(view.GetClassName(), LabelButton::kViewClassName)); | 123 DCHECK(!strcmp(view.GetClassName(), LabelButton::kViewClassName)); |
| 97 const NativeThemeDelegate* native_theme_delegate = | 124 const NativeThemeDelegate* native_theme_delegate = |
| 98 static_cast<const LabelButton*>(&view); | 125 static_cast<const LabelButton*>(&view); |
| 99 ui::NativeTheme::Part part = native_theme_delegate->GetThemePart(); | 126 ui::NativeTheme::Part part = native_theme_delegate->GetThemePart(); |
| 100 gfx::Rect rect(native_theme_delegate->GetThemePaintRect()); | 127 gfx::Rect rect(native_theme_delegate->GetThemePaintRect()); |
| 101 ui::NativeTheme::ExtraParams extra; | 128 ui::NativeTheme::ExtraParams extra; |
| 102 const ui::NativeTheme* theme = view.GetNativeTheme(); | 129 const ui::NativeTheme* theme = view.GetNativeTheme(); |
| 103 const ui::Animation* animation = native_theme_delegate->GetThemeAnimation(); | 130 const ui::Animation* animation = native_theme_delegate->GetThemeAnimation(); |
| 104 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra); | 131 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra); |
| 105 | 132 |
| 106 if (animation && animation->is_animating()) { | 133 if (animation && animation->is_animating()) { |
| 107 // Composite the background and foreground painters during state animations. | 134 // Composite the background and foreground painters during state animations. |
| 135 // The alpha is how much of the foreground state to use. To get this, | |
|
msw
2013/05/13 18:27:20
Hmm, I don't think this is correct, because the im
benwells
2013/05/14 11:30:44
Ah, I think I now understand the intent of that ch
msw
2013/05/14 16:20:02
Ah, you're right, we must blend the images before
| |
| 136 // composite (using SOURCE_OVER) alpha of the foreground over the | |
| 137 // background. This is equivalent to compositing (1 - alpha) of the | |
| 138 // background over the foreground. | |
| 139 state = native_theme_delegate->GetBackgroundThemeState(&extra); | |
| 140 PaintHelper(this, canvas, theme, part, state, rect, extra); | |
| 141 | |
| 108 int alpha = animation->CurrentValueBetween(0, 0xff); | 142 int alpha = animation->CurrentValueBetween(0, 0xff); |
| 109 state = native_theme_delegate->GetBackgroundThemeState(&extra); | |
| 110 canvas->SaveLayerAlpha(static_cast<uint8>(0xff - alpha)); | |
| 111 PaintHelper(this, canvas, theme, part, state, rect, extra); | |
| 112 canvas->Restore(); | |
| 113 | |
| 114 state = native_theme_delegate->GetForegroundThemeState(&extra); | 143 state = native_theme_delegate->GetForegroundThemeState(&extra); |
| 115 canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); | 144 canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); |
| 116 PaintHelper(this, canvas, theme, part, state, rect, extra); | 145 PaintHelper(this, canvas, theme, part, state, rect, extra); |
| 117 canvas->Restore(); | 146 canvas->Restore(); |
| 118 } else if (state == ui::NativeTheme::kDisabled && | 147 } else if (state == ui::NativeTheme::kDisabled && |
| 119 style() == Button::STYLE_BUTTON) { | 148 (style() == Button::STYLE_BUTTON || |
| 149 style() == Button::STYLE_BLUE_BUTTON)) { | |
| 120 canvas->SaveLayerAlpha(static_cast<uint8>(0xff / 2)); | 150 canvas->SaveLayerAlpha(static_cast<uint8>(0xff / 2)); |
| 121 PaintHelper(this, canvas, theme, part, state, rect, extra); | 151 PaintHelper(this, canvas, theme, part, state, rect, extra); |
| 122 canvas->Restore(); | 152 canvas->Restore(); |
| 123 } else { | 153 } else { |
| 124 PaintHelper(this, canvas, theme, part, state, rect, extra); | 154 PaintHelper(this, canvas, theme, part, state, rect, extra); |
| 125 } | 155 } |
| 126 | 156 |
| 127 // Draw the Views focus border for the native theme style. | 157 // Draw the Views focus border for the native theme style. |
| 128 if (style() == Button::STYLE_NATIVE_TEXTBUTTON && | 158 if (style() == Button::STYLE_NATIVE_TEXTBUTTON && |
| 129 view.focus_border() && extra.button.is_focused) | 159 view.focus_border() && extra.button.is_focused) |
| 130 view.focus_border()->Paint(view, canvas); | 160 view.focus_border()->Paint(view, canvas); |
| 131 } | 161 } |
| 132 | 162 |
| 133 gfx::Insets LabelButtonBorder::GetInsets() const { | 163 gfx::Insets LabelButtonBorder::GetInsets() const { |
| 134 // Return the style-specific insets between button contents and edges. | 164 // Return the style-specific insets between button contents and edges. |
| 135 if (style() == Button::STYLE_BUTTON) | 165 if (style() == Button::STYLE_BUTTON) |
| 136 return gfx::Insets(9, 13, 9, 13); | 166 return gfx::Insets(9, 13, 9, 13); |
| 137 if (style() == Button::STYLE_TEXTBUTTON) | 167 if (style() == Button::STYLE_TEXTBUTTON) |
| 138 return gfx::Insets(5, 6, 5, 6); | 168 return gfx::Insets(5, 6, 5, 6); |
| 139 if (style() == Button::STYLE_NATIVE_TEXTBUTTON) | 169 if (style() == Button::STYLE_NATIVE_TEXTBUTTON) |
| 140 return gfx::Insets(5, 12, 5, 12); | 170 return gfx::Insets(5, 12, 5, 12); |
| 171 if (style() == Button::STYLE_BLUE_BUTTON) | |
| 172 return gfx::Insets(9, 13, 9, 13); | |
| 141 NOTREACHED(); | 173 NOTREACHED(); |
| 142 return gfx::Insets(); | 174 return gfx::Insets(); |
| 143 } | 175 } |
| 144 | 176 |
| 145 Painter* LabelButtonBorder::GetPainter(bool focused, | 177 Painter* LabelButtonBorder::GetPainter(bool focused, |
| 146 Button::ButtonState state) { | 178 Button::ButtonState state) { |
| 147 return painters_[focused ? 1 : 0][state].get(); | 179 return painters_[focused ? 1 : 0][state].get(); |
| 148 } | 180 } |
| 149 | 181 |
| 150 void LabelButtonBorder::SetPainter(bool focused, | 182 void LabelButtonBorder::SetPainter(bool focused, |
| 151 Button::ButtonState state, | 183 Button::ButtonState state, |
| 152 Painter* painter) { | 184 Painter* painter) { |
| 153 painters_[focused ? 1 : 0][state].reset(painter); | 185 painters_[focused ? 1 : 0][state].reset(painter); |
| 154 } | 186 } |
| 155 | 187 |
| 156 } // namespace views | 188 } // namespace views |
| OLD | NEW |