| 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 // If the link is "permanently" underlined use a ring to indicate focus. |
| 51 return underline_ ? FocusStyle::RING : FocusStyle::UNDERLINE; |
| 52 } |
| 53 |
| 54 void Link::MaybePaintFocusRing(gfx::Canvas* canvas) const { |
| 55 if (GetFocusStyle() == FocusStyle::RING) |
| 56 canvas->DrawFocusRect(GetFocusRingBounds()); |
| 57 } |
| 58 |
| 59 gfx::Insets Link::GetInsets() const { |
| 60 gfx::Insets insets = Label::GetInsets(); |
| 61 if (GetFocusStyle() == FocusStyle::RING && |
| 62 focus_behavior() != FocusBehavior::NEVER) { |
| 63 DCHECK(!text().empty()); |
| 64 insets += gfx::Insets(kFocusBorderPadding); |
| 65 } |
| 66 return insets; |
| 67 } |
| 68 |
| 41 const char* Link::GetClassName() const { | 69 const char* Link::GetClassName() const { |
| 42 return kViewClassName; | 70 return kViewClassName; |
| 43 } | 71 } |
| 44 | 72 |
| 45 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) { | 73 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) { |
| 46 if (!enabled()) | 74 if (!enabled()) |
| 47 return gfx::kNullCursor; | 75 return gfx::kNullCursor; |
| 48 return GetNativeHandCursor(); | 76 return GetNativeHandCursor(); |
| 49 } | 77 } |
| 50 | 78 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 void Link::SetUnderline(bool underline) { | 213 void Link::SetUnderline(bool underline) { |
| 186 if (underline_ == underline) | 214 if (underline_ == underline) |
| 187 return; | 215 return; |
| 188 underline_ = underline; | 216 underline_ = underline; |
| 189 RecalculateFont(); | 217 RecalculateFont(); |
| 190 } | 218 } |
| 191 | 219 |
| 192 void Link::Init() { | 220 void Link::Init() { |
| 193 listener_ = NULL; | 221 listener_ = NULL; |
| 194 pressed_ = false; | 222 pressed_ = false; |
| 195 underline_ = !ui::MaterialDesignController::IsSecondaryUiMaterial(); | 223 underline_ = GetDefaultFocusStyle() != FocusStyle::UNDERLINE; |
| 196 RecalculateFont(); | 224 RecalculateFont(); |
| 197 | 225 |
| 198 // Label::Init() calls SetText(), but if that's being called from Label(), our | 226 // Label::Init() calls SetText(), but if that's being called from Label(), our |
| 199 // SetText() override will not be reached (because the constructed class is | 227 // SetText() override will not be reached (because the constructed class is |
| 200 // only a Label at the moment, not yet a Link). So explicitly configure focus | 228 // only a Label at the moment, not yet a Link). So explicitly configure focus |
| 201 // here. | 229 // here. |
| 202 ConfigureFocus(); | 230 ConfigureFocus(); |
| 203 } | 231 } |
| 204 | 232 |
| 205 void Link::SetPressed(bool pressed) { | 233 void Link::SetPressed(bool pressed) { |
| 206 if (pressed_ != pressed) { | 234 if (pressed_ != pressed) { |
| 207 pressed_ = pressed; | 235 pressed_ = pressed; |
| 208 Label::SetEnabledColor(GetEnabledColor()); | 236 Label::SetEnabledColor(GetEnabledColor()); |
| 209 RecalculateFont(); | 237 RecalculateFont(); |
| 210 SchedulePaint(); | 238 SchedulePaint(); |
| 211 } | 239 } |
| 212 } | 240 } |
| 213 | 241 |
| 214 void Link::RecalculateFont() { | 242 void Link::RecalculateFont() { |
| 215 // Underline the link if it is enabled and |underline_| is true. Also | 243 // Underline the link if it is enabled and |underline_| is true. Also |
| 216 // underline to indicate focus in MD. | 244 // underline to indicate focus when that's the style. |
| 217 const int style = font_list().GetFontStyle(); | 245 const int style = font_list().GetFontStyle(); |
| 218 const bool underline = | 246 const bool underline = |
| 219 underline_ || | 247 underline_ || (HasFocus() && GetFocusStyle() == FocusStyle::UNDERLINE); |
| 220 (HasFocus() && ui::MaterialDesignController::IsSecondaryUiMaterial()); | |
| 221 const int intended_style = (enabled() && underline) ? | 248 const int intended_style = (enabled() && underline) ? |
| 222 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE); | 249 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE); |
| 223 | 250 |
| 224 if (style != intended_style) | 251 if (style != intended_style) |
| 225 Label::SetFontList(font_list().DeriveWithStyle(intended_style)); | 252 Label::SetFontList(font_list().DeriveWithStyle(intended_style)); |
| 226 } | 253 } |
| 227 | 254 |
| 228 void Link::ConfigureFocus() { | 255 void Link::ConfigureFocus() { |
| 229 // Disable focusability for empty links. Otherwise Label::GetInsets() will | 256 // Disable focusability for empty links. |
| 230 // give them an unconditional 1-px. inset on every side to allow for a focus | |
| 231 // border, when in this case we probably wanted zero width. | |
| 232 if (text().empty()) { | 257 if (text().empty()) { |
| 233 SetFocusBehavior(FocusBehavior::NEVER); | 258 SetFocusBehavior(FocusBehavior::NEVER); |
| 234 } else { | 259 } else { |
| 235 #if defined(OS_MACOSX) | 260 #if defined(OS_MACOSX) |
| 236 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); | 261 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); |
| 237 #else | 262 #else |
| 238 SetFocusBehavior(FocusBehavior::ALWAYS); | 263 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 239 #endif | 264 #endif |
| 240 } | 265 } |
| 241 } | 266 } |
| 242 | 267 |
| 243 SkColor Link::GetEnabledColor() { | 268 SkColor Link::GetEnabledColor() { |
| 244 if (requested_enabled_color_set_) | 269 if (requested_enabled_color_set_) |
| 245 return requested_enabled_color_; | 270 return requested_enabled_color_; |
| 246 | 271 |
| 247 if (GetNativeTheme()) { | 272 if (GetNativeTheme()) { |
| 248 return GetNativeTheme()->GetSystemColor( | 273 return GetNativeTheme()->GetSystemColor( |
| 249 pressed_ ? ui::NativeTheme::kColorId_LinkPressed | 274 pressed_ ? ui::NativeTheme::kColorId_LinkPressed |
| 250 : ui::NativeTheme::kColorId_LinkEnabled); | 275 : ui::NativeTheme::kColorId_LinkEnabled); |
| 251 } | 276 } |
| 252 | 277 |
| 253 return gfx::kPlaceholderColor; | 278 return gfx::kPlaceholderColor; |
| 254 } | 279 } |
| 255 | 280 |
| 256 } // namespace views | 281 } // namespace views |
| OLD | NEW |