diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-04 23:26:48 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-04 23:26:48 +0000 |
commit | 27e20af824af71396399a587fea101f9b2cadffb (patch) | |
tree | c830797e959effa0f512373c425af1c37e96c1e5 /chrome/browser/cocoa | |
parent | b6ea741a64dc5cddcfe735d15e68718b0f377363 (diff) | |
download | chromium_src-27e20af824af71396399a587fea101f9b2cadffb.zip chromium_src-27e20af824af71396399a587fea101f9b2cadffb.tar.gz chromium_src-27e20af824af71396399a587fea101f9b2cadffb.tar.bz2 |
[Mac] Unify drag and copy from Omnibox.
Previously drags went through the NSText code, which allowed drag of
rich text out of the Omnibox. Modify so that both drag and copy use
the same core code.
Also undo the drag-all-like-location-bar-icon effect from an earlier
change.
BUG=41414, 43183, 41218
TEST=Compare Copy with drag&drop into TextEdit with various selections.
Review URL: http://codereview.chromium.org/1936002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46407 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
4 files changed, 37 insertions, 23 deletions
diff --git a/chrome/browser/cocoa/autocomplete_text_field.h b/chrome/browser/cocoa/autocomplete_text_field.h index f07289c..23c0112 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.h +++ b/chrome/browser/cocoa/autocomplete_text_field.h @@ -39,8 +39,12 @@ class AutocompleteTextFieldObserver { // Called when the user pastes into the field. virtual void OnPaste() = 0; - // Called when the user copies the text. - virtual void OnCopy() = 0; + // Return |true| if there is a selection to copy. + virtual bool CanCopy() = 0; + + // Clears the |pboard| and adds the field's current selection. + // Called when the user does a copy or drag. + virtual void CopyToPasteboard(NSPasteboard* pboard) = 0; // Returns true if the current clipboard text supports paste and go // (or paste and search). diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.mm b/chrome/browser/cocoa/autocomplete_text_field_editor.mm index 5bbc40b..6b184bf 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.mm @@ -40,22 +40,23 @@ class Extension; - (BOOL)dragSelectionWithEvent:(NSEvent *)event offset:(NSSize)mouseOffset slideBack:(BOOL)slideBack { - const NSRange allRange = NSMakeRange(0, [[self textStorage] length]); - if (NSEqualRanges(allRange, [self selectedRange])) { - NSPasteboard* pboard = [[self delegate] locationDragPasteboard]; - if (pboard) { - NSPoint p; - NSImage* image = [self dragImageForSelectionWithEvent:event origin:&p]; - - [self dragImage:image - at:p - offset:mouseOffset - event:event - pasteboard:pboard - source:self - slideBack:slideBack]; - return YES; - } + AutocompleteTextFieldObserver* observer = [self observer]; + DCHECK(observer); + if (observer && observer->CanCopy()) { + NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; + observer->CopyToPasteboard(pboard); + + NSPoint p; + NSImage* image = [self dragImageForSelectionWithEvent:event origin:&p]; + + [self dragImage:image + at:p + offset:mouseOffset + event:event + pasteboard:pboard + source:self + slideBack:slideBack]; + return YES; } return [super dragSelectionWithEvent:event offset:mouseOffset @@ -65,8 +66,8 @@ class Extension; - (void)copy:(id)sender { AutocompleteTextFieldObserver* observer = [self observer]; DCHECK(observer); - if (observer) - observer->OnCopy(); + if (observer && observer->CanCopy()) + observer->CopyToPasteboard([NSPasteboard generalPasteboard]); } - (void)cut:(id)sender { diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm index 4d4b7a9..ab38609 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm @@ -12,12 +12,14 @@ #import "chrome/browser/cocoa/cocoa_test_helper.h" #import "chrome/browser/cocoa/test_event_utils.h" #include "grit/generated_resources.h" +#include "testing/gmock/include/gmock/gmock-matchers.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" #import "third_party/ocmock/OCMock/OCMock.h" using ::testing::Return; using ::testing::StrictMock; +using ::testing::A; namespace { @@ -192,7 +194,10 @@ TEST_F(AutocompleteTextFieldEditorObserverTest, Paste) { // Test that -copy: is correctly delegated to the observer. TEST_F(AutocompleteTextFieldEditorObserverTest, Copy) { - EXPECT_CALL(field_observer_, OnCopy()); + EXPECT_CALL(field_observer_, CanCopy()) + .WillOnce(Return(true)); + EXPECT_CALL(field_observer_, CopyToPasteboard(A<NSPasteboard*>())) + .Times(1); [editor_ copy:nil]; } @@ -207,7 +212,10 @@ TEST_F(AutocompleteTextFieldEditorObserverTest, Cut) { [editor_ selectAll:nil]; // Calls cut. - EXPECT_CALL(field_observer_, OnCopy()); + EXPECT_CALL(field_observer_, CanCopy()) + .WillOnce(Return(true)); + EXPECT_CALL(field_observer_, CopyToPasteboard(A<NSPasteboard*>())) + .Times(1); [editor_ cut:nil]; // Check if the field is cleared. diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h index 373ea36..ff2a6d3 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h @@ -35,7 +35,8 @@ namespace { class MockAutocompleteTextFieldObserver : public AutocompleteTextFieldObserver { public: MOCK_METHOD1(OnControlKeyChanged, void(bool pressed)); - MOCK_METHOD0(OnCopy, void()); + MOCK_METHOD0(CanCopy, bool()); + MOCK_METHOD1(CopyToPasteboard, void(NSPasteboard* pboard)); MOCK_METHOD0(OnPaste, void()); MOCK_METHOD0(CanPasteAndGo, bool()); MOCK_METHOD0(GetPasteActionStringId, int()); |