diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-24 02:01:08 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-24 02:01:08 +0000 |
commit | cb26fb7252e6839b7465de9ce0aec64f321a4851 (patch) | |
tree | 287fa98b14126ce7e32a0d1c138125e36135da67 /chrome/browser/cocoa | |
parent | 0790ab29d8b09d8c737ecd976822f794223b2847 (diff) | |
download | chromium_src-cb26fb7252e6839b7465de9ce0aec64f321a4851.zip chromium_src-cb26fb7252e6839b7465de9ce0aec64f321a4851.tar.gz chromium_src-cb26fb7252e6839b7465de9ce0aec64f321a4851.tar.bz2 |
[Mac] Make bookmark bar primitive drag destination.
Credits for the bookmark bar fix to dmac; stolen from http://codereview.chromium.org/267082 .
BUG=18289
TEST=Drag a link or bookmarklet from the web to the bookmark bar. It should be added at the end of the bar.
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=29906
Reverted: http://src.chromium.org/viewvc/chrome?view=rev&revision=29908
Review URL: http://codereview.chromium.org/336001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29990 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_controller.h | 3 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_controller.mm | 19 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_controller_unittest.mm | 32 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_view.h | 6 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_view.mm | 48 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.mm | 2 |
6 files changed, 108 insertions, 2 deletions
diff --git a/chrome/browser/cocoa/bookmark_bar_controller.h b/chrome/browser/cocoa/bookmark_bar_controller.h index 31a7f2b..e01312f 100644 --- a/chrome/browser/cocoa/bookmark_bar_controller.h +++ b/chrome/browser/cocoa/bookmark_bar_controller.h @@ -137,6 +137,9 @@ const CGFloat kNoBookmarksNTPVerticalOffset = 27.0; // to the user having selected "Always show the bookmark bar") - (BOOL)isAlwaysVisible; +// Returns true if at least one bookmark was added. +- (BOOL)addURLs:(NSArray*)urls withTitles:(NSArray*)titles at:(NSPoint)point; + // Actions for manipulating bookmarks. // From a button, ... - (IBAction)openBookmark:(id)sender; diff --git a/chrome/browser/cocoa/bookmark_bar_controller.mm b/chrome/browser/cocoa/bookmark_bar_controller.mm index 52ec734..5d2a6d1 100644 --- a/chrome/browser/cocoa/bookmark_bar_controller.mm +++ b/chrome/browser/cocoa/bookmark_bar_controller.mm @@ -80,6 +80,9 @@ } - (void)dealloc { + // Remove our view from its superview so it doesn't attempt to reference + // it when the controller is gone. + [[self view] removeFromSuperview]; bridge_.reset(NULL); [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; @@ -202,6 +205,22 @@ browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); } +- (BOOL)addURLs:(NSArray*)urls withTitles:(NSArray*)titles at:(NSPoint)point { + // TODO(jrg): Support drops on folders etc + // TODO(jrg): Use |point|. + DCHECK([urls count] == [titles count]); + const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); + + for (size_t i = 0; i < [urls count]; ++i) { + bookmarkModel_->AddURL( + node, + node->GetChildCount(), + base::SysNSStringToWide([titles objectAtIndex:i]), + GURL([[urls objectAtIndex:i] UTF8String])); + } + return YES; +} + - (int)currentTabContentsHeight { return browser_->GetSelectedTabContents()->view()->GetContainerSize(). height(); diff --git a/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm b/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm index 985e96f..11dc3fd 100644 --- a/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm +++ b/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm @@ -6,6 +6,7 @@ #include "base/basictypes.h" #include "base/scoped_nsobject.h" +#include "base/sys_string_conversions.h" #import "chrome/browser/cocoa/bookmark_bar_constants.h" #import "chrome/browser/cocoa/bookmark_bar_controller.h" #import "chrome/browser/cocoa/bookmark_bar_view.h" @@ -669,6 +670,37 @@ TEST_F(BookmarkBarControllerTest, BookmarkButtonSizing) { } } +TEST_F(BookmarkBarControllerTest, DropBookmarks) { + const char* urls[] = { + "http://qwantz.com", + "http://xkcd.com", + "javascript:alert('lolwut')" + }; + std::wstring titles[] = { + std::wstring(L"Philosophoraptor"), + std::wstring(L"Can't draw"), + std::wstring(L"Inspiration") + }; + EXPECT_EQ(arraysize(urls), arraysize(titles)); + + NSMutableArray* nsurls = [NSMutableArray arrayWithCapacity:0]; + NSMutableArray* nstitles = [NSMutableArray arrayWithCapacity:0]; + for (size_t i = 0; i < arraysize(urls); ++i) { + [nsurls addObject:[NSString stringWithCString:urls[i]]]; + [nstitles addObject:base::SysWideToNSString(titles[i])]; + } + + BookmarkModel* model = helper_.profile()->GetBookmarkModel(); + const BookmarkNode* parent = model->GetBookmarkBarNode(); + [bar_ addURLs:nsurls withTitles:nstitles at:NSZeroPoint]; + EXPECT_EQ(3, parent->GetChildCount()); + for (int i = 0; i < parent->GetChildCount(); ++i) { + EXPECT_EQ(parent->GetChild(i)->GetURL(), GURL(urls[i])); + EXPECT_EQ(parent->GetChild(i)->GetTitle(), titles[i]); + } +} + + // Cannot test these methods since they simply call a single static // method, BookmarkEditor::Show(), which is impossible to mock. // editBookmark:, addPage: diff --git a/chrome/browser/cocoa/bookmark_bar_view.h b/chrome/browser/cocoa/bookmark_bar_view.h index 9bd94d2..9b81ec4 100644 --- a/chrome/browser/cocoa/bookmark_bar_view.h +++ b/chrome/browser/cocoa/bookmark_bar_view.h @@ -10,11 +10,15 @@ #import <Cocoa/Cocoa.h> +@class BookmarkBarController; + @interface BookmarkBarView : NSView { + @private + IBOutlet BookmarkBarController* controller_; IBOutlet NSTextField* noItemTextfield_; } --(NSTextField*)noItemTextfield; +- (NSTextField*)noItemTextfield; @end diff --git a/chrome/browser/cocoa/bookmark_bar_view.mm b/chrome/browser/cocoa/bookmark_bar_view.mm index a991e71..d62b755 100644 --- a/chrome/browser/cocoa/bookmark_bar_view.mm +++ b/chrome/browser/cocoa/bookmark_bar_view.mm @@ -3,7 +3,10 @@ // found in the LICENSE file. #import "chrome/browser/cocoa/bookmark_bar_view.h" + +#import "chrome/browser/cocoa/bookmark_bar_controller.h" #import "third_party/GTM/AppKit/GTMTheme.h" +#import "third_party/mozilla/include/NSPasteboard+Utils.h" @interface BookmarkBarView (Private) - (void)themeDidChangeNotification:(NSNotification*)aNotification; @@ -14,6 +17,8 @@ - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; + // This probably isn't strictly necessary, but can't hurt. + [self unregisterDraggedTypes]; [super dealloc]; } @@ -23,6 +28,11 @@ selector:@selector(themeDidChangeNotification:) name:kGTMThemeDidChangeNotification object:nil]; + + DCHECK(controller_ && "Expected this to be hooked up via Interface Builder"); + NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, + NSHTMLPboardType, NSURLPboardType, nil]; + [self registerForDraggedTypes:types]; } - (void)viewDidMoveToWindow { @@ -51,4 +61,42 @@ return noItemTextfield_; } +// NSDraggingDestination methods + +- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info { + if ([[info draggingPasteboard] containsURLData]) + return NSDragOperationCopy; + return NSDragOperationNone; +} + +- (BOOL)wantsPeriodicDraggingUpdates { + // TODO(port): This should probably return |YES| and the controller should + // slide the existing bookmark buttons interactively to the side to make + // room for the about-to-be-dropped bookmark. + return NO; +} + +- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)info { + if ([[info draggingPasteboard] containsURLData]) + return NSDragOperationCopy; + return NSDragOperationNone; +} + +- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)info { + return YES; +} + +- (BOOL)performDragOperation:(id<NSDraggingInfo>)info { + NSPasteboard* pboard = [info draggingPasteboard]; + DCHECK([pboard containsURLData]); + + NSArray* urls = nil; + NSArray* titles = nil; + [pboard getURLs:&urls andTitles:&titles]; + + return [controller_ addURLs:urls + withTitles:titles + at:[info draggingLocation]]; +} + @end // @implementation BookmarkBarView diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm index 2f11527..57a5159 100644 --- a/chrome/browser/cocoa/browser_window_controller.mm +++ b/chrome/browser/cocoa/browser_window_controller.mm @@ -271,7 +271,7 @@ willPositionSheet:(NSWindow*)sheet // We can't actually use |-autorelease| here because there's an embedded // run loop in the |-performClose:| which contains its own autorelease pool. - // Instead we use call it after a zero-length delay, which gets us back + // Instead we call it after a zero-length delay, which gets us back // to the main event loop. [self performSelector:@selector(autorelease) withObject:nil |