| 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 "base/mac_util.h" | 7 #include "base/mac_util.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "chrome/browser/find_bar_controller.h" | 9 #include "chrome/browser/find_bar_controller.h" |
| 10 #include "chrome/browser/cocoa/browser_window_cocoa.h" | 10 #include "chrome/browser/cocoa/browser_window_cocoa.h" |
| 11 #import "chrome/browser/cocoa/find_bar_cocoa_controller.h" | 11 #import "chrome/browser/cocoa/find_bar_cocoa_controller.h" |
| 12 #import "chrome/browser/cocoa/find_bar_bridge.h" | 12 #import "chrome/browser/cocoa/find_bar_bridge.h" |
| 13 #import "chrome/browser/cocoa/find_bar_text_field.h" | 13 #import "chrome/browser/cocoa/find_bar_text_field.h" |
| 14 #import "chrome/browser/cocoa/find_bar_text_field_cell.h" | 14 #import "chrome/browser/cocoa/find_bar_text_field_cell.h" |
| 15 #import "chrome/browser/cocoa/find_pasteboard.h" | 15 #import "chrome/browser/cocoa/find_pasteboard.h" |
| 16 #import "chrome/browser/cocoa/focus_tracker.h" | 16 #import "chrome/browser/cocoa/focus_tracker.h" |
| 17 #import "chrome/browser/cocoa/tab_strip_controller.h" | 17 #import "chrome/browser/cocoa/tab_strip_controller.h" |
| 18 #include "chrome/browser/renderer_host/render_view_host.h" | 18 #include "chrome/browser/renderer_host/render_view_host.h" |
| 19 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
| 20 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" | 20 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" |
| 21 | 21 |
| 22 #include "chrome/browser/browser_theme_provider.h" |
| 23 #import "chrome/browser/cocoa/themed_window.h" |
| 24 |
| 22 namespace { | 25 namespace { |
| 23 const float kFindBarOpenDuration = 0.2; | 26 const float kFindBarOpenDuration = 0.2; |
| 24 const float kFindBarCloseDuration = 0.15; | 27 const float kFindBarCloseDuration = 0.15; |
| 25 } | 28 } |
| 26 | 29 |
| 30 @interface FindBarWindow : NSPanel |
| 31 @end |
| 32 |
| 33 @implementation FindBarWindow |
| 34 - (BOOL)canBecomeKeyWindow { |
| 35 return YES; |
| 36 } |
| 37 |
| 38 - (ThemeProvider*)themeProvider { |
| 39 id delegate = [self parentWindow]; |
| 40 if (![delegate respondsToSelector:@selector(themeProvider)]) |
| 41 return NULL; |
| 42 return [delegate themeProvider]; |
| 43 } |
| 44 |
| 45 - (ThemedWindowStyle)themedWindowStyle { |
| 46 id delegate = [self parentWindow]; |
| 47 if (![delegate respondsToSelector:@selector(themedWindowStyle)]) |
| 48 return THEMED_NORMAL; |
| 49 return [delegate themedWindowStyle]; |
| 50 } |
| 51 |
| 52 - (NSPoint)themePatternPhase { |
| 53 id delegate = [self parentWindow]; |
| 54 if (![delegate respondsToSelector:@selector(themePatternPhase)]) |
| 55 return NSMakePoint(0, 0); |
| 56 NSPoint phase = [delegate themePatternPhase]; |
| 57 NSPoint origin = [self frame].origin; |
| 58 NSPoint parentOrigin = [[self parentWindow] frame].origin; |
| 59 phase.x += (parentOrigin.x - origin.x); |
| 60 phase.y += (parentOrigin.y - origin.y); |
| 61 return phase; |
| 62 } |
| 63 @end |
| 64 |
| 65 |
| 27 @interface FindBarCocoaController (PrivateMethods) | 66 @interface FindBarCocoaController (PrivateMethods) |
| 28 // Returns the appropriate frame for a hidden find bar. | 67 // Returns the appropriate frame for a hidden find bar. |
| 29 - (NSRect)hiddenFindBarFrame; | 68 - (NSRect)hiddenFindBarFrame; |
| 30 | 69 |
| 31 // Sets the frame of |findBarView_|. |duration| is ignored if |animate| is NO. | 70 // Sets the frame of |findBarView_|. |duration| is ignored if |animate| is NO. |
| 32 - (void)setFindBarFrame:(NSRect)endFrame | 71 - (void)setFindBarFrame:(NSRect)endFrame |
| 33 animate:(BOOL)animate | 72 animate:(BOOL)animate |
| 34 duration:(float)duration; | 73 duration:(float)duration; |
| 35 | 74 |
| 36 // Optionally stops the current search, puts |text| into the find bar, and | 75 // Optionally stops the current search, puts |text| into the find bar, and |
| 37 // enables the buttons, but doesn't start a new search for |text|. | 76 // enables the buttons, but doesn't start a new search for |text|. |
| 38 - (void)prepopulateText:(NSString*)text stopSearch:(BOOL)stopSearch; | 77 - (void)prepopulateText:(NSString*)text stopSearch:(BOOL)stopSearch; |
| 39 @end | 78 @end |
| 40 | 79 |
| 41 @implementation FindBarCocoaController | 80 @implementation FindBarCocoaController |
| 42 | 81 |
| 43 - (id)init { | 82 - (id)init { |
| 44 if ((self = [super initWithNibName:@"FindBar" | 83 if ((self = [super initWithNibName:@"FindBar" |
| 45 bundle:mac_util::MainAppBundle()])) { | 84 bundle:mac_util::MainAppBundle()])) { |
| 85 NSRect windowFrame = [[self view] frame]; |
| 86 window_.reset([[FindBarWindow alloc] initWithContentRect:windowFrame |
| 87 styleMask:NSBorderlessWindowMask|NSNona
ctivatingPanelMask |
| 88 backing:NSBackingStoreBuffered |
| 89 defer:YES]); |
| 90 [window_ setMovableByWindowBackground:NO]; |
| 91 [window_ setBackgroundColor:[NSColor clearColor]]; |
| 92 [window_ setLevel:NSNormalWindowLevel]; |
| 93 [window_ setOpaque:NO]; |
| 94 [window_ setHasShadow:NO]; |
| 95 |
| 96 //[[window_ contentView] addSubview:[self view]]; |
| 97 [window_ setContentView:[self view]]; |
| 98 |
| 46 [[NSNotificationCenter defaultCenter] | 99 [[NSNotificationCenter defaultCenter] |
| 47 addObserver:self | 100 addObserver:self |
| 48 selector:@selector(findPboardUpdated:) | 101 selector:@selector(findPboardUpdated:) |
| 49 name:kFindPasteboardChangedNotification | 102 name:kFindPasteboardChangedNotification |
| 50 object:[FindPasteboard sharedInstance]]; | 103 object:[FindPasteboard sharedInstance]]; |
| 51 } | 104 } |
| 52 return self; | 105 return self; |
| 53 } | 106 } |
| 54 | 107 |
| 55 - (void)dealloc { | 108 - (void)dealloc { |
| 56 // All animations should be explicitly stopped by the TabContents before a tab | 109 // All animations should be explicitly stopped by the TabContents before a tab |
| 57 // is closed. | 110 // is closed. |
| 58 DCHECK(!currentAnimation_.get()); | 111 DCHECK(!currentAnimation_.get()); |
| 112 [[window_ parentWindow] removeChildWindow:window_]; |
| 59 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 113 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 60 [super dealloc]; | 114 [super dealloc]; |
| 61 } | 115 } |
| 62 | 116 |
| 117 - (NSWindow*)window { |
| 118 return window_.get(); |
| 119 } |
| 120 |
| 63 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge { | 121 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge { |
| 64 DCHECK(!findBarBridge_); // should only be called once. | 122 DCHECK(!findBarBridge_); // should only be called once. |
| 65 findBarBridge_ = findBarBridge; | 123 findBarBridge_ = findBarBridge; |
| 66 } | 124 } |
| 67 | 125 |
| 68 - (void)awakeFromNib { | 126 - (void)awakeFromNib { |
| 69 [findBarView_ setFrame:[self hiddenFindBarFrame]]; | 127 [findBarView_ setFrame:[self hiddenFindBarFrame]]; |
| 70 | 128 |
| 71 // Stopping the search requires a findbar controller, which isn't valid yet | 129 // Stopping the search requires a findbar controller, which isn't valid yet |
| 72 // during setup. Furthermore, there is no active search yet anyway. | 130 // during setup. Furthermore, there is no active search yet anyway. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 95 } | 153 } |
| 96 | 154 |
| 97 - (void)findPboardUpdated:(NSNotification*)notification { | 155 - (void)findPboardUpdated:(NSNotification*)notification { |
| 98 if (suppressPboardUpdateActions_) | 156 if (suppressPboardUpdateActions_) |
| 99 return; | 157 return; |
| 100 [self prepopulateText:[[FindPasteboard sharedInstance] findText] | 158 [self prepopulateText:[[FindPasteboard sharedInstance] findText] |
| 101 stopSearch:YES]; | 159 stopSearch:YES]; |
| 102 } | 160 } |
| 103 | 161 |
| 104 - (void)positionFindBarViewAtMaxY:(CGFloat)maxY maxWidth:(CGFloat)maxWidth { | 162 - (void)positionFindBarViewAtMaxY:(CGFloat)maxY maxWidth:(CGFloat)maxWidth { |
| 163 NSWindow* parent = [window_ parentWindow]; |
| 164 |
| 105 static const CGFloat kRightEdgeOffset = 25; | 165 static const CGFloat kRightEdgeOffset = 25; |
| 106 NSView* containerView = [self view]; | 166 NSView* containerView = [self view]; |
| 107 CGFloat containerHeight = NSHeight([containerView frame]); | 167 CGFloat containerHeight = NSHeight([containerView frame]); |
| 108 CGFloat containerWidth = NSWidth([containerView frame]); | 168 CGFloat containerWidth = NSWidth([containerView frame]); |
| 109 | 169 |
| 110 // Adjust where we'll actually place the find bar. | 170 // Adjust where we'll actually place the find bar. |
| 111 CGFloat maxX = maxWidth - kRightEdgeOffset; | 171 CGFloat maxX = maxWidth - kRightEdgeOffset; |
| 112 DLOG_IF(WARNING, maxX < 0) << "Window too narrow for find bar"; | 172 DLOG_IF(WARNING, maxX < 0) << "Window too narrow for find bar"; |
| 113 maxY += 1; | 173 maxY += 1; |
| 114 | 174 |
| 115 NSRect newFrame = NSMakeRect(maxX - containerWidth, maxY - containerHeight, | 175 NSRect newFrame = NSMakeRect(maxX - containerWidth, maxY - containerHeight, |
| 116 containerWidth, containerHeight); | 176 containerWidth, containerHeight); |
| 177 NSPoint screenOrigin = [parent convertBaseToScreen:newFrame.origin]; |
| 178 newFrame.origin = screenOrigin; |
| 179 [window_ setFrame:newFrame display:YES]; |
| 180 return; |
| 181 |
| 117 [containerView setFrame:newFrame]; | 182 [containerView setFrame:newFrame]; |
| 118 } | 183 } |
| 119 | 184 |
| 120 // NSControl delegate method. | 185 // NSControl delegate method. |
| 121 - (void)controlTextDidChange:(NSNotification *)aNotification { | 186 - (void)controlTextDidChange:(NSNotification *)aNotification { |
| 122 if (!findBarBridge_) | 187 if (!findBarBridge_) |
| 123 return; | 188 return; |
| 124 | 189 |
| 125 TabContents* tab_contents = | 190 TabContents* tab_contents = |
| 126 findBarBridge_->GetFindBarController()->tab_contents(); | 191 findBarBridge_->GetFindBarController()->tab_contents(); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 285 |
| 221 - (void)stopAnimation { | 286 - (void)stopAnimation { |
| 222 if (currentAnimation_.get()) { | 287 if (currentAnimation_.get()) { |
| 223 [currentAnimation_ stopAnimation]; | 288 [currentAnimation_ stopAnimation]; |
| 224 currentAnimation_.reset(nil); | 289 currentAnimation_.reset(nil); |
| 225 } | 290 } |
| 226 } | 291 } |
| 227 | 292 |
| 228 - (void)setFocusAndSelection { | 293 - (void)setFocusAndSelection { |
| 229 [[findText_ window] makeFirstResponder:findText_]; | 294 [[findText_ window] makeFirstResponder:findText_]; |
| 295 [[findText_ window] makeKeyWindow]; |
| 230 | 296 |
| 231 // Enable the buttons if the find text is non-empty. | 297 // Enable the buttons if the find text is non-empty. |
| 232 BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO; | 298 BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO; |
| 233 [previousButton_ setEnabled:buttonsEnabled]; | 299 [previousButton_ setEnabled:buttonsEnabled]; |
| 234 [nextButton_ setEnabled:buttonsEnabled]; | 300 [nextButton_ setEnabled:buttonsEnabled]; |
| 235 } | 301 } |
| 236 | 302 |
| 237 - (void)restoreSavedFocus { | 303 - (void)restoreSavedFocus { |
| 238 if (!(focusTracker_.get() && | 304 if (!(focusTracker_.get() && |
| 239 [focusTracker_ restoreFocusInWindow:[findBarView_ window]])) { | 305 [focusTracker_ restoreFocusInWindow:[findBarView_ window]])) { |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 } | 441 } |
| 376 } | 442 } |
| 377 | 443 |
| 378 // Has to happen after |ClearResults()| above. | 444 // Has to happen after |ClearResults()| above. |
| 379 BOOL buttonsEnabled = [text length] > 0 ? YES : NO; | 445 BOOL buttonsEnabled = [text length] > 0 ? YES : NO; |
| 380 [previousButton_ setEnabled:buttonsEnabled]; | 446 [previousButton_ setEnabled:buttonsEnabled]; |
| 381 [nextButton_ setEnabled:buttonsEnabled]; | 447 [nextButton_ setEnabled:buttonsEnabled]; |
| 382 } | 448 } |
| 383 | 449 |
| 384 @end | 450 @end |
| OLD | NEW |