12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/frame/browser_view_layout.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/observer_list.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/find_bar/find_bar.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/find_bar/find_bar_controller.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/search/search_model.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/download/download_shelf_view.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/views/frame/contents_layout_manager.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/views/frame/top_container_view.h"
217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/ui/views/fullscreen_exit_bubble_views.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/infobars/infobar_container_view.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/tabs/tab_strip.h"
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "components/web_modal/web_contents_modal_dialog_host.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/hit_test.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/point.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/scrollbar_size.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/size.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/webview/webview.h"
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/views/widget/widget.h"
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/views/window/client_view.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using views::View;
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using web_modal::WebContentsModalDialogHost;
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)using web_modal::ModalDialogHostObserver;
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The visible height of the shadow above the tabs. Clicks in this area are
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// treated as clicks to the frame, rather than clicks to the tab.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kTabShadowSize = 2;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The number of pixels the constrained window should overlap the bottom
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the omnibox.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kConstrainedWindowOverlap = 3;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Combines View::ConvertPointToTarget and View::HitTest for a given |point|.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts |point| from |src| to |dst| and hit tests it against |dst|. The
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// converted |point| can then be retrieved and used for additional tests.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ConvertedHitTest(views::View* src, views::View* dst, gfx::Point* point) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(src);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(dst);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(point);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  views::View::ConvertPointToTarget(src, dst, point);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return dst->HitTestPoint(*point);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class BrowserViewLayout::WebContentsModalDialogHostViews
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : public WebContentsModalDialogHost {
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  explicit WebContentsModalDialogHostViews(
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      BrowserViewLayout* browser_view_layout)
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          : browser_view_layout_(browser_view_layout) {
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual ~WebContentsModalDialogHostViews() {
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    FOR_EACH_OBSERVER(ModalDialogHostObserver,
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                      observer_list_,
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                      OnHostDestroying());
713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void NotifyPositionRequiresUpdate() {
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    FOR_EACH_OBSERVER(ModalDialogHostObserver,
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                      observer_list_,
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                      OnPositionRequiresUpdate());
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE {
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    views::View* view = browser_view_layout_->contents_container_;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::Rect content_area = view->ConvertRectToWidget(view->GetLocalBounds());
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const int middle_x = content_area.x() + content_area.width() / 2;
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const int top = browser_view_layout_->web_contents_modal_dialog_top_y_;
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return gfx::Point(middle_x - size.width() / 2, top);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual gfx::Size GetMaximumDialogSize() OVERRIDE {
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    views::View* view = browser_view_layout_->contents_container_;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::Rect content_area = view->ConvertRectToWidget(view->GetLocalBounds());
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const int top = browser_view_layout_->web_contents_modal_dialog_top_y_;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return gfx::Size(content_area.width(), content_area.bottom() - top);
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private:
95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual gfx::NativeView GetHostView() const OVERRIDE {
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::NativeWindow window =
97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        browser_view_layout_->browser()->window()->GetNativeWindow();
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return views::Widget::GetWidgetForNativeWindow(window)->GetNativeView();
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Add/remove observer.
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void AddObserver(ModalDialogHostObserver* observer) OVERRIDE {
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_list_.AddObserver(observer);
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void RemoveObserver(ModalDialogHostObserver* observer) OVERRIDE {
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_list_.RemoveObserver(observer);
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
109a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  BrowserViewLayout* const browser_view_layout_;
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ObserverList<ModalDialogHostObserver> observer_list_;
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogHostViews);
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const int BrowserViewLayout::kToolbarTabStripVerticalOverlap = 3;
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BrowserViewLayout, public:
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BrowserViewLayout::BrowserViewLayout()
1237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : browser_(NULL),
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      browser_view_(NULL),
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      top_container_(NULL),
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      tab_strip_(NULL),
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      toolbar_(NULL),
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      bookmark_bar_(NULL),
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      infobar_container_(NULL),
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      contents_container_(NULL),
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      contents_layout_manager_(NULL),
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      download_shelf_(NULL),
13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      immersive_mode_controller_(NULL),
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      dialog_host_(new WebContentsModalDialogHostViews(this)),
1357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      web_contents_modal_dialog_top_y_(-1) {}
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BrowserViewLayout::~BrowserViewLayout() {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void BrowserViewLayout::Init(
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    BrowserViewLayoutDelegate* delegate,
14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Browser* browser,
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::ClientView* browser_view,
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    views::View* top_container,
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    TabStrip* tab_strip,
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    views::View* toolbar,
14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    InfoBarContainerView* infobar_container,
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    views::View* contents_container,
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ContentsLayoutManager* contents_layout_manager,
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ImmersiveModeController* immersive_mode_controller) {
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  delegate_.reset(delegate);
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  browser_ = browser;
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  browser_view_ = browser_view;
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  top_container_ = top_container;
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  tab_strip_ = tab_strip;
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  toolbar_ = toolbar;
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  infobar_container_ = infobar_container;
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  contents_container_ = contents_container;
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  contents_layout_manager_ = contents_layout_manager;
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  immersive_mode_controller_ = immersive_mode_controller;
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)WebContentsModalDialogHost*
164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    BrowserViewLayout::GetWebContentsModalDialogHost() {
165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return dialog_host_.get();
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Size BrowserViewLayout::GetMinimumSize() {
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size tabstrip_size(
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser()->SupportsWindowFeature(Browser::FEATURE_TABSTRIP) ?
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      tab_strip_->GetMinimumSize() : gfx::Size());
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size toolbar_size(
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (browser()->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       browser()->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) ?
17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)           toolbar_->GetMinimumSize() : gfx::Size());
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tabstrip_size.height() && toolbar_size.height())
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    toolbar_size.Enlarge(0, -kToolbarTabStripVerticalOverlap);
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size bookmark_bar_size;
179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (bookmark_bar_ &&
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      bookmark_bar_->visible() &&
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser()->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR)) {
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bookmark_bar_size = bookmark_bar_->GetMinimumSize();
183eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    bookmark_bar_size.Enlarge(0, -bookmark_bar_->GetToolbarOverlap());
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
185a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  gfx::Size infobar_container_size(infobar_container_->GetMinimumSize());
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // TODO: Adjust the minimum height for the find bar.
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Size contents_size(contents_container_->GetMinimumSize());
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  int min_height = delegate_->GetTopInsetInBrowserView() +
191a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      tabstrip_size.height() + toolbar_size.height() +
192a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      bookmark_bar_size.height() + infobar_container_size.height() +
193a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      contents_size.height();
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int widths[] = {
195f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        tabstrip_size.width(),
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        toolbar_size.width(),
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        bookmark_bar_size.width(),
198a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        infobar_container_size.width(),
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        contents_size.width() };
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int min_width = *std::max_element(&widths[0], &widths[arraysize(widths)]);
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gfx::Size(min_width, min_height);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Rect BrowserViewLayout::GetFindBarBoundingBox() const {
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This function returns the area the Find Bar can be laid out within. This
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // basically implies the "user-perceived content area" of the browser
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // window excluding the vertical scrollbar. The "user-perceived content area"
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // excludes the detached bookmark bar (in the New Tab case) and any infobars
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // since they are not _visually_ connected to the Toolbar.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // First determine the bounding box of the content area in Widget
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // coordinates.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect bounding_box = contents_container_->ConvertRectToWidget(
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      contents_container_->GetLocalBounds());
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Rect top_container_bounds = top_container_->ConvertRectToWidget(
21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      top_container_->GetLocalBounds());
218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int find_bar_y = 0;
2207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (immersive_mode_controller_->IsEnabled() &&
2217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      !immersive_mode_controller_->IsRevealed()) {
2227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Position the find bar exactly below the top container. In immersive
2237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // fullscreen, when the top-of-window views are not revealed, only the
2247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // miniature immersive style tab strip is visible. Do not overlap the
2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // find bar and the tab strip.
2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    find_bar_y = top_container_bounds.bottom();
2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  } else {
2287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Position the find bar 1 pixel above the bottom of the top container
2297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // so that it occludes the border between the content area and the top
2307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // container and looks connected to the top container.
2317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    find_bar_y = top_container_bounds.bottom() - 1;
2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Grow the height of |bounding_box| by the height of any elements between
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the top container and |contents_container_| such as the detached bookmark
236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // bar and any infobars.
237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  int height_delta = bounding_box.y() - find_bar_y;
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bounding_box.set_y(find_bar_y);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bounding_box.set_height(std::max(0, bounding_box.height() + height_delta));
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Finally decrease the width of the bounding box by the width of
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the vertical scroll bar.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int scrollbar_width = gfx::scrollbar_size();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bounding_box.set_width(std::max(0, bounding_box.width() - scrollbar_width));
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (base::i18n::IsRTL())
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bounding_box.set_x(bounding_box.x() + scrollbar_width);
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return bounding_box;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
251bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochint BrowserViewLayout::NonClientHitTest(const gfx::Point& point) {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Since the TabStrip only renders in some parts of the top of the window,
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the un-obscured area is considered to be part of the non-client caption
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // area of the window. So we need to treat hit-tests in these regions as
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // hit-tests of the titlebar.
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  views::View* parent = browser_view_->parent();
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point point_in_browser_view_coords(point);
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  views::View::ConvertPointToTarget(
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      parent, browser_view_, &point_in_browser_view_coords);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point test_point(point);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determine if the TabStrip exists and is capable of being clicked on. We
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // might be a popup window without a TabStrip.
266f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (delegate_->IsTabStripVisible()) {
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // See if the mouse pointer is within the bounds of the TabStrip.
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (ConvertedHitTest(parent, tab_strip_, &test_point)) {
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (tab_strip_->IsPositionInWindowCaption(test_point))
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return HTCAPTION;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return HTCLIENT;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The top few pixels of the TabStrip are a drop-shadow - as we're pretty
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // starved of dragable area, let's give it to window dragging (this also
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // makes sense visually).
277f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    views::Widget* widget = browser_view_->GetWidget();
278f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!(widget->IsMaximized() || widget->IsFullscreen()) &&
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (point_in_browser_view_coords.y() <
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            (tab_strip_->y() + kTabShadowSize))) {
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We return HTNOWHERE as this is a signal to our containing
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // NonClientView that it should figure out what the correct hit-test
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // code is given the mouse position...
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return HTNOWHERE;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the point's y coordinate is below the top of the toolbar and otherwise
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // within the bounds of this view, the point is considered to be within the
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // client area.
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect bv_bounds = browser_view_->bounds();
29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bv_bounds.Offset(0, toolbar_->y());
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bv_bounds.set_height(bv_bounds.height() - toolbar_->y());
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (bv_bounds.Contains(point))
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return HTCLIENT;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
297010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // If the point's y coordinate is above the top of the toolbar, but not
298010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // over the tabstrip (per previous checking in this function), then we
299010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // consider it in the window caption (e.g. the area to the right of the
300010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // tabstrip underneath the window controls). However, note that we DO NOT
301010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // return HTCAPTION here, because when the window is maximized the window
302010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // controls will fall into this space (since the BrowserView is sized to
303010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // entire size of the window at that point), and the HTCAPTION value will
304010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // cause the window controls not to work. So we return HTNOWHERE so that the
305010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // caller will hit-test the window controls before finally falling back to
306010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // HTCAPTION.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bv_bounds = browser_view_->bounds();
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bv_bounds.set_height(toolbar_->y());
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (bv_bounds.Contains(point))
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return HTNOWHERE;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the point is somewhere else, delegate to the default implementation.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser_view_->views::ClientView::NonClientHitTest(point);
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//////////////////////////////////////////////////////////////////////////////
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BrowserViewLayout, views::LayoutManager implementation:
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void BrowserViewLayout::Layout(views::View* browser_view) {
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  vertical_layout_rect_ = browser_view->GetLocalBounds();
321f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int top = delegate_->GetTopInsetInBrowserView();
322f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  top = LayoutTabStripRegion(top);
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (delegate_->IsTabStripVisible()) {
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int x = tab_strip_->GetMirroredX() +
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        browser_view_->GetMirroredX() +
326f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        delegate_->GetThemeBackgroundXInset();
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    int y = browser_view_->y() + delegate_->GetTopInsetInBrowserView();
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    tab_strip_->SetBackgroundOffset(gfx::Point(x, y));
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  top = LayoutToolbar(top);
331a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
33290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  top = LayoutBookmarkAndInfoBars(top, browser_view->y());
333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Top container requires updated toolbar and bookmark bar to compute bounds.
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  UpdateTopContainerBounds();
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
33790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int bottom = LayoutDownloadShelf(browser_view->height());
338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Treat a detached bookmark bar as if the web contents container is shifted
339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // upwards and overlaps it.
340ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  int active_top_margin = GetContentsOffsetForBookmarkBar();
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  contents_layout_manager_->SetActiveTopMargin(active_top_margin);
342ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  top -= active_top_margin;
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LayoutContentsContainerView(top, bottom);
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This must be done _after_ we lay out the WebContents since this
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // code calls back into us to find the bounding box the find bar
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // must be laid out within, and that code depends on the
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TabContentsContainer's bounds being up to date.
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser()->HasFindBarController()) {
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser()->GetFindBarController()->find_bar()->MoveWindowIfNecessary(
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        gfx::Rect(), true);
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Adjust the fullscreen exit bubble bounds for |top_container_|'s new bounds.
3557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // This makes the fullscreen exit bubble look like it animates with
3567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // |top_container_| in immersive fullscreen.
3577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FullscreenExitBubbleViews* fullscreen_exit_bubble =
3587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      delegate_->GetFullscreenExitBubble();
3597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (fullscreen_exit_bubble)
3607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    fullscreen_exit_bubble->RepositionIfVisible();
3617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adjust any hosted dialogs if the browser's dialog hosting bounds changed.
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const gfx::Rect dialog_bounds(dialog_host_->GetDialogPosition(gfx::Size()),
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                dialog_host_->GetMaximumDialogSize());
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (latest_dialog_bounds_ != dialog_bounds) {
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    latest_dialog_bounds_ = dialog_bounds;
367a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    dialog_host_->NotifyPositionRequiresUpdate();
368a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return the preferred size which is the size required to give each
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// children their respective preferred size.
373cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)gfx::Size BrowserViewLayout::GetPreferredSize(const views::View* host) const {
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gfx::Size();
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//////////////////////////////////////////////////////////////////////////////
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BrowserViewLayout, private:
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
380f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)int BrowserViewLayout::LayoutTabStripRegion(int top) {
38190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!delegate_->IsTabStripVisible()) {
38290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    tab_strip_->SetVisible(false);
38390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    tab_strip_->SetBounds(0, 0, 0, 0);
384f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return top;
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This retrieves the bounds for the tab strip based on whether or not we show
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // anything to the left of it, like the incognito avatar.
388f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect tabstrip_bounds(delegate_->GetBoundsForTabStripInBrowserView());
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
39090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  tab_strip_->SetVisible(true);
39190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  tab_strip_->SetBoundsRect(tabstrip_bounds);
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
393010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return tabstrip_bounds.bottom();
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int BrowserViewLayout::LayoutToolbar(int top) {
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int browser_view_width = vertical_layout_rect_.width();
39890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool toolbar_visible = delegate_->IsToolbarVisible();
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int y = top;
40090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  y -= (toolbar_visible && delegate_->IsTabStripVisible()) ?
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        kToolbarTabStripVerticalOverlap : 0;
40290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int height = toolbar_visible ? toolbar_->GetPreferredSize().height() : 0;
40390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  toolbar_->SetVisible(toolbar_visible);
40490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  toolbar_->SetBounds(vertical_layout_rect_.x(), y, browser_view_width, height);
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return y + height;
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
40990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int BrowserViewLayout::LayoutBookmarkAndInfoBars(int top, int browser_view_y) {
410424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_modal_dialog_top_y_ =
411424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      top + browser_view_y - kConstrainedWindowOverlap;
412424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (bookmark_bar_) {
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If we're showing the Bookmark bar in detached style, then we
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // need to show any Info bar _above_ the Bookmark bar, since the
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Bookmark bar is styled to look like it's part of the page.
41790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (bookmark_bar_->IsDetached()) {
41890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      web_contents_modal_dialog_top_y_ =
41990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          top + browser_view_y - kConstrainedWindowOverlap;
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return LayoutBookmarkBar(LayoutInfoBar(top));
42190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Otherwise, Bookmark bar first, Info bar second.
42390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    top = std::max(toolbar_->bounds().bottom(), LayoutBookmarkBar(top));
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
42590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return LayoutInfoBar(top);
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int BrowserViewLayout::LayoutBookmarkBar(int top) {
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int y = top;
43190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!delegate_->IsBookmarkBarVisible()) {
432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bookmark_bar_->SetVisible(false);
433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(jamescook): Don't change the bookmark bar height when it is
434c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // invisible, so we can use its height for layout even in that state.
435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bookmark_bar_->SetBounds(0, y, browser_view_->width(), 0);
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return y;
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bookmark_bar_->set_infobar_visible(InfobarVisible());
440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  int bookmark_bar_height = bookmark_bar_->GetPreferredSize().height();
441eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  y -= bookmark_bar_->GetToolbarOverlap();
44290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bookmark_bar_->SetBounds(vertical_layout_rect_.x(),
44390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           y,
44490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           vertical_layout_rect_.width(),
44590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                           bookmark_bar_height);
44690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Set visibility after setting bounds, as the visibility update uses the
44790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // bounds to determine if the mouse is hovering over a button.
448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bookmark_bar_->SetVisible(true);
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return y + bookmark_bar_height;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int BrowserViewLayout::LayoutInfoBar(int top) {
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // In immersive fullscreen, the infobar always starts near the top of the
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // screen, just under the "light bar" rectangular stripes.
45590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (immersive_mode_controller_->IsEnabled()) {
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    top = browser_view_->y();
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!immersive_mode_controller_->ShouldHideTabIndicators())
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      top += TabStrip::GetImmersiveHeight();
459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
46090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Raise the |infobar_container_| by its vertical overlap.
46190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  infobar_container_->SetVisible(InfobarVisible());
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int height;
46390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int overlapped_top = top - infobar_container_->GetVerticalOverlap(&height);
46490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  infobar_container_->SetBounds(vertical_layout_rect_.x(),
46590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                overlapped_top,
46690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                vertical_layout_rect_.width(),
46790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                height);
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return overlapped_top + height;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void BrowserViewLayout::LayoutContentsContainerView(int top, int bottom) {
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |contents_container_| contains web page contents and devtools.
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // See browser_view.h for details.
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Rect contents_container_bounds(vertical_layout_rect_.x(),
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      top,
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      vertical_layout_rect_.width(),
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      std::max(0, bottom - top));
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  contents_container_->SetBoundsRect(contents_container_bounds);
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
481868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void BrowserViewLayout::UpdateTopContainerBounds() {
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Set the bounds of the top container view such that it is tall enough to
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // fully show all of its children. In particular, the bottom of the bookmark
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // bar can be above the bottom of the toolbar while the bookmark bar is
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // animating. The top container view is positioned relative to the top of the
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // client view instead of relative to GetTopInsetInBrowserView() because the
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // top container view paints parts of the frame (title, window controls)
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // during an immersive fullscreen reveal.
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int height = 0;
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (int i = 0; i < top_container_->child_count(); ++i) {
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    views::View* child = top_container_->child_at(i);
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!child->visible())
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      continue;
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int child_bottom = child->bounds().bottom();
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (child_bottom > height)
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      height = child_bottom;
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Ensure that the top container view reaches the topmost view in the
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ClientView because the bounds of the top container view are used in
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // layout and we assume that this is the case.
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  height = std::max(height, delegate_->GetTopInsetInBrowserView());
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Rect top_container_bounds(vertical_layout_rect_.width(), height);
505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
506a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // If the immersive mode controller is animating the top container, it may be
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // partly offscreen.
508868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  top_container_bounds.set_y(
509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      immersive_mode_controller_->GetTopContainerVerticalOffset(
510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          top_container_bounds.size()));
511868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  top_container_->SetBoundsRect(top_container_bounds);
512868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)int BrowserViewLayout::GetContentsOffsetForBookmarkBar() {
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the bookmark bar is hidden or attached to the omnibox the web contents
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // will appear directly underneath it and does not need an offset.
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!bookmark_bar_ ||
518f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      !delegate_->IsBookmarkBarVisible() ||
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      !bookmark_bar_->IsDetached()) {
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return 0;
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Offset for the detached bookmark bar.
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return bookmark_bar_->height() -
525eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      bookmark_bar_->GetFullyDetachedToolbarOverlap();
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int BrowserViewLayout::LayoutDownloadShelf(int bottom) {
52990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (delegate_->DownloadShelfNeedsLayout()) {
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool visible = browser()->SupportsWindowFeature(
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Browser::FEATURE_DOWNLOADSHELF);
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(download_shelf_);
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int height = visible ? download_shelf_->GetPreferredSize().height() : 0;
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    download_shelf_->SetVisible(visible);
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    download_shelf_->SetBounds(vertical_layout_rect_.x(), bottom - height,
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               vertical_layout_rect_.width(), height);
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    download_shelf_->Layout();
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bottom -= height;
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return bottom;
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BrowserViewLayout::InfobarVisible() const {
544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Cast to a views::View to access GetPreferredSize().
545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  views::View* infobar_container = infobar_container_;
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: Can't check if the size IsEmpty() since it's always 0-width.
547c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return browser_->SupportsWindowFeature(Browser::FEATURE_INFOBAR) &&
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (infobar_container->GetPreferredSize().height() != 0);
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
550