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

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

Issue 2523053003: Mac: Fix errors in adding the "Paste & Go" menu item. (Closed)
Patch Set: gmock dance Created 4 years, 1 month 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
« no previous file with comments | « no previous file | chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/mac/sdk_forward_declarations.h" 7 #include "base/mac/sdk_forward_declarations.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h" // IDC_* 10 #include "chrome/app/chrome_command_ids.h" // IDC_*
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 BOOL ThePasteboardIsTooDamnBig() { 42 BOOL ThePasteboardIsTooDamnBig() {
43 NSPasteboard* pb = [NSPasteboard generalPasteboard]; 43 NSPasteboard* pb = [NSPasteboard generalPasteboard];
44 NSString* type = 44 NSString* type =
45 [pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]]; 45 [pb availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]];
46 if (!type) 46 if (!type)
47 return NO; 47 return NO;
48 48
49 return [[pb stringForType:type] length] > kMaxPasteLength; 49 return [[pb stringForType:type] length] > kMaxPasteLength;
50 } 50 }
51 51
52 // Returns a possibly disabled "Paste and {Go, Search}" menu item.
53 NSMenuItem* PasteAndGoMenuItemForObserver(
54 AutocompleteTextFieldObserver* observer) {
55 DCHECK(observer);
56 int string_id = IDS_PASTE_AND_GO;
57 BOOL enabled = !ThePasteboardIsTooDamnBig() && observer->CanPasteAndGo();
58 if (enabled)
59 string_id = observer->GetPasteActionStringId();
60
61 NSString* title = l10n_util::GetNSStringWithFixup(string_id);
62 base::scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc]
63 initWithTitle:title
64 action:@selector(pasteAndGo:)
65 keyEquivalent:@""]);
66 [item setEnabled:enabled];
67 return item.autorelease();
68 }
69
52 } // namespace 70 } // namespace
53 71
54 @interface AutocompleteTextFieldEditor ()<NSDraggingSource> 72 @interface AutocompleteTextFieldEditor ()<NSDraggingSource>
55 @end 73 @end
56 74
57 @implementation AutocompleteTextFieldEditor 75 @implementation AutocompleteTextFieldEditor
58 76
59 - (BOOL)shouldDrawInsertionPoint { 77 - (BOOL)shouldDrawInsertionPoint {
60 return [super shouldDrawInsertionPoint] && 78 return [super shouldDrawInsertionPoint] &&
61 ![[[self delegate] cell] hideFocusState]; 79 ![[[self delegate] cell] hideFocusState];
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 action:@selector(cut:) 296 action:@selector(cut:)
279 keyEquivalent:@""]; 297 keyEquivalent:@""];
280 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY) 298 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY)
281 action:@selector(copy:) 299 action:@selector(copy:)
282 keyEquivalent:@""]; 300 keyEquivalent:@""];
283 301
284 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE) 302 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE)
285 action:@selector(paste:) 303 action:@selector(paste:)
286 keyEquivalent:@""]; 304 keyEquivalent:@""];
287 305
288 // TODO(shess): If the control is not editable, should we show a
289 // greyed-out "Paste and Go"?
290 if ([self isEditable]) { 306 if ([self isEditable]) {
291 // Paste and go/search. 307 [menu addItem:PasteAndGoMenuItemForObserver([self observer])];
292 AutocompleteTextFieldObserver* observer = [self observer];
293 DCHECK(observer);
294 if (!ThePasteboardIsTooDamnBig()) {
295 NSString* pasteAndGoLabel =
296 l10n_util::GetNSStringWithFixup(observer->GetPasteActionStringId());
297 DCHECK([pasteAndGoLabel length]);
298 [menu addItemWithTitle:pasteAndGoLabel
299 action:@selector(pasteAndGo:)
300 keyEquivalent:@""];
301 }
302
303 [menu addItem:[NSMenuItem separatorItem]]; 308 [menu addItem:[NSMenuItem separatorItem]];
304 309
305 NSString* searchEngineLabel = 310 NSString* searchEngineLabel =
306 l10n_util::GetNSStringWithFixup(IDS_EDIT_SEARCH_ENGINES); 311 l10n_util::GetNSStringWithFixup(IDS_EDIT_SEARCH_ENGINES);
307 DCHECK([searchEngineLabel length]); 312 DCHECK([searchEngineLabel length]);
308 NSMenuItem* item = [menu addItemWithTitle:searchEngineLabel 313 NSMenuItem* item = [menu addItemWithTitle:searchEngineLabel
309 action:@selector(commandDispatch:) 314 action:@selector(commandDispatch:)
310 keyEquivalent:@""]; 315 keyEquivalent:@""];
311 [item setTag:IDC_EDIT_SEARCH_ENGINES]; 316 [item setTag:IDC_EDIT_SEARCH_ENGINES];
312 } 317 }
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 563
559 // The text has been changed programmatically. The observer should know 564 // The text has been changed programmatically. The observer should know
560 // this change, so setting |textChangedByKeyEvents_| to NO to 565 // this change, so setting |textChangedByKeyEvents_| to NO to
561 // prevent its OnDidChange() method from being called unnecessarily. 566 // prevent its OnDidChange() method from being called unnecessarily.
562 textChangedByKeyEvents_ = NO; 567 textChangedByKeyEvents_ = NO;
563 } 568 }
564 569
565 - (BOOL)validateMenuItem:(NSMenuItem*)item { 570 - (BOOL)validateMenuItem:(NSMenuItem*)item {
566 if ([item action] == @selector(copyToFindPboard:)) 571 if ([item action] == @selector(copyToFindPboard:))
567 return [self selectedRange].length > 0; 572 return [self selectedRange].length > 0;
568 if ([item action] == @selector(pasteAndGo:)) { 573
569 // TODO(rohitrao): If the clipboard is empty, should we show a 574 // Paste & Go doesn't appear in the mainMenu. Use the enabled state when the
570 // greyed-out "Paste and Go" or nothing at all? 575 // menu item was added in -menuForEvent:.
571 AutocompleteTextFieldObserver* observer = [self observer]; 576 if ([item action] == @selector(pasteAndGo:))
572 DCHECK(observer); 577 return [item isEnabled];
573 return observer->CanPasteAndGo(); 578
574 }
575 return [super validateMenuItem:item]; 579 return [super validateMenuItem:item];
576 } 580 }
577 581
578 - (void)copyToFindPboard:(id)sender { 582 - (void)copyToFindPboard:(id)sender {
579 NSRange selectedRange = [self selectedRange]; 583 NSRange selectedRange = [self selectedRange];
580 if (selectedRange.length == 0) 584 if (selectedRange.length == 0)
581 return; 585 return;
582 NSAttributedString* selection = 586 NSAttributedString* selection =
583 [self attributedSubstringForProposedRange:selectedRange 587 [self attributedSubstringForProposedRange:selectedRange
584 actualRange:NULL]; 588 actualRange:NULL];
(...skipping 21 matching lines...) Expand all
606 // ThemedWindowDrawing implementation. 610 // ThemedWindowDrawing implementation.
607 611
608 - (void)windowDidChangeTheme { 612 - (void)windowDidChangeTheme {
609 [self updateColorsToMatchTheme]; 613 [self updateColorsToMatchTheme];
610 } 614 }
611 615
612 - (void)windowDidChangeActive { 616 - (void)windowDidChangeActive {
613 } 617 }
614 618
615 @end 619 @end
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698