OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/location_bar/web_intents_button_decoration.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/ui/browser_tabstrip.h" | |
9 #import "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" | |
10 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | |
11 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | |
12 #include "chrome/browser/ui/omnibox/location_bar_util.h" | |
13 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" | |
14 #include "grit/generated_resources.h" | |
15 #include "grit/theme_resources.h" | |
16 #include "ui/base/l10n/l10n_util_mac.h" | |
17 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | |
18 | |
19 namespace { | |
20 | |
21 // Duration of animation, 150 ms. | |
22 const NSTimeInterval kAnimationDurationS = 0.15; | |
23 | |
24 // Interval of the animation timer, 60Hz. | |
25 const NSTimeInterval kAnimationIntervalS = 1.0 / 60.0; | |
26 | |
27 const CGFloat kTextMarginPadding = 4; | |
28 const CGFloat kIconMarginPadding = 2; | |
29 const CGFloat kBorderPadding = 3; | |
30 | |
31 // During animation, the text opens to full width. | |
32 enum AnimationState { | |
33 kNoAnimation, | |
34 kOpening, | |
35 kOpen | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 // An ObjC class that handles the multiple states of the text animation and | |
41 // bridges NSTimer calls back to the WebIntentsButtonDecoration that owns it. | |
42 // Should be lazily instantiated to only exist when the decoration requires | |
43 // animation. | |
44 // NOTE: One could make this class more generic, but this class only exists | |
45 // because CoreAnimation cannot be used (there are no views to work with). | |
46 @interface WebIntentsButtonAnimationState : NSObject { | |
47 @private | |
48 WebIntentsButtonDecoration* owner_; // Weak, owns this. | |
49 double progress_; // Counter, [0..1], with animation progress. | |
50 NSTimer* timer_; // Animation timer. Owned by the run loop. | |
51 } | |
52 | |
53 // [0..1], the current progress of the animation. -animationState will return | |
54 // |kNoAnimation| when progress is <= 0 or >= 1. Useful when state is | |
55 // |kOpening| as a multiplier for displaying width. Don't use | |
56 // to track state transitions, use -animationState instead. | |
57 @property (readonly, nonatomic) double progress; | |
58 | |
59 // Designated initializer. |owner| must not be nil. Animation timer will start | |
60 // as soon as the object is created. | |
61 - (id)initWithOwner:(WebIntentsButtonDecoration*)owner; | |
62 | |
63 // Returns the current animation state based on how much time has elapsed. | |
64 - (AnimationState)animationState; | |
65 | |
66 // Call when |owner| is going away or the animation needs to be stopped. | |
67 // Ensures that any dangling references are cleared. Can be called multiple | |
68 // times. | |
69 - (void)stopAnimation; | |
70 | |
71 @end | |
72 | |
73 @implementation WebIntentsButtonAnimationState | |
74 | |
75 @synthesize progress = progress_; | |
76 | |
77 - (id)initWithOwner:(WebIntentsButtonDecoration*)owner { | |
78 if ((self = [super init])) { | |
79 owner_ = owner; | |
80 timer_ = [NSTimer scheduledTimerWithTimeInterval:kAnimationIntervalS | |
81 target:self | |
82 selector:@selector(timerFired:) | |
83 userInfo:nil | |
84 repeats:YES]; | |
85 } | |
86 return self; | |
87 } | |
88 | |
89 - (void)dealloc { | |
90 DCHECK(!timer_); | |
91 [super dealloc]; | |
92 } | |
93 | |
94 // Clear weak references and stop the timer. | |
95 - (void)stopAnimation { | |
96 owner_ = nil; | |
97 [timer_ invalidate]; | |
98 timer_ = nil; | |
99 } | |
100 | |
101 // Returns the current state based on how much time has elapsed. | |
102 - (AnimationState)animationState { | |
103 if (progress_ <= 0.0) | |
104 return kNoAnimation; | |
105 if (progress_ <= 1.0) | |
106 return kOpening; | |
107 return kOpen; | |
108 } | |
109 | |
110 - (void)timerFired:(NSTimer*)timer { | |
111 // Increment animation progress, normalized to [0..1]. | |
112 // TODO(gbillock): Note this is brittle. Switch to ui/base/animation. | |
113 progress_ += kAnimationIntervalS / kAnimationDurationS; | |
114 progress_ = std::min(progress_, 1.0); | |
115 owner_->AnimationTimerFired(); | |
116 // Stop timer if it has reached the end of its life. | |
117 if (progress_ >= 1.0) | |
118 [self stopAnimation]; | |
119 } | |
120 | |
121 @end | |
122 | |
123 WebIntentsButtonDecoration::WebIntentsButtonDecoration( | |
124 LocationBarViewMac* owner, NSFont* font) | |
125 : BubbleDecoration(font), | |
126 owner_(owner), | |
127 text_width_(0.0) { | |
128 NSColor* border_color = | |
129 [NSColor colorWithCalibratedRed:0.63 green:0.63 blue:0.63 alpha:1.0]; | |
130 NSColor* background_color = | |
131 [NSColor colorWithCalibratedRed:0.90 green:0.90 blue:0.90 alpha:1.0]; | |
132 SetColors(border_color, background_color, [NSColor blackColor]); | |
133 | |
134 SetLabel(l10n_util::GetNSString(IDS_INTENT_PICKER_USE_ANOTHER_SERVICE)); | |
135 } | |
136 | |
137 WebIntentsButtonDecoration::~WebIntentsButtonDecoration() {} | |
138 | |
139 void WebIntentsButtonDecoration::SetButtonImages(NSImage* left, | |
140 NSImage* center, | |
141 NSImage* right) { | |
142 left_image_.reset([left retain]); | |
143 center_image_.reset([center retain]); | |
144 right_image_.reset([right retain]); | |
145 } | |
146 | |
147 bool WebIntentsButtonDecoration::AcceptsMousePress() { | |
148 return true; | |
149 } | |
150 | |
151 bool WebIntentsButtonDecoration::OnMousePressed(NSRect frame) { | |
152 content::WebContents* web_contents = owner_->GetWebContents(); | |
153 if (!web_contents) | |
154 return true; | |
155 | |
156 WebIntentPickerController::FromWebContents(web_contents)-> | |
157 LocationBarPickerButtonClicked(); | |
158 return true; | |
159 } | |
160 | |
161 // Override to handle the case where there is text to display during the | |
162 // animation. The width is based on the animator's progress. | |
163 CGFloat WebIntentsButtonDecoration::GetWidthForSpace(CGFloat width, | |
164 CGFloat text_width) { | |
165 if (!animation_) | |
166 return BubbleDecoration::GetWidthForSpace(width, text_width); | |
167 | |
168 AnimationState state = [animation_ animationState]; | |
169 CGFloat progress = [animation_ progress]; | |
170 // Set the margins, fixed for all animation states. | |
171 CGFloat preferredWidth = 2 * kTextMarginPadding; | |
172 // Add the width of the text based on the state of the animation. | |
173 switch (state) { | |
174 case kNoAnimation: | |
175 return preferredWidth; | |
176 case kOpening: | |
177 return preferredWidth + (text_width_ * progress); | |
178 case kOpen: | |
179 return preferredWidth + text_width_; | |
180 } | |
181 } | |
182 | |
183 void WebIntentsButtonDecoration::DrawInFrame(NSRect frame, | |
184 NSView* control_view) { | |
185 if ([animation_ animationState] != kOpening) { | |
186 BubbleDecoration::DrawInFrame(frame, control_view); | |
187 return; | |
188 } | |
189 | |
190 frame = NSInsetRect(frame, 0.0, kTextYInset); | |
191 NSDrawThreePartImage(frame, | |
192 left_image_.get(), | |
193 center_image_.get(), | |
194 right_image_.get(), | |
195 NO, // NO=horizontal layout | |
196 NSCompositeSourceOver, | |
197 1.0, | |
198 YES); // use flipped coordinates | |
199 | |
200 // Draw the text, clipped to fit on the right. While handling clipping, | |
201 // NSAttributedString's drawInRect: won't draw a word if it doesn't fit | |
202 // in the bounding box so instead use drawAtPoint: with a manual clip | |
203 // rect. | |
204 gfx::ScopedNSGraphicsContextSaveGState scopedGState; | |
205 NSRectClip(frame); | |
206 NSRect remainder = NSInsetRect(frame, kTextMarginPadding, 0.0); | |
207 // .get() needed to fix compiler warning (confusion with NSImageRep). | |
208 [animated_text_.get() drawAtPoint:remainder.origin]; | |
209 } | |
210 | |
211 void WebIntentsButtonDecoration::Update(content::WebContents* web_contents) { | |
212 WebIntentPickerController* intents_controller = | |
213 WebIntentPickerController::FromWebContents(web_contents); | |
214 SetVisible(intents_controller->ShowLocationBarPickerButton()); | |
215 | |
216 if (IsVisible()) { | |
217 if (!animation_ && | |
218 !intents_controller->location_bar_picker_button_indicated()) { | |
219 intents_controller->SetLocationBarPickerButtonIndicated(); | |
220 animation_.reset( | |
221 [[WebIntentsButtonAnimationState alloc] initWithOwner:this]); | |
222 animated_text_ = CreateAnimatedText(); | |
223 text_width_ = MeasureTextWidth(); | |
224 } | |
225 } else { | |
226 [animation_ stopAnimation]; | |
227 animation_.reset(); | |
228 } | |
229 } | |
230 | |
231 scoped_nsobject<NSAttributedString> | |
232 WebIntentsButtonDecoration::CreateAnimatedText() { | |
233 NSString* text = | |
234 l10n_util::GetNSString(IDS_INTENT_PICKER_USE_ANOTHER_SERVICE); | |
235 return scoped_nsobject<NSAttributedString>( | |
236 [[NSAttributedString alloc] initWithString:text attributes:attributes_]); | |
237 } | |
238 | |
239 CGFloat WebIntentsButtonDecoration::MeasureTextWidth() { | |
240 return [animated_text_ size].width; | |
241 } | |
242 | |
243 void WebIntentsButtonDecoration::AnimationTimerFired() { | |
244 if (owner_) | |
245 owner_->Layout(); | |
246 // Even after the animation completes, the |animator_| object should be kept | |
247 // alive to prevent the animation from re-appearing if the page opens | |
248 // additional popups later. The animator will be cleared when the decoration | |
249 // hides, indicating something has changed with the WebContents (probably | |
250 // navigation). | |
251 } | |
OLD | NEW |