1 // Copyright 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/frame/browser_view_layout.h"
7 #include "base/observer_list.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/find_bar/find_bar.h"
13 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
14 #include "chrome/browser/ui/search/search_model.h"
15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
16 #include "chrome/browser/ui/views/download/download_shelf_view.h"
17 #include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
18 #include "chrome/browser/ui/views/frame/contents_layout_manager.h"
19 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
20 #include "chrome/browser/ui/views/frame/top_container_view.h"
21 #include "chrome/browser/ui/views/fullscreen_exit_bubble_views.h"
22 #include "chrome/browser/ui/views/infobars/infobar_container_view.h"
23 #include "chrome/browser/ui/views/tabs/tab_strip.h"
24 #include "components/web_modal/web_contents_modal_dialog_host.h"
25 #include "ui/base/hit_test.h"
26 #include "ui/gfx/point.h"
27 #include "ui/gfx/scrollbar_size.h"
28 #include "ui/gfx/size.h"
29 #include "ui/views/controls/webview/webview.h"
30 #include "ui/views/widget/widget.h"
31 #include "ui/views/window/client_view.h"
34 using web_modal::WebContentsModalDialogHost
;
35 using web_modal::ModalDialogHostObserver
;
39 // The visible height of the shadow above the tabs. Clicks in this area are
40 // treated as clicks to the frame, rather than clicks to the tab.
41 const int kTabShadowSize
= 2;
42 // The number of pixels the metro switcher is offset from the right edge.
43 const int kWindowSwitcherOffsetX
= 7;
44 // The number of pixels the constrained window should overlap the bottom
46 const int kConstrainedWindowOverlap
= 3;
48 // Combines View::ConvertPointToTarget and View::HitTest for a given |point|.
49 // Converts |point| from |src| to |dst| and hit tests it against |dst|. The
50 // converted |point| can then be retrieved and used for additional tests.
51 bool ConvertedHitTest(views::View
* src
, views::View
* dst
, gfx::Point
* point
) {
55 views::View::ConvertPointToTarget(src
, dst
, point
);
56 return dst
->HitTestPoint(*point
);
61 class BrowserViewLayout::WebContentsModalDialogHostViews
62 : public WebContentsModalDialogHost
{
64 explicit WebContentsModalDialogHostViews(
65 BrowserViewLayout
* browser_view_layout
)
66 : browser_view_layout_(browser_view_layout
) {
69 virtual ~WebContentsModalDialogHostViews() {
70 FOR_EACH_OBSERVER(ModalDialogHostObserver
,
75 void NotifyPositionRequiresUpdate() {
76 FOR_EACH_OBSERVER(ModalDialogHostObserver
,
78 OnPositionRequiresUpdate());
81 virtual gfx::Point
GetDialogPosition(const gfx::Size
& size
) OVERRIDE
{
82 views::View
* view
= browser_view_layout_
->delegate_
->GetContentsWebView();
83 gfx::Rect content_area
= view
->ConvertRectToWidget(view
->GetLocalBounds());
84 const int middle_x
= content_area
.x() + content_area
.width() / 2;
85 const int top
= browser_view_layout_
->web_contents_modal_dialog_top_y_
;
86 return gfx::Point(middle_x
- size
.width() / 2, top
);
89 virtual gfx::Size
GetMaximumDialogSize() OVERRIDE
{
90 views::View
* view
= browser_view_layout_
->delegate_
->GetContentsWebView();
91 gfx::Rect content_area
= view
->ConvertRectToWidget(view
->GetLocalBounds());
92 const int top
= browser_view_layout_
->web_contents_modal_dialog_top_y_
;
93 return gfx::Size(content_area
.width(), content_area
.bottom() - top
);
97 virtual gfx::NativeView
GetHostView() const OVERRIDE
{
98 gfx::NativeWindow window
=
99 browser_view_layout_
->browser()->window()->GetNativeWindow();
100 return views::Widget::GetWidgetForNativeWindow(window
)->GetNativeView();
103 // Add/remove observer.
104 virtual void AddObserver(ModalDialogHostObserver
* observer
) OVERRIDE
{
105 observer_list_
.AddObserver(observer
);
107 virtual void RemoveObserver(ModalDialogHostObserver
* observer
) OVERRIDE
{
108 observer_list_
.RemoveObserver(observer
);
111 BrowserViewLayout
* const browser_view_layout_
;
113 ObserverList
<ModalDialogHostObserver
> observer_list_
;
115 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogHostViews
);
119 const int BrowserViewLayout::kToolbarTabStripVerticalOverlap
= 3;
121 ////////////////////////////////////////////////////////////////////////////////
122 // BrowserViewLayout, public:
124 BrowserViewLayout::BrowserViewLayout()
127 top_container_(NULL
),
131 infobar_container_(NULL
),
132 contents_container_(NULL
),
133 contents_layout_manager_(NULL
),
134 download_shelf_(NULL
),
135 immersive_mode_controller_(NULL
),
136 dialog_host_(new WebContentsModalDialogHostViews(this)),
137 web_contents_modal_dialog_top_y_(-1) {}
139 BrowserViewLayout::~BrowserViewLayout() {
142 void BrowserViewLayout::Init(
143 BrowserViewLayoutDelegate
* delegate
,
145 views::ClientView
* browser_view
,
146 views::View
* top_container
,
148 views::View
* toolbar
,
149 InfoBarContainerView
* infobar_container
,
150 views::View
* contents_container
,
151 ContentsLayoutManager
* contents_layout_manager
,
152 ImmersiveModeController
* immersive_mode_controller
) {
153 delegate_
.reset(delegate
);
155 browser_view_
= browser_view
;
156 top_container_
= top_container
;
157 tab_strip_
= tab_strip
;
159 infobar_container_
= infobar_container
;
160 contents_container_
= contents_container
;
161 contents_layout_manager_
= contents_layout_manager
;
162 immersive_mode_controller_
= immersive_mode_controller
;
165 WebContentsModalDialogHost
*
166 BrowserViewLayout::GetWebContentsModalDialogHost() {
167 return dialog_host_
.get();
170 gfx::Size
BrowserViewLayout::GetMinimumSize() {
171 gfx::Size
tabstrip_size(
172 browser()->SupportsWindowFeature(Browser::FEATURE_TABSTRIP
) ?
173 tab_strip_
->GetMinimumSize() : gfx::Size());
174 gfx::Size
toolbar_size(
175 (browser()->SupportsWindowFeature(Browser::FEATURE_TOOLBAR
) ||
176 browser()->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
)) ?
177 toolbar_
->GetMinimumSize() : gfx::Size());
178 if (tabstrip_size
.height() && toolbar_size
.height())
179 toolbar_size
.Enlarge(0, -kToolbarTabStripVerticalOverlap
);
180 gfx::Size bookmark_bar_size
;
182 bookmark_bar_
->visible() &&
183 browser()->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR
)) {
184 bookmark_bar_size
= bookmark_bar_
->GetMinimumSize();
185 bookmark_bar_size
.Enlarge(0, -bookmark_bar_
->GetToolbarOverlap());
187 gfx::Size
infobar_container_size(infobar_container_
->GetMinimumSize());
188 // TODO: Adjust the minimum height for the find bar.
190 gfx::Size
contents_size(contents_container_
->GetMinimumSize());
192 int min_height
= delegate_
->GetTopInsetInBrowserView() +
193 tabstrip_size
.height() + toolbar_size
.height() +
194 bookmark_bar_size
.height() + infobar_container_size
.height() +
195 contents_size
.height();
197 tabstrip_size
.width(),
198 toolbar_size
.width(),
199 bookmark_bar_size
.width(),
200 infobar_container_size
.width(),
201 contents_size
.width() };
202 int min_width
= *std::max_element(&widths
[0], &widths
[arraysize(widths
)]);
203 return gfx::Size(min_width
, min_height
);
206 gfx::Rect
BrowserViewLayout::GetFindBarBoundingBox() const {
207 // This function returns the area the Find Bar can be laid out within. This
208 // basically implies the "user-perceived content area" of the browser
209 // window excluding the vertical scrollbar. The "user-perceived content area"
210 // excludes the detached bookmark bar (in the New Tab case) and any infobars
211 // since they are not _visually_ connected to the Toolbar.
213 // First determine the bounding box of the content area in Widget
215 gfx::Rect bounding_box
= contents_container_
->ConvertRectToWidget(
216 contents_container_
->GetLocalBounds());
218 gfx::Rect top_container_bounds
= top_container_
->ConvertRectToWidget(
219 top_container_
->GetLocalBounds());
222 if (immersive_mode_controller_
->IsEnabled() &&
223 !immersive_mode_controller_
->IsRevealed()) {
224 // Position the find bar exactly below the top container. In immersive
225 // fullscreen, when the top-of-window views are not revealed, only the
226 // miniature immersive style tab strip is visible. Do not overlap the
227 // find bar and the tab strip.
228 find_bar_y
= top_container_bounds
.bottom();
230 // Position the find bar 1 pixel above the bottom of the top container
231 // so that it occludes the border between the content area and the top
232 // container and looks connected to the top container.
233 find_bar_y
= top_container_bounds
.bottom() - 1;
236 // Grow the height of |bounding_box| by the height of any elements between
237 // the top container and |contents_container_| such as the detached bookmark
238 // bar and any infobars.
239 int height_delta
= bounding_box
.y() - find_bar_y
;
240 bounding_box
.set_y(find_bar_y
);
241 bounding_box
.set_height(std::max(0, bounding_box
.height() + height_delta
));
243 // Finally decrease the width of the bounding box by the width of
244 // the vertical scroll bar.
245 int scrollbar_width
= gfx::scrollbar_size();
246 bounding_box
.set_width(std::max(0, bounding_box
.width() - scrollbar_width
));
247 if (base::i18n::IsRTL())
248 bounding_box
.set_x(bounding_box
.x() + scrollbar_width
);
253 int BrowserViewLayout::NonClientHitTest(const gfx::Point
& point
) {
254 // Since the TabStrip only renders in some parts of the top of the window,
255 // the un-obscured area is considered to be part of the non-client caption
256 // area of the window. So we need to treat hit-tests in these regions as
257 // hit-tests of the titlebar.
259 views::View
* parent
= browser_view_
->parent();
261 gfx::Point
point_in_browser_view_coords(point
);
262 views::View::ConvertPointToTarget(
263 parent
, browser_view_
, &point_in_browser_view_coords
);
264 gfx::Point
test_point(point
);
266 // Determine if the TabStrip exists and is capable of being clicked on. We
267 // might be a popup window without a TabStrip.
268 if (delegate_
->IsTabStripVisible()) {
269 // See if the mouse pointer is within the bounds of the TabStrip.
270 if (ConvertedHitTest(parent
, tab_strip_
, &test_point
)) {
271 if (tab_strip_
->IsPositionInWindowCaption(test_point
))
276 // The top few pixels of the TabStrip are a drop-shadow - as we're pretty
277 // starved of dragable area, let's give it to window dragging (this also
278 // makes sense visually).
279 views::Widget
* widget
= browser_view_
->GetWidget();
280 if (!(widget
->IsMaximized() || widget
->IsFullscreen()) &&
281 (point_in_browser_view_coords
.y() <
282 (tab_strip_
->y() + kTabShadowSize
))) {
283 // We return HTNOWHERE as this is a signal to our containing
284 // NonClientView that it should figure out what the correct hit-test
285 // code is given the mouse position...
290 // If the point's y coordinate is below the top of the toolbar and otherwise
291 // within the bounds of this view, the point is considered to be within the
293 gfx::Rect bv_bounds
= browser_view_
->bounds();
294 bv_bounds
.Offset(0, toolbar_
->y());
295 bv_bounds
.set_height(bv_bounds
.height() - toolbar_
->y());
296 if (bv_bounds
.Contains(point
))
299 // If the point is within the bounds of the window switcher button, the point
300 // is considered to be within the client area.
301 views::View
* window_switcher_button
= delegate_
->GetWindowSwitcherButton();
302 if (window_switcher_button
&& window_switcher_button
->visible()) {
303 gfx::Point
window_switcher_point(point_in_browser_view_coords
);
304 views::View::ConvertPointToTarget(browser_view_
, window_switcher_button
,
305 &window_switcher_point
);
306 if (window_switcher_button
->HitTestPoint(window_switcher_point
))
310 // If the point's y coordinate is above the top of the toolbar, but neither
311 // over the tabstrip nor over the window switcher button (per previous
312 // checking in this function), then we consider it in the window caption
313 // (e.g. the area to the right of the tabstrip underneath the window
314 // controls). However, note that we DO NOT return HTCAPTION here, because
315 // when the window is maximized the window controls will fall into this
316 // space (since the BrowserView is sized to entire size of the window at that
317 // point), and the HTCAPTION value will cause the window controls not to work.
318 // So we return HTNOWHERE so that the caller will hit-test the window controls
319 // before finally falling back to HTCAPTION.
320 bv_bounds
= browser_view_
->bounds();
321 bv_bounds
.set_height(toolbar_
->y());
322 if (bv_bounds
.Contains(point
))
325 // If the point is somewhere else, delegate to the default implementation.
326 return browser_view_
->views::ClientView::NonClientHitTest(point
);
329 //////////////////////////////////////////////////////////////////////////////
330 // BrowserViewLayout, views::LayoutManager implementation:
332 void BrowserViewLayout::Layout(views::View
* browser_view
) {
333 vertical_layout_rect_
= browser_view
->GetLocalBounds();
334 int top
= delegate_
->GetTopInsetInBrowserView();
335 top
= LayoutTabStripRegion(top
);
336 if (delegate_
->IsTabStripVisible()) {
337 int x
= tab_strip_
->GetMirroredX() +
338 browser_view_
->GetMirroredX() +
339 delegate_
->GetThemeBackgroundXInset();
340 int y
= browser_view_
->y() + delegate_
->GetTopInsetInBrowserView();
341 tab_strip_
->SetBackgroundOffset(gfx::Point(x
, y
));
343 top
= LayoutToolbar(top
);
345 top
= LayoutBookmarkAndInfoBars(top
, browser_view
->y());
347 // Top container requires updated toolbar and bookmark bar to compute bounds.
348 UpdateTopContainerBounds();
350 int bottom
= LayoutDownloadShelf(browser_view
->height());
351 // Treat a detached bookmark bar as if the web contents container is shifted
352 // upwards and overlaps it.
353 int active_top_margin
= GetContentsOffsetForBookmarkBar();
354 contents_layout_manager_
->SetActiveTopMargin(active_top_margin
);
355 top
-= active_top_margin
;
356 LayoutContentsContainerView(top
, bottom
);
358 // This must be done _after_ we lay out the WebContents since this
359 // code calls back into us to find the bounding box the find bar
360 // must be laid out within, and that code depends on the
361 // TabContentsContainer's bounds being up to date.
362 if (browser()->HasFindBarController()) {
363 browser()->GetFindBarController()->find_bar()->MoveWindowIfNecessary(
367 // Adjust the fullscreen exit bubble bounds for |top_container_|'s new bounds.
368 // This makes the fullscreen exit bubble look like it animates with
369 // |top_container_| in immersive fullscreen.
370 FullscreenExitBubbleViews
* fullscreen_exit_bubble
=
371 delegate_
->GetFullscreenExitBubble();
372 if (fullscreen_exit_bubble
)
373 fullscreen_exit_bubble
->RepositionIfVisible();
375 // Adjust any hosted dialogs if the browser's dialog hosting bounds changed.
376 const gfx::Rect
dialog_bounds(dialog_host_
->GetDialogPosition(gfx::Size()),
377 dialog_host_
->GetMaximumDialogSize());
378 if (latest_dialog_bounds_
!= dialog_bounds
) {
379 latest_dialog_bounds_
= dialog_bounds
;
380 dialog_host_
->NotifyPositionRequiresUpdate();
384 // Return the preferred size which is the size required to give each
385 // children their respective preferred size.
386 gfx::Size
BrowserViewLayout::GetPreferredSize(views::View
* host
) {
390 //////////////////////////////////////////////////////////////////////////////
391 // BrowserViewLayout, private:
393 int BrowserViewLayout::LayoutTabStripRegion(int top
) {
394 if (!delegate_
->IsTabStripVisible()) {
395 tab_strip_
->SetVisible(false);
396 tab_strip_
->SetBounds(0, 0, 0, 0);
399 // This retrieves the bounds for the tab strip based on whether or not we show
400 // anything to the left of it, like the incognito avatar.
401 gfx::Rect
tabstrip_bounds(delegate_
->GetBoundsForTabStripInBrowserView());
403 tab_strip_
->SetVisible(true);
404 tab_strip_
->SetBoundsRect(tabstrip_bounds
);
405 int bottom
= tabstrip_bounds
.bottom();
407 // The metro window switcher sits at the far right edge of the tabstrip
408 // a |kWindowSwitcherOffsetX| pixels from the right edge.
409 // Only visible if there is more than one type of window to switch between.
410 // TODO(mad): update this code when more window types than just incognito
411 // and regular are available.
412 views::View
* switcher_button
= delegate_
->GetWindowSwitcherButton();
413 if (switcher_button
) {
414 if (browser()->profile()->HasOffTheRecordProfile() &&
415 chrome::FindBrowserWithProfile(
416 browser()->profile()->GetOriginalProfile(),
417 browser()->host_desktop_type()) != NULL
) {
418 switcher_button
->SetVisible(true);
419 int width
= browser_view_
->width();
420 gfx::Size ps
= switcher_button
->GetPreferredSize();
421 if (width
> ps
.width()) {
422 switcher_button
->SetBounds(width
- ps
.width() - kWindowSwitcherOffsetX
,
428 // We hide the button if the incognito profile is not alive.
429 // Note that Layout() is not called to all browser windows automatically
430 // when a profile goes away but we rely in the metro_driver.dll to call
431 // ::SetWindowPos( , .. SWP_SHOWWINDOW) which causes this function to
432 // be called again. This works both in showing or hidding the button.
433 switcher_button
->SetVisible(false);
440 int BrowserViewLayout::LayoutToolbar(int top
) {
441 int browser_view_width
= vertical_layout_rect_
.width();
442 bool toolbar_visible
= delegate_
->IsToolbarVisible();
444 y
-= (toolbar_visible
&& delegate_
->IsTabStripVisible()) ?
445 kToolbarTabStripVerticalOverlap
: 0;
446 int height
= toolbar_visible
? toolbar_
->GetPreferredSize().height() : 0;
447 toolbar_
->SetVisible(toolbar_visible
);
448 toolbar_
->SetBounds(vertical_layout_rect_
.x(), y
, browser_view_width
, height
);
453 int BrowserViewLayout::LayoutBookmarkAndInfoBars(int top
, int browser_view_y
) {
454 web_contents_modal_dialog_top_y_
=
455 top
+ browser_view_y
- kConstrainedWindowOverlap
;
458 // If we're showing the Bookmark bar in detached style, then we
459 // need to show any Info bar _above_ the Bookmark bar, since the
460 // Bookmark bar is styled to look like it's part of the page.
461 if (bookmark_bar_
->IsDetached()) {
462 web_contents_modal_dialog_top_y_
=
463 top
+ browser_view_y
- kConstrainedWindowOverlap
;
464 return LayoutBookmarkBar(LayoutInfoBar(top
));
466 // Otherwise, Bookmark bar first, Info bar second.
467 top
= std::max(toolbar_
->bounds().bottom(), LayoutBookmarkBar(top
));
470 return LayoutInfoBar(top
);
473 int BrowserViewLayout::LayoutBookmarkBar(int top
) {
475 if (!delegate_
->IsBookmarkBarVisible()) {
476 bookmark_bar_
->SetVisible(false);
477 // TODO(jamescook): Don't change the bookmark bar height when it is
478 // invisible, so we can use its height for layout even in that state.
479 bookmark_bar_
->SetBounds(0, y
, browser_view_
->width(), 0);
483 bookmark_bar_
->set_infobar_visible(InfobarVisible());
484 int bookmark_bar_height
= bookmark_bar_
->GetPreferredSize().height();
485 y
-= bookmark_bar_
->GetToolbarOverlap();
486 bookmark_bar_
->SetBounds(vertical_layout_rect_
.x(),
488 vertical_layout_rect_
.width(),
489 bookmark_bar_height
);
490 // Set visibility after setting bounds, as the visibility update uses the
491 // bounds to determine if the mouse is hovering over a button.
492 bookmark_bar_
->SetVisible(true);
493 return y
+ bookmark_bar_height
;
496 int BrowserViewLayout::LayoutInfoBar(int top
) {
497 // In immersive fullscreen, the infobar always starts near the top of the
498 // screen, just under the "light bar" rectangular stripes.
499 if (immersive_mode_controller_
->IsEnabled()) {
500 top
= immersive_mode_controller_
->ShouldHideTabIndicators()
502 : browser_view_
->y() + TabStrip::GetImmersiveHeight();
504 // Raise the |infobar_container_| by its vertical overlap.
505 infobar_container_
->SetVisible(InfobarVisible());
507 int overlapped_top
= top
- infobar_container_
->GetVerticalOverlap(&height
);
508 infobar_container_
->SetBounds(vertical_layout_rect_
.x(),
510 vertical_layout_rect_
.width(),
512 return overlapped_top
+ height
;
515 void BrowserViewLayout::LayoutContentsContainerView(int top
, int bottom
) {
516 // |contents_container_| contains web page contents and devtools.
517 // See browser_view.h for details.
518 gfx::Rect
contents_container_bounds(vertical_layout_rect_
.x(),
520 vertical_layout_rect_
.width(),
521 std::max(0, bottom
- top
));
522 contents_container_
->SetBoundsRect(contents_container_bounds
);
525 void BrowserViewLayout::UpdateTopContainerBounds() {
526 gfx::Rect
top_container_bounds(top_container_
->GetPreferredSize());
528 // If the immersive mode controller is animating the top container, it may be
529 // partly offscreen. The top container is positioned relative to the top of
530 // the client view instead of relative to GetTopInsetInBrowserView() because
531 // the top container paints parts of the frame (title, window controls) during
532 // an immersive reveal.
533 top_container_bounds
.set_y(
534 immersive_mode_controller_
->GetTopContainerVerticalOffset(
535 top_container_bounds
.size()));
536 top_container_
->SetBoundsRect(top_container_bounds
);
539 int BrowserViewLayout::GetContentsOffsetForBookmarkBar() {
540 // If the bookmark bar is hidden or attached to the omnibox the web contents
541 // will appear directly underneath it and does not need an offset.
542 if (!bookmark_bar_
||
543 !delegate_
->IsBookmarkBarVisible() ||
544 !bookmark_bar_
->IsDetached()) {
548 // Offset for the detached bookmark bar.
549 return bookmark_bar_
->height() -
550 bookmark_bar_
->GetFullyDetachedToolbarOverlap();
553 int BrowserViewLayout::LayoutDownloadShelf(int bottom
) {
554 if (delegate_
->DownloadShelfNeedsLayout()) {
555 bool visible
= browser()->SupportsWindowFeature(
556 Browser::FEATURE_DOWNLOADSHELF
);
557 DCHECK(download_shelf_
);
558 int height
= visible
? download_shelf_
->GetPreferredSize().height() : 0;
559 download_shelf_
->SetVisible(visible
);
560 download_shelf_
->SetBounds(vertical_layout_rect_
.x(), bottom
- height
,
561 vertical_layout_rect_
.width(), height
);
562 download_shelf_
->Layout();
568 bool BrowserViewLayout::InfobarVisible() const {
569 // Cast to a views::View to access GetPreferredSize().
570 views::View
* infobar_container
= infobar_container_
;
571 // NOTE: Can't check if the size IsEmpty() since it's always 0-width.
572 return browser_
->SupportsWindowFeature(Browser::FEATURE_INFOBAR
) &&
573 (infobar_container
->GetPreferredSize().height() != 0);