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_host.h"
9 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
10 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
11 #include "chrome/browser/ui/view_ids.h"
12 #include "chrome/browser/ui/views/find_bar_view.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "ui/events/event.h"
17 #include "ui/events/keycodes/keyboard_codes.h"
18 #include "ui/views/focus/external_focus_tracker.h"
19 #include "ui/views/focus/view_storage.h"
20 #include "ui/views/widget/root_view.h"
21 #include "ui/views/widget/widget.h"
23 using content::NativeWebKeyboardEvent
;
27 // Declared in browser_dialogs.h so others don't have to depend on our header.
28 FindBar
* CreateFindBar(BrowserView
* browser_view
) {
29 return new FindBarHost(browser_view
);
34 ////////////////////////////////////////////////////////////////////////////////
35 // FindBarHost, public:
37 FindBarHost::FindBarHost(BrowserView
* browser_view
)
38 : DropdownBarHost(browser_view
),
39 find_bar_controller_(NULL
) {
40 FindBarView
* find_bar_view
= new FindBarView(this);
41 Init(browser_view
->find_bar_host_view(), find_bar_view
, find_bar_view
);
44 FindBarHost::~FindBarHost() {
47 bool FindBarHost::MaybeForwardKeyEventToWebpage(
48 const ui::KeyEvent
& key_event
) {
49 switch (key_event
.key_code()) {
57 if (key_event
.IsControlDown())
64 content::WebContents
* contents
= find_bar_controller_
->web_contents();
68 content::RenderViewHost
* render_view_host
= contents
->GetRenderViewHost();
70 // Make sure we don't have a text field element interfering with keyboard
71 // input. Otherwise Up and Down arrow key strokes get eaten. "Nom Nom Nom".
72 render_view_host
->ClearFocusedElement();
73 NativeWebKeyboardEvent
event(key_event
);
74 render_view_host
->ForwardKeyboardEvent(event
);
78 FindBarController
* FindBarHost::GetFindBarController() const {
79 return find_bar_controller_
;
82 void FindBarHost::SetFindBarController(FindBarController
* find_bar_controller
) {
83 find_bar_controller_
= find_bar_controller
;
86 void FindBarHost::Show(bool animate
) {
87 DropdownBarHost::Show(animate
);
90 void FindBarHost::Hide(bool animate
) {
91 DropdownBarHost::Hide(animate
);
94 void FindBarHost::SetFocusAndSelection() {
95 DropdownBarHost::SetFocusAndSelection();
98 void FindBarHost::ClearResults(const FindNotificationDetails
& results
) {
99 find_bar_view()->UpdateForResult(results
, base::string16());
102 void FindBarHost::StopAnimation() {
103 DropdownBarHost::StopAnimation();
106 void FindBarHost::MoveWindowIfNecessary(const gfx::Rect
& selection_rect
) {
107 // We only move the window if one is active for the current WebContents. If we
108 // don't check this, then SetDialogPosition below will end up making the Find
110 content::WebContents
* web_contents
= find_bar_controller_
->web_contents();
114 FindTabHelper
* find_tab_helper
= FindTabHelper::FromWebContents(web_contents
);
115 if (!find_tab_helper
|| !find_tab_helper
->find_ui_active())
118 gfx::Rect new_pos
= GetDialogPosition(selection_rect
);
119 SetDialogPosition(new_pos
);
121 // May need to redraw our frame to accommodate bookmark bar styles.
122 view()->Layout(); // Bounds may have changed.
123 view()->SchedulePaint();
126 void FindBarHost::SetFindTextAndSelectedRange(
127 const base::string16
& find_text
,
128 const gfx::Range
& selected_range
) {
129 find_bar_view()->SetFindTextAndSelectedRange(find_text
, selected_range
);
132 base::string16
FindBarHost::GetFindText() {
133 return find_bar_view()->GetFindText();
136 gfx::Range
FindBarHost::GetSelectedRange() {
137 return find_bar_view()->GetSelectedRange();
140 void FindBarHost::UpdateUIForFindResult(const FindNotificationDetails
& result
,
141 const base::string16
& find_text
) {
142 // Make sure match count is clear. It may get set again in UpdateForResult
143 // if enough data is available.
144 find_bar_view()->ClearMatchCount();
146 if (!find_text
.empty())
147 find_bar_view()->UpdateForResult(result
, find_text
);
149 // We now need to check if the window is obscuring the search results.
150 MoveWindowIfNecessary(result
.selection_rect());
152 // Once we find a match we no longer want to keep track of what had
153 // focus. EndFindSession will then set the focus to the page content.
154 if (result
.number_of_matches() > 0)
158 void FindBarHost::AudibleAlert() {
164 bool FindBarHost::IsFindBarVisible() {
165 return DropdownBarHost::IsVisible();
168 void FindBarHost::RestoreSavedFocus() {
169 if (focus_tracker() == NULL
) {
170 // TODO(brettw): Focus() should be on WebContentsView.
171 find_bar_controller_
->web_contents()->Focus();
173 focus_tracker()->FocusLastFocusedExternalView();
177 bool FindBarHost::HasGlobalFindPasteboard() {
181 void FindBarHost::UpdateFindBarForChangedWebContents() {
184 FindBarTesting
* FindBarHost::GetFindBarTesting() {
188 ////////////////////////////////////////////////////////////////////////////////
189 // FindBarWin, ui::AcceleratorTarget implementation:
191 bool FindBarHost::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
192 ui::KeyboardCode key
= accelerator
.key_code();
193 if (key
== ui::VKEY_RETURN
&& accelerator
.IsCtrlDown()) {
194 // Ctrl+Enter closes the Find session and navigates any link that is active.
195 find_bar_controller_
->EndFindSession(
196 FindBarController::kActivateSelectionOnPage
,
197 FindBarController::kClearResultsInFindBox
);
199 } else if (key
== ui::VKEY_ESCAPE
) {
200 // This will end the Find session and hide the window, causing it to loose
201 // focus and in the process unregister us as the handler for the Escape
202 // accelerator through the OnWillChangeFocus event.
203 find_bar_controller_
->EndFindSession(
204 FindBarController::kKeepSelectionOnPage
,
205 FindBarController::kKeepResultsInFindBox
);
208 NOTREACHED() << "Unknown accelerator";
214 bool FindBarHost::CanHandleAccelerators() const {
218 ////////////////////////////////////////////////////////////////////////////////
219 // FindBarTesting implementation:
221 bool FindBarHost::GetFindBarWindowInfo(gfx::Point
* position
,
222 bool* fully_visible
) {
223 if (!find_bar_controller_
||
224 #if defined(OS_WIN) && !defined(USE_AURA)
225 !::IsWindow(host()->GetNativeView())) {
228 // TODO(sky): figure out linux side.
229 // This is tricky due to asynchronous nature of x11.
230 // See bug http://crbug.com/28629.
233 *position
= gfx::Point();
235 *fully_visible
= false;
239 gfx::Rect window_rect
= host()->GetWindowBoundsInScreen();
241 *position
= window_rect
.origin();
243 *fully_visible
= IsVisible() && !IsAnimating();
247 base::string16
FindBarHost::GetFindSelectedText() {
248 return find_bar_view()->GetFindSelectedText();
251 base::string16
FindBarHost::GetMatchCountText() {
252 return find_bar_view()->GetMatchCountText();
255 int FindBarHost::GetWidth() {
256 return view()->width();
259 ////////////////////////////////////////////////////////////////////////////////
260 // Overridden from DropdownBarHost:
262 gfx::Rect
FindBarHost::GetDialogPosition(gfx::Rect avoid_overlapping_rect
) {
263 // Find the area we have to work with (after accounting for scrollbars, etc).
264 gfx::Rect widget_bounds
;
265 GetWidgetBounds(&widget_bounds
);
266 if (widget_bounds
.IsEmpty())
269 // Ask the view how large an area it needs to draw on.
270 gfx::Size prefsize
= view()->GetPreferredSize();
272 // Limit width to the available area.
273 if (widget_bounds
.width() < prefsize
.width())
274 prefsize
.set_width(widget_bounds
.width());
276 // Don't show the find bar if |widget_bounds| is not tall enough.
277 if (widget_bounds
.height() < prefsize
.height())
280 // Place the view in the top right corner of the widget boundaries (top left
281 // for RTL languages).
282 gfx::Rect view_location
;
283 int x
= widget_bounds
.x();
284 if (!base::i18n::IsRTL())
285 x
+= widget_bounds
.width() - prefsize
.width();
286 int y
= widget_bounds
.y();
287 view_location
.SetRect(x
, y
, prefsize
.width(), prefsize
.height());
289 // When we get Find results back, we specify a selection rect, which we
290 // should strive to avoid overlapping. But first, we need to offset the
291 // selection rect (if one was provided).
292 if (!avoid_overlapping_rect
.IsEmpty()) {
293 // For comparison (with the Intersects function below) we need to account
294 // for the fact that we draw the Find widget relative to the Chrome frame,
295 // whereas the selection rect is relative to the page.
296 GetWidgetPositionNative(&avoid_overlapping_rect
);
299 gfx::Rect new_pos
= FindBarController::GetLocationForFindbarView(
300 view_location
, widget_bounds
, avoid_overlapping_rect
);
302 // While we are animating, the Find window will grow bottoms up so we need to
303 // re-position the widget so that it appears to grow out of the toolbar.
304 if (animation_offset() > 0)
305 new_pos
.Offset(0, std::min(0, -animation_offset()));
310 void FindBarHost::SetDialogPosition(const gfx::Rect
& new_pos
) {
311 if (new_pos
.IsEmpty())
314 if (!host()->IsVisible())
316 host()->SetBounds(new_pos
);
318 // Tell the immersive mode controller about the find bar's new bounds. The
319 // immersive mode controller uses the bounds to keep the top-of-window views
320 // revealed when the mouse is hovered over the find bar.
321 browser_view()->immersive_mode_controller()->OnFindBarVisibleBoundsChanged(
322 host()->GetWindowBoundsInScreen());
325 void FindBarHost::GetWidgetBounds(gfx::Rect
* bounds
) {
327 // The BrowserView does Layout for the components that we care about
328 // positioning relative to, so we ask it to tell us where we should go.
329 *bounds
= browser_view()->GetFindBarBoundingBox();
332 void FindBarHost::RegisterAccelerators() {
333 DropdownBarHost::RegisterAccelerators();
335 // Register for Ctrl+Return.
336 ui::Accelerator
escape(ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
);
337 focus_manager()->RegisterAccelerator(
338 escape
, ui::AcceleratorManager::kNormalPriority
, this);
341 void FindBarHost::UnregisterAccelerators() {
342 // Unregister Ctrl+Return.
343 ui::Accelerator
escape(ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
);
344 focus_manager()->UnregisterAccelerator(escape
, this);
346 DropdownBarHost::UnregisterAccelerators();
349 void FindBarHost::OnVisibilityChanged() {
350 // Tell the immersive mode controller about the find bar's bounds. The
351 // immersive mode controller uses the bounds to keep the top-of-window views
352 // revealed when the mouse is hovered over the find bar.
353 gfx::Rect visible_bounds
;
355 visible_bounds
= host()->GetWindowBoundsInScreen();
356 browser_view()->immersive_mode_controller()->OnFindBarVisibleBoundsChanged(
360 ////////////////////////////////////////////////////////////////////////////////
363 void FindBarHost::GetWidgetPositionNative(gfx::Rect
* avoid_overlapping_rect
) {
364 gfx::Rect frame_rect
= host()->GetTopLevelWidget()->GetWindowBoundsInScreen();
365 gfx::Rect webcontents_rect
=
366 find_bar_controller_
->web_contents()->GetViewBounds();
367 avoid_overlapping_rect
->Offset(0, webcontents_rect
.y() - frame_rect
.y());