Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / ui / views / controls / link.cc
blob654098fc90df88ecde91245ce7d173b2d5b1f764
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
3 // found in the LICENSE file.
5 #include "ui/views/controls/link.h"
7 #include "build/build_config.h"
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/cursor/cursor.h"
13 #include "ui/base/resource/material_design/material_design_controller.h"
14 #include "ui/events/event.h"
15 #include "ui/events/keycodes/keyboard_codes.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/color_utils.h"
18 #include "ui/gfx/font_list.h"
19 #include "ui/native_theme/native_theme.h"
20 #include "ui/views/controls/link_listener.h"
21 #include "ui/views/native_cursor.h"
23 namespace views {
25 const char Link::kViewClassName[] = "Link";
27 Link::Link()
28 : Label(base::string16()),
29 requested_enabled_color_(SK_ColorBLACK),
30 requested_enabled_color_set_(false),
31 requested_pressed_color_(SK_ColorBLACK),
32 requested_pressed_color_set_(false) {
33 Init();
36 Link::Link(const base::string16& title)
37 : Label(title),
38 requested_enabled_color_(SK_ColorBLACK),
39 requested_enabled_color_set_(false),
40 requested_pressed_color_(SK_ColorBLACK),
41 requested_pressed_color_set_(false) {
42 Init();
45 Link::~Link() {
48 const char* Link::GetClassName() const {
49 return kViewClassName;
52 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) {
53 if (!enabled())
54 return gfx::kNullCursor;
55 return GetNativeHandCursor();
58 bool Link::CanProcessEventsWithinSubtree() const {
59 // Links need to be able to accept events (e.g., clicking) even though
60 // in general Labels do not.
61 return View::CanProcessEventsWithinSubtree();
64 bool Link::OnMousePressed(const ui::MouseEvent& event) {
65 if (!enabled() ||
66 (!event.IsLeftMouseButton() && !event.IsMiddleMouseButton()))
67 return false;
68 SetPressed(true);
69 return true;
72 bool Link::OnMouseDragged(const ui::MouseEvent& event) {
73 SetPressed(enabled() &&
74 (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
75 HitTestPoint(event.location()));
76 return true;
79 void Link::OnMouseReleased(const ui::MouseEvent& event) {
80 // Change the highlight first just in case this instance is deleted
81 // while calling the controller
82 OnMouseCaptureLost();
83 if (enabled() &&
84 (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
85 HitTestPoint(event.location())) {
86 // Focus the link on click.
87 RequestFocus();
89 if (listener_)
90 listener_->LinkClicked(this, event.flags());
94 void Link::OnMouseCaptureLost() {
95 SetPressed(false);
98 bool Link::OnKeyPressed(const ui::KeyEvent& event) {
99 bool activate = ((event.key_code() == ui::VKEY_SPACE) ||
100 (event.key_code() == ui::VKEY_RETURN));
101 if (!activate)
102 return false;
104 SetPressed(false);
106 // Focus the link on key pressed.
107 RequestFocus();
109 if (listener_)
110 listener_->LinkClicked(this, event.flags());
112 return true;
115 void Link::OnGestureEvent(ui::GestureEvent* event) {
116 if (!enabled())
117 return;
119 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
120 SetPressed(true);
121 } else if (event->type() == ui::ET_GESTURE_TAP) {
122 RequestFocus();
123 if (listener_)
124 listener_->LinkClicked(this, event->flags());
125 } else {
126 SetPressed(false);
127 return;
129 event->SetHandled();
132 bool Link::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
133 // Make sure we don't process space or enter as accelerators.
134 return (event.key_code() == ui::VKEY_SPACE) ||
135 (event.key_code() == ui::VKEY_RETURN);
138 void Link::GetAccessibleState(ui::AXViewState* state) {
139 Label::GetAccessibleState(state);
140 state->role = ui::AX_ROLE_LINK;
143 void Link::OnEnabledChanged() {
144 RecalculateFont();
145 View::OnEnabledChanged();
148 void Link::OnFocus() {
149 Label::OnFocus();
150 // We render differently focused.
151 SchedulePaint();
154 void Link::OnBlur() {
155 Label::OnBlur();
156 // We render differently focused.
157 SchedulePaint();
160 void Link::SetFontList(const gfx::FontList& font_list) {
161 Label::SetFontList(font_list);
162 RecalculateFont();
165 void Link::SetText(const base::string16& text) {
166 Label::SetText(text);
167 // Disable focusability for empty links. Otherwise Label::GetInsets() will
168 // give them an unconditional 1-px. inset on every side to allow for a focus
169 // border, when in this case we probably wanted zero width.
170 SetFocusable(!text.empty());
173 void Link::OnNativeThemeChanged(const ui::NativeTheme* theme) {
174 Label::SetEnabledColor(GetEnabledColor());
175 SetDisabledColor(
176 theme->GetSystemColor(ui::NativeTheme::kColorId_LinkDisabled));
179 void Link::SetEnabledColor(SkColor color) {
180 requested_enabled_color_set_ = true;
181 requested_enabled_color_ = color;
182 Label::SetEnabledColor(GetEnabledColor());
185 void Link::SetPressedColor(SkColor color) {
186 requested_pressed_color_set_ = true;
187 requested_pressed_color_ = color;
188 Label::SetEnabledColor(GetEnabledColor());
191 void Link::SetUnderline(bool underline) {
192 if (underline_ == underline)
193 return;
194 underline_ = underline;
195 RecalculateFont();
198 void Link::Init() {
199 listener_ = NULL;
200 pressed_ = false;
201 underline_ = true;
202 RecalculateFont();
204 // Label::Init() calls SetText(), but if that's being called from Label(), our
205 // SetText() override will not be reached (because the constructed class is
206 // only a Label at the moment, not yet a Link). So so the set_focusable()
207 // call explicitly here.
208 SetFocusable(!text().empty());
211 void Link::SetPressed(bool pressed) {
212 if (pressed_ != pressed) {
213 pressed_ = pressed;
214 Label::SetEnabledColor(GetEnabledColor());
215 RecalculateFont();
216 SchedulePaint();
220 void Link::RecalculateFont() {
221 // Underline the link iff it is enabled and |underline_| is true.
222 const int style = font_list().GetFontStyle();
223 const int intended_style = (enabled() && underline_) ?
224 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE);
225 if (style != intended_style)
226 Label::SetFontList(font_list().DeriveWithStyle(intended_style));
229 SkColor Link::GetEnabledColor() {
230 // In material mode, there is no pressed effect, so always use the unpressed
231 // color.
232 if (!pressed_ || ui::MaterialDesignController::IsModeMaterial()) {
233 if (!requested_enabled_color_set_ && GetNativeTheme())
234 return GetNativeTheme()->GetSystemColor(
235 ui::NativeTheme::kColorId_LinkEnabled);
237 return requested_enabled_color_;
240 if (!requested_pressed_color_set_ && GetNativeTheme())
241 return GetNativeTheme()->GetSystemColor(
242 ui::NativeTheme::kColorId_LinkPressed);
244 return requested_pressed_color_;
247 } // namespace views