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 "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"
36 // The amount of whitespace to have before the find button.
37 static const int kWhiteSpaceAfterMatchCountLabel
= 1;
39 // The margins around the search field and the close button.
40 static const int kMarginLeftOfCloseButton
= 3;
41 static const int kMarginRightOfCloseButton
= 7;
42 static const int kMarginLeftOfFindTextfield
= 12;
44 // The margins around the match count label (We add extra space so that the
45 // background highlight extends beyond just the text).
46 static const int kMatchCountExtraWidth
= 9;
48 // Minimum width for the match count label.
49 static const int kMatchCountMinWidth
= 30;
51 // The text color for the match count label.
52 static const SkColor kTextColorMatchCount
= SkColorSetRGB(178, 178, 178);
54 // The text color for the match count label when no matches are found.
55 static const SkColor kTextColorNoMatch
= SK_ColorBLACK
;
57 // The background color of the match count label when results are found.
58 static const SkColor kBackgroundColorMatch
= SkColorSetARGB(0, 255, 255, 255);
60 // The background color of the match count label when no results are found.
61 static const SkColor kBackgroundColorNoMatch
= SkColorSetRGB(255, 102, 102);
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.
65 static const int kDefaultCharWidth
= 43;
67 ////////////////////////////////////////////////////////////////////////////////
68 // FindBarView, public:
70 FindBarView::FindBarView(FindBarHost
* host
)
71 : DropdownBarView(host
),
73 match_count_text_(NULL
),
74 focus_forwarder_view_(NULL
),
75 find_previous_button_(NULL
),
76 find_next_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();
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_
->set_border(NULL
);
90 AddChildView(find_text_
);
92 match_count_text_
= new views::Label();
93 AddChildView(match_count_text_
);
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_
);
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_
);
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_
);
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_
);
149 SetBackground(rb
.GetImageSkiaNamed(IDR_FIND_DLG_LEFT_BACKGROUND
),
150 rb
.GetImageSkiaNamed(IDR_FIND_DLG_RIGHT_BACKGROUND
));
152 SetBorder(IDR_FIND_DIALOG_LEFT
, IDR_FIND_DIALOG_MIDDLE
,
153 IDR_FIND_DIALOG_RIGHT
);
155 preferred_height_
= rb
.GetImageSkiaNamed(IDR_FIND_DIALOG_MIDDLE
)->height();
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
);
162 EnableCanvasFlippingForRTLUI(true);
165 FindBarView::~FindBarView() {
168 void 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
);
175 base::string16
FindBarView::GetFindText() const {
176 return find_text_
->text();
179 gfx::Range
FindBarView::GetSelectedRange() const {
180 return find_text_
->GetSelectedRange();
183 base::string16
FindBarView::GetFindSelectedText() const {
184 return find_text_
->GetSelectedText();
187 base::string16
FindBarView::GetMatchCountText() const {
188 return match_count_text_
->text();
191 void 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;
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);
204 if (find_text
.empty() || !have_valid_range
) {
205 // If there was no text entered, we don't show anything in the result count
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())));
215 UpdateMatchCountAppearance(result
.number_of_matches() == 0 &&
216 result
.final_update());
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.
225 void FindBarView::ClearMatchCount() {
226 match_count_text_
->SetText(base::string16());
227 UpdateMatchCountAppearance(false);
232 void FindBarView::SetFocusAndSelection(bool select_all
) {
233 find_text_
->RequestFocus();
234 if (select_all
&& !find_text_
->text().empty())
235 find_text_
->SelectAll(true);
238 ///////////////////////////////////////////////////////////////////////////////
239 // FindBarView, views::View overrides:
241 void FindBarView::OnPaint(gfx::Canvas
* canvas
) {
242 // Paint drop down bar border and background.
243 DropdownBarView::OnPaint(canvas
);
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();
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());
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());
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());
278 void FindBarView::Layout() {
279 int panel_width
= GetPreferredSize().width();
281 // Stay within view bounds.
282 int view_width
= width();
283 if (view_width
&& view_width
< panel_width
)
284 panel_width
= view_width
;
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,
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,
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,
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));
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());
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());
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());
343 gfx::Size
FindBarView::GetPreferredSize() {
344 gfx::Size prefsize
= find_text_
->GetPreferredSize();
345 prefsize
.set_height(preferred_height_
);
347 // Add up all the preferred sizes and margins of the rest of the controls.
348 prefsize
.Enlarge(kMarginLeftOfCloseButton
+ kMarginRightOfCloseButton
+
349 kMarginLeftOfFindTextfield
,
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);
357 ////////////////////////////////////////////////////////////////////////////////
358 // FindBarView, views::ButtonListener implementation:
360 void FindBarView::ButtonPressed(
361 views::Button
* sender
, const ui::Event
& event
) {
362 switch (sender
->tag()) {
363 case FIND_PREVIOUS_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.
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
378 find_text_
->RequestFocus();
382 find_bar_host()->GetFindBarController()->EndFindSession(
383 FindBarController::kKeepSelectionOnPage
,
384 FindBarController::kKeepResultsInFindBox
);
387 NOTREACHED() << L
"Unknown button";
392 ////////////////////////////////////////////////////////////////////////////////
393 // FindBarView, views::TextfieldController implementation:
395 bool 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())
401 if (find_bar_host()->MaybeForwardKeyEventToWebpage(key_event
))
402 return true; // Handled, we are done!
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.
422 void 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());
429 void 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();
436 void FindBarView::Find(const base::string16
& search_text
) {
437 FindBarController
* controller
= find_bar_host()->GetFindBarController();
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.
445 FindTabHelper
* find_tab_helper
= FindTabHelper::FromWebContents(web_contents
);
447 last_searched_text_
= search_text
;
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);
456 find_tab_helper
->StopFinding(FindBarController::kClearSelectionOnPage
);
457 UpdateForResult(find_tab_helper
->find_result(), base::string16());
458 find_bar_host()->MoveWindowIfNecessary(gfx::Rect(), false);
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.
466 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
467 FindBarState
* find_bar_state
= FindBarStateFactory::GetForProfile(profile
);
468 find_bar_state
->set_last_prepopulate_text(base::string16());
472 void FindBarView::UpdateMatchCountAppearance(bool no_match
) {
474 match_count_text_
->SetBackgroundColor(kBackgroundColorNoMatch
);
475 match_count_text_
->SetEnabledColor(kTextColorNoMatch
);
477 match_count_text_
->SetBackgroundColor(kBackgroundColorMatch
);
478 match_count_text_
->SetEnabledColor(kTextColorMatchCount
);
482 bool FindBarView::FocusForwarderView::OnMousePressed(
483 const ui::MouseEvent
& event
) {
484 if (view_to_focus_on_mousedown_
)
485 view_to_focus_on_mousedown_
->RequestFocus();
489 FindBarHost
* FindBarView::find_bar_host() const {
490 return static_cast<FindBarHost
*>(host());
493 void 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
));