diff options
-rw-r--r-- | chrome/browser/bookmarks/DEPS | 1 | ||||
-rw-r--r-- | chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h | 12 | ||||
-rw-r--r-- | chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.mm | 152 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.cc | 25 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.h | 23 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.mm | 190 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/location_bar/location_icon_decoration.mm | 5 | ||||
-rw-r--r-- | chrome/chrome_browser_ui.gypi | 3 |
8 files changed, 217 insertions, 194 deletions
diff --git a/chrome/browser/bookmarks/DEPS b/chrome/browser/bookmarks/DEPS index b40f808..d07579d 100644 --- a/chrome/browser/bookmarks/DEPS +++ b/chrome/browser/bookmarks/DEPS @@ -34,7 +34,6 @@ include_rules = [ "!chrome/browser/profiles/profile_keyed_service_factory.h", "!chrome/browser/ui/bookmarks/bookmark_tab_helper.h", "!chrome/browser/ui/chrome_select_file_policy.h", - "!chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h", "!chrome/browser/ui/webui/chrome_url_data_manager.h", "!chrome/browser/view_type_utils.h", # Do not add to the list of temporarily-allowed dependencies above, diff --git a/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h b/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h index 98fe3e1..d5d7797 100644 --- a/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h +++ b/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h @@ -7,13 +7,9 @@ #include "base/file_path.h" #include "chrome/browser/bookmarks/bookmark_node_data.h" -#include "ui/gfx/native_widget_types.h" #if defined(__OBJC__) -@class NSImage; @class NSString; -#else // __OBJC__ -class NSImage; #endif // __OBJC__ // This set of functions lets C++ code interact with the cocoa pasteboard @@ -40,14 +36,6 @@ bool ReadFromPasteboard(PasteboardType type, // valid bookmark. bool PasteboardContainsBookmarks(PasteboardType type); -// Returns a drag image for a bookmark. -NSImage* DragImageForBookmark(NSImage* favicon, const string16& title); - -// Copies the bookmark nodes to the dragging pasteboard and initiates a -// drag from the specified view. |view| must be a |TabContentsViewCocoa*|. -void StartDrag(Profile* profile, const std::vector<const BookmarkNode*>& nodes, - gfx::NativeView view); - } // namespace bookmark_pasteboard_helper_mac #if defined(__OBJC__) diff --git a/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.mm b/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.mm index 1cbf701..f0a66f73 100644 --- a/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.mm +++ b/chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.mm @@ -6,18 +6,8 @@ #import <Cocoa/Cocoa.h> -#include <cmath> - -#include "base/memory/scoped_nsobject.h" #include "base/sys_string_conversions.h" #include "chrome/browser/bookmarks/bookmark_model.h" -#include "chrome/browser/bookmarks/bookmark_model_factory.h" -#include "chrome/browser/profiles/profile.h" -#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" -#include "grit/ui_resources.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/gfx/mac/nsimage_cache.h" -#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" NSString* const kBookmarkDictionaryListPboardType = @"BookmarkDictionaryListPboardType"; @@ -245,60 +235,6 @@ NSPasteboard* PasteboardFromType( return [NSPasteboard pasteboardWithName:type_string]; } -// Make a drag image from the drop data. -NSImage* MakeDragImage(BookmarkModel* model, - const std::vector<const BookmarkNode*>& nodes) { - if (nodes.size() == 1) { - const BookmarkNode* node = nodes[0]; - const gfx::Image& favicon = model->GetFavicon(node); - return bookmark_pasteboard_helper_mac::DragImageForBookmark( - favicon.IsEmpty() ? nil : favicon.ToNSImage(), node->GetTitle()); - } else { - // TODO(feldstein): Do something better than this. Should have badging - // and a single drag image. - // http://crbug.com/37264 - return [NSImage imageNamed:NSImageNameMultipleDocuments]; - } -} - -// Draws string |title| within box |frame|, positioning it at the origin. -// Truncates text with fading if it is too long to fit horizontally. -// Based on code from GradientButtonCell but simplified where possible. -void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) { - NSSize size = [title size]; - if (std::floor(size.width) <= NSWidth(frame)) { - [title drawAtPoint:frame.origin]; - return; - } - - // Gradient is about twice our line height long. - CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4); - NSRect solid_part, gradient_part; - NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, NSMaxXEdge); - CGContextRef context = static_cast<CGContextRef>( - [[NSGraphicsContext currentContext] graphicsPort]); - CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0); - { // Draw text clipped to frame. - gfx::ScopedNSGraphicsContextSaveGState scoped_state; - [NSBezierPath clipRect:frame]; - [title drawAtPoint:frame.origin]; - } - - NSColor* color = [NSColor blackColor]; - NSColor* alpha_color = [color colorWithAlphaComponent:0.0]; - scoped_nsobject<NSGradient> mask( - [[NSGradient alloc] initWithStartingColor:color - endingColor:alpha_color]); - // Draw the gradient mask. - CGContextSetBlendMode(context, kCGBlendModeDestinationIn); - [mask drawFromPoint:NSMakePoint(NSMaxX(frame) - gradient_width, - NSMinY(frame)) - toPoint:NSMakePoint(NSMaxX(frame), - NSMinY(frame)) - options:NSGradientDrawsBeforeStartingLocation]; - CGContextEndTransparencyLayer(context); -} - } // namespace namespace bookmark_pasteboard_helper_mac { @@ -349,92 +285,4 @@ bool PasteboardContainsBookmarks(PasteboardType type) { return [pb availableTypeFromArray:availableTypes] != nil; } -NSImage* DragImageForBookmark(NSImage* favicon, const string16& title) { - // If no favicon, use a default. - if (!favicon) { - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); - favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); - } - - // If no title, just use icon. - if (title.empty()) - return favicon; - NSString* ns_title = base::SysUTF16ToNSString(title); - - // Set the look of the title. - NSDictionary* attrs = - [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize: - [NSFont smallSystemFontSize]] - forKey:NSFontAttributeName]; - scoped_nsobject<NSAttributedString> rich_title( - [[NSAttributedString alloc] initWithString:ns_title - attributes:attrs]); - - // Set up sizes and locations for rendering. - const CGFloat kIconMargin = 2.0; // Gap between icon and text. - CGFloat text_left = [favicon size].width + kIconMargin; - NSSize drag_image_size = [favicon size]; - NSSize text_size = [rich_title size]; - CGFloat max_text_width = bookmarks::kDefaultBookmarkWidth - text_left; - text_size.width = std::min(text_size.width, max_text_width); - drag_image_size.width = text_left + text_size.width; - - // Render the drag image. - NSImage* drag_image = - [[[NSImage alloc] initWithSize:drag_image_size] autorelease]; - [drag_image lockFocus]; - [favicon drawAtPoint:NSMakePoint(0, 0) - fromRect:NSZeroRect - operation:NSCompositeSourceOver - fraction:0.7]; - NSRect target_text_rect = NSMakeRect(text_left, 0, - text_size.width, drag_image_size.height); - DrawTruncatedTitle(rich_title, target_text_rect); - [drag_image unlockFocus]; - - return drag_image; -} - -void StartDrag(Profile* profile, - const std::vector<const BookmarkNode*>& nodes, - gfx::NativeView view) { - - std::vector<BookmarkNodeData::Element> elements; - for (std::vector<const BookmarkNode*>::const_iterator it = nodes.begin(); - it != nodes.end(); ++it) { - elements.push_back(BookmarkNodeData::Element(*it)); - } - - WriteToPasteboard(kDragPasteboard, elements, profile->GetPath().value()); - - // Synthesize an event for dragging, since we can't be sure that - // [NSApp currentEvent] will return a valid dragging event. - NSWindow* window = [view window]; - NSPoint position = [window mouseLocationOutsideOfEventStream]; - NSTimeInterval event_time = [[NSApp currentEvent] timestamp]; - NSEvent* drag_event = [NSEvent mouseEventWithType:NSLeftMouseDragged - location:position - modifierFlags:NSLeftMouseDraggedMask - timestamp:event_time - windowNumber:[window windowNumber] - context:nil - eventNumber:0 - clickCount:1 - pressure:1.0]; - - // TODO(avi): Do better than this offset. - NSImage* drag_image = - MakeDragImage(BookmarkModelFactory::GetForProfile(profile), nodes); - NSSize image_size = [drag_image size]; - position.x -= std::floor(image_size.width / 2); - position.y -= std::floor(image_size.height / 5); - [window dragImage:drag_image - at:position - offset:NSZeroSize - event:drag_event - pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard] - source:nil - slideBack:YES]; -} - } // namespace bookmark_pasteboard_helper_mac diff --git a/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.cc b/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.cc deleted file mode 100644 index a133498..0000000 --- a/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.cc +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/bookmarks/bookmark_utils.h" - -#include "base/logging.h" -#include "base/message_loop.h" -#include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" - -namespace bookmark_utils { - -void DragBookmarks(Profile* profile, - const std::vector<const BookmarkNode*>& nodes, - gfx::NativeView view) { - DCHECK(!nodes.empty()); - - // Allow nested message loop so we get DnD events as we drag this around. - bool was_nested = MessageLoop::current()->IsNested(); - MessageLoop::current()->SetNestableTasksAllowed(true); - bookmark_pasteboard_helper_mac::StartDrag(profile, nodes, view); - MessageLoop::current()->SetNestableTasksAllowed(was_nested); -} - -} // namespace bookmark_utils diff --git a/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.h b/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.h new file mode 100644 index 0000000..ed2ac08 --- /dev/null +++ b/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.h @@ -0,0 +1,23 @@ +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_DRAG_DROP_H_ +#define CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_DRAG_DROP_H_ + +#include "base/string16.h" + +#if defined(__OBJC__) +@class NSImage; +#else // __OBJC__ +class NSImage; +#endif // __OBJC__ + +namespace chrome { + +// Returns a drag image for a bookmark. +NSImage* DragImageForBookmark(NSImage* favicon, const string16& title); + +} // namespace chrome + +#endif // CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_DRAG_DROP_H_ diff --git a/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.mm b/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.mm new file mode 100644 index 0000000..6718e7c --- /dev/null +++ b/chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.mm @@ -0,0 +1,190 @@ +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.h" + +#import <Cocoa/Cocoa.h> + +#include <cmath> + +#include "base/logging.h" +#include "base/memory/scoped_nsobject.h" +#include "base/message_loop.h" +#include "base/string16.h" +#include "base/sys_string_conversions.h" +#include "chrome/browser/bookmarks/bookmark_model_factory.h" +#include "chrome/browser/bookmarks/bookmark_model.h" +#include "chrome/browser/bookmarks/bookmark_node_data.h" +#include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" +#include "chrome/browser/bookmarks/bookmark_utils.h" +#include "chrome/browser/profiles/profile.h" +#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" +#include "grit/ui_resources.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/gfx/mac/nsimage_cache.h" +#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" + +namespace chrome { + +namespace { + +// Make a drag image from the drop data. +NSImage* MakeDragImage(BookmarkModel* model, + const std::vector<const BookmarkNode*>& nodes) { + if (nodes.size() == 1) { + const BookmarkNode* node = nodes[0]; + const gfx::Image& favicon = model->GetFavicon(node); + return DragImageForBookmark( + favicon.IsEmpty() ? nil : favicon.ToNSImage(), node->GetTitle()); + } else { + // TODO(feldstein): Do something better than this. Should have badging + // and a single drag image. + // http://crbug.com/37264 + return [NSImage imageNamed:NSImageNameMultipleDocuments]; + } +} + +// Draws string |title| within box |frame|, positioning it at the origin. +// Truncates text with fading if it is too long to fit horizontally. +// Based on code from GradientButtonCell but simplified where possible. +void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) { + NSSize size = [title size]; + if (std::floor(size.width) <= NSWidth(frame)) { + [title drawAtPoint:frame.origin]; + return; + } + + // Gradient is about twice our line height long. + CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4); + NSRect solid_part, gradient_part; + NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, NSMaxXEdge); + CGContextRef context = static_cast<CGContextRef>( + [[NSGraphicsContext currentContext] graphicsPort]); + CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0); + { // Draw text clipped to frame. + gfx::ScopedNSGraphicsContextSaveGState scoped_state; + [NSBezierPath clipRect:frame]; + [title drawAtPoint:frame.origin]; + } + + NSColor* color = [NSColor blackColor]; + NSColor* alpha_color = [color colorWithAlphaComponent:0.0]; + scoped_nsobject<NSGradient> mask( + [[NSGradient alloc] initWithStartingColor:color + endingColor:alpha_color]); + // Draw the gradient mask. + CGContextSetBlendMode(context, kCGBlendModeDestinationIn); + [mask drawFromPoint:NSMakePoint(NSMaxX(frame) - gradient_width, + NSMinY(frame)) + toPoint:NSMakePoint(NSMaxX(frame), + NSMinY(frame)) + options:NSGradientDrawsBeforeStartingLocation]; + CGContextEndTransparencyLayer(context); +} + +} // namespace + +NSImage* DragImageForBookmark(NSImage* favicon, const string16& title) { + // If no favicon, use a default. + if (!favicon) { + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); + favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); + } + + // If no title, just use icon. + if (title.empty()) + return favicon; + NSString* ns_title = base::SysUTF16ToNSString(title); + + // Set the look of the title. + NSDictionary* attrs = + [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize: + [NSFont smallSystemFontSize]] + forKey:NSFontAttributeName]; + scoped_nsobject<NSAttributedString> rich_title( + [[NSAttributedString alloc] initWithString:ns_title + attributes:attrs]); + + // Set up sizes and locations for rendering. + const CGFloat kIconMargin = 2.0; // Gap between icon and text. + CGFloat text_left = [favicon size].width + kIconMargin; + NSSize drag_image_size = [favicon size]; + NSSize text_size = [rich_title size]; + CGFloat max_text_width = bookmarks::kDefaultBookmarkWidth - text_left; + text_size.width = std::min(text_size.width, max_text_width); + drag_image_size.width = text_left + text_size.width; + + // Render the drag image. + NSImage* drag_image = + [[[NSImage alloc] initWithSize:drag_image_size] autorelease]; + [drag_image lockFocus]; + [favicon drawAtPoint:NSMakePoint(0, 0) + fromRect:NSZeroRect + operation:NSCompositeSourceOver + fraction:0.7]; + NSRect target_text_rect = NSMakeRect(text_left, 0, + text_size.width, drag_image_size.height); + DrawTruncatedTitle(rich_title, target_text_rect); + [drag_image unlockFocus]; + + return drag_image; +} + +} // namespace chrome + +namespace bookmark_utils { + +void DragBookmarks(Profile* profile, + const std::vector<const BookmarkNode*>& nodes, + gfx::NativeView view) { + DCHECK(!nodes.empty()); + + // Allow nested message loop so we get DnD events as we drag this around. + bool was_nested = MessageLoop::current()->IsNested(); + MessageLoop::current()->SetNestableTasksAllowed(true); + + std::vector<BookmarkNodeData::Element> elements; + for (std::vector<const BookmarkNode*>::const_iterator it = nodes.begin(); + it != nodes.end(); ++it) { + elements.push_back(BookmarkNodeData::Element(*it)); + } + + bookmark_pasteboard_helper_mac::WriteToPasteboard( + bookmark_pasteboard_helper_mac::kDragPasteboard, + elements, + profile->GetPath().value()); + + // Synthesize an event for dragging, since we can't be sure that + // [NSApp currentEvent] will return a valid dragging event. + NSWindow* window = [view window]; + NSPoint position = [window mouseLocationOutsideOfEventStream]; + NSTimeInterval event_time = [[NSApp currentEvent] timestamp]; + NSEvent* drag_event = [NSEvent mouseEventWithType:NSLeftMouseDragged + location:position + modifierFlags:NSLeftMouseDraggedMask + timestamp:event_time + windowNumber:[window windowNumber] + context:nil + eventNumber:0 + clickCount:1 + pressure:1.0]; + + // TODO(avi): Do better than this offset. + NSImage* drag_image = chrome::MakeDragImage( + BookmarkModelFactory::GetForProfile(profile), nodes); + NSSize image_size = [drag_image size]; + position.x -= std::floor(image_size.width / 2); + position.y -= std::floor(image_size.height / 5); + [window dragImage:drag_image + at:position + offset:NSZeroSize + event:drag_event + pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard] + source:nil + slideBack:YES]; + + MessageLoop::current()->SetNestableTasksAllowed(was_nested); +} + +} // namespace bookmark_utils diff --git a/chrome/browser/ui/cocoa/location_bar/location_icon_decoration.mm b/chrome/browser/ui/cocoa/location_bar/location_icon_decoration.mm index 371de8f..522728e 100644 --- a/chrome/browser/ui/cocoa/location_bar/location_icon_decoration.mm +++ b/chrome/browser/ui/cocoa/location_bar/location_icon_decoration.mm @@ -5,10 +5,10 @@ #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" #include "base/sys_string_conversions.h" -#import "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_commands.h" #include "chrome/browser/ui/browser_finder.h" +#import "chrome/browser/ui/cocoa/bookmarks/bookmark_drag_drop.h" #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_entry.h" @@ -67,8 +67,7 @@ NSImage* LocationIconDecoration::GetDragImage() { NSImage* favicon = owner_->GetFavicon().AsNSImage(); NSImage* iconImage = favicon ? favicon : GetImage(); - NSImage* image = bookmark_pasteboard_helper_mac::DragImageForBookmark( - iconImage, owner_->GetTitle()); + NSImage* image = chrome::DragImageForBookmark(iconImage, owner_->GetTitle()); NSSize imageSize = [image size]; drag_frame_ = NSMakeRect(0, 0, imageSize.width, imageSize.height); return image; diff --git a/chrome/chrome_browser_ui.gypi b/chrome/chrome_browser_ui.gypi index 2ef9985..30542fc 100644 --- a/chrome/chrome_browser_ui.gypi +++ b/chrome/chrome_browser_ui.gypi @@ -323,7 +323,8 @@ 'browser/ui/cocoa/bookmarks/bookmark_button.mm', 'browser/ui/cocoa/bookmarks/bookmark_button_cell.h', 'browser/ui/cocoa/bookmarks/bookmark_button_cell.mm', - 'browser/ui/cocoa/bookmarks/bookmark_drag_drop.cc', + 'browser/ui/cocoa/bookmarks/bookmark_drag_drop.h', + 'browser/ui/cocoa/bookmarks/bookmark_drag_drop.mm', 'browser/ui/cocoa/bookmarks/bookmark_editor_base_controller.h', 'browser/ui/cocoa/bookmarks/bookmark_editor_base_controller.mm', 'browser/ui/cocoa/bookmarks/bookmark_editor_controller.h', |