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

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

Issue 159776: Rewrites the Mac view resizing logic to have the BrowserWindowController... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 4 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 | Annotate | Revision Log
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 "base/mac_util.h" 5 #include "base/mac_util.h"
6 #include "base/sys_string_conversions.h" 6 #include "base/sys_string_conversions.h"
7 #include "chrome/browser/bookmarks/bookmark_editor.h" 7 #include "chrome/browser/bookmarks/bookmark_editor.h"
8 #include "chrome/browser/bookmarks/bookmark_model.h" 8 #include "chrome/browser/bookmarks/bookmark_model.h"
9 #include "chrome/browser/browser.h" 9 #include "chrome/browser/browser.h"
10 #include "chrome/browser/browser_list.h" 10 #include "chrome/browser/browser_list.h"
11 #import "chrome/browser/cocoa/bookmark_bar_bridge.h" 11 #import "chrome/browser/cocoa/bookmark_bar_bridge.h"
12 #import "chrome/browser/cocoa/bookmark_bar_controller.h" 12 #import "chrome/browser/cocoa/bookmark_bar_controller.h"
13 #import "chrome/browser/cocoa/bookmark_button_cell.h" 13 #import "chrome/browser/cocoa/bookmark_button_cell.h"
14 #import "chrome/browser/cocoa/bookmark_editor_controller.h" 14 #import "chrome/browser/cocoa/bookmark_editor_controller.h"
15 #import "chrome/browser/cocoa/bookmark_name_folder_controller.h" 15 #import "chrome/browser/cocoa/bookmark_name_folder_controller.h"
16 #import "chrome/browser/cocoa/bookmark_menu_cocoa_controller.h" 16 #import "chrome/browser/cocoa/bookmark_menu_cocoa_controller.h"
17 #import "chrome/browser/cocoa/view_resizer.h"
17 #include "chrome/browser/cocoa/nsimage_cache.h" 18 #include "chrome/browser/cocoa/nsimage_cache.h"
18 #include "chrome/browser/profile.h" 19 #include "chrome/browser/profile.h"
19 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
20 #include "chrome/common/pref_service.h" 21 #include "chrome/common/pref_service.h"
21 #include "skia/ext/skia_utils_mac.h" 22 #include "skia/ext/skia_utils_mac.h"
22 23
23 @interface BookmarkBarController(Private) 24 @interface BookmarkBarController(Private)
24 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately; 25 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately;
25 - (void)showBookmarkBar:(BOOL)enable immediately:(BOOL)immediately; 26 - (void)showBookmarkBar:(BOOL)enable immediately:(BOOL)immediately;
26 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu; 27 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu;
27 - (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu; 28 - (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu;
28 - (void)tagEmptyMenu:(NSMenu*)menu; 29 - (void)tagEmptyMenu:(NSMenu*)menu;
29 - (void)clearMenuTagMap; 30 - (void)clearMenuTagMap;
30 @end 31 @end
31 32
32 namespace { 33 namespace {
33 34
34 // TODO(jrg): this is the right proportional height but overlaps the
35 // "blue outline" of the omnibox. Fix.
36
37 // Our height, when opened. 35 // Our height, when opened.
38 const int kBookmarkBarHeight = 30; 36 const int kBookmarkBarHeight = 30;
39 // How much to adjust our parent view.
40 const int kBookmarkBarSuperviewHeightAdjustment = 25;
41 // How much to adjust the web frame.
42 const int kBookmarkBarWebframeHeightAdjustment = 25;
43 37
44 // Magic numbers from Cole 38 // Magic numbers from Cole
45 const CGFloat kDefaultBookmarkWidth = 150.0; 39 const CGFloat kDefaultBookmarkWidth = 150.0;
46 const CGFloat kBookmarkVerticalPadding = 2.0; 40 const CGFloat kBookmarkVerticalPadding = 2.0;
47 const CGFloat kBookmarkHorizontalPadding = 1.0; 41 const CGFloat kBookmarkHorizontalPadding = 1.0;
48 }; 42 };
49 43
50 @implementation BookmarkBarController 44 @implementation BookmarkBarController
51 45
52 - (id)initWithProfile:(Profile*)profile 46 - (id)initWithProfile:(Profile*)profile
53 parentView:(NSView*)parentView 47 initialWidth:(float)initialWidth
54 webContentView:(NSView*)webContentView 48 resizeDelegate:(id<ViewResizer>)resizeDelegate
55 infoBarsView:(NSView*)infoBarsView 49 urlDelegate:(id<BookmarkURLOpener>)urlDelegate {
56 delegate:(id<BookmarkURLOpener>)delegate {
57 if ((self = [super initWithNibName:@"BookmarkBar" 50 if ((self = [super initWithNibName:@"BookmarkBar"
58 bundle:mac_util::MainAppBundle()])) { 51 bundle:mac_util::MainAppBundle()])) {
59 profile_ = profile; 52 profile_ = profile;
53 initialWidth_ = initialWidth;
60 bookmarkModel_ = profile->GetBookmarkModel(); 54 bookmarkModel_ = profile->GetBookmarkModel();
61 parentView_ = parentView;
62 webContentView_ = webContentView;
63 infoBarsView_ = infoBarsView;
64 buttons_.reset([[NSMutableArray alloc] init]); 55 buttons_.reset([[NSMutableArray alloc] init]);
65 delegate_ = delegate; 56 resizeDelegate_ = resizeDelegate;
57 urlDelegate_ = urlDelegate;
66 } 58 }
67 return self; 59 return self;
68 } 60 }
69 61
70 - (void)dealloc { 62 - (void)dealloc {
71 [[NSNotificationCenter defaultCenter] removeObserver:self]; 63 [[NSNotificationCenter defaultCenter] removeObserver:self];
72 [super dealloc]; 64 [super dealloc];
73 } 65 }
74 66
75 - (void)awakeFromNib { 67 - (void)awakeFromNib {
76 // We default to NOT open, which means height=0. 68 // We default to NOT open, which means height=0.
77 DCHECK([[self view] isHidden]); // Hidden so it's OK to change. 69 DCHECK([[self view] isHidden]); // Hidden so it's OK to change.
78 NSRect frame = [[self view] frame];
79 frame.size.height = 0;
80 frame.size.width = [parentView_ frame].size.width;
81 [[self view] setFrame:frame];
82 70
83 // Make sure the nodes stay bottom-aligned. 71 // Set our initial height to zero, since that is what the superview
84 [[self view] setAutoresizingMask:(NSViewWidthSizable | 72 // expects. We will resize ourselves open later if needed.
85 NSViewMinYMargin)]; 73 [[self view] setFrame:NSMakeRect(0, 0, initialWidth_, 0)];
74
86 // Be sure to enable the bar before trying to show it... 75 // Be sure to enable the bar before trying to show it...
87 barIsEnabled_ = YES; 76 barIsEnabled_ = YES;
88 if (profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) 77 if (profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar))
89 [self showBookmarkBar:YES immediately:YES]; 78 [self showBookmarkBar:YES immediately:YES];
90 79
91 // Don't pass ourself along (as 'self') until our init is completely 80 // Don't pass ourself along (as 'self') until our init is completely
92 // done. Thus, this call is (almost) last. 81 // done. Thus, this call is (almost) last.
93 bridge_.reset(new BookmarkBarBridge(self, bookmarkModel_)); 82 bridge_.reset(new BookmarkBarBridge(self, bookmarkModel_));
94 83
95 // When resized we may need to add new buttons, or remove them (if 84 // When resized we may need to add new buttons, or remove them (if
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 } 117 }
129 118
130 // Show or hide the bar based on the value of |show|. Handles 119 // Show or hide the bar based on the value of |show|. Handles
131 // animating the resize of the content view. if |immediately| is YES, 120 // animating the resize of the content view. if |immediately| is YES,
132 // make changes immediately instead of using an animator. If the bar 121 // make changes immediately instead of using an animator. If the bar
133 // is disabled, do absolutely nothing. The routine which enables the 122 // is disabled, do absolutely nothing. The routine which enables the
134 // bar will show it if relevant using other mechanisms (the pref) to 123 // bar will show it if relevant using other mechanisms (the pref) to
135 // determine desired state. 124 // determine desired state.
136 - (void)showBookmarkBar:(BOOL)show immediately:(BOOL)immediately { 125 - (void)showBookmarkBar:(BOOL)show immediately:(BOOL)immediately {
137 if (barIsEnabled_ && (barShouldBeShown_ != show)) { 126 if (barIsEnabled_ && (barShouldBeShown_ != show)) {
138 contentViewHasOffset_ = show; 127 if ([self view]) {
139 [[self view] setHidden:show ? NO : YES]; 128 [[self view] setHidden:show ? NO : YES];
129 [resizeDelegate_ resizeView:[self view]
130 newHeight:(show ? kBookmarkBarHeight : 0)];
131 }
140 barShouldBeShown_ = show; 132 barShouldBeShown_ = show;
141 if (show) { 133 if (show) {
142 [self loaded:bookmarkModel_]; 134 [self loaded:bookmarkModel_];
143 } 135 }
144 [self applyContentAreaOffset:show immediately:immediately];
145 } 136 }
146 } 137 }
147 138
148 // Apply a contents box offset to make (or remove) room for the
149 // bookmark bar. If apply==YES, always make room (the contentView_ is
150 // "full size"). If apply==NO we are trying to undo an offset. If no
151 // offset there is nothing to undo.
152 //
153 // TODO(jrg): it is awkward we change the sizes of views for our
154 // parent and siblings; ideally they change their own sizes.
155 //
156 // TODO(jrg): unlike windows, we process events while an animator is
157 // running. Thus, if you resize the window while the bookmark bar is
158 // animating, you'll mess things up. Fix.
159 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately {
160 if ([self view] == nil) {
161 // We're too early, but awakeFromNib will call me again.
162 return;
163 }
164 if (!contentViewHasOffset_ && apply) {
165 // There is no offset to unconditionally apply.
166 return;
167 }
168
169 // None of these locals are members of the Hall of Justice.
170 NSRect superframe = [parentView_ frame];
171 NSRect frame = [[self view] frame];
172 NSRect webframe = [webContentView_ frame];
173 NSRect infoframe = [infoBarsView_ frame];
174 if (apply) {
175 superframe.size.height += kBookmarkBarSuperviewHeightAdjustment;
176 // TODO(jrg): y=0 if we add the bookmark bar before the parent
177 // view (toolbar) is placed in the view hierarchy. A different
178 // CL, where the bookmark bar is extracted from the toolbar nib,
179 // may fix this awkwardness.
180 if (superframe.origin.y > 0) {
181 superframe.origin.y -= kBookmarkBarSuperviewHeightAdjustment;
182 webframe.size.height -= kBookmarkBarWebframeHeightAdjustment;
183 }
184 frame.size.height += kBookmarkBarHeight;
185 infoframe.origin.y -= kBookmarkBarWebframeHeightAdjustment;
186 } else {
187 superframe.size.height -= kBookmarkBarSuperviewHeightAdjustment;
188 superframe.origin.y += kBookmarkBarSuperviewHeightAdjustment;
189 frame.size.height -= kBookmarkBarHeight;
190 webframe.size.height += kBookmarkBarWebframeHeightAdjustment;
191 infoframe.origin.y += kBookmarkBarWebframeHeightAdjustment;
192 }
193
194 // TODO(jrg): Animators can be a little fussy. Setting these three
195 // off can sometimes causes races where the finish isn't as
196 // expected. Fix, or clean out the animators as an option.
197 // Odd racing is FAR worse than a lack of an animator.
198 if (1 /* immediately */) {
199 [parentView_ setFrame:superframe];
200 [webContentView_ setFrame:webframe];
201 [infoBarsView_ setFrame:infoframe];
202 [[self view] setFrame:frame];
203 } else {
204 [[parentView_ animator] setFrame:superframe];
205 [[webContentView_ animator] setFrame:webframe];
206 [[infoBarsView_ animator] setFrame:infoframe];
207 [[[self view] animator] setFrame:frame];
208 }
209
210 [[self view] setNeedsDisplay:YES];
211 [parentView_ setNeedsDisplay:YES];
212 [webContentView_ setNeedsDisplay:YES];
213 [infoBarsView_ setNeedsDisplay:YES];
214 }
215
216 - (BOOL)isBookmarkBarVisible { 139 - (BOOL)isBookmarkBarVisible {
217 return barShouldBeShown_; 140 return barShouldBeShown_;
218 } 141 }
219 142
220 // We don't change a preference; we only change visibility. 143 // We don't change a preference; we only change visibility.
221 // Preference changing (global state) is handled in 144 // Preference changing (global state) is handled in
222 // BrowserWindowCocoa::ToggleBookmarkBar(). 145 // BrowserWindowCocoa::ToggleBookmarkBar().
223 - (void)toggleBookmarkBar { 146 - (void)toggleBookmarkBar {
224 [self showBookmarkBar:!barShouldBeShown_ immediately:YES]; 147 [self showBookmarkBar:!barShouldBeShown_ immediately:YES];
225 } 148 }
(...skipping 26 matching lines...) Expand all
252 - (BookmarkNode*)nodeFromButton:(id)button { 175 - (BookmarkNode*)nodeFromButton:(id)button {
253 NSCell* cell = [button cell]; 176 NSCell* cell = [button cell];
254 BookmarkNode* node = static_cast<BookmarkNode*>( 177 BookmarkNode* node = static_cast<BookmarkNode*>(
255 [[cell representedObject] pointerValue]); 178 [[cell representedObject] pointerValue]);
256 DCHECK(node); 179 DCHECK(node);
257 return node; 180 return node;
258 } 181 }
259 182
260 - (IBAction)openBookmark:(id)sender { 183 - (IBAction)openBookmark:(id)sender {
261 BookmarkNode* node = [self nodeFromButton:sender]; 184 BookmarkNode* node = [self nodeFromButton:sender];
262 [delegate_ openBookmarkURL:node->GetURL() disposition:CURRENT_TAB]; 185 [urlDelegate_ openBookmarkURL:node->GetURL() disposition:CURRENT_TAB];
263 } 186 }
264 187
265 // Given a NSMenuItem tag, return the appropriate bookmark node id. 188 // Given a NSMenuItem tag, return the appropriate bookmark node id.
266 - (int64)nodeIdFromMenuTag:(int32)tag { 189 - (int64)nodeIdFromMenuTag:(int32)tag {
267 return menuTagMap_[tag]; 190 return menuTagMap_[tag];
268 } 191 }
269 192
270 // Create and return a new tag for the given node id. 193 // Create and return a new tag for the given node id.
271 - (int32)menuTagFromNodeId:(int64)menuid { 194 - (int32)menuTagFromNodeId:(int64)menuid {
272 int tag = seedId_++; 195 int tag = seedId_++;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 withEvent:[NSApp currentEvent] 298 withEvent:[NSApp currentEvent]
376 forView:sender]; 299 forView:sender];
377 } 300 }
378 301
379 // As a convention we set the menu's delegate to be the button's cell 302 // As a convention we set the menu's delegate to be the button's cell
380 // so we can easily obtain bookmark info. Convention applied in 303 // so we can easily obtain bookmark info. Convention applied in
381 // -[BookmarkButtonCell menu]. 304 // -[BookmarkButtonCell menu].
382 305
383 - (IBAction)openBookmarkInNewForegroundTab:(id)sender { 306 - (IBAction)openBookmarkInNewForegroundTab:(id)sender {
384 BookmarkNode* node = [self nodeFromMenuItem:sender]; 307 BookmarkNode* node = [self nodeFromMenuItem:sender];
385 [delegate_ openBookmarkURL:node->GetURL() disposition:NEW_FOREGROUND_TAB]; 308 [urlDelegate_ openBookmarkURL:node->GetURL() disposition:NEW_FOREGROUND_TAB];
386 } 309 }
387 310
388 - (IBAction)openBookmarkInNewWindow:(id)sender { 311 - (IBAction)openBookmarkInNewWindow:(id)sender {
389 BookmarkNode* node = [self nodeFromMenuItem:sender]; 312 BookmarkNode* node = [self nodeFromMenuItem:sender];
390 [delegate_ openBookmarkURL:node->GetURL() disposition:NEW_WINDOW]; 313 [urlDelegate_ openBookmarkURL:node->GetURL() disposition:NEW_WINDOW];
391 } 314 }
392 315
393 - (IBAction)openBookmarkInIncognitoWindow:(id)sender { 316 - (IBAction)openBookmarkInIncognitoWindow:(id)sender {
394 BookmarkNode* node = [self nodeFromMenuItem:sender]; 317 BookmarkNode* node = [self nodeFromMenuItem:sender];
395 [delegate_ openBookmarkURL:node->GetURL() disposition:OFF_THE_RECORD]; 318 [urlDelegate_ openBookmarkURL:node->GetURL() disposition:OFF_THE_RECORD];
396 } 319 }
397 320
398 - (IBAction)editBookmark:(id)sender { 321 - (IBAction)editBookmark:(id)sender {
399 BookmarkNode* node = [self nodeFromMenuItem:sender]; 322 BookmarkNode* node = [self nodeFromMenuItem:sender];
400 323
401 // TODO(jrg): on windows, folder "buttons" use the bar's context 324 // TODO(jrg): on windows, folder "buttons" use the bar's context
402 // menu (but with extra items enabled, like Rename). For now we do 325 // menu (but with extra items enabled, like Rename). For now we do
403 // a cheat and redirect so we have the functionality available. 326 // a cheat and redirect so we have the functionality available.
404 if (node->is_folder()) { 327 if (node->is_folder()) {
405 [self addOrRenameFolder:sender]; 328 [self addOrRenameFolder:sender];
(...skipping 18 matching lines...) Expand all
424 - (IBAction)deleteBookmark:(id)sender { 347 - (IBAction)deleteBookmark:(id)sender {
425 BookmarkNode* node = [self nodeFromMenuItem:sender]; 348 BookmarkNode* node = [self nodeFromMenuItem:sender];
426 bookmarkModel_->Remove(node->GetParent(), 349 bookmarkModel_->Remove(node->GetParent(),
427 node->GetParent()->IndexOfChild(node)); 350 node->GetParent()->IndexOfChild(node));
428 } 351 }
429 352
430 - (void)openBookmarkNodesRecursive:(BookmarkNode*)node { 353 - (void)openBookmarkNodesRecursive:(BookmarkNode*)node {
431 for (int i = 0; i < node->GetChildCount(); i++) { 354 for (int i = 0; i < node->GetChildCount(); i++) {
432 BookmarkNode* child = node->GetChild(i); 355 BookmarkNode* child = node->GetChild(i);
433 if (child->is_url()) 356 if (child->is_url())
434 [delegate_ openBookmarkURL:child->GetURL() 357 [urlDelegate_ openBookmarkURL:child->GetURL()
435 disposition:NEW_BACKGROUND_TAB]; 358 disposition:NEW_BACKGROUND_TAB];
436 else 359 else
437 [self openBookmarkNodesRecursive:child]; 360 [self openBookmarkNodesRecursive:child];
438 } 361 }
439 } 362 }
440 363
441 - (IBAction)openAllBookmarks:(id)sender { 364 - (IBAction)openAllBookmarks:(id)sender {
442 // TODO(jrg): 365 // TODO(jrg):
443 // Is there an easier way to get a non-const root node for the bookmark bar? 366 // Is there an easier way to get a non-const root node for the bookmark bar?
444 // I can't iterate over them unless it's non-const. 367 // I can't iterate over them unless it's non-const.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 each_frame.origin.x += delta; 488 each_frame.origin.x += delta;
566 [each_button setFrame:each_frame]; 489 [each_button setFrame:each_frame];
567 } 490 }
568 } 491 }
569 } 492 }
570 } 493 }
571 494
572 - (IBAction)openBookmarkMenuItem:(id)sender { 495 - (IBAction)openBookmarkMenuItem:(id)sender {
573 int64 tag = [self nodeIdFromMenuTag:[sender tag]]; 496 int64 tag = [self nodeIdFromMenuTag:[sender tag]];
574 const BookmarkNode* node = bookmarkModel_->GetNodeByID(tag); 497 const BookmarkNode* node = bookmarkModel_->GetNodeByID(tag);
575 [delegate_ openBookmarkURL:node->GetURL() disposition:CURRENT_TAB]; 498 [urlDelegate_ openBookmarkURL:node->GetURL() disposition:CURRENT_TAB];
576 } 499 }
577 500
578 // Add all items from the given model to our bookmark bar. 501 // Add all items from the given model to our bookmark bar.
579 // TODO(jrg): lots of things! 502 // TODO(jrg): lots of things!
580 // - bookmark folders (e.g. menu from the button) 503 // - bookmark folders (e.g. menu from the button)
581 // - button and menu on the right for when bookmarks don't all fit on the 504 // - button and menu on the right for when bookmarks don't all fit on the
582 // screen 505 // screen
583 // - ... 506 // - ...
584 // 507 //
585 // TODO(jrg): write a "build bar" so there is a nice spot for things 508 // TODO(jrg): write a "build bar" so there is a nice spot for things
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 } 616 }
694 } 617 }
695 } 618 }
696 619
697 // TODO(jrg): for now this is brute force. 620 // TODO(jrg): for now this is brute force.
698 - (void)nodeChildrenReordered:(BookmarkModel*)model 621 - (void)nodeChildrenReordered:(BookmarkModel*)model
699 node:(const BookmarkNode*)node { 622 node:(const BookmarkNode*)node {
700 [self loaded:model]; 623 [self loaded:model];
701 } 624 }
702 625
703 - (void)setDelegate:(id<BookmarkURLOpener>)delegate { 626 - (void)setUrlDelegate:(id<BookmarkURLOpener>)urlDelegate {
704 delegate_ = delegate; 627 urlDelegate_ = urlDelegate;
705 } 628 }
706 629
707 - (NSArray*)buttons { 630 - (NSArray*)buttons {
708 return buttons_.get(); 631 return buttons_.get();
709 } 632 }
710 633
711 @end 634 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/bookmark_bar_controller.h ('k') | chrome/browser/cocoa/bookmark_bar_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698