find_bar_view.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/core/SkPaint.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/border.h" 32#include "ui/views/controls/button/image_button.h" 33#include "ui/views/controls/label.h" 34#include "ui/views/ime/input_method.h" 35#include "ui/views/painter.h" 36#include "ui/views/widget/widget.h" 37 38namespace { 39 40// The margins around the search field, match count label, and the close button. 41const int kMarginLeftOfCloseButton = 3; 42const int kMarginRightOfCloseButton = 7; 43const int kMarginLeftOfMatchCountLabel = 2; 44const int kMarginRightOfMatchCountLabel = 1; 45const int kMarginLeftOfFindTextfield = 12; 46 47// The margins around the match count label (We add extra space so that the 48// background highlight extends beyond just the text). 49const int kMatchCountExtraWidth = 9; 50 51// Minimum width for the match count label. 52const int kMatchCountMinWidth = 30; 53 54// The text color for the match count label. 55const SkColor kTextColorMatchCount = SkColorSetRGB(178, 178, 178); 56 57// The text color for the match count label when no matches are found. 58const SkColor kTextColorNoMatch = SK_ColorBLACK; 59 60// The background color of the match count label when results are found. 61const SkColor kBackgroundColorMatch = SkColorSetARGB(0, 255, 255, 255); 62 63// The background color of the match count label when no results are found. 64const SkColor kBackgroundColorNoMatch = SkColorSetRGB(255, 102, 102); 65 66// The default number of average characters that the text box will be. This 67// number brings the width on a "regular fonts" system to about 300px. 68const int kDefaultCharWidth = 43; 69 70} // namespace 71 72//////////////////////////////////////////////////////////////////////////////// 73// FindBarView, public: 74 75FindBarView::FindBarView(FindBarHost* host) 76 : DropdownBarView(host), 77 find_text_(NULL), 78 match_count_text_(NULL), 79 focus_forwarder_view_(NULL), 80 find_previous_button_(NULL), 81 find_next_button_(NULL), 82 close_button_(NULL) { 83 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 84 85 find_text_ = new views::Textfield; 86 find_text_->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); 87 find_text_->set_default_width_in_chars(kDefaultCharWidth); 88 find_text_->set_controller(this); 89 find_text_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FIND)); 90 // The find bar textfield has a background image instead of a border. 91 find_text_->SetBorder(views::Border::NullBorder()); 92 AddChildView(find_text_); 93 94 match_count_text_ = new views::Label(); 95 AddChildView(match_count_text_); 96 97 // Create a focus forwarder view which sends focus to find_text_. 98 focus_forwarder_view_ = new FocusForwarderView(find_text_); 99 AddChildView(focus_forwarder_view_); 100 101 find_previous_button_ = new views::ImageButton(this); 102 find_previous_button_->set_tag(FIND_PREVIOUS_TAG); 103 find_previous_button_->SetFocusable(true); 104 find_previous_button_->SetImage(views::CustomButton::STATE_NORMAL, 105 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV)); 106 find_previous_button_->SetImage(views::CustomButton::STATE_HOVERED, 107 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_H)); 108 find_previous_button_->SetImage(views::CustomButton::STATE_PRESSED, 109 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_P)); 110 find_previous_button_->SetImage(views::CustomButton::STATE_DISABLED, 111 rb.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_D)); 112 find_previous_button_->SetTooltipText( 113 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP)); 114 find_previous_button_->SetAccessibleName( 115 l10n_util::GetStringUTF16(IDS_ACCNAME_PREVIOUS)); 116 AddChildView(find_previous_button_); 117 118 find_next_button_ = new views::ImageButton(this); 119 find_next_button_->set_tag(FIND_NEXT_TAG); 120 find_next_button_->SetFocusable(true); 121 find_next_button_->SetImage(views::CustomButton::STATE_NORMAL, 122 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT)); 123 find_next_button_->SetImage(views::CustomButton::STATE_HOVERED, 124 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_H)); 125 find_next_button_->SetImage(views::CustomButton::STATE_PRESSED, 126 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_P)); 127 find_next_button_->SetImage(views::CustomButton::STATE_DISABLED, 128 rb.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_D)); 129 find_next_button_->SetTooltipText( 130 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_NEXT_TOOLTIP)); 131 find_next_button_->SetAccessibleName( 132 l10n_util::GetStringUTF16(IDS_ACCNAME_NEXT)); 133 AddChildView(find_next_button_); 134 135 close_button_ = new views::ImageButton(this); 136 close_button_->set_tag(CLOSE_TAG); 137 close_button_->SetFocusable(true); 138 close_button_->SetImage(views::CustomButton::STATE_NORMAL, 139 rb.GetImageSkiaNamed(IDR_CLOSE_1)); 140 close_button_->SetImage(views::CustomButton::STATE_HOVERED, 141 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); 142 close_button_->SetImage(views::CustomButton::STATE_PRESSED, 143 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); 144 close_button_->SetTooltipText( 145 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP)); 146 close_button_->SetAccessibleName( 147 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); 148 close_button_->SetAnimationDuration(0); 149 AddChildView(close_button_); 150 151 SetBackground(rb.GetImageSkiaNamed(IDR_FIND_DLG_LEFT_BACKGROUND), 152 rb.GetImageSkiaNamed(IDR_FIND_DLG_RIGHT_BACKGROUND)); 153 154 SetBorderFromIds( 155 IDR_FIND_DIALOG_LEFT, IDR_FIND_DIALOG_MIDDLE, IDR_FIND_DIALOG_RIGHT); 156 157 preferred_height_ = rb.GetImageSkiaNamed(IDR_FIND_DIALOG_MIDDLE)->height(); 158 159 static const int kImages[] = IMAGE_GRID(IDR_TEXTFIELD); 160 find_text_border_.reset(views::Painter::CreateImageGridPainter(kImages)); 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 GetInputMethod()->ShowImeIfNeeded(); 235 if (select_all && !find_text_->text().empty()) 236 find_text_->SelectAll(true); 237} 238 239/////////////////////////////////////////////////////////////////////////////// 240// FindBarView, views::View overrides: 241 242void FindBarView::OnPaint(gfx::Canvas* canvas) { 243 // Paint drop down bar border and background. 244 DropdownBarView::OnPaint(canvas); 245 246 // Paint the background and border for the textfield. 247 const int find_text_x = kMarginLeftOfFindTextfield / 2; 248 const gfx::Rect text_bounds(find_text_x, find_next_button_->y(), 249 find_next_button_->bounds().right() - find_text_x, 250 find_next_button_->height()); 251 const int kBorderCornerRadius = 2; 252 gfx::Rect background_bounds = text_bounds; 253 background_bounds.Inset(kBorderCornerRadius, kBorderCornerRadius); 254 SkPaint paint; 255 paint.setStyle(SkPaint::kFill_Style); 256 paint.setColor(find_text_->GetBackgroundColor()); 257 canvas->DrawRoundRect(background_bounds, kBorderCornerRadius, paint); 258 canvas->Save(); 259 canvas->ClipRect(gfx::Rect(0, 0, find_previous_button_->x(), height())); 260 views::Painter::PaintPainterAt(canvas, find_text_border_.get(), text_bounds); 261 canvas->Restore(); 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() - kMarginRightOfMatchCountLabel - 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 - 329 kMarginLeftOfMatchCountLabel - kMarginLeftOfFindTextfield); 330 find_text_->SetBounds(kMarginLeftOfFindTextfield, find_text_y, 331 find_text_width, 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() const { 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