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