OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "skia/ext/skia_utils_mac.h" | 10 #include "skia/ext/skia_utils_mac.h" |
11 #include "ui/base/ime/input_method.h" | 11 #include "ui/base/ime/input_method.h" |
12 #include "ui/base/ime/text_input_client.h" | 12 #include "ui/base/ime/text_input_client.h" |
13 #include "ui/compositor/canvas_painter.h" | 13 #include "ui/compositor/canvas_painter.h" |
14 #import "ui/events/cocoa/cocoa_event_utils.h" | 14 #import "ui/events/cocoa/cocoa_event_utils.h" |
15 #include "ui/events/keycodes/dom/dom_code.h" | 15 #include "ui/events/keycodes/dom/dom_code.h" |
16 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 16 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
17 #include "ui/gfx/canvas_paint_mac.h" | 17 #include "ui/gfx/canvas_paint_mac.h" |
18 #include "ui/gfx/geometry/rect.h" | 18 #include "ui/gfx/geometry/rect.h" |
19 #import "ui/gfx/mac/coordinate_conversion.h" | |
19 #include "ui/strings/grit/ui_strings.h" | 20 #include "ui/strings/grit/ui_strings.h" |
20 #include "ui/views/controls/menu/menu_config.h" | 21 #include "ui/views/controls/menu/menu_config.h" |
21 #include "ui/views/controls/menu/menu_controller.h" | 22 #include "ui/views/controls/menu/menu_controller.h" |
22 #include "ui/views/view.h" | 23 #include "ui/views/view.h" |
23 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
24 | 25 |
25 using views::MenuController; | 26 using views::MenuController; |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
(...skipping 20 matching lines...) Expand all Loading... | |
49 bool DispatchEventToMenu(views::Widget* widget, ui::KeyboardCode key_code) { | 50 bool DispatchEventToMenu(views::Widget* widget, ui::KeyboardCode key_code) { |
50 MenuController* menuController = MenuController::GetActiveInstance(); | 51 MenuController* menuController = MenuController::GetActiveInstance(); |
51 if (menuController && menuController->owner() == widget) { | 52 if (menuController && menuController->owner() == widget) { |
52 if (menuController->OnWillDispatchKeyEvent(0, key_code) == | 53 if (menuController->OnWillDispatchKeyEvent(0, key_code) == |
53 ui::POST_DISPATCH_NONE) | 54 ui::POST_DISPATCH_NONE) |
54 return true; | 55 return true; |
55 } | 56 } |
56 return false; | 57 return false; |
57 } | 58 } |
58 | 59 |
60 // Returns the boundary rectangle for composition characters in the | |
61 // |requested_range|. Sets |actual_range| corresponding to the returned | |
62 // rectangle. For cases, where there is no composition text or the | |
63 // |requested_range| lies outside the composition range, a zero width rectangle | |
64 // corresponding to the caret bounds is returned. Logic used is similar to | |
65 // RenderWidgetHostViewMac::GetCachedFirstRectForCharacterRange(...). | |
66 gfx::Rect GetFirstRectForRangeHelper(const ui::TextInputClient* client, | |
67 const gfx::Range& requested_range, | |
68 gfx::Range* actual_range) { | |
69 // NSRange doesn't support reversed ranges. | |
70 DCHECK(!requested_range.is_reversed()); | |
71 DCHECK(actual_range); | |
72 | |
73 // Set up default return values, to be returned in case of unusual cases. | |
74 gfx::Rect default_rect; | |
75 *actual_range = gfx::Range::InvalidRange(); | |
76 if (!client) | |
77 return default_rect; | |
78 | |
79 default_rect = client->GetCaretBounds(); | |
80 default_rect.set_width(0); | |
Shu Chen
2016/01/08 04:18:53
Why zero?
karandeepb
2016/01/12 05:43:52
From what I have seen, IME clients query with an e
Shu Chen
2016/01/12 05:54:35
Acknowledged.
| |
81 | |
82 // If possible, modify actual_range to correspond to caret position. | |
83 gfx::Range selection_range; | |
84 if (client->GetSelectionRange(&selection_range)) { | |
85 // Caret bounds correspond to end index of selection_range. | |
86 *actual_range = gfx::Range(selection_range.end()); | |
87 } | |
88 | |
89 if (!client->HasCompositionText()) | |
90 return default_rect; | |
Shu Chen
2016/01/08 04:18:53
Should return the selection bounds (if valid) I th
karandeepb
2016/01/12 05:43:52
Do you mean the bounds corresponding to the 'whole
Shu Chen
2016/01/12 05:54:35
Ah, thanks for the clarification. That should be a
| |
91 | |
92 gfx::Range composition_range; | |
93 if (!client->GetCompositionTextRange(&composition_range)) | |
94 return default_rect; | |
95 | |
96 DCHECK(!composition_range.is_reversed()); | |
97 | |
98 if (!composition_range.Contains(requested_range)) | |
99 return default_rect; | |
100 | |
101 // Range relative to composition_range.start(). | |
102 const gfx::Range relative_range( | |
103 requested_range.start() - composition_range.start(), | |
104 requested_range.end() - composition_range.start()); | |
105 | |
106 // Pick the first character's bounds as the initial rectangle, then grow it to | |
107 // the full |requested_range| if possible. | |
108 gfx::Rect union_rect; | |
109 const size_t composition_count = composition_range.length(); | |
110 | |
111 // In the case where relative_range is [composition_count, composition_count], | |
112 // use the bounds for the last compositioned character. | |
113 const bool request_is_composition_end = | |
114 relative_range.start() == composition_count; | |
115 const size_t first_index = request_is_composition_end | |
116 ? composition_count - 1 | |
117 : relative_range.start(); | |
118 if (!client->GetCompositionCharacterBounds(first_index, &union_rect)) | |
119 return default_rect; | |
120 | |
121 // If relative_range is empty, return a zero width rectangle corresponding to | |
122 // the relative_range. | |
123 if (relative_range.is_empty()) { | |
124 if (request_is_composition_end) { | |
125 // In case of an empty requested range at end of composition, return the | |
126 // rectangle to the right of the last compositioned character. | |
127 union_rect.set_origin(union_rect.top_right()); | |
Shu Chen
2016/01/08 04:18:53
top_right() may not be correct for RTL text.
karandeepb
2016/01/12 05:43:52
Done.
| |
128 } | |
129 union_rect.set_width(0); | |
130 *actual_range = requested_range; | |
131 return union_rect; | |
132 } | |
133 | |
134 // Toolkit-views textfields are always single-line, so no need to check for | |
135 // line breaks. | |
136 | |
137 // Return the union rectangle of the character bounds within the | |
138 // relative_range. | |
139 for (size_t i = relative_range.start() + 1; i < relative_range.end(); i++) { | |
140 gfx::Rect current_rect; | |
141 if (client->GetCompositionCharacterBounds(i, ¤t_rect)) { | |
142 union_rect.Union(current_rect); | |
143 } else { | |
144 *actual_range = | |
145 gfx::Range(requested_range.start(), i + composition_range.start()); | |
146 return union_rect; | |
147 } | |
148 } | |
149 *actual_range = requested_range; | |
150 return union_rect; | |
Shu Chen
2016/01/08 04:18:53
(optional) can you please make the code conciser?
karandeepb
2016/01/12 05:43:52
This does not handle the case where from==to but f
Shu Chen
2016/01/12 05:54:35
Acknowledged.
| |
151 } | |
152 | |
59 } // namespace | 153 } // namespace |
60 | 154 |
61 @interface BridgedContentView () | 155 @interface BridgedContentView () |
62 | 156 |
63 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes | 157 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes |
64 // the event to the InputMethod for dispatch. | 158 // the event to the InputMethod for dispatch. |
65 - (void)handleKeyEvent:(NSEvent*)theEvent; | 159 - (void)handleKeyEvent:(NSEvent*)theEvent; |
66 | 160 |
67 // Handles an NSResponder Action Message by mapping it to a corresponding text | 161 // Handles an NSResponder Action Message by mapping it to a corresponding text |
68 // editing command from ui_strings.grd and, when not being sent to a | 162 // editing command from ui_strings.grd and, when not being sent to a |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 return; | 678 return; |
585 } | 679 } |
586 | 680 |
587 if ([self respondsToSelector:selector]) | 681 if ([self respondsToSelector:selector]) |
588 [self performSelector:selector withObject:nil]; | 682 [self performSelector:selector withObject:nil]; |
589 else | 683 else |
590 [[self nextResponder] doCommandBySelector:selector]; | 684 [[self nextResponder] doCommandBySelector:selector]; |
591 } | 685 } |
592 | 686 |
593 - (NSRect)firstRectForCharacterRange:(NSRange)range | 687 - (NSRect)firstRectForCharacterRange:(NSRange)range |
594 actualRange:(NSRangePointer)actualRange { | 688 actualRange:(NSRangePointer)actualNSRange { |
595 NOTIMPLEMENTED(); | 689 gfx::Range actualRange; |
596 return NSZeroRect; | 690 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, |
691 gfx::Range(range), &actualRange); | |
692 if (actualNSRange) | |
693 *actualNSRange = actualRange.ToNSRange(); | |
694 return gfx::ScreenRectToNSRect(rect); | |
597 } | 695 } |
598 | 696 |
599 - (BOOL)hasMarkedText { | 697 - (BOOL)hasMarkedText { |
600 return textInputClient_ && textInputClient_->HasCompositionText(); | 698 return textInputClient_ && textInputClient_->HasCompositionText(); |
601 } | 699 } |
602 | 700 |
603 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { | 701 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { |
604 if (!hostedView_) | 702 if (!hostedView_) |
605 return; | 703 return; |
606 | 704 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
722 } | 820 } |
723 | 821 |
724 return [super accessibilityAttributeValue:attribute]; | 822 return [super accessibilityAttributeValue:attribute]; |
725 } | 823 } |
726 | 824 |
727 - (id)accessibilityHitTest:(NSPoint)point { | 825 - (id)accessibilityHitTest:(NSPoint)point { |
728 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 826 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
729 } | 827 } |
730 | 828 |
731 @end | 829 @end |
OLD | NEW |