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

Side by Side Diff: chrome/browser/cocoa/find_bar_cocoa_controller.mm

Issue 206035: Support the OS X find pasteboard on OS X. (Closed)
Patch Set: foo Created 11 years, 2 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
OLDNEW
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/find_pasteboard.h"
15 #import "chrome/browser/cocoa/focus_tracker.h" 16 #import "chrome/browser/cocoa/focus_tracker.h"
16 #import "chrome/browser/cocoa/tab_strip_controller.h" 17 #import "chrome/browser/cocoa/tab_strip_controller.h"
17 #include "chrome/browser/tab_contents/tab_contents.h" 18 #include "chrome/browser/tab_contents/tab_contents.h"
18 19
19 namespace { 20 namespace {
20 static float kFindBarOpenDuration = 0.2; 21 static float kFindBarOpenDuration = 0.2;
21 static float kFindBarCloseDuration = 0.15; 22 static float kFindBarCloseDuration = 0.15;
22 } 23 }
23 24
24 @interface FindBarCocoaController (PrivateMethods) 25 @interface FindBarCocoaController (PrivateMethods)
25 // Returns the appropriate frame for a hidden find bar. 26 // Returns the appropriate frame for a hidden find bar.
26 - (NSRect)hiddenFindBarFrame; 27 - (NSRect)hiddenFindBarFrame;
27 28
28 // Sets the frame of |findBarView_|. |duration| is ignored if |animate| is NO. 29 // Sets the frame of |findBarView_|. |duration| is ignored if |animate| is NO.
29 - (void)setFindBarFrame:(NSRect)endFrame 30 - (void)setFindBarFrame:(NSRect)endFrame
30 animate:(BOOL)animate 31 animate:(BOOL)animate
31 duration:(float)duration; 32 duration:(float)duration;
32 @end 33 @end
33 34
34 @implementation FindBarCocoaController 35 @implementation FindBarCocoaController
35 36
36 - (id)init { 37 - (id)init {
37 if ((self = [super initWithNibName:@"FindBar" 38 if ((self = [super initWithNibName:@"FindBar"
38 bundle:mac_util::MainAppBundle()])) { 39 bundle:mac_util::MainAppBundle()])) {
40 [[NSNotificationCenter defaultCenter]
41 addObserver:self
42 selector:@selector(findPboardUpdated:)
43 name:kFindPasteboardChangedNotification
44 object:[FindPasteboard sharedInstance]];
39 } 45 }
40 return self; 46 return self;
41 } 47 }
42 48
43 - (void)dealloc { 49 - (void)dealloc {
44 // All animations should be explicitly stopped by the TabContents before a tab 50 // All animations should be explicitly stopped by the TabContents before a tab
45 // is closed. 51 // is closed.
46 DCHECK(!currentAnimation_.get()); 52 DCHECK(!currentAnimation_.get());
53 [[NSNotificationCenter defaultCenter] removeObserver:self];
47 [super dealloc]; 54 [super dealloc];
48 } 55 }
49 56
50 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge { 57 - (void)setFindBarBridge:(FindBarBridge*)findBarBridge {
51 DCHECK(!findBarBridge_); // should only be called once. 58 DCHECK(!findBarBridge_); // should only be called once.
52 findBarBridge_ = findBarBridge; 59 findBarBridge_ = findBarBridge;
53 } 60 }
54 61
55 - (void)awakeFromNib { 62 - (void)awakeFromNib {
56 [findBarView_ setFrame:[self hiddenFindBarFrame]]; 63 [findBarView_ setFrame:[self hiddenFindBarFrame]];
(...skipping 11 matching lines...) Expand all
68 false, false); 75 false, false);
69 } 76 }
70 77
71 - (IBAction)nextResult:(id)sender { 78 - (IBAction)nextResult:(id)sender {
72 if (findBarBridge_) 79 if (findBarBridge_)
73 findBarBridge_->GetFindBarController()->tab_contents()->StartFinding( 80 findBarBridge_->GetFindBarController()->tab_contents()->StartFinding(
74 base::SysNSStringToUTF16([findText_ stringValue]), 81 base::SysNSStringToUTF16([findText_ stringValue]),
75 true, false); 82 true, false);
76 } 83 }
77 84
85 - (void)findPboardUpdated:(NSNotification*)notification {
86 [self setFindText:[[FindPasteboard sharedInstance] findText]];
87 }
88
78 // Positions the find bar container view in the correct location based on the 89 // Positions the find bar container view in the correct location based on the
79 // current state of the window. The find bar container is always positioned one 90 // current state of the window. The find bar container is always positioned one
80 // pixel above the infobar container. Note that we are using the infobar 91 // pixel above the infobar container. Note that we are using the infobar
81 // container location as a proxy for the toolbar location, but we cannot 92 // container location as a proxy for the toolbar location, but we cannot
82 // position based on the toolbar because the toolbar is not always present (for 93 // position based on the toolbar because the toolbar is not always present (for
83 // example in fullscreen windows). 94 // example in fullscreen windows).
84 - (void)positionFindBarView:(NSView*)infoBarContainerView { 95 - (void)positionFindBarView:(NSView*)infoBarContainerView {
85 static const int kRightEdgeOffset = 25; 96 static const int kRightEdgeOffset = 25;
86 NSView* containerView = [self view]; 97 NSView* containerView = [self view];
87 int containerHeight = NSHeight([containerView frame]); 98 int containerHeight = NSHeight([containerView frame]);
(...skipping 15 matching lines...) Expand all
103 // NSControl delegate method. 114 // NSControl delegate method.
104 - (void)controlTextDidChange:(NSNotification *)aNotification { 115 - (void)controlTextDidChange:(NSNotification *)aNotification {
105 if (!findBarBridge_) 116 if (!findBarBridge_)
106 return; 117 return;
107 118
108 TabContents* tab_contents = 119 TabContents* tab_contents =
109 findBarBridge_->GetFindBarController()->tab_contents(); 120 findBarBridge_->GetFindBarController()->tab_contents();
110 if (!tab_contents) 121 if (!tab_contents)
111 return; 122 return;
112 123
113 string16 findText = base::SysNSStringToUTF16([findText_ stringValue]); 124 NSString* findText = [findText_ stringValue];
114 if (findText.length() > 0) { 125 [[FindPasteboard sharedInstance] setFindText:findText];
115 tab_contents->StartFinding(findText, true, false); 126
127 if ([findText length] > 0) {
128 tab_contents->StartFinding(base::SysNSStringToUTF16(findText), true, false);
116 } else { 129 } else {
117 // The textbox is empty so we reset. 130 // The textbox is empty so we reset.
118 tab_contents->StopFinding(true); // true = clear selection on page. 131 tab_contents->StopFinding(true); // true = clear selection on page.
119 [self updateUIForFindResult:tab_contents->find_result() 132 [self updateUIForFindResult:tab_contents->find_result()
120 withText:string16()]; 133 withText:string16()];
121 } 134 }
122 } 135 }
123 136
124 // NSControl delegate method 137 // NSControl delegate method
125 - (BOOL)control:(NSControl*)control 138 - (BOOL)control:(NSControl*)control
126 textView:(NSTextView*)textView 139 textView:(NSTextView*)textView
127 doCommandBySelector:(SEL)command { 140 doCommandBySelector:(SEL)command {
128 if (command == @selector(insertNewline:)) { 141 if (command == @selector(insertNewline:)) {
129 NSEvent* event = [NSApp currentEvent]; 142 NSEvent* event = [NSApp currentEvent];
130 143
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 216
204 - (void)restoreSavedFocus { 217 - (void)restoreSavedFocus {
205 if (!(focusTracker_.get() && 218 if (!(focusTracker_.get() &&
206 [focusTracker_ restoreFocusInWindow:[findBarView_ window]])) { 219 [focusTracker_ restoreFocusInWindow:[findBarView_ window]])) {
207 // Fall back to giving focus to the tab contents. 220 // Fall back to giving focus to the tab contents.
208 findBarBridge_->GetFindBarController()->tab_contents()->Focus(); 221 findBarBridge_->GetFindBarController()->tab_contents()->Focus();
209 } 222 }
210 focusTracker_.reset(nil); 223 focusTracker_.reset(nil);
211 } 224 }
212 225
213 - (void)setFindText:(const string16&)findText { 226 - (void)setFindText:(NSString*)findText {
214 [findText_ setStringValue:base::SysUTF16ToNSString(findText)]; 227 [findText_ setStringValue:findText];
228
229 // Make sure the text in the find bar always ends up in the find pasteboard
230 // (and, via notifications, in the other find bars too).
231 [[FindPasteboard sharedInstance] setFindText:findText];
215 } 232 }
216 233
217 - (void)clearResults:(const FindNotificationDetails&)results { 234 - (void)clearResults:(const FindNotificationDetails&)results {
218 // Just call updateUIForFindResult, which will take care of clearing 235 // Just call updateUIForFindResult, which will take care of clearing
219 // the search text and the results label. 236 // the search text and the results label.
220 [self updateUIForFindResult:results withText:string16()]; 237 [self updateUIForFindResult:results withText:string16()];
221 } 238 }
222 239
223 - (void)updateUIForFindResult:(const FindNotificationDetails&)result 240 - (void)updateUIForFindResult:(const FindNotificationDetails&)result
224 withText:(const string16&)findText { 241 withText:(const string16&)findText {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 // Resize |resultsLabel_| to completely contain its string and right-justify 276 // Resize |resultsLabel_| to completely contain its string and right-justify
260 // it within |findText_|. sizeToFit may shrink the frame vertically, which we 277 // it within |findText_|. sizeToFit may shrink the frame vertically, which we
261 // don't want, so we save the original vertical positioning. 278 // don't want, so we save the original vertical positioning.
262 NSRect labelFrame = [resultsLabel_ frame]; 279 NSRect labelFrame = [resultsLabel_ frame];
263 [resultsLabel_ sizeToFit]; 280 [resultsLabel_ sizeToFit];
264 labelFrame.size.width = [resultsLabel_ frame].size.width; 281 labelFrame.size.width = [resultsLabel_ frame].size.width;
265 labelFrame.origin.x = NSMaxX([findText_ frame]) - labelFrame.size.width; 282 labelFrame.origin.x = NSMaxX([findText_ frame]) - labelFrame.size.width;
266 [resultsLabel_ setFrame:labelFrame]; 283 [resultsLabel_ setFrame:labelFrame];
267 284
268 // TODO(rohitrao): If the search string is too long, then it will overlap with 285 // TODO(rohitrao): If the search string is too long, then it will overlap with
269 // the results label. Fix. 286 // the results label. Fix. Perhaps use the code that fades out the tab titles
287 // if they are too long.
270 } 288 }
271 289
272 - (BOOL)isFindBarVisible { 290 - (BOOL)isFindBarVisible {
273 // Find bar is visible if any part of it is on the screen. 291 // Find bar is visible if any part of it is on the screen.
274 return NSIntersectsRect([[self view] bounds], [findBarView_ frame]); 292 return NSIntersectsRect([[self view] bounds], [findBarView_ frame]);
275 } 293 }
276 294
277 // NSAnimation delegate methods. 295 // NSAnimation delegate methods.
278 - (void)animationDidEnd:(NSAnimation*)animation { 296 - (void)animationDidEnd:(NSAnimation*)animation {
279 // Autorelease the animation (cannot use release because the animation object 297 // Autorelease the animation (cannot use release because the animation object
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 334
317 currentAnimation_.reset( 335 currentAnimation_.reset(
318 [[NSViewAnimation alloc] 336 [[NSViewAnimation alloc]
319 initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]]); 337 initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]]);
320 [currentAnimation_ setDuration:duration]; 338 [currentAnimation_ setDuration:duration];
321 [currentAnimation_ setDelegate:self]; 339 [currentAnimation_ setDelegate:self];
322 [currentAnimation_ startAnimation]; 340 [currentAnimation_ startAnimation];
323 } 341 }
324 342
325 @end 343 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/find_bar_cocoa_controller.h ('k') | chrome/browser/cocoa/find_bar_cocoa_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698