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; | |
Alexei Svitkine (slow)
2011/08/16 14:10:04
Can you DCHECK() the mode to be one of the expecte
Mark Mentovai
2011/08/16 14:17:24
asvitkine_ wrote:
Alexei Svitkine (slow)
2011/08/16 14:21:26
Good point - I take that back, the compiler would
| |
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 NSRect windowFrame = [parent_ frame]; | |
129 CGFloat minX = NSMinX(windowFrame); | |
130 CGFloat maxX = NSMaxX(windowFrame) - NSWidth([[self window] frame]); | |
131 CGFloat x = 0; | |
132 if (mode_ == kHistoryOverlayModeForward) | |
133 x = maxX + gestureAmount * (maxX - minX); | |
134 else if (mode_ == kHistoryOverlayModeBack) | |
135 x = minX + gestureAmount * (maxX - minX); | |
Alexei Svitkine (slow)
2011/08/16 14:10:04
Maybe just initialize x to "gestureAmount * (maxX
Nico
2011/08/16 16:07:53
That's shorter, but harder to understand imho.
| |
136 NSPoint p = [parent_ frame].origin; | |
137 p.x = x; | |
138 p.y += (NSHeight(windowFrame) - NSHeight([[self window] frame])) * 0.65; | |
Alexei Svitkine (slow)
2011/08/16 14:10:04
Can you make move 0.65 to a constant? Something li
Nico
2011/08/16 16:07:53
Done.
| |
139 [[self window] setFrameOrigin:p]; | |
140 | |
141 CGFloat alpha = | |
142 -(std::abs(gestureAmount) - 1) * (std::abs(gestureAmount) - 1) + 1; | |
143 [[self window] setAlphaValue:alpha]; | |
144 } | |
145 | |
146 - (void)dismiss { | |
147 const CGFloat kFadeOutDurationSeconds = 0.2; | |
148 | |
149 NSWindow* overlayWindow = [self window]; | |
150 | |
151 scoped_nsobject<CAAnimation> animation( | |
152 [[overlayWindow animationForKey:@"alphaValue"] copy]); | |
153 [animation setDelegate:self]; | |
154 [animation setDuration:kFadeOutDurationSeconds]; | |
155 NSMutableDictionary* dictionary = | |
156 [NSMutableDictionary dictionaryWithCapacity:1]; | |
157 [dictionary setObject:animation forKey:@"alphaValue"]; | |
158 [overlayWindow setAnimations:dictionary]; | |
159 [[overlayWindow animator] setAlphaValue:0.0]; | |
160 } | |
161 | |
162 - (void)windowWillClose:(NSNotification*)notification { | |
163 // Release all animations because CAAnimation retains its delegate (self), | |
164 // which will cause a retain cycle. Break it! | |
165 [[self window] setAnimations:[NSDictionary dictionary]]; | |
166 } | |
167 | |
168 - (void)showPanelForWindow:(NSWindow*)window { | |
169 parent_.reset([window retain]); | |
170 [self setProgress:0]; // Set initial window position. | |
171 [self showWindow:self]; | |
172 } | |
173 | |
174 - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)finished { | |
175 [self close]; | |
176 } | |
177 | |
178 @end | |
OLD | NEW |