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

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

Issue 339087: Mac: correct favicons in bookmarks bar (for pages w/o one and for subfolders). (Closed)
Patch Set: Updated per Nico's review and merged ToT. Created 11 years, 1 month 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
« no previous file with comments | « chrome/browser/cocoa/bookmark_bar_controller.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #include "app/l10n_util_mac.h" 5 #include "app/l10n_util_mac.h"
6 #include "app/resource_bundle.h" 6 #include "app/resource_bundle.h"
7 #include "base/mac_util.h" 7 #include "base/mac_util.h"
8 #include "base/sys_string_conversions.h" 8 #include "base/sys_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_editor.h" 9 #include "chrome/browser/bookmarks/bookmark_editor.h"
10 #include "chrome/browser/bookmarks/bookmark_model.h" 10 #include "chrome/browser/bookmarks/bookmark_model.h"
(...skipping 10 matching lines...) Expand all
21 #import "chrome/browser/cocoa/bookmark_menu_cocoa_controller.h" 21 #import "chrome/browser/cocoa/bookmark_menu_cocoa_controller.h"
22 #import "chrome/browser/cocoa/event_utils.h" 22 #import "chrome/browser/cocoa/event_utils.h"
23 #import "chrome/browser/cocoa/menu_button.h" 23 #import "chrome/browser/cocoa/menu_button.h"
24 #import "chrome/browser/cocoa/toolbar_controller.h" 24 #import "chrome/browser/cocoa/toolbar_controller.h"
25 #import "chrome/browser/cocoa/view_resizer.h" 25 #import "chrome/browser/cocoa/view_resizer.h"
26 #include "chrome/browser/profile.h" 26 #include "chrome/browser/profile.h"
27 #include "chrome/browser/tab_contents/tab_contents.h" 27 #include "chrome/browser/tab_contents/tab_contents.h"
28 #include "chrome/browser/tab_contents/tab_contents_view.h" 28 #include "chrome/browser/tab_contents/tab_contents_view.h"
29 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/pref_service.h" 30 #include "chrome/common/pref_service.h"
31 #include "grit/app_resources.h"
31 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
32 #include "grit/theme_resources.h" 33 #include "grit/theme_resources.h"
33 #include "skia/ext/skia_utils_mac.h" 34 #include "skia/ext/skia_utils_mac.h"
34 #import "third_party/mozilla/include/NSPasteboard+Utils.h" 35 #import "third_party/mozilla/include/NSPasteboard+Utils.h"
35 36
36 // Specialization of NSButton that responds to middle-clicks. By default, 37 // Specialization of NSButton that responds to middle-clicks. By default,
37 // NSButton ignores them. 38 // NSButton ignores them.
38 @interface BookmarkButton : NSButton 39 @interface BookmarkButton : NSButton
39 @end 40 @end
40 41
41 @implementation BookmarkButton 42 @implementation BookmarkButton
42 - (void)otherMouseUp:(NSEvent*) event { 43 - (void)otherMouseUp:(NSEvent*) event {
43 [self performClick:self]; 44 [self performClick:self];
44 } 45 }
45 @end 46 @end
46 47
47 @interface BookmarkBarController(Private) 48 @interface BookmarkBarController(Private)
48 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately; 49 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately;
49 - (void)showBookmarkBar:(BOOL)enable immediately:(BOOL)immediately; 50 - (void)showBookmarkBar:(BOOL)enable immediately:(BOOL)immediately;
50 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu; 51 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu;
51 - (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu; 52 - (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu;
52 - (void)tagEmptyMenu:(NSMenu*)menu; 53 - (void)tagEmptyMenu:(NSMenu*)menu;
53 - (void)clearMenuTagMap; 54 - (void)clearMenuTagMap;
54 - (int)preferredHeight; 55 - (int)preferredHeight;
55 - (void)addNonBookmarkButtonsToView; 56 - (void)addNonBookmarkButtonsToView;
56 - (void)addButtonsToView; 57 - (void)addButtonsToView;
57 - (void)resizeButtons; 58 - (void)resizeButtons;
58 - (void)centerNoItemsLabel; 59 - (void)centerNoItemsLabel;
60 - (NSImage*)getFavIconForNode:(const BookmarkNode*)node;
pink (ping after 24hrs) 2009/11/03 19:42:44 the normal obj-c method naming conventions avoid t
59 @end 61 @end
60 62
61 @implementation BookmarkBarController 63 @implementation BookmarkBarController
62 64
63 - (id)initWithBrowser:(Browser*)browser 65 - (id)initWithBrowser:(Browser*)browser
64 initialWidth:(float)initialWidth 66 initialWidth:(float)initialWidth
65 compressDelegate:(id<ToolbarCompressable>)compressDelegate 67 compressDelegate:(id<ToolbarCompressable>)compressDelegate
66 resizeDelegate:(id<ViewResizer>)resizeDelegate 68 resizeDelegate:(id<ViewResizer>)resizeDelegate
67 urlDelegate:(id<BookmarkURLOpener>)urlDelegate { 69 urlDelegate:(id<BookmarkURLOpener>)urlDelegate {
68 if ((self = [super initWithNibName:@"BookmarkBar" 70 if ((self = [super initWithNibName:@"BookmarkBar"
69 bundle:mac_util::MainAppBundle()])) { 71 bundle:mac_util::MainAppBundle()])) {
70 browser_ = browser; 72 browser_ = browser;
71 initialWidth_ = initialWidth; 73 initialWidth_ = initialWidth;
72 bookmarkModel_ = browser_->profile()->GetBookmarkModel(); 74 bookmarkModel_ = browser_->profile()->GetBookmarkModel();
73 buttons_.reset([[NSMutableArray alloc] init]); 75 buttons_.reset([[NSMutableArray alloc] init]);
74 compressDelegate_ = compressDelegate; 76 compressDelegate_ = compressDelegate;
75 resizeDelegate_ = resizeDelegate; 77 resizeDelegate_ = resizeDelegate;
76 urlDelegate_ = urlDelegate; 78 urlDelegate_ = urlDelegate;
77 tabObserver_.reset( 79 tabObserver_.reset(
78 new TabStripModelObserverBridge(browser_->tabstrip_model(), self)); 80 new TabStripModelObserverBridge(browser_->tabstrip_model(), self));
79 81
80 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 82 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
81 folderImage_.reset([rb.GetNSImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain]); 83 folderImage_.reset([rb.GetNSImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain]);
84 defaultImage_.reset([rb.GetNSImageNamed(IDR_DEFAULT_FAVICON) retain]);
82 } 85 }
83 return self; 86 return self;
84 } 87 }
85 88
86 - (void)dealloc { 89 - (void)dealloc {
87 // Remove our view from its superview so it doesn't attempt to reference 90 // Remove our view from its superview so it doesn't attempt to reference
88 // it when the controller is gone. 91 // it when the controller is gone.
89 //TODO(dmaclach): Remove -- http://crbug.com/25845 92 //TODO(dmaclach): Remove -- http://crbug.com/25845
90 [[self view] removeFromSuperview]; 93 [[self view] removeFromSuperview];
91 94
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 } 353 }
351 354
352 // Recursively add the given bookmark node and all its children to 355 // Recursively add the given bookmark node and all its children to
353 // menu, one menu item per node. 356 // menu, one menu item per node.
354 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu { 357 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu {
355 NSString* title = [BookmarkMenuCocoaController menuTitleForNode:child]; 358 NSString* title = [BookmarkMenuCocoaController menuTitleForNode:child];
356 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title 359 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title
357 action:nil 360 action:nil
358 keyEquivalent:@""] autorelease]; 361 keyEquivalent:@""] autorelease];
359 [menu addItem:item]; 362 [menu addItem:item];
363 [item setImage:[self getFavIconForNode:child]];
360 if (child->is_folder()) { 364 if (child->is_folder()) {
361 NSMenu* submenu = [[[NSMenu alloc] initWithTitle:title] autorelease]; 365 NSMenu* submenu = [[[NSMenu alloc] initWithTitle:title] autorelease];
362 [menu setSubmenu:submenu forItem:item]; 366 [menu setSubmenu:submenu forItem:item];
363 if (child->GetChildCount()) { 367 if (child->GetChildCount()) {
364 [self addFolderNode:child toMenu:submenu]; // potentially recursive 368 [self addFolderNode:child toMenu:submenu]; // potentially recursive
365 } else { 369 } else {
366 [self tagEmptyMenu:submenu]; 370 [self tagEmptyMenu:submenu];
367 } 371 }
368 } else { 372 } else {
369 [item setTarget:self]; 373 [item setTarget:self];
370 [item setAction:@selector(openBookmarkMenuItem:)]; 374 [item setAction:@selector(openBookmarkMenuItem:)];
371 [item setTag:[self menuTagFromNodeId:child->id()]]; 375 [item setTag:[self menuTagFromNodeId:child->id()]];
372 // Add a tooltip 376 // Add a tooltip
373 std::string url_string = child->GetURL().possibly_invalid_spec(); 377 std::string url_string = child->GetURL().possibly_invalid_spec();
374 NSString* tooltip = [NSString stringWithFormat:@"%@\n%s", 378 NSString* tooltip = [NSString stringWithFormat:@"%@\n%s",
375 base::SysWideToNSString(child->GetTitle()), 379 base::SysWideToNSString(child->GetTitle()),
376 url_string.c_str()]; 380 url_string.c_str()];
377 [item setToolTip:tooltip]; 381 [item setToolTip:tooltip];
378 const SkBitmap& favicon = bookmarkModel_->GetFavIcon(child);
379 if (!favicon.isNull()) {
380 NSImage* image = gfx::SkBitmapToNSImage(favicon);
381 if (image) {
382 [item setImage:image];
383 }
384 }
385 } 382 }
386 } 383 }
387 384
388 // Empty menus are odd; if empty, add something to look at. 385 // Empty menus are odd; if empty, add something to look at.
389 // Matches windows behavior. 386 // Matches windows behavior.
390 - (void)tagEmptyMenu:(NSMenu*)menu { 387 - (void)tagEmptyMenu:(NSMenu*)menu {
391 NSString* empty_menu_title = l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU); 388 NSString* empty_menu_title = l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU);
392 [menu addItem:[[[NSMenuItem alloc] initWithTitle:empty_menu_title 389 [menu addItem:[[[NSMenuItem alloc] initWithTitle:empty_menu_title
393 action:NULL 390 action:NULL
394 keyEquivalent:@""] autorelease]]; 391 keyEquivalent:@""] autorelease]];
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 585
589 // Return an autoreleased NSCell suitable for a bookmark button. 586 // Return an autoreleased NSCell suitable for a bookmark button.
590 // TODO(jrg): move much of the cell config into the BookmarkButtonCell class. 587 // TODO(jrg): move much of the cell config into the BookmarkButtonCell class.
591 - (NSCell*)cellForBookmarkNode:(const BookmarkNode*)node { 588 - (NSCell*)cellForBookmarkNode:(const BookmarkNode*)node {
592 NSString* title = base::SysWideToNSString(node->GetTitle()); 589 NSString* title = base::SysWideToNSString(node->GetTitle());
593 NSButtonCell *cell = [[[BookmarkButtonCell alloc] initTextCell:nil] 590 NSButtonCell *cell = [[[BookmarkButtonCell alloc] initTextCell:nil]
594 autorelease]; 591 autorelease];
595 DCHECK(cell); 592 DCHECK(cell);
596 [cell setRepresentedObject:[NSValue valueWithPointer:node]]; 593 [cell setRepresentedObject:[NSValue valueWithPointer:node]];
597 594
598 NSImage* image = NULL; 595 [cell setImage:[self getFavIconForNode:node]];
599 if (node->is_folder()) { 596 [cell setImagePosition:NSImageLeft];
600 image = folderImage_;
601 } else {
602 const SkBitmap& favicon = bookmarkModel_->GetFavIcon(node);
603 if (!favicon.isNull()) {
604 image = gfx::SkBitmapToNSImage(favicon);
605 }
606 }
607 if (image) {
608 [cell setImage:image];
609 [cell setImagePosition:NSImageLeft];
610 }
611 [cell setTitle:title]; 597 [cell setTitle:title];
612 [cell setMenu:buttonContextMenu_]; 598 [cell setMenu:buttonContextMenu_];
613 return cell; 599 return cell;
614 } 600 }
615 601
616 // Return an appropriate width for the given bookmark button cell. 602 // Return an appropriate width for the given bookmark button cell.
617 // The "+2" is needed because, sometimes, Cocoa is off by a tad. 603 // The "+2" is needed because, sometimes, Cocoa is off by a tad.
618 // Example: for a bookmark named "Moma" or "SFGate", it is one pixel 604 // Example: for a bookmark named "Moma" or "SFGate", it is one pixel
619 // too small. For "FBL" it is 2 pixels too small. 605 // too small. For "FBL" it is 2 pixels too small.
620 // For a bookmark named "SFGateFooWoo", it is just fine. 606 // For a bookmark named "SFGateFooWoo", it is just fine.
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
866 // this looks bad I may need to change widthForBookmarkButtonCell to 852 // this looks bad I may need to change widthForBookmarkButtonCell to
867 // add space for an image even if not there on the assumption that 853 // add space for an image even if not there on the assumption that
868 // favicons will eventually load. 854 // favicons will eventually load.
869 - (void)nodeFavIconLoaded:(BookmarkModel*)model 855 - (void)nodeFavIconLoaded:(BookmarkModel*)model
870 node:(const BookmarkNode*)node { 856 node:(const BookmarkNode*)node {
871 for (NSButton* button in buttons_.get()) { 857 for (NSButton* button in buttons_.get()) {
872 NSButtonCell* cell = [button cell]; 858 NSButtonCell* cell = [button cell];
873 void* pointer = [[cell representedObject] pointerValue]; 859 void* pointer = [[cell representedObject] pointerValue];
874 const BookmarkNode* cellnode = static_cast<const BookmarkNode*>(pointer); 860 const BookmarkNode* cellnode = static_cast<const BookmarkNode*>(pointer);
875 if (cellnode == node) { 861 if (cellnode == node) {
876 NSImage* image = gfx::SkBitmapToNSImage(bookmarkModel_->GetFavIcon(node)); 862 [cell setImage:[self getFavIconForNode:node]];
877 if (image) { 863 [cell setImagePosition:NSImageLeft];
878 [cell setImage:image]; 864 // Adding an image means we might need more room for the
879 [cell setImagePosition:NSImageLeft]; 865 // bookmark. Test for it by growing the button (if needed)
880 // Adding an image means we might need more room for the 866 // and shifting everything else over.
881 // bookmark. Test for it by growing the button (if needed) 867 [self checkForBookmarkButtonGrowth:button];
882 // and shifting everything else over.
883 [self checkForBookmarkButtonGrowth:button];
884 }
885 return;
886 } 868 }
887 } 869 }
888 } 870 }
889 871
890 // TODO(jrg): for now this is brute force. 872 // TODO(jrg): for now this is brute force.
891 - (void)nodeChildrenReordered:(BookmarkModel*)model 873 - (void)nodeChildrenReordered:(BookmarkModel*)model
892 node:(const BookmarkNode*)node { 874 node:(const BookmarkNode*)node {
893 [self loaded:model]; 875 [self loaded:model];
894 } 876 }
895 877
896 - (void)setUrlDelegate:(id<BookmarkURLOpener>)urlDelegate { 878 - (void)setUrlDelegate:(id<BookmarkURLOpener>)urlDelegate {
897 urlDelegate_ = urlDelegate; 879 urlDelegate_ = urlDelegate;
898 } 880 }
899 881
900 - (NSArray*)buttons { 882 - (NSArray*)buttons {
901 return buttons_.get(); 883 return buttons_.get();
902 } 884 }
903 885
904 - (NSButton*)offTheSideButton { 886 - (NSButton*)offTheSideButton {
905 return offTheSideButton_; 887 return offTheSideButton_;
906 } 888 }
907 889
908 - (NSButton*)otherBookmarksButton { 890 - (NSButton*)otherBookmarksButton {
909 return otherBookmarksButton_.get(); 891 return otherBookmarksButton_.get();
910 } 892 }
911 893
894 - (NSImage*)getFavIconForNode:(const BookmarkNode*)node {
895 if (node->is_folder())
896 return folderImage_;
897
898 const SkBitmap& favIcon = bookmarkModel_->GetFavIcon(node);
899 if (!favIcon.isNull())
900 return gfx::SkBitmapToNSImage(favIcon);
901
902 return defaultImage_;
903 }
904
912 @end 905 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/bookmark_bar_controller.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698