Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: chrome/browser/ui/cocoa/bookmarks/bookmark_button.mm

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

Powered by Google App Engine
This is Rietveld 408576698