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

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: Rebased 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
« no previous file with comments | « no previous file | ui/views/cocoa/bridged_native_widget_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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
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 true if |client| has RTL text.
61 bool IsTextRTL(const ui::TextInputClient* client) {
62 gfx::Range text_range;
63 base::string16 text;
64 return client->GetTextRange(&text_range) &&
65 client->GetTextFromRange(text_range, &text) &&
66 base::i18n::GetStringDirection(text) == base::i18n::RIGHT_TO_LEFT;
67 }
68
69 // Returns the boundary rectangle for composition characters in the
70 // |requested_range|. Sets |actual_range| corresponding to the returned
71 // rectangle. For cases, where there is no composition text or the
72 // |requested_range| lies outside the composition range, a zero width rectangle
73 // corresponding to the caret bounds is returned. Logic used is similar to
74 // RenderWidgetHostViewMac::GetCachedFirstRectForCharacterRange(...).
75 gfx::Rect GetFirstRectForRangeHelper(const ui::TextInputClient* client,
76 const gfx::Range& requested_range,
77 gfx::Range* actual_range) {
78 // NSRange doesn't support reversed ranges.
79 DCHECK(!requested_range.is_reversed());
80 DCHECK(actual_range);
81
82 // Set up default return values, to be returned in case of unusual cases.
83 gfx::Rect default_rect;
84 *actual_range = gfx::Range::InvalidRange();
85 if (!client)
86 return default_rect;
87
88 default_rect = client->GetCaretBounds();
89 default_rect.set_width(0);
90
91 // If possible, modify actual_range to correspond to caret position.
92 gfx::Range selection_range;
93 if (client->GetSelectionRange(&selection_range)) {
94 // Caret bounds correspond to end index of selection_range.
95 *actual_range = gfx::Range(selection_range.end());
96 }
97
98 gfx::Range composition_range;
99 if (!client->HasCompositionText() ||
100 !client->GetCompositionTextRange(&composition_range) ||
101 !composition_range.Contains(requested_range))
102 return default_rect;
103
104 DCHECK(!composition_range.is_reversed());
105
106 const size_t from = requested_range.start() - composition_range.start();
107 const size_t to = requested_range.end() - composition_range.start();
108
109 // Pick the first character's bounds as the initial rectangle, then grow it to
110 // the full |requested_range| if possible.
111 const bool request_is_composition_end = from == composition_range.length();
112 const size_t first_index = request_is_composition_end ? from - 1 : from;
113 gfx::Rect union_rect;
114 if (!client->GetCompositionCharacterBounds(first_index, &union_rect))
115 return default_rect;
116
117 // If requested_range is empty, return a zero width rectangle corresponding to
118 // it.
119 if (from == to) {
120 if (request_is_composition_end && !IsTextRTL(client)) {
121 // In case of an empty requested range at end of composition, return the
122 // rectangle to the right of the last compositioned character.
123 union_rect.set_origin(union_rect.top_right());
124 }
125 union_rect.set_width(0);
126 *actual_range = requested_range;
127 return union_rect;
128 }
129
130 // Toolkit-views textfields are always single-line, so no need to check for
131 // line breaks.
132 for (size_t i = from + 1; i < to; i++) {
133 gfx::Rect current_rect;
134 if (client->GetCompositionCharacterBounds(i, &current_rect)) {
135 union_rect.Union(current_rect);
136 } else {
137 *actual_range =
138 gfx::Range(requested_range.start(), i + composition_range.start());
139 return union_rect;
140 }
141 }
142 *actual_range = requested_range;
143 return union_rect;
144 }
145
59 } // namespace 146 } // namespace
60 147
61 @interface BridgedContentView () 148 @interface BridgedContentView ()
62 149
63 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes 150 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes
64 // the event to the InputMethod for dispatch. 151 // the event to the InputMethod for dispatch.
65 - (void)handleKeyEvent:(NSEvent*)theEvent; 152 - (void)handleKeyEvent:(NSEvent*)theEvent;
66 153
67 // Handles an NSResponder Action Message by mapping it to a corresponding text 154 // 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 155 // editing command from ui_strings.grd and, when not being sent to a
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 return; 684 return;
598 } 685 }
599 686
600 if ([self respondsToSelector:selector]) 687 if ([self respondsToSelector:selector])
601 [self performSelector:selector withObject:nil]; 688 [self performSelector:selector withObject:nil];
602 else 689 else
603 [[self nextResponder] doCommandBySelector:selector]; 690 [[self nextResponder] doCommandBySelector:selector];
604 } 691 }
605 692
606 - (NSRect)firstRectForCharacterRange:(NSRange)range 693 - (NSRect)firstRectForCharacterRange:(NSRange)range
607 actualRange:(NSRangePointer)actualRange { 694 actualRange:(NSRangePointer)actualNSRange {
608 NOTIMPLEMENTED(); 695 gfx::Range actualRange;
609 return NSZeroRect; 696 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_,
697 gfx::Range(range), &actualRange);
698 if (actualNSRange)
699 *actualNSRange = actualRange.ToNSRange();
700 return gfx::ScreenRectToNSRect(rect);
610 } 701 }
611 702
612 - (BOOL)hasMarkedText { 703 - (BOOL)hasMarkedText {
613 return textInputClient_ && textInputClient_->HasCompositionText(); 704 return textInputClient_ && textInputClient_->HasCompositionText();
614 } 705 }
615 706
616 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { 707 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange {
617 if (!hostedView_) 708 if (!hostedView_)
618 return; 709 return;
619 710
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 } 826 }
736 827
737 return [super accessibilityAttributeValue:attribute]; 828 return [super accessibilityAttributeValue:attribute];
738 } 829 }
739 830
740 - (id)accessibilityHitTest:(NSPoint)point { 831 - (id)accessibilityHitTest:(NSPoint)point {
741 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; 832 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point];
742 } 833 }
743 834
744 @end 835 @end
OLDNEW
« no previous file with comments | « no previous file | ui/views/cocoa/bridged_native_widget_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698