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/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <climits> | 8 #include <climits> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
893 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); | 893 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); |
894 } | 894 } |
895 | 895 |
896 const Vector2d& RenderText::GetUpdatedDisplayOffset() { | 896 const Vector2d& RenderText::GetUpdatedDisplayOffset() { |
897 UpdateCachedBoundsAndOffset(); | 897 UpdateCachedBoundsAndOffset(); |
898 return display_offset_; | 898 return display_offset_; |
899 } | 899 } |
900 | 900 |
901 void RenderText::SetDisplayOffset(int horizontal_offset) { | 901 void RenderText::SetDisplayOffset(int horizontal_offset) { |
902 const int extra_content = GetContentWidth() - display_rect_.width(); | 902 const int extra_content = GetContentWidth() - display_rect_.width(); |
903 const int cursor_width = cursor_enabled_ ? 1 : 0; | |
903 | 904 |
904 int min_offset = 0; | 905 int min_offset = 0; |
905 int max_offset = 0; | 906 int max_offset = 0; |
906 if (extra_content > 0) { | 907 if (extra_content > 0) { |
907 switch (horizontal_alignment_) { | 908 switch (GetCurrentHorizontalAlignment()) { |
908 case ALIGN_LEFT: | 909 case ALIGN_LEFT: |
909 min_offset = -extra_content; | 910 min_offset = -extra_content; |
910 break; | 911 break; |
911 case ALIGN_RIGHT: | 912 case ALIGN_RIGHT: |
912 max_offset = extra_content; | 913 max_offset = extra_content; |
913 break; | 914 break; |
914 case ALIGN_CENTER: | 915 case ALIGN_CENTER: |
915 min_offset = -extra_content / 2; | 916 min_offset = -(extra_content - cursor_width + 1) / 2 - cursor_width; |
msw
2014/07/22 22:00:27
nit: It's a bit odd that ALIGN_[LEFT|RIGHT] consid
mohsen
2014/07/22 23:38:35
I'm not getting exactly what you mean here. The ex
msw
2014/07/23 03:08:16
That's exactly what I'm saying is confusing, and w
| |
916 max_offset = extra_content / 2; | 917 max_offset = (extra_content - cursor_width) / 2; |
917 break; | 918 break; |
918 default: | 919 default: |
919 break; | 920 break; |
920 } | 921 } |
921 } | 922 } |
922 if (horizontal_offset < min_offset) | 923 if (horizontal_offset < min_offset) |
923 horizontal_offset = min_offset; | 924 horizontal_offset = min_offset; |
924 else if (horizontal_offset > max_offset) | 925 else if (horizontal_offset > max_offset) |
925 horizontal_offset = max_offset; | 926 horizontal_offset = max_offset; |
926 | 927 |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1393 username = Elide(username, available_width, ELIDE_TAIL); | 1394 username = Elide(username, available_width, ELIDE_TAIL); |
1394 return username + kAtSignUTF16 + domain; | 1395 return username + kAtSignUTF16 + domain; |
1395 } | 1396 } |
1396 | 1397 |
1397 void RenderText::UpdateCachedBoundsAndOffset() { | 1398 void RenderText::UpdateCachedBoundsAndOffset() { |
1398 if (cached_bounds_and_offset_valid_) | 1399 if (cached_bounds_and_offset_valid_) |
1399 return; | 1400 return; |
1400 | 1401 |
1401 // TODO(ckocagil): Add support for scrolling multiline text. | 1402 // TODO(ckocagil): Add support for scrolling multiline text. |
1402 | 1403 |
1403 // First, set the valid flag true to calculate the current cursor bounds using | 1404 int delta_x = 0; |
1404 // the stale |display_offset_|. Applying |delta_offset| at the end of this | 1405 |
1405 // function will set |cursor_bounds_| and |display_offset_| to correct values. | 1406 if (cursor_enabled()) { |
1406 cached_bounds_and_offset_valid_ = true; | 1407 // When cursor is enabled, ensure it is visible. For this, set the valid |
1407 if (cursor_enabled()) | 1408 // flag true and calculate the current cursor bounds using the stale |
1409 // |display_offset_|. Then calculate the change in offset needed to move the | |
1410 // cursor into the visible area. | |
1411 cached_bounds_and_offset_valid_ = true; | |
1408 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_); | 1412 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_); |
1409 | 1413 |
1410 // Update |display_offset_| to ensure the current cursor is visible. | 1414 // TODO(bidi): Show RTL glyphs at the cursor position for ALIGN_LEFT, etc. |
1411 const int display_width = display_rect_.width(); | 1415 if (cursor_bounds_.right() > display_rect_.right()) |
1412 const int content_width = GetContentWidth(); | 1416 delta_x = display_rect_.right() - cursor_bounds_.right(); |
1413 | 1417 else if (cursor_bounds_.x() < display_rect_.x()) |
1414 int delta_x = 0; | 1418 delta_x = display_rect_.x() - cursor_bounds_.x(); |
1415 if (content_width <= display_width || !cursor_enabled()) { | |
1416 // Don't pan if the text fits in the display width or when the cursor is | |
1417 // disabled. | |
1418 delta_x = -display_offset_.x(); | |
1419 } else if (cursor_bounds_.right() > display_rect_.right()) { | |
1420 // TODO(xji): when the character overflow is a RTL character, currently, if | |
1421 // we pan cursor at the rightmost position, the entered RTL character is not | |
1422 // displayed. Should pan cursor to show the last logical characters. | |
1423 // | |
1424 // Pan to show the cursor when it overflows to the right. | |
1425 delta_x = display_rect_.right() - cursor_bounds_.right(); | |
1426 } else if (cursor_bounds_.x() < display_rect_.x()) { | |
1427 // TODO(xji): have similar problem as above when overflow character is a | |
1428 // LTR character. | |
1429 // | |
1430 // Pan to show the cursor when it overflows to the left. | |
1431 delta_x = display_rect_.x() - cursor_bounds_.x(); | |
1432 } else if (display_offset_.x() != 0) { | |
1433 // Reduce the pan offset to show additional overflow text when the display | |
1434 // width increases. | |
1435 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment(); | |
1436 const int negate_rtl = horizontal_alignment == ALIGN_RIGHT ? -1 : 1; | |
1437 const int offset = negate_rtl * display_offset_.x(); | |
1438 if (display_width > (content_width + offset)) { | |
1439 delta_x = negate_rtl * (display_width - (content_width + offset)); | |
1440 } | |
1441 } | 1419 } |
1442 | 1420 |
1443 Vector2d delta_offset(delta_x, 0); | 1421 SetDisplayOffset(display_offset_.x() + delta_x); |
1444 display_offset_ += delta_offset; | |
1445 cursor_bounds_ += delta_offset; | |
1446 } | 1422 } |
1447 | 1423 |
1448 void RenderText::DrawSelection(Canvas* canvas) { | 1424 void RenderText::DrawSelection(Canvas* canvas) { |
1449 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1425 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
1450 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1426 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
1451 canvas->FillRect(*i, selection_background_focused_color_); | 1427 canvas->FillRect(*i, selection_background_focused_color_); |
1452 } | 1428 } |
1453 | 1429 |
1454 } // namespace gfx | 1430 } // namespace gfx |
OLD | NEW |