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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/bookmarks/bookmark_node_data.h"
11 #include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_controller.h"
14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
15 #import "ui/base/cocoa/cocoa_event_utils.h"
17 NSString* kBookmarkButtonDragType = @"ChromiumBookmarkButtonDragType";
19 @interface BookmarkFolderTarget()
20 // Copies the given bookmark node to the given pasteboard, declaring appropriate
21 // types (to paste a URL with a title).
22 - (void)copyBookmarkNode:(const BookmarkNode*)node
23 toDragPasteboard:(NSPasteboard*)pboard;
26 @implementation BookmarkFolderTarget
28 - (id)initWithController:(id<BookmarkButtonControllerProtocol>)controller
29 profile:(Profile*)profile {
30 if ((self = [super init])) {
31 controller_ = controller;
37 // This IBAction is called when the user clicks (mouseUp, really) on a
38 // "folder" bookmark button. (In this context, "Click" does not
39 // include right-click to open a context menu which follows a
40 // different path). Scenarios when folder X is clicked:
41 // *Predicate* *Action*
42 // (nothing) Open Folder X
43 // Folder X open Close folder X
44 // Folder Y open Close Y, open X
45 // Cmd-click Open All with proper disposition
47 // Note complication in which a click-drag engages drag and drop, not
48 // a click-to-open. Thus the path to get here is a little twisted.
49 - (IBAction)openBookmarkFolderFromButton:(id)sender {
51 // Watch out for a modifier click. For example, command-click
54 // NOTE: we cannot use [[sender cell] mouseDownFlags] because we
55 // thwart the normal mouse click mechanism to make buttons
56 // draggable. Thus we must use [NSApp currentEvent].
58 // Holding command while using the scroll wheel (or moving around
59 // over a bookmark folder) can confuse us. Unless we check the
60 // event type, we are not sure if this is an "open folder" due to a
61 // hover-open or "open folder" due to a click. It doesn't matter
62 // (both do the same thing) unless a modifier is held, since
63 // command-click should "open all" but command-move should not.
64 // WindowOpenDispositionFromNSEvent does not consider the event
65 // type; only the modifiers. Thus the need for an extra
66 // event-type-check here.
67 DCHECK([sender bookmarkNode]->is_folder());
68 NSEvent* event = [NSApp currentEvent];
69 WindowOpenDisposition disposition =
70 ui::WindowOpenDispositionFromNSEvent(event);
71 if (([event type] != NSMouseEntered) &&
72 ([event type] != NSMouseMoved) &&
73 ([event type] != NSScrollWheel) &&
74 (disposition == NEW_BACKGROUND_TAB)) {
75 [controller_ closeAllBookmarkFolders];
76 [controller_ openAll:[sender bookmarkNode] disposition:disposition];
80 // If click on same folder, close it and be done.
81 // Else we clicked on a different folder so more work to do.
82 if ([[controller_ folderController] parentButton] == sender) {
83 [controller_ closeBookmarkFolder:controller_];
87 [controller_ addNewFolderControllerWithParentButton:sender];
90 - (void)copyBookmarkNode:(const BookmarkNode*)node
91 toDragPasteboard:(NSPasteboard*)pboard {
97 if (node->is_folder()) {
98 // TODO(viettrungluu): I'm not sure what we should do, so just declare the
99 // "additional" types we're given for now. Maybe we want to add a list of
100 // URLs? Would we then have to recurse if there were subfolders?
101 // In the meanwhile, we *must* set it to a known state.
102 [pboard clearContents];
104 BookmarkNodeData data(node);
105 data.SetOriginatingProfile(profile_);
106 data.WriteToClipboard(ui::CLIPBOARD_TYPE_DRAG);
110 - (void)fillPasteboard:(NSPasteboard*)pboard
111 forDragOfButton:(BookmarkButton*)button {
112 if (const BookmarkNode* node = [button bookmarkNode]) {
113 // Put the bookmark information into the pasteboard, and then write our own
114 // data for |kBookmarkButtonDragType|.
115 [self copyBookmarkNode:node toDragPasteboard:pboard];
116 [pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]
118 [pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]
119 forType:kBookmarkButtonDragType];