Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/ui/cocoa/history_overlay_controller.mm

Issue 7645041: mac: Makeshift UI for scroll feedback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/logging.h"
Mark Mentovai 2011/08/16 01:01:20 Used? Doesn’t look it.
Nico 2011/08/16 04:56:17 Done.
10 #include "base/memory/scoped_nsobject.h"
11 #include "base/sys_string_conversions.h"
Mark Mentovai 2011/08/16 01:01:20 Used? Doesn’t look it.
Nico 2011/08/16 04:56:17 Done.
12
13 // OverlayFrameView ////////////////////////////////////////////////////////////
14
15 // The content view of the window that draws a custom frame.
16 @interface OverlayFrameView : NSView {
17 @private
18 NSTextField* message_; // Weak, owned by the view hierarchy.
19 }
20 - (void)setMessageText:(NSString*)text;
21 @end
22
23 // The content view of the window that draws a custom frame.
24 @implementation OverlayFrameView
25
26 - (id)initWithFrame:(NSRect)frameRect {
Mark Mentovai 2011/08/16 01:01:20 I know where this code is ultimately ripped from:
Nico 2011/08/16 04:56:17 I'm hoping to get a bigger version of our back but
27 if ((self = [super initWithFrame:frameRect])) {
28 scoped_nsobject<NSTextField> message(
29 // The frame will be fixed up when |-setMessageText:| is called.
30 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);
Mark Mentovai 2011/08/16 01:01:20 NSZeroRect?
Nico 2011/08/16 04:56:17 Done.
31 message_ = message.get();
32 [message_ setEditable:NO];
33 [message_ setSelectable:NO];
34 [message_ setBezeled:NO];
35 [message_ setDrawsBackground:NO];
36 [message_ setFont:[NSFont boldSystemFontOfSize:72]];
37 [message_ setTextColor:[NSColor whiteColor]];
38 [self addSubview:message_];
39 }
40 return self;
41 }
42
43 - (void)drawRect:(NSRect)dirtyRect {
44 const CGFloat kCornerRadius = 5.0;
45 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:[self bounds]
46 xRadius:kCornerRadius
47 yRadius:kCornerRadius];
48
49 NSColor* fillColor = [NSColor colorWithCalibratedWhite:0.2 alpha:0.85];
50 [fillColor set];
51 [path fill];
52 }
53
54 - (void)setMessageText:(NSString*)text {
55 const CGFloat kHorizontalPadding = 30; // In view coordinates.
56
57 // Style the string.
58 scoped_nsobject<NSMutableAttributedString> attrString(
59 [[NSMutableAttributedString alloc] initWithString:text]);
60 scoped_nsobject<NSShadow> textShadow([[NSShadow alloc] init]);
61 [textShadow.get() setShadowColor:[NSColor colorWithCalibratedWhite:0
62 alpha:0.6]];
63 [textShadow.get() setShadowOffset:NSMakeSize(0, -1)];
64 [textShadow setShadowBlurRadius:1.0];
65 [attrString addAttribute:NSShadowAttributeName
66 value:textShadow
67 range:NSMakeRange(0, [text length])];
68 [message_ setAttributedStringValue:attrString];
69
70 // Fixup the frame of the string.
Mark Mentovai 2011/08/16 01:01:20 “Fix up” as a verb is two words (although the prob
Nico 2011/08/16 04:56:17 Done.
71 [message_ sizeToFit];
72 NSRect messageFrame = [message_ frame];
73 NSRect frameInViewSpace =
74 [message_ convertRect:[[self window] frame] fromView:nil];
75
76 if (NSWidth(messageFrame) > NSWidth(frameInViewSpace))
77 frameInViewSpace.size.width = NSWidth(messageFrame) + kHorizontalPadding;
78
79 messageFrame.origin.x =
80 (NSWidth(frameInViewSpace) - NSWidth(messageFrame)) / 2;
81 messageFrame.origin.y =
82 (NSHeight(frameInViewSpace) - NSHeight(messageFrame)) / 2;
83
84 [[self window] setFrame:[message_ convertRect:frameInViewSpace toView:nil]
85 display:YES];
86 [message_ setFrame:messageFrame];
87 }
88
89 @end
90
91 // HistoryOverlayController ////////////////////////////////////////////////////
92
93 @implementation HistoryOverlayController
94
95 - (id)initForMode:(HistoryOverlayMode)mode {
96 const NSRect kWindowFrame = NSMakeRect(0, 0, 120, 70);
97 scoped_nsobject<NSWindow> window(
98 [[NSWindow alloc] initWithContentRect:kWindowFrame
99 styleMask:NSBorderlessWindowMask
100 backing:NSBackingStoreBuffered
101 defer:NO]);
102 if ((self = [super initWithWindow:window])) {
103 mode_ = mode;
104
105 [window setDelegate:self];
106 [window setBackgroundColor:[NSColor clearColor]];
107 [window setOpaque:NO];
108 [window setHasShadow:NO];
109
110 // Create the content view. Take the frame from the existing content view.
111 NSRect frame = [[window contentView] frame];
112 scoped_nsobject<OverlayFrameView> frameView(
113 [[OverlayFrameView alloc] initWithFrame:frame]);
114 contentView_ = frameView.get();
115 [window setContentView:contentView_];
116
117 const unichar kBackArrowCharacter = 0x2190;
118 const unichar kForwardArrowCharacter = 0x2192;
119
120 unichar commandChar = mode_ == kHistoryOverlayModeForward ?
121 kForwardArrowCharacter : kBackArrowCharacter;
122 NSString* text =
123 [NSString stringWithCharacters:&commandChar length:1];
124 [contentView_ setMessageText:text];
125 }
126 return self;
127 }
128
129 - (void)setProgress:(CGFloat)gestureAmount {
130 CGFloat alpha = 0;
131 CGFloat x = 0;
132
133 NSRect windowFrame = [parent_ frame];
134 CGFloat minX = NSMinX(windowFrame);
135 CGFloat maxX = NSMaxX(windowFrame) - NSWidth([[self window] frame]);
136
137 if (mode_ == kHistoryOverlayModeForward) {
138 alpha = -(gestureAmount + 1) * (gestureAmount + 1) + 1;
Mark Mentovai 2011/08/16 01:01:20 Can you pull the alpha computation out of the cond
Nico 2011/08/16 04:56:17 It feels good starting at 0 I think. Did the rest.
139 x = maxX + gestureAmount * (maxX - minX);
140 } else if (mode_ == kHistoryOverlayModeBack) {
141 alpha = -(gestureAmount - 1) * (gestureAmount - 1) + 1;
142 x = minX + gestureAmount * (maxX - minX);
143 }
144 [[self window] setAlphaValue:alpha];
145
146 NSPoint p = [parent_ frame].origin;
147 p.x = x;
Mark Mentovai 2011/08/16 01:01:20 I wonder if we need a “target” for the arrow to hi
Nico 2011/08/16 04:56:17 Yes, this will need tweaking.
148 p.y += (NSHeight(windowFrame) - NSHeight([[self window] frame])) * 0.65;
149 [[self window] setFrameOrigin:p];
150 }
151
152 - (void)dismiss {
153 const CGFloat kFadeOutDuration = 0.2; // In seconds.
Mark Mentovai 2011/08/16 01:01:20 Instead of the comment, name the variable kFadeOut
Nico 2011/08/16 04:56:17 Done.
154
155 NSWindow* overlayWindow = [self window];
156
157 scoped_nsobject<CAAnimation> animation(
158 [[overlayWindow animationForKey:@"alphaValue"] copy]);
159 [animation setDelegate:self];
160 [animation setDuration:kFadeOutDuration];
161 NSMutableDictionary* dictionary =
162 [NSMutableDictionary dictionaryWithCapacity:1];
163 [dictionary setObject:animation forKey:@"alphaValue"];
164 [overlayWindow setAnimations:dictionary];
165 [[overlayWindow animator] setAlphaValue:0.0];
166 }
167
168 - (void)windowWillClose:(NSNotification*)notif {
Mark Mentovai 2011/08/16 01:01:20 Don’t name variables notif, even if they’re unused
Nico 2011/08/16 04:56:17 Done.
169 // Release all animations because CAAnimation retains its delegate (self),
170 // which will cause a retain cycle. Break it!
171 [[self window] setAnimations:[NSDictionary dictionary]];
172 }
173
174 - (void)showPanelForWindow:(NSWindow*)window {
175 parent_ = window;
Mark Mentovai 2011/08/16 01:01:20 Is it possible for parent_ to go away while this o
Nico 2011/08/16 04:56:17 Great catch, done. I made it a scoped_nsobject (hi
176 [self setProgress:0]; // Set initial window position.
177 [self showWindow:self];
178 }
179
180 - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)finished {
181 [self close];
182 }
183
184 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698