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.h" | 5 #include "ui/views/controls/button/label_button.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/throb_animation.h" | 9 #include "ui/base/animation/throb_animation.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
11 #include "ui/native_theme/native_theme.h" | 11 #include "ui/native_theme/native_theme.h" |
12 #include "ui/views/controls/button/label_button_border.h" | 12 #include "ui/views/controls/button/label_button_border.h" |
13 #include "ui/views/focus_border.h" | 13 #include "ui/views/focus_border.h" |
14 #include "ui/views/window/dialog_delegate.h" | 14 #include "ui/views/window/dialog_delegate.h" |
15 | 15 |
16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
17 #include "ui/gfx/color_utils.h" | 17 #include "ui/gfx/color_utils.h" |
18 #include "ui/native_theme/native_theme_win.h" | 18 #include "ui/native_theme/native_theme_win.h" |
19 #endif | 19 #endif |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // The spacing between the icon and text. | 23 // The spacing between the icon and text. |
24 const int kSpacing = 5; | 24 const int kSpacing = 5; |
25 | 25 |
26 // The length of the hover fade animation. | 26 // The length of the hover fade animation. |
27 const int kHoverAnimationDurationMs = 170; | 27 const int kHoverAnimationDurationMs = 170; |
28 | 28 |
29 // Blue button style default font colors. | |
30 const SkColor kBlueButtonEnabledColor = SK_ColorWHITE; | |
31 const SkColor kBlueButtonDisabledColor = SK_ColorWHITE; | |
msw
2013/05/13 18:27:20
nit: consider using a single constant for the matc
benwells
2013/05/16 11:01:13
Done.
| |
32 | |
29 } // namespace | 33 } // namespace |
30 | 34 |
31 namespace views { | 35 namespace views { |
32 | 36 |
33 // static | 37 // static |
34 const char LabelButton::kViewClassName[] = "views/LabelButton"; | 38 const char LabelButton::kViewClassName[] = "views/LabelButton"; |
35 | 39 |
36 LabelButton::LabelButton(ButtonListener* listener, const string16& text) | 40 LabelButton::LabelButton(ButtonListener* listener, const string16& text) |
37 : CustomButton(listener), | 41 : CustomButton(listener), |
38 image_(new ImageView()), | 42 image_(new ImageView()), |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 if (style == STYLE_BUTTON || style == STYLE_NATIVE_TEXTBUTTON) { | 148 if (style == STYLE_BUTTON || style == STYLE_NATIVE_TEXTBUTTON) { |
145 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); | 149 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
146 set_focusable(true); | 150 set_focusable(true); |
147 } | 151 } |
148 if (style == STYLE_BUTTON) { | 152 if (style == STYLE_BUTTON) { |
149 set_min_size(gfx::Size(70, 31)); | 153 set_min_size(gfx::Size(70, 31)); |
150 const SkColor color = GetNativeTheme()->GetSystemColor( | 154 const SkColor color = GetNativeTheme()->GetSystemColor( |
151 ui::NativeTheme::kColorId_WindowBackground); | 155 ui::NativeTheme::kColorId_WindowBackground); |
152 label_->SetShadowColors(color, color); | 156 label_->SetShadowColors(color, color); |
153 label_->SetShadowOffset(0, 1); | 157 label_->SetShadowOffset(0, 1); |
158 } else if (style == STYLE_BLUE_BUTTON) { | |
msw
2013/05/13 18:27:20
Please implement the blue buttons' 1px #538CEA dro
| |
159 if (label_->enabled() && !explicitly_set_colors_[state()]) | |
msw
2013/05/13 18:27:20
Ignore the current enabled/disabled state, both sh
benwells
2013/05/14 11:30:44
Yes I was thinking client code should be able to s
msw
2013/05/14 16:20:02
Maybe we can get API flexibility and code simplici
benwells
2013/05/16 11:01:13
OK, I've moved the logic into ResetColorsFromNativ
| |
160 label_->SetEnabledColor(kBlueButtonEnabledColor); | |
msw
2013/05/13 18:27:20
Call SetTextColor here to mark these as explicitly
msw
2013/05/14 16:20:02
Ignore this!
| |
161 if (!explicitly_set_colors_[STATE_DISABLED]) | |
162 label_->SetDisabledColor(kBlueButtonDisabledColor); | |
154 } | 163 } |
155 // Invalidate the layout to pickup the new insets from the border. | 164 // Invalidate the layout to pickup the new insets from the border. |
156 InvalidateLayout(); | 165 InvalidateLayout(); |
157 ResetColorsFromNativeTheme(); | 166 ResetColorsFromNativeTheme(); |
158 } | 167 } |
159 | 168 |
160 gfx::Size LabelButton::GetPreferredSize() { | 169 gfx::Size LabelButton::GetPreferredSize() { |
161 // Accommodate bold text in case this STYLE_BUTTON button is made default. | 170 // Accommodate bold text in case this STYLE_BUTTON button is made default. |
162 const gfx::Font font = label_->font(); | 171 const gfx::Font font = label_->font(); |
163 if (style_ == STYLE_BUTTON) | 172 if (style_ == STYLE_BUTTON) |
(...skipping 28 matching lines...) Expand all Loading... | |
192 if (max_size_.height() > 0) | 201 if (max_size_.height() > 0) |
193 size.set_height(std::min(max_size_.height(), size.height())); | 202 size.set_height(std::min(max_size_.height(), size.height())); |
194 return size; | 203 return size; |
195 } | 204 } |
196 | 205 |
197 const char* LabelButton::GetClassName() const { | 206 const char* LabelButton::GetClassName() const { |
198 return kViewClassName; | 207 return kViewClassName; |
199 } | 208 } |
200 | 209 |
201 void LabelButton::ResetColorsFromNativeTheme() { | 210 void LabelButton::ResetColorsFromNativeTheme() { |
211 if (style() == STYLE_BLUE_BUTTON) | |
msw
2013/05/13 18:27:20
This isn't needed if you just call SetTextColor ab
| |
212 return; | |
213 | |
202 const ui::NativeTheme* theme = GetNativeTheme(); | 214 const ui::NativeTheme* theme = GetNativeTheme(); |
203 SkColor colors[STATE_COUNT] = { | 215 SkColor colors[STATE_COUNT] = { |
204 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), | 216 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), |
205 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), | 217 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
206 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), | 218 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
207 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), | 219 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), |
208 }; | 220 }; |
209 | 221 |
210 // Certain styles do not change text color when hovered or pressed. | 222 // Certain styles do not change text color when hovered or pressed. |
211 bool constant_text_color = style() == STYLE_BUTTON; | 223 bool constant_text_color = style() == STYLE_BUTTON; |
212 #if defined(OS_WIN) | 224 #if defined(OS_WIN) |
213 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && | 225 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && |
214 theme == ui::NativeThemeWin::instance()); | 226 theme == ui::NativeThemeWin::instance()); |
215 #endif | 227 #endif |
216 if (constant_text_color) | 228 if (constant_text_color) |
217 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; | 229 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; |
218 | 230 |
219 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { | 231 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { |
220 if (!explicitly_set_colors_[state]) { | 232 if (!explicitly_set_colors_[state]) { |
221 SetTextColor(static_cast<ButtonState>(state), colors[state]); | 233 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
222 explicitly_set_colors_[state] = false; | 234 explicitly_set_colors_[state] = false; |
223 } | 235 } |
224 } | 236 } |
225 } | 237 } |
226 | 238 |
227 void LabelButton::StateChanged() { | 239 void LabelButton::StateChanged() { |
228 const gfx::Size previous_image_size(image_->GetPreferredSize()); | 240 const gfx::Size previous_image_size(image_->GetPreferredSize()); |
229 image_->SetImage(GetImage(state())); | 241 image_->SetImage(GetImage(state())); |
230 const SkColor color = button_state_colors_[state()]; | 242 SkColor color = button_state_colors_[state()]; |
243 if (style() == STYLE_BLUE_BUTTON && ! explicitly_set_colors_[state()]) | |
msw
2013/05/13 18:27:20
You shouldn't need to modify StateChanged at all.
benwells
2013/05/16 11:01:13
Done.
| |
244 color = kBlueButtonEnabledColor; | |
231 if (state() != STATE_DISABLED && label_->enabled_color() != color) | 245 if (state() != STATE_DISABLED && label_->enabled_color() != color) |
232 label_->SetEnabledColor(color); | 246 label_->SetEnabledColor(color); |
233 label_->SetEnabled(state() != STATE_DISABLED); | 247 label_->SetEnabled(state() != STATE_DISABLED); |
234 if (image_->GetPreferredSize() != previous_image_size) | 248 if (image_->GetPreferredSize() != previous_image_size) |
235 Layout(); | 249 Layout(); |
236 } | 250 } |
237 | 251 |
238 void LabelButton::Layout() { | 252 void LabelButton::Layout() { |
239 gfx::Rect child_area(GetLocalBounds()); | 253 gfx::Rect child_area(GetLocalBounds()); |
240 child_area.Inset(GetInsets()); | 254 child_area.Inset(GetInsets()); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 params->button.indeterminate = false; | 348 params->button.indeterminate = false; |
335 params->button.is_default = is_default_; | 349 params->button.is_default = is_default_; |
336 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); | 350 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
337 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; | 351 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; |
338 params->button.classic_state = 0; | 352 params->button.classic_state = 0; |
339 params->button.background_color = GetNativeTheme()->GetSystemColor( | 353 params->button.background_color = GetNativeTheme()->GetSystemColor( |
340 ui::NativeTheme::kColorId_TextButtonBackgroundColor); | 354 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
341 } | 355 } |
342 | 356 |
343 } // namespace views | 357 } // namespace views |
OLD | NEW |