[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_drag_drop_cocoa.mm
blob9b54cc084af5a17237ae42908cbb505d458129c7
1 // Copyright 2012 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 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop_cocoa.h"
7 #import <Cocoa/Cocoa.h>
9 #include <cmath>
11 #include "base/logging.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h"
19 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
20 #include "components/bookmarks/browser/bookmark_model.h"
21 #include "components/bookmarks/browser/bookmark_node_data.h"
22 #include "grit/ui_resources.h"
23 #include "ui/base/dragdrop/drag_drop_types.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
27 namespace chrome {
29 namespace {
31 // Make a drag image from the drop data.
32 NSImage* MakeDragImage(BookmarkModel* model,
33                        const std::vector<const BookmarkNode*>& nodes) {
34   if (nodes.size() == 1) {
35     const BookmarkNode* node = nodes[0];
36     const gfx::Image& favicon = model->GetFavicon(node);
37     return DragImageForBookmark(
38         favicon.IsEmpty() ? nil : favicon.ToNSImage(), node->GetTitle());
39   } else {
40     // TODO(feldstein): Do something better than this. Should have badging
41     // and a single drag image.
42     // http://crbug.com/37264
43     return [NSImage imageNamed:NSImageNameMultipleDocuments];
44   }
47 // Draws string |title| within box |frame|, positioning it at the origin.
48 // Truncates text with fading if it is too long to fit horizontally.
49 // Based on code from GradientButtonCell but simplified where possible.
50 void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) {
51   NSSize size = [title size];
52   if (std::floor(size.width) <= NSWidth(frame)) {
53     [title drawAtPoint:frame.origin];
54     return;
55   }
57   // Gradient is about twice our line height long.
58   CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4);
59   NSRect solid_part, gradient_part;
60   NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, NSMaxXEdge);
61   CGContextRef context = static_cast<CGContextRef>(
62       [[NSGraphicsContext currentContext] graphicsPort]);
63   CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0);
64   { // Draw text clipped to frame.
65     gfx::ScopedNSGraphicsContextSaveGState scoped_state;
66     [NSBezierPath clipRect:frame];
67     [title drawAtPoint:frame.origin];
68   }
70   NSColor* color = [NSColor blackColor];
71   NSColor* alpha_color = [color colorWithAlphaComponent:0.0];
72   base::scoped_nsobject<NSGradient> mask(
73       [[NSGradient alloc] initWithStartingColor:color endingColor:alpha_color]);
74   // Draw the gradient mask.
75   CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
76   [mask drawFromPoint:NSMakePoint(NSMaxX(frame) - gradient_width,
77                                   NSMinY(frame))
78               toPoint:NSMakePoint(NSMaxX(frame),
79                                   NSMinY(frame))
80               options:NSGradientDrawsBeforeStartingLocation];
81   CGContextEndTransparencyLayer(context);
84 }  // namespace
86 NSImage* DragImageForBookmark(NSImage* favicon, const base::string16& title) {
87   // If no favicon, use a default.
88   if (!favicon) {
89     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
90     favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage();
91   }
93   // If no title, just use icon.
94   if (title.empty())
95     return favicon;
96   NSString* ns_title = base::SysUTF16ToNSString(title);
98   // Set the look of the title.
99   NSDictionary* attrs =
100       [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:
101                                            [NSFont smallSystemFontSize]]
102                                   forKey:NSFontAttributeName];
103   base::scoped_nsobject<NSAttributedString> rich_title(
104       [[NSAttributedString alloc] initWithString:ns_title attributes:attrs]);
106   // Set up sizes and locations for rendering.
107   const CGFloat kIconMargin = 2.0;  // Gap between icon and text.
108   CGFloat text_left = [favicon size].width + kIconMargin;
109   NSSize drag_image_size = [favicon size];
110   NSSize text_size = [rich_title size];
111   CGFloat max_text_width = bookmarks::kDefaultBookmarkWidth - text_left;
112   text_size.width = std::min(text_size.width, max_text_width);
113   drag_image_size.width = text_left + text_size.width;
115   // Render the drag image.
116   NSImage* drag_image =
117       [[[NSImage alloc] initWithSize:drag_image_size] autorelease];
118   [drag_image lockFocus];
119   [favicon drawAtPoint:NSZeroPoint
120               fromRect:NSZeroRect
121              operation:NSCompositeSourceOver
122               fraction:0.7];
123   NSRect target_text_rect = NSMakeRect(text_left, 0,
124                                        text_size.width, drag_image_size.height);
125   DrawTruncatedTitle(rich_title, target_text_rect);
126   [drag_image unlockFocus];
128   return drag_image;
131 void DragBookmarks(Profile* profile,
132                    const std::vector<const BookmarkNode*>& nodes,
133                    gfx::NativeView view,
134                    ui::DragDropTypes::DragEventSource source) {
135   DCHECK(!nodes.empty());
137   // Allow nested message loop so we get DnD events as we drag this around.
138   bool was_nested = base::MessageLoop::current()->IsNested();
139   base::MessageLoop::current()->SetNestableTasksAllowed(true);
141   BookmarkNodeData drag_data(nodes);
142   drag_data.SetOriginatingProfilePath(profile->GetPath());
143   drag_data.WriteToClipboard(ui::CLIPBOARD_TYPE_DRAG);
145   // Synthesize an event for dragging, since we can't be sure that
146   // [NSApp currentEvent] will return a valid dragging event.
147   NSWindow* window = [view window];
148   NSPoint position = [window mouseLocationOutsideOfEventStream];
149   NSTimeInterval event_time = [[NSApp currentEvent] timestamp];
150   NSEvent* drag_event = [NSEvent mouseEventWithType:NSLeftMouseDragged
151                                            location:position
152                                       modifierFlags:NSLeftMouseDraggedMask
153                                           timestamp:event_time
154                                        windowNumber:[window windowNumber]
155                                             context:nil
156                                         eventNumber:0
157                                          clickCount:1
158                                            pressure:1.0];
160   // TODO(avi): Do better than this offset.
161   NSImage* drag_image = chrome::MakeDragImage(
162       BookmarkModelFactory::GetForProfile(profile), nodes);
163   NSSize image_size = [drag_image size];
164   position.x -= std::floor(image_size.width / 2);
165   position.y -= std::floor(image_size.height / 5);
166   [window dragImage:drag_image
167                  at:position
168              offset:NSZeroSize
169               event:drag_event
170          pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard]
171              source:nil
172           slideBack:YES];
174   base::MessageLoop::current()->SetNestableTasksAllowed(was_nested);
177 }  // namespace chrome