| 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/extensions/browser_actions_container_view.h" | 5 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #import "chrome/browser/ui/cocoa/l10n_util.h" | |
| 11 #import "chrome/browser/ui/cocoa/view_id_util.h" | 10 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| 12 #include "ui/base/cocoa/appkit_utils.h" | 11 #include "ui/base/cocoa/appkit_utils.h" |
| 13 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" | 12 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 14 | 13 |
| 15 NSString* const kBrowserActionGrippyDragStartedNotification = | 14 NSString* const kBrowserActionGrippyDragStartedNotification = |
| 16 @"BrowserActionGrippyDragStartedNotification"; | 15 @"BrowserActionGrippyDragStartedNotification"; |
| 17 NSString* const kBrowserActionGrippyDraggingNotification = | 16 NSString* const kBrowserActionGrippyDraggingNotification = |
| 18 @"BrowserActionGrippyDraggingNotification"; | 17 @"BrowserActionGrippyDraggingNotification"; |
| 19 NSString* const kBrowserActionGrippyDragFinishedNotification = | 18 NSString* const kBrowserActionGrippyDragFinishedNotification = |
| 20 @"BrowserActionGrippyDragFinishedNotification"; | 19 @"BrowserActionGrippyDragFinishedNotification"; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 @synthesize maxDesiredWidth = maxDesiredWidth_; | 51 @synthesize maxDesiredWidth = maxDesiredWidth_; |
| 53 @synthesize userIsResizing = userIsResizing_; | 52 @synthesize userIsResizing = userIsResizing_; |
| 54 @synthesize delegate = delegate_; | 53 @synthesize delegate = delegate_; |
| 55 | 54 |
| 56 #pragma mark - | 55 #pragma mark - |
| 57 #pragma mark Overridden Class Functions | 56 #pragma mark Overridden Class Functions |
| 58 | 57 |
| 59 - (id)initWithFrame:(NSRect)frameRect { | 58 - (id)initWithFrame:(NSRect)frameRect { |
| 60 if ((self = [super initWithFrame:frameRect])) { | 59 if ((self = [super initWithFrame:frameRect])) { |
| 61 grippyRect_ = NSMakeRect(0.0, 0.0, kGrippyWidth, NSHeight([self bounds])); | 60 grippyRect_ = NSMakeRect(0.0, 0.0, kGrippyWidth, NSHeight([self bounds])); |
| 62 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) | |
| 63 grippyRect_.origin.x = NSWidth(frameRect) - NSWidth(grippyRect_); | |
| 64 | |
| 65 canDragLeft_ = YES; | 61 canDragLeft_ = YES; |
| 66 canDragRight_ = YES; | 62 canDragRight_ = YES; |
| 67 resizable_ = YES; | 63 resizable_ = YES; |
| 68 | 64 |
| 69 resizeAnimation_.reset([[NSViewAnimation alloc] init]); | 65 resizeAnimation_.reset([[NSViewAnimation alloc] init]); |
| 70 [resizeAnimation_ setDuration:kAnimationDuration]; | 66 [resizeAnimation_ setDuration:kAnimationDuration]; |
| 71 [resizeAnimation_ setAnimationBlockingMode:NSAnimationNonblocking]; | 67 [resizeAnimation_ setAnimationBlockingMode:NSAnimationNonblocking]; |
| 72 [resizeAnimation_ setDelegate:self]; | 68 [resizeAnimation_ setDelegate:self]; |
| 73 | 69 |
| 74 [self setHidden:YES]; | 70 [self setHidden:YES]; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 155 |
| 160 - (void)setIsOverflow:(BOOL)isOverflow { | 156 - (void)setIsOverflow:(BOOL)isOverflow { |
| 161 if (isOverflow_ != isOverflow) { | 157 if (isOverflow_ != isOverflow) { |
| 162 isOverflow_ = isOverflow; | 158 isOverflow_ = isOverflow; |
| 163 resizable_ = !isOverflow_; | 159 resizable_ = !isOverflow_; |
| 164 [self setNeedsDisplay:YES]; | 160 [self setNeedsDisplay:YES]; |
| 165 } | 161 } |
| 166 } | 162 } |
| 167 | 163 |
| 168 - (void)resetCursorRects { | 164 - (void)resetCursorRects { |
| 169 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) | |
| 170 grippyRect_.origin.x = NSWidth([self frame]) - NSWidth(grippyRect_); | |
| 171 [self addCursorRect:grippyRect_ cursor:[self appropriateCursorForGrippy]]; | 165 [self addCursorRect:grippyRect_ cursor:[self appropriateCursorForGrippy]]; |
| 172 } | 166 } |
| 173 | 167 |
| 174 - (BOOL)acceptsFirstResponder { | 168 - (BOOL)acceptsFirstResponder { |
| 175 // The overflow container needs to receive key events to handle in-item | 169 // The overflow container needs to receive key events to handle in-item |
| 176 // navigation. The top-level container should not become first responder, | 170 // navigation. The top-level container should not become first responder, |
| 177 // allowing focus travel to proceed to the first action. | 171 // allowing focus travel to proceed to the first action. |
| 178 return isOverflow_; | 172 return isOverflow_; |
| 179 } | 173 } |
| 180 | 174 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 207 |
| 214 - (void)mouseDragged:(NSEvent*)theEvent { | 208 - (void)mouseDragged:(NSEvent*)theEvent { |
| 215 if (!userIsResizing_) | 209 if (!userIsResizing_) |
| 216 return; | 210 return; |
| 217 | 211 |
| 218 NSPoint location = [self convertPoint:[theEvent locationInWindow] | 212 NSPoint location = [self convertPoint:[theEvent locationInWindow] |
| 219 fromView:nil]; | 213 fromView:nil]; |
| 220 NSRect containerFrame = [self frame]; | 214 NSRect containerFrame = [self frame]; |
| 221 CGFloat dX = [theEvent deltaX]; | 215 CGFloat dX = [theEvent deltaX]; |
| 222 CGFloat withDelta = location.x - dX; | 216 CGFloat withDelta = location.x - dX; |
| 223 BOOL isRTL = cocoa_l10n_util::ShouldDoExperimentalRTLLayout(); | 217 canDragRight_ = (withDelta >= initialDragPoint_.x) && |
| 218 (NSWidth(containerFrame) > kMinimumContainerWidth); |
| 219 CGFloat maxAllowedWidth = [self maxAllowedWidth]; |
| 220 containerFrame.size.width = |
| 221 std::max(NSWidth(containerFrame) - dX, kMinimumContainerWidth); |
| 222 canDragLeft_ = withDelta <= initialDragPoint_.x && |
| 223 NSWidth(containerFrame) < maxDesiredWidth_ && |
| 224 NSWidth(containerFrame) < maxAllowedWidth; |
| 224 | 225 |
| 225 CGFloat maxAllowedWidth = [self maxAllowedWidth]; | 226 if ((dX < 0.0 && !canDragLeft_) || (dX > 0.0 && !canDragRight_)) |
| 226 | |
| 227 const CGFloat maxWidth = std::min(maxAllowedWidth, maxDesiredWidth_); | |
| 228 CGFloat newWidth = NSWidth(containerFrame) + (isRTL ? dX : -dX); | |
| 229 newWidth = std::min(std::max(newWidth, kMinimumContainerWidth), maxWidth); | |
| 230 | |
| 231 BOOL canGrow = NSWidth(containerFrame) < maxWidth; | |
| 232 BOOL canShrink = NSWidth(containerFrame) > kMinimumContainerWidth; | |
| 233 | |
| 234 canDragLeft_ = | |
| 235 withDelta <= initialDragPoint_.x && (isRTL ? canShrink : canGrow); | |
| 236 canDragRight_ = | |
| 237 (withDelta >= initialDragPoint_.x) && (isRTL ? canGrow : canShrink); | |
| 238 if ((dX < 0.0 && !canDragLeft_) || (dX > 0.0 && !canDragRight_) || | |
| 239 fabs(dX) < FLT_EPSILON) | |
| 240 return; | 227 return; |
| 241 | 228 |
| 242 grippyPinned_ = newWidth >= maxAllowedWidth; | 229 if (NSWidth(containerFrame) <= kMinimumContainerWidth) |
| 243 if (!isRTL) | 230 return; |
| 244 containerFrame.origin.x += dX; | 231 |
| 245 containerFrame.size.width = newWidth; | 232 grippyPinned_ = NSWidth(containerFrame) >= maxAllowedWidth; |
| 233 containerFrame.origin.x += dX; |
| 246 | 234 |
| 247 [self setFrame:containerFrame]; | 235 [self setFrame:containerFrame]; |
| 248 [self setNeedsDisplay:YES]; | 236 [self setNeedsDisplay:YES]; |
| 249 | 237 |
| 250 [[NSNotificationCenter defaultCenter] | 238 [[NSNotificationCenter defaultCenter] |
| 251 postNotificationName:kBrowserActionGrippyDraggingNotification | 239 postNotificationName:kBrowserActionGrippyDraggingNotification |
| 252 object:self]; | 240 object:self]; |
| 253 } | 241 } |
| 254 | 242 |
| 255 - (void)animationDidEnd:(NSAnimation*)animation { | 243 - (void)animationDidEnd:(NSAnimation*)animation { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 276 | 264 |
| 277 - (ViewID)viewID { | 265 - (ViewID)viewID { |
| 278 return VIEW_ID_BROWSER_ACTION_TOOLBAR; | 266 return VIEW_ID_BROWSER_ACTION_TOOLBAR; |
| 279 } | 267 } |
| 280 | 268 |
| 281 #pragma mark - | 269 #pragma mark - |
| 282 #pragma mark Public Methods | 270 #pragma mark Public Methods |
| 283 | 271 |
| 284 - (void)resizeToWidth:(CGFloat)width animate:(BOOL)animate { | 272 - (void)resizeToWidth:(CGFloat)width animate:(BOOL)animate { |
| 285 width = std::max(width, kMinimumContainerWidth); | 273 width = std::max(width, kMinimumContainerWidth); |
| 286 NSRect newFrame = [self frame]; | 274 NSRect frame = [self frame]; |
| 287 | 275 |
| 288 CGFloat maxAllowedWidth = [self maxAllowedWidth]; | 276 CGFloat maxAllowedWidth = [self maxAllowedWidth]; |
| 289 width = std::min(maxAllowedWidth, width); | 277 width = std::min(maxAllowedWidth, width); |
| 290 | 278 |
| 291 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) { | 279 CGFloat dX = frame.size.width - width; |
| 292 newFrame.size.width = width; | 280 frame.size.width = width; |
| 293 } else { | 281 NSRect newFrame = NSOffsetRect(frame, dX, 0); |
| 294 CGFloat dX = NSWidth(newFrame) - width; | |
| 295 newFrame.size.width = width; | |
| 296 newFrame.origin.x += dX; | |
| 297 } | |
| 298 | 282 |
| 299 grippyPinned_ = width == maxAllowedWidth; | 283 grippyPinned_ = width == maxAllowedWidth; |
| 300 | 284 |
| 301 [self stopAnimation]; | 285 [self stopAnimation]; |
| 302 | 286 |
| 303 if (animate) { | 287 if (animate) { |
| 304 NSDictionary* animationDictionary = @{ | 288 NSDictionary* animationDictionary = @{ |
| 305 NSViewAnimationTargetKey : self, | 289 NSViewAnimationTargetKey : self, |
| 306 NSViewAnimationStartFrameKey : [NSValue valueWithRect:[self frame]], | 290 NSViewAnimationStartFrameKey : [NSValue valueWithRect:[self frame]], |
| 307 NSViewAnimationEndFrameKey : [NSValue valueWithRect:newFrame] | 291 NSViewAnimationEndFrameKey : [NSValue valueWithRect:newFrame] |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 retVal = [NSCursor resizeLeftRightCursor]; | 338 retVal = [NSCursor resizeLeftRightCursor]; |
| 355 } | 339 } |
| 356 return retVal; | 340 return retVal; |
| 357 } | 341 } |
| 358 | 342 |
| 359 - (CGFloat)maxAllowedWidth { | 343 - (CGFloat)maxAllowedWidth { |
| 360 return delegate_ ? delegate_->GetMaxAllowedWidth() : CGFLOAT_MAX; | 344 return delegate_ ? delegate_->GetMaxAllowedWidth() : CGFLOAT_MAX; |
| 361 } | 345 } |
| 362 | 346 |
| 363 @end | 347 @end |
| OLD | NEW |