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

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: Test errors. 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
219 setDataProvider:self
220 forTypes:[NSArray arrayWithObjects:kBookmarkButtonDragType, nil]];
Avi (use Gerrit) 2016/03/18 21:41:03 @[ kBookmarkButtonDragType ] ?
erikchen 2016/03/18 22:20:35 Done.
221
222 base::scoped_nsobject<NSDraggingItem> dragItem(
223 [[NSDraggingItem alloc] initWithPasteboardWriter:pbItem]);
224 [dragItem setDraggingFrame:[self bounds] contents:image];
225
226 [self beginDraggingSessionWithItems:@[ dragItem.get() ]
227 event:event
228 source:self];
229 while (gDraggedButton != nil) {
230 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
231 beforeDate:[NSDate distantFuture]];
232 }
211 [self setHidden:NO]; 233 [self setHidden:NO];
212 234
213 // And we're done. 235 // And we're done.
214 dragPending_ = NO; 236 dragPending_ = NO;
215 gDraggedButton = nil; 237 gDraggedButton = nil;
216 238
217 [self autorelease]; 239 [self autorelease];
218 } 240 }
219 241
242 - (void)pasteboard:(NSPasteboard*)sender
243 item:(NSPasteboardItem*)item
244 provideDataForType:(NSString*)type {
245 [sender setData:[NSData dataWithBytes:&gDraggedButton
246 length:sizeof(gDraggedButton)]
247 forType:kBookmarkButtonDragType];
248 }
249
250 - (NSDragOperation)draggingSession:(NSDraggingSession*)session
251 sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
252 NSDragOperation operation = NSDragOperationCopy;
253
254 if (context == NSDraggingContextWithinApplication)
255 operation |= NSDragOperationMove;
256
257 if ([delegate_ canDragBookmarkButtonToTrash:self])
258 operation |= NSDragOperationDelete;
259
260 return operation;
261 }
262
220 // Overridden to release bar visibility. 263 // Overridden to release bar visibility.
221 - (DraggableButtonResult)endDrag { 264 - (DraggableButtonResult)endDrag {
222 gDraggedButton = nil; 265 gDraggedButton = nil;
223 266
224 // visibilityDelegate_ can be nil if we're detached, and that's fine. 267 // visibilityDelegate_ can be nil if we're detached, and that's fine.
225 [visibilityDelegate_ releaseBarVisibilityForOwner:self 268 [visibilityDelegate_ releaseBarVisibilityForOwner:self
226 withAnimation:YES 269 withAnimation:YES
227 delay:YES]; 270 delay:YES];
228 visibilityDelegate_ = nil; 271 visibilityDelegate_ = nil;
229 272
230 return kDraggableButtonImplUseBase; 273 return kDraggableButtonImplUseBase;
231 } 274 }
232 275
233 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { 276 - (void)draggingSession:(NSDraggingSession*)session
234 NSDragOperation operation = NSDragOperationCopy; 277 endedAtPoint:(NSPoint)aPoint
235 if (isLocal) { 278 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; 279 gDraggedButton = nil;
248 // Inform delegate of drag source that we're finished dragging, 280 // Inform delegate of drag source that we're finished dragging,
249 // so it can close auto-opened bookmark folders etc. 281 // so it can close auto-opened bookmark folders etc.
250 [delegate_ bookmarkDragDidEnd:self 282 [delegate_ bookmarkDragDidEnd:self
251 operation:operation]; 283 operation:operation];
252 // Tell delegate if it should delete us. 284 // Tell delegate if it should delete us.
253 if (operation & NSDragOperationDelete) { 285 if (operation & NSDragOperationDelete) {
254 dragEndScreenLocation_ = aPoint; 286 dragEndScreenLocation_ = aPoint;
255 [delegate_ didDragBookmarkToTrash:self]; 287 [delegate_ didDragBookmarkToTrash:self];
256 } 288 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 break; 350 break;
319 } 351 }
320 default: 352 default:
321 /* Ignore any other kind of event. */ 353 /* Ignore any other kind of event. */
322 break; 354 break;
323 } 355 }
324 } 356 }
325 return kDraggableButtonMixinDidWork; 357 return kDraggableButtonMixinDidWork;
326 } 358 }
327 359
328
329
330 // mouseEntered: and mouseExited: are called from our 360 // mouseEntered: and mouseExited: are called from our
331 // BookmarkButtonCell. We redirect this information to our delegate. 361 // BookmarkButtonCell. We redirect this information to our delegate.
332 // The controller can then perform menu-like actions (e.g. "hover over 362 // The controller can then perform menu-like actions (e.g. "hover over
333 // to open menu"). 363 // to open menu").
334 - (void)mouseEntered:(NSEvent*)event { 364 - (void)mouseEntered:(NSEvent*)event {
335 [delegate_ mouseEnteredButton:self event:event]; 365 [delegate_ mouseEnteredButton:self event:event];
336 } 366 }
337 367
338 // See comments above mouseEntered:. 368 // See comments above mouseEntered:.
339 - (void)mouseExited:(NSEvent*)event { 369 - (void)mouseExited:(NSEvent*)event {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 // ThemedWindowDrawing implementation. 451 // ThemedWindowDrawing implementation.
422 452
423 - (void)windowDidChangeTheme { 453 - (void)windowDidChangeTheme {
424 [self setNeedsDisplay:YES]; 454 [self setNeedsDisplay:YES];
425 } 455 }
426 456
427 - (void)windowDidChangeActive { 457 - (void)windowDidChangeActive {
428 [self setNeedsDisplay:YES]; 458 [self setNeedsDisplay:YES];
429 } 459 }
430 460
431 @end
432
433 @implementation BookmarkButton(Private)
434
435
436 - (void)installCustomTrackingArea { 461 - (void)installCustomTrackingArea {
437 const NSTrackingAreaOptions options = 462 const NSTrackingAreaOptions options =
438 NSTrackingActiveAlways | 463 NSTrackingActiveAlways |
439 NSTrackingMouseEnteredAndExited | 464 NSTrackingMouseEnteredAndExited |
440 NSTrackingEnabledDuringMouseDrag; 465 NSTrackingEnabledDuringMouseDrag;
441 466
442 if (area_) { 467 if (area_) {
443 [self removeTrackingArea:area_]; 468 [self removeTrackingArea:area_];
444 [area_ release]; 469 [area_ release];
445 } 470 }
446 471
447 area_ = [[NSTrackingArea alloc] initWithRect:[self bounds] 472 area_ = [[NSTrackingArea alloc] initWithRect:[self bounds]
448 options:options 473 options:options
449 owner:self 474 owner:self
450 userInfo:nil]; 475 userInfo:nil];
451 [self addTrackingArea:area_]; 476 [self addTrackingArea:area_];
452 } 477 }
453 478
454
455 - (NSImage*)dragImage { 479 - (NSImage*)dragImage {
456 NSRect bounds = [self bounds]; 480 NSRect bounds = [self bounds];
457 base::scoped_nsobject<NSImage> image( 481 base::scoped_nsobject<NSImage> image(
458 [[NSImage alloc] initWithSize:bounds.size]); 482 [[NSImage alloc] initWithSize:bounds.size]);
459 [image lockFocusFlipped:[self isFlipped]]; 483 [image lockFocusFlipped:[self isFlipped]];
460 484
461 NSGraphicsContext* context = [NSGraphicsContext currentContext]; 485 NSGraphicsContext* context = [NSGraphicsContext currentContext];
462 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]); 486 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]);
463 CGContextBeginTransparencyLayer(cgContext, 0); 487 CGContextBeginTransparencyLayer(cgContext, 0);
464 CGContextSetAlpha(cgContext, kDragImageOpacity); 488 CGContextSetAlpha(cgContext, kDragImageOpacity);
465 489
466 GradientButtonCell* cell = 490 GradientButtonCell* cell =
467 base::mac::ObjCCastStrict<GradientButtonCell>([self cell]); 491 base::mac::ObjCCastStrict<GradientButtonCell>([self cell]);
468 [[cell clipPathForFrame:bounds inView:self] setClip]; 492 [[cell clipPathForFrame:bounds inView:self] setClip];
469 [cell drawWithFrame:bounds inView:self]; 493 [cell drawWithFrame:bounds inView:self];
470 494
471 CGContextEndTransparencyLayer(cgContext); 495 CGContextEndTransparencyLayer(cgContext);
472 [image unlockFocus]; 496 [image unlockFocus];
473 497
474 return image.autorelease(); 498 return image.autorelease();
475 } 499 }
476 500
477 @end // @implementation BookmarkButton(Private) 501 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698