| 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/link.h" | 5 #include "ui/views/controls/link.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "ui/accessibility/ax_node_data.h" | 11 #include "ui/accessibility/ax_node_data.h" |
| 12 #include "ui/base/cursor/cursor.h" | 12 #include "ui/base/cursor/cursor.h" |
| 13 #include "ui/base/material_design/material_design_controller.h" | 13 #include "ui/base/material_design/material_design_controller.h" |
| 14 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 15 #include "ui/events/keycodes/keyboard_codes.h" | 15 #include "ui/events/keycodes/keyboard_codes.h" |
| 16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/color_palette.h" | 17 #include "ui/gfx/color_palette.h" |
| 18 #include "ui/gfx/color_utils.h" | 18 #include "ui/gfx/color_utils.h" |
| 19 #include "ui/gfx/font_list.h" | 19 #include "ui/gfx/font_list.h" |
| 20 #include "ui/native_theme/native_theme.h" | 20 #include "ui/native_theme/native_theme.h" |
| 21 #include "ui/views/controls/link_listener.h" | 21 #include "ui/views/controls/link_listener.h" |
| 22 #include "ui/views/native_cursor.h" | 22 #include "ui/views/native_cursor.h" |
| 23 #include "ui/views/style/platform_style.h" | 23 #include "ui/views/style/platform_style.h" |
| 24 | 24 |
| 25 namespace views { | 25 namespace views { |
| 26 | 26 |
| 27 const char Link::kViewClassName[] = "Link"; | 27 const char Link::kViewClassName[] = "Link"; |
| 28 constexpr int Link::kFocusBorderPadding; |
| 28 | 29 |
| 29 Link::Link() : Link(base::string16()) {} | 30 Link::Link() : Link(base::string16()) {} |
| 30 | 31 |
| 31 Link::Link(const base::string16& title) | 32 Link::Link(const base::string16& title) |
| 32 : Label(title), | 33 : Label(title), |
| 33 requested_enabled_color_(gfx::kPlaceholderColor), | 34 requested_enabled_color_(gfx::kPlaceholderColor), |
| 34 requested_enabled_color_set_(false) { | 35 requested_enabled_color_set_(false) { |
| 35 Init(); | 36 Init(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 Link::~Link() { | 39 Link::~Link() { |
| 39 } | 40 } |
| 40 | 41 |
| 42 // static |
| 43 Link::FocusStyle Link::GetDefaultFocusStyle() { |
| 44 return ui::MaterialDesignController::IsSecondaryUiMaterial() |
| 45 ? FocusStyle::UNDERLINE |
| 46 : FocusStyle::RING; |
| 47 } |
| 48 |
| 49 Link::FocusStyle Link::GetFocusStyle() const { |
| 50 // Use the default, unless the link would "always" be underlined. |
| 51 if (underline_ && GetDefaultFocusStyle() == FocusStyle::UNDERLINE) |
| 52 return FocusStyle::RING; |
| 53 |
| 54 return GetDefaultFocusStyle(); |
| 55 } |
| 56 |
| 57 void Link::PaintFocusRing(gfx::Canvas* canvas) const { |
| 58 if (GetFocusStyle() == FocusStyle::RING) |
| 59 canvas->DrawFocusRect(GetFocusRingBounds()); |
| 60 } |
| 61 |
| 62 gfx::Insets Link::GetInsets() const { |
| 63 gfx::Insets insets = Label::GetInsets(); |
| 64 if (GetFocusStyle() == FocusStyle::RING && |
| 65 focus_behavior() != FocusBehavior::NEVER) { |
| 66 DCHECK(!text().empty()); |
| 67 insets += gfx::Insets(kFocusBorderPadding); |
| 68 } |
| 69 return insets; |
| 70 } |
| 71 |
| 41 const char* Link::GetClassName() const { | 72 const char* Link::GetClassName() const { |
| 42 return kViewClassName; | 73 return kViewClassName; |
| 43 } | 74 } |
| 44 | 75 |
| 45 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) { | 76 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) { |
| 46 if (!enabled()) | 77 if (!enabled()) |
| 47 return gfx::kNullCursor; | 78 return gfx::kNullCursor; |
| 48 return GetNativeHandCursor(); | 79 return GetNativeHandCursor(); |
| 49 } | 80 } |
| 50 | 81 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 void Link::SetUnderline(bool underline) { | 217 void Link::SetUnderline(bool underline) { |
| 187 if (underline_ == underline) | 218 if (underline_ == underline) |
| 188 return; | 219 return; |
| 189 underline_ = underline; | 220 underline_ = underline; |
| 190 RecalculateFont(); | 221 RecalculateFont(); |
| 191 } | 222 } |
| 192 | 223 |
| 193 void Link::Init() { | 224 void Link::Init() { |
| 194 listener_ = NULL; | 225 listener_ = NULL; |
| 195 pressed_ = false; | 226 pressed_ = false; |
| 196 underline_ = !ui::MaterialDesignController::IsSecondaryUiMaterial(); | 227 underline_ = GetDefaultFocusStyle() != FocusStyle::UNDERLINE; |
| 197 RecalculateFont(); | 228 RecalculateFont(); |
| 198 | 229 |
| 199 // Label::Init() calls SetText(), but if that's being called from Label(), our | 230 // Label::Init() calls SetText(), but if that's being called from Label(), our |
| 200 // SetText() override will not be reached (because the constructed class is | 231 // SetText() override will not be reached (because the constructed class is |
| 201 // only a Label at the moment, not yet a Link). So explicitly configure focus | 232 // only a Label at the moment, not yet a Link). So explicitly configure focus |
| 202 // here. | 233 // here. |
| 203 ConfigureFocus(); | 234 ConfigureFocus(); |
| 204 } | 235 } |
| 205 | 236 |
| 206 void Link::SetPressed(bool pressed) { | 237 void Link::SetPressed(bool pressed) { |
| 207 if (pressed_ != pressed) { | 238 if (pressed_ != pressed) { |
| 208 pressed_ = pressed; | 239 pressed_ = pressed; |
| 209 Label::SetEnabledColor(GetEnabledColor()); | 240 Label::SetEnabledColor(GetEnabledColor()); |
| 210 RecalculateFont(); | 241 RecalculateFont(); |
| 211 SchedulePaint(); | 242 SchedulePaint(); |
| 212 } | 243 } |
| 213 } | 244 } |
| 214 | 245 |
| 215 void Link::RecalculateFont() { | 246 void Link::RecalculateFont() { |
| 216 // Underline the link if it is enabled and |underline_| is true. Also | 247 // Underline the link if it is enabled and |underline_| is true. Also |
| 217 // underline to indicate focus in MD. | 248 // underline to indicate focus when that's the style. |
| 218 const int style = font_list().GetFontStyle(); | 249 const int style = font_list().GetFontStyle(); |
| 219 const bool underline = | 250 const bool underline = |
| 220 underline_ || | 251 underline_ || (HasFocus() && GetFocusStyle() == FocusStyle::UNDERLINE); |
| 221 (HasFocus() && ui::MaterialDesignController::IsSecondaryUiMaterial()); | |
| 222 const int intended_style = (enabled() && underline) ? | 252 const int intended_style = (enabled() && underline) ? |
| 223 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE); | 253 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE); |
| 224 | 254 |
| 225 if (style != intended_style) | 255 if (style != intended_style) |
| 226 Label::SetFontList(font_list().DeriveWithStyle(intended_style)); | 256 Label::SetFontList(font_list().DeriveWithStyle(intended_style)); |
| 227 } | 257 } |
| 228 | 258 |
| 229 void Link::ConfigureFocus() { | 259 void Link::ConfigureFocus() { |
| 230 // Disable focusability for empty links. Otherwise Label::GetInsets() will | 260 // Disable focusability for empty links. |
| 231 // give them an unconditional 1-px. inset on every side to allow for a focus | |
| 232 // border, when in this case we probably wanted zero width. | |
| 233 if (text().empty()) { | 261 if (text().empty()) { |
| 234 SetFocusBehavior(FocusBehavior::NEVER); | 262 SetFocusBehavior(FocusBehavior::NEVER); |
| 235 } else { | 263 } else { |
| 236 #if defined(OS_MACOSX) | 264 #if defined(OS_MACOSX) |
| 237 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); | 265 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); |
| 238 #else | 266 #else |
| 239 SetFocusBehavior(FocusBehavior::ALWAYS); | 267 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 240 #endif | 268 #endif |
| 241 } | 269 } |
| 242 } | 270 } |
| 243 | 271 |
| 244 SkColor Link::GetEnabledColor() { | 272 SkColor Link::GetEnabledColor() { |
| 245 if (requested_enabled_color_set_) | 273 if (requested_enabled_color_set_) |
| 246 return requested_enabled_color_; | 274 return requested_enabled_color_; |
| 247 | 275 |
| 248 if (GetNativeTheme()) { | 276 if (GetNativeTheme()) { |
| 249 return GetNativeTheme()->GetSystemColor( | 277 return GetNativeTheme()->GetSystemColor( |
| 250 pressed_ ? ui::NativeTheme::kColorId_LinkPressed | 278 pressed_ ? ui::NativeTheme::kColorId_LinkPressed |
| 251 : ui::NativeTheme::kColorId_LinkEnabled); | 279 : ui::NativeTheme::kColorId_LinkEnabled); |
| 252 } | 280 } |
| 253 | 281 |
| 254 return gfx::kPlaceholderColor; | 282 return gfx::kPlaceholderColor; |
| 255 } | 283 } |
| 256 | 284 |
| 257 } // namespace views | 285 } // namespace views |
| OLD | NEW |