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

Side by Side Diff: chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.mm

Issue 1841813002: Update AutocompleteTextFieldEditor to use non-deprecated dragging APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h"
6 6
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h" // IDC_* 9 #include "chrome/app/chrome_command_ids.h" // IDC_*
10 #include "chrome/browser/themes/theme_service.h" 10 #include "chrome/browser/themes/theme_service.h"
11 #include "chrome/browser/ui/browser_list.h" 11 #include "chrome/browser/ui/browser_list.h"
12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 12 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
13 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" 13 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
14 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" 14 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
15 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" 15 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
16 #include "chrome/grit/generated_resources.h" 16 #include "chrome/grit/generated_resources.h"
17 #import "ui/base/cocoa/find_pasteboard.h" 17 #import "ui/base/cocoa/find_pasteboard.h"
18 #include "ui/base/l10n/l10n_util_mac.h" 18 #include "ui/base/l10n/l10n_util_mac.h"
19 #include "ui/base/material_design/material_design_controller.h" 19 #include "ui/base/material_design/material_design_controller.h"
20 20
21 @interface NSView (MountainLionSDK)
22 - (BOOL)dragSelectionWithEvent:(NSEvent*)event
23 offset:(NSSize)mouseOffset
24 slideBack:(BOOL)slideBack;
25 @end
26
21 namespace { 27 namespace {
22 28
29 // Set to true when an instance of this class is running a nested run loop.
Mark Mentovai 2016/03/30 19:59:08 Add a few words on why it’s desirable for this to
erikchen 2016/03/30 20:05:29 Done: """ 30 // Since this must always be run on
30 bool gInDrag = false;
31
23 // When too much data is put into a single-line text field, things get 32 // When too much data is put into a single-line text field, things get
24 // janky due to the cost of computing the blink rect. Sometimes users 33 // janky due to the cost of computing the blink rect. Sometimes users
25 // accidentally paste large amounts, so place a limit on what will be 34 // accidentally paste large amounts, so place a limit on what will be
26 // accepted. 35 // accepted.
27 // 36 //
28 // 10k characters was arbitrarily chosen by seeing how much a text 37 // 10k characters was arbitrarily chosen by seeing how much a text
29 // field could handle in a single line before it started getting too 38 // field could handle in a single line before it started getting too
30 // janky to recover from (jankiness was detectable around 5k). 39 // janky to recover from (jankiness was detectable around 5k).
31 // www.google.com returns an error for searches around 2k characters, 40 // www.google.com returns an error for searches around 2k characters,
32 // so this is conservative. 41 // so this is conservative.
33 const NSUInteger kMaxPasteLength = 10000; 42 const NSUInteger kMaxPasteLength = 10000;
34 43
35 // Returns |YES| if too much text would be pasted. 44 // Returns |YES| if too much text would be pasted.
36 BOOL ThePasteboardIsTooDamnBig() { 45 BOOL ThePasteboardIsTooDamnBig() {
37 NSPasteboard* pb = [NSPasteboard generalPasteboard]; 46 NSPasteboard* pb = [NSPasteboard generalPasteboard];
38 NSString* type = 47 NSString* type =
39 [pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]]; 48 [pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]];
40 if (!type) 49 if (!type)
41 return NO; 50 return NO;
42 51
43 return [[pb stringForType:type] length] > kMaxPasteLength; 52 return [[pb stringForType:type] length] > kMaxPasteLength;
44 } 53 }
45 54
46 } // namespace 55 } // namespace
47 56
57 @interface AutocompleteTextFieldEditor ()<NSDraggingSource>
58 @end
59
48 @implementation AutocompleteTextFieldEditor 60 @implementation AutocompleteTextFieldEditor
49 61
50 - (BOOL)shouldDrawInsertionPoint { 62 - (BOOL)shouldDrawInsertionPoint {
51 return [super shouldDrawInsertionPoint] && 63 return [super shouldDrawInsertionPoint] &&
52 ![[[self delegate] cell] hideFocusState]; 64 ![[[self delegate] cell] hideFocusState];
53 } 65 }
54 66
55 - (id)initWithFrame:(NSRect)frameRect { 67 - (id)initWithFrame:(NSRect)frameRect {
56 if ((self = [super initWithFrame:frameRect])) { 68 if ((self = [super initWithFrame:frameRect])) {
57 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]); 69 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // If the entire field is selected, drag the same data as would be 109 // If the entire field is selected, drag the same data as would be
98 // dragged from the field's location icon. In some cases the textual 110 // dragged from the field's location icon. In some cases the textual
99 // contents will not contain relevant data (for instance, "http://" is 111 // contents will not contain relevant data (for instance, "http://" is
100 // stripped from URLs). 112 // stripped from URLs).
101 - (BOOL)dragSelectionWithEvent:(NSEvent *)event 113 - (BOOL)dragSelectionWithEvent:(NSEvent *)event
102 offset:(NSSize)mouseOffset 114 offset:(NSSize)mouseOffset
103 slideBack:(BOOL)slideBack { 115 slideBack:(BOOL)slideBack {
104 AutocompleteTextFieldObserver* observer = [self observer]; 116 AutocompleteTextFieldObserver* observer = [self observer];
105 DCHECK(observer); 117 DCHECK(observer);
106 if (observer && observer->CanCopy()) { 118 if (observer && observer->CanCopy()) {
107 NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
108 observer->CopyToPasteboard(pboard);
109
110 NSPoint p; 119 NSPoint p;
111 NSImage* image = [self dragImageForSelectionWithEvent:event origin:&p]; 120 NSImage* image = [self dragImageForSelectionWithEvent:event origin:&p];
112 121
113 [self dragImage:image 122 base::scoped_nsobject<NSPasteboardItem> item(
114 at:p 123 observer->CreatePasteboardItem());
115 offset:mouseOffset 124 base::scoped_nsobject<NSDraggingItem> dragItem(
116 event:event 125 [[NSDraggingItem alloc] initWithPasteboardWriter:item]);
117 pasteboard:pboard 126 [dragItem setDraggingFrame:[self bounds] contents:image];
118 source:self 127 [self beginDraggingSessionWithItems:@[ dragItem.get() ]
119 slideBack:slideBack]; 128 event:event
129 source:self];
130 gInDrag = true;
Mark Mentovai 2016/03/30 19:59:08 DCHECK(!gInDrag);
erikchen 2016/03/30 20:05:29 Done.
131 while (gInDrag) {
132 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
133 beforeDate:[NSDate distantFuture]];
134 }
135
120 return YES; 136 return YES;
121 } 137 }
122 return [super dragSelectionWithEvent:event 138 return [super dragSelectionWithEvent:event
123 offset:mouseOffset 139 offset:mouseOffset
124 slideBack:slideBack]; 140 slideBack:slideBack];
125 } 141 }
126 142
143 - (void)draggingSession:(NSDraggingSession*)session
144 endedAtPoint:(NSPoint)aPoint
145 operation:(NSDragOperation)operation {
146 gInDrag = false;
147 }
148
127 - (void)copy:(id)sender { 149 - (void)copy:(id)sender {
128 AutocompleteTextFieldObserver* observer = [self observer]; 150 AutocompleteTextFieldObserver* observer = [self observer];
129 DCHECK(observer); 151 DCHECK(observer);
130 if (observer && observer->CanCopy()) 152 if (observer && observer->CanCopy())
131 observer->CopyToPasteboard([NSPasteboard generalPasteboard]); 153 observer->CopyToPasteboard([NSPasteboard generalPasteboard]);
132 } 154 }
133 155
134 - (void)cut:(id)sender { 156 - (void)cut:(id)sender {
135 [self copy:sender]; 157 [self copy:sender];
136 [self delete:nil]; 158 [self delete:nil];
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 [[self delegate] suggestText], 586 [[self delegate] suggestText],
565 [[self delegate] suggestColor], 587 [[self delegate] suggestColor],
566 self, 588 self,
567 [self bounds]); 589 [self bounds]);
568 AutocompleteTextFieldObserver* observer = [self observer]; 590 AutocompleteTextFieldObserver* observer = [self observer];
569 if (observer) 591 if (observer)
570 observer->OnDidDrawRect(); 592 observer->OnDidDrawRect();
571 } 593 }
572 594
573 @end 595 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698