find_bar_view.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/views/find_bar_view.h" 6 7#include <algorithm> 8 9#include "base/strings/string_number_conversions.h" 10#include "base/strings/string_util.h" 11#include "base/strings/utf_string_conversions.h" 12#include "chrome/browser/profiles/profile.h" 13#include "chrome/browser/themes/theme_properties.h" 14#include "chrome/browser/ui/find_bar/find_bar_controller.h" 15#include "chrome/browser/ui/find_bar/find_bar_state.h" 16#include "chrome/browser/ui/find_bar/find_bar_state_factory.h" 17#include "chrome/browser/ui/find_bar/find_notification_details.h" 18#include "chrome/browser/ui/find_bar/find_tab_helper.h" 19#include "chrome/browser/ui/view_ids.h" 20#include "chrome/browser/ui/views/find_bar_host.h" 21#include "chrome/browser/ui/views/frame/browser_view.h" 22#include "grit/generated_resources.h" 23#include "grit/theme_resources.h" 24#include "grit/ui_resources.h" 25#include "third_party/skia/include/effects/SkGradientShader.h" 26#include "ui/base/l10n/l10n_util.h" 27#include "ui/base/resource/resource_bundle.h" 28#include "ui/base/theme_provider.h" 29#include "ui/events/event.h" 30#include "ui/gfx/canvas.h" 31#include "ui/views/controls/button/image_button.h" 32#include "ui/views/controls/label.h" 33#include "ui/views/controls/textfield/textfield.h" 34#include "ui/views/widget/widget.h" 35 36// The amount of whitespace to have before the find button. 37static const int kWhiteSpaceAfterMatchCountLabel = 1; 38 39// The margins around the search field and the close button. 40static const int kMarginLeftOfCloseButton = 3; 41static const int kMarginRightOfCloseButton = 7; 42static const int kMarginLeftOfFindTextfield = 12; 43 44// The margins around the match count label (We add extra space so that the 45// background highlight extends beyond just the text). 46static const int kMatchCountExtraWidth = 9; 47 48// Minimum width for the match count label. 49static const int kMatchCountMinWidth = 30; 50 51// The text color for the match count label. 52static const SkColor kTextColorMatchCount = SkColorSetRGB(178, 178, 178); 53 54// The text color for the match count label when no matches are found. 55static const SkColor kTextColorNoMatch = SK_ColorBLACK; 56 57// The background color of the match count label when results are found. 58static const SkColor kBackgroundColorMatch = SkColorSetARGB(0, 255, 255, 255); 59 60// The background color of the match count label when no results are found. 61static const SkColor kBackgroundColorNoMatch = SkColorSetRGB(255, 102, 102); 62 63// The default number of average characters that the text box will be. This 64// number brings the width on a "regular fonts" system to about 300px. 65static const int kDefaultCharWidth = 43; 66 67//////////////////////////////////////////////////////////////////////////////// 68// FindBarView, public: 69 70FindBarView::FindBarView(FindBarHost* host) 71 : DropdownBarView(host), 72 find_text_(NULL), 73 match_count_text_(NULL), 74 focus_forwarder_view_(NULL), 75 find_previous_button_(NULL), 76 find_next_button_(NULL), 77 close_button_(NULL), 78 text_box_background_(NULL), 79 text_box_background_left_(NULL) { 80 set_id(VIEW_ID_FIND_IN_PAGE); 81 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 82 83 find_text_ = new views::Textfield; 84 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); 85 find_text_->set_default_width_in_chars(kDefaultCharWidth); 86 find_text_->set_controller(this); 87 find_text_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FIND)); 88 // The find bar textfield has a background image instead of a border. 89 find_text_->SetBorder(views::Border::NullBorder()); 90 AddChildView(find_text_); 91 92 match_count_text_ = new views::Label(); 93 AddChildView(match_count_text_); 94 95 // Create a focus forwarder view which sends focus to find_text_. 96 focus_forwarder_view_ = new FocusForwarderView(find_text_); 97 AddChildView(focus_forwarder_view_); 98 99 find_previous_button_ = new views::ImageButton(this); 100 find_previous_button_->set_tag(FIND_PREVIOUS_TAG); 101 find_previous_button_->SetFocusable(true); 102 find_previous_button_->SetImage(views::CustomButton::STATE_NORMAL, 103 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV)); 104 find_previous_button_->SetImage(views::CustomButton::STATE_HOVERED, 105 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_H)); 106 find_previous_button_->SetImage(views::CustomButton::STATE_PRESSED, 107 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_P)); 108 find_previous_button_->SetImage(views::CustomButton::STATE_DISABLED, 109 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_D)); 110 find_previous_button_->SetTooltipText( 111 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP)); 112 find_previous_button_->SetAccessibleName( 113 l10n_util::GetStringUTF16(IDS_ACCNAME_PREVIOUS)); 114 AddChildView(find_previous_button_); 115 116 find_next_button_ = new views::ImageButton(this); 117 find_next_button_->set_tag(FIND_NEXT_TAG); 118 find_next_button_->SetFocusable(true); 119 find_next_button_->SetImage(views::CustomButton::STATE_NORMAL, 120 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT)); 121 find_next_button_->SetImage(views::CustomButton::STATE_HOVERED, 122 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_H)); 123 find_next_button_->SetImage(views::CustomButton::STATE_PRESSED, 124 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_P)); 125 find_next_button_->SetImage(views::CustomButton::STATE_DISABLED, 126 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_D)); 127 find_next_button_->SetTooltipText( 128 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_NEXT_TOOLTIP)); 129 find_next_button_->SetAccessibleName( 130 l10n_util::GetStringUTF16(IDS_ACCNAME_NEXT)); 131 AddChildView(find_next_button_); 132 133 close_button_ = new views::ImageButton(this); 134 close_button_->set_tag(CLOSE_TAG); 135 close_button_->SetFocusable(true); 136 close_button_->SetImage(views::CustomButton::STATE_NORMAL, 137 rb.GetImageSkiaNamed(IDR_CLOSE_1)); 138 close_button_->SetImage(views::CustomButton::STATE_HOVERED, 139 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); 140 close_button_->SetImage(views::CustomButton::STATE_PRESSED, 141 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); 142 close_button_->SetTooltipText( 143 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP)); 144 close_button_->SetAccessibleName( 145 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); 146 close_button_->SetAnimationDuration(0); 147 AddChildView(close_button_); 148 149 SetBackground(rb.GetImageSkiaNamed(IDR_FIND_DLG_LEFT_BACKGROUND), 150 rb.GetImageSkiaNamed(IDR_FIND_DLG_RIGHT_BACKGROUND)); 151 152 SetBorderFromIds( 153 IDR_FIND_DIALOG_LEFT, IDR_FIND_DIALOG_MIDDLE, IDR_FIND_DIALOG_RIGHT); 154 155 preferred_height_ = rb.GetImageSkiaNamed(IDR_FIND_DIALOG_MIDDLE)->height(); 156 157 // Background images for the Find edit box. 158 text_box_background_ = rb.GetImageSkiaNamed(IDR_FIND_BOX_BACKGROUND); 159 text_box_background_left_ = 160 rb.GetImageSkiaNamed(IDR_FIND_BOX_BACKGROUND_LEFT); 161 162 EnableCanvasFlippingForRTLUI(true); 163} 164 165FindBarView::~FindBarView() { 166} 167 168void FindBarView::SetFindTextAndSelectedRange( 169 const base::string16& find_text, 170 const gfx::Range& selected_range) { 171 find_text_->SetText(find_text); 172 find_text_->SelectRange(selected_range); 173} 174 175base::string16 FindBarView::GetFindText() const { 176 return find_text_->text(); 177} 178 179gfx::Range FindBarView::GetSelectedRange() const { 180 return find_text_->GetSelectedRange(); 181} 182 183base::string16 FindBarView::GetFindSelectedText() const { 184 return find_text_->GetSelectedText(); 185} 186 187base::string16 FindBarView::GetMatchCountText() const { 188 return match_count_text_->text(); 189} 190 191void FindBarView::UpdateForResult(const FindNotificationDetails& result, 192 const base::string16& find_text) { 193 bool have_valid_range = 194 result.number_of_matches() != -1 && result.active_match_ordinal() != -1; 195 196 // http://crbug.com/34970: some IMEs get confused if we change the text 197 // composed by them. To avoid this problem, we should check the IME status and 198 // update the text only when the IME is not composing text. 199 if (find_text_->text() != find_text && !find_text_->IsIMEComposing()) { 200 find_text_->SetText(find_text); 201 find_text_->SelectAll(true); 202 } 203 204 if (find_text.empty() || !have_valid_range) { 205 // If there was no text entered, we don't show anything in the result count 206 // area. 207 ClearMatchCount(); 208 return; 209 } 210 211 match_count_text_->SetText(l10n_util::GetStringFUTF16(IDS_FIND_IN_PAGE_COUNT, 212 base::IntToString16(result.active_match_ordinal()), 213 base::IntToString16(result.number_of_matches()))); 214 215 UpdateMatchCountAppearance(result.number_of_matches() == 0 && 216 result.final_update()); 217 218 // The match_count label may have increased/decreased in size so we need to 219 // do a layout and repaint the dialog so that the find text field doesn't 220 // partially overlap the match-count label when it increases on no matches. 221 Layout(); 222 SchedulePaint(); 223} 224 225void FindBarView::ClearMatchCount() { 226 match_count_text_->SetText(base::string16()); 227 UpdateMatchCountAppearance(false); 228 Layout(); 229 SchedulePaint(); 230} 231 232void FindBarView::SetFocusAndSelection(bool select_all) { 233 find_text_->RequestFocus(); 234 if (select_all && !find_text_->text().empty()) 235 find_text_->SelectAll(true); 236} 237 238/////////////////////////////////////////////////////////////////////////////// 239// FindBarView, views::View overrides: 240 241void FindBarView::OnPaint(gfx::Canvas* canvas) { 242 // Paint drop down bar border and background. 243 DropdownBarView::OnPaint(canvas); 244 245 // Then we draw the background image for the Find Textfield. We start by 246 // calculating the position of background images for the Find text box. 247 int find_text_x = find_text_->x(); 248 gfx::Point back_button_origin = find_previous_button_->bounds().origin(); 249 250 // Draw the image to the left that creates a curved left edge for the box. 251 canvas->TileImageInt(*text_box_background_left_, 252 find_text_x - text_box_background_left_->width(), 253 back_button_origin.y(), text_box_background_left_->width(), 254 text_box_background_left_->height()); 255 256 // Draw the top and bottom border for whole text box (encompasses both the 257 // find_text_ edit box and the match_count_text_ label). 258 canvas->TileImageInt(*text_box_background_, find_text_x, 259 back_button_origin.y(), 260 back_button_origin.x() - find_text_x, 261 text_box_background_->height()); 262 263 // Draw the background of the match text. We want to make sure the red 264 // "no-match" background almost completely fills up the amount of vertical 265 // space within the text box. We therefore fix the size relative to the button 266 // heights. We use the FindPrev button, which has a 1px outer whitespace 267 // margin, 1px border and we want to appear 1px below the border line so we 268 // subtract 3 for top and 3 for bottom. 269 gfx::Rect match_count_background_bounds(match_count_text_->bounds()); 270 match_count_background_bounds.set_height( 271 find_previous_button_->height() - 6); // Subtract 3px x 2. 272 match_count_background_bounds.set_y( 273 (height() - match_count_background_bounds.height()) / 2); 274 canvas->FillRect(match_count_background_bounds, 275 match_count_text_->background_color()); 276} 277 278void FindBarView::Layout() { 279 int panel_width = GetPreferredSize().width(); 280 281 // Stay within view bounds. 282 int view_width = width(); 283 if (view_width && view_width < panel_width) 284 panel_width = view_width; 285 286 // First we draw the close button on the far right. 287 gfx::Size sz = close_button_->GetPreferredSize(); 288 close_button_->SetBounds(panel_width - sz.width() - 289 kMarginRightOfCloseButton, 290 (height() - sz.height()) / 2, 291 sz.width(), 292 sz.height()); 293 // Set the color. 294 OnThemeChanged(); 295 296 // Next, the FindNext button to the left the close button. 297 sz = find_next_button_->GetPreferredSize(); 298 find_next_button_->SetBounds(close_button_->x() - 299 find_next_button_->width() - 300 kMarginLeftOfCloseButton, 301 (height() - sz.height()) / 2, 302 sz.width(), 303 sz.height()); 304 305 // Then, the FindPrevious button to the left the FindNext button. 306 sz = find_previous_button_->GetPreferredSize(); 307 find_previous_button_->SetBounds(find_next_button_->x() - 308 find_previous_button_->width(), 309 (height() - sz.height()) / 2, 310 sz.width(), 311 sz.height()); 312 313 // Then the label showing the match count number. 314 sz = match_count_text_->GetPreferredSize(); 315 // We extend the label bounds a bit to give the background highlighting a bit 316 // of breathing room (margins around the text). 317 sz.Enlarge(kMatchCountExtraWidth, 0); 318 sz.SetToMax(gfx::Size(kMatchCountMinWidth, 0)); 319 int match_count_x = 320 find_previous_button_->x() - kWhiteSpaceAfterMatchCountLabel - sz.width(); 321 int find_text_y = (height() - find_text_->GetPreferredSize().height()) / 2; 322 match_count_text_->SetBounds(match_count_x, 323 find_text_y + find_text_->GetBaseline() - 324 match_count_text_->GetBaseline(), 325 sz.width(), sz.height()); 326 327 // And whatever space is left in between, gets filled up by the find edit box. 328 int find_text_width = std::max(0, match_count_x - kMarginLeftOfFindTextfield); 329 find_text_->SetBounds(std::max(0, match_count_x - find_text_width), 330 find_text_y, find_text_width, 331 find_text_->GetPreferredSize().height()); 332 333 // The focus forwarder view is a hidden view that should cover the area 334 // between the find text box and the find button so that when the user clicks 335 // in that area we focus on the find text box. 336 int find_text_edge = find_text_->x() + find_text_->width(); 337 focus_forwarder_view_->SetBounds( 338 find_text_edge, find_previous_button_->y(), 339 find_previous_button_->x() - find_text_edge, 340 find_previous_button_->height()); 341} 342 343gfx::Size FindBarView::GetPreferredSize() { 344 gfx::Size prefsize = find_text_->GetPreferredSize(); 345 prefsize.set_height(preferred_height_); 346 347 // Add up all the preferred sizes and margins of the rest of the controls. 348 prefsize.Enlarge(kMarginLeftOfCloseButton + kMarginRightOfCloseButton + 349 kMarginLeftOfFindTextfield, 350 0); 351 prefsize.Enlarge(find_previous_button_->GetPreferredSize().width(), 0); 352 prefsize.Enlarge(find_next_button_->GetPreferredSize().width(), 0); 353 prefsize.Enlarge(close_button_->GetPreferredSize().width(), 0); 354 return prefsize; 355} 356 357//////////////////////////////////////////////////////////////////////////////// 358// FindBarView, views::ButtonListener implementation: 359 360void FindBarView::ButtonPressed( 361 views::Button* sender, const ui::Event& event) { 362 switch (sender->tag()) { 363 case FIND_PREVIOUS_TAG: 364 case FIND_NEXT_TAG: 365 if (!find_text_->text().empty()) { 366 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents( 367 find_bar_host()->GetFindBarController()->web_contents()); 368 find_tab_helper->StartFinding(find_text_->text(), 369 sender->tag() == FIND_NEXT_TAG, 370 false); // Not case sensitive. 371 } 372 if (event.IsMouseEvent()) { 373 // If mouse event, we move the focus back to the text-field, so that the 374 // user doesn't have to click on the text field to change the search. We 375 // don't want to do this for keyboard clicks on the button, since the 376 // user is more likely to press FindNext again than change the search 377 // query. 378 find_text_->RequestFocus(); 379 } 380 break; 381 case CLOSE_TAG: 382 find_bar_host()->GetFindBarController()->EndFindSession( 383 FindBarController::kKeepSelectionOnPage, 384 FindBarController::kKeepResultsInFindBox); 385 break; 386 default: 387 NOTREACHED() << L"Unknown button"; 388 break; 389 } 390} 391 392//////////////////////////////////////////////////////////////////////////////// 393// FindBarView, views::TextfieldController implementation: 394 395bool FindBarView::HandleKeyEvent(views::Textfield* sender, 396 const ui::KeyEvent& key_event) { 397 // If the dialog is not visible, there is no reason to process keyboard input. 398 if (!host()->IsVisible()) 399 return false; 400 401 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event)) 402 return true; // Handled, we are done! 403 404 if (key_event.key_code() == ui::VKEY_RETURN) { 405 // Pressing Return/Enter starts the search (unless text box is empty). 406 base::string16 find_string = find_text_->text(); 407 if (!find_string.empty()) { 408 FindBarController* controller = find_bar_host()->GetFindBarController(); 409 FindTabHelper* find_tab_helper = 410 FindTabHelper::FromWebContents(controller->web_contents()); 411 // Search forwards for enter, backwards for shift-enter. 412 find_tab_helper->StartFinding(find_string, 413 !key_event.IsShiftDown(), 414 false); // Not case sensitive. 415 } 416 return true; 417 } 418 419 return false; 420} 421 422void FindBarView::OnAfterUserAction(views::Textfield* sender) { 423 // The composition text wouldn't be what the user is really looking for. 424 // We delay the search until the user commits the composition text. 425 if (!sender->IsIMEComposing() && sender->text() != last_searched_text_) 426 Find(sender->text()); 427} 428 429void FindBarView::OnAfterPaste() { 430 // Clear the last search text so we always search for the user input after 431 // a paste operation, even if the pasted text is the same as before. 432 // See http://crbug.com/79002 433 last_searched_text_.clear(); 434} 435 436void FindBarView::Find(const base::string16& search_text) { 437 FindBarController* controller = find_bar_host()->GetFindBarController(); 438 DCHECK(controller); 439 content::WebContents* web_contents = controller->web_contents(); 440 // We must guard against a NULL web_contents, which can happen if the text 441 // in the Find box is changed right after the tab is destroyed. Otherwise, it 442 // can lead to crashes, as exposed by automation testing in issue 8048. 443 if (!web_contents) 444 return; 445 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents); 446 447 last_searched_text_ = search_text; 448 449 // When the user changes something in the text box we check the contents and 450 // if the textbox contains something we set it as the new search string and 451 // initiate search (even though old searches might be in progress). 452 if (!search_text.empty()) { 453 // The last two params here are forward (true) and case sensitive (false). 454 find_tab_helper->StartFinding(search_text, true, false); 455 } else { 456 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage); 457 UpdateForResult(find_tab_helper->find_result(), base::string16()); 458 find_bar_host()->MoveWindowIfNecessary(gfx::Rect(), false); 459 460 // Clearing the text box should clear the prepopulate state so that when 461 // we close and reopen the Find box it doesn't show the search we just 462 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged 463 // sent for a lot more things than just the user nulling out the search 464 // terms. See http://crbug.com/45372. 465 Profile* profile = 466 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 467 FindBarState* find_bar_state = FindBarStateFactory::GetForProfile(profile); 468 find_bar_state->set_last_prepopulate_text(base::string16()); 469 } 470} 471 472void FindBarView::UpdateMatchCountAppearance(bool no_match) { 473 if (no_match) { 474 match_count_text_->SetBackgroundColor(kBackgroundColorNoMatch); 475 match_count_text_->SetEnabledColor(kTextColorNoMatch); 476 } else { 477 match_count_text_->SetBackgroundColor(kBackgroundColorMatch); 478 match_count_text_->SetEnabledColor(kTextColorMatchCount); 479 } 480} 481 482bool FindBarView::FocusForwarderView::OnMousePressed( 483 const ui::MouseEvent& event) { 484 if (view_to_focus_on_mousedown_) 485 view_to_focus_on_mousedown_->RequestFocus(); 486 return true; 487} 488 489FindBarHost* FindBarView::find_bar_host() const { 490 return static_cast<FindBarHost*>(host()); 491} 492 493void FindBarView::OnThemeChanged() { 494 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 495 if (GetThemeProvider()) { 496 close_button_->SetBackground( 497 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), 498 rb.GetImageSkiaNamed(IDR_CLOSE_1), 499 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); 500 } 501} 502