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