| 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 #import "chrome/browser/cocoa/download_shelf_controller.h" | 5 #import "chrome/browser/cocoa/download_shelf_controller.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/mac_util.h" | 8 #include "base/mac_util.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "chrome/browser/browser.h" | 10 #include "chrome/browser/browser.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // Horizontal padding between two download items. | 33 // Horizontal padding between two download items. |
| 34 const int kDownloadItemPadding = 10; | 34 const int kDownloadItemPadding = 10; |
| 35 | 35 |
| 36 // Duration for the open-new-leftmost-item animation, in seconds. | 36 // Duration for the open-new-leftmost-item animation, in seconds. |
| 37 const NSTimeInterval kDownloadItemOpenDuration = 0.8; | 37 const NSTimeInterval kDownloadItemOpenDuration = 0.8; |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 @interface DownloadShelfController(Private) | 41 @interface DownloadShelfController(Private) |
| 42 - (void)applyContentAreaOffset:(BOOL)apply; | 42 - (void)applyContentAreaOffset:(BOOL)apply; |
| 43 - (void)positionBar; | |
| 44 - (void)showDownloadShelf:(BOOL)enable; | 43 - (void)showDownloadShelf:(BOOL)enable; |
| 45 - (void)resizeDownloadLinkToFit; | 44 - (void)resizeDownloadLinkToFit; |
| 46 @end | 45 @end |
| 47 | 46 |
| 48 | 47 |
| 49 @implementation DownloadShelfController | 48 @implementation DownloadShelfController |
| 50 | 49 |
| 51 - (id)initWithBrowser:(Browser*)browser | 50 - (id)initWithBrowser:(Browser*)browser |
| 52 contentArea:(NSView*)content { | 51 resizeDelegate:(id<ViewResizer>)resizeDelegate { |
| 53 if ((self = [super initWithNibName:@"DownloadShelf" | 52 if ((self = [super initWithNibName:@"DownloadShelf" |
| 54 bundle:mac_util::MainAppBundle()])) { | 53 bundle:mac_util::MainAppBundle()])) { |
| 55 contentArea_ = content; | 54 resizeDelegate_ = resizeDelegate; |
| 56 shelfHeight_ = [[self view] bounds].size.height; | 55 shelfHeight_ = [[self view] bounds].size.height; |
| 57 | 56 |
| 58 [self positionBar]; | 57 // Reset the download shelf's frame to zero. It will be properly positioned |
| 59 [[[contentArea_ window] contentView] addSubview:[self view]]; | 58 // and sized the first time we try to set its height. |
| 59 [[self view] setFrame:NSZeroRect]; |
| 60 | 60 |
| 61 downloadItemControllers_.reset([[NSMutableArray alloc] init]); | 61 downloadItemControllers_.reset([[NSMutableArray alloc] init]); |
| 62 | 62 |
| 63 // This calls show:, so it needs to be last. | 63 // This calls show:, so it needs to be last. |
| 64 bridge_.reset(new DownloadShelfMac(browser, self)); | 64 bridge_.reset(new DownloadShelfMac(browser, self)); |
| 65 } | 65 } |
| 66 return self; | 66 return self; |
| 67 } | 67 } |
| 68 | 68 |
| 69 - (void)awakeFromNib { | 69 - (void)awakeFromNib { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 [itemContainerView_ setNeedsDisplay:YES]; | 129 [itemContainerView_ setNeedsDisplay:YES]; |
| 130 } | 130 } |
| 131 | 131 |
| 132 - (BOOL)textView:(NSTextView *)aTextView | 132 - (BOOL)textView:(NSTextView *)aTextView |
| 133 clickedOnLink:(id)link | 133 clickedOnLink:(id)link |
| 134 atIndex:(NSUInteger)charIndex { | 134 atIndex:(NSUInteger)charIndex { |
| 135 bridge_->browser()->ShowDownloadsTab(); | 135 bridge_->browser()->ShowDownloadsTab(); |
| 136 return YES; | 136 return YES; |
| 137 } | 137 } |
| 138 | 138 |
| 139 // Initializes the download shelf at the bottom edge of |contentArea_|. | |
| 140 - (void)positionBar { | |
| 141 // Set the bar's height to zero and position it at the bottom of the content | |
| 142 // area, within the window's content view (as opposed to the tab strip, which | |
| 143 // is a sibling). We'll enlarge it and slide the content area up when we need | |
| 144 // to show this strip. | |
| 145 NSRect contentFrame = [contentArea_ frame]; | |
| 146 NSRect barFrame = NSMakeRect(0, 0, contentFrame.size.width, shelfHeight_); | |
| 147 [[self view] setFrame:barFrame]; | |
| 148 } | |
| 149 | |
| 150 // Called when the contentArea's frame changes. Enlarge the view to stay with | |
| 151 // the bottom of the contentArea. | |
| 152 - (void)resizeDownloadShelf { | |
| 153 NSRect barFrame = [[self view] frame]; | |
| 154 barFrame.origin.y = 0; | |
| 155 barFrame.size.height = NSMinY([contentArea_ frame]); | |
| 156 [[self view] setFrame:barFrame]; | |
| 157 } | |
| 158 | |
| 159 - (void)remove:(DownloadItemController*)download { | 139 - (void)remove:(DownloadItemController*)download { |
| 160 // Look for the download in our controller array and remove it. This will | 140 // Look for the download in our controller array and remove it. This will |
| 161 // explicity release it so that it removes itself as an Observer of the | 141 // explicity release it so that it removes itself as an Observer of the |
| 162 // DownloadItem. We don't want to wait for autorelease since the DownloadItem | 142 // DownloadItem. We don't want to wait for autorelease since the DownloadItem |
| 163 // we are observing will likely be gone by then. | 143 // we are observing will likely be gone by then. |
| 164 [[NSNotificationCenter defaultCenter] removeObserver:download]; | 144 [[NSNotificationCenter defaultCenter] removeObserver:download]; |
| 165 [[download view] removeFromSuperview]; | 145 [[download view] removeFromSuperview]; |
| 166 [downloadItemControllers_ removeObject:download]; | 146 [downloadItemControllers_ removeObject:download]; |
| 167 | 147 |
| 168 // TODO(thakis): Need to relayout the remaining item views here ( | 148 // TODO(thakis): Need to relayout the remaining item views here ( |
| 169 // crbug.com/17831 ). | 149 // crbug.com/17831 ). |
| 170 | 150 |
| 171 // Check to see if we have any downloads remaining and if not, hide the shelf. | 151 // Check to see if we have any downloads remaining and if not, hide the shelf. |
| 172 if (![downloadItemControllers_ count]) | 152 if (![downloadItemControllers_ count]) |
| 173 [self showDownloadShelf:NO]; | 153 [self showDownloadShelf:NO]; |
| 174 } | 154 } |
| 175 | 155 |
| 176 // We need to explicitly release our download controllers here since they need | 156 // We need to explicitly release our download controllers here since they need |
| 177 // to remove themselves as observers before the remaining shutdown happens. | 157 // to remove themselves as observers before the remaining shutdown happens. |
| 178 - (void)exiting { | 158 - (void)exiting { |
| 179 downloadItemControllers_.reset(); | 159 downloadItemControllers_.reset(); |
| 180 } | 160 } |
| 181 | 161 |
| 182 // Show or hide the bar based on the value of |enable|. Handles animating the | 162 // Show or hide the bar based on the value of |enable|. Handles animating the |
| 183 // resize of the content view. | 163 // resize of the content view. |
| 184 - (void)showDownloadShelf:(BOOL)enable { | 164 - (void)showDownloadShelf:(BOOL)enable { |
| 185 if ([self isVisible] == enable) | 165 if ([self isVisible] == enable) |
| 186 return; | 166 return; |
| 187 | 167 |
| 188 contentAreaHasOffset_ = enable; | 168 [resizeDelegate_ resizeView:[self view] |
| 189 [[self view] setHidden:enable ? NO : YES]; | 169 newHeight:(enable ? shelfHeight_ : 0)]; |
| 190 [self applyContentAreaOffset:enable]; | |
| 191 | |
| 192 barIsVisible_ = enable; | 170 barIsVisible_ = enable; |
| 193 } | 171 } |
| 194 | 172 |
| 195 // Apply a contents box offset to make (or remove) room for the download shelf. | |
| 196 // If apply is YES, always make room (the contentView_ is "full size"). If apply | |
| 197 // is NO, we are trying to undo an offset. If no offset there is nothing to undo
. | |
| 198 - (void)applyContentAreaOffset:(BOOL)apply { | |
| 199 if (!contentAreaHasOffset_ && apply) { | |
| 200 // There is no offset to unconditionally apply. | |
| 201 return; | |
| 202 } | |
| 203 | |
| 204 NSRect frame = [contentArea_ frame]; | |
| 205 if (apply) { | |
| 206 frame.origin.y += shelfHeight_; | |
| 207 frame.size.height -= shelfHeight_; | |
| 208 } else { | |
| 209 frame.origin.y -= shelfHeight_; | |
| 210 frame.size.height += shelfHeight_; | |
| 211 } | |
| 212 | |
| 213 [[contentArea_ animator] setFrame:frame]; | |
| 214 [[self view] setNeedsDisplay:YES]; | |
| 215 [contentArea_ setNeedsDisplay:YES]; | |
| 216 } | |
| 217 | |
| 218 - (DownloadShelf*)bridge { | 173 - (DownloadShelf*)bridge { |
| 219 return bridge_.get(); | 174 return bridge_.get(); |
| 220 } | 175 } |
| 221 | 176 |
| 222 - (BOOL)isVisible { | 177 - (BOOL)isVisible { |
| 223 return barIsVisible_; | 178 return barIsVisible_; |
| 224 } | 179 } |
| 225 | 180 |
| 226 - (void)show:(id)sender { | 181 - (void)show:(id)sender { |
| 227 [self showDownloadShelf:YES]; | 182 [self showDownloadShelf:YES]; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 DCHECK(kMaxDownloadItemCount > 0); | 244 DCHECK(kMaxDownloadItemCount > 0); |
| 290 | 245 |
| 291 // Since no user will ever see the item being removed (needs a horizontal | 246 // Since no user will ever see the item being removed (needs a horizontal |
| 292 // screen resolution greater than 3200 at 16 items at 200 pixels each), | 247 // screen resolution greater than 3200 at 16 items at 200 pixels each), |
| 293 // there's no point in animating the removal. | 248 // there's no point in animating the removal. |
| 294 [self remove:[downloadItemControllers_ objectAtIndex:0]]; | 249 [self remove:[downloadItemControllers_ objectAtIndex:0]]; |
| 295 } | 250 } |
| 296 } | 251 } |
| 297 | 252 |
| 298 @end | 253 @end |
| OLD | NEW |