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

Side by Side Diff: content/browser/renderer_host/render_widget_host_view_mac.mm

Issue 2240553003: Track text selection on the browser side (Mac) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added #ifdef Created 4 years, 4 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "content/browser/renderer_host/render_widget_host_view_mac.h" 5 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
6 6
7 #import <objc/runtime.h> 7 #import <objc/runtime.h>
8 #include <OpenGL/gl.h> 8 #include <OpenGL/gl.h>
9 #include <QuartzCore/QuartzCore.h> 9 #include <QuartzCore/QuartzCore.h>
10 #include <stdint.h> 10 #include <stdint.h>
(...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 RenderWidgetHostViewBase* updated_view) { 904 RenderWidgetHostViewBase* updated_view) {
905 const TextInputManager::CompositionRangeInfo* info = 905 const TextInputManager::CompositionRangeInfo* info =
906 GetTextInputManager()->GetCompositionRangeInfo(); 906 GetTextInputManager()->GetCompositionRangeInfo();
907 // The RangeChanged message is only sent with valid values. The current 907 // The RangeChanged message is only sent with valid values. The current
908 // caret position (start == end) will be sent if there is no IME range. 908 // caret position (start == end) will be sent if there is no IME range.
909 [cocoa_view_ setMarkedRange:info->range.ToNSRange()]; 909 [cocoa_view_ setMarkedRange:info->range.ToNSRange()];
910 composition_range_ = info->range; 910 composition_range_ = info->range;
911 composition_bounds_ = info->character_bounds; 911 composition_bounds_ = info->character_bounds;
912 } 912 }
913 913
914 void RenderWidgetHostViewMac::OnTextSelectionChanged(
915 TextInputManager* text_input_manager,
916 RenderWidgetHostViewBase* updated_view) {
917 DCHECK_EQ(GetTextInputManager(), text_input_manager);
918
919 // We obtain the TextSelection from focused RWH which is obtained from the
920 // frame tree. BrowserPlugin-based guests' RWH is not part of the frame tree
921 // and the focused RWH will be that of the embedder which is incorrect. In
922 // this case we should use TextSelection for |this| since RWHV for guest
923 // forwards text selection information to its platform view.
924 RenderWidgetHostViewBase* focused_view =
925 is_guest_view_hack_ ? this : GetFocusedWidget()
926 ? GetFocusedWidget()->GetView()
927 : nullptr;
928
929 if (!focused_view)
930 return;
931
932 const TextInputManager::TextSelection* selection =
933 GetTextInputManager()->GetTextSelection(focused_view);
934
935 base::string16 text;
936 if (selection->GetSelectedText(&text))
937 selected_text_ = base::UTF16ToUTF8(text);
938
939 [cocoa_view_ setSelectedRange:selection->range.ToNSRange()];
940 // Updates markedRange when there is no marked text so that retrieving
941 // markedRange immediately after calling setMarkdText: returns the current
942 // caret position.
943 if (![cocoa_view_ hasMarkedText]) {
944 [cocoa_view_ setMarkedRange:selection->range.ToNSRange()];
945 }
946
947 // TODO(ekaramad): The following values are tracked by TextInputManager and
948 // should be cleaned up from this class (https://crbug.com/602427).
949 selection_text_ = selection->text;
950 selection_range_ = selection->range;
951 selection_text_offset_ = selection->offset;
952 }
953
914 void RenderWidgetHostViewMac::RenderProcessGone(base::TerminationStatus status, 954 void RenderWidgetHostViewMac::RenderProcessGone(base::TerminationStatus status,
915 int error_code) { 955 int error_code) {
916 Destroy(); 956 Destroy();
917 } 957 }
918 958
919 void RenderWidgetHostViewMac::Destroy() { 959 void RenderWidgetHostViewMac::Destroy() {
920 // SurfaceClientIds registered with RenderWidgetHostInputEventRouter 960 // SurfaceClientIds registered with RenderWidgetHostInputEventRouter
921 // have already been cleared when RenderWidgetHostViewBase notified its 961 // have already been cleared when RenderWidgetHostViewBase notified its
922 // observers of our impending destruction. 962 // observers of our impending destruction.
923 [[NSNotificationCenter defaultCenter] 963 [[NSNotificationCenter defaultCenter]
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 1050
1011 void RenderWidgetHostViewMac::StopSpeaking() { 1051 void RenderWidgetHostViewMac::StopSpeaking() {
1012 if ([NSApp respondsToSelector:@selector(stopSpeaking:)]) 1052 if ([NSApp respondsToSelector:@selector(stopSpeaking:)])
1013 [NSApp stopSpeaking:cocoa_view_]; 1053 [NSApp stopSpeaking:cocoa_view_];
1014 } 1054 }
1015 1055
1016 // 1056 //
1017 // RenderWidgetHostViewCocoa uses the stored selection text, 1057 // RenderWidgetHostViewCocoa uses the stored selection text,
1018 // which implements NSServicesRequests protocol. 1058 // which implements NSServicesRequests protocol.
1019 // 1059 //
1020 void RenderWidgetHostViewMac::SelectionChanged(const base::string16& text,
1021 size_t offset,
1022 const gfx::Range& range) {
1023 if (range.is_empty() || text.empty()) {
1024 selected_text_.clear();
1025 } else {
1026 size_t pos = range.GetMin() - offset;
1027 size_t n = range.length();
1028
1029 DCHECK(pos + n <= text.length()) << "The text can not fully cover range.";
1030 if (pos >= text.length()) {
1031 DCHECK(false) << "The text can not cover range.";
1032 return;
1033 }
1034 selected_text_ = base::UTF16ToUTF8(text.substr(pos, n));
1035 }
1036
1037 [cocoa_view_ setSelectedRange:range.ToNSRange()];
1038 // Updates markedRange when there is no marked text so that retrieving
1039 // markedRange immediately after calling setMarkdText: returns the current
1040 // caret position.
1041 if (![cocoa_view_ hasMarkedText]) {
1042 [cocoa_view_ setMarkedRange:range.ToNSRange()];
1043 }
1044
1045 RenderWidgetHostViewBase::SelectionChanged(text, offset, range);
1046 }
1047 1060
1048 void RenderWidgetHostViewMac::SelectionBoundsChanged( 1061 void RenderWidgetHostViewMac::SelectionBoundsChanged(
1049 const ViewHostMsg_SelectionBounds_Params& params) { 1062 const ViewHostMsg_SelectionBounds_Params& params) {
1050 if (params.anchor_rect == params.focus_rect) 1063 if (params.anchor_rect == params.focus_rect)
1051 caret_rect_ = params.anchor_rect; 1064 caret_rect_ = params.anchor_rect;
1052 first_selection_rect_ = params.anchor_rect; 1065 first_selection_rect_ = params.anchor_rect;
1053 } 1066 }
1054 1067
1055 void RenderWidgetHostViewMac::SetShowingContextMenu(bool showing) { 1068 void RenderWidgetHostViewMac::SetShowingContextMenu(bool showing) {
1056 RenderWidgetHostViewBase::SetShowingContextMenu(showing); 1069 RenderWidgetHostViewBase::SetShowingContextMenu(showing);
(...skipping 2189 matching lines...) Expand 10 before | Expand all | Expand 10 after
3246 3259
3247 // "-webkit-app-region: drag | no-drag" is implemented on Mac by excluding 3260 // "-webkit-app-region: drag | no-drag" is implemented on Mac by excluding
3248 // regions that are not draggable. (See ControlRegionView in 3261 // regions that are not draggable. (See ControlRegionView in
3249 // native_app_window_cocoa.mm). This requires the render host view to be 3262 // native_app_window_cocoa.mm). This requires the render host view to be
3250 // draggable by default. 3263 // draggable by default.
3251 - (BOOL)mouseDownCanMoveWindow { 3264 - (BOOL)mouseDownCanMoveWindow {
3252 return YES; 3265 return YES;
3253 } 3266 }
3254 3267
3255 @end 3268 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698