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/exclusive_access_bubble_views.h"
18 #include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
19 #include "chrome/browser/ui/views/frame/contents_layout_manager.h"
20 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
21 #include "chrome/browser/ui/views/frame/top_container_view.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/base/resource/material_design/material_design_controller.h"
27 #include "ui/gfx/geometry/point.h"
28 #include "ui/gfx/geometry/size.h"
29 #include "ui/gfx/scrollbar_size.h"
30 #include "ui/views/controls/webview/webview.h"
31 #include "ui/views/widget/widget.h"
32 #include "ui/views/window/client_view.h"
35 using web_modal::WebContentsModalDialogHost
;
36 using web_modal::ModalDialogHostObserver
;
40 // The visible height of the shadow above the tabs. Clicks in this area are
41 // treated as clicks to the frame, rather than clicks to the tab.
42 const int kTabShadowSize
= 2;
43 // The number of pixels the constrained window should overlap the bottom
45 const int kConstrainedWindowOverlap
= 3;
47 // Combines View::ConvertPointToTarget and View::HitTest for a given |point|.
48 // Converts |point| from |src| to |dst| and hit tests it against |dst|. The
49 // converted |point| can then be retrieved and used for additional tests.
50 bool ConvertedHitTest(views::View
* src
, views::View
* dst
, gfx::Point
* point
) {
54 views::View::ConvertPointToTarget(src
, dst
, point
);
55 return dst
->HitTestPoint(*point
);
60 class BrowserViewLayout::WebContentsModalDialogHostViews
61 : public WebContentsModalDialogHost
{
63 explicit WebContentsModalDialogHostViews(
64 BrowserViewLayout
* browser_view_layout
)
65 : browser_view_layout_(browser_view_layout
) {
68 ~WebContentsModalDialogHostViews() override
{
69 FOR_EACH_OBSERVER(ModalDialogHostObserver
,
74 void NotifyPositionRequiresUpdate() {
75 FOR_EACH_OBSERVER(ModalDialogHostObserver
,
77 OnPositionRequiresUpdate());
80 gfx::Point
GetDialogPosition(const gfx::Size
& size
) override
{
81 views::View
* view
= browser_view_layout_
->contents_container_
;
82 gfx::Rect content_area
= view
->ConvertRectToWidget(view
->GetLocalBounds());
83 const int middle_x
= content_area
.x() + content_area
.width() / 2;
84 const int top
= browser_view_layout_
->web_contents_modal_dialog_top_y_
;
85 return gfx::Point(middle_x
- size
.width() / 2, top
);
88 gfx::Size
GetMaximumDialogSize() override
{
89 views::View
* view
= browser_view_layout_
->contents_container_
;
90 gfx::Rect content_area
= view
->ConvertRectToWidget(view
->GetLocalBounds());
91 const int top
= browser_view_layout_
->web_contents_modal_dialog_top_y_
;
92 return gfx::Size(content_area
.width(), content_area
.bottom() - top
);
96 gfx::NativeView
GetHostView() const override
{
97 gfx::NativeWindow window
=
98 browser_view_layout_
->browser()->window()->GetNativeWindow();
99 return views::Widget::GetWidgetForNativeWindow(window
)->GetNativeView();
102 // Add/remove observer.
103 void AddObserver(ModalDialogHostObserver
* observer
) override
{
104 observer_list_
.AddObserver(observer
);
106 void RemoveObserver(ModalDialogHostObserver
* observer
) override
{
107 observer_list_
.RemoveObserver(observer
);
110 BrowserViewLayout
* const browser_view_layout_
;
112 base::ObserverList
<ModalDialogHostObserver
> observer_list_
;
114 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogHostViews
);
118 const int BrowserViewLayout::kToolbarTabStripVerticalOverlap
= 3;
120 ////////////////////////////////////////////////////////////////////////////////
121 // BrowserViewLayout, public:
123 BrowserViewLayout::BrowserViewLayout()
125 browser_view_(nullptr),
126 top_container_(nullptr),
129 bookmark_bar_(nullptr),
130 infobar_container_(nullptr),
131 contents_container_(nullptr),
132 contents_layout_manager_(nullptr),
133 download_shelf_(nullptr),
134 immersive_mode_controller_(nullptr),
135 dialog_host_(new WebContentsModalDialogHostViews(this)),
136 web_contents_modal_dialog_top_y_(-1) {}
138 BrowserViewLayout::~BrowserViewLayout() {
141 void BrowserViewLayout::Init(
142 BrowserViewLayoutDelegate
* delegate
,
144 views::ClientView
* browser_view
,
145 views::View
* top_container
,
147 views::View
* toolbar
,
148 InfoBarContainerView
* infobar_container
,
149 views::View
* contents_container
,
150 ContentsLayoutManager
* contents_layout_manager
,
151 ImmersiveModeController
* immersive_mode_controller
) {
152 delegate_
.reset(delegate
);
154 browser_view_
= browser_view
;
155 top_container_
= top_container
;
156 tab_strip_
= tab_strip
;
158 infobar_container_
= infobar_container
;
159 contents_container_
= contents_container
;
160 contents_layout_manager_
= contents_layout_manager
;
161 immersive_mode_controller_
= immersive_mode_controller
;
164 WebContentsModalDialogHost
*
165 BrowserViewLayout::GetWebContentsModalDialogHost() {
166 return dialog_host_
.get();
169 gfx::Size
BrowserViewLayout::GetMinimumSize() {
170 gfx::Size
tabstrip_size(
171 browser()->SupportsWindowFeature(Browser::FEATURE_TABSTRIP
) ?
172 tab_strip_
->GetMinimumSize() : gfx::Size());
173 gfx::Size
toolbar_size(
174 (browser()->SupportsWindowFeature(Browser::FEATURE_TOOLBAR
) ||
175 browser()->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR
)) ?
176 toolbar_
->GetMinimumSize() : gfx::Size());
177 if (tabstrip_size
.height() && toolbar_size
.height())
178 toolbar_size
.Enlarge(0, -kToolbarTabStripVerticalOverlap
);
179 gfx::Size bookmark_bar_size
;
181 bookmark_bar_
->visible() &&
182 browser()->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR
)) {
183 bookmark_bar_size
= bookmark_bar_
->GetMinimumSize();
184 bookmark_bar_size
.Enlarge(0, -bookmark_bar_
->GetToolbarOverlap());
186 gfx::Size
infobar_container_size(infobar_container_
->GetMinimumSize());
187 // TODO: Adjust the minimum height for the find bar.
189 gfx::Size
contents_size(contents_container_
->GetMinimumSize());
191 int min_height
= delegate_
->GetTopInsetInBrowserView() +
192 tabstrip_size
.height() + toolbar_size
.height() +
193 bookmark_bar_size
.height() + infobar_container_size
.height() +
194 contents_size
.height();
196 tabstrip_size
.width(),
197 toolbar_size
.width(),
198 bookmark_bar_size
.width(),
199 infobar_container_size
.width(),
200 contents_size
.width() };
201 int min_width
= *std::max_element(&widths
[0], &widths
[arraysize(widths
)]);
202 return gfx::Size(min_width
, min_height
);
205 gfx::Rect
BrowserViewLayout::GetFindBarBoundingBox() const {
206 // This function returns the area the Find Bar can be laid out within. This
207 // basically implies the "user-perceived content area" of the browser
208 // window excluding the vertical scrollbar. The "user-perceived content area"
209 // excludes the detached bookmark bar (in the New Tab case) and any infobars
210 // since they are not _visually_ connected to the Toolbar.
212 // First determine the bounding box of the content area in Widget
214 gfx::Rect bounding_box
= contents_container_
->ConvertRectToWidget(
215 contents_container_
->GetLocalBounds());
217 gfx::Rect top_container_bounds
= top_container_
->ConvertRectToWidget(
218 top_container_
->GetLocalBounds());
221 if (immersive_mode_controller_
->IsEnabled() &&
222 !immersive_mode_controller_
->IsRevealed()) {
223 // Position the find bar exactly below the top container. In immersive
224 // fullscreen, when the top-of-window views are not revealed, only the
225 // miniature immersive style tab strip is visible. Do not overlap the
226 // find bar and the tab strip.
227 find_bar_y
= top_container_bounds
.bottom();
228 } else if (ui::MaterialDesignController::IsModeMaterial()) {
229 find_bar_y
= top_container_bounds
.bottom() - 6;
231 // Position the find bar 1 pixel above the bottom of the top container
232 // so that it occludes the border between the content area and the top
233 // container and looks connected to the top container.
234 find_bar_y
= top_container_bounds
.bottom() - 1;
237 // Grow the height of |bounding_box| by the height of any elements between
238 // the top container and |contents_container_| such as the detached bookmark
239 // bar and any infobars.
240 int height_delta
= bounding_box
.y() - find_bar_y
;
241 bounding_box
.set_y(find_bar_y
);
242 bounding_box
.set_height(std::max(0, bounding_box
.height() + height_delta
));
244 // Finally decrease the width of the bounding box by the width of
245 // the vertical scroll bar.
246 int scrollbar_width
= gfx::scrollbar_size();
247 bounding_box
.set_width(std::max(0, bounding_box
.width() - scrollbar_width
));
248 if (base::i18n::IsRTL())
249 bounding_box
.set_x(bounding_box
.x() + scrollbar_width
);
254 int BrowserViewLayout::NonClientHitTest(const gfx::Point
& point
) {
255 // Since the TabStrip only renders in some parts of the top of the window,
256 // the un-obscured area is considered to be part of the non-client caption
257 // area of the window. So we need to treat hit-tests in these regions as
258 // hit-tests of the titlebar.
260 views::View
* parent
= browser_view_
->parent();
262 gfx::Point
point_in_browser_view_coords(point
);
263 views::View::ConvertPointToTarget(
264 parent
, browser_view_
, &point_in_browser_view_coords
);
265 gfx::Point
test_point(point
);
267 // Determine if the TabStrip exists and is capable of being clicked on. We
268 // might be a popup window without a TabStrip.
269 if (delegate_
->IsTabStripVisible()) {
270 // See if the mouse pointer is within the bounds of the TabStrip.
271 if (ConvertedHitTest(parent
, tab_strip_
, &test_point
)) {
272 if (tab_strip_
->IsPositionInWindowCaption(test_point
))
277 // The top few pixels of the TabStrip are a drop-shadow - as we're pretty
278 // starved of dragable area, let's give it to window dragging (this also
279 // makes sense visually).
280 views::Widget
* widget
= browser_view_
->GetWidget();
281 if (!(widget
->IsMaximized() || widget
->IsFullscreen()) &&
282 (point_in_browser_view_coords
.y() <
283 (tab_strip_
->y() + kTabShadowSize
))) {
284 // We return HTNOWHERE as this is a signal to our containing
285 // NonClientView that it should figure out what the correct hit-test
286 // code is given the mouse position...
291 // If the point's y coordinate is below the top of the toolbar and otherwise
292 // within the bounds of this view, the point is considered to be within the
294 gfx::Rect bv_bounds
= browser_view_
->bounds();
295 bv_bounds
.Offset(0, toolbar_
->y());
296 bv_bounds
.set_height(bv_bounds
.height() - toolbar_
->y());
297 if (bv_bounds
.Contains(point
))
300 // If the point's y coordinate is above the top of the toolbar, but not
301 // over the tabstrip (per previous checking in this function), then we
302 // consider it in the window caption (e.g. the area to the right of the
303 // tabstrip underneath the window controls). However, note that we DO NOT
304 // return HTCAPTION here, because when the window is maximized the window
305 // controls will fall into this space (since the BrowserView is sized to
306 // entire size of the window at that point), and the HTCAPTION value will
307 // cause the window controls not to work. So we return HTNOWHERE so that the
308 // caller will hit-test the window controls before finally falling back to
310 bv_bounds
= browser_view_
->bounds();
311 bv_bounds
.set_height(toolbar_
->y());
312 if (bv_bounds
.Contains(point
))
315 // If the point is somewhere else, delegate to the default implementation.
316 return browser_view_
->views::ClientView::NonClientHitTest(point
);
319 //////////////////////////////////////////////////////////////////////////////
320 // BrowserViewLayout, views::LayoutManager implementation:
322 void BrowserViewLayout::Layout(views::View
* browser_view
) {
323 vertical_layout_rect_
= browser_view
->GetLocalBounds();
324 int top
= delegate_
->GetTopInsetInBrowserView();
325 top
= LayoutTabStripRegion(top
);
326 if (delegate_
->IsTabStripVisible()) {
327 int x
= tab_strip_
->GetMirroredX() +
328 browser_view_
->GetMirroredX() +
329 delegate_
->GetThemeBackgroundXInset();
330 int y
= browser_view_
->y() + delegate_
->GetTopInsetInBrowserView();
331 tab_strip_
->SetBackgroundOffset(gfx::Point(x
, y
));
333 top
= LayoutToolbar(top
);
335 top
= LayoutBookmarkAndInfoBars(top
, browser_view
->y());
337 // Top container requires updated toolbar and bookmark bar to compute bounds.
338 UpdateTopContainerBounds();
340 int bottom
= LayoutDownloadShelf(browser_view
->height());
341 // Treat a detached bookmark bar as if the web contents container is shifted
342 // upwards and overlaps it.
343 int active_top_margin
= GetContentsOffsetForBookmarkBar();
344 contents_layout_manager_
->SetActiveTopMargin(active_top_margin
);
345 top
-= active_top_margin
;
346 LayoutContentsContainerView(top
, bottom
);
348 // This must be done _after_ we lay out the WebContents since this
349 // code calls back into us to find the bounding box the find bar
350 // must be laid out within, and that code depends on the
351 // TabContentsContainer's bounds being up to date.
352 if (browser()->HasFindBarController()) {
353 browser()->GetFindBarController()->find_bar()->MoveWindowIfNecessary(
357 // Adjust the fullscreen exit bubble bounds for |top_container_|'s new bounds.
358 // This makes the fullscreen exit bubble look like it animates with
359 // |top_container_| in immersive fullscreen.
360 ExclusiveAccessBubbleViews
* exclusive_access_bubble
=
361 delegate_
->GetExclusiveAccessBubble();
362 if (exclusive_access_bubble
)
363 exclusive_access_bubble
->RepositionIfVisible();
365 // Adjust any hosted dialogs if the browser's dialog hosting bounds changed.
366 const gfx::Rect
dialog_bounds(dialog_host_
->GetDialogPosition(gfx::Size()),
367 dialog_host_
->GetMaximumDialogSize());
368 if (latest_dialog_bounds_
!= dialog_bounds
) {
369 latest_dialog_bounds_
= dialog_bounds
;
370 dialog_host_
->NotifyPositionRequiresUpdate();
374 // Return the preferred size which is the size required to give each
375 // children their respective preferred size.
376 gfx::Size
BrowserViewLayout::GetPreferredSize(const views::View
* host
) const {
380 //////////////////////////////////////////////////////////////////////////////
381 // BrowserViewLayout, private:
383 int BrowserViewLayout::LayoutTabStripRegion(int top
) {
384 if (!delegate_
->IsTabStripVisible()) {
385 tab_strip_
->SetVisible(false);
386 tab_strip_
->SetBounds(0, 0, 0, 0);
389 // This retrieves the bounds for the tab strip based on whether or not we show
390 // anything to the left of it, like the incognito avatar.
391 gfx::Rect
tabstrip_bounds(delegate_
->GetBoundsForTabStripInBrowserView());
393 tab_strip_
->SetVisible(true);
394 tab_strip_
->SetBoundsRect(tabstrip_bounds
);
396 return tabstrip_bounds
.bottom();
399 int BrowserViewLayout::LayoutToolbar(int top
) {
400 int browser_view_width
= vertical_layout_rect_
.width();
401 bool toolbar_visible
= delegate_
->IsToolbarVisible();
403 y
-= (toolbar_visible
&& delegate_
->IsTabStripVisible()) ?
404 kToolbarTabStripVerticalOverlap
: 0;
405 int height
= toolbar_visible
? toolbar_
->GetPreferredSize().height() : 0;
406 toolbar_
->SetVisible(toolbar_visible
);
407 toolbar_
->SetBounds(vertical_layout_rect_
.x(), y
, browser_view_width
, height
);
412 int BrowserViewLayout::LayoutBookmarkAndInfoBars(int top
, int browser_view_y
) {
413 web_contents_modal_dialog_top_y_
=
414 top
+ browser_view_y
- kConstrainedWindowOverlap
;
417 // If we're showing the Bookmark bar in detached style, then we
418 // need to show any Info bar _above_ the Bookmark bar, since the
419 // Bookmark bar is styled to look like it's part of the page.
420 if (bookmark_bar_
->IsDetached()) {
421 web_contents_modal_dialog_top_y_
=
422 top
+ browser_view_y
- kConstrainedWindowOverlap
;
423 return LayoutBookmarkBar(LayoutInfoBar(top
));
425 // Otherwise, Bookmark bar first, Info bar second.
426 top
= std::max(toolbar_
->bounds().bottom(), LayoutBookmarkBar(top
));
429 return LayoutInfoBar(top
);
432 int BrowserViewLayout::LayoutBookmarkBar(int top
) {
434 if (!delegate_
->IsBookmarkBarVisible()) {
435 bookmark_bar_
->SetVisible(false);
436 // TODO(jamescook): Don't change the bookmark bar height when it is
437 // invisible, so we can use its height for layout even in that state.
438 bookmark_bar_
->SetBounds(0, y
, browser_view_
->width(), 0);
442 bookmark_bar_
->set_infobar_visible(InfobarVisible());
443 int bookmark_bar_height
= bookmark_bar_
->GetPreferredSize().height();
444 y
-= bookmark_bar_
->GetToolbarOverlap();
445 bookmark_bar_
->SetBounds(vertical_layout_rect_
.x(),
447 vertical_layout_rect_
.width(),
448 bookmark_bar_height
);
449 // Set visibility after setting bounds, as the visibility update uses the
450 // bounds to determine if the mouse is hovering over a button.
451 bookmark_bar_
->SetVisible(true);
452 return y
+ bookmark_bar_height
;
455 int BrowserViewLayout::LayoutInfoBar(int top
) {
456 // In immersive fullscreen, the infobar always starts near the top of the
457 // screen, just under the "light bar" rectangular stripes.
458 if (immersive_mode_controller_
->IsEnabled()) {
459 top
= browser_view_
->y();
460 if (!immersive_mode_controller_
->ShouldHideTabIndicators())
461 top
+= TabStrip::GetImmersiveHeight();
463 // Raise the |infobar_container_| by its vertical overlap.
464 infobar_container_
->SetVisible(InfobarVisible());
466 int overlapped_top
= top
- infobar_container_
->GetVerticalOverlap(&height
);
467 infobar_container_
->SetBounds(vertical_layout_rect_
.x(),
469 vertical_layout_rect_
.width(),
471 return overlapped_top
+ height
;
474 void BrowserViewLayout::LayoutContentsContainerView(int top
, int bottom
) {
475 // |contents_container_| contains web page contents and devtools.
476 // See browser_view.h for details.
477 gfx::Rect
contents_container_bounds(vertical_layout_rect_
.x(),
479 vertical_layout_rect_
.width(),
480 std::max(0, bottom
- top
));
481 contents_container_
->SetBoundsRect(contents_container_bounds
);
484 void BrowserViewLayout::UpdateTopContainerBounds() {
485 // Set the bounds of the top container view such that it is tall enough to
486 // fully show all of its children. In particular, the bottom of the bookmark
487 // bar can be above the bottom of the toolbar while the bookmark bar is
488 // animating. The top container view is positioned relative to the top of the
489 // client view instead of relative to GetTopInsetInBrowserView() because the
490 // top container view paints parts of the frame (title, window controls)
491 // during an immersive fullscreen reveal.
493 for (int i
= 0; i
< top_container_
->child_count(); ++i
) {
494 views::View
* child
= top_container_
->child_at(i
);
495 if (!child
->visible())
497 int child_bottom
= child
->bounds().bottom();
498 if (child_bottom
> height
)
499 height
= child_bottom
;
502 // Ensure that the top container view reaches the topmost view in the
503 // ClientView because the bounds of the top container view are used in
504 // layout and we assume that this is the case.
505 height
= std::max(height
, delegate_
->GetTopInsetInBrowserView());
507 gfx::Rect
top_container_bounds(vertical_layout_rect_
.width(), height
);
509 // If the immersive mode controller is animating the top container, it may be
511 top_container_bounds
.set_y(
512 immersive_mode_controller_
->GetTopContainerVerticalOffset(
513 top_container_bounds
.size()));
514 top_container_
->SetBoundsRect(top_container_bounds
);
517 int BrowserViewLayout::GetContentsOffsetForBookmarkBar() {
518 // If the bookmark bar is hidden or attached to the omnibox the web contents
519 // will appear directly underneath it and does not need an offset.
520 if (!bookmark_bar_
||
521 !delegate_
->IsBookmarkBarVisible() ||
522 !bookmark_bar_
->IsDetached()) {
526 // Offset for the detached bookmark bar.
527 return bookmark_bar_
->height() -
528 bookmark_bar_
->GetFullyDetachedToolbarOverlap();
531 int BrowserViewLayout::LayoutDownloadShelf(int bottom
) {
532 if (delegate_
->DownloadShelfNeedsLayout()) {
533 bool visible
= browser()->SupportsWindowFeature(
534 Browser::FEATURE_DOWNLOADSHELF
);
535 DCHECK(download_shelf_
);
536 int height
= visible
? download_shelf_
->GetPreferredSize().height() : 0;
537 download_shelf_
->SetVisible(visible
);
538 download_shelf_
->SetBounds(vertical_layout_rect_
.x(), bottom
- height
,
539 vertical_layout_rect_
.width(), height
);
540 download_shelf_
->Layout();
546 bool BrowserViewLayout::InfobarVisible() const {
547 // Cast to a views::View to access GetPreferredSize().
548 views::View
* infobar_container
= infobar_container_
;
549 // NOTE: Can't check if the size IsEmpty() since it's always 0-width.
550 return browser_
->SupportsWindowFeature(Browser::FEATURE_INFOBAR
) &&
551 (infobar_container
->GetPreferredSize().height() != 0);