OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/history_overlay_controller.h" |
| 6 |
| 7 #import <QuartzCore/QuartzCore.h> |
| 8 |
| 9 #include <cmath> |
| 10 |
| 11 // OverlayFrameView //////////////////////////////////////////////////////////// |
| 12 |
| 13 // The content view of the window that draws a custom frame. |
| 14 @interface OverlayFrameView : NSView { |
| 15 @private |
| 16 NSTextField* message_; // Weak, owned by the view hierarchy. |
| 17 } |
| 18 - (void)setMessageText:(NSString*)text; |
| 19 @end |
| 20 |
| 21 // The content view of the window that draws a custom frame. |
| 22 @implementation OverlayFrameView |
| 23 |
| 24 - (id)initWithFrame:(NSRect)frameRect { |
| 25 if ((self = [super initWithFrame:frameRect])) { |
| 26 scoped_nsobject<NSTextField> message( |
| 27 // The frame will be fixed up when |-setMessageText:| is called. |
| 28 [[NSTextField alloc] initWithFrame:NSZeroRect]); |
| 29 message_ = message.get(); |
| 30 [message_ setEditable:NO]; |
| 31 [message_ setSelectable:NO]; |
| 32 [message_ setBezeled:NO]; |
| 33 [message_ setDrawsBackground:NO]; |
| 34 [message_ setFont:[NSFont boldSystemFontOfSize:72]]; |
| 35 [message_ setTextColor:[NSColor whiteColor]]; |
| 36 [self addSubview:message_]; |
| 37 } |
| 38 return self; |
| 39 } |
| 40 |
| 41 - (void)drawRect:(NSRect)dirtyRect { |
| 42 const CGFloat kCornerRadius = 5.0; |
| 43 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] |
| 44 xRadius:kCornerRadius |
| 45 yRadius:kCornerRadius]; |
| 46 |
| 47 NSColor* fillColor = [NSColor colorWithCalibratedWhite:0.2 alpha:0.85]; |
| 48 [fillColor set]; |
| 49 [path fill]; |
| 50 } |
| 51 |
| 52 - (void)setMessageText:(NSString*)text { |
| 53 const CGFloat kHorizontalPadding = 30; // In view coordinates. |
| 54 |
| 55 // Style the string. |
| 56 scoped_nsobject<NSMutableAttributedString> attrString( |
| 57 [[NSMutableAttributedString alloc] initWithString:text]); |
| 58 scoped_nsobject<NSShadow> textShadow([[NSShadow alloc] init]); |
| 59 [textShadow.get() setShadowColor:[NSColor colorWithCalibratedWhite:0 |
| 60 alpha:0.6]]; |
| 61 [textShadow.get() setShadowOffset:NSMakeSize(0, -1)]; |
| 62 [textShadow setShadowBlurRadius:1.0]; |
| 63 [attrString addAttribute:NSShadowAttributeName |
| 64 value:textShadow |
| 65 range:NSMakeRange(0, [text length])]; |
| 66 [message_ setAttributedStringValue:attrString]; |
| 67 |
| 68 // Fix up the frame of the string. |
| 69 [message_ sizeToFit]; |
| 70 NSRect messageFrame = [message_ frame]; |
| 71 NSRect frameInViewSpace = |
| 72 [message_ convertRect:[[self window] frame] fromView:nil]; |
| 73 |
| 74 if (NSWidth(messageFrame) > NSWidth(frameInViewSpace)) |
| 75 frameInViewSpace.size.width = NSWidth(messageFrame) + kHorizontalPadding; |
| 76 |
| 77 messageFrame.origin.x = |
| 78 (NSWidth(frameInViewSpace) - NSWidth(messageFrame)) / 2; |
| 79 messageFrame.origin.y = |
| 80 (NSHeight(frameInViewSpace) - NSHeight(messageFrame)) / 2; |
| 81 |
| 82 [[self window] setFrame:[message_ convertRect:frameInViewSpace toView:nil] |
| 83 display:YES]; |
| 84 [message_ setFrame:messageFrame]; |
| 85 } |
| 86 |
| 87 @end |
| 88 |
| 89 // HistoryOverlayController //////////////////////////////////////////////////// |
| 90 |
| 91 @implementation HistoryOverlayController |
| 92 |
| 93 - (id)initForMode:(HistoryOverlayMode)mode { |
| 94 const NSRect kWindowFrame = NSMakeRect(0, 0, 120, 70); |
| 95 scoped_nsobject<NSWindow> window( |
| 96 [[NSWindow alloc] initWithContentRect:kWindowFrame |
| 97 styleMask:NSBorderlessWindowMask |
| 98 backing:NSBackingStoreBuffered |
| 99 defer:NO]); |
| 100 if ((self = [super initWithWindow:window])) { |
| 101 mode_ = mode; |
| 102 |
| 103 [window setDelegate:self]; |
| 104 [window setBackgroundColor:[NSColor clearColor]]; |
| 105 [window setOpaque:NO]; |
| 106 [window setHasShadow:NO]; |
| 107 |
| 108 // Create the content view. Take the frame from the existing content view. |
| 109 NSRect frame = [[window contentView] frame]; |
| 110 scoped_nsobject<OverlayFrameView> frameView( |
| 111 [[OverlayFrameView alloc] initWithFrame:frame]); |
| 112 contentView_ = frameView.get(); |
| 113 [window setContentView:contentView_]; |
| 114 |
| 115 const unichar kBackArrowCharacter = 0x2190; |
| 116 const unichar kForwardArrowCharacter = 0x2192; |
| 117 |
| 118 unichar commandChar = mode_ == kHistoryOverlayModeForward ? |
| 119 kForwardArrowCharacter : kBackArrowCharacter; |
| 120 NSString* text = |
| 121 [NSString stringWithCharacters:&commandChar length:1]; |
| 122 [contentView_ setMessageText:text]; |
| 123 } |
| 124 return self; |
| 125 } |
| 126 |
| 127 - (void)setProgress:(CGFloat)gestureAmount { |
| 128 const CGFloat kVerticalPositionRatio = 0.65; |
| 129 NSRect windowFrame = [parent_ frame]; |
| 130 CGFloat minX = NSMinX(windowFrame); |
| 131 CGFloat maxX = NSMaxX(windowFrame) - NSWidth([[self window] frame]); |
| 132 CGFloat x = 0; |
| 133 if (mode_ == kHistoryOverlayModeForward) |
| 134 x = maxX + gestureAmount * (maxX - minX); |
| 135 else if (mode_ == kHistoryOverlayModeBack) |
| 136 x = minX + gestureAmount * (maxX - minX); |
| 137 NSPoint p = [parent_ frame].origin; |
| 138 p.x = x; |
| 139 p.y += (NSHeight(windowFrame) - NSHeight([[self window] frame])) * |
| 140 kVerticalPositionRatio; |
| 141 [[self window] setFrameOrigin:p]; |
| 142 |
| 143 CGFloat alpha = |
| 144 -(std::abs(gestureAmount) - 1) * (std::abs(gestureAmount) - 1) + 1; |
| 145 [[self window] setAlphaValue:alpha]; |
| 146 } |
| 147 |
| 148 - (void)dismiss { |
| 149 const CGFloat kFadeOutDurationSeconds = 0.2; |
| 150 |
| 151 NSWindow* overlayWindow = [self window]; |
| 152 |
| 153 scoped_nsobject<CAAnimation> animation( |
| 154 [[overlayWindow animationForKey:@"alphaValue"] copy]); |
| 155 [animation setDelegate:self]; |
| 156 [animation setDuration:kFadeOutDurationSeconds]; |
| 157 NSMutableDictionary* dictionary = |
| 158 [NSMutableDictionary dictionaryWithCapacity:1]; |
| 159 [dictionary setObject:animation forKey:@"alphaValue"]; |
| 160 [overlayWindow setAnimations:dictionary]; |
| 161 [[overlayWindow animator] setAlphaValue:0.0]; |
| 162 } |
| 163 |
| 164 - (void)windowWillClose:(NSNotification*)notification { |
| 165 // Release all animations because CAAnimation retains its delegate (self), |
| 166 // which will cause a retain cycle. Break it! |
| 167 [[self window] setAnimations:[NSDictionary dictionary]]; |
| 168 } |
| 169 |
| 170 - (void)showPanelForWindow:(NSWindow*)window { |
| 171 parent_.reset([window retain]); |
| 172 [self setProgress:0]; // Set initial window position. |
| 173 [self showWindow:self]; |
| 174 } |
| 175 |
| 176 - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)finished { |
| 177 [self close]; |
| 178 } |
| 179 |
| 180 @end |
OLD | NEW |