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/tab_strip_controller.h" | 16 #import "chrome/browser/cocoa/tab_strip_controller.h" |
16 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
17 | 18 |
18 @implementation FindBarCocoaController | 19 @implementation FindBarCocoaController |
19 | 20 |
20 - (id)init { | 21 - (id)init { |
21 if ((self = [super initWithNibName:@"FindBar" | 22 if ((self = [super initWithNibName:@"FindBar" |
22 bundle:mac_util::MainAppBundle()])) { | 23 bundle:mac_util::MainAppBundle()])) { |
23 } | 24 } |
24 return self; | 25 return self; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 114 |
114 return YES; | 115 return YES; |
115 } | 116 } |
116 | 117 |
117 return NO; | 118 return NO; |
118 } | 119 } |
119 | 120 |
120 // Methods from FindBar | 121 // Methods from FindBar |
121 - (void)showFindBar { | 122 - (void)showFindBar { |
122 [[self view] setHidden:NO]; | 123 [[self view] setHidden:NO]; |
| 124 |
| 125 // Save the currently-focused view. |[self view]| is in the view |
| 126 // hierarchy by now. showFindBar can be called even when the |
| 127 // findbar is already open, so do not overwrite an already saved |
| 128 // view. |
| 129 if (!focusTracker_.get()) |
| 130 focusTracker_.reset( |
| 131 [[FocusTracker alloc] initWithWindow:[[self view] window]]); |
123 } | 132 } |
124 | 133 |
125 - (void)hideFindBar { | 134 - (void)hideFindBar { |
126 [[self view] setHidden:YES]; | 135 [[self view] setHidden:YES]; |
127 } | 136 } |
128 | 137 |
129 - (void)setFocusAndSelection { | 138 - (void)setFocusAndSelection { |
130 [[findText_ window] makeFirstResponder:findText_]; | 139 [[findText_ window] makeFirstResponder:findText_]; |
131 | 140 |
132 // Enable the buttons if the find text is non-empty. | 141 // Enable the buttons if the find text is non-empty. |
133 BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO; | 142 BOOL buttonsEnabled = ([[findText_ stringValue] length] > 0) ? YES : NO; |
134 [previousButton_ setEnabled:buttonsEnabled]; | 143 [previousButton_ setEnabled:buttonsEnabled]; |
135 [nextButton_ setEnabled:buttonsEnabled]; | 144 [nextButton_ setEnabled:buttonsEnabled]; |
136 | 145 |
137 } | 146 } |
138 | 147 |
| 148 - (void)restoreSavedFocus { |
| 149 if (!(focusTracker_.get() && |
| 150 [focusTracker_ restoreFocusInWindow:[[self view] window]])) { |
| 151 // Fall back to giving focus to the tab contents. |
| 152 findBarBridge_->GetFindBarController()->tab_contents()->Focus(); |
| 153 } |
| 154 focusTracker_.reset(nil); |
| 155 } |
| 156 |
139 - (void)setFindText:(const string16&)findText { | 157 - (void)setFindText:(const string16&)findText { |
140 [findText_ setStringValue:base::SysUTF16ToNSString(findText)]; | 158 [findText_ setStringValue:base::SysUTF16ToNSString(findText)]; |
141 } | 159 } |
142 | 160 |
143 - (void)clearResults:(const FindNotificationDetails&)results { | 161 - (void)clearResults:(const FindNotificationDetails&)results { |
144 // Just call updateUIForFindResult, which will take care of clearing | 162 // Just call updateUIForFindResult, which will take care of clearing |
145 // the search text and the results label. | 163 // the search text and the results label. |
146 [self updateUIForFindResult:results withText:string16()]; | 164 [self updateUIForFindResult:results withText:string16()]; |
147 } | 165 } |
148 | 166 |
(...skipping 21 matching lines...) Expand all Loading... |
170 [resultsLabel_ setStringValue:base::SysWideToNSString( | 188 [resultsLabel_ setStringValue:base::SysWideToNSString( |
171 l10n_util::GetStringF(IDS_FIND_IN_PAGE_COUNT, | 189 l10n_util::GetStringF(IDS_FIND_IN_PAGE_COUNT, |
172 IntToWString(result.active_match_ordinal()), | 190 IntToWString(result.active_match_ordinal()), |
173 IntToWString(result.number_of_matches())))]; | 191 IntToWString(result.number_of_matches())))]; |
174 } else { | 192 } else { |
175 // If there was no text entered, we don't show anything in the result count | 193 // If there was no text entered, we don't show anything in the result count |
176 // area. | 194 // area. |
177 [resultsLabel_ setStringValue:@""]; | 195 [resultsLabel_ setStringValue:@""]; |
178 } | 196 } |
179 | 197 |
| 198 // If we found any results, reset the focus tracker, so we always |
| 199 // restore focus to the tab contents. |
| 200 if (result.number_of_matches() > 0) |
| 201 focusTracker_.reset(nil); |
| 202 |
180 // Resize |resultsLabel_| to completely contain its string and right-justify | 203 // Resize |resultsLabel_| to completely contain its string and right-justify |
181 // it within |findText_|. sizeToFit may shrink the frame vertically, which we | 204 // it within |findText_|. sizeToFit may shrink the frame vertically, which we |
182 // don't want, so we save the original vertical positioning. | 205 // don't want, so we save the original vertical positioning. |
183 NSRect labelFrame = [resultsLabel_ frame]; | 206 NSRect labelFrame = [resultsLabel_ frame]; |
184 [resultsLabel_ sizeToFit]; | 207 [resultsLabel_ sizeToFit]; |
185 labelFrame.size.width = [resultsLabel_ frame].size.width; | 208 labelFrame.size.width = [resultsLabel_ frame].size.width; |
186 labelFrame.origin.x = NSMaxX([findText_ frame]) - labelFrame.size.width; | 209 labelFrame.origin.x = NSMaxX([findText_ frame]) - labelFrame.size.width; |
187 [resultsLabel_ setFrame:labelFrame]; | 210 [resultsLabel_ setFrame:labelFrame]; |
188 | 211 |
189 // TODO(rohitrao): If the search string is too long, then it will overlap with | 212 // TODO(rohitrao): If the search string is too long, then it will overlap with |
190 // the results label. Fix. | 213 // the results label. Fix. |
191 } | 214 } |
192 | 215 |
193 - (BOOL)isFindBarVisible { | 216 - (BOOL)isFindBarVisible { |
194 return [[self view] isHidden] ? NO : YES; | 217 return [[self view] isHidden] ? NO : YES; |
195 } | 218 } |
196 | 219 |
197 @end | 220 @end |
OLD | NEW |