Index: content/browser/renderer_host/render_widget_host_view_mac.mm |
diff --git a/content/browser/renderer_host/render_widget_host_view_mac.mm b/content/browser/renderer_host/render_widget_host_view_mac.mm |
index 9500038092b8c8630da89909a8f4d2ae95452169..93c64697d80663210b28d42357da081bc16f82f9 100644 |
--- a/content/browser/renderer_host/render_widget_host_view_mac.mm |
+++ b/content/browser/renderer_host/render_widget_host_view_mac.mm |
@@ -1226,6 +1226,7 @@ void RenderWidgetHostViewMac::SelectionBoundsChanged( |
const ViewHostMsg_SelectionBounds_Params& params) { |
if (params.anchor_rect == params.focus_rect) |
caret_rect_ = params.anchor_rect; |
+ first_selection_rect_ = params.anchor_rect; |
} |
void RenderWidgetHostViewMac::SetShowingContextMenu(bool showing) { |
@@ -1446,16 +1447,26 @@ bool RenderWidgetHostViewMac::GetCachedFirstRectForCharacterRange( |
TRACE_EVENT0("browser", |
"RenderWidgetHostViewMac::GetFirstRectForCharacterRange"); |
+ const gfx::Range requested_range(range); |
// If requested range is same as caret location, we can just return it. |
- if (selection_range_.is_empty() && gfx::Range(range) == selection_range_) { |
+ if (selection_range_.is_empty() && requested_range == selection_range_) { |
if (actual_range) |
*actual_range = range; |
*rect = NSRectFromCGRect(caret_rect_.ToCGRect()); |
return true; |
} |
+ if (composition_range_.is_empty()) { |
+ if (!selection_range_.Contains(requested_range)) |
+ return false; |
+ if (actual_range) |
+ *actual_range = selection_range_.ToNSRange(); |
+ *rect = NSRectFromCGRect(first_selection_rect_.ToCGRect()); |
+ return true; |
+ } |
+ |
const gfx::Range request_range_in_composition = |
- ConvertCharacterRangeToCompositionRange(gfx::Range(range)); |
+ ConvertCharacterRangeToCompositionRange(requested_range); |
if (request_range_in_composition == gfx::Range::InvalidRange()) |
return false; |
@@ -2355,10 +2366,7 @@ void RenderWidgetHostViewMac::OnDisplayMetricsChanged( |
} |
} |
-// This is invoked only on 10.8 or newer when the user taps a word using |
-// three fingers. |
-- (void)quickLookWithEvent:(NSEvent*)event { |
- NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; |
+- (void) ShowLookUpDictionary:(NSPoint)point { |
TextInputClientMac::GetInstance()->GetStringAtPoint( |
renderWidgetHostView_->render_widget_host_, |
gfx::Point(point.x, NSHeight([self frame]) - point.y), |
@@ -2373,6 +2381,13 @@ void RenderWidgetHostViewMac::OnDisplayMetricsChanged( |
); |
} |
+// This is invoked only on 10.8 or newer when the user taps a word using |
+// three fingers. |
+- (void)quickLookWithEvent:(NSEvent*)event { |
Alexei Svitkine (slow)
2015/09/01 16:27:03
Please separate the dictionary logic change into i
Shu Chen
2015/09/02 03:35:42
Done.
|
+ NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; |
+ [self ShowLookUpDictionary:point]; |
+} |
+ |
// This method handles 2 different types of hardware events. |
// (Apple does not distinguish between them). |
// a. Scrolling the middle wheel of a mouse. |
@@ -2895,10 +2910,36 @@ extern NSString *NSTextInputReplacementRangeAttributeName; |
// TODO(thakis): Pipe |actualRange| through TextInputClientMac machinery. |
if (actualRange) |
*actualRange = range; |
- NSAttributedString* str = |
- TextInputClientMac::GetInstance()->GetAttributedSubstringFromRange( |
- renderWidgetHostView_->render_widget_host_, range); |
- return str; |
+ |
+ const gfx::Range requested_range(range); |
+ if (requested_range.is_reversed()) |
+ return nil; |
+ |
+ gfx::Range expected_range; |
+ const base::string16* expected_text; |
+ |
+ if (!renderWidgetHostView_->composition_range().is_empty()) { |
+ expected_text = &markedText_; |
+ expected_range = renderWidgetHostView_->composition_range(); |
+ } else { |
+ expected_text = &renderWidgetHostView_->selection_text(); |
+ size_t offset = renderWidgetHostView_->selection_text_offset(); |
+ expected_range = gfx::Range(offset, offset + expected_text->size()); |
palmer
2015/09/01 18:59:11
How do you know this range is valid? Can offset +
|
+ } |
+ |
+ if (!expected_range.Contains(requested_range)) |
+ return nil; |
+ |
+ // Gets the raw bytes to avoid unnecessary string copies for generating |
+ // NSString. |
+ const base::char16* bytes = |
+ &(*expected_text)[requested_range.start() - expected_range.start()]; |
palmer
2015/09/01 18:59:12
I don't understand this code. What is going on?
W
|
+ NSUInteger bytes_len = requested_range.length() * sizeof(base::char16); |
palmer
2015/09/01 18:59:12
Is NSUInteger guaranteed to have the same range as
|
+ base::scoped_nsobject<NSString> ns_string( |
+ [[NSString alloc] initWithBytes:bytes |
+ length:bytes_len |
+ encoding:NSUTF16LittleEndianStringEncoding]); |
+ return [[[NSAttributedString alloc] initWithString:ns_string] autorelease]; |
} |
- (NSInteger)conversationIdentifier { |