Chromium Code Reviews| 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" | |
|
tapted
2016/01/04 02:28:10
nit: import
karandeepb
2016/01/04 04:33:56
Done.
| |
| 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 // If possible, modify default return values to correspond to caret position. | |
| 80 gfx::Range selection_range; | |
| 81 if (client->GetSelectionRange(&selection_range)) { | |
| 82 default_rect = client->GetCaretBounds(); | |
| 83 default_rect.set_width(0); | |
| 84 // Caret bounds correspond to end index of selection_range. | |
| 85 *actual_range = gfx::Range(selection_range.end()); | |
| 86 } | |
| 87 | |
| 88 if (!client->HasCompositionText()) | |
| 89 return default_rect; | |
| 90 | |
| 91 gfx::Range composition_range; | |
| 92 if (!client->GetCompositionTextRange(&composition_range)) | |
| 93 return default_rect; | |
| 94 | |
| 95 DCHECK(!composition_range.is_reversed()); | |
| 96 | |
| 97 if (!composition_range.Contains(requested_range)) | |
| 98 return default_rect; | |
| 99 | |
| 100 // Range relative to composition_range.start(). | |
| 101 const gfx::Range relative_range( | |
| 102 requested_range.start() - composition_range.start(), | |
| 103 requested_range.end() - composition_range.start()); | |
| 104 | |
| 105 // Pick the first character's bounds as the initial rectangle, then grow it to | |
| 106 // the full |requested_range| if possible. | |
| 107 gfx::Rect union_rect; | |
| 108 const size_t composition_count = composition_range.length(); | |
| 109 | |
| 110 // In the case where relative_range is [composition_count, composition_count], | |
| 111 // use the bounds for the last compositioned character. | |
| 112 const bool request_is_composition_end = | |
| 113 relative_range.start() == composition_count; | |
| 114 const size_t first_index = request_is_composition_end | |
| 115 ? composition_count - 1 | |
| 116 : relative_range.start(); | |
| 117 if (!client->GetCompositionCharacterBounds(first_index, &union_rect)) | |
| 118 return default_rect; | |
| 119 | |
| 120 // If relative_range is empty, return a zero width rectangle corresponding to | |
| 121 // the relative_range. | |
| 122 if (relative_range.is_empty()) { | |
|
karandeepb
2015/12/31 02:42:44
Can any reviewer confirm whether the handling of t
tapted
2016/01/04 02:28:10
I don't know the answer from a NSTextInputClient s
karandeepb
2016/01/04 04:33:55
Done.
| |
| 123 if (request_is_composition_end) { | |
| 124 // In case of an empty requested range at end of composition, return the | |
| 125 // rectangle to the right of the last compositioned character. | |
| 126 union_rect.set_origin(union_rect.top_right()); | |
| 127 } | |
| 128 union_rect.set_width(0); | |
| 129 *actual_range = requested_range; | |
| 130 return union_rect; | |
| 131 } | |
| 132 | |
| 133 // Toolkit-views textfields are always single-line, so no need to check for | |
| 134 // line breaks. | |
| 135 | |
| 136 // Return the union rectangle of the character bounds within the | |
| 137 // relative_range. | |
| 138 for (size_t i = relative_range.start() + 1; i < relative_range.end(); i++) { | |
| 139 gfx::Rect current_rect; | |
| 140 if (client->GetCompositionCharacterBounds(i, ¤t_rect)) { | |
| 141 union_rect.Union(current_rect); | |
| 142 } else { | |
| 143 *actual_range = | |
| 144 gfx::Range(requested_range.start(), i + composition_range.start()); | |
| 145 return union_rect; | |
| 146 } | |
| 147 } | |
| 148 *actual_range = requested_range; | |
| 149 return union_rect; | |
| 150 } | |
| 151 | |
| 59 } // namespace | 152 } // namespace |
| 60 | 153 |
| 61 @interface BridgedContentView () | 154 @interface BridgedContentView () |
| 62 | 155 |
| 63 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes | 156 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes |
| 64 // the event to the InputMethod for dispatch. | 157 // the event to the InputMethod for dispatch. |
| 65 - (void)handleKeyEvent:(NSEvent*)theEvent; | 158 - (void)handleKeyEvent:(NSEvent*)theEvent; |
| 66 | 159 |
| 67 // Handles an NSResponder Action Message by mapping it to a corresponding text | 160 // 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 | 161 // 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; | 677 return; |
| 585 } | 678 } |
| 586 | 679 |
| 587 if ([self respondsToSelector:selector]) | 680 if ([self respondsToSelector:selector]) |
| 588 [self performSelector:selector withObject:nil]; | 681 [self performSelector:selector withObject:nil]; |
| 589 else | 682 else |
| 590 [[self nextResponder] doCommandBySelector:selector]; | 683 [[self nextResponder] doCommandBySelector:selector]; |
| 591 } | 684 } |
| 592 | 685 |
| 593 - (NSRect)firstRectForCharacterRange:(NSRange)range | 686 - (NSRect)firstRectForCharacterRange:(NSRange)range |
| 594 actualRange:(NSRangePointer)actualRange { | 687 actualRange:(NSRangePointer)actualNSRange { |
| 595 NOTIMPLEMENTED(); | 688 gfx::Range actualRange; |
| 596 return NSZeroRect; | 689 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, |
| 690 gfx::Range(range), &actualRange); | |
| 691 if (actualNSRange) | |
| 692 *actualNSRange = actualRange.ToNSRange(); | |
| 693 return gfx::ScreenRectToNSRect(rect); | |
| 597 } | 694 } |
| 598 | 695 |
| 599 - (BOOL)hasMarkedText { | 696 - (BOOL)hasMarkedText { |
| 600 return textInputClient_ && textInputClient_->HasCompositionText(); | 697 return textInputClient_ && textInputClient_->HasCompositionText(); |
| 601 } | 698 } |
| 602 | 699 |
| 603 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { | 700 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { |
| 604 if (!hostedView_) | 701 if (!hostedView_) |
| 605 return; | 702 return; |
| 606 | 703 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 722 } | 819 } |
| 723 | 820 |
| 724 return [super accessibilityAttributeValue:attribute]; | 821 return [super accessibilityAttributeValue:attribute]; |
| 725 } | 822 } |
| 726 | 823 |
| 727 - (id)accessibilityHitTest:(NSPoint)point { | 824 - (id)accessibilityHitTest:(NSPoint)point { |
| 728 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 825 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
| 729 } | 826 } |
| 730 | 827 |
| 731 @end | 828 @end |
| OLD | NEW |