Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: ui/views/cocoa/bridged_content_view.mm

Issue 1531213002: Mac: Implement firstRectForCharacterRange:actualRange in BridgedContentView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review comments and made tests concise. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // Helper function for firstRectForCharacterRange:actualRange defined below.
tapted 2015/12/30 06:48:52 This comment doesn't add much beyond the function
karandeepb 2015/12/31 02:42:43 Done.
61 gfx::Rect GetFirstRectForRangeHelper(const ui::TextInputClient* client,
62 const gfx::Range& requested_range,
63 gfx::Range* actual_range) {
64 // NSRange doesn't support reversed ranges.
65 DCHECK(!requested_range.is_reversed());
66 DCHECK(actual_range);
67
68 // Set up default return values, to be returned in case of unusual cases.
69 gfx::Rect default_rect = gfx::Rect();
tapted 2015/12/30 06:48:52 nit: no need for the ` = gfx::Rect()` - just use d
karandeepb 2015/12/31 02:42:43 Done.
70 *actual_range = gfx::Range::InvalidRange();
71 if (!client)
72 return default_rect;
73
74 // If possible, modify default return values to correspond to caret position.
75 gfx::Range selection_range;
76 if (client->GetSelectionRange(&selection_range)) {
77 default_rect = client->GetCaretBounds();
78 default_rect.set_width(0);
79 // Caret bounds correspond to end index of selection_range.
80 *actual_range = gfx::Range(selection_range.end());
81 }
82
83 if (!client->HasCompositionText())
84 return default_rect;
85
86 gfx::Range composition_range;
87 if (!client->GetCompositionTextRange(&composition_range))
88 return default_rect;
89
90 DCHECK(!composition_range.is_reversed());
91
92 if (!composition_range.Contains(requested_range))
93 return default_rect;
94
95 // Range relative to composition_range.start().
96 gfx::Range relative_range(requested_range.start() - composition_range.start(),
tapted 2015/12/30 06:48:52 nit: declare const
karandeepb 2015/12/31 02:42:43 Done.
97 requested_range.end() - composition_range.start());
98
99 // Pick the first character's bounds as the initial rectangle, then grow it to
100 // the full |requested_range| if possible.
101 gfx::Rect union_rect;
102 size_t composition_count = composition_range.length();
tapted 2015/12/30 06:48:52 nit: declare const
karandeepb 2015/12/31 02:42:43 Done.
103
104 // In the case where relative_range is [composition_count, composition_count],
105 // use the bounds for the last compositioned character.
tapted 2015/12/30 06:48:52 Is using the last character bounds better than usi
karandeepb 2015/12/31 02:42:43 This is how RenderWidgetHostViewMac::GetCachedFir
106 const bool request_is_composition_end =
107 relative_range.start() == composition_count;
108 size_t first_index = request_is_composition_end ? composition_count - 1
109 : relative_range.start();
110 if (!client->GetCompositionCharacterBounds(first_index, &union_rect))
111 return default_rect;
112
113 // If relative_range is empty, return a zero width rectangle corresponding to
114 // the relative_range.
115 if (relative_range.is_empty()) {
116 if (request_is_composition_end) {
117 // In case of an empty requested range at end of composition, return the
118 // rectangle to the right of the last compositioned character.
119 union_rect.set_origin(union_rect.top_right());
120 }
121 union_rect.set_width(0);
122 *actual_range = requested_range;
123 return union_rect;
124 }
125
126 // Toolkit-views textfields are always single-line, so no need to check for
127 // line breaks.
128
129 // Return the union rectangle of the character bounds within the
130 // relative_range.
131 gfx::Rect current_rect;
tapted 2015/12/30 06:48:52 move declaration into loop body
karandeepb 2015/12/31 02:42:43 Done.
132 for (size_t i = relative_range.start() + 1; i < relative_range.end(); i++) {
133 if (client->GetCompositionCharacterBounds(i, &current_rect)) {
134 union_rect.Union(current_rect);
135 } else {
136 *actual_range =
137 gfx::Range(requested_range.start(), i + composition_range.start());
138 return union_rect;
139 }
140 }
141 *actual_range = requested_range;
142 return union_rect;
143 }
144
59 } // namespace 145 } // namespace
60 146
61 @interface BridgedContentView () 147 @interface BridgedContentView ()
62 148
63 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes 149 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes
64 // the event to the InputMethod for dispatch. 150 // the event to the InputMethod for dispatch.
65 - (void)handleKeyEvent:(NSEvent*)theEvent; 151 - (void)handleKeyEvent:(NSEvent*)theEvent;
66 152
67 // Handles an NSResponder Action Message by mapping it to a corresponding text 153 // 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 154 // 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
584 return; 670 return;
585 } 671 }
586 672
587 if ([self respondsToSelector:selector]) 673 if ([self respondsToSelector:selector])
588 [self performSelector:selector withObject:nil]; 674 [self performSelector:selector withObject:nil];
589 else 675 else
590 [[self nextResponder] doCommandBySelector:selector]; 676 [[self nextResponder] doCommandBySelector:selector];
591 } 677 }
592 678
593 - (NSRect)firstRectForCharacterRange:(NSRange)range 679 - (NSRect)firstRectForCharacterRange:(NSRange)range
594 actualRange:(NSRangePointer)actualRange { 680 actualRange:(NSRangePointer)actualNSRange {
595 NOTIMPLEMENTED(); 681 gfx::Range actualRange;
596 return NSZeroRect; 682 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_,
683 gfx::Range(range), &actualRange);
684 if (actualNSRange)
685 *actualNSRange = actualRange.ToNSRange();
686 return gfx::ScreenRectToNSRect(rect);
597 } 687 }
598 688
599 - (BOOL)hasMarkedText { 689 - (BOOL)hasMarkedText {
600 return textInputClient_ && textInputClient_->HasCompositionText(); 690 return textInputClient_ && textInputClient_->HasCompositionText();
601 } 691 }
602 692
603 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { 693 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange {
604 if (!hostedView_) 694 if (!hostedView_)
605 return; 695 return;
606 696
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 } 812 }
723 813
724 return [super accessibilityAttributeValue:attribute]; 814 return [super accessibilityAttributeValue:attribute];
725 } 815 }
726 816
727 - (id)accessibilityHitTest:(NSPoint)point { 817 - (id)accessibilityHitTest:(NSPoint)point {
728 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; 818 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point];
729 } 819 }
730 820
731 @end 821 @end
OLDNEW
« no previous file with comments | « no previous file | ui/views/cocoa/bridged_native_widget_unittest.mm » ('j') | ui/views/cocoa/bridged_native_widget_unittest.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698