This source file includes following definitions.
- ConvertedHitTest
- WebContentsModalDialogHostViews
- WebContentsModalDialogHostViews
- NotifyPositionRequiresUpdate
- GetDialogPosition
- GetMaximumDialogSize
- GetHostView
- AddObserver
- RemoveObserver
- web_contents_modal_dialog_top_y_
- Init
- GetWebContentsModalDialogHost
- GetMinimumSize
- GetFindBarBoundingBox
- NonClientHitTest
- GetPreferredSize
- LayoutTabStripRegion
- LayoutToolbar
- LayoutBookmarkAndInfoBars
- LayoutBookmarkBar
- LayoutInfoBar
- LayoutContentsContainerView
- UpdateTopContainerBounds
- GetContentsOffsetForBookmarkBar
- LayoutDownloadShelf
- InfobarVisible
#include "chrome/browser/ui/views/frame/browser_view_layout.h"
#include "base/observer_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/find_bar/find_bar.h"
#include "chrome/browser/ui/find_bar/find_bar_controller.h"
#include "chrome/browser/ui/search/search_model.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
#include "chrome/browser/ui/views/download/download_shelf_view.h"
#include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
#include "chrome/browser/ui/views/frame/contents_layout_manager.h"
#include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
#include "chrome/browser/ui/views/frame/top_container_view.h"
#include "chrome/browser/ui/views/fullscreen_exit_bubble_views.h"
#include "chrome/browser/ui/views/infobars/infobar_container_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "ui/base/hit_test.h"
#include "ui/gfx/point.h"
#include "ui/gfx/scrollbar_size.h"
#include "ui/gfx/size.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/client_view.h"
using views::View;
using web_modal::WebContentsModalDialogHost;
using web_modal::ModalDialogHostObserver;
namespace {
const int kTabShadowSize = 2;
const int kWindowSwitcherOffsetX = 7;
const int kConstrainedWindowOverlap = 3;
bool ConvertedHitTest(views::View* src, views::View* dst, gfx::Point* point) {
DCHECK(src);
DCHECK(dst);
DCHECK(point);
views::View::ConvertPointToTarget(src, dst, point);
return dst->HitTestPoint(*point);
}
}
class BrowserViewLayout::WebContentsModalDialogHostViews
: public WebContentsModalDialogHost {
public:
explicit WebContentsModalDialogHostViews(
BrowserViewLayout* browser_view_layout)
: browser_view_layout_(browser_view_layout) {
}
virtual ~WebContentsModalDialogHostViews() {
FOR_EACH_OBSERVER(ModalDialogHostObserver,
observer_list_,
OnHostDestroying());
}
void NotifyPositionRequiresUpdate() {
FOR_EACH_OBSERVER(ModalDialogHostObserver,
observer_list_,
OnPositionRequiresUpdate());
}
virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE {
views::View* view = browser_view_layout_->contents_container_;
gfx::Rect content_area = view->ConvertRectToWidget(view->GetLocalBounds());
const int middle_x = content_area.x() + content_area.width() / 2;
const int top = browser_view_layout_->web_contents_modal_dialog_top_y_;
return gfx::Point(middle_x - size.width() / 2, top);
}
virtual gfx::Size GetMaximumDialogSize() OVERRIDE {
views::View* view = browser_view_layout_->contents_container_;
gfx::Rect content_area = view->ConvertRectToWidget(view->GetLocalBounds());
const int top = browser_view_layout_->web_contents_modal_dialog_top_y_;
return gfx::Size(content_area.width(), content_area.bottom() - top);
}
private:
virtual gfx::NativeView GetHostView() const OVERRIDE {
gfx::NativeWindow window =
browser_view_layout_->browser()->window()->GetNativeWindow();
return views::Widget::GetWidgetForNativeWindow(window)->GetNativeView();
}
virtual void AddObserver(ModalDialogHostObserver* observer) OVERRIDE {
observer_list_.AddObserver(observer);
}
virtual void RemoveObserver(ModalDialogHostObserver* observer) OVERRIDE {
observer_list_.RemoveObserver(observer);
}
BrowserViewLayout* const browser_view_layout_;
ObserverList<ModalDialogHostObserver> observer_list_;
DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogHostViews);
};
const int BrowserViewLayout::kToolbarTabStripVerticalOverlap = 3;
BrowserViewLayout::BrowserViewLayout()
: browser_(NULL),
browser_view_(NULL),
top_container_(NULL),
tab_strip_(NULL),
toolbar_(NULL),
bookmark_bar_(NULL),
infobar_container_(NULL),
contents_container_(NULL),
contents_layout_manager_(NULL),
download_shelf_(NULL),
immersive_mode_controller_(NULL),
dialog_host_(new WebContentsModalDialogHostViews(this)),
web_contents_modal_dialog_top_y_(-1) {}
BrowserViewLayout::~BrowserViewLayout() {
}
void BrowserViewLayout::Init(
BrowserViewLayoutDelegate* delegate,
Browser* browser,
views::ClientView* browser_view,
views::View* top_container,
TabStrip* tab_strip,
views::View* toolbar,
InfoBarContainerView* infobar_container,
views::View* contents_container,
ContentsLayoutManager* contents_layout_manager,
ImmersiveModeController* immersive_mode_controller) {
delegate_.reset(delegate);
browser_ = browser;
browser_view_ = browser_view;
top_container_ = top_container;
tab_strip_ = tab_strip;
toolbar_ = toolbar;
infobar_container_ = infobar_container;
contents_container_ = contents_container;
contents_layout_manager_ = contents_layout_manager;
immersive_mode_controller_ = immersive_mode_controller;
}
WebContentsModalDialogHost*
BrowserViewLayout::GetWebContentsModalDialogHost() {
return dialog_host_.get();
}
gfx::Size BrowserViewLayout::GetMinimumSize() {
gfx::Size tabstrip_size(
browser()->SupportsWindowFeature(Browser::FEATURE_TABSTRIP) ?
tab_strip_->GetMinimumSize() : gfx::Size());
gfx::Size toolbar_size(
(browser()->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
browser()->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) ?
toolbar_->GetMinimumSize() : gfx::Size());
if (tabstrip_size.height() && toolbar_size.height())
toolbar_size.Enlarge(0, -kToolbarTabStripVerticalOverlap);
gfx::Size bookmark_bar_size;
if (bookmark_bar_ &&
bookmark_bar_->visible() &&
browser()->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR)) {
bookmark_bar_size = bookmark_bar_->GetMinimumSize();
bookmark_bar_size.Enlarge(0, -bookmark_bar_->GetToolbarOverlap());
}
gfx::Size infobar_container_size(infobar_container_->GetMinimumSize());
gfx::Size contents_size(contents_container_->GetMinimumSize());
int min_height = delegate_->GetTopInsetInBrowserView() +
tabstrip_size.height() + toolbar_size.height() +
bookmark_bar_size.height() + infobar_container_size.height() +
contents_size.height();
int widths[] = {
tabstrip_size.width(),
toolbar_size.width(),
bookmark_bar_size.width(),
infobar_container_size.width(),
contents_size.width() };
int min_width = *std::max_element(&widths[0], &widths[arraysize(widths)]);
return gfx::Size(min_width, min_height);
}
gfx::Rect BrowserViewLayout::GetFindBarBoundingBox() const {
gfx::Rect bounding_box = contents_container_->ConvertRectToWidget(
contents_container_->GetLocalBounds());
gfx::Rect top_container_bounds = top_container_->ConvertRectToWidget(
top_container_->GetLocalBounds());
int find_bar_y = 0;
if (immersive_mode_controller_->IsEnabled() &&
!immersive_mode_controller_->IsRevealed()) {
find_bar_y = top_container_bounds.bottom();
} else {
find_bar_y = top_container_bounds.bottom() - 1;
}
int height_delta = bounding_box.y() - find_bar_y;
bounding_box.set_y(find_bar_y);
bounding_box.set_height(std::max(0, bounding_box.height() + height_delta));
int scrollbar_width = gfx::scrollbar_size();
bounding_box.set_width(std::max(0, bounding_box.width() - scrollbar_width));
if (base::i18n::IsRTL())
bounding_box.set_x(bounding_box.x() + scrollbar_width);
return bounding_box;
}
int BrowserViewLayout::NonClientHitTest(const gfx::Point& point) {
views::View* parent = browser_view_->parent();
gfx::Point point_in_browser_view_coords(point);
views::View::ConvertPointToTarget(
parent, browser_view_, &point_in_browser_view_coords);
gfx::Point test_point(point);
if (delegate_->IsTabStripVisible()) {
if (ConvertedHitTest(parent, tab_strip_, &test_point)) {
if (tab_strip_->IsPositionInWindowCaption(test_point))
return HTCAPTION;
return HTCLIENT;
}
views::Widget* widget = browser_view_->GetWidget();
if (!(widget->IsMaximized() || widget->IsFullscreen()) &&
(point_in_browser_view_coords.y() <
(tab_strip_->y() + kTabShadowSize))) {
return HTNOWHERE;
}
}
gfx::Rect bv_bounds = browser_view_->bounds();
bv_bounds.Offset(0, toolbar_->y());
bv_bounds.set_height(bv_bounds.height() - toolbar_->y());
if (bv_bounds.Contains(point))
return HTCLIENT;
views::View* window_switcher_button = delegate_->GetWindowSwitcherButton();
if (window_switcher_button && window_switcher_button->visible()) {
gfx::Point window_switcher_point(point_in_browser_view_coords);
views::View::ConvertPointToTarget(browser_view_, window_switcher_button,
&window_switcher_point);
if (window_switcher_button->HitTestPoint(window_switcher_point))
return HTCLIENT;
}
bv_bounds = browser_view_->bounds();
bv_bounds.set_height(toolbar_->y());
if (bv_bounds.Contains(point))
return HTNOWHERE;
return browser_view_->views::ClientView::NonClientHitTest(point);
}
void BrowserViewLayout::Layout(views::View* browser_view) {
vertical_layout_rect_ = browser_view->GetLocalBounds();
int top = delegate_->GetTopInsetInBrowserView();
top = LayoutTabStripRegion(top);
if (delegate_->IsTabStripVisible()) {
int x = tab_strip_->GetMirroredX() +
browser_view_->GetMirroredX() +
delegate_->GetThemeBackgroundXInset();
int y = browser_view_->y() + delegate_->GetTopInsetInBrowserView();
tab_strip_->SetBackgroundOffset(gfx::Point(x, y));
}
top = LayoutToolbar(top);
top = LayoutBookmarkAndInfoBars(top, browser_view->y());
UpdateTopContainerBounds();
int bottom = LayoutDownloadShelf(browser_view->height());
int active_top_margin = GetContentsOffsetForBookmarkBar();
contents_layout_manager_->SetActiveTopMargin(active_top_margin);
top -= active_top_margin;
LayoutContentsContainerView(top, bottom);
if (browser()->HasFindBarController()) {
browser()->GetFindBarController()->find_bar()->MoveWindowIfNecessary(
gfx::Rect(), true);
}
FullscreenExitBubbleViews* fullscreen_exit_bubble =
delegate_->GetFullscreenExitBubble();
if (fullscreen_exit_bubble)
fullscreen_exit_bubble->RepositionIfVisible();
const gfx::Rect dialog_bounds(dialog_host_->GetDialogPosition(gfx::Size()),
dialog_host_->GetMaximumDialogSize());
if (latest_dialog_bounds_ != dialog_bounds) {
latest_dialog_bounds_ = dialog_bounds;
dialog_host_->NotifyPositionRequiresUpdate();
}
}
gfx::Size BrowserViewLayout::GetPreferredSize(views::View* host) {
return gfx::Size();
}
int BrowserViewLayout::LayoutTabStripRegion(int top) {
if (!delegate_->IsTabStripVisible()) {
tab_strip_->SetVisible(false);
tab_strip_->SetBounds(0, 0, 0, 0);
return top;
}
gfx::Rect tabstrip_bounds(delegate_->GetBoundsForTabStripInBrowserView());
tab_strip_->SetVisible(true);
tab_strip_->SetBoundsRect(tabstrip_bounds);
int bottom = tabstrip_bounds.bottom();
views::View* switcher_button = delegate_->GetWindowSwitcherButton();
if (switcher_button) {
if (browser()->profile()->HasOffTheRecordProfile() &&
chrome::FindBrowserWithProfile(
browser()->profile()->GetOriginalProfile(),
browser()->host_desktop_type()) != NULL) {
switcher_button->SetVisible(true);
int width = browser_view_->width();
gfx::Size ps = switcher_button->GetPreferredSize();
if (width > ps.width()) {
switcher_button->SetBounds(width - ps.width() - kWindowSwitcherOffsetX,
0,
ps.width(),
ps.height());
}
} else {
switcher_button->SetVisible(false);
}
}
return bottom;
}
int BrowserViewLayout::LayoutToolbar(int top) {
int browser_view_width = vertical_layout_rect_.width();
bool toolbar_visible = delegate_->IsToolbarVisible();
int y = top;
y -= (toolbar_visible && delegate_->IsTabStripVisible()) ?
kToolbarTabStripVerticalOverlap : 0;
int height = toolbar_visible ? toolbar_->GetPreferredSize().height() : 0;
toolbar_->SetVisible(toolbar_visible);
toolbar_->SetBounds(vertical_layout_rect_.x(), y, browser_view_width, height);
return y + height;
}
int BrowserViewLayout::LayoutBookmarkAndInfoBars(int top, int browser_view_y) {
web_contents_modal_dialog_top_y_ =
top + browser_view_y - kConstrainedWindowOverlap;
if (bookmark_bar_) {
if (bookmark_bar_->IsDetached()) {
web_contents_modal_dialog_top_y_ =
top + browser_view_y - kConstrainedWindowOverlap;
return LayoutBookmarkBar(LayoutInfoBar(top));
}
top = std::max(toolbar_->bounds().bottom(), LayoutBookmarkBar(top));
}
return LayoutInfoBar(top);
}
int BrowserViewLayout::LayoutBookmarkBar(int top) {
int y = top;
if (!delegate_->IsBookmarkBarVisible()) {
bookmark_bar_->SetVisible(false);
bookmark_bar_->SetBounds(0, y, browser_view_->width(), 0);
return y;
}
bookmark_bar_->set_infobar_visible(InfobarVisible());
int bookmark_bar_height = bookmark_bar_->GetPreferredSize().height();
y -= bookmark_bar_->GetToolbarOverlap();
bookmark_bar_->SetBounds(vertical_layout_rect_.x(),
y,
vertical_layout_rect_.width(),
bookmark_bar_height);
bookmark_bar_->SetVisible(true);
return y + bookmark_bar_height;
}
int BrowserViewLayout::LayoutInfoBar(int top) {
if (immersive_mode_controller_->IsEnabled()) {
top = browser_view_->y();
if (!immersive_mode_controller_->ShouldHideTabIndicators())
top += TabStrip::GetImmersiveHeight();
}
infobar_container_->SetVisible(InfobarVisible());
int height;
int overlapped_top = top - infobar_container_->GetVerticalOverlap(&height);
infobar_container_->SetBounds(vertical_layout_rect_.x(),
overlapped_top,
vertical_layout_rect_.width(),
height);
return overlapped_top + height;
}
void BrowserViewLayout::LayoutContentsContainerView(int top, int bottom) {
gfx::Rect contents_container_bounds(vertical_layout_rect_.x(),
top,
vertical_layout_rect_.width(),
std::max(0, bottom - top));
contents_container_->SetBoundsRect(contents_container_bounds);
}
void BrowserViewLayout::UpdateTopContainerBounds() {
int height = 0;
for (int i = 0; i < top_container_->child_count(); ++i) {
views::View* child = top_container_->child_at(i);
if (!child->visible())
continue;
int child_bottom = child->bounds().bottom();
if (child_bottom > height)
height = child_bottom;
}
height = std::max(height, delegate_->GetTopInsetInBrowserView());
gfx::Rect top_container_bounds(vertical_layout_rect_.width(), height);
top_container_bounds.set_y(
immersive_mode_controller_->GetTopContainerVerticalOffset(
top_container_bounds.size()));
top_container_->SetBoundsRect(top_container_bounds);
}
int BrowserViewLayout::GetContentsOffsetForBookmarkBar() {
if (!bookmark_bar_ ||
!delegate_->IsBookmarkBarVisible() ||
!bookmark_bar_->IsDetached()) {
return 0;
}
return bookmark_bar_->height() -
bookmark_bar_->GetFullyDetachedToolbarOverlap();
}
int BrowserViewLayout::LayoutDownloadShelf(int bottom) {
if (delegate_->DownloadShelfNeedsLayout()) {
bool visible = browser()->SupportsWindowFeature(
Browser::FEATURE_DOWNLOADSHELF);
DCHECK(download_shelf_);
int height = visible ? download_shelf_->GetPreferredSize().height() : 0;
download_shelf_->SetVisible(visible);
download_shelf_->SetBounds(vertical_layout_rect_.x(), bottom - height,
vertical_layout_rect_.width(), height);
download_shelf_->Layout();
bottom -= height;
}
return bottom;
}
bool BrowserViewLayout::InfobarVisible() const {
views::View* infobar_container = infobar_container_;
return browser_->SupportsWindowFeature(Browser::FEATURE_INFOBAR) &&
(infobar_container->GetPreferredSize().height() != 0);
}