OLD | NEW |
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/sys_string_conversions.h" | 5 #include "base/sys_string_conversions.h" |
6 #include "chrome/browser/bookmarks/bookmark_model.h" | 6 #include "chrome/browser/bookmarks/bookmark_model.h" |
7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
8 #include "chrome/browser/browser_list.h" | 8 #include "chrome/browser/browser_list.h" |
9 #import "chrome/browser/cocoa/bookmark_bar_bridge.h" | 9 #import "chrome/browser/cocoa/bookmark_bar_bridge.h" |
10 #import "chrome/browser/cocoa/bookmark_bar_controller.h" | 10 #import "chrome/browser/cocoa/bookmark_bar_controller.h" |
11 #import "chrome/browser/cocoa/bookmark_bar_view.h" | 11 #import "chrome/browser/cocoa/bookmark_bar_view.h" |
12 #import "chrome/browser/cocoa/bookmark_button_cell.h" | 12 #import "chrome/browser/cocoa/bookmark_button_cell.h" |
13 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/common/pref_service.h" | 15 #include "chrome/common/pref_service.h" |
16 #include "skia/ext/skia_utils_mac.h" | 16 #include "skia/ext/skia_utils_mac.h" |
17 | 17 |
18 @interface BookmarkBarController(Private) | 18 @interface BookmarkBarController(Private) |
19 - (void)applyContentAreaOffset:(BOOL)apply; | 19 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately; |
20 - (void)positionBar; | 20 - (void)positionBar; |
21 - (void)showBookmarkBar:(BOOL)enable; | 21 - (void)showBookmarkBar:(BOOL)enable immediately:(BOOL)immediately; |
22 @end | 22 @end |
23 | 23 |
24 namespace { | 24 namespace { |
25 const int kBookmarkBarHeight = 30; | 25 const int kBookmarkBarHeight = 30; |
26 // Magic numbers from Cole | 26 // Magic numbers from Cole |
27 const CGFloat kDefaultBookmarkWidth = 150.0; | 27 const CGFloat kDefaultBookmarkWidth = 150.0; |
28 const CGFloat kBookmarkVerticalPadding = 2.0; | 28 const CGFloat kBookmarkVerticalPadding = 2.0; |
29 const CGFloat kBookmarkHorizontalPadding = 8.0; | 29 const CGFloat kBookmarkHorizontalPadding = 8.0; |
30 }; | 30 }; |
31 | 31 |
32 @implementation BookmarkBarController | 32 @implementation BookmarkBarController |
33 | 33 |
34 - (id)initWithProfile:(Profile*)profile | 34 - (id)initWithProfile:(Profile*)profile |
35 contentView:(NSView*)content | 35 contentView:(NSView*)content |
36 delegate:(id<BookmarkURLOpener>)delegate { | 36 delegate:(id<BookmarkURLOpener>)delegate { |
37 if ((self = [super init])) { | 37 if ((self = [super init])) { |
38 bookmarkModel_ = profile->GetBookmarkModel(); | 38 bookmarkModel_ = profile->GetBookmarkModel(); |
39 contentView_ = content; | 39 contentView_ = content; |
40 delegate_ = delegate; | 40 delegate_ = delegate; |
| 41 // Be sure to enable the bar before trying to show it... |
| 42 barIsEnabled_ = YES; |
41 // Create and initialize the view's position. Show it if appropriate. | 43 // Create and initialize the view's position. Show it if appropriate. |
42 // TODO(jrg): put it in a nib if necessary. | 44 // TODO(jrg): put it in a nib if necessary. |
43 NSRect frame = NSMakeRect(0, 0, 0, 30); | 45 NSRect frame = NSMakeRect(0, 0, 0, 30); |
44 bookmarkBarView_ = [[BookmarkBarView alloc] initWithFrame:frame]; | 46 bookmarkBarView_ = [[BookmarkBarView alloc] initWithFrame:frame]; |
45 [self positionBar]; | 47 [self positionBar]; |
46 if (profile->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) | 48 preferences_ = profile->GetPrefs(); |
47 [self showBookmarkBar:YES]; | 49 if (preferences_->GetBoolean(prefs::kShowBookmarkBar)) |
| 50 [self showBookmarkBar:YES immediately:NO]; |
48 [[[contentView_ window] contentView] addSubview:bookmarkBarView_]; | 51 [[[contentView_ window] contentView] addSubview:bookmarkBarView_]; |
49 } | 52 } |
50 // Don't pass ourself along until our init is done. | 53 // Don't pass ourself along until our init is done. |
51 // Thus, this call is (almost) last. | 54 // Thus, this call is (almost) last. |
52 bridge_.reset(new BookmarkBarBridge(self, bookmarkModel_)); | 55 bridge_.reset(new BookmarkBarBridge(self, bookmarkModel_)); |
53 return self; | 56 return self; |
54 } | 57 } |
55 | 58 |
56 - (void)dealloc { | 59 - (void)dealloc { |
57 [bookmarkBarView_ release]; | 60 [bookmarkBarView_ release]; |
58 [super dealloc]; | 61 [super dealloc]; |
59 } | 62 } |
60 | 63 |
61 // Initializes the bookmark bar at the top edge of |contentArea_| and the | 64 // Initializes the bookmark bar at the top edge of |contentArea_| and the |
62 // view's visibility to match the pref. This doesn't move the content view at | 65 // view's visibility to match the pref. This doesn't move the content view at |
63 // all, you need to call |-showBookmarkBar:| to do that. | 66 // all, you need to call |-showBookmarkBar:| to do that. |
64 - (void)positionBar { | 67 - (void)positionBar { |
65 // Hide or show bar based on initial visibility and set the resize flags. | 68 // Hide or show bar based on initial visibility and set the resize flags. |
66 [bookmarkBarView_ setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin]; | 69 [bookmarkBarView_ setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin]; |
67 [bookmarkBarView_ setHidden:!barIsVisible_]; | 70 [bookmarkBarView_ setHidden:!barShouldBeShown_]; |
68 | 71 |
69 // Set the bar's height to zero and position it at the top of the | 72 // Set the bar's height to zero and position it at the top of the |
70 // content area, within the window's content view (as opposed to the | 73 // content area, within the window's content view (as opposed to the |
71 // tab strip, which is a sibling). We'll enlarge it and slide the | 74 // tab strip, which is a sibling). We'll enlarge it and slide the |
72 // content area down when we need to show this strip. | 75 // content area down when we need to show this strip. |
73 NSRect contentFrame = [contentView_ frame]; | 76 NSRect contentFrame = [contentView_ frame]; |
74 NSRect barFrame = NSMakeRect(0, NSMaxY(contentFrame), | 77 NSRect barFrame = NSMakeRect(0, NSMaxY(contentFrame), |
75 contentFrame.size.width, 0); | 78 contentFrame.size.width, 0); |
76 [bookmarkBarView_ setFrame:barFrame]; | 79 [bookmarkBarView_ setFrame:barFrame]; |
77 } | 80 } |
78 | 81 |
79 // Called when the contentView's frame changes. Enlarge the view to | 82 // Called when the contentView's frame changes. Enlarge the view to |
80 // stay with the top of the contentView. | 83 // stay with the top of the contentView. |
81 - (void)resizeBookmarkBar { | 84 - (void)resizeBookmarkBar { |
82 NSRect barFrame = [bookmarkBarView_ frame]; | 85 NSRect barFrame = [bookmarkBarView_ frame]; |
83 const int maxY = NSMaxY(barFrame); | 86 const int maxY = NSMaxY(barFrame); |
84 barFrame.origin.y = NSMaxY([contentView_ frame]); | 87 barFrame.origin.y = NSMaxY([contentView_ frame]); |
85 barFrame.size.height = maxY - barFrame.origin.y; | 88 barFrame.size.height = maxY - barFrame.origin.y; |
86 [bookmarkBarView_ setFrame:barFrame]; | 89 [bookmarkBarView_ setFrame:barFrame]; |
87 } | 90 } |
88 | 91 |
89 // Show or hide the bar based on the value of |enable|. Handles animating the | 92 // Show or hide the bar based on the value of |show|. Handles |
90 // resize of the content view. | 93 // animating the resize of the content view. if |immediately| is YES, |
91 - (void)showBookmarkBar:(BOOL)enable { | 94 // make changes immediately instead of using an animator. If the bar |
92 contentViewHasOffset_ = enable; | 95 // is disabled, do absolutely nothing. The routine which enables the |
93 [bookmarkBarView_ setHidden:enable ? NO : YES]; | 96 // bar will show it if relevant using other mechanisms (the pref) to |
94 [self applyContentAreaOffset:enable]; | 97 // determine desired state. |
95 | 98 - (void)showBookmarkBar:(BOOL)show immediately:(BOOL)immediately { |
96 barIsVisible_ = enable; | 99 if (barIsEnabled_) { |
97 if (enable) { | 100 contentViewHasOffset_ = show; |
98 [self loaded:bookmarkModel_]; | 101 [bookmarkBarView_ setHidden:show ? NO : YES]; |
| 102 [self applyContentAreaOffset:show immediately:immediately]; |
| 103 barShouldBeShown_ = show; |
| 104 if (show) { |
| 105 [self loaded:bookmarkModel_]; |
| 106 } |
99 } | 107 } |
100 } | 108 } |
101 | 109 |
102 // Apply a contents box offset to make (or remove) room for the | 110 // Apply a contents box offset to make (or remove) room for the |
103 // bookmark bar. If apply==YES, always make room (the contentView_ is | 111 // bookmark bar. If apply==YES, always make room (the contentView_ is |
104 // "full size"). If apply==NO we are trying to undo an offset. If no | 112 // "full size"). If apply==NO we are trying to undo an offset. If no |
105 // offset there is nothing to undo. | 113 // offset there is nothing to undo. |
106 - (void)applyContentAreaOffset:(BOOL)apply { | 114 - (void)applyContentAreaOffset:(BOOL)apply immediately:(BOOL)immediately { |
107 if (bookmarkBarView_ == nil) { | 115 if (bookmarkBarView_ == nil) { |
108 // We're too early, but awakeFromNib will call me again. | 116 // We're too early, but awakeFromNib will call me again. |
109 return; | 117 return; |
110 } | 118 } |
111 if (!contentViewHasOffset_ && apply) { | 119 if (!contentViewHasOffset_ && apply) { |
112 // There is no offset to unconditionally apply. | 120 // There is no offset to unconditionally apply. |
113 return; | 121 return; |
114 } | 122 } |
115 | 123 |
116 NSRect frame = [contentView_ frame]; | 124 NSRect frame = [contentView_ frame]; |
117 if (apply) | 125 if (apply) |
118 frame.size.height -= kBookmarkBarHeight; | 126 frame.size.height -= kBookmarkBarHeight; |
119 else | 127 else |
120 frame.size.height += kBookmarkBarHeight; | 128 frame.size.height += kBookmarkBarHeight; |
121 | 129 |
122 [[contentView_ animator] setFrame:frame]; | 130 if (immediately) { |
| 131 [contentView_ setFrame:frame]; |
| 132 [contentView_ setNeedsDisplay:YES]; |
| 133 } else { |
| 134 [[contentView_ animator] setFrame:frame]; |
| 135 } |
| 136 |
123 [bookmarkBarView_ setNeedsDisplay:YES]; | 137 [bookmarkBarView_ setNeedsDisplay:YES]; |
124 [contentView_ setNeedsDisplay:YES]; | 138 [contentView_ setNeedsDisplay:YES]; |
125 } | 139 } |
126 | 140 |
127 - (BOOL)isBookmarkBarVisible { | 141 - (BOOL)isBookmarkBarVisible { |
128 return barIsVisible_; | 142 return barShouldBeShown_; |
129 } | 143 } |
130 | 144 |
131 // We don't change a preference; we only change visibility. | 145 // We don't change a preference; we only change visibility. |
132 // Preference changing (global state) is handled in | 146 // Preference changing (global state) is handled in |
133 // BrowserWindowCocoa::ToggleBookmarkBar(). | 147 // BrowserWindowCocoa::ToggleBookmarkBar(). |
134 - (void)toggleBookmarkBar { | 148 - (void)toggleBookmarkBar { |
135 [self showBookmarkBar:!barIsVisible_]; | 149 [self showBookmarkBar:!barShouldBeShown_ immediately:NO]; |
| 150 } |
| 151 |
| 152 - (void)setBookmarkBarEnabled:(BOOL)enabled { |
| 153 if (enabled) { |
| 154 // Enabling the bar; set enabled then show if needed. |
| 155 barIsEnabled_ = YES; |
| 156 if (preferences_->GetBoolean(prefs::kShowBookmarkBar)) |
| 157 [self showBookmarkBar:YES immediately:YES]; |
| 158 } else { |
| 159 // Disabling the bar; hide if visible. |
| 160 if ([self isBookmarkBarVisible]) { |
| 161 [self showBookmarkBar:NO immediately:YES]; |
| 162 } |
| 163 barIsEnabled_ = NO; |
| 164 } |
136 } | 165 } |
137 | 166 |
138 // Delete all items from the bookmark bar. | 167 // Delete all items from the bookmark bar. |
139 - (void)clearBookmarkBar { | 168 - (void)clearBookmarkBar { |
140 [bookmarkBarView_ setSubviews:[NSArray array]]; | 169 [bookmarkBarView_ setSubviews:[NSArray array]]; |
141 } | 170 } |
142 | 171 |
143 // TODO(jrg): add an openBookmarkInBackground() for ctrl-click which | 172 // TODO(jrg): add an openBookmarkInBackground() for ctrl-click which |
144 // has a different disposition. | 173 // has a different disposition. |
145 - (void)openBookmark:(id)sender { | 174 - (void)openBookmark:(id)sender { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 [button setToolTip:tooltip]; | 272 [button setToolTip:tooltip]; |
244 } | 273 } |
245 // Finally, add it to the bookmark bar. | 274 // Finally, add it to the bookmark bar. |
246 [bookmarkBarView_ addSubview:button]; | 275 [bookmarkBarView_ addSubview:button]; |
247 } | 276 } |
248 } | 277 } |
249 | 278 |
250 // TODO(jrg): for now this is brute force. | 279 // TODO(jrg): for now this is brute force. |
251 - (void)loaded:(BookmarkModel*)model { | 280 - (void)loaded:(BookmarkModel*)model { |
252 DCHECK(model == bookmarkModel_); | 281 DCHECK(model == bookmarkModel_); |
253 // Do nothing if not visible or too early | 282 // Do nothing if not active or too early |
254 if ((barIsVisible_ == NO) || !model->IsLoaded()) | 283 if ((barShouldBeShown_ == NO) || !model->IsLoaded()) |
255 return; | 284 return; |
256 // Else brute force nuke and build. | 285 // Else brute force nuke and build. |
257 const BookmarkNode* node = model->GetBookmarkBarNode(); | 286 const BookmarkNode* node = model->GetBookmarkBarNode(); |
258 [self clearBookmarkBar]; | 287 [self clearBookmarkBar]; |
259 [self addNodesToBar:node]; | 288 [self addNodesToBar:node]; |
260 } | 289 } |
261 | 290 |
262 - (void)beingDeleted:(BookmarkModel*)model { | 291 - (void)beingDeleted:(BookmarkModel*)model { |
263 [self clearBookmarkBar]; | 292 [self clearBookmarkBar]; |
264 } | 293 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 - (void)nodeChildrenReordered:(BookmarkModel*)model | 335 - (void)nodeChildrenReordered:(BookmarkModel*)model |
307 node:(const BookmarkNode*)node { | 336 node:(const BookmarkNode*)node { |
308 [self loaded:model]; | 337 [self loaded:model]; |
309 } | 338 } |
310 | 339 |
311 - (NSView*)view { | 340 - (NSView*)view { |
312 return bookmarkBarView_; | 341 return bookmarkBarView_; |
313 } | 342 } |
314 | 343 |
315 @end | 344 @end |
OLD | NEW |