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

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

Issue 2729603008: [Mac] Remove bookmark bar NIB (Closed)
Patch Set: CL comments Created 3 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_bar_controller.h" 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #import "base/mac/bundle_locations.h" 9 #import "base/mac/bundle_locations.h"
10 #import "base/mac/foundation_util.h" 10 #import "base/mac/foundation_util.h"
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 } 314 }
315 315
316 - (BookmarkBarToolbarView*)controlledView { 316 - (BookmarkBarToolbarView*)controlledView {
317 return base::mac::ObjCCastStrict<BookmarkBarToolbarView>([self view]); 317 return base::mac::ObjCCastStrict<BookmarkBarToolbarView>([self view]);
318 } 318 }
319 319
320 - (BookmarkContextMenuCocoaController*)menuController { 320 - (BookmarkContextMenuCocoaController*)menuController {
321 return contextMenuController_.get(); 321 return contextMenuController_.get();
322 } 322 }
323 323
324 - (void)loadView {
325 // Height is 0 because this is what the superview expects
326 [self setView:[[[BookmarkBarToolbarView alloc]
327 initWithFrame:NSMakeRect(0, 0, initialWidth_, 0)]
328 autorelease]];
329 [[self view] setHidden:YES];
330 [[self view] setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin];
331 [[self controlledView] setController:self];
332 [[self controlledView] setDelegate:self];
333
334 buttonView_.reset([[BookmarkBarView alloc]
335 initWithController:self
336 frame:NSMakeRect(0, -2, 584, 144)]);
337 [buttonView_ setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin |
338 NSViewMaxXMargin];
339 [[buttonView_ importBookmarksButton] setTarget:self];
340 [[buttonView_ importBookmarksButton] setAction:@selector(importBookmarks:)];
341
342 [self createOffTheSideButton];
343 [buttonView_ addSubview:offTheSideButton_];
344
345 [self.view addSubview:buttonView_];
346 // viewDidLoad became part of the API in 10.10
347 if (!base::mac::IsAtLeastOS10_10())
348 [self viewDidLoad];
349 }
350
324 - (BookmarkButton*)bookmarkButtonToPulseForNode:(const BookmarkNode*)node { 351 - (BookmarkButton*)bookmarkButtonToPulseForNode:(const BookmarkNode*)node {
325 // Find the closest parent that is visible on the bar. 352 // Find the closest parent that is visible on the bar.
326 while (node) { 353 while (node) {
327 // Check if we've reached one of the special buttons. Otherwise, if the next 354 // Check if we've reached one of the special buttons. Otherwise, if the next
328 // parent is the boomark bar, find the corresponding button. 355 // parent is the boomark bar, find the corresponding button.
329 if ([managedBookmarksButton_ bookmarkNode] == node) 356 if ([managedBookmarksButton_ bookmarkNode] == node)
330 return managedBookmarksButton_; 357 return managedBookmarksButton_;
331 358
332 if ([supervisedBookmarksButton_ bookmarkNode] == node) 359 if ([supervisedBookmarksButton_ bookmarkNode] == node)
333 return supervisedBookmarksButton_; 360 return supervisedBookmarksButton_;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 - (void)stopPulsingBookmarkNode { 400 - (void)stopPulsingBookmarkNode {
374 if (!pulsingButton_) 401 if (!pulsingButton_)
375 return; 402 return;
376 403
377 [pulsingButton_ setPulseIsStuckOn:NO]; 404 [pulsingButton_ setPulseIsStuckOn:NO];
378 pulsingButton_.reset(); 405 pulsingButton_.reset();
379 pulsingBookmarkObserver_.reset(); 406 pulsingBookmarkObserver_.reset();
380 } 407 }
381 408
382 - (void)dealloc { 409 - (void)dealloc {
410 [buttonView_ setController:nil];
411 [[self controlledView] setController:nil];
412 [[self controlledView] setDelegate:nil];
383 [self browserWillBeDestroyed]; 413 [self browserWillBeDestroyed];
384 [super dealloc]; 414 [super dealloc];
385 } 415 }
386 416
387 - (void)browserWillBeDestroyed { 417 - (void)browserWillBeDestroyed {
388 // If |bridge_| is null it means -viewDidLoad has not yet been called, which 418 // If |bridge_| is null it means -viewDidLoad has not yet been called, which
389 // can only happen if the nib wasn't loaded. Retrieving it via -[self view] 419 // can only happen if the nib wasn't loaded. Retrieving it via -[self view]
390 // would load it now, but it's too late for that, so let it be nil. Note this 420 // would load it now, but it's too late for that, so let it be nil. Note this
391 // should only happen in tests. 421 // should only happen in tests.
392 BookmarkBarToolbarView* view = nil; 422 BookmarkBarToolbarView* view = nil;
(...skipping 21 matching lines...) Expand all
414 [button setTarget:nil]; 444 [button setTarget:nil];
415 [button setAction:nil]; 445 [button setAction:nil];
416 } 446 }
417 447
418 bridge_.reset(NULL); 448 bridge_.reset(NULL);
419 [[NSNotificationCenter defaultCenter] removeObserver:self]; 449 [[NSNotificationCenter defaultCenter] removeObserver:self];
420 [self watchForExitEvent:NO]; 450 [self watchForExitEvent:NO];
421 browser_ = nullptr; 451 browser_ = nullptr;
422 } 452 }
423 453
424 - (void)awakeFromNib {
425 [self viewDidLoad];
426 }
427
428 - (void)viewDidLoad { 454 - (void)viewDidLoad {
429 if (bridge_) {
430 // When running on 10.10, expect both -awakeFromNib and -viewDidLoad to be
431 // called, but only initialize once.
432 DCHECK(base::mac::IsAtLeastOS10_10());
433 return;
434 }
435
436 // We default to NOT open, which means height=0.
437 DCHECK([[self view] isHidden]); // Hidden so it's OK to change.
438
439 // Set our initial height to zero, since that is what the superview
440 // expects. We will resize ourselves open later if needed.
441 [[self view] setFrame:NSMakeRect(0, 0, initialWidth_, 0)];
442
443 // Complete init of the "off the side" button, as much as we can.
444 [offTheSideButton_ setImage:[self offTheSideButtonImage:NO]];
445 BookmarkButtonCell* offTheSideCell = [offTheSideButton_ cell];
446 [offTheSideCell setTag:kMaterialStandardButtonTypeWithLimitedClickFeedback];
447 [offTheSideCell setImagePosition:NSImageOnly];
448
449 // The cell is configured in the nib to draw a white highlight when clicked.
450 [offTheSideCell setHighlightsBy:NSNoCellMask];
451 [offTheSideButton_.draggableButton setDraggable:NO];
452 [offTheSideButton_.draggableButton setActsOnMouseDown:YES];
453
454 // We are enabled by default. 455 // We are enabled by default.
455 barIsEnabled_ = YES; 456 barIsEnabled_ = YES;
456 457
457 // Remember the original sizes of the 'no items' and 'import bookmarks' 458 // Remember the original sizes of the 'no items' and 'import bookmarks'
458 // fields to aid in resizing when the window frame changes. 459 // fields to aid in resizing when the window frame changes.
459 originalNoItemsRect_ = [[buttonView_ noItemTextfield] frame]; 460 originalNoItemsRect_ = [[buttonView_ noItemTextfield] frame];
460 originalImportBookmarksRect_ = [[buttonView_ importBookmarksButton] frame]; 461 originalImportBookmarksRect_ = [[buttonView_ importBookmarksButton] frame];
461 462
462 // Bookmark buttons start farther from the bookmark bar's left edge so 463 // Bookmark buttons start farther from the bookmark bar's left edge so
463 // adjust the positions of the noItems and importBookmarks textfields. 464 // adjust the positions of the noItems and importBookmarks textfields.
464 const CGFloat kBookmarksTextfieldOffsetX = 14; 465 const CGFloat kBookmarksTextfieldOffsetX = 14;
465 originalNoItemsRect_.origin.x += kBookmarksTextfieldOffsetX; 466 originalNoItemsRect_.origin.x += kBookmarksTextfieldOffsetX;
466 [[buttonView_ noItemTextfield] setFrame:originalNoItemsRect_]; 467 [[buttonView_ noItemTextfield] setFrame:originalNoItemsRect_];
467 468
468 originalImportBookmarksRect_.origin.x += kBookmarksTextfieldOffsetX; 469 originalImportBookmarksRect_.origin.x += kBookmarksTextfieldOffsetX;
469 [[buttonView_ importBookmarksButton] setFrame:originalImportBookmarksRect_]; 470 [[buttonView_ importBookmarksButton] setFrame:originalImportBookmarksRect_];
470 471
471 // Move the chevron button up 2pts from its position in the xib.
472 NSRect chevronButtonFrame = [offTheSideButton_ frame];
473 chevronButtonFrame.origin.y -= 2;
474 [offTheSideButton_ setFrame:chevronButtonFrame];
475
476 // To make life happier when the bookmark bar is floating, the chevron is a
477 // child of the button view.
478 [offTheSideButton_ removeFromSuperview];
479 [buttonView_ addSubview:offTheSideButton_];
480
481 // When resized we may need to add new buttons, or remove them (if 472 // When resized we may need to add new buttons, or remove them (if
482 // no longer visible), or add/remove the "off the side" menu. 473 // no longer visible), or add/remove the "off the side" menu.
483 [[self view] setPostsFrameChangedNotifications:YES]; 474 [[self view] setPostsFrameChangedNotifications:YES];
484 [[NSNotificationCenter defaultCenter] 475 [[NSNotificationCenter defaultCenter]
485 addObserver:self 476 addObserver:self
486 selector:@selector(frameDidChange) 477 selector:@selector(frameDidChange)
487 name:NSViewFrameDidChangeNotification 478 name:NSViewFrameDidChangeNotification
488 object:[self view]]; 479 object:[self view]];
489 480
490 // Watch for things going to or from fullscreen. 481 // Watch for things going to or from fullscreen.
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 DCHECK([sender isKindOfClass:[BookmarkButton class]]); 821 DCHECK([sender isKindOfClass:[BookmarkButton class]]);
831 DCHECK([[sender cell] isKindOfClass:[BookmarkButtonCell class]]); 822 DCHECK([[sender cell] isKindOfClass:[BookmarkButtonCell class]]);
832 823
833 // Only record the action if it's the initial folder being opened. 824 // Only record the action if it's the initial folder being opened.
834 if (!showFolderMenus_) 825 if (!showFolderMenus_)
835 RecordBookmarkFolderOpen([self bookmarkLaunchLocation]); 826 RecordBookmarkFolderOpen([self bookmarkLaunchLocation]);
836 showFolderMenus_ = !showFolderMenus_; 827 showFolderMenus_ = !showFolderMenus_;
837 828
838 // Middle click on chevron should not open bookmarks under it, instead just 829 // Middle click on chevron should not open bookmarks under it, instead just
839 // open its folder menu. 830 // open its folder menu.
840 if (sender == offTheSideButton_) { 831 if (sender == offTheSideButton_.get()) {
841 [[sender cell] setStartingChildIndex:displayedButtonCount_]; 832 [[sender cell] setStartingChildIndex:displayedButtonCount_];
842 NSEvent* event = [NSApp currentEvent]; 833 NSEvent* event = [NSApp currentEvent];
843 if ([event type] == NSOtherMouseUp) { 834 if ([event type] == NSOtherMouseUp) {
844 [self openOrCloseBookmarkFolderForOffTheSideButton]; 835 [self openOrCloseBookmarkFolderForOffTheSideButton];
845 return; 836 return;
846 } 837 }
847 } 838 }
848 // Toggle presentation of bar folder menus. 839 // Toggle presentation of bar folder menus.
849 [folderTarget_ openBookmarkFolderFromButton:sender]; 840 [folderTarget_ openBookmarkFolderFromButton:sender];
850 } 841 }
851 842
852 - (void)openOrCloseBookmarkFolderForOffTheSideButton { 843 - (void)openOrCloseBookmarkFolderForOffTheSideButton {
853 // If clicked on already opened folder, then close it and return. 844 // If clicked on already opened folder, then close it and return.
854 if ([folderController_ parentButton] == offTheSideButton_) 845 if ([folderController_ parentButton] == offTheSideButton_)
855 [self closeBookmarkFolder:self]; 846 [self closeBookmarkFolder:self];
856 else 847 else
857 [self addNewFolderControllerWithParentButton:offTheSideButton_]; 848 [self addNewFolderControllerWithParentButton:offTheSideButton_];
858 } 849 }
859 850
860 // Click on a bookmark folder button. 851 // Click on a bookmark folder button.
861 - (IBAction)openBookmarkFolderFromButton:(id)sender { 852 - (void)openBookmarkFolderFromButton:(id)sender {
862 [self openBookmarkFolder:sender]; 853 [self openBookmarkFolder:sender];
863 } 854 }
864 855
865 // Click on the "off the side" button (chevron), which opens like a folder 856 // Click on the "off the side" button (chevron), which opens like a folder
866 // button but isn't exactly a parent folder. 857 // button but isn't exactly a parent folder.
867 - (IBAction)openOffTheSideFolderFromButton:(id)sender { 858 - (void)openOffTheSideFolderFromButton:(id)sender {
868 [self openBookmarkFolder:sender]; 859 [self openBookmarkFolder:sender];
869 } 860 }
870 861
871 - (IBAction)importBookmarks:(id)sender { 862 - (void)importBookmarks:(id)sender {
872 chrome::ShowImportDialog(browser_); 863 chrome::ShowImportDialog(browser_);
873 } 864 }
874 865
875 - (NSButton*)appsPageShortcutButton { 866 - (NSButton*)appsPageShortcutButton {
876 return appsPageShortcutButton_; 867 return appsPageShortcutButton_;
877 } 868 }
878 869
879 - (NSButton*)offTheSideButton { 870 - (NSButton*)offTheSideButton {
880 return offTheSideButton_; 871 return offTheSideButton_;
881 } 872 }
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 [[appsPageShortcutButton_ draggableButton] setActsOnMouseDown:NO]; 1407 [[appsPageShortcutButton_ draggableButton] setActsOnMouseDown:NO];
1417 [appsPageShortcutButton_ setAction:@selector(openAppsPage:)]; 1408 [appsPageShortcutButton_ setAction:@selector(openAppsPage:)];
1418 NSString* tooltip = 1409 NSString* tooltip =
1419 l10n_util::GetNSString(IDS_BOOKMARK_BAR_APPS_SHORTCUT_TOOLTIP); 1410 l10n_util::GetNSString(IDS_BOOKMARK_BAR_APPS_SHORTCUT_TOOLTIP);
1420 [appsPageShortcutButton_ setToolTip:tooltip]; 1411 [appsPageShortcutButton_ setToolTip:tooltip];
1421 [buttonView_ addSubview:appsPageShortcutButton_.get()]; 1412 [buttonView_ addSubview:appsPageShortcutButton_.get()];
1422 1413
1423 [self setAppsPageShortcutButtonVisibility]; 1414 [self setAppsPageShortcutButtonVisibility];
1424 } 1415 }
1425 1416
1417 - (void)createOffTheSideButton {
1418 offTheSideButton_.reset(
1419 [[BookmarkButton alloc] initWithFrame:NSMakeRect(586, 0, 20, 24)]);
1420 id offTheSideCell = [BookmarkButtonCell offTheSideButtonCell];
1421 [offTheSideCell setTag:kMaterialStandardButtonTypeWithLimitedClickFeedback];
1422 [offTheSideCell setImagePosition:NSImageOnly];
1423
1424 [offTheSideCell setHighlightsBy:NSNoCellMask];
1425 [offTheSideCell setShowsBorderOnlyWhileMouseInside:YES];
1426 [offTheSideCell setBezelStyle:NSShadowlessSquareBezelStyle];
1427 [offTheSideButton_ setCell:offTheSideCell];
1428 [offTheSideButton_ setImage:[self offTheSideButtonImage:NO]];
1429 [offTheSideButton_ setButtonType:NSMomentaryLightButton];
1430
1431 [offTheSideButton_ setTarget:self];
1432 [offTheSideButton_ setAction:@selector(openOffTheSideFolderFromButton:)];
1433 [offTheSideButton_ setDelegate:self];
1434 [[offTheSideButton_ draggableButton] setDraggable:NO];
1435 [[offTheSideButton_ draggableButton] setActsOnMouseDown:YES];
1436 }
1437
1426 - (void)openAppsPage:(id)sender { 1438 - (void)openAppsPage:(id)sender {
1427 WindowOpenDisposition disposition = 1439 WindowOpenDisposition disposition =
1428 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]); 1440 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
1429 [self openURL:GURL(chrome::kChromeUIAppsURL) disposition:disposition]; 1441 [self openURL:GURL(chrome::kChromeUIAppsURL) disposition:disposition];
1430 RecordBookmarkAppsPageOpen([self bookmarkLaunchLocation]); 1442 RecordBookmarkAppsPageOpen([self bookmarkLaunchLocation]);
1431 } 1443 }
1432 1444
1433 // To avoid problems with sync, changes that may impact the current 1445 // To avoid problems with sync, changes that may impact the current
1434 // bookmark (e.g. deletion) make sure context menus are closed. This 1446 // bookmark (e.g. deletion) make sure context menus are closed. This
1435 // prevents deleting a node which no longer exists. 1447 // prevents deleting a node which no longer exists.
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
1947 // If the click is in my window but NOT in the bookmark bar, consider 1959 // If the click is in my window but NOT in the bookmark bar, consider
1948 // it a click 'outside'. Clicks directly on an active button (i.e. one 1960 // it a click 'outside'. Clicks directly on an active button (i.e. one
1949 // that is a folder and for which its folder menu is showing) are 'in'. 1961 // that is a folder and for which its folder menu is showing) are 'in'.
1950 // All other clicks on the bookmarks bar are counted as 'outside' 1962 // All other clicks on the bookmarks bar are counted as 'outside'
1951 // because they should close any open bookmark folder menu. 1963 // because they should close any open bookmark folder menu.
1952 if (eventWindow == myWindow) { 1964 if (eventWindow == myWindow) {
1953 NSView* hitView = 1965 NSView* hitView =
1954 [[eventWindow contentView] hitTest:[event locationInWindow]]; 1966 [[eventWindow contentView] hitTest:[event locationInWindow]];
1955 if (hitView == [folderController_ parentButton]) 1967 if (hitView == [folderController_ parentButton])
1956 return NO; 1968 return NO;
1957 if (![hitView isDescendantOf:[self view]] || hitView == buttonView_) 1969 if (![hitView isDescendantOf:[self view]] ||
1970 hitView == buttonView_.get())
1958 return YES; 1971 return YES;
1959 } 1972 }
1960 // If a click in a bookmark bar folder window and that isn't 1973 // If a click in a bookmark bar folder window and that isn't
1961 // one of my bookmark bar folders, YES is click outside. 1974 // one of my bookmark bar folders, YES is click outside.
1962 if (![eventWindow isKindOfClass:[BookmarkBarFolderWindow 1975 if (![eventWindow isKindOfClass:[BookmarkBarFolderWindow
1963 class]]) { 1976 class]]) {
1964 return YES; 1977 return YES;
1965 } 1978 }
1966 break; 1979 break;
1967 case NSKeyDown: { 1980 case NSKeyDown: {
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after
2940 - (id<BookmarkButtonControllerProtocol>)controllerForNode: 2953 - (id<BookmarkButtonControllerProtocol>)controllerForNode:
2941 (const BookmarkNode*)node { 2954 (const BookmarkNode*)node {
2942 // See if it's in the bar, then if it is in the hierarchy of visible 2955 // See if it's in the bar, then if it is in the hierarchy of visible
2943 // folder menus. 2956 // folder menus.
2944 if (bookmarkModel_->bookmark_bar_node() == node) 2957 if (bookmarkModel_->bookmark_bar_node() == node)
2945 return self; 2958 return self;
2946 return [folderController_ controllerForNode:node]; 2959 return [folderController_ controllerForNode:node];
2947 } 2960 }
2948 2961
2949 @end 2962 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698