Index: ui/views/cocoa/bridged_content_view.mm |
diff --git a/ui/views/cocoa/bridged_content_view.mm b/ui/views/cocoa/bridged_content_view.mm |
index 2167561368c38f1b8a637d99c1b1daa022faae15..7c07fb14dfedfdff9d8ee2157624e2ac6bb79f91 100644 |
--- a/ui/views/cocoa/bridged_content_view.mm |
+++ b/ui/views/cocoa/bridged_content_view.mm |
@@ -16,6 +16,7 @@ |
#import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
#include "ui/gfx/canvas_paint_mac.h" |
#include "ui/gfx/geometry/rect.h" |
+#include "ui/gfx/mac/coordinate_conversion.h" |
#include "ui/strings/grit/ui_strings.h" |
#include "ui/views/controls/menu/menu_config.h" |
#include "ui/views/controls/menu/menu_controller.h" |
@@ -56,6 +57,72 @@ bool DispatchEventToMenu(views::Widget* widget, ui::KeyboardCode key_code) { |
return false; |
} |
+gfx::Rect GetFirstRectForRangeHelper(const ui::TextInputClient* client, |
tapted
2015/12/22 02:33:57
This method is big enough that it should have a co
karandeepb
2015/12/29 07:21:15
Done.
|
+ const gfx::Range& requested_range, |
+ gfx::Range* actual_range) { |
+ if (!client) |
+ return gfx::Rect(); |
+ |
+ // NSRange doesn't support reversed ranges. |
+ DCHECK(!requested_range.is_reversed()); |
+ |
+ // Set up default return values, to be returned in case of unusual cases. |
+ gfx::Rect caret_bounds = client->GetCaretBounds(); |
+ caret_bounds.set_width(0); |
+ *actual_range = gfx::Range(requested_range.start()); |
+ |
+ if (!client->HasCompositionText()) |
+ return caret_bounds; |
+ |
+ gfx::Range composition_range; |
+ if (!client->GetCompositionTextRange(&composition_range)) |
tapted
2015/12/22 02:33:58
Can the composition_range be reversed? (will that
karandeepb
2015/12/29 07:21:15
Its not the case for a textfield atleast - https:/
|
+ return caret_bounds; |
+ |
+ if (!composition_range.Contains(requested_range)) |
+ return caret_bounds; |
+ |
+ // Range relative to composition_range.start(). |
+ gfx::Range relative_range(requested_range.start() - composition_range.start(), |
+ requested_range.end() - composition_range.start()); |
+ gfx::Rect union_rect; |
tapted
2015/12/22 02:33:58
Maybe a comment about what's going on with |union_
karandeepb
2015/12/29 07:21:15
Done.
|
+ size_t composition_count = |
tapted
2015/12/22 02:33:58
composition_count -> composition_range.length() (a
karandeepb
2015/12/29 07:21:15
Done. It can't be zero since we have already check
|
+ composition_range.end() - composition_range.start(); |
+ size_t first_index = relative_range.start() == composition_count |
tapted
2015/12/22 02:33:57
Needs a comment here - it's not immediately obviou
karandeepb
2015/12/29 07:21:15
Done.
|
+ ? composition_count - 1 |
+ : relative_range.start(); |
+ if (!client->GetCompositionCharacterBounds(first_index, &union_rect)) |
+ return caret_bounds; |
+ |
+ // If |requested_range| is empty, return a zero width rectangle corresponding |
tapted
2015/12/22 02:33:58
comment needs updating
karandeepb
2015/12/29 07:21:15
Updated. I had kept this because relative_range is
|
+ // to the |requested_range|. |
+ if (relative_range.is_empty()) { |
+ if (relative_range.start() == composition_count) { |
tapted
2015/12/22 02:33:58
Maybe this can be saved in a bool up above to help
karandeepb
2015/12/29 07:21:15
Done.
|
+ // Return a rectangle corresponding to the top right corner of the last |
tapted
2015/12/22 02:33:58
Rather than a comment just saying what the code do
karandeepb
2015/12/29 07:21:15
This is a case which I can't reproduce in practice
|
+ // compositioned character. |
+ union_rect.set_origin(union_rect.top_right()); |
tapted
2015/12/22 02:33:57
Is top_right always correct? (e.g. is there any su
karandeepb
2015/12/29 07:21:15
I couldn't find any RTL IME. I had also talked to
|
+ } |
+ union_rect.set_width(0); |
+ return union_rect; |
+ } |
+ |
+ // Toolkit-views textfields are always single-line, so no need to check for |
+ // line breaks. |
+ |
+ // Return the union rectangle of the character bounds within the |
+ // |requested_range|. |
+ gfx::Rect current_rect; |
+ for (size_t i = relative_range.start() + 1; i < relative_range.end(); i++) { |
+ if (client->GetCompositionCharacterBounds(i, ¤t_rect)) { |
+ union_rect.Union(current_rect); |
+ } else { |
+ actual_range->set_end(i + composition_range.start()); |
+ return union_rect; |
+ } |
+ } |
+ actual_range->set_end(requested_range.end()); |
+ return union_rect; |
+} |
+ |
} // namespace |
@interface BridgedContentView () |
@@ -591,9 +658,15 @@ bool DispatchEventToMenu(views::Widget* widget, ui::KeyboardCode key_code) { |
} |
- (NSRect)firstRectForCharacterRange:(NSRange)range |
- actualRange:(NSRangePointer)actualRange { |
- NOTIMPLEMENTED(); |
- return NSZeroRect; |
+ actualRange:(NSRangePointer)actualNSRange { |
+ gfx::Range requestedRange(range); |
tapted
2015/12/22 02:33:57
nit: temporary not needed
karandeepb
2015/12/29 07:21:15
Done.
|
+ gfx::Range actualRange; |
+ gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, requestedRange, |
+ &actualRange); |
+ *actualNSRange = actualRange.ToNSRange(); |
+ |
+ // Convert rect to AppKit screen coordinates. |
tapted
2015/12/22 02:33:58
nit: comment not needed (it's on the function prot
karandeepb
2015/12/29 07:21:15
Done.
|
+ return gfx::ScreenRectToNSRect(rect); |
} |
- (BOOL)hasMarkedText { |