OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
11 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
12 #include "base/mac/sdk_forward_declarations.h" | |
13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_window.h" | 12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_window.h" |
14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" | 13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" |
15 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" | |
16 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
17 #import "chrome/browser/ui/cocoa/view_id_util.h" | 15 #import "chrome/browser/ui/cocoa/view_id_util.h" |
18 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
19 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
20 #include "ui/base/cocoa/cocoa_base_utils.h" | 18 #include "ui/base/cocoa/cocoa_base_utils.h" |
21 #import "ui/base/cocoa/nsview_additions.h" | 19 #import "ui/base/cocoa/nsview_additions.h" |
22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 20 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
23 | 21 |
24 using base::UserMetricsAction; | 22 using base::UserMetricsAction; |
25 using bookmarks::BookmarkNode; | 23 using bookmarks::BookmarkNode; |
26 | 24 |
27 // The opacity of the bookmark button drag image. | 25 // The opacity of the bookmark button drag image. |
28 static const CGFloat kDragImageOpacity = 0.7; | 26 static const CGFloat kDragImageOpacity = 0.7; |
29 | 27 |
30 namespace { | 28 namespace { |
31 // We need a class variable to track the current dragged button to enable | 29 // We need a class variable to track the current dragged button to enable |
32 // proper live animated dragging behavior, and can't do it in the | 30 // proper live animated dragging behavior, and can't do it in the |
33 // delegate/controller since you can drag a button from one domain to the | 31 // delegate/controller since you can drag a button from one domain to the |
34 // other (from a "folder" menu, to the main bar, or vice versa). | 32 // other (from a "folder" menu, to the main bar, or vice versa). |
35 BookmarkButton* gDraggedButton = nil; // Weak | 33 BookmarkButton* gDraggedButton = nil; // Weak |
36 }; | 34 }; |
37 | 35 |
38 @interface BookmarkButton() <NSPasteboardItemDataProvider> | 36 @interface BookmarkButton(Private) |
39 | |
40 // NSPasteboardItemDataProvider: | |
41 - (void)pasteboard:(NSPasteboard*)sender | |
42 item:(NSPasteboardItem*)item | |
43 provideDataForType:(NSString*)type; | |
44 | |
45 // NSDraggingSource: | |
46 - (void)draggingSession:(NSDraggingSession*)session | |
47 endedAtPoint:(NSPoint)aPoint | |
48 operation:(NSDragOperation)operation; | |
49 - (NSDragOperation)draggingSession:(NSDraggingSession*)session | |
50 sourceOperationMaskForDraggingContext:(NSDraggingContext)context; | |
51 | 37 |
52 // Make a drag image for the button. | 38 // Make a drag image for the button. |
53 - (NSImage*)dragImage; | 39 - (NSImage*)dragImage; |
54 | 40 |
55 - (void)installCustomTrackingArea; | 41 - (void)installCustomTrackingArea; |
56 | 42 |
57 @end // @interface BookmarkButton(Private) | 43 @end // @interface BookmarkButton(Private) |
58 | 44 |
| 45 |
59 @implementation BookmarkButton | 46 @implementation BookmarkButton |
60 | 47 |
61 @synthesize delegate = delegate_; | 48 @synthesize delegate = delegate_; |
62 @synthesize acceptsTrackIn = acceptsTrackIn_; | 49 @synthesize acceptsTrackIn = acceptsTrackIn_; |
63 | 50 |
64 - (id)initWithFrame:(NSRect)frameRect { | 51 - (id)initWithFrame:(NSRect)frameRect { |
65 // BookmarkButton's ViewID may be changed to VIEW_ID_OTHER_BOOKMARKS in | 52 // BookmarkButton's ViewID may be changed to VIEW_ID_OTHER_BOOKMARKS in |
66 // BookmarkBarController, so we can't just override -viewID method to return | 53 // BookmarkBarController, so we can't just override -viewID method to return |
67 // it. | 54 // it. |
68 if ((self = [super initWithFrame:frameRect])) { | 55 if ((self = [super initWithFrame:frameRect])) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if ([self isFolder]) { | 168 if ([self isFolder]) { |
182 // Close the folder's drop-down menu if it's visible. | 169 // Close the folder's drop-down menu if it's visible. |
183 [[self target] closeBookmarkFolder:self]; | 170 [[self target] closeBookmarkFolder:self]; |
184 } | 171 } |
185 | 172 |
186 // At the moment, moving bookmarks causes their buttons (like me!) | 173 // At the moment, moving bookmarks causes their buttons (like me!) |
187 // to be destroyed and rebuilt. Make sure we don't go away while on | 174 // to be destroyed and rebuilt. Make sure we don't go away while on |
188 // the stack. | 175 // the stack. |
189 [self retain]; | 176 [self retain]; |
190 | 177 |
| 178 // Ask our delegate to fill the pasteboard for us. |
| 179 NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; |
| 180 [[self delegate] fillPasteboard:pboard forDragOfButton:self]; |
| 181 |
191 // Lock bar visibility, forcing the overlay to stay visible if we are in | 182 // Lock bar visibility, forcing the overlay to stay visible if we are in |
192 // fullscreen mode. | 183 // fullscreen mode. |
193 if ([[self delegate] dragShouldLockBarVisibility]) { | 184 if ([[self delegate] dragShouldLockBarVisibility]) { |
194 DCHECK(!visibilityDelegate_); | 185 DCHECK(!visibilityDelegate_); |
195 NSWindow* window = [[self delegate] browserWindow]; | 186 NSWindow* window = [[self delegate] browserWindow]; |
196 visibilityDelegate_ = | 187 visibilityDelegate_ = |
197 [BrowserWindowController browserWindowControllerForWindow:window]; | 188 [BrowserWindowController browserWindowControllerForWindow:window]; |
198 [visibilityDelegate_ lockBarVisibilityForOwner:self | 189 [visibilityDelegate_ lockBarVisibilityForOwner:self |
199 withAnimation:NO | 190 withAnimation:NO |
200 delay:NO]; | 191 delay:NO]; |
201 } | 192 } |
202 const BookmarkNode* node = [self bookmarkNode]; | 193 const BookmarkNode* node = [self bookmarkNode]; |
203 const BookmarkNode* parent = node ? node->parent() : NULL; | 194 const BookmarkNode* parent = node ? node->parent() : NULL; |
204 if (parent && parent->type() == BookmarkNode::FOLDER) { | 195 if (parent && parent->type() == BookmarkNode::FOLDER) { |
205 content::RecordAction(UserMetricsAction("BookmarkBarFolder_DragStart")); | 196 content::RecordAction(UserMetricsAction("BookmarkBarFolder_DragStart")); |
206 } else { | 197 } else { |
207 content::RecordAction(UserMetricsAction("BookmarkBar_DragStart")); | 198 content::RecordAction(UserMetricsAction("BookmarkBar_DragStart")); |
208 } | 199 } |
209 | 200 |
210 dragMouseOffset_ = [self convertPoint:[event locationInWindow] fromView:nil]; | 201 dragMouseOffset_ = [self convertPoint:[event locationInWindow] fromView:nil]; |
211 dragPending_ = YES; | 202 dragPending_ = YES; |
212 gDraggedButton = self; | 203 gDraggedButton = self; |
213 | 204 |
| 205 CGFloat yAt = [self bounds].size.height; |
| 206 NSSize dragOffset = NSMakeSize(0.0, 0.0); |
214 NSImage* image = [self dragImage]; | 207 NSImage* image = [self dragImage]; |
215 [self setHidden:YES]; | 208 [self setHidden:YES]; |
216 | 209 [self dragImage:image at:NSMakePoint(0, yAt) offset:dragOffset |
217 NSPasteboardItem* pbItem = [NSPasteboardItem new]; | 210 event:event pasteboard:pboard source:self slideBack:YES]; |
218 [pbItem setDataProvider:self forTypes:@[ kBookmarkButtonDragType ]]; | |
219 | |
220 base::scoped_nsobject<NSDraggingItem> dragItem( | |
221 [[NSDraggingItem alloc] initWithPasteboardWriter:pbItem]); | |
222 [dragItem setDraggingFrame:[self bounds] contents:image]; | |
223 | |
224 [self beginDraggingSessionWithItems:@[ dragItem.get() ] | |
225 event:event | |
226 source:self]; | |
227 while (gDraggedButton != nil) { | |
228 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
229 beforeDate:[NSDate distantFuture]]; | |
230 } | |
231 [self setHidden:NO]; | 211 [self setHidden:NO]; |
232 | 212 |
233 // And we're done. | 213 // And we're done. |
234 dragPending_ = NO; | 214 dragPending_ = NO; |
235 gDraggedButton = nil; | 215 gDraggedButton = nil; |
236 | 216 |
237 [self autorelease]; | 217 [self autorelease]; |
238 } | 218 } |
239 | 219 |
240 - (void)pasteboard:(NSPasteboard*)sender | |
241 item:(NSPasteboardItem*)item | |
242 provideDataForType:(NSString*)type { | |
243 [sender setData:[NSData dataWithBytes:&gDraggedButton | |
244 length:sizeof(gDraggedButton)] | |
245 forType:kBookmarkButtonDragType]; | |
246 } | |
247 | |
248 - (NSDragOperation)draggingSession:(NSDraggingSession*)session | |
249 sourceOperationMaskForDraggingContext:(NSDraggingContext)context { | |
250 NSDragOperation operation = NSDragOperationCopy; | |
251 | |
252 if (context == NSDraggingContextWithinApplication) | |
253 operation |= NSDragOperationMove; | |
254 | |
255 if ([delegate_ canDragBookmarkButtonToTrash:self]) | |
256 operation |= NSDragOperationDelete; | |
257 | |
258 return operation; | |
259 } | |
260 | |
261 // Overridden to release bar visibility. | 220 // Overridden to release bar visibility. |
262 - (DraggableButtonResult)endDrag { | 221 - (DraggableButtonResult)endDrag { |
263 gDraggedButton = nil; | 222 gDraggedButton = nil; |
264 | 223 |
265 // visibilityDelegate_ can be nil if we're detached, and that's fine. | 224 // visibilityDelegate_ can be nil if we're detached, and that's fine. |
266 [visibilityDelegate_ releaseBarVisibilityForOwner:self | 225 [visibilityDelegate_ releaseBarVisibilityForOwner:self |
267 withAnimation:YES | 226 withAnimation:YES |
268 delay:YES]; | 227 delay:YES]; |
269 visibilityDelegate_ = nil; | 228 visibilityDelegate_ = nil; |
270 | 229 |
271 return kDraggableButtonImplUseBase; | 230 return kDraggableButtonImplUseBase; |
272 } | 231 } |
273 | 232 |
274 - (void)draggingSession:(NSDraggingSession*)session | 233 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { |
275 endedAtPoint:(NSPoint)aPoint | 234 NSDragOperation operation = NSDragOperationCopy; |
276 operation:(NSDragOperation)operation { | 235 if (isLocal) { |
| 236 operation |= NSDragOperationMove; |
| 237 } |
| 238 if ([delegate_ canDragBookmarkButtonToTrash:self]) { |
| 239 operation |= NSDragOperationDelete; |
| 240 } |
| 241 return operation; |
| 242 } |
| 243 |
| 244 - (void)draggedImage:(NSImage *)anImage |
| 245 endedAt:(NSPoint)aPoint |
| 246 operation:(NSDragOperation)operation { |
277 gDraggedButton = nil; | 247 gDraggedButton = nil; |
278 // Inform delegate of drag source that we're finished dragging, | 248 // Inform delegate of drag source that we're finished dragging, |
279 // so it can close auto-opened bookmark folders etc. | 249 // so it can close auto-opened bookmark folders etc. |
280 [delegate_ bookmarkDragDidEnd:self | 250 [delegate_ bookmarkDragDidEnd:self |
281 operation:operation]; | 251 operation:operation]; |
282 // Tell delegate if it should delete us. | 252 // Tell delegate if it should delete us. |
283 if (operation & NSDragOperationDelete) { | 253 if (operation & NSDragOperationDelete) { |
284 dragEndScreenLocation_ = aPoint; | 254 dragEndScreenLocation_ = aPoint; |
285 [delegate_ didDragBookmarkToTrash:self]; | 255 [delegate_ didDragBookmarkToTrash:self]; |
286 } | 256 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 break; | 318 break; |
349 } | 319 } |
350 default: | 320 default: |
351 /* Ignore any other kind of event. */ | 321 /* Ignore any other kind of event. */ |
352 break; | 322 break; |
353 } | 323 } |
354 } | 324 } |
355 return kDraggableButtonMixinDidWork; | 325 return kDraggableButtonMixinDidWork; |
356 } | 326 } |
357 | 327 |
| 328 |
| 329 |
358 // mouseEntered: and mouseExited: are called from our | 330 // mouseEntered: and mouseExited: are called from our |
359 // BookmarkButtonCell. We redirect this information to our delegate. | 331 // BookmarkButtonCell. We redirect this information to our delegate. |
360 // The controller can then perform menu-like actions (e.g. "hover over | 332 // The controller can then perform menu-like actions (e.g. "hover over |
361 // to open menu"). | 333 // to open menu"). |
362 - (void)mouseEntered:(NSEvent*)event { | 334 - (void)mouseEntered:(NSEvent*)event { |
363 [delegate_ mouseEnteredButton:self event:event]; | 335 [delegate_ mouseEnteredButton:self event:event]; |
364 } | 336 } |
365 | 337 |
366 // See comments above mouseEntered:. | 338 // See comments above mouseEntered:. |
367 - (void)mouseExited:(NSEvent*)event { | 339 - (void)mouseExited:(NSEvent*)event { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 // ThemedWindowDrawing implementation. | 421 // ThemedWindowDrawing implementation. |
450 | 422 |
451 - (void)windowDidChangeTheme { | 423 - (void)windowDidChangeTheme { |
452 [self setNeedsDisplay:YES]; | 424 [self setNeedsDisplay:YES]; |
453 } | 425 } |
454 | 426 |
455 - (void)windowDidChangeActive { | 427 - (void)windowDidChangeActive { |
456 [self setNeedsDisplay:YES]; | 428 [self setNeedsDisplay:YES]; |
457 } | 429 } |
458 | 430 |
| 431 @end |
| 432 |
| 433 @implementation BookmarkButton(Private) |
| 434 |
| 435 |
459 - (void)installCustomTrackingArea { | 436 - (void)installCustomTrackingArea { |
460 const NSTrackingAreaOptions options = | 437 const NSTrackingAreaOptions options = |
461 NSTrackingActiveAlways | | 438 NSTrackingActiveAlways | |
462 NSTrackingMouseEnteredAndExited | | 439 NSTrackingMouseEnteredAndExited | |
463 NSTrackingEnabledDuringMouseDrag; | 440 NSTrackingEnabledDuringMouseDrag; |
464 | 441 |
465 if (area_) { | 442 if (area_) { |
466 [self removeTrackingArea:area_]; | 443 [self removeTrackingArea:area_]; |
467 [area_ release]; | 444 [area_ release]; |
468 } | 445 } |
469 | 446 |
470 area_ = [[NSTrackingArea alloc] initWithRect:[self bounds] | 447 area_ = [[NSTrackingArea alloc] initWithRect:[self bounds] |
471 options:options | 448 options:options |
472 owner:self | 449 owner:self |
473 userInfo:nil]; | 450 userInfo:nil]; |
474 [self addTrackingArea:area_]; | 451 [self addTrackingArea:area_]; |
475 } | 452 } |
476 | 453 |
| 454 |
477 - (NSImage*)dragImage { | 455 - (NSImage*)dragImage { |
478 NSRect bounds = [self bounds]; | 456 NSRect bounds = [self bounds]; |
479 base::scoped_nsobject<NSImage> image( | 457 base::scoped_nsobject<NSImage> image( |
480 [[NSImage alloc] initWithSize:bounds.size]); | 458 [[NSImage alloc] initWithSize:bounds.size]); |
481 [image lockFocusFlipped:[self isFlipped]]; | 459 [image lockFocusFlipped:[self isFlipped]]; |
482 | 460 |
483 NSGraphicsContext* context = [NSGraphicsContext currentContext]; | 461 NSGraphicsContext* context = [NSGraphicsContext currentContext]; |
484 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]); | 462 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]); |
485 CGContextBeginTransparencyLayer(cgContext, 0); | 463 CGContextBeginTransparencyLayer(cgContext, 0); |
486 CGContextSetAlpha(cgContext, kDragImageOpacity); | 464 CGContextSetAlpha(cgContext, kDragImageOpacity); |
487 | 465 |
488 GradientButtonCell* cell = | 466 GradientButtonCell* cell = |
489 base::mac::ObjCCastStrict<GradientButtonCell>([self cell]); | 467 base::mac::ObjCCastStrict<GradientButtonCell>([self cell]); |
490 [[cell clipPathForFrame:bounds inView:self] setClip]; | 468 [[cell clipPathForFrame:bounds inView:self] setClip]; |
491 [cell drawWithFrame:bounds inView:self]; | 469 [cell drawWithFrame:bounds inView:self]; |
492 | 470 |
493 CGContextEndTransparencyLayer(cgContext); | 471 CGContextEndTransparencyLayer(cgContext); |
494 [image unlockFocus]; | 472 [image unlockFocus]; |
495 | 473 |
496 return image.autorelease(); | 474 return image.autorelease(); |
497 } | 475 } |
498 | 476 |
499 @end | 477 @end // @implementation BookmarkButton(Private) |
OLD | NEW |