Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_view_layout.cc
bloba8d1c71940db9671eecdc80bc6967ea6f9f824b6
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/layout_constants.h"
24 #include "chrome/browser/ui/views/tabs/tab_strip.h"
25 #include "components/web_modal/web_contents_modal_dialog_host.h"
26 #include "ui/base/hit_test.h"
27 #include "ui/base/resource/material_design/material_design_controller.h"
28 #include "ui/gfx/geometry/point.h"
29 #include "ui/gfx/geometry/size.h"
30 #include "ui/gfx/scrollbar_size.h"
31 #include "ui/views/controls/webview/webview.h"
32 #include "ui/views/widget/widget.h"
33 #include "ui/views/window/client_view.h"
35 using views::View;
36 using web_modal::WebContentsModalDialogHost;
37 using web_modal::ModalDialogHostObserver;
39 namespace {
41 // The visible height of the shadow above the tabs. Clicks in this area are
42 // treated as clicks to the frame, rather than clicks to the tab.
43 const int kTabShadowSize = 2;
44 // The number of pixels the constrained window should overlap the bottom
45 // of the omnibox.
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) {
52 DCHECK(src);
53 DCHECK(dst);
54 DCHECK(point);
55 views::View::ConvertPointToTarget(src, dst, point);
56 return dst->HitTestPoint(*point);
59 } // namespace
61 class BrowserViewLayout::WebContentsModalDialogHostViews
62 : public WebContentsModalDialogHost {
63 public:
64 explicit WebContentsModalDialogHostViews(
65 BrowserViewLayout* browser_view_layout)
66 : browser_view_layout_(browser_view_layout) {
69 ~WebContentsModalDialogHostViews() override {
70 FOR_EACH_OBSERVER(ModalDialogHostObserver,
71 observer_list_,
72 OnHostDestroying());
75 void NotifyPositionRequiresUpdate() {
76 FOR_EACH_OBSERVER(ModalDialogHostObserver,
77 observer_list_,
78 OnPositionRequiresUpdate());
81 gfx::Point GetDialogPosition(const gfx::Size& size) override {
82 views::View* view = browser_view_layout_->contents_container_;
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 gfx::Size GetMaximumDialogSize() override {
90 views::View* view = browser_view_layout_->contents_container_;
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);
96 private:
97 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 void AddObserver(ModalDialogHostObserver* observer) override {
105 observer_list_.AddObserver(observer);
107 void RemoveObserver(ModalDialogHostObserver* observer) override {
108 observer_list_.RemoveObserver(observer);
111 BrowserViewLayout* const browser_view_layout_;
113 base::ObserverList<ModalDialogHostObserver> observer_list_;
115 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogHostViews);
118 ////////////////////////////////////////////////////////////////////////////////
119 // BrowserViewLayout, public:
121 BrowserViewLayout::BrowserViewLayout()
122 : browser_(nullptr),
123 browser_view_(nullptr),
124 top_container_(nullptr),
125 tab_strip_(nullptr),
126 toolbar_(nullptr),
127 bookmark_bar_(nullptr),
128 infobar_container_(nullptr),
129 contents_container_(nullptr),
130 contents_layout_manager_(nullptr),
131 download_shelf_(nullptr),
132 immersive_mode_controller_(nullptr),
133 dialog_host_(new WebContentsModalDialogHostViews(this)),
134 web_contents_modal_dialog_top_y_(-1) {}
136 BrowserViewLayout::~BrowserViewLayout() {
139 void BrowserViewLayout::Init(
140 BrowserViewLayoutDelegate* delegate,
141 Browser* browser,
142 views::ClientView* browser_view,
143 views::View* top_container,
144 TabStrip* tab_strip,
145 views::View* toolbar,
146 InfoBarContainerView* infobar_container,
147 views::View* contents_container,
148 ContentsLayoutManager* contents_layout_manager,
149 ImmersiveModeController* immersive_mode_controller) {
150 delegate_.reset(delegate);
151 browser_ = browser;
152 browser_view_ = browser_view;
153 top_container_ = top_container;
154 tab_strip_ = tab_strip;
155 toolbar_ = toolbar;
156 infobar_container_ = infobar_container;
157 contents_container_ = contents_container;
158 contents_layout_manager_ = contents_layout_manager;
159 immersive_mode_controller_ = immersive_mode_controller;
162 WebContentsModalDialogHost*
163 BrowserViewLayout::GetWebContentsModalDialogHost() {
164 return dialog_host_.get();
167 gfx::Size BrowserViewLayout::GetMinimumSize() {
168 gfx::Size tabstrip_size(
169 browser()->SupportsWindowFeature(Browser::FEATURE_TABSTRIP) ?
170 tab_strip_->GetMinimumSize() : gfx::Size());
171 gfx::Size toolbar_size(
172 (browser()->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
173 browser()->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) ?
174 toolbar_->GetMinimumSize() : gfx::Size());
175 if (tabstrip_size.height() && toolbar_size.height())
176 toolbar_size.Enlarge(0, -GetLayoutConstant(TABSTRIP_TOOLBAR_OVERLAP));
177 gfx::Size bookmark_bar_size;
178 if (bookmark_bar_ &&
179 bookmark_bar_->visible() &&
180 browser()->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR)) {
181 bookmark_bar_size = bookmark_bar_->GetMinimumSize();
182 bookmark_bar_size.Enlarge(0, -bookmark_bar_->GetToolbarOverlap());
184 gfx::Size infobar_container_size(infobar_container_->GetMinimumSize());
185 // TODO: Adjust the minimum height for the find bar.
187 gfx::Size contents_size(contents_container_->GetMinimumSize());
189 int min_height = delegate_->GetTopInsetInBrowserView() +
190 tabstrip_size.height() + toolbar_size.height() +
191 bookmark_bar_size.height() + infobar_container_size.height() +
192 contents_size.height();
193 int widths[] = {
194 tabstrip_size.width(),
195 toolbar_size.width(),
196 bookmark_bar_size.width(),
197 infobar_container_size.width(),
198 contents_size.width() };
199 int min_width = *std::max_element(&widths[0], &widths[arraysize(widths)]);
200 return gfx::Size(min_width, min_height);
203 gfx::Rect BrowserViewLayout::GetFindBarBoundingBox() const {
204 // This function returns the area the Find Bar can be laid out within. This
205 // basically implies the "user-perceived content area" of the browser
206 // window excluding the vertical scrollbar. The "user-perceived content area"
207 // excludes the detached bookmark bar (in the New Tab case) and any infobars
208 // since they are not _visually_ connected to the Toolbar.
210 // First determine the bounding box of the content area in Widget
211 // coordinates.
212 gfx::Rect bounding_box = contents_container_->ConvertRectToWidget(
213 contents_container_->GetLocalBounds());
215 gfx::Rect top_container_bounds = top_container_->ConvertRectToWidget(
216 top_container_->GetLocalBounds());
218 int find_bar_y = 0;
219 if (immersive_mode_controller_->IsEnabled() &&
220 !immersive_mode_controller_->IsRevealed()) {
221 // Position the find bar exactly below the top container. In immersive
222 // fullscreen, when the top-of-window views are not revealed, only the
223 // miniature immersive style tab strip is visible. Do not overlap the
224 // find bar and the tab strip.
225 find_bar_y = top_container_bounds.bottom();
226 } else if (ui::MaterialDesignController::IsModeMaterial()) {
227 find_bar_y = top_container_bounds.bottom() - 6;
228 } else {
229 // Position the find bar 1 pixel above the bottom of the top container
230 // so that it occludes the border between the content area and the top
231 // container and looks connected to the top container.
232 find_bar_y = top_container_bounds.bottom() - 1;
235 // Grow the height of |bounding_box| by the height of any elements between
236 // the top container and |contents_container_| such as the detached bookmark
237 // bar and any infobars.
238 int height_delta = bounding_box.y() - find_bar_y;
239 bounding_box.set_y(find_bar_y);
240 bounding_box.set_height(std::max(0, bounding_box.height() + height_delta));
242 // Finally decrease the width of the bounding box by the width of
243 // the vertical scroll bar.
244 int scrollbar_width = gfx::scrollbar_size();
245 bounding_box.set_width(std::max(0, bounding_box.width() - scrollbar_width));
246 if (base::i18n::IsRTL())
247 bounding_box.set_x(bounding_box.x() + scrollbar_width);
249 return bounding_box;
252 int BrowserViewLayout::NonClientHitTest(const gfx::Point& point) {
253 // Since the TabStrip only renders in some parts of the top of the window,
254 // the un-obscured area is considered to be part of the non-client caption
255 // area of the window. So we need to treat hit-tests in these regions as
256 // hit-tests of the titlebar.
258 views::View* parent = browser_view_->parent();
260 gfx::Point point_in_browser_view_coords(point);
261 views::View::ConvertPointToTarget(
262 parent, browser_view_, &point_in_browser_view_coords);
263 gfx::Point test_point(point);
265 // Determine if the TabStrip exists and is capable of being clicked on. We
266 // might be a popup window without a TabStrip.
267 if (delegate_->IsTabStripVisible()) {
268 // See if the mouse pointer is within the bounds of the TabStrip.
269 if (ConvertedHitTest(parent, tab_strip_, &test_point)) {
270 if (tab_strip_->IsPositionInWindowCaption(test_point))
271 return HTCAPTION;
272 return HTCLIENT;
275 // The top few pixels of the TabStrip are a drop-shadow - as we're pretty
276 // starved of dragable area, let's give it to window dragging (this also
277 // makes sense visually).
278 views::Widget* widget = browser_view_->GetWidget();
279 if (!(widget->IsMaximized() || widget->IsFullscreen()) &&
280 (point_in_browser_view_coords.y() <
281 (tab_strip_->y() + kTabShadowSize))) {
282 // We return HTNOWHERE as this is a signal to our containing
283 // NonClientView that it should figure out what the correct hit-test
284 // code is given the mouse position...
285 return HTNOWHERE;
289 // If the point's y coordinate is below the top of the toolbar and otherwise
290 // within the bounds of this view, the point is considered to be within the
291 // client area.
292 gfx::Rect bv_bounds = browser_view_->bounds();
293 bv_bounds.Offset(0, toolbar_->y());
294 bv_bounds.set_height(bv_bounds.height() - toolbar_->y());
295 if (bv_bounds.Contains(point))
296 return HTCLIENT;
298 // If the point's y coordinate is above the top of the toolbar, but not
299 // over the tabstrip (per previous checking in this function), then we
300 // consider it in the window caption (e.g. the area to the right of the
301 // tabstrip underneath the window controls). However, note that we DO NOT
302 // return HTCAPTION here, because when the window is maximized the window
303 // controls will fall into this space (since the BrowserView is sized to
304 // entire size of the window at that point), and the HTCAPTION value will
305 // cause the window controls not to work. So we return HTNOWHERE so that the
306 // caller will hit-test the window controls before finally falling back to
307 // HTCAPTION.
308 bv_bounds = browser_view_->bounds();
309 bv_bounds.set_height(toolbar_->y());
310 if (bv_bounds.Contains(point))
311 return HTNOWHERE;
313 // If the point is somewhere else, delegate to the default implementation.
314 return browser_view_->views::ClientView::NonClientHitTest(point);
317 //////////////////////////////////////////////////////////////////////////////
318 // BrowserViewLayout, views::LayoutManager implementation:
320 void BrowserViewLayout::Layout(views::View* browser_view) {
321 vertical_layout_rect_ = browser_view->GetLocalBounds();
322 int top = delegate_->GetTopInsetInBrowserView();
323 top = LayoutTabStripRegion(top);
324 if (delegate_->IsTabStripVisible()) {
325 int x = tab_strip_->GetMirroredX() +
326 browser_view_->GetMirroredX() +
327 delegate_->GetThemeBackgroundXInset();
328 int y = browser_view_->y() + delegate_->GetTopInsetInBrowserView();
329 tab_strip_->SetBackgroundOffset(gfx::Point(x, y));
331 top = LayoutToolbar(top);
333 top = LayoutBookmarkAndInfoBars(top, browser_view->y());
335 // Top container requires updated toolbar and bookmark bar to compute bounds.
336 UpdateTopContainerBounds();
338 int bottom = LayoutDownloadShelf(browser_view->height());
339 // Treat a detached bookmark bar as if the web contents container is shifted
340 // upwards and overlaps it.
341 int active_top_margin = GetContentsOffsetForBookmarkBar();
342 contents_layout_manager_->SetActiveTopMargin(active_top_margin);
343 top -= active_top_margin;
344 LayoutContentsContainerView(top, bottom);
346 // This must be done _after_ we lay out the WebContents since this
347 // code calls back into us to find the bounding box the find bar
348 // must be laid out within, and that code depends on the
349 // TabContentsContainer's bounds being up to date.
350 if (browser()->HasFindBarController()) {
351 browser()->GetFindBarController()->find_bar()->MoveWindowIfNecessary(
352 gfx::Rect());
355 // Adjust the fullscreen exit bubble bounds for |top_container_|'s new bounds.
356 // This makes the fullscreen exit bubble look like it animates with
357 // |top_container_| in immersive fullscreen.
358 ExclusiveAccessBubbleViews* exclusive_access_bubble =
359 delegate_->GetExclusiveAccessBubble();
360 if (exclusive_access_bubble)
361 exclusive_access_bubble->RepositionIfVisible();
363 // Adjust any hosted dialogs if the browser's dialog hosting bounds changed.
364 const gfx::Rect dialog_bounds(dialog_host_->GetDialogPosition(gfx::Size()),
365 dialog_host_->GetMaximumDialogSize());
366 if (latest_dialog_bounds_ != dialog_bounds) {
367 latest_dialog_bounds_ = dialog_bounds;
368 dialog_host_->NotifyPositionRequiresUpdate();
372 // Return the preferred size which is the size required to give each
373 // children their respective preferred size.
374 gfx::Size BrowserViewLayout::GetPreferredSize(const views::View* host) const {
375 return gfx::Size();
378 //////////////////////////////////////////////////////////////////////////////
379 // BrowserViewLayout, private:
381 int BrowserViewLayout::LayoutTabStripRegion(int top) {
382 if (!delegate_->IsTabStripVisible()) {
383 tab_strip_->SetVisible(false);
384 tab_strip_->SetBounds(0, 0, 0, 0);
385 return top;
387 // This retrieves the bounds for the tab strip based on whether or not we show
388 // anything to the left of it, like the incognito avatar.
389 gfx::Rect tabstrip_bounds(delegate_->GetBoundsForTabStripInBrowserView());
391 tab_strip_->SetVisible(true);
392 tab_strip_->SetBoundsRect(tabstrip_bounds);
394 return tabstrip_bounds.bottom();
397 int BrowserViewLayout::LayoutToolbar(int top) {
398 int browser_view_width = vertical_layout_rect_.width();
399 bool toolbar_visible = delegate_->IsToolbarVisible();
400 int y = top;
401 y -= (toolbar_visible && delegate_->IsTabStripVisible()) ?
402 GetLayoutConstant(TABSTRIP_TOOLBAR_OVERLAP) : 0;
403 int height = toolbar_visible ? toolbar_->GetPreferredSize().height() : 0;
404 toolbar_->SetVisible(toolbar_visible);
405 toolbar_->SetBounds(vertical_layout_rect_.x(), y, browser_view_width, height);
407 return y + height;
410 int BrowserViewLayout::LayoutBookmarkAndInfoBars(int top, int browser_view_y) {
411 web_contents_modal_dialog_top_y_ =
412 top + browser_view_y - kConstrainedWindowOverlap;
414 if (bookmark_bar_) {
415 // If we're showing the Bookmark bar in detached style, then we
416 // need to show any Info bar _above_ the Bookmark bar, since the
417 // Bookmark bar is styled to look like it's part of the page.
418 if (bookmark_bar_->IsDetached()) {
419 web_contents_modal_dialog_top_y_ =
420 top + browser_view_y - kConstrainedWindowOverlap;
421 return LayoutBookmarkBar(LayoutInfoBar(top));
423 // Otherwise, Bookmark bar first, Info bar second.
424 top = std::max(toolbar_->bounds().bottom(), LayoutBookmarkBar(top));
427 return LayoutInfoBar(top);
430 int BrowserViewLayout::LayoutBookmarkBar(int top) {
431 int y = top;
432 if (!delegate_->IsBookmarkBarVisible()) {
433 bookmark_bar_->SetVisible(false);
434 // TODO(jamescook): Don't change the bookmark bar height when it is
435 // invisible, so we can use its height for layout even in that state.
436 bookmark_bar_->SetBounds(0, y, browser_view_->width(), 0);
437 return y;
440 bookmark_bar_->set_infobar_visible(InfobarVisible());
441 int bookmark_bar_height = bookmark_bar_->GetPreferredSize().height();
442 y -= bookmark_bar_->GetToolbarOverlap();
443 bookmark_bar_->SetBounds(vertical_layout_rect_.x(),
445 vertical_layout_rect_.width(),
446 bookmark_bar_height);
447 // Set visibility after setting bounds, as the visibility update uses the
448 // bounds to determine if the mouse is hovering over a button.
449 bookmark_bar_->SetVisible(true);
450 return y + bookmark_bar_height;
453 int BrowserViewLayout::LayoutInfoBar(int top) {
454 // In immersive fullscreen, the infobar always starts near the top of the
455 // screen, just under the "light bar" rectangular stripes.
456 if (immersive_mode_controller_->IsEnabled()) {
457 top = browser_view_->y();
458 if (!immersive_mode_controller_->ShouldHideTabIndicators())
459 top += TabStrip::GetImmersiveHeight();
461 // Raise the |infobar_container_| by its vertical overlap.
462 infobar_container_->SetVisible(InfobarVisible());
463 int height;
464 int overlapped_top = top - infobar_container_->GetVerticalOverlap(&height);
465 infobar_container_->SetBounds(vertical_layout_rect_.x(),
466 overlapped_top,
467 vertical_layout_rect_.width(),
468 height);
469 return overlapped_top + height;
472 void BrowserViewLayout::LayoutContentsContainerView(int top, int bottom) {
473 // |contents_container_| contains web page contents and devtools.
474 // See browser_view.h for details.
475 gfx::Rect contents_container_bounds(vertical_layout_rect_.x(),
476 top,
477 vertical_layout_rect_.width(),
478 std::max(0, bottom - top));
479 contents_container_->SetBoundsRect(contents_container_bounds);
482 void BrowserViewLayout::UpdateTopContainerBounds() {
483 // Set the bounds of the top container view such that it is tall enough to
484 // fully show all of its children. In particular, the bottom of the bookmark
485 // bar can be above the bottom of the toolbar while the bookmark bar is
486 // animating. The top container view is positioned relative to the top of the
487 // client view instead of relative to GetTopInsetInBrowserView() because the
488 // top container view paints parts of the frame (title, window controls)
489 // during an immersive fullscreen reveal.
490 int height = 0;
491 for (int i = 0; i < top_container_->child_count(); ++i) {
492 views::View* child = top_container_->child_at(i);
493 if (!child->visible())
494 continue;
495 int child_bottom = child->bounds().bottom();
496 if (child_bottom > height)
497 height = child_bottom;
500 // Ensure that the top container view reaches the topmost view in the
501 // ClientView because the bounds of the top container view are used in
502 // layout and we assume that this is the case.
503 height = std::max(height, delegate_->GetTopInsetInBrowserView());
505 gfx::Rect top_container_bounds(vertical_layout_rect_.width(), height);
507 // If the immersive mode controller is animating the top container, it may be
508 // partly offscreen.
509 top_container_bounds.set_y(
510 immersive_mode_controller_->GetTopContainerVerticalOffset(
511 top_container_bounds.size()));
512 top_container_->SetBoundsRect(top_container_bounds);
515 int BrowserViewLayout::GetContentsOffsetForBookmarkBar() {
516 // If the bookmark bar is hidden or attached to the omnibox the web contents
517 // will appear directly underneath it and does not need an offset.
518 if (!bookmark_bar_ ||
519 !delegate_->IsBookmarkBarVisible() ||
520 !bookmark_bar_->IsDetached()) {
521 return 0;
524 // Offset for the detached bookmark bar.
525 return bookmark_bar_->height() -
526 views::NonClientFrameView::kClientEdgeThickness;
529 int BrowserViewLayout::LayoutDownloadShelf(int bottom) {
530 if (delegate_->DownloadShelfNeedsLayout()) {
531 bool visible = browser()->SupportsWindowFeature(
532 Browser::FEATURE_DOWNLOADSHELF);
533 DCHECK(download_shelf_);
534 int height = visible ? download_shelf_->GetPreferredSize().height() : 0;
535 download_shelf_->SetVisible(visible);
536 download_shelf_->SetBounds(vertical_layout_rect_.x(), bottom - height,
537 vertical_layout_rect_.width(), height);
538 download_shelf_->Layout();
539 bottom -= height;
541 return bottom;
544 bool BrowserViewLayout::InfobarVisible() const {
545 // Cast to a views::View to access GetPreferredSize().
546 views::View* infobar_container = infobar_container_;
547 // NOTE: Can't check if the size IsEmpty() since it's always 0-width.
548 return browser_->SupportsWindowFeature(Browser::FEATURE_INFOBAR) &&
549 (infobar_container->GetPreferredSize().height() != 0);