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