| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_view_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view_cocoa.h" |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #import "chrome/browser/themes/theme_properties.h" | 8 #import "chrome/browser/themes/theme_properties.h" |
| 9 #import "chrome/browser/themes/theme_service.h" | 9 #import "chrome/browser/themes/theme_service.h" |
| 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" | 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" |
| 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
| 12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_context_menu_cocoa_controlle
r.h" | 12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_context_menu_cocoa_controlle
r.h" |
| 13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" | 13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" |
| 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 15 #import "chrome/browser/ui/cocoa/themed_window.h" | 15 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 16 #import "chrome/browser/ui/cocoa/view_id_util.h" | 16 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| 17 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/bookmarks/browser/bookmark_pasteboard_helper_mac.h" | 18 #include "components/bookmarks/browser/bookmark_pasteboard_helper_mac.h" |
| 18 #include "components/bookmarks/browser/bookmark_utils.h" | 19 #include "components/bookmarks/browser/bookmark_utils.h" |
| 19 #include "content/public/browser/user_metrics.h" | 20 #include "content/public/browser/user_metrics.h" |
| 20 #import "third_party/mozilla/NSPasteboard+Utils.h" | 21 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 21 #include "ui/base/clipboard/clipboard_util_mac.h" | 22 #include "ui/base/clipboard/clipboard_util_mac.h" |
| 23 #import "ui/base/cocoa/controls/hyperlink_button_cell.h" |
| 22 #import "ui/base/cocoa/nsview_additions.h" | 24 #import "ui/base/cocoa/nsview_additions.h" |
| 25 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 | 26 |
| 24 using base::UserMetricsAction; | 27 using base::UserMetricsAction; |
| 25 using bookmarks::BookmarkModel; | 28 using bookmarks::BookmarkModel; |
| 26 using bookmarks::BookmarkNode; | 29 using bookmarks::BookmarkNode; |
| 27 | 30 |
| 31 static const CGFloat kInitialContainerWidth = 596; |
| 32 static const CGFloat kInitialContainerHeight = 41; |
| 33 static const CGFloat kInitialElementYOrigin = 20; |
| 34 static const CGFloat kInitialElementHeight = 14; |
| 35 static const CGFloat kInitialTextFieldXOrigin = 5; |
| 36 // static const CGFloat kInitialTextFieldWidth = 167; |
| 37 static const CGFloat kTextFieldTrailingPadding = 5; |
| 38 // static const CGFloat kInitialButtonWidth = 199; |
| 39 |
| 28 @interface BookmarkBarView (Private) | 40 @interface BookmarkBarView (Private) |
| 29 - (void)themeDidChangeNotification:(NSNotification*)aNotification; | 41 - (void)themeDidChangeNotification:(NSNotification*)aNotification; |
| 30 - (void)updateTheme:(const ui::ThemeProvider*)themeProvider; | 42 - (void)updateTheme:(const ui::ThemeProvider*)themeProvider; |
| 31 | 43 |
| 32 // NSView override. | 44 // NSView override. |
| 33 - (void)setFrameSize:(NSSize)size; | 45 - (void)setFrameSize:(NSSize)size; |
| 34 @end | 46 @end |
| 35 | 47 |
| 36 @implementation BookmarkBarView | 48 @implementation BookmarkBarView |
| 37 | 49 |
| 38 @synthesize dropIndicatorShown = dropIndicatorShown_; | 50 @synthesize dropIndicatorShown = dropIndicatorShown_; |
| 39 @synthesize dropIndicatorPosition = dropIndicatorPosition_; | 51 @synthesize dropIndicatorPosition = dropIndicatorPosition_; |
| 40 @synthesize noItemContainer = noItemContainer_; | 52 @synthesize controller = controller_; |
| 41 | 53 |
| 42 - (void)setFrameSize:(NSSize)size { | 54 - (void)setFrameSize:(NSSize)size { |
| 43 NSSize oldFrameSize = [self frame].size; | 55 NSSize oldFrameSize = [self frame].size; |
| 44 [super setFrameSize:size]; | 56 [super setFrameSize:size]; |
| 45 // Any time the size of the bookmark bar view changes, the bookmark bar view | 57 // Any time the size of the bookmark bar view changes, the bookmark bar view |
| 46 // buttons needs to be redrawn. | 58 // buttons needs to be redrawn. |
| 47 // https://code.google.com/p/chromium/issues/detail?id=521025#c7 | 59 // https://code.google.com/p/chromium/issues/detail?id=521025#c7 |
| 48 if (!NSEqualSizes(oldFrameSize, size)) | 60 if (!NSEqualSizes(oldFrameSize, size)) |
| 49 [self cr_recursivelySetNeedsDisplay:YES]; | 61 [self cr_recursivelySetNeedsDisplay:YES]; |
| 50 } | 62 } |
| 51 | 63 |
| 52 - (void)dealloc { | 64 - (void)dealloc { |
| 53 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 65 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 54 // This probably isn't strictly necessary, but can't hurt. | 66 // This probably isn't strictly necessary, but can't hurt. |
| 55 [self unregisterDraggedTypes]; | 67 [self unregisterDraggedTypes]; |
| 56 [super dealloc]; | 68 [super dealloc]; |
| 57 | 69 |
| 58 // To be clear, our controller_ is an IBOutlet and owns us, so we | 70 // To be clear, our controller_ owns us, so we on't deallocate it explicitly. |
| 59 // don't deallocate it explicitly. It is owned by the browser | 71 // It is owned by the browser window controller, so gets deleted with a |
| 60 // window controller, so gets deleted with a browser window is | 72 // browser window is closed. |
| 61 // closed. | |
| 62 } | 73 } |
| 63 | 74 |
| 64 - (void)awakeFromNib { | 75 - (instancetype)initWithController:(BookmarkBarController*)controller |
| 76 frame:(NSRect)frame { |
| 77 DCHECK(controller) << "Controller shouldn't be nil"; |
| 78 if (self = [super initWithFrame:frame]) { |
| 79 controller_ = controller; |
| 80 |
| 81 NSFont* smallSystemFont = |
| 82 [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; |
| 83 noItemContainer_.reset( |
| 84 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, kInitialContainerWidth, |
| 85 kInitialContainerHeight)]); |
| 86 [noItemContainer_ setAutoresizingMask:NSViewMaxXMargin]; |
| 87 |
| 88 noItemTextfield_.reset([[NSTextField alloc] |
| 89 initWithFrame:NSMakeRect(kInitialTextFieldXOrigin, |
| 90 kInitialElementYOrigin, CGFLOAT_MAX, |
| 91 kInitialElementHeight)]); |
| 92 [noItemTextfield_ setAutoresizingMask:NSViewWidthSizable]; |
| 93 [noItemTextfield_ setFont:smallSystemFont]; |
| 94 [noItemTextfield_ |
| 95 setStringValue:l10n_util::GetNSString(IDS_BOOKMARKS_NO_ITEMS)]; |
| 96 |
| 97 [noItemTextfield_ setBordered:NO]; |
| 98 [[noItemTextfield_ cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 99 |
| 100 [noItemTextfield_ setTextColor:[NSColor controlTextColor]]; |
| 101 [noItemTextfield_ setBackgroundColor:[NSColor controlColor]]; |
| 102 |
| 103 [noItemTextfield_ setDrawsBackground:NO]; |
| 104 [noItemTextfield_ setTextColor:[NSColor controlTextColor]]; |
| 105 [noItemTextfield_ setBackgroundColor:[NSColor controlColor]]; |
| 106 [noItemTextfield_ sizeToFit]; |
| 107 |
| 108 NSButton* importButton = [HyperlinkButtonCell |
| 109 buttonWithString:l10n_util::GetNSString(IDS_BOOKMARK_BAR_IMPORT_LINK)]; |
| 110 importBookmarksButton_.reset([importButton retain]); |
| 111 [importBookmarksButton_ |
| 112 setFrame:NSMakeRect(NSMaxX([noItemTextfield_ frame]) + |
| 113 kTextFieldTrailingPadding, |
| 114 kInitialElementYOrigin, CGFLOAT_MAX, |
| 115 kInitialElementHeight)]; |
| 116 [importBookmarksButton_ setAutoresizingMask:NSViewMaxXMargin]; |
| 117 [importBookmarksButton_ setFont:smallSystemFont]; |
| 118 [importBookmarksButton_ sizeToFit]; |
| 119 [noItemContainer_ addSubview:importBookmarksButton_]; |
| 120 |
| 121 [noItemContainer_ addSubview:noItemTextfield_]; |
| 122 |
| 123 [self addSubview:noItemContainer_]; |
| 124 [self registerForNotificationsAndDraggedTypes]; |
| 125 } |
| 126 return self; |
| 127 } |
| 128 |
| 129 - (void)registerForNotificationsAndDraggedTypes { |
| 65 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; | 130 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; |
| 66 [defaultCenter addObserver:self | 131 [defaultCenter addObserver:self |
| 67 selector:@selector(themeDidChangeNotification:) | 132 selector:@selector(themeDidChangeNotification:) |
| 68 name:kBrowserThemeDidChangeNotification | 133 name:kBrowserThemeDidChangeNotification |
| 69 object:nil]; | 134 object:nil]; |
| 70 | 135 |
| 71 DCHECK(controller_) << "Expected this to be hooked up via Interface Builder"; | |
| 72 NSArray* types = @[ | 136 NSArray* types = @[ |
| 73 NSStringPboardType, NSHTMLPboardType, NSURLPboardType, | 137 NSStringPboardType, NSHTMLPboardType, NSURLPboardType, |
| 74 ui::ClipboardUtil::UTIForPasteboardType(kBookmarkButtonDragType), | 138 ui::ClipboardUtil::UTIForPasteboardType(kBookmarkButtonDragType), |
| 75 ui::ClipboardUtil::UTIForPasteboardType(kBookmarkDictionaryListPboardType) | 139 ui::ClipboardUtil::UTIForPasteboardType(kBookmarkDictionaryListPboardType) |
| 76 ]; | 140 ]; |
| 77 [self registerForDraggedTypes:types]; | 141 [self registerForDraggedTypes:types]; |
| 78 } | 142 } |
| 79 | 143 |
| 80 // We need the theme to color the bookmark buttons properly. But our | 144 // We need the theme to color the bookmark buttons properly. But our |
| 81 // controller desn't have access to it until it's placed in the view | 145 // controller desn't have access to it until it's placed in the view |
| (...skipping 28 matching lines...) Expand all Loading... |
| 110 // Mouse down events on the bookmark bar should not allow dragging the parent | 174 // Mouse down events on the bookmark bar should not allow dragging the parent |
| 111 // window around. | 175 // window around. |
| 112 - (BOOL)mouseDownCanMoveWindow { | 176 - (BOOL)mouseDownCanMoveWindow { |
| 113 return NO; | 177 return NO; |
| 114 } | 178 } |
| 115 | 179 |
| 116 - (NSTextField*)noItemTextfield { | 180 - (NSTextField*)noItemTextfield { |
| 117 return noItemTextfield_; | 181 return noItemTextfield_; |
| 118 } | 182 } |
| 119 | 183 |
| 184 - (NSView*)noItemContainer { |
| 185 return noItemContainer_; |
| 186 } |
| 187 |
| 120 - (NSButton*)importBookmarksButton { | 188 - (NSButton*)importBookmarksButton { |
| 121 return importBookmarksButton_; | 189 return importBookmarksButton_; |
| 122 } | 190 } |
| 123 | 191 |
| 124 - (BookmarkBarController*)controller { | |
| 125 return controller_; | |
| 126 } | |
| 127 | |
| 128 // Internal method, needs to be called whenever a change has been made to | 192 // Internal method, needs to be called whenever a change has been made to |
| 129 // dropIndicatorShown_ or dropIndicatorPosition_ so it can get the controller | 193 // dropIndicatorShown_ or dropIndicatorPosition_ so it can get the controller |
| 130 // to reflect the change by moving buttons around. | 194 // to reflect the change by moving buttons around. |
| 131 - (void)dropIndicatorChanged { | 195 - (void)dropIndicatorChanged { |
| 132 if (dropIndicatorShown_) | 196 if (dropIndicatorShown_) |
| 133 [controller_ setDropInsertionPos:dropIndicatorPosition_]; | 197 [controller_ setDropInsertionPos:dropIndicatorPosition_]; |
| 134 else | 198 else |
| 135 [controller_ clearDropInsertionPos]; | 199 [controller_ clearDropInsertionPos]; |
| 136 } | 200 } |
| 137 | 201 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 if ([self performDragOperationForURL:info]) | 338 if ([self performDragOperationForURL:info]) |
| 275 return YES; | 339 return YES; |
| 276 } | 340 } |
| 277 return NO; | 341 return NO; |
| 278 } | 342 } |
| 279 | 343 |
| 280 - (NSMenu*)menu { | 344 - (NSMenu*)menu { |
| 281 return [[controller_ menuController] menuForBookmarkBar]; | 345 return [[controller_ menuController] menuForBookmarkBar]; |
| 282 } | 346 } |
| 283 | 347 |
| 284 - (void)setController:(id)controller { | |
| 285 controller_ = controller; | |
| 286 } | |
| 287 | |
| 288 - (ViewID)viewID { | 348 - (ViewID)viewID { |
| 289 return VIEW_ID_BOOKMARK_BAR; | 349 return VIEW_ID_BOOKMARK_BAR; |
| 290 } | 350 } |
| 291 | 351 |
| 292 @end // @implementation BookmarkBarView | 352 @end // @implementation BookmarkBarView |
| OLD | NEW |