Update V8 to version 4.7.21.
[chromium-blink-merge.git] / ash / system / web_notification / ash_popup_alignment_delegate.cc
bloba452f18047b9af07a1336f44aa07354d2c9a29e6
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/window_tree_host_manager.h"
8 #include "ash/screen_util.h"
9 #include "ash/shelf/shelf_constants.h"
10 #include "ash/shelf/shelf_layout_manager.h"
11 #include "ash/shelf/shelf_types.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "base/i18n/rtl.h"
15 #include "ui/aura/window.h"
16 #include "ui/gfx/display.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/message_center/message_center_style.h"
20 #include "ui/message_center/views/message_popup_collection.h"
22 namespace ash {
24 namespace {
26 const int kToastMarginX = 3;
28 // If there should be no margin for the first item, this value needs to be
29 // substracted to flush the message to the shelf (the width of the border +
30 // shadow).
31 const int kNoToastMarginBorderAndShadowOffset = 2;
35 AshPopupAlignmentDelegate::AshPopupAlignmentDelegate()
36 : screen_(NULL), root_window_(NULL), shelf_(NULL), system_tray_height_(0) {
39 AshPopupAlignmentDelegate::~AshPopupAlignmentDelegate() {
40 if (screen_)
41 screen_->RemoveObserver(this);
42 Shell::GetInstance()->RemoveShellObserver(this);
43 if (shelf_)
44 shelf_->RemoveObserver(this);
47 void AshPopupAlignmentDelegate::StartObserving(gfx::Screen* screen,
48 const gfx::Display& display) {
49 screen_ = screen;
50 work_area_ = display.work_area();
51 root_window_ = ash::Shell::GetInstance()
52 ->window_tree_host_manager()
53 ->GetRootWindowForDisplayId(display.id());
54 UpdateShelf();
55 screen->AddObserver(this);
56 Shell::GetInstance()->AddShellObserver(this);
57 if (system_tray_height_ > 0)
58 UpdateWorkArea();
61 void AshPopupAlignmentDelegate::SetSystemTrayHeight(int height) {
62 system_tray_height_ = height;
64 // If the shelf is shown during auto-hide state, the distance from the edge
65 // should be reduced by the height of shelf's shown height.
66 if (shelf_ && shelf_->visibility_state() == SHELF_AUTO_HIDE &&
67 shelf_->auto_hide_state() == SHELF_AUTO_HIDE_SHOWN) {
68 system_tray_height_ -= kShelfSize - ShelfLayoutManager::kAutoHideSize;
71 if (system_tray_height_ > 0)
72 system_tray_height_ += message_center::kMarginBetweenItems;
73 else
74 system_tray_height_ = 0;
76 if (!shelf_)
77 return;
79 DoUpdateIfPossible();
82 int AshPopupAlignmentDelegate::GetToastOriginX(
83 const gfx::Rect& toast_bounds) const {
84 // In Ash, RTL UI language mirrors the whole ash layout, so the toast
85 // widgets should be at the bottom-left instead of bottom right.
86 if (base::i18n::IsRTL())
87 return work_area_.x() + kToastMarginX;
89 if (IsFromLeft())
90 return work_area_.x() + kToastMarginX;
91 return work_area_.right() - kToastMarginX - toast_bounds.width();
94 int AshPopupAlignmentDelegate::GetBaseLine() const {
95 return IsTopDown()
96 ? work_area_.y() + kNoToastMarginBorderAndShadowOffset +
97 system_tray_height_
98 : work_area_.bottom() - kNoToastMarginBorderAndShadowOffset -
99 system_tray_height_;
102 int AshPopupAlignmentDelegate::GetWorkAreaBottom() const {
103 return work_area_.bottom() - system_tray_height_;
106 bool AshPopupAlignmentDelegate::IsTopDown() const {
107 return GetAlignment() == SHELF_ALIGNMENT_TOP;
110 bool AshPopupAlignmentDelegate::IsFromLeft() const {
111 return GetAlignment() == SHELF_ALIGNMENT_LEFT;
114 void AshPopupAlignmentDelegate::RecomputeAlignment(
115 const gfx::Display& display) {
116 // Nothing needs to be done.
119 ShelfAlignment AshPopupAlignmentDelegate::GetAlignment() const {
120 return shelf_ ? shelf_->GetAlignment() : SHELF_ALIGNMENT_BOTTOM;
123 void AshPopupAlignmentDelegate::UpdateShelf() {
124 if (shelf_)
125 return;
127 shelf_ = ShelfLayoutManager::ForShelf(root_window_);
128 if (shelf_)
129 shelf_->AddObserver(this);
132 gfx::Display AshPopupAlignmentDelegate::GetCurrentDisplay() const {
133 return Shell::GetScreen()->GetDisplayNearestWindow(
134 shelf_->shelf_widget()->GetNativeView());
137 void AshPopupAlignmentDelegate::UpdateWorkArea() {
138 work_area_ = shelf_->user_work_area_bounds();
139 DoUpdateIfPossible();
142 void AshPopupAlignmentDelegate::OnDisplayWorkAreaInsetsChanged() {
143 UpdateShelf();
144 UpdateWorkArea();
147 void AshPopupAlignmentDelegate::WillChangeVisibilityState(
148 ShelfVisibilityState new_state) {
149 UpdateWorkArea();
152 void AshPopupAlignmentDelegate::OnAutoHideStateChanged(
153 ShelfAutoHideState new_state) {
154 UpdateWorkArea();
157 void AshPopupAlignmentDelegate::OnDisplayAdded(
158 const gfx::Display& new_display) {
161 void AshPopupAlignmentDelegate::OnDisplayRemoved(
162 const gfx::Display& old_display) {
165 void AshPopupAlignmentDelegate::OnDisplayMetricsChanged(
166 const gfx::Display& display,
167 uint32_t metrics) {
168 UpdateShelf();
169 if (shelf_ && GetCurrentDisplay().id() == display.id())
170 UpdateWorkArea();
173 } // namespace ash