diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-14 21:46:48 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-14 21:46:48 +0000 |
commit | 7421ce42842857210306b2501ecada7f877e721d (patch) | |
tree | 5495d269366fdf4e6fe5dee3356caf5f572b78c5 | |
parent | 2f2d704a4fbee7cf83f30be7f668c7c793a08b54 (diff) | |
download | chromium_src-7421ce42842857210306b2501ecada7f877e721d.zip chromium_src-7421ce42842857210306b2501ecada7f877e721d.tar.gz chromium_src-7421ce42842857210306b2501ecada7f877e721d.tar.bz2 |
[Mac] Omnibox text drag drag URL when select-all.
If the entire field is selected, make the drag contents match those
which would be dragged from the location icon.
BUG=41218
TEST=Browser to www.google.com. Select-all, drag&drop to bookmarks.
Review URL: http://codereview.chromium.org/1622024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44545 0039d316-1c4b-4281-b951-d872f2087c98
6 files changed, 78 insertions, 5 deletions
diff --git a/chrome/browser/cocoa/autocomplete_text_field.h b/chrome/browser/cocoa/autocomplete_text_field.h index 5130072..8e16705 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.h +++ b/chrome/browser/cocoa/autocomplete_text_field.h @@ -123,6 +123,9 @@ class AutocompleteTextFieldObserver { // positioning the bookmark bubble. - (NSRect)starIconFrame; +// If the location icon is draggable, return its drag pasteboard. +- (NSPasteboard*)locationDragPasteboard; + @end #endif // CHROME_BROWSER_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_ diff --git a/chrome/browser/cocoa/autocomplete_text_field.mm b/chrome/browser/cocoa/autocomplete_text_field.mm index a06ecac..49b0d80 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.mm +++ b/chrome/browser/cocoa/autocomplete_text_field.mm @@ -377,4 +377,8 @@ return [cell starIconFrameForFrame:[self bounds]]; } +- (NSPasteboard*)locationDragPasteboard { + return [[self autocompleteTextFieldCell] locationDragPasteboard]; +} + @end diff --git a/chrome/browser/cocoa/autocomplete_text_field_cell.h b/chrome/browser/cocoa/autocomplete_text_field_cell.h index 080e580..21963bc 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_cell.h +++ b/chrome/browser/cocoa/autocomplete_text_field_cell.h @@ -146,6 +146,9 @@ class ExtensionAction; inRect:(NSRect)cellFrame ofView:(AutocompleteTextField*)controlView; +// If the location icon is draggable, return its drag pasteboard. +- (NSPasteboard*)locationDragPasteboard; + @end // Internal methods here exposed for unit testing. diff --git a/chrome/browser/cocoa/autocomplete_text_field_cell.mm b/chrome/browser/cocoa/autocomplete_text_field_cell.mm index ae9777f..cc8fc51 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_cell.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_cell.mm @@ -665,4 +665,11 @@ NSAttributedString* AttributedStringForImage(NSImage* anImage, return NSDragOperationCopy; } +- (NSPasteboard*)locationDragPasteboard { + if (locationIconView_ && locationIconView_->IsDraggable()) + return locationIconView_->GetDragPasteboard(); + + return nil; +} + @end diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.mm b/chrome/browser/cocoa/autocomplete_text_field_editor.mm index 70aa5cd..974b346 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.mm @@ -33,6 +33,35 @@ class Extension; return self; } +// If the entire field is selected, drag the same data as would be +// dragged from the field's location icon. In some cases the textual +// contents will not contain relevant data (for instance, "http://" is +// stripped from URLs). +- (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; + } + } + return [super dragSelectionWithEvent:event + offset:mouseOffset + slideBack:slideBack]; +} + - (void)copy:(id)sender { AutocompleteTextFieldObserver* observer = [self observer]; DCHECK(observer); diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm index b6f044f..1d0ae49 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm @@ -24,23 +24,32 @@ using ::testing::StrictMock; namespace { class MockLocationIconView : public LocationBarViewMac::LocationIconView { public: - MockLocationIconView(LocationBarViewMac* owner) - : LocationBarViewMac::LocationIconView(owner) {} + MockLocationIconView() + : LocationBarViewMac::LocationIconView(NULL), + is_draggable_(false), + mouse_was_pressed_(false) {} // |LocationBarViewMac::LocationIconView| dragging support needs // more setup than this test provides. bool IsDraggable() { - return false; + return is_draggable_; } virtual NSPasteboard* GetDragPasteboard() { return [NSPasteboard pasteboardWithUniqueName]; } + void SetDraggable(bool is_draggable) { + is_draggable_ = is_draggable; + } // We can't use gmock's MOCK_METHOD macro, because it doesn't like the // NSRect argument to OnMousePressed. virtual void OnMousePressed(NSRect bounds) { mouse_was_pressed_ = true; } + bool MouseWasPressed() { return mouse_was_pressed_; } + + private: + bool is_draggable_; bool mouse_was_pressed_; }; @@ -594,7 +603,7 @@ TEST_F(AutocompleteTextFieldTest, TripleClickSelectsAll) { TEST_F(AutocompleteTextFieldTest, LocationIconMouseDown) { AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; - MockLocationIconView location_icon_view(NULL); + MockLocationIconView location_icon_view; [cell setLocationIconView:&location_icon_view]; location_icon_view.SetImage( ResourceBundle::GetSharedInstance().GetNSImageNamed( @@ -610,7 +619,7 @@ TEST_F(AutocompleteTextFieldTest, LocationIconMouseDown) { // mouse-up. [NSApp postEvent:upEvent atStart:YES]; [field_ mouseDown:downEvent]; - EXPECT_TRUE(location_icon_view.mouse_was_pressed_); + EXPECT_TRUE(location_icon_view.MouseWasPressed()); // TODO(shess): Test that mouse drags are initiated if the next // event is a drag, or if the mouse-up takes too long to arrive. @@ -863,4 +872,22 @@ TEST_F(AutocompleteTextFieldObserverTest, SendsOnResignKey) { [test_window() resignKeyWindow]; } +TEST_F(AutocompleteTextFieldTest, LocationDragPasteboard) { + AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; + + MockLocationIconView location_icon_view; + location_icon_view.SetImage( + ResourceBundle::GetSharedInstance().GetNSImageNamed( + IDR_OMNIBOX_HTTPS_VALID)); + location_icon_view.SetVisible(true); + [cell setLocationIconView:&location_icon_view]; + + // Not draggable, so no pasteboard. + EXPECT_FALSE([field_ locationDragPasteboard]); + + // Gets a pasteboard when draggable. + location_icon_view.SetDraggable(true); + EXPECT_TRUE([field_ locationDragPasteboard]); +} + } // namespace |