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