Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / system / web_notification / ash_popup_alignment_delegate.cc
blob96ffc795396cf4012729cf0d80a5998fdd3ee688
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/system/web_notification/ash_popup_alignment_delegate.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/shelf/shelf_constants.h"
9 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shelf/shelf_types.h"
11 #include "ash/shelf/shelf_widget.h"
12 #include "ash/shell.h"
13 #include "base/i18n/rtl.h"
14 #include "ui/aura/window.h"
15 #include "ui/gfx/display.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/message_center/message_center_style.h"
19 #include "ui/message_center/views/message_popup_collection.h"
21 namespace ash {
23 namespace {
25 const int kToastMarginX = 3;
27 // If there should be no margin for the first item, this value needs to be
28 // substracted to flush the message to the shelf (the width of the border +
29 // shadow).
30 const int kNoToastMarginBorderAndShadowOffset = 2;
34 AshPopupAlignmentDelegate::AshPopupAlignmentDelegate()
35 : screen_(NULL), root_window_(NULL), shelf_(NULL), system_tray_height_(0) {
38 AshPopupAlignmentDelegate::~AshPopupAlignmentDelegate() {
39 if (screen_)
40 screen_->RemoveObserver(this);
41 Shell::GetInstance()->RemoveShellObserver(this);
42 if (shelf_)
43 shelf_->RemoveObserver(this);
46 void AshPopupAlignmentDelegate::StartObserving(gfx::Screen* screen,
47 const gfx::Display& display) {
48 screen_ = screen;
49 work_area_ = display.work_area();
50 root_window_ = ash::Shell::GetInstance()
51 ->display_controller()
52 ->GetRootWindowForDisplayId(display.id());
53 UpdateShelf();
54 screen->AddObserver(this);
55 Shell::GetInstance()->AddShellObserver(this);
56 if (system_tray_height_ > 0)
57 UpdateWorkArea(display, shelf_->auto_hide_state());
60 void AshPopupAlignmentDelegate::SetSystemTrayHeight(int height) {
61 system_tray_height_ = height;
63 // If the shelf is shown during auto-hide state, the distance from the edge
64 // should be reduced by the height of shelf's shown height.
65 if (shelf_ && shelf_->visibility_state() == SHELF_AUTO_HIDE &&
66 shelf_->auto_hide_state() == SHELF_AUTO_HIDE_SHOWN) {
67 system_tray_height_ -= kShelfSize - ShelfLayoutManager::kAutoHideSize;
70 if (system_tray_height_ > 0)
71 system_tray_height_ += message_center::kMarginBetweenItems;
72 else
73 system_tray_height_ = 0;
75 if (!shelf_)
76 return;
78 DoUpdateIfPossible();
81 int AshPopupAlignmentDelegate::GetToastOriginX(
82 const gfx::Rect& toast_bounds) const {
83 // In Ash, RTL UI language mirrors the whole ash layout, so the toast
84 // widgets should be at the bottom-left instead of bottom right.
85 if (base::i18n::IsRTL())
86 return work_area_.x() + kToastMarginX;
88 if (IsFromLeft())
89 return work_area_.x() + kToastMarginX;
90 return work_area_.right() - kToastMarginX - toast_bounds.width();
93 int AshPopupAlignmentDelegate::GetBaseLine() const {
94 return IsTopDown()
95 ? work_area_.y() + kNoToastMarginBorderAndShadowOffset +
96 system_tray_height_
97 : work_area_.bottom() - kNoToastMarginBorderAndShadowOffset -
98 system_tray_height_;
101 int AshPopupAlignmentDelegate::GetWorkAreaBottom() const {
102 return work_area_.bottom() - system_tray_height_;
105 bool AshPopupAlignmentDelegate::IsTopDown() const {
106 return GetAlignment() == SHELF_ALIGNMENT_TOP;
109 bool AshPopupAlignmentDelegate::IsFromLeft() const {
110 return GetAlignment() == SHELF_ALIGNMENT_LEFT;
113 void AshPopupAlignmentDelegate::RecomputeAlignment(
114 const gfx::Display& display) {
115 // Nothing needs to be done.
118 ShelfAlignment AshPopupAlignmentDelegate::GetAlignment() const {
119 return shelf_ ? shelf_->GetAlignment() : SHELF_ALIGNMENT_BOTTOM;
122 void AshPopupAlignmentDelegate::UpdateShelf() {
123 if (shelf_)
124 return;
126 shelf_ = ShelfLayoutManager::ForShelf(root_window_);
127 if (shelf_)
128 shelf_->AddObserver(this);
131 gfx::Display AshPopupAlignmentDelegate::GetCurrentDisplay() const {
132 return Shell::GetScreen()->GetDisplayNearestWindow(
133 shelf_->shelf_widget()->GetNativeView());
136 void AshPopupAlignmentDelegate::UpdateWorkArea(const gfx::Display& display,
137 ShelfAutoHideState new_state) {
138 work_area_ = display.work_area();
139 int width = 0;
140 if (shelf_ && (shelf_->visibility_state() == SHELF_AUTO_HIDE) &&
141 new_state == SHELF_AUTO_HIDE_SHOWN) {
142 // Since the work_area is already reduced by kAutoHideSize, the inset width
143 // should be just the difference.
144 width = kShelfSize - ShelfLayoutManager::kAutoHideSize;
146 work_area_.Inset(shelf_->SelectValueForShelfAlignment(
147 gfx::Insets(0, 0, width, 0),
148 gfx::Insets(0, width, 0, 0),
149 gfx::Insets(0, 0, 0, width),
150 gfx::Insets(width, 0, 0, 0)));
152 DoUpdateIfPossible();
155 void AshPopupAlignmentDelegate::OnDisplayWorkAreaInsetsChanged() {
156 UpdateShelf();
157 UpdateWorkArea(GetCurrentDisplay(), shelf_->auto_hide_state());
160 void AshPopupAlignmentDelegate::OnAutoHideStateChanged(
161 ShelfAutoHideState new_state) {
162 UpdateWorkArea(GetCurrentDisplay(), new_state);
165 void AshPopupAlignmentDelegate::OnDisplayAdded(
166 const gfx::Display& new_display) {
169 void AshPopupAlignmentDelegate::OnDisplayRemoved(
170 const gfx::Display& old_display) {
173 void AshPopupAlignmentDelegate::OnDisplayMetricsChanged(
174 const gfx::Display& display,
175 uint32_t metrics) {
176 UpdateShelf();
177 if (shelf_ && GetCurrentDisplay().id() == display.id())
178 UpdateWorkArea(display, shelf_->auto_hide_state());
181 } // namespace ash