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.
5 #include "chrome/browser/ui/views/find_bar_view.h"
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 "chrome/grit/generated_resources.h"
23 #include "grit/theme_resources.h"
24 #include "third_party/skia/include/core/SkPaint.h"
25 #include "ui/base/ime/text_input_flags.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/resources/grit/ui_resources.h"
32 #include "ui/views/border.h"
33 #include "ui/views/controls/button/image_button.h"
34 #include "ui/views/controls/label.h"
35 #include "ui/views/ime/input_method.h"
36 #include "ui/views/painter.h"
37 #include "ui/views/widget/widget.h"
41 // The margins around the UI controls, derived from assets and design specs.
42 const int kMarginLeftOfCloseButton
= 3;
43 const int kMarginRightOfCloseButton
= 7;
44 const int kMarginLeftOfMatchCountLabel
= 3;
45 const int kMarginRightOfMatchCountLabel
= 1;
46 const int kMarginLeftOfFindTextfield
= 12;
47 const int kMarginVerticalFindTextfield
= 6;
49 // The margins around the match count label (We add extra space so that the
50 // background highlight extends beyond just the text).
51 const int kMatchCountExtraWidth
= 9;
53 // Minimum width for the match count label.
54 const int kMatchCountMinWidth
= 30;
56 // The text color for the match count label.
57 const SkColor kTextColorMatchCount
= SkColorSetRGB(178, 178, 178);
59 // The text color for the match count label when no matches are found.
60 const SkColor kTextColorNoMatch
= SK_ColorBLACK
;
62 // The background color of the match count label when results are found.
63 const SkColor kBackgroundColorMatch
= SkColorSetARGB(0, 255, 255, 255);
65 // The background color of the match count label when no results are found.
66 const SkColor kBackgroundColorNoMatch
= SkColorSetRGB(255, 102, 102);
68 // The default number of average characters that the text box will be. This
69 // number brings the width on a "regular fonts" system to about 300px.
70 const int kDefaultCharWidth
= 43;
74 ////////////////////////////////////////////////////////////////////////////////
75 // FindBarView, public:
77 FindBarView::FindBarView(FindBarHost
* host
)
78 : DropdownBarView(host
),
80 match_count_text_(NULL
),
81 focus_forwarder_view_(NULL
),
82 find_previous_button_(NULL
),
83 find_next_button_(NULL
),
85 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
87 find_text_
= new views::Textfield
;
88 find_text_
->set_id(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
);
89 find_text_
->set_default_width_in_chars(kDefaultCharWidth
);
90 find_text_
->set_controller(this);
91 find_text_
->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FIND
));
92 find_text_
->SetTextInputFlags(ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF
);
93 // The find bar textfield has a background image instead of a border.
94 find_text_
->SetBorder(views::Border::NullBorder());
95 AddChildView(find_text_
);
97 match_count_text_
= new views::Label();
98 AddChildView(match_count_text_
);
100 // Create a focus forwarder view which sends focus to find_text_.
101 focus_forwarder_view_
= new FocusForwarderView(find_text_
);
102 AddChildView(focus_forwarder_view_
);
104 find_previous_button_
= new views::ImageButton(this);
105 find_previous_button_
->set_tag(FIND_PREVIOUS_TAG
);
106 find_previous_button_
->SetFocusable(true);
107 find_previous_button_
->SetImage(views::CustomButton::STATE_NORMAL
,
108 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_PREV
));
109 find_previous_button_
->SetImage(views::CustomButton::STATE_HOVERED
,
110 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_H
));
111 find_previous_button_
->SetImage(views::CustomButton::STATE_PRESSED
,
112 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_P
));
113 find_previous_button_
->SetImage(views::CustomButton::STATE_DISABLED
,
114 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_PREV_D
));
115 find_previous_button_
->SetTooltipText(
116 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP
));
117 find_previous_button_
->SetAccessibleName(
118 l10n_util::GetStringUTF16(IDS_ACCNAME_PREVIOUS
));
119 AddChildView(find_previous_button_
);
121 find_next_button_
= new views::ImageButton(this);
122 find_next_button_
->set_tag(FIND_NEXT_TAG
);
123 find_next_button_
->SetFocusable(true);
124 find_next_button_
->SetImage(views::CustomButton::STATE_NORMAL
,
125 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT
));
126 find_next_button_
->SetImage(views::CustomButton::STATE_HOVERED
,
127 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_H
));
128 find_next_button_
->SetImage(views::CustomButton::STATE_PRESSED
,
129 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_P
));
130 find_next_button_
->SetImage(views::CustomButton::STATE_DISABLED
,
131 rb
.GetImageSkiaNamed(IDR_FINDINPAGE_NEXT_D
));
132 find_next_button_
->SetTooltipText(
133 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_NEXT_TOOLTIP
));
134 find_next_button_
->SetAccessibleName(
135 l10n_util::GetStringUTF16(IDS_ACCNAME_NEXT
));
136 AddChildView(find_next_button_
);
138 close_button_
= new views::ImageButton(this);
139 close_button_
->set_tag(CLOSE_TAG
);
140 close_button_
->SetFocusable(true);
141 close_button_
->SetImage(views::CustomButton::STATE_NORMAL
,
142 rb
.GetImageSkiaNamed(IDR_CLOSE_1
));
143 close_button_
->SetImage(views::CustomButton::STATE_HOVERED
,
144 rb
.GetImageSkiaNamed(IDR_CLOSE_1_H
));
145 close_button_
->SetImage(views::CustomButton::STATE_PRESSED
,
146 rb
.GetImageSkiaNamed(IDR_CLOSE_1_P
));
147 close_button_
->SetTooltipText(
148 l10n_util::GetStringUTF16(IDS_FIND_IN_PAGE_CLOSE_TOOLTIP
));
149 close_button_
->SetAccessibleName(
150 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE
));
151 close_button_
->SetAnimationDuration(0);
152 AddChildView(close_button_
);
154 SetBackground(rb
.GetImageSkiaNamed(IDR_FIND_DLG_LEFT_BACKGROUND
),
155 rb
.GetImageSkiaNamed(IDR_FIND_DLG_RIGHT_BACKGROUND
));
158 IDR_FIND_DIALOG_LEFT
, IDR_FIND_DIALOG_MIDDLE
, IDR_FIND_DIALOG_RIGHT
);
160 preferred_height_
= rb
.GetImageSkiaNamed(IDR_FIND_DIALOG_MIDDLE
)->height();
162 static const int kImages
[] = IMAGE_GRID(IDR_TEXTFIELD
);
163 find_text_border_
.reset(views::Painter::CreateImageGridPainter(kImages
));
165 EnableCanvasFlippingForRTLUI(true);
168 FindBarView::~FindBarView() {
171 void FindBarView::SetFindTextAndSelectedRange(
172 const base::string16
& find_text
,
173 const gfx::Range
& selected_range
) {
174 find_text_
->SetText(find_text
);
175 find_text_
->SelectRange(selected_range
);
178 base::string16
FindBarView::GetFindText() const {
179 return find_text_
->text();
182 gfx::Range
FindBarView::GetSelectedRange() const {
183 return find_text_
->GetSelectedRange();
186 base::string16
FindBarView::GetFindSelectedText() const {
187 return find_text_
->GetSelectedText();
190 base::string16
FindBarView::GetMatchCountText() const {
191 return match_count_text_
->text();
194 void FindBarView::UpdateForResult(const FindNotificationDetails
& result
,
195 const base::string16
& find_text
) {
196 bool have_valid_range
=
197 result
.number_of_matches() != -1 && result
.active_match_ordinal() != -1;
199 // http://crbug.com/34970: some IMEs get confused if we change the text
200 // composed by them. To avoid this problem, we should check the IME status and
201 // update the text only when the IME is not composing text.
202 if (find_text_
->text() != find_text
&& !find_text_
->IsIMEComposing()) {
203 find_text_
->SetText(find_text
);
204 find_text_
->SelectAll(true);
207 if (find_text
.empty() || !have_valid_range
) {
208 // If there was no text entered, we don't show anything in the result count
214 match_count_text_
->SetText(l10n_util::GetStringFUTF16(IDS_FIND_IN_PAGE_COUNT
,
215 base::IntToString16(result
.active_match_ordinal()),
216 base::IntToString16(result
.number_of_matches())));
218 UpdateMatchCountAppearance(result
.number_of_matches() == 0 &&
219 result
.final_update());
221 // The match_count label may have increased/decreased in size so we need to
222 // do a layout and repaint the dialog so that the find text field doesn't
223 // partially overlap the match-count label when it increases on no matches.
228 void FindBarView::ClearMatchCount() {
229 match_count_text_
->SetText(base::string16());
230 UpdateMatchCountAppearance(false);
235 void FindBarView::SetFocusAndSelection(bool select_all
) {
236 find_text_
->RequestFocus();
237 GetInputMethod()->ShowImeIfNeeded();
238 if (select_all
&& !find_text_
->text().empty())
239 find_text_
->SelectAll(true);
242 ///////////////////////////////////////////////////////////////////////////////
243 // FindBarView, views::View overrides:
245 void FindBarView::OnPaint(gfx::Canvas
* canvas
) {
246 // Paint drop down bar border and background.
247 DropdownBarView::OnPaint(canvas
);
249 // Paint the background and border for the textfield.
250 const int find_text_x
= kMarginLeftOfFindTextfield
/ 2;
251 const gfx::Rect
text_bounds(find_text_x
, find_next_button_
->y(),
252 find_next_button_
->bounds().right() - find_text_x
,
253 find_next_button_
->height());
254 const int kBorderCornerRadius
= 2;
255 gfx::Rect background_bounds
= text_bounds
;
256 background_bounds
.Inset(kBorderCornerRadius
, kBorderCornerRadius
);
258 paint
.setStyle(SkPaint::kFill_Style
);
259 paint
.setColor(find_text_
->GetBackgroundColor());
260 canvas
->DrawRoundRect(background_bounds
, kBorderCornerRadius
, paint
);
262 canvas
->ClipRect(gfx::Rect(0, 0, find_previous_button_
->x(), height()));
263 views::Painter::PaintPainterAt(canvas
, find_text_border_
.get(), text_bounds
);
266 // Draw the background of the match text. We want to make sure the red
267 // "no-match" background almost completely fills up the amount of vertical
268 // space within the text box. We therefore fix the size relative to the button
269 // heights. We use the FindPrev button, which has a 1px outer whitespace
270 // margin, 1px border and we want to appear 1px below the border line so we
271 // subtract 3 for top and 3 for bottom.
272 gfx::Rect
match_count_background_bounds(match_count_text_
->bounds());
273 match_count_background_bounds
.set_height(
274 find_previous_button_
->height() - 6); // Subtract 3px x 2.
275 match_count_background_bounds
.set_y(
276 (height() - match_count_background_bounds
.height()) / 2);
277 canvas
->FillRect(match_count_background_bounds
,
278 match_count_text_
->background_color());
281 void FindBarView::Layout() {
282 int panel_width
= GetPreferredSize().width();
284 // Stay within view bounds.
285 int view_width
= width();
286 if (view_width
&& view_width
< panel_width
)
287 panel_width
= view_width
;
289 // First we draw the close button on the far right.
290 gfx::Size sz
= close_button_
->GetPreferredSize();
291 close_button_
->SetBounds(panel_width
- sz
.width() -
292 kMarginRightOfCloseButton
,
293 (height() - sz
.height()) / 2,
299 // Next, the FindNext button to the left the close button.
300 sz
= find_next_button_
->GetPreferredSize();
301 find_next_button_
->SetBounds(close_button_
->x() -
302 find_next_button_
->width() -
303 kMarginLeftOfCloseButton
,
304 (height() - sz
.height()) / 2,
308 // Then, the FindPrevious button to the left the FindNext button.
309 sz
= find_previous_button_
->GetPreferredSize();
310 find_previous_button_
->SetBounds(find_next_button_
->x() -
311 find_previous_button_
->width(),
312 (height() - sz
.height()) / 2,
316 // Then the label showing the match count number.
317 sz
= match_count_text_
->GetPreferredSize();
318 // We extend the label bounds a bit to give the background highlighting a bit
319 // of breathing room (margins around the text).
320 sz
.Enlarge(kMatchCountExtraWidth
, 0);
321 sz
.SetToMax(gfx::Size(kMatchCountMinWidth
, 0));
322 const int match_count_x
=
323 find_previous_button_
->x() - kMarginRightOfMatchCountLabel
- sz
.width();
324 const int find_text_y
= kMarginVerticalFindTextfield
;
325 const gfx::Insets
find_text_insets(find_text_
->GetInsets());
326 match_count_text_
->SetBounds(match_count_x
,
327 find_text_y
- find_text_insets
.top() +
328 find_text_
->GetBaseline() -
329 match_count_text_
->GetBaseline(),
330 sz
.width(), sz
.height());
332 // Fill the remaining width and available height with the textfield.
333 const int left_margin
= kMarginLeftOfFindTextfield
- find_text_insets
.left();
334 const int find_text_width
= std::max(0, match_count_x
- left_margin
-
335 kMarginLeftOfMatchCountLabel
+ find_text_insets
.right());
336 find_text_
->SetBounds(left_margin
, find_text_y
, find_text_width
,
337 height() - 2 * kMarginVerticalFindTextfield
);
339 // The focus forwarder view is a hidden view that should cover the area
340 // between the find text box and the find button so that when the user clicks
341 // in that area we focus on the find text box.
342 const int find_text_edge
= find_text_
->x() + find_text_
->width();
343 focus_forwarder_view_
->SetBounds(
344 find_text_edge
, find_previous_button_
->y(),
345 find_previous_button_
->x() - find_text_edge
,
346 find_previous_button_
->height());
349 gfx::Size
FindBarView::GetPreferredSize() const {
350 gfx::Size prefsize
= find_text_
->GetPreferredSize();
351 prefsize
.set_height(preferred_height_
);
353 // Add up all the preferred sizes and margins of the rest of the controls.
354 prefsize
.Enlarge(kMarginLeftOfCloseButton
+ kMarginRightOfCloseButton
+
355 kMarginLeftOfFindTextfield
-
356 find_text_
->GetInsets().width(),
358 prefsize
.Enlarge(find_previous_button_
->GetPreferredSize().width(), 0);
359 prefsize
.Enlarge(find_next_button_
->GetPreferredSize().width(), 0);
360 prefsize
.Enlarge(close_button_
->GetPreferredSize().width(), 0);
364 ////////////////////////////////////////////////////////////////////////////////
365 // FindBarView, views::ButtonListener implementation:
367 void FindBarView::ButtonPressed(
368 views::Button
* sender
, const ui::Event
& event
) {
369 switch (sender
->tag()) {
370 case FIND_PREVIOUS_TAG
:
372 if (!find_text_
->text().empty()) {
373 FindTabHelper
* find_tab_helper
= FindTabHelper::FromWebContents(
374 find_bar_host()->GetFindBarController()->web_contents());
375 find_tab_helper
->StartFinding(find_text_
->text(),
376 sender
->tag() == FIND_NEXT_TAG
,
377 false); // Not case sensitive.
379 if (event
.IsMouseEvent()) {
380 // If mouse event, we move the focus back to the text-field, so that the
381 // user doesn't have to click on the text field to change the search. We
382 // don't want to do this for keyboard clicks on the button, since the
383 // user is more likely to press FindNext again than change the search
385 find_text_
->RequestFocus();
389 find_bar_host()->GetFindBarController()->EndFindSession(
390 FindBarController::kKeepSelectionOnPage
,
391 FindBarController::kKeepResultsInFindBox
);
394 NOTREACHED() << L
"Unknown button";
399 ////////////////////////////////////////////////////////////////////////////////
400 // FindBarView, views::TextfieldController implementation:
402 bool FindBarView::HandleKeyEvent(views::Textfield
* sender
,
403 const ui::KeyEvent
& key_event
) {
404 // If the dialog is not visible, there is no reason to process keyboard input.
405 if (!host()->IsVisible())
408 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event
))
409 return true; // Handled, we are done!
411 if (key_event
.key_code() == ui::VKEY_RETURN
) {
412 // Pressing Return/Enter starts the search (unless text box is empty).
413 base::string16 find_string
= find_text_
->text();
414 if (!find_string
.empty()) {
415 FindBarController
* controller
= find_bar_host()->GetFindBarController();
416 FindTabHelper
* find_tab_helper
=
417 FindTabHelper::FromWebContents(controller
->web_contents());
418 // Search forwards for enter, backwards for shift-enter.
419 find_tab_helper
->StartFinding(find_string
,
420 !key_event
.IsShiftDown(),
421 false); // Not case sensitive.
429 void FindBarView::OnAfterUserAction(views::Textfield
* sender
) {
430 // The composition text wouldn't be what the user is really looking for.
431 // We delay the search until the user commits the composition text.
432 if (!sender
->IsIMEComposing() && sender
->text() != last_searched_text_
)
433 Find(sender
->text());
436 void FindBarView::OnAfterPaste() {
437 // Clear the last search text so we always search for the user input after
438 // a paste operation, even if the pasted text is the same as before.
439 // See http://crbug.com/79002
440 last_searched_text_
.clear();
443 void FindBarView::Find(const base::string16
& search_text
) {
444 FindBarController
* controller
= find_bar_host()->GetFindBarController();
446 content::WebContents
* web_contents
= controller
->web_contents();
447 // We must guard against a NULL web_contents, which can happen if the text
448 // in the Find box is changed right after the tab is destroyed. Otherwise, it
449 // can lead to crashes, as exposed by automation testing in issue 8048.
452 FindTabHelper
* find_tab_helper
= FindTabHelper::FromWebContents(web_contents
);
454 last_searched_text_
= search_text
;
456 // When the user changes something in the text box we check the contents and
457 // if the textbox contains something we set it as the new search string and
458 // initiate search (even though old searches might be in progress).
459 if (!search_text
.empty()) {
460 // The last two params here are forward (true) and case sensitive (false).
461 find_tab_helper
->StartFinding(search_text
, true, false);
463 find_tab_helper
->StopFinding(FindBarController::kClearSelectionOnPage
);
464 UpdateForResult(find_tab_helper
->find_result(), base::string16());
465 find_bar_host()->MoveWindowIfNecessary(gfx::Rect());
467 // Clearing the text box should clear the prepopulate state so that when
468 // we close and reopen the Find box it doesn't show the search we just
469 // deleted. We can't do this on ChromeOS yet because we get ContentsChanged
470 // sent for a lot more things than just the user nulling out the search
471 // terms. See http://crbug.com/45372.
473 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
474 FindBarState
* find_bar_state
= FindBarStateFactory::GetForProfile(profile
);
475 find_bar_state
->set_last_prepopulate_text(base::string16());
479 void FindBarView::UpdateMatchCountAppearance(bool no_match
) {
481 match_count_text_
->SetBackgroundColor(kBackgroundColorNoMatch
);
482 match_count_text_
->SetEnabledColor(kTextColorNoMatch
);
484 match_count_text_
->SetBackgroundColor(kBackgroundColorMatch
);
485 match_count_text_
->SetEnabledColor(kTextColorMatchCount
);
489 bool FindBarView::FocusForwarderView::OnMousePressed(
490 const ui::MouseEvent
& event
) {
491 if (view_to_focus_on_mousedown_
)
492 view_to_focus_on_mousedown_
->RequestFocus();
496 FindBarHost
* FindBarView::find_bar_host() const {
497 return static_cast<FindBarHost
*>(host());
500 const char* FindBarView::GetClassName() const {
501 return "FindBarView";
504 void FindBarView::OnThemeChanged() {
505 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
506 if (GetThemeProvider()) {
507 close_button_
->SetBackground(
508 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT
),
509 rb
.GetImageSkiaNamed(IDR_CLOSE_1
),
510 rb
.GetImageSkiaNamed(IDR_CLOSE_1_MASK
));