Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 - (NSDraggingSession*)beginDraggingSessionWithItems:(NSArray*)items | |
| 23 event:(NSEvent*)event | |
| 24 source: | |
| 25 (id<NSDraggingSource>)source; | |
| 26 @end | |
|
Avi (use Gerrit)
2016/04/01 22:31:25
base/mac/sdk_forward_declarations.h ?
erikchen
2016/04/01 22:41:08
Done.
| |
| 27 | |
| 21 namespace { | 28 namespace { |
| 22 | 29 |
| 30 // Set to true when an instance of this class is running a nested run loop. | |
| 31 // Since this must always be run on the UI thread, there should never be two | |
| 32 // simultaneous drags. | |
| 33 bool gInDrag = false; | |
| 34 | |
| 23 // When too much data is put into a single-line text field, things get | 35 // 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 | 36 // 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 | 37 // accidentally paste large amounts, so place a limit on what will be |
| 26 // accepted. | 38 // accepted. |
| 27 // | 39 // |
| 28 // 10k characters was arbitrarily chosen by seeing how much a text | 40 // 10k characters was arbitrarily chosen by seeing how much a text |
| 29 // field could handle in a single line before it started getting too | 41 // field could handle in a single line before it started getting too |
| 30 // janky to recover from (jankiness was detectable around 5k). | 42 // janky to recover from (jankiness was detectable around 5k). |
| 31 // www.google.com returns an error for searches around 2k characters, | 43 // www.google.com returns an error for searches around 2k characters, |
| 32 // so this is conservative. | 44 // so this is conservative. |
| 33 const NSUInteger kMaxPasteLength = 10000; | 45 const NSUInteger kMaxPasteLength = 10000; |
| 34 | 46 |
| 35 // Returns |YES| if too much text would be pasted. | 47 // Returns |YES| if too much text would be pasted. |
| 36 BOOL ThePasteboardIsTooDamnBig() { | 48 BOOL ThePasteboardIsTooDamnBig() { |
| 37 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 49 NSPasteboard* pb = [NSPasteboard generalPasteboard]; |
| 38 NSString* type = | 50 NSString* type = |
| 39 [pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]]; | 51 [pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]]; |
| 40 if (!type) | 52 if (!type) |
| 41 return NO; | 53 return NO; |
| 42 | 54 |
| 43 return [[pb stringForType:type] length] > kMaxPasteLength; | 55 return [[pb stringForType:type] length] > kMaxPasteLength; |
| 44 } | 56 } |
| 45 | 57 |
| 46 } // namespace | 58 } // namespace |
| 47 | 59 |
| 60 @interface AutocompleteTextFieldEditor ()<NSDraggingSource> | |
| 61 @end | |
| 62 | |
| 48 @implementation AutocompleteTextFieldEditor | 63 @implementation AutocompleteTextFieldEditor |
| 49 | 64 |
| 50 - (BOOL)shouldDrawInsertionPoint { | 65 - (BOOL)shouldDrawInsertionPoint { |
| 51 return [super shouldDrawInsertionPoint] && | 66 return [super shouldDrawInsertionPoint] && |
| 52 ![[[self delegate] cell] hideFocusState]; | 67 ![[[self delegate] cell] hideFocusState]; |
| 53 } | 68 } |
| 54 | 69 |
| 55 - (id)initWithFrame:(NSRect)frameRect { | 70 - (id)initWithFrame:(NSRect)frameRect { |
| 56 if ((self = [super initWithFrame:frameRect])) { | 71 if ((self = [super initWithFrame:frameRect])) { |
| 57 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]); | 72 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 // If the entire field is selected, drag the same data as would be | 112 // 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 | 113 // dragged from the field's location icon. In some cases the textual |
| 99 // contents will not contain relevant data (for instance, "http://" is | 114 // contents will not contain relevant data (for instance, "http://" is |
| 100 // stripped from URLs). | 115 // stripped from URLs). |
| 101 - (BOOL)dragSelectionWithEvent:(NSEvent *)event | 116 - (BOOL)dragSelectionWithEvent:(NSEvent *)event |
| 102 offset:(NSSize)mouseOffset | 117 offset:(NSSize)mouseOffset |
| 103 slideBack:(BOOL)slideBack { | 118 slideBack:(BOOL)slideBack { |
| 104 AutocompleteTextFieldObserver* observer = [self observer]; | 119 AutocompleteTextFieldObserver* observer = [self observer]; |
| 105 DCHECK(observer); | 120 DCHECK(observer); |
| 106 if (observer && observer->CanCopy()) { | 121 if (observer && observer->CanCopy()) { |
| 107 NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; | |
| 108 observer->CopyToPasteboard(pboard); | |
| 109 | |
| 110 NSPoint p; | 122 NSPoint p; |
| 111 NSImage* image = [self dragImageForSelectionWithEvent:event origin:&p]; | 123 NSImage* image = [self dragImageForSelectionWithEvent:event origin:&p]; |
| 112 | 124 |
| 113 [self dragImage:image | 125 base::scoped_nsobject<NSPasteboardItem> item( |
| 114 at:p | 126 observer->CreatePasteboardItem()); |
| 115 offset:mouseOffset | 127 base::scoped_nsobject<NSDraggingItem> dragItem( |
| 116 event:event | 128 [[NSDraggingItem alloc] initWithPasteboardWriter:item]); |
| 117 pasteboard:pboard | 129 [dragItem setDraggingFrame:[self bounds] contents:image]; |
| 118 source:self | 130 [self beginDraggingSessionWithItems:@[ dragItem.get() ] |
| 119 slideBack:slideBack]; | 131 event:event |
| 132 source:self]; | |
| 133 DCHECK(!gInDrag); | |
| 134 gInDrag = true; | |
| 135 while (gInDrag) { | |
| 136 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
| 137 beforeDate:[NSDate distantFuture]]; | |
| 138 } | |
| 139 | |
| 120 return YES; | 140 return YES; |
| 121 } | 141 } |
| 122 return [super dragSelectionWithEvent:event | 142 return [super dragSelectionWithEvent:event |
| 123 offset:mouseOffset | 143 offset:mouseOffset |
| 124 slideBack:slideBack]; | 144 slideBack:slideBack]; |
| 125 } | 145 } |
| 126 | 146 |
| 147 - (void)draggingSession:(NSDraggingSession*)session | |
| 148 endedAtPoint:(NSPoint)aPoint | |
| 149 operation:(NSDragOperation)operation { | |
| 150 gInDrag = false; | |
| 151 } | |
| 152 | |
| 127 - (void)copy:(id)sender { | 153 - (void)copy:(id)sender { |
| 128 AutocompleteTextFieldObserver* observer = [self observer]; | 154 AutocompleteTextFieldObserver* observer = [self observer]; |
| 129 DCHECK(observer); | 155 DCHECK(observer); |
| 130 if (observer && observer->CanCopy()) | 156 if (observer && observer->CanCopy()) |
| 131 observer->CopyToPasteboard([NSPasteboard generalPasteboard]); | 157 observer->CopyToPasteboard([NSPasteboard generalPasteboard]); |
| 132 } | 158 } |
| 133 | 159 |
| 134 - (void)cut:(id)sender { | 160 - (void)cut:(id)sender { |
| 135 [self copy:sender]; | 161 [self copy:sender]; |
| 136 [self delete:nil]; | 162 [self delete:nil]; |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 [[self delegate] suggestText], | 590 [[self delegate] suggestText], |
| 565 [[self delegate] suggestColor], | 591 [[self delegate] suggestColor], |
| 566 self, | 592 self, |
| 567 [self bounds]); | 593 [self bounds]); |
| 568 AutocompleteTextFieldObserver* observer = [self observer]; | 594 AutocompleteTextFieldObserver* observer = [self observer]; |
| 569 if (observer) | 595 if (observer) |
| 570 observer->OnDidDrawRect(); | 596 observer->OnDidDrawRect(); |
| 571 } | 597 } |
| 572 | 598 |
| 573 @end | 599 @end |
| OLD | NEW |