OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/ui/views/find_bar_view.h" | 5 #include "chrome/browser/ui/views/find_bar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/number_formatting.h" | 9 #include "base/i18n/number_formatting.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 DISALLOW_COPY_AND_ASSIGN(FocusForwarderView); | 105 DISALLOW_COPY_AND_ASSIGN(FocusForwarderView); |
106 }; | 106 }; |
107 | 107 |
108 } // namespace | 108 } // namespace |
109 | 109 |
110 //////////////////////////////////////////////////////////////////////////////// | 110 //////////////////////////////////////////////////////////////////////////////// |
111 // FindBarView, public: | 111 // FindBarView, public: |
112 | 112 |
113 FindBarView::FindBarView(FindBarHost* host) | 113 FindBarView::FindBarView(FindBarHost* host) |
114 : DropdownBarView(host), | 114 : find_bar_host_(host), |
115 find_text_(new views::Textfield), | 115 find_text_(new views::Textfield), |
116 match_count_text_(new MatchCountLabel()), | 116 match_count_text_(new MatchCountLabel()), |
117 focus_forwarder_view_(new FocusForwarderView(find_text_)), | 117 focus_forwarder_view_(new FocusForwarderView(find_text_)), |
118 separator_(new views::Separator(views::Separator::VERTICAL)), | 118 separator_(new views::Separator(views::Separator::VERTICAL)), |
119 find_previous_button_(new views::VectorIconButton(this)), | 119 find_previous_button_(new views::VectorIconButton(this)), |
120 find_next_button_(new views::VectorIconButton(this)), | 120 find_next_button_(new views::VectorIconButton(this)), |
121 close_button_(new views::VectorIconButton(this)) { | 121 close_button_(new views::VectorIconButton(this)) { |
122 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); | 122 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
123 find_text_->set_default_width_in_chars(kDefaultCharWidth); | 123 find_text_->set_default_width_in_chars(kDefaultCharWidth); |
124 find_text_->set_controller(this); | 124 find_text_->set_controller(this); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 SchedulePaint(); | 236 SchedulePaint(); |
237 } | 237 } |
238 | 238 |
239 void FindBarView::ClearMatchCount() { | 239 void FindBarView::ClearMatchCount() { |
240 match_count_text_->SetText(base::string16()); | 240 match_count_text_->SetText(base::string16()); |
241 UpdateMatchCountAppearance(false); | 241 UpdateMatchCountAppearance(false); |
242 Layout(); | 242 Layout(); |
243 SchedulePaint(); | 243 SchedulePaint(); |
244 } | 244 } |
245 | 245 |
246 void FindBarView::SetFocusAndSelection(bool select_all) { | |
247 find_text_->RequestFocus(); | |
248 GetWidget()->GetInputMethod()->ShowImeIfNeeded(); | |
249 if (select_all && !find_text_->text().empty()) | |
250 find_text_->SelectAll(true); | |
251 } | |
252 | |
253 /////////////////////////////////////////////////////////////////////////////// | 246 /////////////////////////////////////////////////////////////////////////////// |
254 // FindBarView, views::View overrides: | 247 // FindBarView, views::View overrides: |
255 | 248 |
256 void FindBarView::Layout() { | 249 void FindBarView::Layout() { |
257 views::View::Layout(); | 250 views::View::Layout(); |
258 | 251 |
259 // The focus forwarder view is a hidden view that should cover the area | 252 // The focus forwarder view is a hidden view that should cover the area |
260 // between the find text box and the find button so that when the user clicks | 253 // between the find text box and the find button so that when the user clicks |
261 // in that area we focus on the find text box. | 254 // in that area we focus on the find text box. |
262 const int find_text_edge = find_text_->x() + find_text_->width(); | 255 const int find_text_edge = find_text_->x() + find_text_->width(); |
263 focus_forwarder_view_->SetBounds( | 256 focus_forwarder_view_->SetBounds( |
264 find_text_edge, find_previous_button_->y(), | 257 find_text_edge, find_previous_button_->y(), |
265 find_previous_button_->x() - find_text_edge, | 258 find_previous_button_->x() - find_text_edge, |
266 find_previous_button_->height()); | 259 find_previous_button_->height()); |
267 } | 260 } |
268 | 261 |
269 gfx::Size FindBarView::GetPreferredSize() const { | 262 gfx::Size FindBarView::GetPreferredSize() const { |
270 gfx::Size size = views::View::GetPreferredSize(); | 263 gfx::Size size = views::View::GetPreferredSize(); |
271 // Ignore the preferred size for the match count label, and just let it take | 264 // Ignore the preferred size for the match count label, and just let it take |
272 // up part of the space for the input textfield. This prevents the overall | 265 // up part of the space for the input textfield. This prevents the overall |
273 // width from changing every time the match count text changes. | 266 // width from changing every time the match count text changes. |
274 size.set_width(size.width() - match_count_text_->GetPreferredSize().width()); | 267 size.set_width(size.width() - match_count_text_->GetPreferredSize().width()); |
275 return size; | 268 return size; |
276 } | 269 } |
277 | 270 |
278 //////////////////////////////////////////////////////////////////////////////// | 271 //////////////////////////////////////////////////////////////////////////////// |
| 272 // FindBarView, DropdownBarHostDelegate implementation: |
| 273 |
| 274 void FindBarView::SetFocusAndSelection(bool select_all) { |
| 275 find_text_->RequestFocus(); |
| 276 GetWidget()->GetInputMethod()->ShowImeIfNeeded(); |
| 277 if (select_all && !find_text_->text().empty()) |
| 278 find_text_->SelectAll(true); |
| 279 } |
| 280 |
| 281 //////////////////////////////////////////////////////////////////////////////// |
279 // FindBarView, views::VectorIconButtonDelegate implementation: | 282 // FindBarView, views::VectorIconButtonDelegate implementation: |
280 | 283 |
281 void FindBarView::ButtonPressed( | 284 void FindBarView::ButtonPressed( |
282 views::Button* sender, const ui::Event& event) { | 285 views::Button* sender, const ui::Event& event) { |
283 switch (sender->id()) { | 286 switch (sender->id()) { |
284 case VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON: | 287 case VIEW_ID_FIND_IN_PAGE_PREVIOUS_BUTTON: |
285 case VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON: | 288 case VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON: |
286 if (!find_text_->text().empty()) { | 289 if (!find_text_->text().empty()) { |
287 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents( | 290 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents( |
288 find_bar_host()->GetFindBarController()->web_contents()); | 291 find_bar_host_->GetFindBarController()->web_contents()); |
289 find_tab_helper->StartFinding( | 292 find_tab_helper->StartFinding( |
290 find_text_->text(), | 293 find_text_->text(), |
291 sender->id() == VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON, | 294 sender->id() == VIEW_ID_FIND_IN_PAGE_NEXT_BUTTON, |
292 false); // Not case sensitive. | 295 false); // Not case sensitive. |
293 } | 296 } |
294 break; | 297 break; |
295 case VIEW_ID_FIND_IN_PAGE_CLOSE_BUTTON: | 298 case VIEW_ID_FIND_IN_PAGE_CLOSE_BUTTON: |
296 find_bar_host()->GetFindBarController()->EndFindSession( | 299 find_bar_host_->GetFindBarController()->EndFindSession( |
297 FindBarController::kKeepSelectionOnPage, | 300 FindBarController::kKeepSelectionOnPage, |
298 FindBarController::kKeepResultsInFindBox); | 301 FindBarController::kKeepResultsInFindBox); |
299 break; | 302 break; |
300 default: | 303 default: |
301 NOTREACHED() << "Unknown button"; | 304 NOTREACHED() << "Unknown button"; |
302 break; | 305 break; |
303 } | 306 } |
304 } | 307 } |
305 | 308 |
306 SkColor FindBarView::GetVectorIconBaseColor() const { | 309 SkColor FindBarView::GetVectorIconBaseColor() const { |
307 return GetNativeTheme()->GetSystemColor( | 310 return GetNativeTheme()->GetSystemColor( |
308 ui::NativeTheme::kColorId_TextfieldDefaultColor); | 311 ui::NativeTheme::kColorId_TextfieldDefaultColor); |
309 } | 312 } |
310 | 313 |
311 //////////////////////////////////////////////////////////////////////////////// | 314 //////////////////////////////////////////////////////////////////////////////// |
312 // FindBarView, views::TextfieldController implementation: | 315 // FindBarView, views::TextfieldController implementation: |
313 | 316 |
314 bool FindBarView::HandleKeyEvent(views::Textfield* sender, | 317 bool FindBarView::HandleKeyEvent(views::Textfield* sender, |
315 const ui::KeyEvent& key_event) { | 318 const ui::KeyEvent& key_event) { |
316 // If the dialog is not visible, there is no reason to process keyboard input. | 319 // If the dialog is not visible, there is no reason to process keyboard input. |
317 if (!host()->IsVisible()) | 320 if (!find_bar_host_->IsVisible()) |
318 return false; | 321 return false; |
319 | 322 |
320 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event)) | 323 if (find_bar_host_->MaybeForwardKeyEventToWebpage(key_event)) |
321 return true; // Handled, we are done! | 324 return true; // Handled, we are done! |
322 | 325 |
323 if (key_event.key_code() == ui::VKEY_RETURN && | 326 if (key_event.key_code() == ui::VKEY_RETURN && |
324 key_event.type() == ui::ET_KEY_PRESSED) { | 327 key_event.type() == ui::ET_KEY_PRESSED) { |
325 // Pressing Return/Enter starts the search (unless text box is empty). | 328 // Pressing Return/Enter starts the search (unless text box is empty). |
326 base::string16 find_string = find_text_->text(); | 329 base::string16 find_string = find_text_->text(); |
327 if (!find_string.empty()) { | 330 if (!find_string.empty()) { |
328 FindBarController* controller = find_bar_host()->GetFindBarController(); | 331 FindBarController* controller = find_bar_host_->GetFindBarController(); |
329 FindTabHelper* find_tab_helper = | 332 FindTabHelper* find_tab_helper = |
330 FindTabHelper::FromWebContents(controller->web_contents()); | 333 FindTabHelper::FromWebContents(controller->web_contents()); |
331 // Search forwards for enter, backwards for shift-enter. | 334 // Search forwards for enter, backwards for shift-enter. |
332 find_tab_helper->StartFinding(find_string, | 335 find_tab_helper->StartFinding(find_string, |
333 !key_event.IsShiftDown(), | 336 !key_event.IsShiftDown(), |
334 false); // Not case sensitive. | 337 false); // Not case sensitive. |
335 } | 338 } |
336 return true; | 339 return true; |
337 } | 340 } |
338 | 341 |
(...skipping 13 matching lines...) Expand all Loading... |
352 // See http://crbug.com/79002 | 355 // See http://crbug.com/79002 |
353 last_searched_text_.clear(); | 356 last_searched_text_.clear(); |
354 } | 357 } |
355 | 358 |
356 views::View* FindBarView::TargetForRect(View* root, const gfx::Rect& rect) { | 359 views::View* FindBarView::TargetForRect(View* root, const gfx::Rect& rect) { |
357 DCHECK_EQ(match_count_text_, root); | 360 DCHECK_EQ(match_count_text_, root); |
358 return find_text_; | 361 return find_text_; |
359 } | 362 } |
360 | 363 |
361 void FindBarView::Find(const base::string16& search_text) { | 364 void FindBarView::Find(const base::string16& search_text) { |
362 FindBarController* controller = find_bar_host()->GetFindBarController(); | 365 FindBarController* controller = find_bar_host_->GetFindBarController(); |
363 DCHECK(controller); | 366 DCHECK(controller); |
364 content::WebContents* web_contents = controller->web_contents(); | 367 content::WebContents* web_contents = controller->web_contents(); |
365 // We must guard against a NULL web_contents, which can happen if the text | 368 // We must guard against a NULL web_contents, which can happen if the text |
366 // in the Find box is changed right after the tab is destroyed. Otherwise, it | 369 // in the Find box is changed right after the tab is destroyed. Otherwise, it |
367 // can lead to crashes, as exposed by automation testing in issue 8048. | 370 // can lead to crashes, as exposed by automation testing in issue 8048. |
368 if (!web_contents) | 371 if (!web_contents) |
369 return; | 372 return; |
370 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); | 373 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); |
371 | 374 |
372 last_searched_text_ = search_text; | 375 last_searched_text_ = search_text; |
373 | 376 |
374 // When the user changes something in the text box we check the contents and | 377 // When the user changes something in the text box we check the contents and |
375 // if the textbox contains something we set it as the new search string and | 378 // if the textbox contains something we set it as the new search string and |
376 // initiate search (even though old searches might be in progress). | 379 // initiate search (even though old searches might be in progress). |
377 if (!search_text.empty()) { | 380 if (!search_text.empty()) { |
378 // The last two params here are forward (true) and case sensitive (false). | 381 // The last two params here are forward (true) and case sensitive (false). |
379 find_tab_helper->StartFinding(search_text, true, false); | 382 find_tab_helper->StartFinding(search_text, true, false); |
380 } else { | 383 } else { |
381 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); | 384 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); |
382 UpdateForResult(find_tab_helper->find_result(), base::string16()); | 385 UpdateForResult(find_tab_helper->find_result(), base::string16()); |
383 find_bar_host()->MoveWindowIfNecessary(gfx::Rect()); | 386 find_bar_host_->MoveWindowIfNecessary(gfx::Rect()); |
384 | 387 |
385 // Clearing the text box should clear the prepopulate state so that when | 388 // Clearing the text box should clear the prepopulate state so that when |
386 // we close and reopen the Find box it doesn't show the search we just | 389 // we close and reopen the Find box it doesn't show the search we just |
387 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged | 390 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged |
388 // sent for a lot more things than just the user nulling out the search | 391 // sent for a lot more things than just the user nulling out the search |
389 // terms. See http://crbug.com/45372. | 392 // terms. See http://crbug.com/45372. |
390 Profile* profile = | 393 Profile* profile = |
391 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 394 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
392 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); | 395 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); |
393 find_bar_state->set_last_prepopulate_text(base::string16()); | 396 find_bar_state->set_last_prepopulate_text(base::string16()); |
394 } | 397 } |
395 } | 398 } |
396 | 399 |
397 void FindBarView::UpdateMatchCountAppearance(bool no_match) { | 400 void FindBarView::UpdateMatchCountAppearance(bool no_match) { |
398 bool enable_buttons = !find_text_->text().empty() && !no_match; | 401 bool enable_buttons = !find_text_->text().empty() && !no_match; |
399 find_previous_button_->SetEnabled(enable_buttons); | 402 find_previous_button_->SetEnabled(enable_buttons); |
400 find_next_button_->SetEnabled(enable_buttons); | 403 find_next_button_->SetEnabled(enable_buttons); |
401 } | 404 } |
402 | 405 |
403 FindBarHost* FindBarView::find_bar_host() const { | |
404 return static_cast<FindBarHost*>(host()); | |
405 } | |
406 | |
407 const char* FindBarView::GetClassName() const { | 406 const char* FindBarView::GetClassName() const { |
408 return "FindBarView"; | 407 return "FindBarView"; |
409 } | 408 } |
410 | 409 |
411 void FindBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 410 void FindBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
412 SkColor bg_color = theme->GetSystemColor( | 411 SkColor bg_color = theme->GetSystemColor( |
413 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 412 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
414 auto border = base::MakeUnique<views::BubbleBorder>( | 413 auto border = base::MakeUnique<views::BubbleBorder>( |
415 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW, | 414 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW, |
416 bg_color); | 415 bg_color); |
417 set_background(new views::BubbleBackground(border.get())); | 416 set_background(new views::BubbleBackground(border.get())); |
418 SetBorder(std::move(border)); | 417 SetBorder(std::move(border)); |
419 | 418 |
420 match_count_text_->SetBackgroundColor(bg_color); | 419 match_count_text_->SetBackgroundColor(bg_color); |
421 SkColor text_color = | 420 SkColor text_color = |
422 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor); | 421 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor); |
423 match_count_text_->SetEnabledColor(SkColorSetA(text_color, 0x69)); | 422 match_count_text_->SetEnabledColor(SkColorSetA(text_color, 0x69)); |
424 separator_->SetColor(SkColorSetA(text_color, 0x26)); | 423 separator_->SetColor(SkColorSetA(text_color, 0x26)); |
425 } | 424 } |
426 | 425 |
OLD | NEW |