Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: ui/views/controls/label.cc

Issue 2413223003: Views:: Make Labels support text selection. (Closed)
Patch Set: Cleanup. More tests. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/label.h" 5 #include "ui/views/controls/label.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
11 #include <limits> 11 #include <limits>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/i18n/rtl.h" 15 #include "base/i18n/rtl.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/profiler/scoped_tracker.h" 17 #include "base/profiler/scoped_tracker.h"
18 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "ui/accessibility/ax_view_state.h" 20 #include "ui/accessibility/ax_view_state.h"
21 #include "ui/base/clipboard/scoped_clipboard_writer.h"
22 #include "ui/base/cursor/cursor.h"
21 #include "ui/base/default_style.h" 23 #include "ui/base/default_style.h"
22 #include "ui/base/material_design/material_design_controller.h" 24 #include "ui/base/material_design/material_design_controller.h"
23 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/canvas.h" 26 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/color_utils.h" 27 #include "ui/gfx/color_utils.h"
26 #include "ui/gfx/geometry/insets.h" 28 #include "ui/gfx/geometry/insets.h"
27 #include "ui/gfx/text_elider.h" 29 #include "ui/gfx/text_elider.h"
28 #include "ui/native_theme/native_theme.h" 30 #include "ui/native_theme/native_theme.h"
31 #include "ui/views/focus/focus_manager.h"
32 #include "ui/views/native_cursor.h"
33 #include "ui/views/selection_controller.h"
29 34
30 namespace views { 35 namespace views {
31 // static 36 // static
32 const char Label::kViewClassName[] = "Label"; 37 const char Label::kViewClassName[] = "Label";
33 const int Label::kFocusBorderPadding = 1; 38 const int Label::kFocusBorderPadding = 1;
34 39
35 Label::Label() : Label(base::string16()) { 40 Label::Label() : Label(base::string16()) {
36 } 41 }
37 42
38 Label::Label(const base::string16& text) : Label(text, GetDefaultFontList()) { 43 Label::Label(const base::string16& text) : Label(text, GetDefaultFontList()) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 98
94 void Label::SetBackgroundColor(SkColor color) { 99 void Label::SetBackgroundColor(SkColor color) {
95 if (background_color_set_ && background_color_ == color) 100 if (background_color_set_ && background_color_ == color)
96 return; 101 return;
97 is_first_paint_text_ = true; 102 is_first_paint_text_ = true;
98 background_color_ = color; 103 background_color_ = color;
99 background_color_set_ = true; 104 background_color_set_ = true;
100 RecalculateColors(); 105 RecalculateColors();
101 } 106 }
102 107
108 void Label::SetSelectionTextColor(SkColor color) {
109 if (selection_text_color_set_ && requested_selection_text_color_ == color)
110 return;
111 is_first_paint_text_ = true;
112 requested_selection_text_color_ = color;
113 selection_text_color_set_ = true;
114 RecalculateColors();
115 }
116
117 void Label::SetSelectionBackgroundColor(SkColor color) {
118 if (selection_background_color_set_ && selection_background_color_ == color)
119 return;
120 is_first_paint_text_ = true;
121 selection_background_color_ = color;
122 selection_background_color_set_ = true;
123 RecalculateColors();
124 }
125
103 void Label::SetShadows(const gfx::ShadowValues& shadows) { 126 void Label::SetShadows(const gfx::ShadowValues& shadows) {
104 // TODO(mukai): early exit if the specified shadows are same. 127 // TODO(mukai): early exit if the specified shadows are same.
105 is_first_paint_text_ = true; 128 is_first_paint_text_ = true;
106 render_text_->set_shadows(shadows); 129 render_text_->set_shadows(shadows);
107 ResetLayout(); 130 ResetLayout();
108 } 131 }
109 132
110 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) { 133 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) {
111 if (subpixel_rendering_enabled_ == subpixel_rendering_enabled) 134 if (subpixel_rendering_enabled_ == subpixel_rendering_enabled)
112 return; 135 return;
(...skipping 27 matching lines...) Expand all
140 void Label::SetMultiLine(bool multi_line) { 163 void Label::SetMultiLine(bool multi_line) {
141 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL || 164 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL ||
142 elide_behavior_ == gfx::NO_ELIDE)); 165 elide_behavior_ == gfx::NO_ELIDE));
143 if (this->multi_line() == multi_line) 166 if (this->multi_line() == multi_line)
144 return; 167 return;
145 is_first_paint_text_ = true; 168 is_first_paint_text_ = true;
146 multi_line_ = multi_line; 169 multi_line_ = multi_line;
147 if (render_text_->MultilineSupported()) 170 if (render_text_->MultilineSupported())
148 render_text_->SetMultiline(multi_line); 171 render_text_->SetMultiline(multi_line);
149 render_text_->SetReplaceNewlineCharsWithSymbols(!multi_line); 172 render_text_->SetReplaceNewlineCharsWithSymbols(!multi_line);
173 if (multi_line)
174 SetSelectable(false);
150 ResetLayout(); 175 ResetLayout();
151 } 176 }
152 177
153 void Label::SetObscured(bool obscured) { 178 void Label::SetObscured(bool obscured) {
154 if (this->obscured() == obscured) 179 if (this->obscured() == obscured)
155 return; 180 return;
156 is_first_paint_text_ = true; 181 is_first_paint_text_ = true;
157 render_text_->SetObscured(obscured); 182 render_text_->SetObscured(obscured);
158 ResetLayout(); 183 ResetLayout();
159 } 184 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 } 222 }
198 223
199 void Label::SetMaximumWidth(int max_width) { 224 void Label::SetMaximumWidth(int max_width) {
200 DCHECK(multi_line()); 225 DCHECK(multi_line());
201 DCHECK_EQ(0, fixed_width_); 226 DCHECK_EQ(0, fixed_width_);
202 max_width_ = max_width; 227 max_width_ = max_width;
203 SizeToPreferredSize(); 228 SizeToPreferredSize();
204 } 229 }
205 230
206 base::string16 Label::GetDisplayTextForTesting() { 231 base::string16 Label::GetDisplayTextForTesting() {
207 lines_.clear(); 232 ClearRenderTextLines();
208 MaybeBuildRenderTextLines(); 233 MaybeBuildRenderTextLines();
209 base::string16 result; 234 base::string16 result;
210 if (lines_.empty()) 235 if (lines_.empty())
211 return result; 236 return result;
212 result.append(lines_[0]->GetDisplayText()); 237 result.append(lines_[0]->GetDisplayText());
213 for (size_t i = 1; i < lines_.size(); ++i) { 238 for (size_t i = 1; i < lines_.size(); ++i) {
214 result.append(1, '\n'); 239 result.append(1, '\n');
215 result.append(lines_[i]->GetDisplayText()); 240 result.append(lines_[i]->GetDisplayText());
216 } 241 }
217 return result; 242 return result;
218 } 243 }
219 244
245 bool Label::IsSelectionSupported() const {
246 return !multi_line() && render_text_->IsSelectionSupported();
247 }
248
249 bool Label::SetSelectable(bool value) {
250 if (value == selectable())
251 return true;
252
253 if (!value) {
254 ClearSelection();
255 selection_controller_.reset();
256 return true;
257 }
258
259 if (!IsSelectionSupported())
260 return false;
261
262 selection_controller_.reset(new SelectionController(this));
263
264 // On Linux, update the selection clipboard on a text selection.
265 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
266 selection_controller_->set_handles_selection_clipboard(true);
267 #endif
268
269 return true;
270 }
271
272 bool Label::HasSelection() const {
273 const gfx::RenderText* render_text = GetRenderTextForSelectionController();
274 return render_text ? !render_text->selection().is_empty() : false;
275 }
276
277 void Label::SelectAll() {
278 gfx::RenderText* render_text = GetRenderTextForSelectionController();
279 if (!render_text)
280 return;
281 render_text->SelectAll(false);
282 SchedulePaint();
283 }
284
285 void Label::ClearSelection() {
286 gfx::RenderText* render_text = GetRenderTextForSelectionController();
287 if (!render_text)
288 return;
289 render_text->ClearSelection();
290 SchedulePaint();
291 }
292
293 void Label::SelectRange(const gfx::Range& range) {
294 gfx::RenderText* render_text = GetRenderTextForSelectionController();
295 if (render_text && render_text->SelectRange(range))
296 SchedulePaint();
297 }
298
299 base::string16 Label::GetSelectedText() const {
300 const gfx::RenderText* render_text = GetRenderTextForSelectionController();
301 return render_text ? render_text->GetTextFromRange(render_text->selection())
302 : base::string16();
303 }
304
220 gfx::Insets Label::GetInsets() const { 305 gfx::Insets Label::GetInsets() const {
221 gfx::Insets insets = View::GetInsets(); 306 gfx::Insets insets = View::GetInsets();
222 if (focus_behavior() != FocusBehavior::NEVER) { 307 if (focus_behavior() != FocusBehavior::NEVER) {
223 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, 308 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding,
224 kFocusBorderPadding, kFocusBorderPadding); 309 kFocusBorderPadding, kFocusBorderPadding);
225 } 310 }
226 return insets; 311 return insets;
227 } 312 }
228 313
229 int Label::GetBaseline() const { 314 int Label::GetBaseline() const {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 height = render_text_->GetStringSize().height(); 374 height = render_text_->GetStringSize().height();
290 } else { 375 } else {
291 std::vector<base::string16> lines = GetLinesForWidth(w); 376 std::vector<base::string16> lines = GetLinesForWidth(w);
292 height = lines.size() * std::max(line_height(), font_list().GetHeight()); 377 height = lines.size() * std::max(line_height(), font_list().GetHeight());
293 } 378 }
294 height -= gfx::ShadowValue::GetMargin(render_text_->shadows()).height(); 379 height -= gfx::ShadowValue::GetMargin(render_text_->shadows()).height();
295 return height + GetInsets().height(); 380 return height + GetInsets().height();
296 } 381 }
297 382
298 void Label::Layout() { 383 void Label::Layout() {
299 lines_.clear(); 384 ClearRenderTextLines();
300 } 385 }
301 386
302 const char* Label::GetClassName() const { 387 const char* Label::GetClassName() const {
303 return kViewClassName; 388 return kViewClassName;
304 } 389 }
305 390
306 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { 391 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) {
307 if (!handles_tooltips_ || 392 if (!handles_tooltips_ ||
308 (tooltip_text_.empty() && !ShouldShowDefaultTooltip())) 393 (tooltip_text_.empty() && !ShouldShowDefaultTooltip()))
309 return NULL; 394 return NULL;
310 395
311 return HitTestPoint(point) ? this : NULL; 396 return HitTestPoint(point) ? this : NULL;
312 } 397 }
313 398
314 bool Label::CanProcessEventsWithinSubtree() const { 399 bool Label::CanProcessEventsWithinSubtree() const {
315 // Send events to the parent view for handling. 400 return !!GetRenderTextForSelectionController();
316 return false;
317 } 401 }
318 402
319 void Label::GetAccessibleState(ui::AXViewState* state) { 403 void Label::GetAccessibleState(ui::AXViewState* state) {
320 state->role = ui::AX_ROLE_STATIC_TEXT; 404 state->role = ui::AX_ROLE_STATIC_TEXT;
321 state->AddStateFlag(ui::AX_STATE_READ_ONLY); 405 state->AddStateFlag(ui::AX_STATE_READ_ONLY);
322 // Note that |render_text_| is never elided (see the comment in Init() too). 406 // Note that |render_text_| is never elided (see the comment in Init() too).
323 state->name = render_text_->GetDisplayText(); 407 state->name = render_text_->GetDisplayText();
324 } 408 }
325 409
326 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { 410 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const {
(...skipping 16 matching lines...) Expand all
343 427
344 void Label::OnEnabledChanged() { 428 void Label::OnEnabledChanged() {
345 ApplyTextColors(); 429 ApplyTextColors();
346 View::OnEnabledChanged(); 430 View::OnEnabledChanged();
347 } 431 }
348 432
349 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( 433 std::unique_ptr<gfx::RenderText> Label::CreateRenderText(
350 const base::string16& text, 434 const base::string16& text,
351 gfx::HorizontalAlignment alignment, 435 gfx::HorizontalAlignment alignment,
352 gfx::DirectionalityMode directionality, 436 gfx::DirectionalityMode directionality,
353 gfx::ElideBehavior elide_behavior) { 437 gfx::ElideBehavior elide_behavior) const {
354 std::unique_ptr<gfx::RenderText> render_text( 438 std::unique_ptr<gfx::RenderText> render_text(
355 render_text_->CreateInstanceOfSameType()); 439 render_text_->CreateInstanceOfSameType());
356 render_text->SetHorizontalAlignment(alignment); 440 render_text->SetHorizontalAlignment(alignment);
357 render_text->SetDirectionalityMode(directionality); 441 render_text->SetDirectionalityMode(directionality);
358 render_text->SetElideBehavior(elide_behavior); 442 render_text->SetElideBehavior(elide_behavior);
359 render_text->SetObscured(obscured()); 443 render_text->SetObscured(obscured());
360 render_text->SetMinLineHeight(line_height()); 444 render_text->SetMinLineHeight(line_height());
361 render_text->SetFontList(font_list()); 445 render_text->SetFontList(font_list());
362 render_text->set_shadows(shadows()); 446 render_text->set_shadows(shadows());
363 render_text->SetCursorEnabled(false); 447 render_text->SetCursorEnabled(false);
(...skipping 18 matching lines...) Expand all
382 // TODO(ckocagil): Remove ScopedTracker below once crbug.com/441028 is 466 // TODO(ckocagil): Remove ScopedTracker below once crbug.com/441028 is
383 // fixed. 467 // fixed.
384 tracked_objects::ScopedTracker tracking_profile( 468 tracked_objects::ScopedTracker tracking_profile(
385 FROM_HERE_WITH_EXPLICIT_FUNCTION("441028 First PaintText()")); 469 FROM_HERE_WITH_EXPLICIT_FUNCTION("441028 First PaintText()"));
386 470
387 is_first_paint_text_ = false; 471 is_first_paint_text_ = false;
388 PaintText(canvas); 472 PaintText(canvas);
389 } else { 473 } else {
390 PaintText(canvas); 474 PaintText(canvas);
391 } 475 }
392 if (HasFocus() && !ui::MaterialDesignController::IsSecondaryUiMaterial()) 476
477 // Check for IsAccessibilityFocusable() to prevent drawing a focus rect for
478 // non-focusable labels with selection, which are given focus explicitly in
479 // OnMousePressed.
480 if (HasFocus() && !ui::MaterialDesignController::IsSecondaryUiMaterial() &&
481 IsAccessibilityFocusable()) {
393 canvas->DrawFocusRect(GetFocusBounds()); 482 canvas->DrawFocusRect(GetFocusBounds());
483 }
394 } 484 }
395 485
396 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { 486 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) {
397 UpdateColorsFromTheme(theme); 487 UpdateColorsFromTheme(theme);
398 } 488 }
399 489
490 gfx::NativeCursor Label::GetCursor(const ui::MouseEvent& event) {
491 return GetRenderTextForSelectionController() ? GetNativeIBeamCursor()
492 : gfx::kNullCursor;
493 }
494
495 void Label::OnFocus() {
496 gfx::RenderText* render_text = GetRenderTextForSelectionController();
497 if (render_text) {
498 render_text->set_focused(true);
499 SchedulePaint();
500 }
501 View::OnFocus();
502 }
503
504 void Label::OnBlur() {
505 gfx::RenderText* render_text = GetRenderTextForSelectionController();
506 if (render_text) {
507 render_text->set_focused(false);
508 SchedulePaint();
509 }
510 View::OnBlur();
511 }
512
513 bool Label::OnMousePressed(const ui::MouseEvent& event) {
514 if (!GetRenderTextForSelectionController())
515 return false;
516
517 // RequestFocus() won't work when the label has FocusBehavior::NEVER. Hence
518 // explicitly set the focused view.
519 // TODO(karandeepb): If a widget with a label having FocusBehavior::NEVER as
520 // the currently focused view (due to selection) was to lose focus, focus
521 // won't be restored to the label (and hence a text selection won't be drawn)
522 // when the widget gets focus again. Fix this.
523 // Tracked in https://crbug.com/630365.
524 if ((event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) &&
525 GetFocusManager()) {
526 GetFocusManager()->SetFocusedView(this);
527 }
528
529 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
530 if (event.IsOnlyMiddleMouseButton() && GetFocusManager())
531 GetFocusManager()->SetFocusedView(this);
532 #endif
533
534 return selection_controller_->OnMousePressed(event, false);
535 }
536
537 bool Label::OnMouseDragged(const ui::MouseEvent& event) {
538 if (!GetRenderTextForSelectionController())
539 return false;
540
541 return selection_controller_->OnMouseDragged(event);
542 }
543
544 void Label::OnMouseReleased(const ui::MouseEvent& event) {
545 if (!GetRenderTextForSelectionController())
546 return;
547
548 selection_controller_->OnMouseReleased(event);
549 }
550
551 void Label::OnMouseCaptureLost() {
552 if (!GetRenderTextForSelectionController())
553 return;
554
555 selection_controller_->OnMouseCaptureLost();
556 }
557
400 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { 558 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) {
401 View::OnDeviceScaleFactorChanged(device_scale_factor); 559 View::OnDeviceScaleFactorChanged(device_scale_factor);
402 // When the device scale factor is changed, some font rendering parameters is 560 // When the device scale factor is changed, some font rendering parameters is
403 // changed (especially, hinting). The bounding box of the text has to be 561 // changed (especially, hinting). The bounding box of the text has to be
404 // re-computed based on the new parameters. See crbug.com/441439 562 // re-computed based on the new parameters. See crbug.com/441439
405 ResetLayout(); 563 ResetLayout();
406 } 564 }
407 565
408 void Label::VisibilityChanged(View* starting_from, bool is_visible) { 566 void Label::VisibilityChanged(View* starting_from, bool is_visible) {
409 if (!is_visible) 567 if (!is_visible)
410 lines_.clear(); 568 ClearRenderTextLines();
569 }
570
571 gfx::RenderText* Label::GetRenderTextForSelectionController() {
572 return const_cast<gfx::RenderText*>(
573 static_cast<const Label*>(this)->GetRenderTextForSelectionController());
574 }
575
576 bool Label::IsReadOnly() const {
577 return true;
578 }
579
580 bool Label::SupportsDrag() const {
581 // Todo(karandeepb): Labels should support dragging selected text. Tracked in
msw 2016/11/01 23:42:21 optional nit: all-caps TODO is more common, you ca
karandeepb 2016/11/02 06:29:37 Done. Also, filed a new bug report for this.
582 // https://crbug.com/630365.
583 return false;
584 }
585
586 bool Label::HasTextBeingDragged() const {
587 return false;
588 }
589
590 void Label::SetTextBeingDragged(bool value) {
591 NOTREACHED();
592 }
593
594 int Label::GetViewHeight() const {
595 return height();
596 }
597
598 int Label::GetViewWidth() const {
599 return width();
600 }
601
602 int Label::GetDragSelectionDelay() const {
603 // Labels don't need to use a repeating timer to update the drag selection.
604 // Since the cursor is disabled for labels, a selection outside the display
605 // area won't change the text in the display area. It is expected that all the
606 // text will fit in the display area for labels anyway.
607 return 0;
608 }
609
610 void Label::OnBeforePointerAction() {}
611
612 void Label::OnAfterPointerAction(bool text_changed, bool selection_changed) {
613 DCHECK(!text_changed);
614 if (selection_changed)
615 SchedulePaint();
616 }
617
618 bool Label::PasteSelectionClipboard() {
619 NOTREACHED();
620 return false;
621 }
622
623 void Label::UpdateSelectionClipboard() {
624 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
625 if (!obscured()) {
626 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_SELECTION)
627 .WriteText(GetSelectedText());
628 }
629 #endif
630 }
631
632 const gfx::RenderText* Label::GetRenderTextForSelectionController() const {
633 if (!selectable())
634 return nullptr;
635 MaybeBuildRenderTextLines();
636
637 // This may happen when the content bounds of the view are empty.
638 if (lines_.empty())
639 return nullptr;
640
641 DCHECK_EQ(1u, lines_.size());
642 return lines_[0].get();
411 } 643 }
412 644
413 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { 645 void Label::Init(const base::string16& text, const gfx::FontList& font_list) {
414 render_text_.reset(gfx::RenderText::CreateInstance()); 646 render_text_.reset(gfx::RenderText::CreateInstance());
415 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER); 647 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
416 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); 648 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT);
417 // NOTE: |render_text_| should not be elided at all. This is used to keep some 649 // NOTE: |render_text_| should not be elided at all. This is used to keep some
418 // properties and to compute the size of the string. 650 // properties and to compute the size of the string.
419 render_text_->SetElideBehavior(gfx::NO_ELIDE); 651 render_text_->SetElideBehavior(gfx::NO_ELIDE);
420 render_text_->SetFontList(font_list); 652 render_text_->SetFontList(font_list);
421 render_text_->SetCursorEnabled(false); 653 render_text_->SetCursorEnabled(false);
422 render_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS); 654 render_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS);
423 655
424 elide_behavior_ = gfx::ELIDE_TAIL; 656 elide_behavior_ = gfx::ELIDE_TAIL;
657 stored_selection_range_ = gfx::Range::InvalidRange();
425 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; 658 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
659 selection_text_color_set_ = selection_background_color_set_ = false;
426 subpixel_rendering_enabled_ = true; 660 subpixel_rendering_enabled_ = true;
427 auto_color_readability_ = true; 661 auto_color_readability_ = true;
428 multi_line_ = false; 662 multi_line_ = false;
429 UpdateColorsFromTheme(GetNativeTheme()); 663 UpdateColorsFromTheme(GetNativeTheme());
430 handles_tooltips_ = true; 664 handles_tooltips_ = true;
431 collapse_when_hidden_ = false; 665 collapse_when_hidden_ = false;
432 fixed_width_ = 0; 666 fixed_width_ = 0;
433 max_width_ = 0; 667 max_width_ = 0;
434 is_first_paint_text_ = true; 668 is_first_paint_text_ = true;
435 SetText(text); 669 SetText(text);
436 } 670 }
437 671
438 void Label::ResetLayout() { 672 void Label::ResetLayout() {
439 InvalidateLayout(); 673 InvalidateLayout();
440 PreferredSizeChanged(); 674 PreferredSizeChanged();
441 SchedulePaint(); 675 SchedulePaint();
442 lines_.clear(); 676 ClearRenderTextLines();
443 } 677 }
444 678
445 void Label::MaybeBuildRenderTextLines() { 679 void Label::MaybeBuildRenderTextLines() const {
446 if (!lines_.empty()) 680 if (!lines_.empty())
447 return; 681 return;
448 682
449 gfx::Rect rect = GetContentsBounds(); 683 gfx::Rect rect = GetContentsBounds();
450 if (focus_behavior() != FocusBehavior::NEVER) 684 if (focus_behavior() != FocusBehavior::NEVER)
451 rect.Inset(kFocusBorderPadding, kFocusBorderPadding); 685 rect.Inset(kFocusBorderPadding, kFocusBorderPadding);
452 if (rect.IsEmpty()) 686 if (rect.IsEmpty())
453 return; 687 return;
454 rect.Inset(-gfx::ShadowValue::GetMargin(shadows())); 688 rect.Inset(-gfx::ShadowValue::GetMargin(shadows()));
455 689
(...skipping 12 matching lines...) Expand all
468 // Text eliding is not supported for multi-lined Labels. 702 // Text eliding is not supported for multi-lined Labels.
469 // TODO(mukai): Add multi-lined elided text support. 703 // TODO(mukai): Add multi-lined elided text support.
470 gfx::ElideBehavior elide_behavior = 704 gfx::ElideBehavior elide_behavior =
471 multi_line() ? gfx::NO_ELIDE : elide_behavior_; 705 multi_line() ? gfx::NO_ELIDE : elide_behavior_;
472 if (!multi_line() || render_text_->MultilineSupported()) { 706 if (!multi_line() || render_text_->MultilineSupported()) {
473 std::unique_ptr<gfx::RenderText> render_text = 707 std::unique_ptr<gfx::RenderText> render_text =
474 CreateRenderText(text(), alignment, directionality, elide_behavior); 708 CreateRenderText(text(), alignment, directionality, elide_behavior);
475 render_text->SetDisplayRect(rect); 709 render_text->SetDisplayRect(rect);
476 render_text->SetMultiline(multi_line()); 710 render_text->SetMultiline(multi_line());
477 render_text->SetWordWrapBehavior(render_text_->word_wrap_behavior()); 711 render_text->SetWordWrapBehavior(render_text_->word_wrap_behavior());
712
713 // Setup render text for selection controller.
714 if (selectable()) {
715 render_text->set_focused(HasFocus());
716 if (stored_selection_range_.IsValid())
717 render_text->SelectRange(stored_selection_range_);
718 }
719
478 lines_.push_back(std::move(render_text)); 720 lines_.push_back(std::move(render_text));
479 } else { 721 } else {
480 std::vector<base::string16> lines = GetLinesForWidth(rect.width()); 722 std::vector<base::string16> lines = GetLinesForWidth(rect.width());
481 if (lines.size() > 1) 723 if (lines.size() > 1)
482 rect.set_height(std::max(line_height(), font_list().GetHeight())); 724 rect.set_height(std::max(line_height(), font_list().GetHeight()));
483 725
484 const int bottom = GetContentsBounds().bottom(); 726 const int bottom = GetContentsBounds().bottom();
485 for (size_t i = 0; i < lines.size() && rect.y() <= bottom; ++i) { 727 for (size_t i = 0; i < lines.size() && rect.y() <= bottom; ++i) {
486 std::unique_ptr<gfx::RenderText> line = 728 std::unique_ptr<gfx::RenderText> line =
487 CreateRenderText(lines[i], alignment, directionality, elide_behavior); 729 CreateRenderText(lines[i], alignment, directionality, elide_behavior);
488 line->SetDisplayRect(rect); 730 line->SetDisplayRect(rect);
489 lines_.push_back(std::move(line)); 731 lines_.push_back(std::move(line));
490 rect.set_y(rect.y() + rect.height()); 732 rect.set_y(rect.y() + rect.height());
491 } 733 }
492 // Append the remaining text to the last visible line. 734 // Append the remaining text to the last visible line.
493 for (size_t i = lines_.size(); i < lines.size(); ++i) 735 for (size_t i = lines_.size(); i < lines.size(); ++i)
494 lines_.back()->SetText(lines_.back()->text() + lines[i]); 736 lines_.back()->SetText(lines_.back()->text() + lines[i]);
495 } 737 }
738
739 stored_selection_range_ = gfx::Range::InvalidRange();
496 ApplyTextColors(); 740 ApplyTextColors();
497 } 741 }
498 742
499 gfx::Rect Label::GetFocusBounds() { 743 gfx::Rect Label::GetFocusBounds() const {
500 MaybeBuildRenderTextLines(); 744 MaybeBuildRenderTextLines();
501 745
502 gfx::Rect focus_bounds; 746 gfx::Rect focus_bounds;
503 if (lines_.empty()) { 747 if (lines_.empty()) {
504 focus_bounds = gfx::Rect(GetTextSize()); 748 focus_bounds = gfx::Rect(GetTextSize());
505 } else { 749 } else {
506 for (size_t i = 0; i < lines_.size(); ++i) { 750 for (size_t i = 0; i < lines_.size(); ++i) {
507 gfx::Point origin; 751 gfx::Point origin;
508 origin += lines_[i]->GetLineOffset(0); 752 origin += lines_[i]->GetLineOffset(0);
509 focus_bounds.Union(gfx::Rect(origin, lines_[i]->GetStringSize())); 753 focus_bounds.Union(gfx::Rect(origin, lines_[i]->GetStringSize()));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 808
565 void Label::RecalculateColors() { 809 void Label::RecalculateColors() {
566 actual_enabled_color_ = auto_color_readability_ ? 810 actual_enabled_color_ = auto_color_readability_ ?
567 color_utils::GetReadableColor(requested_enabled_color_, 811 color_utils::GetReadableColor(requested_enabled_color_,
568 background_color_) : 812 background_color_) :
569 requested_enabled_color_; 813 requested_enabled_color_;
570 actual_disabled_color_ = auto_color_readability_ ? 814 actual_disabled_color_ = auto_color_readability_ ?
571 color_utils::GetReadableColor(requested_disabled_color_, 815 color_utils::GetReadableColor(requested_disabled_color_,
572 background_color_) : 816 background_color_) :
573 requested_disabled_color_; 817 requested_disabled_color_;
818 actual_selection_text_color_ =
819 auto_color_readability_
820 ? color_utils::GetReadableColor(requested_selection_text_color_,
821 selection_background_color_)
822 : requested_selection_text_color_;
574 823
575 ApplyTextColors(); 824 ApplyTextColors();
576 SchedulePaint(); 825 SchedulePaint();
577 } 826 }
578 827
579 void Label::ApplyTextColors() { 828 void Label::ApplyTextColors() const {
580 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; 829 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_;
581 bool subpixel_rendering_suppressed = 830 bool subpixel_rendering_suppressed =
582 SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_; 831 SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_;
583 for (size_t i = 0; i < lines_.size(); ++i) { 832 for (size_t i = 0; i < lines_.size(); ++i) {
584 lines_[i]->SetColor(color); 833 lines_[i]->SetColor(color);
834 lines_[i]->set_selection_color(actual_selection_text_color_);
835 lines_[i]->set_selection_background_focused_color(
836 selection_background_color_);
585 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); 837 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed);
586 } 838 }
587 } 839 }
588 840
589 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { 841 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
590 if (!enabled_color_set_) { 842 if (!enabled_color_set_) {
591 requested_enabled_color_ = theme->GetSystemColor( 843 requested_enabled_color_ = theme->GetSystemColor(
592 ui::NativeTheme::kColorId_LabelEnabledColor); 844 ui::NativeTheme::kColorId_LabelEnabledColor);
593 } 845 }
594 if (!disabled_color_set_) { 846 if (!disabled_color_set_) {
595 requested_disabled_color_ = theme->GetSystemColor( 847 requested_disabled_color_ = theme->GetSystemColor(
596 ui::NativeTheme::kColorId_LabelDisabledColor); 848 ui::NativeTheme::kColorId_LabelDisabledColor);
597 } 849 }
598 if (!background_color_set_) { 850 if (!background_color_set_) {
599 background_color_ = 851 background_color_ =
600 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); 852 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground);
601 } 853 }
854 if (!selection_text_color_set_) {
855 requested_selection_text_color_ = theme->GetSystemColor(
856 ui::NativeTheme::kColorId_LabelTextSelectionColor);
857 }
858 if (!selection_background_color_set_) {
859 selection_background_color_ = theme->GetSystemColor(
860 ui::NativeTheme::kColorId_LabelTextSelectionBackgroundFocused);
861 }
602 RecalculateColors(); 862 RecalculateColors();
603 } 863 }
604 864
605 bool Label::ShouldShowDefaultTooltip() const { 865 bool Label::ShouldShowDefaultTooltip() const {
606 const gfx::Size text_size = GetTextSize(); 866 const gfx::Size text_size = GetTextSize();
607 const gfx::Size size = GetContentsBounds().size(); 867 const gfx::Size size = GetContentsBounds().size();
608 return !obscured() && (text_size.width() > size.width() || 868 return !obscured() && (text_size.width() > size.width() ||
609 (multi_line() && text_size.height() > size.height())); 869 (multi_line() && text_size.height() > size.height()));
610 } 870 }
611 871
872 void Label::ClearRenderTextLines() const {
873 // Persist the selection range if there is an active selection.
874 if (HasSelection())
msw 2016/11/01 23:42:21 nit: curlies
karandeepb 2016/11/02 06:29:37 Done.
875 stored_selection_range_ =
876 GetRenderTextForSelectionController()->selection();
877 lines_.clear();
878 }
879
612 } // namespace views 880 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698