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 if (!ShouldForwardKeyEventToWebpageNative(key_event
)) {
50 // Native implementation says not to forward these events.
54 switch (key_event
.key_code()) {
62 if (key_event
.IsControlDown())
69 content::WebContents
* contents
= find_bar_controller_
->web_contents();
73 content::RenderViewHost
* render_view_host
= contents
->GetRenderViewHost();
75 // Make sure we don't have a text field element interfering with keyboard
76 // input. Otherwise Up and Down arrow key strokes get eaten. "Nom Nom Nom".
77 render_view_host
->ClearFocusedElement();
78 NativeWebKeyboardEvent event
= GetKeyboardEvent(contents
, key_event
);
79 render_view_host
->ForwardKeyboardEvent(event
);
83 FindBarController
* FindBarHost::GetFindBarController() const {
84 return find_bar_controller_
;
87 void FindBarHost::SetFindBarController(FindBarController
* find_bar_controller
) {
88 find_bar_controller_
= find_bar_controller
;
91 void FindBarHost::Show(bool animate
) {
92 DropdownBarHost::Show(animate
);
95 void FindBarHost::Hide(bool animate
) {
96 DropdownBarHost::Hide(animate
);
99 void FindBarHost::SetFocusAndSelection() {
100 DropdownBarHost::SetFocusAndSelection();
103 void FindBarHost::ClearResults(const FindNotificationDetails
& results
) {
104 find_bar_view()->UpdateForResult(results
, base::string16());
107 void FindBarHost::StopAnimation() {
108 DropdownBarHost::StopAnimation();
111 void FindBarHost::MoveWindowIfNecessary(const gfx::Rect
& selection_rect
,
113 // We only move the window if one is active for the current WebContents. If we
114 // don't check this, then SetWidgetPosition below will end up making the Find
116 content::WebContents
* web_contents
= find_bar_controller_
->web_contents();
120 FindTabHelper
* find_tab_helper
= FindTabHelper::FromWebContents(web_contents
);
121 if (!find_tab_helper
|| !find_tab_helper
->find_ui_active())
124 gfx::Rect new_pos
= GetDialogPosition(selection_rect
);
125 SetDialogPosition(new_pos
, no_redraw
);
127 // May need to redraw our frame to accommodate bookmark bar styles.
128 view()->Layout(); // Bounds may have changed.
129 view()->SchedulePaint();
132 void FindBarHost::SetFindTextAndSelectedRange(
133 const base::string16
& find_text
,
134 const gfx::Range
& selected_range
) {
135 find_bar_view()->SetFindTextAndSelectedRange(find_text
, selected_range
);
138 base::string16
FindBarHost::GetFindText() {
139 return find_bar_view()->GetFindText();
142 gfx::Range
FindBarHost::GetSelectedRange() {
143 return find_bar_view()->GetSelectedRange();
146 void FindBarHost::UpdateUIForFindResult(const FindNotificationDetails
& result
,
147 const base::string16
& find_text
) {
148 // Make sure match count is clear. It may get set again in UpdateForResult
149 // if enough data is available.
150 find_bar_view()->ClearMatchCount();
152 if (!find_text
.empty())
153 find_bar_view()->UpdateForResult(result
, find_text
);
155 // We now need to check if the window is obscuring the search results.
156 MoveWindowIfNecessary(result
.selection_rect(), false);
158 // Once we find a match we no longer want to keep track of what had
159 // focus. EndFindSession will then set the focus to the page content.
160 if (result
.number_of_matches() > 0)
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
, bool no_redraw
) {
311 if (new_pos
.IsEmpty())
314 // Make sure the window edges are clipped to just the visible region. We need
315 // to do this before changing position, so that when we animate the closure
316 // of it it doesn't look like the window crumbles into the toolbar.
317 UpdateWindowEdges(new_pos
);
319 SetWidgetPositionNative(new_pos
, no_redraw
);
321 // Tell the immersive mode controller about the find bar's new bounds. The
322 // immersive mode controller uses the bounds to keep the top-of-window views
323 // revealed when the mouse is hovered over the find bar.
324 browser_view()->immersive_mode_controller()->OnFindBarVisibleBoundsChanged(
325 host()->GetWindowBoundsInScreen());
328 void FindBarHost::GetWidgetBounds(gfx::Rect
* bounds
) {
330 // The BrowserView does Layout for the components that we care about
331 // positioning relative to, so we ask it to tell us where we should go.
332 *bounds
= browser_view()->GetFindBarBoundingBox();
335 void FindBarHost::RegisterAccelerators() {
336 DropdownBarHost::RegisterAccelerators();
338 // Register for Ctrl+Return.
339 ui::Accelerator
escape(ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
);
340 focus_manager()->RegisterAccelerator(
341 escape
, ui::AcceleratorManager::kNormalPriority
, this);
344 void FindBarHost::UnregisterAccelerators() {
345 // Unregister Ctrl+Return.
346 ui::Accelerator
escape(ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
);
347 focus_manager()->UnregisterAccelerator(escape
, this);
349 DropdownBarHost::UnregisterAccelerators();
352 void FindBarHost::OnVisibilityChanged() {
353 // Tell the immersive mode controller about the find bar's bounds. The
354 // immersive mode controller uses the bounds to keep the top-of-window views
355 // revealed when the mouse is hovered over the find bar.
356 gfx::Rect visible_bounds
;
358 visible_bounds
= host()->GetWindowBoundsInScreen();
359 browser_view()->immersive_mode_controller()->OnFindBarVisibleBoundsChanged(
363 ////////////////////////////////////////////////////////////////////////////////
366 void FindBarHost::GetWidgetPositionNative(gfx::Rect
* avoid_overlapping_rect
) {
367 gfx::Rect frame_rect
= host()->GetTopLevelWidget()->GetWindowBoundsInScreen();
368 gfx::Rect webcontents_rect
=
369 find_bar_controller_
->web_contents()->GetViewBounds();
370 avoid_overlapping_rect
->Offset(0, webcontents_rect
.y() - frame_rect
.y());