Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/mac_util.h" | 8 #include "base/mac_util.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "grit/generated_resources.h" | 10 #include "grit/generated_resources.h" |
| 11 #include "chrome/browser/find_bar_controller.h" | 11 #include "chrome/browser/find_bar_controller.h" |
| 12 #include "chrome/browser/cocoa/browser_window_cocoa.h" | 12 #include "chrome/browser/cocoa/browser_window_cocoa.h" |
| 13 #import "chrome/browser/cocoa/find_bar_cocoa_controller.h" | 13 #import "chrome/browser/cocoa/find_bar_cocoa_controller.h" |
| 14 #import "chrome/browser/cocoa/find_bar_bridge.h" | 14 #import "chrome/browser/cocoa/find_bar_bridge.h" |
| 15 #import "chrome/browser/cocoa/focus_tracker.h" | 15 #import "chrome/browser/cocoa/focus_tracker.h" |
| 16 #import "chrome/browser/cocoa/tab_strip_controller.h" | 16 #import "chrome/browser/cocoa/tab_strip_controller.h" |
| 17 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
| 18 | 18 |
| 19 namespace { | |
| 20 static float kFindBarOpenDuration = 0.2; | |
| 21 static float kFindBarCloseDuration = 0.15; | |
| 22 } | |
| 23 | |
| 24 @interface FindBarCocoaController (PrivateMethods) | |
| 25 // Returns the appropriate frame for a hidden find bar. | |
| 26 - (NSRect)hiddenFindBarFrame; | |
| 27 | |
| 28 // Sets the frame of |findBarView_|. |duration| is ignored if |animate| is NO. | |
| 29 - (void)setFindBarFrame:(NSRect)endFrame | |
| 30 animate:(BOOL)animate | |
| 31 duration:(float)duration; | |
| 32 @end | |
| 33 | |
| 19 @implementation FindBarCocoaController | 34 @implementation FindBarCocoaController |
| 20 | 35 |
| 21 - (id)init { | 36 - (id)init { |
| 22 if ((self = [super initWithNibName:@"FindBar" | 37 if ((self = [super initWithNibName:@"FindBar" |
| 23 bundle:mac_util::MainAppBundle()])) { | 38 bundle:mac_util::MainAppBundle()])) { |
| 24 } | 39 } |
| 25 return self; | 40 return self; |
| 26 } | 41 } |
| 27 | 42 |
| 43 - (void)dealloc { | |
| 44 // All animations should be explicitly stopped by the TabContents before a tab | |
| 45 // is closed. | |
| 46 DCHECK(!currentAnimation_.get()); | |
| 47 [super dealloc]; | |
| 48 } | |
| 49 | |
| 28 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge { | 50 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge { |
| 29 DCHECK(!findBarBridge_); // should only be called once. | 51 DCHECK(!findBarBridge_); // should only be called once. |
| 30 findBarBridge_ = findBarBridge; | 52 findBarBridge_ = findBarBridge; |
| 31 } | 53 } |
| 32 | 54 |
| 33 - (void)awakeFromNib { | 55 - (void)awakeFromNib { |
| 34 [[self view] setHidden:YES]; | 56 [findBarView_ setFrame:[self hiddenFindBarFrame]]; |
| 35 } | 57 } |
| 36 | 58 |
| 37 - (IBAction)close:(id)sender { | 59 - (IBAction)close:(id)sender { |
| 38 if (findBarBridge_) | 60 if (findBarBridge_) |
| 39 findBarBridge_->GetFindBarController()->EndFindSession(); | 61 findBarBridge_->GetFindBarController()->EndFindSession(); |
| 40 } | 62 } |
| 41 | 63 |
| 42 - (IBAction)previousResult:(id)sender { | 64 - (IBAction)previousResult:(id)sender { |
| 43 if (findBarBridge_) | 65 if (findBarBridge_) |
| 44 findBarBridge_->GetFindBarController()->tab_contents()->StartFinding( | 66 findBarBridge_->GetFindBarController()->tab_contents()->StartFinding( |
| 45 base::SysNSStringToUTF16([findText_ stringValue]), | 67 base::SysNSStringToUTF16([findText_ stringValue]), |
| 46 false, false); | 68 false, false); |
| 47 } | 69 } |
| 48 | 70 |
| 49 - (IBAction)nextResult:(id)sender { | 71 - (IBAction)nextResult:(id)sender { |
| 50 if (findBarBridge_) | 72 if (findBarBridge_) |
| 51 findBarBridge_->GetFindBarController()->tab_contents()->StartFinding( | 73 findBarBridge_->GetFindBarController()->tab_contents()->StartFinding( |
| 52 base::SysNSStringToUTF16([findText_ stringValue]), | 74 base::SysNSStringToUTF16([findText_ stringValue]), |
| 53 true, false); | 75 true, false); |
| 54 } | 76 } |
| 55 | 77 |
| 56 // Positions the find bar view in the correct location based on the current | 78 // Positions the find bar container view in the correct location based on the |
| 57 // state of the window. The findbar is always positioned one pixel above the | 79 // current state of the window. The find bar container is always positioned one |
| 58 // infobar container. Note that we are using the infobar container location as | 80 // pixel above the infobar container. Note that we are using the infobar |
| 59 // a proxy for the toolbar location, but we cannot position based on the toolbar | 81 // container location as a proxy for the toolbar location, but we cannot |
| 60 // because the toolbar is not always present (for example in fullscreen | 82 // position based on the toolbar because the toolbar is not always present (for |
| 61 // windows). | 83 // example in fullscreen windows). |
| 62 - (void)positionFindBarView:(NSView*)infoBarContainerView { | 84 - (void)positionFindBarView:(NSView*)infoBarContainerView { |
| 63 static const int kRightEdgeOffset = 25; | 85 static const int kRightEdgeOffset = 25; |
| 64 NSView* findBarView = [self view]; | 86 NSView* containerView = [self view]; |
| 65 int findBarHeight = NSHeight([findBarView frame]); | 87 int containerHeight = NSHeight([containerView frame]); |
| 66 int findBarWidth = NSWidth([findBarView frame]); | 88 int containerWidth = NSWidth([containerView frame]); |
| 67 | 89 |
| 68 // Start by computing the upper right corner of the infobar container, then | 90 // Start by computing the upper right corner of the infobar container, then |
| 69 // move left by a constant offset and up one pixel. This gives us the upper | 91 // move left by a constant offset and up one pixel. This gives us the upper |
| 70 // right corner of our bounding box. We move up one pixel to overlap with the | 92 // right corner of our bounding box. We move up one pixel to overlap with the |
| 71 // toolbar area, which allows us to cover up the toolbar's border, if present. | 93 // toolbar area, which allows us to cover up the toolbar's border, if present. |
| 72 NSRect windowRect = [infoBarContainerView frame]; | 94 NSRect windowRect = [infoBarContainerView frame]; |
| 73 int max_x = NSMaxX(windowRect) - kRightEdgeOffset; | 95 int max_x = NSMaxX(windowRect) - kRightEdgeOffset; |
| 74 int max_y = NSMaxY(windowRect) + 1; | 96 int max_y = NSMaxY(windowRect) + 1; |
| 75 | 97 |
| 76 NSRect findRect = NSMakeRect(max_x - findBarWidth, max_y - findBarHeight, | 98 NSRect newFrame = NSMakeRect(max_x - containerWidth, max_y - containerHeight, |
| 77 findBarWidth, findBarHeight); | 99 containerWidth, containerHeight); |
| 78 [findBarView setFrame:findRect]; | 100 [containerView setFrame:newFrame]; |
| 79 } | 101 } |
| 80 | 102 |
| 81 // NSControl delegate method. | 103 // NSControl delegate method. |
| 82 - (void)controlTextDidChange:(NSNotification *)aNotification { | 104 - (void)controlTextDidChange:(NSNotification *)aNotification { |
| 83 if (!findBarBridge_) | 105 if (!findBarBridge_) |
| 84 return; | 106 return; |
| 85 | 107 |
| 86 TabContents* tab_contents = | 108 TabContents* tab_contents = |
| 87 findBarBridge_->GetFindBarController()->tab_contents(); | 109 findBarBridge_->GetFindBarController()->tab_contents(); |
| 88 if (!tab_contents) | 110 if (!tab_contents) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 // that function that we want to keep or avoid? | 156 // that function that we want to keep or avoid? |
| 135 RenderViewHost* render_view_host = contents->render_view_host(); | 157 RenderViewHost* render_view_host = contents->render_view_host(); |
| 136 render_view_host->ForwardKeyboardEvent(NativeWebKeyboardEvent(event)); | 158 render_view_host->ForwardKeyboardEvent(NativeWebKeyboardEvent(event)); |
| 137 return YES; | 159 return YES; |
| 138 } | 160 } |
| 139 | 161 |
| 140 return NO; | 162 return NO; |
| 141 } | 163 } |
| 142 | 164 |
| 143 // Methods from FindBar | 165 // Methods from FindBar |
| 144 - (void)showFindBar { | 166 - (void)showFindBar:(BOOL)animate { |
| 145 [[self view] setHidden:NO]; | 167 // Save the currently-focused view. |findBarView_| is in the view |
| 146 | |
| 147 // Save the currently-focused view. |[self view]| is in the view | |
| 148 // hierarchy by now. showFindBar can be called even when the | 168 // hierarchy by now. showFindBar can be called even when the |
| 149 // findbar is already open, so do not overwrite an already saved | 169 // findbar is already open, so do not overwrite an already saved |
| 150 // view. | 170 // view. |
| 151 if (!focusTracker_.get()) | 171 if (!focusTracker_.get()) |
| 152 focusTracker_.reset( | 172 focusTracker_.reset( |
| 153 [[FocusTracker alloc] initWithWindow:[[self view] window]]); | 173 [[FocusTracker alloc] initWithWindow:[findBarView_ window]]); |
| 174 | |
| 175 // Animate the view into place. | |
| 176 NSRect frame = [findBarView_ frame]; | |
| 177 frame.origin = NSMakePoint(0, 0); | |
| 178 [self setFindBarFrame:frame animate:animate duration:kFindBarOpenDuration]; | |
| 154 } | 179 } |
| 155 | 180 |
| 156 - (void)hideFindBar { | 181 - (void)hideFindBar:(BOOL)animate { |
| 157 [[self view] setHidden:YES]; | 182 NSRect frame = [self hiddenFindBarFrame]; |
| 183 [self setFindBarFrame:frame animate:animate duration:kFindBarCloseDuration]; | |
| 184 } | |
| 185 | |
| 186 - (void)stopAnimation { | |
| 187 if (currentAnimation_.get()) { | |
| 188 [currentAnimation_ stopAnimation]; | |
| 189 currentAnimation_.reset(nil); | |
| 190 } | |
| 158 } | 191 } |
| 159 | 192 |
| 160 - (void)setFocusAndSelection { | 193 - (void)setFocusAndSelection { |
| 161 [[findText_ window] makeFirstResponder:findText_]; | 194 [[findText_ window] makeFirstResponder:findText_]; |
| 162 | 195 |
| 163 // Enable the buttons if the find text is non-empty. | 196 // Enable the buttons if the find text is non-empty. |
| 164 BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO; | 197 BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO; |
| 165 [previousButton_ setEnabled:buttonsEnabled]; | 198 [previousButton_ setEnabled:buttonsEnabled]; |
| 166 [nextButton_ setEnabled:buttonsEnabled]; | 199 [nextButton_ setEnabled:buttonsEnabled]; |
| 167 | |
| 168 } | 200 } |
| 169 | 201 |
| 170 - (void)restoreSavedFocus { | 202 - (void)restoreSavedFocus { |
| 171 if (!(focusTracker_.get() && | 203 if (!(focusTracker_.get() && |
| 172 [focusTracker_ restoreFocusInWindow:[[self view] window]])) { | 204 [focusTracker_ restoreFocusInWindow:[findBarView_ window]])) { |
| 173 // Fall back to giving focus to the tab contents. | 205 // Fall back to giving focus to the tab contents. |
| 174 findBarBridge_->GetFindBarController()->tab_contents()->Focus(); | 206 findBarBridge_->GetFindBarController()->tab_contents()->Focus(); |
| 175 } | 207 } |
| 176 focusTracker_.reset(nil); | 208 focusTracker_.reset(nil); |
| 177 } | 209 } |
| 178 | 210 |
| 179 - (void)setFindText:(const string16&)findText { | 211 - (void)setFindText:(const string16&)findText { |
| 180 [findText_ setStringValue:base::SysUTF16ToNSString(findText)]; | 212 [findText_ setStringValue:base::SysUTF16ToNSString(findText)]; |
| 181 } | 213 } |
| 182 | 214 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 [resultsLabel_ sizeToFit]; | 261 [resultsLabel_ sizeToFit]; |
| 230 labelFrame.size.width = [resultsLabel_ frame].size.width; | 262 labelFrame.size.width = [resultsLabel_ frame].size.width; |
| 231 labelFrame.origin.x = NSMaxX([findText_ frame]) - labelFrame.size.width; | 263 labelFrame.origin.x = NSMaxX([findText_ frame]) - labelFrame.size.width; |
| 232 [resultsLabel_ setFrame:labelFrame]; | 264 [resultsLabel_ setFrame:labelFrame]; |
| 233 | 265 |
| 234 // TODO(rohitrao): If the search string is too long, then it will overlap with | 266 // TODO(rohitrao): If the search string is too long, then it will overlap with |
| 235 // the results label. Fix. | 267 // the results label. Fix. |
| 236 } | 268 } |
| 237 | 269 |
| 238 - (BOOL)isFindBarVisible { | 270 - (BOOL)isFindBarVisible { |
| 239 return [[self view] isHidden] ? NO : YES; | 271 // Find bar is visible if any part of it is on the screen. |
| 272 return NSIntersectsRect([[self view] bounds], [findBarView_ frame]); | |
| 273 } | |
| 274 | |
| 275 // NSAnimation delegate methods. | |
| 276 - (void)animationDidEnd:(NSAnimation*)animation { | |
| 277 // Autorelease the animation (cannot use release because the animation object | |
| 278 // is still on the stack. | |
| 279 DCHECK(animation == currentAnimation_.get()); | |
| 280 [currentAnimation_.release() autorelease]; | |
|
pink (ping after 24hrs)
2009/09/18 13:40:12
i assume release() here doesn't actually call rele
rohitrao (ping after 24h)
2009/09/18 15:17:58
I'll file a bug against Mark =)
Mark Mentovai
2009/09/18 15:35:13
This is not what you want. Use current_animation_
| |
| 240 } | 281 } |
| 241 | 282 |
| 242 @end | 283 @end |
| 284 | |
| 285 @implementation FindBarCocoaController (PrivateMethods) | |
| 286 - (NSRect)hiddenFindBarFrame { | |
|
pink (ping after 24hrs)
2009/09/18 13:40:12
blank line between these, maybe?
rohitrao (ping after 24h)
2009/09/18 15:17:58
Done.
| |
| 287 NSRect frame = [findBarView_ frame]; | |
| 288 NSRect containerBounds = [[self view] bounds]; | |
| 289 frame.origin = NSMakePoint(NSMinX(containerBounds), NSMaxY(containerBounds)); | |
| 290 return frame; | |
| 291 } | |
| 292 | |
| 293 - (void)setFindBarFrame:(NSRect)endFrame | |
| 294 animate:(BOOL)animate | |
| 295 duration:(float)duration { | |
| 296 // Save the current frame. | |
| 297 NSRect startFrame = [findBarView_ frame]; | |
| 298 | |
| 299 // Stop any existing animations. | |
| 300 [currentAnimation_ stopAnimation]; | |
| 301 | |
| 302 if (!animate) { | |
| 303 [findBarView_ setFrame:endFrame]; | |
| 304 currentAnimation_.reset(nil); | |
| 305 return; | |
| 306 } | |
| 307 | |
| 308 // Reset the frame to what was saved above. | |
| 309 [findBarView_ setFrame:startFrame]; | |
| 310 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: | |
| 311 findBarView_, NSViewAnimationTargetKey, | |
| 312 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil]; | |
| 313 | |
| 314 currentAnimation_.reset( | |
| 315 [[NSViewAnimation alloc] | |
| 316 initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]]); | |
| 317 [currentAnimation_ setDuration:duration]; | |
| 318 [currentAnimation_ setDelegate:self]; | |
| 319 [currentAnimation_ startAnimation]; | |
| 320 } | |
| 321 | |
| 322 @end | |
| OLD | NEW |