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 #include "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 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.
| |
61 const gfx::Range& requested_range, | |
62 gfx::Range* actual_range) { | |
63 if (!client) | |
64 return gfx::Rect(); | |
65 | |
66 // NSRange doesn't support reversed ranges. | |
67 DCHECK(!requested_range.is_reversed()); | |
68 | |
69 // Set up default return values, to be returned in case of unusual cases. | |
70 gfx::Rect caret_bounds = client->GetCaretBounds(); | |
71 caret_bounds.set_width(0); | |
72 *actual_range = gfx::Range(requested_range.start()); | |
73 | |
74 if (!client->HasCompositionText()) | |
75 return caret_bounds; | |
76 | |
77 gfx::Range composition_range; | |
78 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:/
| |
79 return caret_bounds; | |
80 | |
81 if (!composition_range.Contains(requested_range)) | |
82 return caret_bounds; | |
83 | |
84 // Range relative to composition_range.start(). | |
85 gfx::Range relative_range(requested_range.start() - composition_range.start(), | |
86 requested_range.end() - composition_range.start()); | |
87 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.
| |
88 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
| |
89 composition_range.end() - composition_range.start(); | |
90 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.
| |
91 ? composition_count - 1 | |
92 : relative_range.start(); | |
93 if (!client->GetCompositionCharacterBounds(first_index, &union_rect)) | |
94 return caret_bounds; | |
95 | |
96 // 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
| |
97 // to the |requested_range|. | |
98 if (relative_range.is_empty()) { | |
99 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.
| |
100 // 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
| |
101 // compositioned character. | |
102 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
| |
103 } | |
104 union_rect.set_width(0); | |
105 return union_rect; | |
106 } | |
107 | |
108 // Toolkit-views textfields are always single-line, so no need to check for | |
109 // line breaks. | |
110 | |
111 // Return the union rectangle of the character bounds within the | |
112 // |requested_range|. | |
113 gfx::Rect current_rect; | |
114 for (size_t i = relative_range.start() + 1; i < relative_range.end(); i++) { | |
115 if (client->GetCompositionCharacterBounds(i, ¤t_rect)) { | |
116 union_rect.Union(current_rect); | |
117 } else { | |
118 actual_range->set_end(i + composition_range.start()); | |
119 return union_rect; | |
120 } | |
121 } | |
122 actual_range->set_end(requested_range.end()); | |
123 return union_rect; | |
124 } | |
125 | |
59 } // namespace | 126 } // namespace |
60 | 127 |
61 @interface BridgedContentView () | 128 @interface BridgedContentView () |
62 | 129 |
63 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes | 130 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes |
64 // the event to the InputMethod for dispatch. | 131 // the event to the InputMethod for dispatch. |
65 - (void)handleKeyEvent:(NSEvent*)theEvent; | 132 - (void)handleKeyEvent:(NSEvent*)theEvent; |
66 | 133 |
67 // Handles an NSResponder Action Message by mapping it to a corresponding text | 134 // 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 | 135 // 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; | 651 return; |
585 } | 652 } |
586 | 653 |
587 if ([self respondsToSelector:selector]) | 654 if ([self respondsToSelector:selector]) |
588 [self performSelector:selector withObject:nil]; | 655 [self performSelector:selector withObject:nil]; |
589 else | 656 else |
590 [[self nextResponder] doCommandBySelector:selector]; | 657 [[self nextResponder] doCommandBySelector:selector]; |
591 } | 658 } |
592 | 659 |
593 - (NSRect)firstRectForCharacterRange:(NSRange)range | 660 - (NSRect)firstRectForCharacterRange:(NSRange)range |
594 actualRange:(NSRangePointer)actualRange { | 661 actualRange:(NSRangePointer)actualNSRange { |
595 NOTIMPLEMENTED(); | 662 gfx::Range requestedRange(range); |
tapted
2015/12/22 02:33:57
nit: temporary not needed
karandeepb
2015/12/29 07:21:15
Done.
| |
596 return NSZeroRect; | 663 gfx::Range actualRange; |
664 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, requestedRange, | |
665 &actualRange); | |
666 *actualNSRange = actualRange.ToNSRange(); | |
667 | |
668 // 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.
| |
669 return gfx::ScreenRectToNSRect(rect); | |
597 } | 670 } |
598 | 671 |
599 - (BOOL)hasMarkedText { | 672 - (BOOL)hasMarkedText { |
600 return textInputClient_ && textInputClient_->HasCompositionText(); | 673 return textInputClient_ && textInputClient_->HasCompositionText(); |
601 } | 674 } |
602 | 675 |
603 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { | 676 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { |
604 if (!hostedView_) | 677 if (!hostedView_) |
605 return; | 678 return; |
606 | 679 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
722 } | 795 } |
723 | 796 |
724 return [super accessibilityAttributeValue:attribute]; | 797 return [super accessibilityAttributeValue:attribute]; |
725 } | 798 } |
726 | 799 |
727 - (id)accessibilityHitTest:(NSPoint)point { | 800 - (id)accessibilityHitTest:(NSPoint)point { |
728 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 801 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
729 } | 802 } |
730 | 803 |
731 @end | 804 @end |
OLD | NEW |